[FOLLOWUP][TASK] Remove leftover unzipping core functionality
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Authentication / BackendUserAuthenticationTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Authentication;
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 * Testcase for \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
19 */
20 class BackendUserAuthenticationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
21 {
22 /**
23 * @var array
24 */
25 protected $defaultFilePermissions = array(
26 // File permissions
27 'addFile' => false,
28 'readFile' => false,
29 'writeFile' => false,
30 'copyFile' => false,
31 'moveFile' => false,
32 'renameFile' => false,
33 'deleteFile' => false,
34 // Folder permissions
35 'addFolder' => false,
36 'readFolder' => false,
37 'writeFolder' => false,
38 'copyFolder' => false,
39 'moveFolder' => false,
40 'renameFolder' => false,
41 'deleteFolder' => false,
42 'recursivedeleteFolder' => false
43 );
44
45 protected function setUp()
46 {
47 // reset hooks
48 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'] = array();
49 }
50
51 protected function tearDown()
52 {
53 \TYPO3\CMS\Core\FormProtection\FormProtectionFactory::purgeInstances();
54 parent::tearDown();
55 }
56
57 /////////////////////////////////////////
58 // Tests concerning the form protection
59 /////////////////////////////////////////
60 /**
61 * @test
62 */
63 public function logoffCleansFormProtectionIfBackendUserIsLoggedIn()
64 {
65 $formProtection = $this->getMock(
66 \TYPO3\CMS\Core\FormProtection\BackendFormProtection::class,
67 array('clean'),
68 array(),
69 '',
70 false
71 );
72 $formProtection->expects($this->once())->method('clean');
73
74 \TYPO3\CMS\Core\FormProtection\FormProtectionFactory::set(
75 'default',
76 $formProtection
77 );
78
79 // logoff() call the static factory that has a dependency to a valid BE_USER object. Mock this away
80 $GLOBALS['BE_USER'] = $this->getMock(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class, array(), array(), '', false);
81 $GLOBALS['BE_USER']->user = array('uid' => $this->getUniqueId());
82 $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array(), array(), '', false);
83
84 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class, array('dummy'), array(), '', false);
85 $subject->_set('db', $GLOBALS['TYPO3_DB']);
86 $subject->logoff();
87 }
88
89 /**
90 * @return array
91 */
92 public function getTSConfigDataProvider()
93 {
94 $completeConfiguration = array(
95 'value' => 'oneValue',
96 'value.' => array('oneProperty' => 'oneValue'),
97 'permissions.' => array(
98 'file.' => array(
99 'default.' => array('readAction' => '1'),
100 '1.' => array('writeAction' => '1'),
101 '0.' => array('readAction' => '0'),
102 ),
103 )
104 );
105
106 return array(
107 'single level string' => array(
108 $completeConfiguration,
109 'permissions',
110 array(
111 'value' => null,
112 'properties' =>
113 array(
114 'file.' => array(
115 'default.' => array('readAction' => '1'),
116 '1.' => array('writeAction' => '1'),
117 '0.' => array('readAction' => '0'),
118 ),
119 ),
120 ),
121 ),
122 'two levels string' => array(
123 $completeConfiguration,
124 'permissions.file',
125 array(
126 'value' => null,
127 'properties' =>
128 array(
129 'default.' => array('readAction' => '1'),
130 '1.' => array('writeAction' => '1'),
131 '0.' => array('readAction' => '0'),
132 ),
133 ),
134 ),
135 'three levels string' => array(
136 $completeConfiguration,
137 'permissions.file.default',
138 array(
139 'value' => null,
140 'properties' =>
141 array('readAction' => '1'),
142 ),
143 ),
144 'three levels string with integer property' => array(
145 $completeConfiguration,
146 'permissions.file.1',
147 array(
148 'value' => null,
149 'properties' => array('writeAction' => '1'),
150 ),
151 ),
152 'three levels string with integer zero property' => array(
153 $completeConfiguration,
154 'permissions.file.0',
155 array(
156 'value' => null,
157 'properties' => array('readAction' => '0'),
158 ),
159 ),
160 'four levels string with integer zero property, value, no properties' => array(
161 $completeConfiguration,
162 'permissions.file.0.readAction',
163 array(
164 'value' => '0',
165 'properties' => null,
166 ),
167 ),
168 'four levels string with integer property, value, no properties' => array(
169 $completeConfiguration,
170 'permissions.file.1.writeAction',
171 array(
172 'value' => '1',
173 'properties' => null,
174 ),
175 ),
176 'one level, not existent string' => array(
177 $completeConfiguration,
178 'foo',
179 array(
180 'value' => null,
181 'properties' => null,
182 ),
183 ),
184 'two level, not existent string' => array(
185 $completeConfiguration,
186 'foo.bar',
187 array(
188 'value' => null,
189 'properties' => null,
190 ),
191 ),
192 'two level, where second level does not exist' => array(
193 $completeConfiguration,
194 'permissions.bar',
195 array(
196 'value' => null,
197 'properties' => null,
198 ),
199 ),
200 'three level, where third level does not exist' => array(
201 $completeConfiguration,
202 'permissions.file.foo',
203 array(
204 'value' => null,
205 'properties' => null,
206 ),
207 ),
208 'three level, where second and third level does not exist' => array(
209 $completeConfiguration,
210 'permissions.foo.bar',
211 array(
212 'value' => null,
213 'properties' => null,
214 ),
215 ),
216 'value and properties' => array(
217 $completeConfiguration,
218 'value',
219 array(
220 'value' => 'oneValue',
221 'properties' => array('oneProperty' => 'oneValue'),
222 ),
223 ),
224 );
225 }
226
227 /**
228 * @param array $completeConfiguration
229 * @param string $objectString
230 * @param array $expectedConfiguration
231 * @dataProvider getTSConfigDataProvider
232 * @test
233 */
234 public function getTSConfigReturnsCorrectArrayForGivenObjectString(array $completeConfiguration, $objectString, array $expectedConfiguration)
235 {
236 $subject = $this->getMock(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class, array('dummy'), array(), '', false);
237 $subject->userTS = $completeConfiguration;
238
239 $actualConfiguration = $subject->getTSConfig($objectString);
240 $this->assertSame($expectedConfiguration, $actualConfiguration);
241 }
242
243 /**
244 * @return array
245 */
246 public function getFilePermissionsTakesUserDefaultAndStoragePermissionsIntoAccountIfUserIsNotAdminDataProvider()
247 {
248 return array(
249 'Only read permissions' => array(
250 array(
251 'addFile' => 0,
252 'readFile' => 1,
253 'writeFile' => 0,
254 'copyFile' => 0,
255 'moveFile' => 0,
256 'renameFile' => 0,
257 'deleteFile' => 0,
258 'addFolder' => 0,
259 'readFolder' => 1,
260 'copyFolder' => 0,
261 'moveFolder' => 0,
262 'renameFolder' => 0,
263 'writeFolder' => 0,
264 'deleteFolder' => 0,
265 'recursivedeleteFolder' => 0,
266 )
267 ),
268 'Uploading allowed' => array(
269 array(
270 'addFile' => 1,
271 'readFile' => 1,
272 'writeFile' => 1,
273 'copyFile' => 1,
274 'moveFile' => 1,
275 'renameFile' => 1,
276 'deleteFile' => 1,
277 'addFolder' => 0,
278 'readFolder' => 1,
279 'copyFolder' => 0,
280 'moveFolder' => 0,
281 'renameFolder' => 0,
282 'writeFolder' => 0,
283 'deleteFolder' => 0,
284 'recursivedeleteFolder' => 0
285 )
286 ),
287 'One value is enough' => array(
288 array(
289 'addFile' => 1,
290 )
291 ),
292 );
293 }
294
295 /**
296 * @param array $userTsConfiguration
297 * @test
298 * @dataProvider getFilePermissionsTakesUserDefaultAndStoragePermissionsIntoAccountIfUserIsNotAdminDataProvider
299 */
300 public function getFilePermissionsTakesUserDefaultPermissionsFromTsConfigIntoAccountIfUserIsNotAdmin(array $userTsConfiguration)
301 {
302 $subject = $this->getMock(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class, array('isAdmin'));
303
304 $subject
305 ->expects($this->any())
306 ->method('isAdmin')
307 ->will($this->returnValue(false));
308
309 $subject->userTS = array(
310 'permissions.' => array(
311 'file.' => array(
312 'default.' => $userTsConfiguration
313 ),
314 )
315 );
316
317 $expectedPermissions = array_merge($this->defaultFilePermissions, $userTsConfiguration);
318 array_walk(
319 $expectedPermissions,
320 function (&$value) {
321 $value = (bool)$value;
322 }
323 );
324
325 $this->assertEquals($expectedPermissions, $subject->getFilePermissions());
326 }
327
328 /**
329 * @return array
330 */
331 public function getFilePermissionsFromStorageDataProvider()
332 {
333 $defaultPermissions = array(
334 'addFile' => true,
335 'readFile' => true,
336 'writeFile' => true,
337 'copyFile' => true,
338 'moveFile' => true,
339 'renameFile' => true,
340 'deleteFile' => true,
341 'addFolder' => true,
342 'readFolder' => true,
343 'copyFolder' => true,
344 'moveFolder' => true,
345 'renameFolder' => true,
346 'writeFolder' => true,
347 'deleteFolder' => true,
348 'recursivedeleteFolder' => true
349 );
350
351 return array(
352 'Overwrites given storage permissions with default permissions' => array(
353 $defaultPermissions,
354 1,
355 array(
356 'addFile' => 0,
357 'recursivedeleteFolder' =>0
358 ),
359 array(
360 'addFile' => 0,
361 'readFile' => 1,
362 'writeFile' => 1,
363 'copyFile' => 1,
364 'moveFile' => 1,
365 'renameFile' => 1,
366 'deleteFile' => 1,
367 'addFolder' => 1,
368 'readFolder' => 1,
369 'copyFolder' => 1,
370 'moveFolder' => 1,
371 'renameFolder' => 1,
372 'writeFolder' => 1,
373 'deleteFolder' => 1,
374 'recursivedeleteFolder' => 0
375 )
376 ),
377 'Overwrites given storage 0 permissions with default permissions' => array(
378 $defaultPermissions,
379 0,
380 array(
381 'addFile' => 0,
382 'recursivedeleteFolder' =>0
383 ),
384 array(
385 'addFile' => false,
386 'readFile' => true,
387 'writeFile' => true,
388 'copyFile' => true,
389 'moveFile' => true,
390 'renameFile' => true,
391 'deleteFile' => true,
392 'addFolder' => true,
393 'readFolder' => true,
394 'copyFolder' => true,
395 'moveFolder' => true,
396 'renameFolder' => true,
397 'writeFolder' => true,
398 'deleteFolder' => true,
399 'recursivedeleteFolder' => false
400 )
401 ),
402 'Returns default permissions if no storage permissions are found' => array(
403 $defaultPermissions,
404 1,
405 array(),
406 array(
407 'addFile' => true,
408 'readFile' => true,
409 'writeFile' => true,
410 'copyFile' => true,
411 'moveFile' => true,
412 'renameFile' => true,
413 'deleteFile' => true,
414 'addFolder' => true,
415 'readFolder' => true,
416 'copyFolder' => true,
417 'moveFolder' => true,
418 'renameFolder' => true,
419 'writeFolder' => true,
420 'deleteFolder' => true,
421 'recursivedeleteFolder' => true
422 )
423 ),
424 );
425 }
426
427 /**
428 * @param array $defaultPermissions
429 * @param int $storageUid
430 * @param array $storagePermissions
431 * @param array $expectedPermissions
432 * @test
433 * @dataProvider getFilePermissionsFromStorageDataProvider
434 */
435 public function getFilePermissionsFromStorageOverwritesDefaultPermissions(array $defaultPermissions, $storageUid, array $storagePermissions, array $expectedPermissions)
436 {
437 $subject = $this->getMock(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class, array('isAdmin', 'getFilePermissions'));
438 $storageMock = $this->getMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class, array(), array(), '', false);
439 $storageMock->expects($this->any())->method('getUid')->will($this->returnValue($storageUid));
440
441 $subject
442 ->expects($this->any())
443 ->method('isAdmin')
444 ->will($this->returnValue(false));
445
446 $subject
447 ->expects($this->any())
448 ->method('getFilePermissions')
449 ->will($this->returnValue($defaultPermissions));
450
451 $subject->userTS = array(
452 'permissions.' => array(
453 'file.' => array(
454 'storage.' => array(
455 $storageUid . '.' => $storagePermissions
456 ),
457 ),
458 )
459 );
460
461 $this->assertEquals($expectedPermissions, $subject->getFilePermissionsForStorage($storageMock));
462 }
463
464 /**
465 * @param array $defaultPermissions
466 * @param $storageUid
467 * @param array $storagePermissions
468 * @test
469 * @dataProvider getFilePermissionsFromStorageDataProvider
470 */
471 public function getFilePermissionsFromStorageAlwaysReturnsDefaultPermissionsForAdmins(array $defaultPermissions, $storageUid, array $storagePermissions)
472 {
473 $subject = $this->getMock(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class, array('isAdmin', 'getFilePermissions'));
474 $storageMock = $this->getMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class, array(), array(), '', false);
475 $storageMock->expects($this->any())->method('getUid')->will($this->returnValue($storageUid));
476
477 $subject
478 ->expects($this->any())
479 ->method('isAdmin')
480 ->will($this->returnValue(true));
481
482 $subject
483 ->expects($this->any())
484 ->method('getFilePermissions')
485 ->will($this->returnValue($defaultPermissions));
486
487 $subject->userTS = array(
488 'permissions.' => array(
489 'file.' => array(
490 'storage.' => array(
491 $storageUid . '.' => $storagePermissions
492 ),
493 ),
494 )
495 );
496
497 $this->assertEquals($defaultPermissions, $subject->getFilePermissionsForStorage($storageMock));
498 }
499
500 /**
501 * @return array
502 */
503 public function getFilePermissionsTakesUserDefaultPermissionsFromRecordIntoAccountIfUserIsNotAdminDataProvider()
504 {
505 return array(
506 'No permission' => array(
507 '',
508 array(
509 'addFile' => false,
510 'readFile' => false,
511 'writeFile' => false,
512 'copyFile' => false,
513 'moveFile' => false,
514 'renameFile' => false,
515 'deleteFile' => false,
516 'addFolder' => false,
517 'readFolder' => false,
518 'copyFolder' => false,
519 'moveFolder' => false,
520 'renameFolder' => false,
521 'writeFolder' => false,
522 'deleteFolder' => false,
523 'recursivedeleteFolder' => false
524 )
525 ),
526 'Standard file permissions' => array(
527 'addFile,readFile,writeFile,copyFile,moveFile,renameFile,deleteFile',
528 array(
529 'addFile' => true,
530 'readFile' => true,
531 'writeFile' => true,
532 'copyFile' => true,
533 'moveFile' => true,
534 'renameFile' => true,
535 'deleteFile' => true,
536 'addFolder' => false,
537 'readFolder' => false,
538 'copyFolder' => false,
539 'moveFolder' => false,
540 'renameFolder' => false,
541 'writeFolder' => false,
542 'deleteFolder' => false,
543 'recursivedeleteFolder' => false
544 )
545 ),
546 'Standard folder permissions' => array(
547 'addFolder,readFolder,moveFolder,renameFolder,writeFolder,deleteFolder',
548 array(
549 'addFile' => false,
550 'readFile' => false,
551 'writeFile' => false,
552 'copyFile' => false,
553 'moveFile' => false,
554 'renameFile' => false,
555 'deleteFile' => false,
556 'addFolder' => true,
557 'readFolder' => true,
558 'writeFolder' => true,
559 'copyFolder' => false,
560 'moveFolder' => true,
561 'renameFolder' => true,
562 'deleteFolder' => true,
563 'recursivedeleteFolder' => false
564 )
565 ),
566 'Copy folder allowed' => array(
567 'readFolder,copyFolder',
568 array(
569 'addFile' => false,
570 'readFile' => false,
571 'writeFile' => false,
572 'copyFile' => false,
573 'moveFile' => false,
574 'renameFile' => false,
575 'deleteFile' => false,
576 'addFolder' => false,
577 'readFolder' => true,
578 'writeFolder' => false,
579 'copyFolder' => true,
580 'moveFolder' => false,
581 'renameFolder' => false,
582 'deleteFolder' => false,
583 'recursivedeleteFolder' => false
584 )
585 ),
586 'Copy folder and remove subfolders allowed' => array(
587 'readFolder,copyFolder,recursivedeleteFolder',
588 array(
589 'addFile' => false,
590 'readFile' => false,
591 'writeFile' => false,
592 'copyFile' => false,
593 'moveFile' => false,
594 'renameFile' => false,
595 'deleteFile' => false,
596 'addFolder' => false,
597 'readFolder' => true,
598 'writeFolder' => false,
599 'copyFolder' => true,
600 'moveFolder' => false,
601 'renameFolder' => false,
602 'deleteFolder' => false,
603 'recursivedeleteFolder' => true
604 )
605 ),
606 );
607 }
608
609 /**
610 * @test
611 * @dataProvider getFilePermissionsTakesUserDefaultPermissionsFromRecordIntoAccountIfUserIsNotAdminDataProvider
612 */
613 public function getFilePermissionsTakesUserDefaultPermissionsFromRecordIntoAccountIfUserIsNotAdmin($permissionValue, $expectedPermissions)
614 {
615 $subject = $this->getMock(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class, array('isAdmin'));
616
617 $subject
618 ->expects($this->any())
619 ->method('isAdmin')
620 ->will($this->returnValue(false));
621
622 $subject->userTS = array();
623 $subject->groupData['file_permissions'] = $permissionValue;
624 $this->assertEquals($expectedPermissions, $subject->getFilePermissions());
625 }
626
627 /**
628 * @test
629 */
630 public function getFilePermissionsGrantsAllPermissionsToAdminUsers()
631 {
632 $subject = $this->getMock(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class, array('isAdmin'));
633
634 $subject
635 ->expects($this->any())
636 ->method('isAdmin')
637 ->will($this->returnValue(true));
638
639 $expectedPermissions = array(
640 'addFile' => true,
641 'readFile' => true,
642 'writeFile' => true,
643 'copyFile' => true,
644 'moveFile' => true,
645 'renameFile' => true,
646 'deleteFile' => true,
647 'addFolder' => true,
648 'readFolder' => true,
649 'writeFolder' => true,
650 'copyFolder' => true,
651 'moveFolder' => true,
652 'renameFolder' => true,
653 'deleteFolder' => true,
654 'recursivedeleteFolder' => true
655 );
656
657 $this->assertEquals($expectedPermissions, $subject->getFilePermissions());
658 }
659 }