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