[TASK] Update typo3/testing-framework to 4.10.0
[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 /**
84
85 */
86 protected function setUp()
87 {
88 $this->viewHelper = $this->getAccessibleMock(AbstractWidgetViewHelper::class, ['validateArguments', 'initialize', 'callRenderMethod', 'getWidgetConfiguration', 'getRenderingContext']);
89 $this->mockExtensionService = $this->createMock(ExtensionService::class);
90 $this->viewHelper->_set('extensionService', $this->mockExtensionService);
91 $this->ajaxWidgetContextHolder = $this->createMock(AjaxWidgetContextHolder::class);
92 $this->viewHelper->injectAjaxWidgetContextHolder($this->ajaxWidgetContextHolder);
93 $this->widgetContext = $this->createMock(WidgetContext::class);
94 $this->objectManager = $this->createMock(ObjectManagerInterface::class);
95 $this->objectManager->expects($this->at(0))->method('get')->with(WidgetContext::class)->will($this->returnValue($this->widgetContext));
96 $this->viewHelper->injectObjectManager($this->objectManager);
97 $this->request = $this->createMock(Request::class);
98 $this->controllerContext = $this->createMock(ControllerContext::class);
99 $this->controllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
100 $this->renderingContext = $this->getMockBuilder(RenderingContextFixture::class)
101 ->setMethods(['getControllerContext'])
102 ->getMock();
103 $this->renderingContext->expects($this->any())->method('getControllerContext')->willReturn($this->controllerContext);
104 $this->viewHelper->_set('renderingContext', $this->renderingContext);
105 }
106
107 /**
108 * @test
109 */
110 public function initializeArgumentsAndRenderCallsTheRightSequenceOfMethods()
111 {
112 $this->callViewHelper();
113 }
114
115 /**
116 * @test
117 */
118 public function initializeArgumentsAndRenderStoresTheWidgetContextIfInAjaxMode()
119 {
120 $this->viewHelper->_set('ajaxWidget', true);
121 $this->ajaxWidgetContextHolder->expects($this->once())->method('store')->with($this->widgetContext);
122 $this->callViewHelper();
123 }
124
125 /**
126 * Calls the ViewHelper, and emulates a rendering.
127 */
128 public function callViewHelper()
129 {
130 $mockViewHelperVariableContainer = $this->createMock(ViewHelperVariableContainer::class);
131 $mockViewHelperVariableContainer->expects($this->any())->method('get')->willReturnArgument(2);
132 $mockRenderingContext = $this->createMock(RenderingContextFixture::class);
133 $mockRenderingContext->expects($this->atLeastOnce())->method('getViewHelperVariableContainer')->will($this->returnValue($mockViewHelperVariableContainer));
134 $mockRenderingContext->expects($this->any())->method('getControllerContext')->willReturn($this->controllerContext);
135 $this->viewHelper->setRenderingContext($mockRenderingContext);
136 $this->viewHelper->expects($this->once())->method('getWidgetConfiguration')->will($this->returnValue('Some Widget Configuration'));
137 $this->widgetContext->expects($this->once())->method('setWidgetConfiguration')->with('Some Widget Configuration');
138 $this->widgetContext->expects($this->once())->method('setWidgetIdentifier')->with('@widget_0');
139 $this->viewHelper->_set('controller', new \stdClass());
140 $this->viewHelper->_set('renderingContext', $mockRenderingContext);
141 $this->widgetContext->expects($this->once())->method('setControllerObjectName')->with('stdClass');
142 $this->viewHelper->expects($this->once())->method('validateArguments');
143 $this->viewHelper->expects($this->once())->method('initialize');
144 $this->viewHelper->expects($this->once())->method('callRenderMethod')->will($this->returnValue('renderedResult'));
145 $output = $this->viewHelper->initializeArgumentsAndRender();
146 $this->assertEquals('renderedResult', $output);
147 }
148
149 /**
150 * @test
151 */
152 public function setChildNodesAddsChildNodesToWidgetContext()
153 {
154 $node1 = $this->createMock(AbstractNode::class);
155 $node2 = $this->createMock(TextNode::class);
156 $node3 = $this->createMock(AbstractNode::class);
157 $rootNode = $this->createMock(RootNode::class);
158 $rootNode->expects($this->at(0))->method('addChildNode')->with($node1);
159 $rootNode->expects($this->at(1))->method('addChildNode')->with($node2);
160 $rootNode->expects($this->at(2))->method('addChildNode')->with($node3);
161 $this->objectManager->expects($this->once())->method('get')->with(RootNode::class)->will($this->returnValue($rootNode));
162 $renderingContext = $this->createMock(RenderingContextInterface::class);
163 $this->viewHelper->_set('renderingContext', $renderingContext);
164 $this->widgetContext->expects($this->once())->method('setViewHelperChildNodes')->with($rootNode, $renderingContext);
165 $this->viewHelper->setChildNodes([$node1, $node2, $node3]);
166 }
167
168 /**
169 * @test
170 */
171 public function initiateSubRequestThrowsExceptionIfControllerIsNoWidgetController()
172 {
173 $controller = $this->createMock(AbstractWidgetViewHelper::class);
174
175 $this->expectException(MissingControllerException::class);
176 $this->expectExceptionCode(1289422564);
177
178 $this->viewHelper->_set('controller', $controller);
179 $this->viewHelper->_call('initiateSubRequest');
180 }
181
182 /**
183 * @test
184 */
185 public function initiateSubRequestBuildsRequestProperly()
186 {
187 $controller = $this->createMock(AbstractWidgetController::class);
188 $this->viewHelper->_set('controller', $controller);
189 // Initial Setup
190 $widgetRequest = $this->createMock(WidgetRequest::class);
191 $response = $this->createMock(Response::class);
192 $this->objectManager->expects($this->at(0))->method('get')->with(WidgetRequest::class)->will($this->returnValue($widgetRequest));
193 $this->objectManager->expects($this->at(1))->method('get')->with(Response::class)->will($this->returnValue($response));
194 // Widget Context is set
195 $widgetRequest->expects($this->once())->method('setWidgetContext')->with($this->widgetContext);
196 // The namespaced arguments are passed to the sub-request
197 // and the action name is exctracted from the namespace.
198 $this->controllerContext->expects($this->once())->method('getRequest')->will($this->returnValue($this->request));
199 $this->widgetContext->expects($this->once())->method('getWidgetIdentifier')->will($this->returnValue('widget-1'));
200 $this->request->expects($this->once())->method('getArguments')->will($this->returnValue([
201 'k1' => 'k2',
202 'widget-1' => [
203 'arg1' => 'val1',
204 'arg2' => 'val2',
205 'action' => 'myAction'
206 ]
207 ]));
208 $widgetRequest->expects($this->once())->method('setArguments')->with([
209 'arg1' => 'val1',
210 'arg2' => 'val2'
211 ]);
212 $widgetRequest->expects($this->once())->method('setControllerActionName')->with('myAction');
213 // Controller is called
214 $controller->expects($this->once())->method('processRequest')->with($widgetRequest, $response);
215 $output = $this->viewHelper->_call('initiateSubRequest');
216 // SubResponse is returned
217 $this->assertSame($response, $output);
218 }
219
220 /**
221 * @test
222 */
223 public function getWidgetConfigurationReturnsArgumentsProperty()
224 {
225 $viewHelper = $this->getMockBuilder(AbstractWidgetViewHelper::class)
226 ->setMethods(['dummy'])
227 ->getMock();
228 $viewHelper->setArguments(['foo' => 'bar']);
229 $this->assertEquals(['foo' => 'bar'], $this->callInaccessibleMethod($viewHelper, 'getWidgetConfiguration'));
230 }
231
232 /**
233 * @test
234 */
235 public function compileDisablesTemplateCompiler()
236 {
237 $viewHelper = $this->getMockBuilder(AbstractWidgetViewHelper::class)
238 ->setMethods(['dummy'])
239 ->getMock();
240 $node = $this->getMockBuilder(ViewHelperNode::class)
241 ->setMethods(['dummy'])
242 ->disableOriginalConstructor()
243 ->getMock();
244 $compiler = $this->getMockBuilder(TemplateCompiler::class)
245 ->setMethods(['disable'])
246 ->getMock();
247 $compiler->expects($this->once())->method('disable');
248 $code = ''; // referenced
249 $result = $viewHelper->compile('', '', $code, $node, $compiler);
250 $this->assertEquals('\'\'', $result);
251 $this->assertEquals('', $code);
252 }
253 }