[TASK] Create own response instance in controller actions
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Controller / FormInlineAjaxControllerTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Controller;
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 use Psr\Http\Message\ServerRequestInterface;
18 use TYPO3\CMS\Backend\Controller\FormInlineAjaxController;
19 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20
21 /**
22 * Test case
23 */
24 class FormInlineAjaxControllerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
25 {
26 /**
27 * Subject is not notice free, disable E_NOTICES
28 */
29 protected static $suppressNotices = true;
30
31 /**
32 * @test
33 */
34 public function createActionThrowsExceptionIfContextIsEmpty()
35 {
36 $requestProphecy = $this->prophesize(ServerRequestInterface::class);
37 $requestProphecy->getParsedBody()->shouldBeCalled()->willReturn(
38 [
39 'ajax' => [
40 'context' => '',
41 ],
42 ]
43 );
44 $this->expectException(\RuntimeException::class);
45 $this->expectExceptionCode(1489751361);
46 (new FormInlineAjaxController())->createAction($requestProphecy->reveal());
47 }
48
49 /**
50 * @test
51 */
52 public function createActionThrowsExceptionIfContextConfigSectionIsEmpty()
53 {
54 $requestProphecy = $this->prophesize(ServerRequestInterface::class);
55 $requestProphecy->getParsedBody()->shouldBeCalled()->willReturn(
56 [
57 'ajax' => [
58 'context' => json_encode([ 'config' => '' ]),
59 ],
60 ]
61 );
62 $this->expectException(\RuntimeException::class);
63 $this->expectExceptionCode(1489751362);
64 (new FormInlineAjaxController())->createAction($requestProphecy->reveal());
65 }
66
67 /**
68 * @test
69 */
70 public function createActionThrowsExceptionIfContextConfigSectionDoesNotValidate()
71 {
72 $requestProphecy = $this->prophesize(ServerRequestInterface::class);
73 $requestProphecy->getParsedBody()->shouldBeCalled()->willReturn(
74 [
75 'ajax' => [
76 'context' => json_encode(
77 [
78 'config' => [
79 'type' => 'inline',
80 ],
81 'hmac' => 'anInvalidHash',
82 ]
83 ),
84 ],
85 ]
86 );
87 $this->expectException(\RuntimeException::class);
88 $this->expectExceptionCode(1489751363);
89 (new FormInlineAjaxController())->createAction($requestProphecy->reveal());
90 }
91
92 /**
93 * @test
94 */
95 public function detailsActionThrowsExceptionIfContextIsEmpty()
96 {
97 $requestProphecy = $this->prophesize(ServerRequestInterface::class);
98 $requestProphecy->getParsedBody()->shouldBeCalled()->willReturn(
99 [
100 'ajax' => [
101 'context' => '',
102 ],
103 ]
104 );
105 $this->expectException(\RuntimeException::class);
106 $this->expectExceptionCode(1489751361);
107 (new FormInlineAjaxController())->detailsAction($requestProphecy->reveal());
108 }
109
110 /**
111 * @test
112 */
113 public function detailsActionThrowsExceptionIfContextConfigSectionIsEmpty()
114 {
115 $requestProphecy = $this->prophesize(ServerRequestInterface::class);
116 $requestProphecy->getParsedBody()->shouldBeCalled()->willReturn(
117 [
118 'ajax' => [
119 'context' => json_encode([ 'config' => '' ]),
120 ],
121 ]
122 );
123 $this->expectException(\RuntimeException::class);
124 $this->expectExceptionCode(1489751362);
125 (new FormInlineAjaxController())->detailsAction($requestProphecy->reveal());
126 }
127
128 /**
129 * @test
130 */
131 public function detailsActionThrowsExceptionIfContextConfigSectionDoesNotValidate()
132 {
133 $requestProphecy = $this->prophesize(ServerRequestInterface::class);
134 $requestProphecy->getParsedBody()->shouldBeCalled()->willReturn(
135 [
136 'ajax' => [
137 'context' => json_encode(
138 [
139 'config' => [
140 'type' => 'inline',
141 ],
142 'hmac' => 'anInvalidHash',
143 ]
144 ),
145 ],
146 ]
147 );
148 $this->expectException(\RuntimeException::class);
149 $this->expectExceptionCode(1489751363);
150 (new FormInlineAjaxController())->detailsAction($requestProphecy->reveal());
151 }
152
153 /**
154 * @test
155 */
156 public function synchronizeLocalizeActionThrowsExceptionIfContextIsEmpty()
157 {
158 $requestProphecy = $this->prophesize(ServerRequestInterface::class);
159 $requestProphecy->getParsedBody()->shouldBeCalled()->willReturn(
160 [
161 'ajax' => [
162 'context' => '',
163 ],
164 ]
165 );
166 $this->expectException(\RuntimeException::class);
167 $this->expectExceptionCode(1489751361);
168 (new FormInlineAjaxController())->synchronizeLocalizeAction($requestProphecy->reveal());
169 }
170
171 /**
172 * @test
173 */
174 public function synchronizeLocalizeActionThrowsExceptionIfContextConfigSectionIsEmpty()
175 {
176 $requestProphecy = $this->prophesize(ServerRequestInterface::class);
177 $requestProphecy->getParsedBody()->shouldBeCalled()->willReturn(
178 [
179 'ajax' => [
180 'context' => json_encode([ 'config' => '' ]),
181 ],
182 ]
183 );
184 $this->expectException(\RuntimeException::class);
185 $this->expectExceptionCode(1489751362);
186 (new FormInlineAjaxController())->synchronizeLocalizeAction($requestProphecy->reveal());
187 }
188
189 /**
190 * @test
191 */
192 public function synchronizeLocalizeActionThrowsExceptionIfContextConfigSectionDoesNotValidate()
193 {
194 $requestProphecy = $this->prophesize(ServerRequestInterface::class);
195 $requestProphecy->getParsedBody()->shouldBeCalled()->willReturn(
196 [
197 'ajax' => [
198 'context' => json_encode(
199 [
200 'config' => [
201 'type' => 'inline',
202 ],
203 'hmac' => 'anInvalidHash',
204 ]
205 ),
206 ],
207 ]
208 );
209 $this->expectException(\RuntimeException::class);
210 $this->expectExceptionCode(1489751363);
211 (new FormInlineAjaxController())->synchronizeLocalizeAction($requestProphecy->reveal());
212 }
213
214 /**
215 * Fallback for IRRE items without inline view attribute
216 * @see https://forge.typo3.org/issues/76561
217 *
218 * @test
219 */
220 public function getInlineExpandCollapseStateArraySwitchesToFallbackIfTheBackendUserDoesNotHaveAnUCInlineViewProperty()
221 {
222 $backendUserProphecy = $this->prophesize(BackendUserAuthentication::class);
223 $backendUserProphecy->uc = [];
224 $backendUser = $backendUserProphecy->reveal();
225
226 $mockObject = $this->getAccessibleMock(
227 FormInlineAjaxController::class,
228 ['getBackendUserAuthentication'],
229 [],
230 '',
231 false
232 );
233 $mockObject->method('getBackendUserAuthentication')->willReturn($backendUser);
234 $result = $mockObject->_call('getInlineExpandCollapseStateArray');
235
236 $this->assertEmpty($result);
237 }
238
239 /**
240 * Unserialize uc inline view string for IRRE item
241 * @see https://forge.typo3.org/issues/76561
242 *
243 * @test
244 */
245 public function getInlineExpandCollapseStateArrayWillUnserializeUCInlineViewPropertyAsAnArrayWithData()
246 {
247 $backendUserProphecy = $this->prophesize(BackendUserAuthentication::class);
248 $backendUserProphecy->uc = ['inlineView' => serialize(['foo' => 'bar'])];
249 $backendUser = $backendUserProphecy->reveal();
250
251 $mockObject = $this->getAccessibleMock(
252 FormInlineAjaxController::class,
253 ['getBackendUserAuthentication'],
254 [],
255 '',
256 false
257 );
258 $mockObject->method('getBackendUserAuthentication')->willReturn($backendUser);
259 $result = $mockObject->_call('getInlineExpandCollapseStateArray');
260
261 $this->assertNotEmpty($result);
262 }
263 }