/Tests/Unit/Core/Widget/AbstractWidgetViewHelperTest.php

https://github.com/arbyte/FLOW3-X-TYPO3.Fluid · PHP · 220 lines · 113 code · 40 blank · 67 comment · 0 complexity · 08396359ce5b2da8e353c7754476e0f5 MD5 · raw file

  1. <?php
  2. namespace TYPO3\Fluid\Tests\Unit\Core\Widget;
  3. /* *
  4. * This script belongs to the FLOW3 package "Fluid". *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. /**
  13. * Testcase for AbstractWidgetViewHelper
  14. *
  15. */
  16. class AbstractWidgetViewHelperTest extends \TYPO3\FLOW3\Tests\UnitTestCase {
  17. /**
  18. * @var \TYPO3\Fluid\Core\Widget\AbstractWidgetViewHelper
  19. */
  20. protected $viewHelper;
  21. /**
  22. * @var \TYPO3\Fluid\Core\Widget\AjaxWidgetContextHolder
  23. */
  24. protected $ajaxWidgetContextHolder;
  25. /**
  26. * @var \TYPO3\Fluid\Core\Widget\WidgetContext
  27. */
  28. protected $widgetContext;
  29. /**
  30. * @var \TYPO3\FLOW3\Object\ObjectManagerInterface
  31. */
  32. protected $objectManager;
  33. /**
  34. * @var \TYPO3\FLOW3\MVC\Controller\ControllerContext
  35. */
  36. protected $controllerContext;
  37. /**
  38. * @var \TYPO3\FLOW3\MVC\Web\Request
  39. */
  40. protected $request;
  41. /**
  42. */
  43. public function setUp() {
  44. $this->viewHelper = $this->getAccessibleMock('TYPO3\Fluid\Core\Widget\AbstractWidgetViewHelper', array('validateArguments', 'initialize', 'callRenderMethod', 'getWidgetConfiguration', 'getRenderingContext'));
  45. $this->ajaxWidgetContextHolder = $this->getMock('TYPO3\Fluid\Core\Widget\AjaxWidgetContextHolder');
  46. $this->viewHelper->injectAjaxWidgetContextHolder($this->ajaxWidgetContextHolder);
  47. $this->widgetContext = $this->getMock('TYPO3\Fluid\Core\Widget\WidgetContext');
  48. $this->viewHelper->injectWidgetContext($this->widgetContext);
  49. $this->objectManager = $this->getMock('TYPO3\FLOW3\Object\ObjectManagerInterface');
  50. $this->viewHelper->injectObjectManager($this->objectManager);
  51. $this->controllerContext = $this->getMock('TYPO3\FLOW3\MVC\Controller\ControllerContext', array(), array(), '', FALSE);
  52. $this->viewHelper->_set('controllerContext', $this->controllerContext);
  53. $this->request = $this->getMock('TYPO3\FLOW3\MVC\Web\Request');
  54. }
  55. /**
  56. * @test
  57. */
  58. public function initializeArgumentsAndRenderCallsTheRightSequenceOfMethods() {
  59. $this->callViewHelper();
  60. }
  61. /**
  62. * @test
  63. */
  64. public function initializeArgumentsAndRenderStoresTheWidgetContextIfInAjaxMode() {
  65. $this->viewHelper->_set('ajaxWidget', TRUE);
  66. $this->ajaxWidgetContextHolder->expects($this->once())->method('store')->with($this->widgetContext);
  67. $this->callViewHelper();
  68. }
  69. /**
  70. * Calls the ViewHelper, and emulates a rendering.
  71. *
  72. * @return void
  73. */
  74. public function callViewHelper() {
  75. $viewHelperVariableContainer = $this->getMock('TYPO3\Fluid\Core\ViewHelper\ViewHelperVariableContainer');
  76. $renderingContext = new \TYPO3\Fluid\Core\Rendering\RenderingContext();
  77. $renderingContext->injectViewHelperVariableContainer($viewHelperVariableContainer);
  78. $this->viewHelper->setRenderingContext($renderingContext);
  79. $this->viewHelper->expects($this->any())->method('getWidgetConfiguration')->will($this->returnValue('Some Widget Configuration'));
  80. $this->widgetContext->expects($this->once())->method('setNonAjaxWidgetConfiguration')->with('Some Widget Configuration');
  81. $this->widgetContext->expects($this->once())->method('setWidgetIdentifier')->with('@widget_0');
  82. $this->viewHelper->_set('controller', new \stdClass());
  83. $this->widgetContext->expects($this->once())->method('setControllerObjectName')->with('stdClass');
  84. $this->viewHelper->expects($this->once())->method('validateArguments');
  85. $this->viewHelper->expects($this->once())->method('initialize');
  86. $this->viewHelper->expects($this->once())->method('callRenderMethod')->will($this->returnValue('renderedResult'));
  87. $output = $this->viewHelper->initializeArgumentsAndRender(array('arg1' => 'val1'));
  88. $this->assertEquals('renderedResult', $output);
  89. }
  90. /**
  91. * @test
  92. */
  93. public function setChildNodesAddsChildNodesToWidgetContext() {
  94. $node1 = $this->getMock('TYPO3\Fluid\Core\Parser\SyntaxTree\AbstractNode');
  95. $node2 = $this->getMock('TYPO3\Fluid\Core\Parser\SyntaxTree\TextNode', array(), array(), '', FALSE);
  96. $node3 = $this->getMock('TYPO3\Fluid\Core\Parser\SyntaxTree\AbstractNode');
  97. $rootNode = $this->getMock('TYPO3\Fluid\Core\Parser\SyntaxTree\RootNode');
  98. $rootNode->expects($this->at(0))->method('addChildNode')->with($node1);
  99. $rootNode->expects($this->at(1))->method('addChildNode')->with($node2);
  100. $rootNode->expects($this->at(2))->method('addChildNode')->with($node3);
  101. $this->objectManager->expects($this->once())->method('create')->with('TYPO3\Fluid\Core\Parser\SyntaxTree\RootNode')->will($this->returnValue($rootNode));
  102. $renderingContext = $this->getMock('TYPO3\Fluid\Core\Rendering\RenderingContextInterface');
  103. $this->viewHelper->_set('renderingContext', $renderingContext);
  104. $this->widgetContext->expects($this->once())->method('setViewHelperChildNodes')->with($rootNode, $renderingContext);
  105. $this->viewHelper->setChildNodes(array($node1, $node2, $node3));
  106. }
  107. /**
  108. * @test
  109. * @expectedException TYPO3\Fluid\Core\Widget\Exception\MissingControllerException
  110. */
  111. public function initiateSubRequestThrowsExceptionIfControllerIsNoWidgetController() {
  112. $controller = $this->getMock('TYPO3\FLOW3\MVC\Controller\ControllerInterface');
  113. $this->viewHelper->_set('controller', $controller);
  114. $this->viewHelper->_call('initiateSubRequest');
  115. }
  116. /**
  117. * @test
  118. */
  119. public function initiateSubRequestBuildsRequestProperly() {
  120. $controller = $this->getMock('TYPO3\Fluid\Core\Widget\AbstractWidgetController', array(), array(), '', FALSE);
  121. $this->viewHelper->_set('controller', $controller);
  122. // Initial Setup
  123. $widgetRequest = $this->getMock('TYPO3\FLOW3\MVC\Web\SubRequest', array('setControllerObjectName', 'setArguments', 'setArgument', 'setControllerActionName'), array($this->getMock('TYPO3\FLOW3\MVC\Web\Request')));
  124. $response = $this->getMock('TYPO3\FLOW3\MVC\Web\Response');
  125. $this->objectManager->expects($this->at(0))->method('create')->with('TYPO3\FLOW3\MVC\Web\SubRequest')->will($this->returnValue($widgetRequest));
  126. $this->objectManager->expects($this->at(1))->method('create')->with('TYPO3\FLOW3\MVC\Web\Response')->will($this->returnValue($response));
  127. // Widget Context is set
  128. $widgetRequest->expects($this->once())->method('setArgument')->with('__widgetContext', $this->widgetContext);
  129. // The namespaced arguments are passed to the sub-request
  130. // and the action name is exctracted from the namespace.
  131. $this->controllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
  132. $this->widgetContext->expects($this->any())->method('getWidgetIdentifier')->will($this->returnValue('widget-1'));
  133. $this->request->expects($this->once())->method('getArguments')->will($this->returnValue(array(
  134. 'k1' => 'k2',
  135. 'widget-1' => array(
  136. 'arg1' => 'val1',
  137. 'arg2' => 'val2',
  138. '@action' => 'myAction'
  139. )
  140. )));
  141. $widgetRequest->expects($this->once())->method('setArguments')->with(array(
  142. 'arg1' => 'val1',
  143. 'arg2' => 'val2'
  144. ));
  145. $widgetRequest->expects($this->once())->method('setControllerActionName')->with('myAction');
  146. // Controller is called
  147. $controller->expects($this->once())->method('processRequest')->with($widgetRequest, $response);
  148. $output = $this->viewHelper->_call('initiateSubRequest');
  149. // SubResponse is returned
  150. $this->assertSame($response, $output);
  151. }
  152. /**
  153. * @test
  154. */
  155. public function initiateSubRequestSetsIndexActionIfNoActionSet() {
  156. $controller = $this->getMock('TYPO3\Fluid\Core\Widget\AbstractWidgetController', array(), array(), '', FALSE);
  157. $this->viewHelper->_set('controller', $controller);
  158. // Initial Setup
  159. $widgetRequest = $this->getMock('TYPO3\FLOW3\MVC\Web\SubRequest', array('setControllerObjectName', 'setArguments', 'setArgument', 'setControllerActionName'), array($this->getMock('TYPO3\FLOW3\MVC\Web\Request')));
  160. $response = $this->getMock('TYPO3\FLOW3\MVC\Web\Response');
  161. $this->objectManager->expects($this->at(0))->method('create')->with('TYPO3\FLOW3\MVC\Web\SubRequest')->will($this->returnValue($widgetRequest));
  162. $this->objectManager->expects($this->at(1))->method('create')->with('TYPO3\FLOW3\MVC\Web\Response')->will($this->returnValue($response));
  163. // Widget Context is set
  164. $widgetRequest->expects($this->once())->method('setArgument')->with('__widgetContext', $this->widgetContext);
  165. // The namespaced arguments are passed to the sub-request
  166. // and the action name is exctracted from the namespace.
  167. $this->controllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
  168. $this->widgetContext->expects($this->any())->method('getWidgetIdentifier')->will($this->returnValue('widget-1'));
  169. $this->request->expects($this->once())->method('getArguments')->will($this->returnValue(array(
  170. 'k1' => 'k2',
  171. 'widget-1' => array(
  172. 'arg1' => 'val1',
  173. 'arg2' => 'val2',
  174. )
  175. )));
  176. $widgetRequest->expects($this->once())->method('setControllerActionName')->with('index');
  177. $this->viewHelper->_call('initiateSubRequest');
  178. }
  179. }
  180. ?>