[TASK] Make CObjectViewHelperTest notice free
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / CObjectViewHelperTest.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Prophecy\Argument;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
21 use TYPO3\CMS\Extbase\Object\ObjectManager;
22 use TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper;
23 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
24 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
25
26 /**
27 * Class CObjectViewHelperTest
28 */
29 class CObjectViewHelperTest extends ViewHelperBaseTestcase
30 {
31 /**
32 * @var CObjectViewHelper
33 */
34 protected $viewHelper;
35
36 /**
37 * @var ConfigurationManagerInterface
38 */
39 protected $configurationManager;
40
41 /**
42 * @var ContentObjectRenderer
43 */
44 protected $contentObjectRenderer;
45
46 /**
47 * Set up the fixture
48 */
49 protected function setUp()
50 {
51 parent::setUp();
52 $this->viewHelper = new CObjectViewHelper();
53 $this->injectDependenciesIntoViewHelper($this->viewHelper);
54
55 $this->configurationManager = $this->prophesize(ConfigurationManagerInterface::class);
56 $this->contentObjectRenderer = $this->prophesize(ContentObjectRenderer::class);
57 }
58
59 /**
60 * @test
61 */
62 public function viewHelperAcceptsDataParameterAsInput()
63 {
64 $this->stubBaseDependencies();
65
66 $this->setArgumentsUnderTest(
67 $this->viewHelper,
68 [
69 'data' => 'foo',
70 ]
71 );
72 $this->contentObjectRenderer->start(['foo'], '')->willReturn();
73 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
74 $this->assertSame('', $actualResult);
75 }
76
77 /**
78 * @test
79 */
80 public function viewHelperAcceptsChildrenClosureAsInput()
81 {
82 $this->stubBaseDependencies();
83
84 $this->viewHelper->setRenderChildrenClosure(
85 function () {
86 return 'foo';
87 }
88 );
89 $this->setArgumentsUnderTest(
90 $this->viewHelper,
91 []
92 );
93 $this->contentObjectRenderer->start(['foo'], '')->willReturn();
94 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
95 $this->assertSame('', $actualResult);
96 }
97
98 /**
99 * @test
100 */
101 public function renderThrowsExceptionIfTyposcriptObjectPathDoesNotExist()
102 {
103 $this->stubBaseDependencies();
104 $this->contentObjectRenderer->start(Argument::cetera())->willReturn();
105
106 $this->setArgumentsUnderTest(
107 $this->viewHelper,
108 [
109 'data' => 'foo',
110 'typoscriptObjectPath' => 'test.path',
111 ]
112 );
113
114 $this->expectException(\TYPO3\CMS\Fluid\Core\ViewHelper\Exception::class);
115 $this->expectExceptionCode(1253191023);
116 $this->viewHelper->initializeArgumentsAndRender();
117 }
118
119 /**
120 * @test
121 */
122 public function renderReturnsSimpleTyposcriptValue()
123 {
124 $this->setArgumentsUnderTest(
125 $this->viewHelper,
126 [
127 'typoscriptObjectPath' => 'plugin.test',
128 'data' => 'foo',
129 'table' => 'table',
130 ]
131 );
132
133 $subConfigArray = [
134 'value' => 'Hello World',
135 'wrap' => 'ab | cd',
136 ];
137
138 $configArray = [
139 'olugin' => 'COA',
140 'plugin.' => [
141 'test' => 'TEXT',
142 'test.' => $subConfigArray,
143
144 ]
145 ];
146
147 $this->configurationManager->getConfiguration(Argument::any())->willReturn($configArray);
148
149 $this->contentObjectRenderer->start(['foo'], 'table')->willReturn();
150 $this->contentObjectRenderer->setCurrentVal('foo')->willReturn();
151 $this->contentObjectRenderer->cObjGetSingle('TEXT', $subConfigArray)->willReturn('Hello World');
152
153 $objectManager = $this->prophesize(ObjectManager::class);
154 $objectManager->get(ConfigurationManagerInterface::class)->willReturn($this->configurationManager->reveal());
155 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());
156 $GLOBALS['TSFE'] = (object)['cObj' => $this->contentObjectRenderer->reveal()];
157
158 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
159 $expectedResult = 'Hello World';
160 $this->assertSame($expectedResult, $actualResult);
161 }
162
163 /**
164 * Stubs base dependencies
165 */
166 protected function stubBaseDependencies()
167 {
168 $this->configurationManager->getConfiguration(Argument::any())->willReturn([]);
169 $this->contentObjectRenderer->setCurrentVal(Argument::cetera())->willReturn();
170 $this->contentObjectRenderer->cObjGetSingle(Argument::cetera())->willReturn('');
171 $objectManager = $this->prophesize(ObjectManager::class);
172 $objectManager->get(ConfigurationManagerInterface::class)->willReturn($this->configurationManager->reveal());
173 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());
174 $GLOBALS['TSFE'] = (object)['cObj' => $this->contentObjectRenderer->reveal()];
175 }
176 }