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