TYPO3 CMS  TYPO3_8-7
AbstractWidgetViewHelperTest.php
Go to the documentation of this file.
1 <?php
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  */
21 
25 class AbstractWidgetViewHelperTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
26 {
30  protected $viewHelper;
31 
36 
40  protected $widgetContext;
41 
45  protected $objectManager;
46 
50  protected $controllerContext;
51 
55  protected $request;
56 
61 
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 
90  {
91  $this->callViewHelper();
92  }
93 
98  {
99  $this->viewHelper->_set('ajaxWidget', true);
100  $this->ajaxWidgetContextHolder->expects($this->once())->method('store')->with($this->widgetContext);
101  $this->callViewHelper();
102  }
103 
107  public function callViewHelper()
108  {
109  $mockViewHelperVariableContainer = $this->createMock(ViewHelperVariableContainer::class);
110  $mockViewHelperVariableContainer->expects($this->any())->method('get')->willReturnArgument(2);
111  $mockRenderingContext = $this->createMock(\TYPO3\CMS\Fluid\Tests\Unit\Core\Rendering\RenderingContextFixture::class);
112  $mockRenderingContext->expects($this->atLeastOnce())->method('getViewHelperVariableContainer')->will($this->returnValue($mockViewHelperVariableContainer));
113  $mockRenderingContext->expects($this->any())->method('getControllerContext')->willReturn($this->controllerContext);
114  $this->viewHelper->setRenderingContext($mockRenderingContext);
115  $this->viewHelper->expects($this->once())->method('getWidgetConfiguration')->will($this->returnValue('Some Widget Configuration'));
116  $this->widgetContext->expects($this->once())->method('setWidgetConfiguration')->with('Some Widget Configuration');
117  $this->widgetContext->expects($this->once())->method('setWidgetIdentifier')->with('@widget_0');
118  $this->viewHelper->_set('controller', new \stdClass());
119  $this->viewHelper->_set('renderingContext', $mockRenderingContext);
120  $this->widgetContext->expects($this->once())->method('setControllerObjectName')->with('stdClass');
121  $this->viewHelper->expects($this->once())->method('validateArguments');
122  $this->viewHelper->expects($this->once())->method('initialize');
123  $this->viewHelper->expects($this->once())->method('callRenderMethod')->will($this->returnValue('renderedResult'));
124  $output = $this->viewHelper->initializeArgumentsAndRender();
125  $this->assertEquals('renderedResult', $output);
126  }
127 
132  {
133  $node1 = $this->createMock(\TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\AbstractNode::class);
134  $node2 = $this->createMock(\TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\TextNode::class);
135  $node3 = $this->createMock(\TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\AbstractNode::class);
136  $rootNode = $this->createMock(\TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode::class);
137  $rootNode->expects($this->at(0))->method('addChildNode')->with($node1);
138  $rootNode->expects($this->at(1))->method('addChildNode')->with($node2);
139  $rootNode->expects($this->at(2))->method('addChildNode')->with($node3);
140  $this->objectManager->expects($this->once())->method('get')->with(\TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode::class)->will($this->returnValue($rootNode));
141  $renderingContext = $this->createMock(\TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface::class);
142  $this->viewHelper->_set('renderingContext', $renderingContext);
143  $this->widgetContext->expects($this->once())->method('setViewHelperChildNodes')->with($rootNode, $renderingContext);
144  $this->viewHelper->setChildNodes([$node1, $node2, $node3]);
145  }
146 
151  {
152  $controller = $this->createMock(AbstractWidgetViewHelper::class);
153 
154  $this->expectException(MissingControllerException::class);
155  $this->expectExceptionCode(1289422564);
156 
157  $this->viewHelper->_set('controller', $controller);
158  $this->viewHelper->_call('initiateSubRequest');
159  }
160 
165  {
166  $controller = $this->createMock(\TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController::class);
167  $this->viewHelper->_set('controller', $controller);
168  // Initial Setup
169  $widgetRequest = $this->createMock(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class);
170  $response = $this->createMock(\TYPO3\CMS\Extbase\Mvc\Web\Response::class);
171  $this->objectManager->expects($this->at(0))->method('get')->with(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class)->will($this->returnValue($widgetRequest));
172  $this->objectManager->expects($this->at(1))->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Web\Response::class)->will($this->returnValue($response));
173  // Widget Context is set
174  $widgetRequest->expects($this->once())->method('setWidgetContext')->with($this->widgetContext);
175  // The namespaced arguments are passed to the sub-request
176  // and the action name is exctracted from the namespace.
177  $this->controllerContext->expects($this->once())->method('getRequest')->will($this->returnValue($this->request));
178  $this->widgetContext->expects($this->once())->method('getWidgetIdentifier')->will($this->returnValue('widget-1'));
179  $this->request->expects($this->once())->method('getArguments')->will($this->returnValue([
180  'k1' => 'k2',
181  'widget-1' => [
182  'arg1' => 'val1',
183  'arg2' => 'val2',
184  'action' => 'myAction'
185  ]
186  ]));
187  $widgetRequest->expects($this->once())->method('setArguments')->with([
188  'arg1' => 'val1',
189  'arg2' => 'val2'
190  ]);
191  $widgetRequest->expects($this->once())->method('setControllerActionName')->with('myAction');
192  // Controller is called
193  $controller->expects($this->once())->method('processRequest')->with($widgetRequest, $response);
194  $output = $this->viewHelper->_call('initiateSubRequest');
195  // SubResponse is returned
196  $this->assertSame($response, $output);
197  }
198 
203  {
204  $viewHelper = $this->getMockBuilder(AbstractWidgetViewHelper::class)
205  ->setMethods(['dummy'])
206  ->getMock();
207  $viewHelper->setArguments(['foo' => 'bar']);
208  $this->assertEquals(['foo' => 'bar'], $this->callInaccessibleMethod($viewHelper, 'getWidgetConfiguration'));
209  }
210 
215  {
216  $viewHelper = $this->getMockBuilder(AbstractWidgetViewHelper::class)
217  ->setMethods(['dummy'])
218  ->getMock();
219  $node = $this->getMockBuilder(ViewHelperNode::class)
220  ->setMethods(['dummy'])
221  ->disableOriginalConstructor()
222  ->getMock();
223  $compiler = $this->getMockBuilder(TemplateCompiler::class)
224  ->setMethods(['disable'])
225  ->getMock();
226  $compiler->expects($this->once())->method('disable');
227  $code = ''; // referenced
228  $result = $viewHelper->compile('', '', $code, $node, $compiler);
229  $this->assertEquals('\'\'', $result);
230  $this->assertEquals('', $code);
231  }
232 }