TYPO3 CMS  TYPO3_7-6
AbstractWidgetViewHelperTest.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is backported from the FLOW3 package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * *
12  * This script is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15  * General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU Lesser General Public *
18  * License along with the script. *
19  * If not, see http://www.gnu.org/licenses/lgpl.html *
20  * *
21  * The TYPO3 project - inspiring people to share! *
22  * */
23 
28 {
32  protected $viewHelper;
33 
38 
42  protected $widgetContext;
43 
47  protected $objectManager;
48 
52  protected $controllerContext;
53 
57  protected $request;
58 
63 
67  protected function setUp()
68  {
69  $this->viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper::class, ['validateArguments', 'initialize', 'callRenderMethod', 'getWidgetConfiguration', 'getRenderingContext']);
70  $this->mockExtensionService = $this->getMock(\TYPO3\CMS\Extbase\Service\ExtensionService::class);
71  $this->viewHelper->_set('extensionService', $this->mockExtensionService);
72  $this->ajaxWidgetContextHolder = $this->getMock(\TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder::class, [], [], '', false);
73  $this->viewHelper->injectAjaxWidgetContextHolder($this->ajaxWidgetContextHolder);
74  $this->widgetContext = $this->getMock(\TYPO3\CMS\Fluid\Core\Widget\WidgetContext::class);
75  $this->objectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
76  $this->objectManager->expects($this->at(0))->method('get')->with(\TYPO3\CMS\Fluid\Core\Widget\WidgetContext::class)->will($this->returnValue($this->widgetContext));
77  $this->viewHelper->injectObjectManager($this->objectManager);
78  $this->request = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Web\Request::class);
79  $this->controllerContext = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext::class, [], [], '', false);
80  $this->controllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
81  $this->viewHelper->_set('controllerContext', $this->controllerContext);
82  }
83 
88  {
89  $this->callViewHelper();
90  }
91 
96  {
97  $this->viewHelper->_set('ajaxWidget', true);
98  $this->ajaxWidgetContextHolder->expects($this->once())->method('store')->with($this->widgetContext);
99  $this->callViewHelper();
100  }
101 
107  public function callViewHelper()
108  {
109  $mockViewHelperVariableContainer = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\ViewHelperVariableContainer::class);
110  $mockRenderingContext = $this->getMock(\TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface::class);
111  $mockRenderingContext->expects($this->atLeastOnce())->method('getViewHelperVariableContainer')->will($this->returnValue($mockViewHelperVariableContainer));
112  $this->viewHelper->setRenderingContext($mockRenderingContext);
113  $this->viewHelper->expects($this->once())->method('getWidgetConfiguration')->will($this->returnValue('Some Widget Configuration'));
114  $this->widgetContext->expects($this->once())->method('setWidgetConfiguration')->with('Some Widget Configuration');
115  $this->widgetContext->expects($this->once())->method('setWidgetIdentifier')->with('@widget_0');
116  $this->viewHelper->_set('controller', new \stdClass());
117  $this->widgetContext->expects($this->once())->method('setControllerObjectName')->with('stdClass');
118  $this->viewHelper->expects($this->once())->method('validateArguments');
119  $this->viewHelper->expects($this->once())->method('initialize');
120  $this->viewHelper->expects($this->once())->method('callRenderMethod')->will($this->returnValue('renderedResult'));
121  $output = $this->viewHelper->initializeArgumentsAndRender();
122  $this->assertEquals('renderedResult', $output);
123  }
124 
129  {
130  $node1 = $this->getMock(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode::class);
131  $node2 = $this->getMock(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\TextNode::class, [], [], '', false);
132  $node3 = $this->getMock(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode::class);
133  $rootNode = $this->getMock(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\RootNode::class);
134  $rootNode->expects($this->at(0))->method('addChildNode')->with($node1);
135  $rootNode->expects($this->at(1))->method('addChildNode')->with($node2);
136  $rootNode->expects($this->at(2))->method('addChildNode')->with($node3);
137  $this->objectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\RootNode::class)->will($this->returnValue($rootNode));
138  $renderingContext = $this->getMock(\TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface::class);
139  $this->viewHelper->_set('renderingContext', $renderingContext);
140  $this->widgetContext->expects($this->once())->method('setViewHelperChildNodes')->with($rootNode, $renderingContext);
141  $this->viewHelper->setChildNodes([$node1, $node2, $node3]);
142  }
143 
149  {
150  $controller = $this->getMock('Tx_Fluid_MVC_Controller_ControllerInterface');
151  $this->viewHelper->_set('controller', $controller);
152  $this->viewHelper->_call('initiateSubRequest');
153  }
154 
159  {
160  $controller = $this->getMock(\TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController::class, [], [], '', false);
161  $this->viewHelper->_set('controller', $controller);
162  // Initial Setup
163  $widgetRequest = $this->getMock(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class);
164  $response = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Web\Response::class);
165  $this->objectManager->expects($this->at(0))->method('get')->with(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class)->will($this->returnValue($widgetRequest));
166  $this->objectManager->expects($this->at(1))->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Web\Response::class)->will($this->returnValue($response));
167  // Widget Context is set
168  $widgetRequest->expects($this->once())->method('setWidgetContext')->with($this->widgetContext);
169  // The namespaced arguments are passed to the sub-request
170  // and the action name is exctracted from the namespace.
171  $this->controllerContext->expects($this->once())->method('getRequest')->will($this->returnValue($this->request));
172  $this->widgetContext->expects($this->once())->method('getWidgetIdentifier')->will($this->returnValue('widget-1'));
173  $this->request->expects($this->once())->method('getArguments')->will($this->returnValue([
174  'k1' => 'k2',
175  'widget-1' => [
176  'arg1' => 'val1',
177  'arg2' => 'val2',
178  'action' => 'myAction'
179  ]
180  ]));
181  $widgetRequest->expects($this->once())->method('setArguments')->with([
182  'arg1' => 'val1',
183  'arg2' => 'val2'
184  ]);
185  $widgetRequest->expects($this->once())->method('setControllerActionName')->with('myAction');
186  // Controller is called
187  $controller->expects($this->once())->method('processRequest')->with($widgetRequest, $response);
188  $output = $this->viewHelper->_call('initiateSubRequest');
189  // SubResponse is returned
190  $this->assertSame($response, $output);
191  }
192 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)