[TASK] Remove TYPO3_DB from Tests
[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 }
40
41 /**
42 * @test
43 */
44 public function folderHasFilesInUseReturnsTrueIfItHasFiles()
45 {
46 $fileUid = 1;
47 $file = $this->getMockBuilder(File::class)
48 ->setMethods(['getUid'])
49 ->disableOriginalConstructor()
50 ->getMock();
51 $file->expects($this->once())->method('getUid')->will($this->returnValue($fileUid));
52
53 $folder = $this->getMockBuilder(Folder::class)
54 ->setMethods(['getFiles'])
55 ->disableOriginalConstructor()
56 ->getMock();
57 $folder->expects($this->once())
58 ->method('getFiles')->with(0, 0, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, true)
59 ->will($this->returnValue([$file])
60 );
61
62 /** @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility $subject */
63 $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class)
64 ->setMethods(['addFlashMessage'])
65 ->getMock();
66
67 // prophetizing the DB query
68 $expressionBuilderProphet = $this->prophesize(ExpressionBuilder::class);
69 $expressionBuilderProphet->eq(Argument::cetera())->willReturn('1 = 1');
70 $expressionBuilderProphet->neq(Argument::cetera())->willReturn('1 != 1');
71 $expressionBuilderProphet->in(Argument::cetera())->willReturn('uid IN (1)');
72 $databaseStatementProphet = $this->prophesize(Statement::class);
73 $databaseStatementProphet->fetchColumn(Argument::cetera())->willReturn(1);
74 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
75 $queryBuilderProphet->getRestrictions()->willReturn(GeneralUtility::makeInstance(DefaultRestrictionContainer::class));
76 $queryBuilderProphet->count(Argument::cetera())->willReturn($queryBuilderProphet);
77 $queryBuilderProphet->from(Argument::cetera())->willReturn($queryBuilderProphet);
78 $queryBuilderProphet->where(Argument::cetera())->willReturn($queryBuilderProphet);
79 $queryBuilderProphet->createNamedParameter(Argument::cetera())->willReturnArgument(0);
80 $queryBuilderProphet->execute()->willReturn($databaseStatementProphet);
81 $queryBuilderProphet->expr()->willReturn($expressionBuilderProphet->reveal());
82 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
83 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
84 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
85
86 $GLOBALS['LANG']->expects($this->at(0))->method('sL')
87 ->with('LLL:EXT:lang/locallang_core.xlf:message.description.folderNotDeletedHasFilesWithReferences')
88 ->will($this->returnValue('folderNotDeletedHasFilesWithReferences'));
89 $GLOBALS['LANG']->expects($this->at(1))->method('sL')
90 ->with('LLL:EXT:lang/locallang_core.xlf:message.header.folderNotDeletedHasFilesWithReferences')
91 ->will($this->returnValue('folderNotDeletedHasFilesWithReferences'));
92
93 $result = $subject->folderHasFilesInUse($folder);
94 $this->assertTrue($result);
95 }
96
97 /**
98 * @test
99 */
100 public function folderHasFilesInUseReturnsFalseIfItHasNoFiles()
101 {
102 $folder = $this->getMockBuilder(Folder::class)
103 ->setMethods(['getFiles'])
104 ->disableOriginalConstructor()
105 ->getMock();
106 $folder->expects($this->once())->method('getFiles')->with(0, 0, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, true)->will(
107 $this->returnValue([])
108 );
109
110 /** @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility $subject */
111 $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class)
112 ->setMethods(['addFlashMessage'])
113 ->getMock();
114 $this->assertFalse($subject->folderHasFilesInUse($folder));
115 }
116 }