[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / Core / Widget / AbstractWidgetViewHelperTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Fluid\Tests\Unit\Core\Widget;
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 use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
18 use TYPO3\CMS\Extbase\Mvc\Web\Request;
19 use TYPO3\CMS\Extbase\Mvc\Web\Response;
20 use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
21 use TYPO3\CMS\Extbase\Service\ExtensionService;
22 use TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController;
23 use TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper;
24 use TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder;
25 use TYPO3\CMS\Fluid\Core\Widget\Exception\MissingControllerException;
26 use TYPO3\CMS\Fluid\Core\Widget\WidgetContext;
27 use TYPO3\CMS\Fluid\Core\Widget\WidgetRequest;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 use TYPO3\TestingFramework\Fluid\Unit\Core\Rendering\RenderingContextFixture;
30 use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
31 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\AbstractNode;
32 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode;
33 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\TextNode;
34 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
35 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
36 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
37
38 /**
39 * Test case
40 */
41 class AbstractWidgetViewHelperTest extends UnitTestCase
42 {
43 /**
44 * @var AbstractWidgetViewHelper
45 */
46 protected $viewHelper;
47
48 /**
49 * @var AjaxWidgetContextHolder
50 */
51 protected $ajaxWidgetContextHolder;
52
53 /**
54 * @var WidgetContext
55 */
56 protected $widgetContext;
57
58 /**
59 * @var ObjectManagerInterface
60 */
61 protected $objectManager;
62
63 /**
64 * @var ControllerContext
65 */
66 protected $controllerContext;
67
68 /**
69 * @var Request
70 */
71 protected $request;
72
73 /**
74 * @var ExtensionService
75 */
76 protected $mockExtensionService;
77
78 /**
79 * @var RenderingContextFixture
80 */
81 protected $renderingContext;
82
83 protected function setUp(): void
84 {
85 parent::setUp();
86 $this->viewHelper = $this->getAccessibleMock(AbstractWidgetViewHelper::class, ['validateArguments', 'initialize', 'callRenderMethod', 'getWidgetConfiguration', 'getRenderingContext']);
87 $this->mockExtensionService = $this->createMock(ExtensionService::class);
88 $this->viewHelper->_set('extensionService', $this->mockExtensionService);
89 $this->ajaxWidgetContextHolder = $this->createMock(AjaxWidgetContextHolder::class);
90 $this->viewHelper->injectAjaxWidgetContextHolder($this->ajaxWidgetContextHolder);
91 $this->widgetContext = $this->createMock(WidgetContext::class);
92 $this->objectManager = $this->createMock(ObjectManagerInterface::class);
93 $this->objectManager->expects($this->at(0))->method('get')->with(WidgetContext::class)->will($this->returnValue($this->widgetContext));
94 $this->viewHelper->injectObjectManager($this->objectManager);
95 $this->request = $this->createMock(Request::class);
96 $this->controllerContext = $this->createMock(ControllerContext::class);
97 $this->controllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
98 $this->renderingContext = $this->getMockBuilder(RenderingContextFixture::class)
99 ->setMethods(['getControllerContext'])
100 ->getMock();
101 $this->renderingContext->expects($this->any())->method('getControllerContext')->willReturn($this->controllerContext);
102 $this->viewHelper->_set('renderingContext', $this->renderingContext);
103 }
104
105 /**
106 * @test
107 */
108 public function initializeArgumentsAndRenderCallsTheRightSequenceOfMethods()
109 {
110 $this->callViewHelper();
111 }
112
113 /**
114 * @test
115 */
116 public function initializeArgumentsAndRenderStoresTheWidgetContextIfInAjaxMode()
117 {
118 $this->viewHelper->_set('ajaxWidget', true);
119 $this->ajaxWidgetContextHolder->expects($this->once())->method('store')->with($this->widgetContext);
120 $this->callViewHelper();
121 }
122
123 /**
124 * Calls the ViewHelper, and emulates a rendering.
125 */
126 public function callViewHelper()
127 {
128 $mockViewHelperVariableContainer = $this->createMock(ViewHelperVariableContainer::class);
129 $mockViewHelperVariableContainer->expects($this->any())->method('get')->willReturnArgument(2);
130 $mockRenderingContext = $this->createMock(RenderingContextFixture::class);
131 $mockRenderingContext->expects($this->atLeastOnce())->method('getViewHelperVariableContainer')->will($this->returnValue($mockViewHelperVariableContainer));
132 $mockRenderingContext->expects($this->any())->method('getControllerContext')->willReturn($this->controllerContext);
133 $this->viewHelper->setRenderingContext($mockRenderingContext);
134 $this->viewHelper->expects($this->once())->method('getWidgetConfiguration')->will($this->returnValue('Some Widget Configuration'));
135 $this->widgetContext->expects($this->once())->method('setWidgetConfiguration')->with('Some Widget Configuration');
136 $this->widgetContext->expects($this->once())->method('setWidgetIdentifier')->with('@widget_0');
137 $this->viewHelper->_set('controller', new \stdClass());
138 $this->viewHelper->_set('renderingContext', $mockRenderingContext);
139 $this->widgetContext->expects($this->once())->method('setControllerObjectName')->with('stdClass');
140 $this->viewHelper->expects($this->once())->method('validateArguments');
141 $this->viewHelper->expects($this->once())->method('initialize');
142 $this->viewHelper->expects($this->once())->method('callRenderMethod')->will($this->returnValue('renderedResult'));
143 $output = $this->viewHelper->initializeArgumentsAndRender();
144 $this->assertEquals('renderedResult', $output);
145 }
146
147 /**
148 * @test
149 */
150 public function setChildNodesAddsChildNodesToWidgetContext()
151 {
152 $node1 = $this->createMock(AbstractNode::class);
153 $node2 = $this->createMock(TextNode::class);
154 $node3 = $this->createMock(AbstractNode::class);
155 $rootNode = $this->createMock(RootNode::class);
156 $rootNode->expects($this->at(0))->method('addChildNode')->with($node1);
157 $rootNode->expects($this->at(1))->method('addChildNode')->with($node2);
158 $rootNode->expects($this->at(2))->method('addChildNode')->with($node3);
159 $this->objectManager->expects($this->once())->method('get')->with(RootNode::class)->will($this->returnValue($rootNode));
160 $renderingContext = $this->createMock(RenderingContextInterface::class);
161 $this->viewHelper->_set('renderingContext', $renderingContext);
162 $this->widgetContext->expects($this->once())->method('setViewHelperChildNodes')->with($rootNode, $renderingContext);
163 $this->viewHelper->setChildNodes([$node1, $node2, $node3]);
164 }
165
166 /**
167 * @test
168 */
169 public function initiateSubRequestThrowsExceptionIfControllerIsNoWidgetController()
170 {
171 $controller = $this->createMock(AbstractWidgetViewHelper::class);
172
173 $this->expectException(MissingControllerException::class);
174 $this->expectExceptionCode(1289422564);
175
176 $this->viewHelper->_set('controller', $controller);
177 $this->viewHelper->_call('initiateSubRequest');
178 }
179
180 /**
181 * @test
182 */
183 public function initiateSubRequestBuildsRequestProperly()
184 {
185 $controller = $this->createMock(AbstractWidgetController::class);
186 $this->viewHelper->_set('controller', $controller);
187 // Initial Setup
188 $widgetRequest = $this->createMock(WidgetRequest::class);
189 $response = $this->createMock(Response::class);
190 $this->objectManager->expects($this->at(0))->method('get')->with(WidgetRequest::class)->will($this->returnValue($widgetRequest));
191 $this->objectManager->expects($this->at(1))->method('get')->with(Response::class)->will($this->returnValue($response));
192 // Widget Context is set
193 $widgetRequest->expects($this->once())->method('setWidgetContext')->with($this->widgetContext);
194 // The namespaced arguments are passed to the sub-request
195 // and the action name is extracted from the namespace.
196 $this->controllerContext->expects($this->once())->method('getRequest')->will($this->returnValue($this->request));
197 $this->widgetContext->expects($this->once())->method('getWidgetIdentifier')->will($this->returnValue('widget-1'));
198 $this->request->expects($this->once())->method('getArguments')->will($this->returnValue([
199 'k1' => 'k2',
200 'widget-1' => [
201 'arg1' => 'val1',
202 'arg2' => 'val2',
203 'action' => 'myAction'
204 ]
205 ]));
206 $widgetRequest->expects($this->once())->method('setArguments')->with([
207 'arg1' => 'val1',
208 'arg2' => 'val2'
209 ]);
210 $widgetRequest->expects($this->once())->method('setControllerActionName')->with('myAction');
211 // Controller is called
212 $controller->expects($this->once())->method('processRequest')->with($widgetRequest, $response);
213 $output = $this->viewHelper->_call('initiateSubRequest');
214 // SubResponse is returned
215 $this->assertSame($response, $output);
216 }
217
218 /**
219 * @test
220 */
221 public function getWidgetConfigurationReturnsArgumentsProperty()
222 {
223 $viewHelper = $this->getMockBuilder(AbstractWidgetViewHelper::class)
224 ->setMethods(['dummy'])
225 ->getMock();
226 $viewHelper->setArguments(['foo' => 'bar']);
227 $this->assertEquals(['foo' => 'bar'], $this->callInaccessibleMethod($viewHelper, 'getWidgetConfiguration'));
228 }
229
230 /**
231 * @test
232 */
233 public function compileDisablesTemplateCompiler()
234 {
235 $viewHelper = $this->getMockBuilder(AbstractWidgetViewHelper::class)
236 ->setMethods(['dummy'])
237 ->getMock();
238 $node = $this->getMockBuilder(ViewHelperNode::class)
239 ->setMethods(['dummy'])
240 ->disableOriginalConstructor()
241 ->getMock();
242 $compiler = $this->getMockBuilder(TemplateCompiler::class)
243 ->setMethods(['disable'])
244 ->getMock();
245 $compiler->expects($this->once())->method('disable');
246 $code = ''; // referenced
247 $result = $viewHelper->compile('', '', $code, $node, $compiler);
248 $this->assertEquals('\'\'', $result);
249 $this->assertEquals('', $code);
250 }
251 }