[BUGFIX] Prevent folder deletion with referenced files
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / File / ExtendedFileUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility\File;
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 use TYPO3\CMS\Core\Resource\Folder;
17 use TYPO3\CMS\Core\Resource\File;
18
19 /**
20 * Testcase for class \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility
21 *
22 * @author Armin RĂ¼diger Vieweg <armin@v.ieweg.de>
23 */
24 class ExtendedFileUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
25
26 /**
27 * Sets up this testcase
28 */
29 protected function setUp() {
30 $GLOBALS['LANG'] = $this->getMock(\TYPO3\CMS\Lang\LanguageService::class, array('sL'));
31 $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array());
32 }
33
34 /**
35 * @test
36 */
37 public function folderHasFilesInUseReturnsTrueIfItHasFiles() {
38 $fileUid = 1;
39 $file = $this->getMock(File::class, array('getUid'), array(), '', FALSE);
40 $file->expects($this->once())->method('getUid')->will($this->returnValue($fileUid));
41
42 $folder = $this->getMock(Folder::class, array('getFiles'), array(), '', FALSE);
43 $folder->expects($this->once())
44 ->method('getFiles')->with(0, 0, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, TRUE)
45 ->will($this->returnValue(array($file))
46 );
47
48 /** @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility $subject */
49 $subject = $this->getMock(\TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class, array('addFlashMessage'), array(), '');
50 $GLOBALS['TYPO3_DB']->expects($this->once())
51 ->method('exec_SELECTcountRows')->with('*', 'sys_refindex', 'deleted=0 AND ref_table="sys_file" AND ref_uid IN (' . $fileUid . ') AND tablename<>"sys_file_metadata"')
52 ->will($this->returnValue(1));
53
54 $GLOBALS['LANG']->expects($this->at(0))->method('sL')
55 ->with('LLL:EXT:lang/locallang_core.xlf:message.description.folderNotDeletedHasFilesWithReferences')
56 ->will($this->returnValue('folderNotDeletedHasFilesWithReferences'));
57 $GLOBALS['LANG']->expects($this->at(1))->method('sL')
58 ->with('LLL:EXT:lang/locallang_core.xlf:message.header.folderNotDeletedHasFilesWithReferences')
59 ->will($this->returnValue('folderNotDeletedHasFilesWithReferences'));
60
61 $result = $subject->folderHasFilesInUse($folder);
62 $this->assertTrue($result);
63 }
64
65 /**
66 * @test
67 */
68 public function folderHasFilesInUseReturnsFalseIfItHasNoFiles() {
69 $folder = $this->getMock(Folder::class, array('getFiles'), array(), '', FALSE);
70 $folder->expects($this->once())->method('getFiles')->with(0, 0, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, TRUE)->will(
71 $this->returnValue(array())
72 );
73
74 /** @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility $subject */
75 $subject = $this->getMock(\TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class, array('addFlashMessage'), array(), '');
76 $this->assertFalse($subject->folderHasFilesInUse($folder));
77 }
78
79 }