1058a340804881e6d4783721b109593af6e86d09
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / Core / Widget / WidgetRequestBuilderTest.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 WidgetRequestBuilder
25 */
26 class WidgetRequestBuilderTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
27
28 /**
29 * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetRequestBuilder
30 */
31 protected $widgetRequestBuilder;
32
33 /**
34 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
35 */
36 protected $mockObjectManager;
37
38 /**
39 * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetRequest
40 */
41 protected $mockWidgetRequest;
42
43 /**
44 * @var \TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder
45 */
46 protected $mockAjaxWidgetContextHolder;
47
48 /**
49 * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
50 */
51 protected $mockWidgetContext;
52
53 public function setUp() {
54 $this->widgetRequestBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\Core\\Widget\\WidgetRequestBuilder', array('setArgumentsFromRawRequestData'));
55 $this->mockWidgetRequest = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Widget\\WidgetRequest');
56 $this->mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
57 $this->mockObjectManager->expects($this->once())->method('get')->with('TYPO3\\CMS\\Fluid\\Core\\Widget\\WidgetRequest')->will($this->returnValue($this->mockWidgetRequest));
58 $this->widgetRequestBuilder->_set('objectManager', $this->mockObjectManager);
59 $this->mockWidgetContext = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Widget\\WidgetContext');
60 $this->mockAjaxWidgetContextHolder = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Widget\\AjaxWidgetContextHolder');
61 $this->widgetRequestBuilder->injectAjaxWidgetContextHolder($this->mockAjaxWidgetContextHolder);
62 $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
63 }
64
65 /**
66 * @test
67 */
68 public function buildSetsRequestUri() {
69 $requestUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
70 $this->mockWidgetRequest->expects($this->once())->method('setRequestURI')->with($requestUri);
71 $this->widgetRequestBuilder->build();
72 }
73
74 /**
75 * @test
76 */
77 public function buildSetsBaseUri() {
78 $baseUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
79 $this->mockWidgetRequest->expects($this->once())->method('setBaseURI')->with($baseUri);
80 $this->widgetRequestBuilder->build();
81 }
82
83 /**
84 * @test
85 */
86 public function buildSetsRequestMethod() {
87 $_SERVER['REQUEST_METHOD'] = 'POST';
88 $this->mockWidgetRequest->expects($this->once())->method('setMethod')->with('POST');
89 $this->widgetRequestBuilder->build();
90 }
91
92 /**
93 * @test
94 */
95 public function buildSetsPostArgumentsFromRequest() {
96 $_SERVER['REQUEST_METHOD'] = 'POST';
97 $_GET = array('get' => 'foo');
98 $_POST = array('post' => 'bar');
99 $this->mockWidgetRequest->expects($this->once())->method('setArguments')->with($_POST);
100 $this->widgetRequestBuilder->build();
101 }
102
103 /**
104 * @test
105 */
106 public function buildSetsGetArgumentsFromRequest() {
107 $_SERVER['REQUEST_METHOD'] = 'GET';
108 $_GET = array('get' => 'foo');
109 $_POST = array('post' => 'bar');
110 $this->mockWidgetRequest->expects($this->once())->method('setArguments')->with($_GET);
111 $this->widgetRequestBuilder->build();
112 }
113
114 /**
115 * @test
116 */
117 public function buildSetsControllerActionNameFromGetArguments() {
118 $_GET = array('action' => 'myAction');
119 $this->mockWidgetRequest->expects($this->once())->method('setControllerActionName')->with('myAction');
120 $this->widgetRequestBuilder->build();
121 }
122
123 /**
124 * @test
125 */
126 public function buildSetsWidgetContext() {
127 $_GET = array('fluid-widget-id' => '123');
128 $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->with('123')->will($this->returnValue($this->mockWidgetContext));
129 $this->mockWidgetRequest->expects($this->once())->method('setWidgetContext')->with($this->mockWidgetContext);
130 $this->widgetRequestBuilder->build();
131 }
132
133 /**
134 * @test
135 */
136 public function buildReturnsRequest() {
137 $expected = $this->mockWidgetRequest;
138 $actual = $this->widgetRequestBuilder->build();
139 $this->assertSame($expected, $actual);
140 }
141 }