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