[BUGFIX] Avoid overwriting page context in TSFE->cObj by f:cObject
[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\CMS\Frontend\Controller\TypoScriptFrontendController;
25 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
26
27 /**
28 * Test case
29 */
30 class CObjectViewHelperTest extends ViewHelperBaseTestcase
31 {
32 /**
33 * @var bool Reset singletons created by subject
34 */
35 protected $resetSingletonInstances = true;
36
37 /**
38 * @var CObjectViewHelper
39 */
40 protected $viewHelper;
41
42 /**
43 * @var ConfigurationManagerInterface
44 */
45 protected $configurationManager;
46
47 /**
48 * @var ContentObjectRenderer
49 */
50 protected $contentObjectRenderer;
51
52 /**
53 * Set up the fixture
54 */
55 protected function setUp()
56 {
57 parent::setUp();
58 $this->viewHelper = new CObjectViewHelper();
59 $this->injectDependenciesIntoViewHelper($this->viewHelper);
60
61 $this->configurationManager = $this->prophesize(ConfigurationManagerInterface::class);
62 $this->contentObjectRenderer = $this->prophesize(ContentObjectRenderer::class);
63 }
64
65 /**
66 * @test
67 */
68 public function viewHelperAcceptsDataParameterAsInput()
69 {
70 $this->stubBaseDependencies();
71
72 $this->setArgumentsUnderTest(
73 $this->viewHelper,
74 [
75 'typoscriptObjectPath' => 'test',
76 'data' => 'foo',
77 ]
78 );
79 $configArray = [
80 'test' => 'TEXT',
81 'test.' => [],
82 ];
83 $this->configurationManager->getConfiguration(Argument::any())->willReturn($configArray);
84
85 $this->contentObjectRenderer->start(['foo'], '')->willReturn();
86 $this->viewHelper->initializeArgumentsAndRender();
87 }
88
89 /**
90 * @test
91 */
92 public function viewHelperAcceptsChildrenClosureAsInput()
93 {
94 $this->stubBaseDependencies();
95
96 $this->viewHelper->setRenderChildrenClosure(
97 function () {
98 return 'foo';
99 }
100 );
101 $this->setArgumentsUnderTest(
102 $this->viewHelper,
103 [
104 'typoscriptObjectPath' => 'test',
105 ]
106 );
107 $configArray = [
108 'test' => 'TEXT',
109 'test.' => [],
110 ];
111 $this->configurationManager->getConfiguration(Argument::any())->willReturn($configArray);
112
113 $this->contentObjectRenderer->start(['foo'], '')->willReturn();
114 $this->viewHelper->initializeArgumentsAndRender();
115 }
116
117 public function renderThrowsExceptionIfTypoScriptObjectPathDoesNotExistDataProvider(): array
118 {
119 return [
120 'Single path' => [
121 'test',
122 1540246570
123 ],
124 'Multi path' => [
125 'test.path',
126 1253191023
127 ],
128 ];
129 }
130
131 /**
132 * @test
133 * @dataProvider renderThrowsExceptionIfTypoScriptObjectPathDoesNotExistDataProvider
134 */
135 public function renderThrowsExceptionIfTypoScriptObjectPathDoesNotExist(string $objectPath, int $exceptionCode)
136 {
137 $this->stubBaseDependencies();
138 $this->contentObjectRenderer->start(Argument::cetera())->willReturn();
139
140 $this->setArgumentsUnderTest(
141 $this->viewHelper,
142 [
143 'data' => 'foo',
144 'typoscriptObjectPath' => $objectPath,
145 ]
146 );
147
148 $this->expectException(\TYPO3\CMS\Fluid\Core\ViewHelper\Exception::class);
149 $this->expectExceptionCode($exceptionCode);
150 $this->viewHelper->initializeArgumentsAndRender();
151 }
152
153 public function renderReturnsSimpleTypoScriptValueDataProvider(): array
154 {
155 $subConfigArray = [
156 'value' => 'Hello World',
157 'wrap' => 'ab | cd',
158 ];
159 return [
160 'Single path' => [
161 'test',
162 [
163 'test' => 'TEXT',
164 'test.' => $subConfigArray,
165 ],
166 $subConfigArray
167 ],
168 'Single path no config' => [
169 'test',
170 [
171 'test' => 'TEXT',
172 ],
173 []
174 ],
175 'Multi path' => [
176 'plugin.test',
177 [
178 'plugin.' => [
179 'test' => 'TEXT',
180 'test.' => $subConfigArray,
181 ]
182 ],
183 $subConfigArray
184 ],
185 'Multi path no config' => [
186 'plugin.test',
187 [
188 'plugin.' => [
189 'test' => 'TEXT',
190 ]
191 ],
192 []
193 ],
194 ];
195 }
196
197 /**
198 * @test
199 * @dataProvider renderReturnsSimpleTypoScriptValueDataProvider
200 * @param string $objectPath
201 * @param array $configArray
202 * @param array $subConfigArray
203 */
204 public function renderReturnsSimpleTypoScriptValue(string $objectPath, array $configArray, array $subConfigArray)
205 {
206 $this->stubBaseDependencies();
207 $this->setArgumentsUnderTest(
208 $this->viewHelper,
209 [
210 'typoscriptObjectPath' => $objectPath,
211 'data' => 'foo',
212 'table' => 'table',
213 ]
214 );
215
216 $this->configurationManager->getConfiguration(Argument::any())->willReturn($configArray);
217
218 $this->contentObjectRenderer->start(['foo'], 'table')->willReturn();
219 $this->contentObjectRenderer->setCurrentVal('foo')->willReturn();
220 $this->contentObjectRenderer->cObjGetSingle('TEXT', $subConfigArray)->willReturn('Hello World');
221
222 $objectManager = $this->prophesize(ObjectManager::class);
223 $objectManager->get(ConfigurationManagerInterface::class)->willReturn($this->configurationManager->reveal());
224 GeneralUtility::addInstance(ContentObjectRenderer::class, $this->contentObjectRenderer->reveal());
225 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());
226
227 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
228 $expectedResult = 'Hello World';
229 $this->assertSame($expectedResult, $actualResult);
230 }
231
232 /**
233 * Stubs base dependencies
234 */
235 protected function stubBaseDependencies()
236 {
237 $this->configurationManager->getConfiguration(Argument::any())->willReturn([]);
238 $this->contentObjectRenderer->setCurrentVal(Argument::cetera())->willReturn();
239 $this->contentObjectRenderer->cObjGetSingle(Argument::cetera())->willReturn('');
240 $objectManager = $this->prophesize(ObjectManager::class);
241 $objectManager->get(ConfigurationManagerInterface::class)->willReturn($this->configurationManager->reveal());
242 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());
243 $GLOBALS['TSFE'] = $this->getAccessibleMock(TypoScriptFrontendController::class, ['initCaches'], [], '', false);
244 }
245 }