‪TYPO3CMS  10.4
AbstractWidgetViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 
3 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  */
17 
19 
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33 use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
34 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\AbstractNode;
35 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode;
36 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\TextNode;
37 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
38 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
39 
43 class ‪AbstractWidgetViewHelperTest extends UnitTestCase
44 {
48  protected ‪$viewHelper;
49 
54 
58  protected ‪$widgetContext;
59 
63  protected ‪$objectManager;
64 
68  protected ‪$controllerContext;
69 
73  protected ‪$request;
74 
78  protected ‪$mockExtensionService;
79 
83  protected ‪$renderingContext;
84 
85  protected function ‪setUp(): void
86  {
87  parent::setUp();
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(self::at(0))->method('get')->with(WidgetContext::class)->willReturn($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(self::any())->method('getRequest')->willReturn($this->request);
100  $this->renderingContext = $this->getMockBuilder(RenderingContext::class)
101  ->onlyMethods(['getControllerContext'])
102  ->disableOriginalConstructor()
103  ->getMock();
104  $this->renderingContext->expects(self::any())->method('getControllerContext')->willReturn($this->controllerContext);
105  $this->viewHelper->_set('renderingContext', $this->renderingContext);
106  }
107 
112  {
113  $this->‪callViewHelper();
114  }
115 
120  {
121  $this->viewHelper->_set('ajaxWidget', true);
122  $this->viewHelper->setArguments(['storeSession' => true]);
123  $this->ajaxWidgetContextHolder->expects(self::once())->method('store')->with($this->widgetContext);
124  $this->‪callViewHelper();
125  }
126 
131  {
132  $this->viewHelper->_set('ajaxWidget', true);
133  $this->viewHelper->setArguments(['storeSession' => false]);
134  $this->ajaxWidgetContextHolder->expects(self::never())->method('store')->with($this->widgetContext);
135  $this->‪callViewHelper();
136  }
137 
141  public function ‪callViewHelper()
142  {
143  $mockViewHelperVariableContainer = $this->createMock(ViewHelperVariableContainer::class);
144  $mockViewHelperVariableContainer->expects(self::any())->method('get')->willReturnArgument(2);
145  $mockRenderingContext = $this->getMockBuilder(RenderingContext::class)->disableOriginalConstructor()->getMock();
146  $mockRenderingContext->expects(self::atLeastOnce())->method('getViewHelperVariableContainer')->willReturn($mockViewHelperVariableContainer);
147  $mockRenderingContext->expects(self::any())->method('getControllerContext')->willReturn($this->controllerContext);
148  $this->viewHelper->setRenderingContext($mockRenderingContext);
149  $this->viewHelper->expects(self::once())->method('getWidgetConfiguration')->willReturn('Some Widget Configuration');
150  $this->widgetContext->expects(self::once())->method('setWidgetConfiguration')->with('Some Widget Configuration');
151  $this->widgetContext->expects(self::once())->method('setWidgetIdentifier')->with('@widget_0');
152  $this->viewHelper->_set('controller', new \stdClass());
153  $this->viewHelper->_set('renderingContext', $mockRenderingContext);
154  $this->widgetContext->expects(self::once())->method('setControllerObjectName')->with('stdClass');
155  $this->viewHelper->expects(self::once())->method('validateArguments');
156  $this->viewHelper->expects(self::once())->method('initialize');
157  $this->viewHelper->expects(self::once())->method('callRenderMethod')->willReturn('renderedResult');
158  ‪$output = $this->viewHelper->initializeArgumentsAndRender();
159  self::assertEquals('renderedResult', ‪$output);
160  }
161 
166  {
167  $node1 = $this->createMock(AbstractNode::class);
168  $node2 = $this->createMock(TextNode::class);
169  $node3 = $this->createMock(AbstractNode::class);
170  $rootNode = $this->createMock(RootNode::class);
171  $rootNode->expects(self::at(0))->method('addChildNode')->with($node1);
172  $rootNode->expects(self::at(1))->method('addChildNode')->with($node2);
173  $rootNode->expects(self::at(2))->method('addChildNode')->with($node3);
174  $this->objectManager->expects(self::once())->method('get')->with(RootNode::class)->willReturn($rootNode);
175  ‪$renderingContext = $this->createMock(RenderingContext::class);
176  $this->viewHelper->_set('renderingContext', ‪$renderingContext);
177  $this->widgetContext->expects(self::once())->method('setViewHelperChildNodes')->with($rootNode, ‪$renderingContext);
178  $this->viewHelper->setChildNodes([$node1, $node2, $node3]);
179  }
180 
185  {
186  $controller = $this->createMock(AbstractWidgetViewHelper::class);
187 
188  $this->expectException(MissingControllerException::class);
189  $this->expectExceptionCode(1289422564);
190 
191  $this->viewHelper->_set('controller', $controller);
192  $this->viewHelper->_call('initiateSubRequest');
193  }
194 
199  {
200  $controller = $this->createMock(AbstractWidgetController::class);
201  $this->viewHelper->_set('controller', $controller);
202  // Initial Setup
203  $widgetRequest = $this->createMock(WidgetRequest::class);
204  $response = $this->createMock(Response::class);
205  $this->objectManager->expects(self::at(0))->method('get')->with(WidgetRequest::class)->willReturn($widgetRequest);
206  $this->objectManager->expects(self::at(1))->method('get')->with(Response::class)->willReturn($response);
207  // Widget Context is set
208  $widgetRequest->expects(self::once())->method('setWidgetContext')->with($this->widgetContext);
209  // The namespaced arguments are passed to the sub-request
210  // and the action name is extracted from the namespace.
211  $this->controllerContext->expects(self::once())->method('getRequest')->willReturn($this->request);
212  $this->widgetContext->expects(self::once())->method('getWidgetIdentifier')->willReturn('widget-1');
213  $this->request->expects(self::once())->method('getArguments')->willReturn([
214  'k1' => 'k2',
215  'widget-1' => [
216  'arg1' => 'val1',
217  'arg2' => 'val2',
218  'action' => 'myAction'
219  ]
220  ]);
221  $widgetRequest->expects(self::once())->method('setArguments')->with([
222  'arg1' => 'val1',
223  'arg2' => 'val2'
224  ]);
225  $widgetRequest->expects(self::once())->method('setControllerActionName')->with('myAction');
226  // Controller is called
227  $controller->expects(self::once())->method('processRequest')->with($widgetRequest, $response);
228  ‪$output = $this->viewHelper->_call('initiateSubRequest');
229  // SubResponse is returned
230  self::assertSame($response, ‪$output);
231  }
232 
237  {
238  ‪$viewHelper = $this->getAccessibleMock(AbstractWidgetViewHelper::class, ['dummy'], [], '', false);
239  ‪$viewHelper->setArguments(['foo' => 'bar']);
240  self::assertEquals(['foo' => 'bar'], ‪$viewHelper->_call('getWidgetConfiguration'));
241  }
242 
246  public function ‪compileDisablesTemplateCompiler()
247  {
248  ‪$viewHelper = $this->getMockBuilder(AbstractWidgetViewHelper::class)
249  ->setMethods(['dummy'])
250  ->getMock();
251  $node = $this->getMockBuilder(ViewHelperNode::class)
252  ->setMethods(['dummy'])
253  ->disableOriginalConstructor()
254  ->getMock();
255  $compiler = $this->getMockBuilder(TemplateCompiler::class)
256  ->setMethods(['disable'])
257  ->getMock();
258  $compiler->expects(self::once())->method('disable');
259  $code = ''; // referenced
260  $result = ‪$viewHelper->‪compile('', '', $code, $node, $compiler);
261  self::assertEquals('\'\'', $result);
262  self::assertEquals('', $code);
263  }
264 }
‪TYPO3\CMS\Fluid\Core\Widget\WidgetRequest
Definition: WidgetRequest.php:25
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\storeSessionSetToFalseDoesNotStoreTheWidgetContextIfInAjaxMode
‪storeSessionSetToFalseDoesNotStoreTheWidgetContextIfInAjaxMode()
Definition: AbstractWidgetViewHelperTest.php:122
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest
Definition: AbstractWidgetViewHelperTest.php:44
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\getWidgetConfigurationReturnsArgumentsProperty
‪getWidgetConfigurationReturnsArgumentsProperty()
Definition: AbstractWidgetViewHelperTest.php:228
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$viewHelper
‪AbstractWidgetViewHelper $viewHelper
Definition: AbstractWidgetViewHelperTest.php:47
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\initiateSubRequestBuildsRequestProperly
‪initiateSubRequestBuildsRequestProperly()
Definition: AbstractWidgetViewHelperTest.php:190
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$renderingContext
‪RenderingContext $renderingContext
Definition: AbstractWidgetViewHelperTest.php:75
‪TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext
Definition: ControllerContext.php:28
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$widgetContext
‪WidgetContext $widgetContext
Definition: AbstractWidgetViewHelperTest.php:55
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\callViewHelper
‪callViewHelper()
Definition: AbstractWidgetViewHelperTest.php:133
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\initializeArgumentsAndRenderStoresTheWidgetContextIfInAjaxMode
‪initializeArgumentsAndRenderStoresTheWidgetContextIfInAjaxMode()
Definition: AbstractWidgetViewHelperTest.php:111
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\compileDisablesTemplateCompiler
‪compileDisablesTemplateCompiler()
Definition: AbstractWidgetViewHelperTest.php:238
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:26
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$request
‪Request $request
Definition: AbstractWidgetViewHelperTest.php:67
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper
Definition: AbstractWidgetViewHelper.php:30
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\initiateSubRequestThrowsExceptionIfControllerIsNoWidgetController
‪initiateSubRequestThrowsExceptionIfControllerIsNoWidgetController()
Definition: AbstractWidgetViewHelperTest.php:176
‪TYPO3\CMS\Extbase\Mvc\Web\Response
Definition: Response.php:26
‪TYPO3\CMS\Fluid\Core\Widget\Exception\MissingControllerException
Definition: MissingControllerException.php:26
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$controllerContext
‪ControllerContext $controllerContext
Definition: AbstractWidgetViewHelperTest.php:63
‪TYPO3\CMS\Extbase\Mvc\Web\Request
Definition: Request.php:23
‪$output
‪$output
Definition: annotationChecker.php:119
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget
Definition: AbstractWidgetControllerTest.php:16
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\compile
‪string compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
Definition: AbstractWidgetViewHelper.php:273
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\setChildNodesAddsChildNodesToWidgetContext
‪setChildNodesAddsChildNodesToWidgetContext()
Definition: AbstractWidgetViewHelperTest.php:157
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder
Definition: AjaxWidgetContextHolder.php:33
‪TYPO3\CMS\Extbase\Service\ExtensionService
Definition: ExtensionService.php:33
‪TYPO3\CMS\Fluid\Core\Widget\WidgetContext
Definition: WidgetContext.php:33
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\initializeArgumentsAndRenderCallsTheRightSequenceOfMethods
‪initializeArgumentsAndRenderCallsTheRightSequenceOfMethods()
Definition: AbstractWidgetViewHelperTest.php:103
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$objectManager
‪ObjectManagerInterface $objectManager
Definition: AbstractWidgetViewHelperTest.php:59
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\setUp
‪setUp()
Definition: AbstractWidgetViewHelperTest.php:77
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContext
Definition: RenderingContext.php:39
‪TYPO3\CMS\Extbase\Mvc\Request
Definition: Request.php:31
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController
Definition: AbstractWidgetController.php:32
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$mockExtensionService
‪ExtensionService $mockExtensionService
Definition: AbstractWidgetViewHelperTest.php:71
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\AbstractWidgetViewHelperTest\$ajaxWidgetContextHolder
‪AjaxWidgetContextHolder $ajaxWidgetContextHolder
Definition: AbstractWidgetViewHelperTest.php:51