‪TYPO3CMS  9.5
AbstractWidgetViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
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  */
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 
41 class ‪AbstractWidgetViewHelperTest extends UnitTestCase
42 {
46  protected ‪$viewHelper;
47 
52 
56  protected ‪$widgetContext;
57 
61  protected ‪$objectManager;
62 
66  protected ‪$controllerContext;
67 
71  protected ‪$request;
72 
76  protected ‪$mockExtensionService;
77 
81  protected ‪$renderingContext;
82 
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 
111  {
112  $this->‪callViewHelper();
113  }
114 
119  {
120  $this->viewHelper->_set('ajaxWidget', true);
121  $this->ajaxWidgetContextHolder->expects($this->once())->method('store')->with($this->widgetContext);
122  $this->‪callViewHelper();
123  }
124 
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 
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 
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 
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 
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 
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 }
‪TYPO3\CMS\Fluid\Core\Widget\WidgetRequest
Definition: WidgetRequest.php:22
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest
Definition: AbstractWidgetViewHelperTest.php:42
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\getWidgetConfigurationReturnsArgumentsProperty
‪getWidgetConfigurationReturnsArgumentsProperty()
Definition: AbstractWidgetViewHelperTest.php:215
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$viewHelper
‪AbstractWidgetViewHelper $viewHelper
Definition: AbstractWidgetViewHelperTest.php:45
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\initiateSubRequestBuildsRequestProperly
‪initiateSubRequestBuildsRequestProperly()
Definition: AbstractWidgetViewHelperTest.php:177
‪TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext
Definition: ControllerContext.php:21
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$widgetContext
‪WidgetContext $widgetContext
Definition: AbstractWidgetViewHelperTest.php:53
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\callViewHelper
‪callViewHelper()
Definition: AbstractWidgetViewHelperTest.php:120
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\initializeArgumentsAndRenderStoresTheWidgetContextIfInAjaxMode
‪initializeArgumentsAndRenderStoresTheWidgetContextIfInAjaxMode()
Definition: AbstractWidgetViewHelperTest.php:110
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\compileDisablesTemplateCompiler
‪compileDisablesTemplateCompiler()
Definition: AbstractWidgetViewHelperTest.php:227
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:23
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$request
‪Request $request
Definition: AbstractWidgetViewHelperTest.php:65
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper
Definition: AbstractWidgetViewHelper.php:26
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$renderingContext
‪RenderingContextFixture $renderingContext
Definition: AbstractWidgetViewHelperTest.php:73
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\initiateSubRequestThrowsExceptionIfControllerIsNoWidgetController
‪initiateSubRequestThrowsExceptionIfControllerIsNoWidgetController()
Definition: AbstractWidgetViewHelperTest.php:163
‪TYPO3\CMS\Extbase\Mvc\Web\Response
Definition: Response.php:25
‪TYPO3\CMS\Fluid\Core\Widget\Exception\MissingControllerException
Definition: MissingControllerException.php:23
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$controllerContext
‪ControllerContext $controllerContext
Definition: AbstractWidgetViewHelperTest.php:61
‪TYPO3\CMS\Extbase\Mvc\Web\Request
Definition: Request.php:21
‪$output
‪$output
Definition: annotationChecker.php:113
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget
Definition: AbstractWidgetControllerTest.php:2
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\compile
‪string compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
Definition: AbstractWidgetViewHelper.php:265
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\setChildNodesAddsChildNodesToWidgetContext
‪setChildNodesAddsChildNodesToWidgetContext()
Definition: AbstractWidgetViewHelperTest.php:144
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder
Definition: AjaxWidgetContextHolder.php:29
‪TYPO3\CMS\Extbase\Service\ExtensionService
Definition: ExtensionService.php:29
‪TYPO3\CMS\Fluid\Core\Widget\WidgetContext
Definition: WidgetContext.php:29
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\initializeArgumentsAndRenderCallsTheRightSequenceOfMethods
‪initializeArgumentsAndRenderCallsTheRightSequenceOfMethods()
Definition: AbstractWidgetViewHelperTest.php:102
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$objectManager
‪ObjectManagerInterface $objectManager
Definition: AbstractWidgetViewHelperTest.php:57
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\setUp
‪setUp()
Definition: AbstractWidgetViewHelperTest.php:78
‪TYPO3\CMS\Extbase\Mvc\Request
Definition: Request.php:23
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController
Definition: AbstractWidgetController.php:25
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$mockExtensionService
‪ExtensionService $mockExtensionService
Definition: AbstractWidgetViewHelperTest.php:69
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$ajaxWidgetContextHolder
‪AjaxWidgetContextHolder $ajaxWidgetContextHolder
Definition: AbstractWidgetViewHelperTest.php:49