8a04baac1337ddcd473fd9cce0aadaf06062614b
[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 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 /**
24 * Testcase for AbstractWidgetViewHelper
25 */
26 class AbstractWidgetViewHelperTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
27
28 /**
29 * @var \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper
30 */
31 protected $viewHelper;
32
33 /**
34 * @var \TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder
35 */
36 protected $ajaxWidgetContextHolder;
37
38 /**
39 * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
40 */
41 protected $widgetContext;
42
43 /**
44 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
45 */
46 protected $objectManager;
47
48 /**
49 * @var \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext
50 */
51 protected $controllerContext;
52
53 /**
54 * @var \TYPO3\CMS\Extbase\Mvc\Web\Request
55 */
56 protected $request;
57
58 /**
59 * @var \TYPO3\CMS\Extbase\Service\ExtensionService
60 */
61 protected $mockExtensionService;
62
63 /**
64
65 */
66 public function setUp() {
67 $this->viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\Core\\Widget\\AbstractWidgetViewHelper', array('validateArguments', 'initialize', 'callRenderMethod', 'getWidgetConfiguration', 'getRenderingContext'));
68 $this->mockExtensionService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\ExtensionService');
69 $this->viewHelper->_set('extensionService', $this->mockExtensionService);
70 $this->ajaxWidgetContextHolder = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Widget\\AjaxWidgetContextHolder');
71 $this->viewHelper->injectAjaxWidgetContextHolder($this->ajaxWidgetContextHolder);
72 $this->widgetContext = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Widget\\WidgetContext');
73 $this->objectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
74 $this->objectManager->expects($this->at(0))->method('get')->with('TYPO3\\CMS\\Fluid\\Core\\Widget\\WidgetContext')->will($this->returnValue($this->widgetContext));
75 $this->viewHelper->injectObjectManager($this->objectManager);
76 $this->request = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request');
77 $this->controllerContext = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerContext', array(), array(), '', FALSE);
78 $this->controllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
79 $this->viewHelper->_set('controllerContext', $this->controllerContext);
80 }
81
82 /**
83 * @test
84 */
85 public function initializeArgumentsAndRenderCallsTheRightSequenceOfMethods() {
86 $this->callViewHelper();
87 }
88
89 /**
90 * @test
91 */
92 public function initializeArgumentsAndRenderStoresTheWidgetContextIfInAjaxMode() {
93 $this->viewHelper->_set('ajaxWidget', TRUE);
94 $this->ajaxWidgetContextHolder->expects($this->once())->method('store')->with($this->widgetContext);
95 $this->callViewHelper();
96 }
97
98 /**
99 * Calls the ViewHelper, and emulates a rendering.
100 *
101 * @return void
102 */
103 public function callViewHelper() {
104 $mockViewHelperVariableContainer = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\ViewHelper\\ViewHelperVariableContainer');
105 $mockRenderingContext = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Rendering\\RenderingContextInterface');
106 $mockRenderingContext->expects($this->atLeastOnce())->method('getViewHelperVariableContainer')->will($this->returnValue($mockViewHelperVariableContainer));
107 $this->viewHelper->setRenderingContext($mockRenderingContext);
108 $this->viewHelper->expects($this->once())->method('getWidgetConfiguration')->will($this->returnValue('Some Widget Configuration'));
109 $this->widgetContext->expects($this->once())->method('setWidgetConfiguration')->with('Some Widget Configuration');
110 $this->widgetContext->expects($this->once())->method('setWidgetIdentifier')->with('@widget_0');
111 $this->viewHelper->_set('controller', new \stdClass());
112 $this->widgetContext->expects($this->once())->method('setControllerObjectName')->with('stdClass');
113 $this->viewHelper->expects($this->once())->method('validateArguments');
114 $this->viewHelper->expects($this->once())->method('initialize');
115 $this->viewHelper->expects($this->once())->method('callRenderMethod')->will($this->returnValue('renderedResult'));
116 $output = $this->viewHelper->initializeArgumentsAndRender();
117 $this->assertEquals('renderedResult', $output);
118 }
119
120 /**
121 * @test
122 */
123 public function setChildNodesAddsChildNodesToWidgetContext() {
124 $node1 = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Parser\\SyntaxTree\\AbstractNode');
125 $node2 = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Parser\\SyntaxTree\\TextNode', array(), array(), '', FALSE);
126 $node3 = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Parser\\SyntaxTree\\AbstractNode');
127 $rootNode = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Parser\\SyntaxTree\\RootNode');
128 $rootNode->expects($this->at(0))->method('addChildNode')->with($node1);
129 $rootNode->expects($this->at(1))->method('addChildNode')->with($node2);
130 $rootNode->expects($this->at(2))->method('addChildNode')->with($node3);
131 $this->objectManager->expects($this->once())->method('get')->with('TYPO3\\CMS\\Fluid\\Core\\Parser\\SyntaxTree\\RootNode')->will($this->returnValue($rootNode));
132 $renderingContext = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Rendering\\RenderingContextInterface');
133 $this->viewHelper->_set('renderingContext', $renderingContext);
134 $this->widgetContext->expects($this->once())->method('setViewHelperChildNodes')->with($rootNode, $renderingContext);
135 $this->viewHelper->setChildNodes(array($node1, $node2, $node3));
136 }
137
138 /**
139 * @test
140 * @expectedException \TYPO3\CMS\Fluid\Core\Widget\Exception\MissingControllerException
141 */
142 public function initiateSubRequestThrowsExceptionIfControllerIsNoWidgetController() {
143 $controller = $this->getMock('Tx_Fluid_MVC_Controller_ControllerInterface');
144 $this->viewHelper->_set('controller', $controller);
145 $this->viewHelper->_call('initiateSubRequest');
146 }
147
148 /**
149 * @test
150 */
151 public function initiateSubRequestBuildsRequestProperly() {
152 $controller = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Widget\\AbstractWidgetController', array(), array(), '', FALSE);
153 $this->viewHelper->_set('controller', $controller);
154 // Initial Setup
155 $widgetRequest = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Widget\\WidgetRequest');
156 $response = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Response');
157 $this->objectManager->expects($this->at(0))->method('get')->with('TYPO3\\CMS\\Fluid\\Core\\Widget\\WidgetRequest')->will($this->returnValue($widgetRequest));
158 $this->objectManager->expects($this->at(1))->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Response')->will($this->returnValue($response));
159 // Widget Context is set
160 $widgetRequest->expects($this->once())->method('setWidgetContext')->with($this->widgetContext);
161 // The namespaced arguments are passed to the sub-request
162 // and the action name is exctracted from the namespace.
163 $this->controllerContext->expects($this->once())->method('getRequest')->will($this->returnValue($this->request));
164 $this->widgetContext->expects($this->once())->method('getWidgetIdentifier')->will($this->returnValue('widget-1'));
165 $this->request->expects($this->once())->method('getArguments')->will($this->returnValue(array(
166 'k1' => 'k2',
167 'widget-1' => array(
168 'arg1' => 'val1',
169 'arg2' => 'val2',
170 'action' => 'myAction'
171 )
172 )));
173 $widgetRequest->expects($this->once())->method('setArguments')->with(array(
174 'arg1' => 'val1',
175 'arg2' => 'val2'
176 ));
177 $widgetRequest->expects($this->once())->method('setControllerActionName')->with('myAction');
178 // Controller is called
179 $controller->expects($this->once())->method('processRequest')->with($widgetRequest, $response);
180 $output = $this->viewHelper->_call('initiateSubRequest');
181 // SubResponse is returned
182 $this->assertSame($response, $output);
183 }
184 }