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