85ef211071a2f254ab43b2e98910515b13fb5683
[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 Doctrine\DBAL\Driver\Statement;
17 use Prophecy\Argument;
18 use TYPO3\CMS\Core\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
20 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
21 use TYPO3\CMS\Core\Database\Query\Restriction\DefaultRestrictionContainer;
22 use TYPO3\CMS\Core\Resource\File;
23 use TYPO3\CMS\Core\Resource\Folder;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * Testcase for class \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility
28 */
29 class ExtendedFileUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
30 {
31 /**
32 * Sets up this testcase
33 */
34 protected function setUp()
35 {
36 $GLOBALS['LANG'] = $this->getMockBuilder(\TYPO3\CMS\Lang\LanguageService::class)
37 ->setMethods(['sL'])
38 ->getMock();
39 $GLOBALS['TYPO3_DB'] = $this->createMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
40 }
41
42 /**
43 * @test
44 */
45 public function folderHasFilesInUseReturnsTrueIfItHasFiles()
46 {
47 $fileUid = 1;
48 $file = $this->getMockBuilder(File::class)
49 ->setMethods(['getUid'])
50 ->disableOriginalConstructor()
51 ->getMock();
52 $file->expects($this->once())->method('getUid')->will($this->returnValue($fileUid));
53
54 $folder = $this->getMockBuilder(Folder::class)
55 ->setMethods(['getFiles'])
56 ->disableOriginalConstructor()
57 ->getMock();
58 $folder->expects($this->once())
59 ->method('getFiles')->with(0, 0, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, true)
60 ->will($this->returnValue([$file])
61 );
62
63 /** @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility $subject */
64 $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class)
65 ->setMethods(['addFlashMessage'])
66 ->getMock();
67
68 // prophetizing the DB query
69 $expressionBuilderProphet = $this->prophesize(ExpressionBuilder::class);
70 $expressionBuilderProphet->eq(Argument::cetera())->willReturn('1 = 1');
71 $expressionBuilderProphet->neq(Argument::cetera())->willReturn('1 != 1');
72 $expressionBuilderProphet->in(Argument::cetera())->willReturn('uid IN (1)');
73 $databaseStatementProphet = $this->prophesize(Statement::class);
74 $databaseStatementProphet->fetchColumn(Argument::cetera())->willReturn(1);
75 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
76 $queryBuilderProphet->getRestrictions()->willReturn(GeneralUtility::makeInstance(DefaultRestrictionContainer::class));
77 $queryBuilderProphet->count(Argument::cetera())->willReturn($queryBuilderProphet);
78 $queryBuilderProphet->from(Argument::cetera())->willReturn($queryBuilderProphet);
79 $queryBuilderProphet->where(Argument::cetera())->willReturn($queryBuilderProphet);
80 $queryBuilderProphet->createNamedParameter(Argument::cetera())->willReturnArgument(0);
81 $queryBuilderProphet->execute()->willReturn($databaseStatementProphet);
82 $queryBuilderProphet->expr()->willReturn($expressionBuilderProphet->reveal());
83 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
84 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
85 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
86
87 $GLOBALS['LANG']->expects($this->at(0))->method('sL')
88 ->with('LLL:EXT:lang/locallang_core.xlf:message.description.folderNotDeletedHasFilesWithReferences')
89 ->will($this->returnValue('folderNotDeletedHasFilesWithReferences'));
90 $GLOBALS['LANG']->expects($this->at(1))->method('sL')
91 ->with('LLL:EXT:lang/locallang_core.xlf:message.header.folderNotDeletedHasFilesWithReferences')
92 ->will($this->returnValue('folderNotDeletedHasFilesWithReferences'));
93
94 $result = $subject->folderHasFilesInUse($folder);
95 $this->assertTrue($result);
96 }
97
98 /**
99 * @test
100 */
101 public function folderHasFilesInUseReturnsFalseIfItHasNoFiles()
102 {
103 $folder = $this->getMockBuilder(Folder::class)
104 ->setMethods(['getFiles'])
105 ->disableOriginalConstructor()
106 ->getMock();
107 $folder->expects($this->once())->method('getFiles')->with(0, 0, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, true)->will(
108 $this->returnValue([])
109 );
110
111 /** @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility $subject */
112 $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class)
113 ->setMethods(['addFlashMessage'])
114 ->getMock();
115 $this->assertFalse($subject->folderHasFilesInUse($folder));
116 }
117 }