99c861efa5749d1722eb17524f5d2f8bdb962922
[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 file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Test case
19 */
20 class WidgetRequestBuilderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
21 {
22 /**
23 * Subject is not notice free, disable E_NOTICES
24 */
25 protected static $suppressNotices = true;
26
27 /**
28 * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetRequestBuilder
29 */
30 protected $widgetRequestBuilder;
31
32 /**
33 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
34 */
35 protected $mockObjectManager;
36
37 /**
38 * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetRequest
39 */
40 protected $mockWidgetRequest;
41
42 /**
43 * @var \TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder
44 */
45 protected $mockAjaxWidgetContextHolder;
46
47 /**
48 * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
49 */
50 protected $mockWidgetContext;
51
52 protected function setUp()
53 {
54 $this->widgetRequestBuilder = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequestBuilder::class, ['setArgumentsFromRawRequestData']);
55 $this->mockWidgetRequest = $this->createMock(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class);
56 $this->mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
57 $this->mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class)->will($this->returnValue($this->mockWidgetRequest));
58 $this->widgetRequestBuilder->_set('objectManager', $this->mockObjectManager);
59 $this->mockWidgetContext = $this->createMock(\TYPO3\CMS\Fluid\Core\Widget\WidgetContext::class);
60 $this->mockAjaxWidgetContextHolder = $this->createMock(\TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder::class);
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 {
70 $requestUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
71 $this->mockWidgetRequest->expects($this->once())->method('setRequestURI')->with($requestUri);
72 $this->widgetRequestBuilder->build();
73 }
74
75 /**
76 * @test
77 */
78 public function buildSetsBaseUri()
79 {
80 $baseUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
81 $this->mockWidgetRequest->expects($this->once())->method('setBaseURI')->with($baseUri);
82 $this->widgetRequestBuilder->build();
83 }
84
85 /**
86 * @test
87 */
88 public function buildSetsRequestMethod()
89 {
90 $_SERVER['REQUEST_METHOD'] = 'POST';
91 $this->mockWidgetRequest->expects($this->once())->method('setMethod')->with('POST');
92 $this->widgetRequestBuilder->build();
93 }
94
95 /**
96 * @test
97 */
98 public function buildSetsPostArgumentsFromRequest()
99 {
100 $_SERVER['REQUEST_METHOD'] = 'POST';
101 $_GET = ['get' => 'foo'];
102 $_POST = ['post' => 'bar'];
103 $this->mockWidgetRequest->expects($this->once())->method('setArguments')->with($_POST);
104 $this->widgetRequestBuilder->build();
105 }
106
107 /**
108 * @test
109 */
110 public function buildSetsGetArgumentsFromRequest()
111 {
112 $_SERVER['REQUEST_METHOD'] = 'GET';
113 $_GET = ['get' => 'foo'];
114 $_POST = ['post' => 'bar'];
115 $this->mockWidgetRequest->expects($this->once())->method('setArguments')->with($_GET);
116 $this->widgetRequestBuilder->build();
117 }
118
119 /**
120 * @test
121 */
122 public function buildSetsControllerActionNameFromGetArguments()
123 {
124 $_GET = ['action' => 'myAction'];
125 $this->mockWidgetRequest->expects($this->once())->method('setControllerActionName')->with('myAction');
126 $this->widgetRequestBuilder->build();
127 }
128
129 /**
130 * @test
131 */
132 public function buildSetsWidgetContext()
133 {
134 $_GET = ['fluid-widget-id' => '123'];
135 $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->with('123')->will($this->returnValue($this->mockWidgetContext));
136 $this->mockWidgetRequest->expects($this->once())->method('setWidgetContext')->with($this->mockWidgetContext);
137 $this->widgetRequestBuilder->build();
138 }
139
140 /**
141 * @test
142 */
143 public function buildReturnsRequest()
144 {
145 $expected = $this->mockWidgetRequest;
146 $actual = $this->widgetRequestBuilder->build();
147 $this->assertSame($expected, $actual);
148 }
149 }