‪TYPO3CMS  10.4
ExtendedFileUtilityTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Doctrine\DBAL\Driver\Statement;
19 use Prophecy\Argument;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
34 class ‪ExtendedFileUtilityTest extends UnitTestCase
35 {
39  protected function ‪setUp(): void
40  {
41  parent::setUp();
42  ‪$GLOBALS['LANG'] = $this->getMockBuilder(LanguageService::class)
43  ->disableOriginalConstructor()
44  ->setMethods(['sL'])
45  ->getMock();
46  }
47 
52  {
53  $fileUid = 1;
54  $file = $this->getMockBuilder(File::class)
55  ->setMethods(['getUid'])
56  ->disableOriginalConstructor()
57  ->getMock();
58  $file->expects(self::once())->method('getUid')->willReturn($fileUid);
59 
60  $folder = $this->getMockBuilder(Folder::class)
61  ->setMethods(['getFiles'])
62  ->disableOriginalConstructor()
63  ->getMock();
64  $folder->expects(self::once())
65  ->method('getFiles')->with(0, 0, ‪Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, true)
66  ->willReturn(
67  [$file]
68  );
69 
71  $subject = $this->getMockBuilder(ExtendedFileUtility::class)
72  ->setMethods(['addFlashMessage'])
73  ->getMock();
74 
75  // prophetizing the DB query
76  $expressionBuilderProphet = $this->prophesize(ExpressionBuilder::class);
77  $expressionBuilderProphet->eq(Argument::cetera())->willReturn('1 = 1');
78  $expressionBuilderProphet->neq(Argument::cetera())->willReturn('1 != 1');
79  $expressionBuilderProphet->in(Argument::cetera())->willReturn('uid IN (1)');
80  $databaseStatementProphet = $this->prophesize(Statement::class);
81  $databaseStatementProphet->fetchColumn(Argument::cetera())->willReturn(1);
82  $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
83  $queryBuilderProphet->getRestrictions()->willReturn(GeneralUtility::makeInstance(DefaultRestrictionContainer::class));
84  $queryBuilderProphet->count(Argument::cetera())->willReturn($queryBuilderProphet);
85  $queryBuilderProphet->from(Argument::cetera())->willReturn($queryBuilderProphet);
86  $queryBuilderProphet->where(Argument::cetera())->willReturn($queryBuilderProphet);
87  $queryBuilderProphet->createNamedParameter(Argument::cetera())->willReturn(Argument::type('string'));
88  $queryBuilderProphet->execute()->willReturn($databaseStatementProphet);
89  $queryBuilderProphet->expr()->willReturn($expressionBuilderProphet->reveal());
90  $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
91  $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
92  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
93 
94  ‪$GLOBALS['LANG']->expects(self::exactly(2))->method('sL')
95  ->withConsecutive(
96  ['LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.description.folderNotDeletedHasFilesWithReferences'],
97  ['LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.header.folderNotDeletedHasFilesWithReferences']
98  )
99  ->willReturnOnConsecutiveCalls(
100  'folderNotDeletedHasFilesWithReferences',
101  'folderNotDeletedHasFilesWithReferences'
102  );
103 
104  $result = $subject->folderHasFilesInUse($folder);
105  self::assertTrue($result);
106  }
107 
112  {
113  $folder = $this->getMockBuilder(Folder::class)
114  ->setMethods(['getFiles'])
115  ->disableOriginalConstructor()
116  ->getMock();
117  $folder->expects(self::once())->method('getFiles')->with(0, 0, ‪Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, true)->willReturn(
118  []
119  );
120 
122  $subject = $this->getMockBuilder(ExtendedFileUtility::class)
123  ->setMethods(['addFlashMessage'])
124  ->getMock();
125  self::assertFalse($subject->folderHasFilesInUse($folder));
126  }
127 }
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:35
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
‪const FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
Definition: Folder.php:69
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:52
‪TYPO3\CMS\Core\Tests\Unit\Utility\File\ExtendedFileUtilityTest\folderHasFilesInUseReturnsTrueIfItHasFiles
‪folderHasFilesInUseReturnsTrueIfItHasFiles()
Definition: ExtendedFileUtilityTest.php:51
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility
Definition: ExtendedFileUtility.php:70
‪TYPO3\CMS\Core\Tests\Unit\Utility\File
Definition: ExtendedFileUtilityTest.php:16
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Tests\Unit\Utility\File\ExtendedFileUtilityTest
Definition: ExtendedFileUtilityTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Utility\File\ExtendedFileUtilityTest\setUp
‪setUp()
Definition: ExtendedFileUtilityTest.php:39
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Tests\Unit\Utility\File\ExtendedFileUtilityTest\folderHasFilesInUseReturnsFalseIfItHasNoFiles
‪folderHasFilesInUseReturnsFalseIfItHasNoFiles()
Definition: ExtendedFileUtilityTest.php:111
‪TYPO3\CMS\Core\Database\Query\Restriction\DefaultRestrictionContainer
Definition: DefaultRestrictionContainer.php:24