[TASK] Shorten the extend class in all unit 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 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26
27 /**
28 * Testcase for class \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility
29 */
30 class ExtendedFileUtilityTest extends UnitTestCase
31 {
32 /**
33 * Sets up this testcase
34 */
35 protected function setUp()
36 {
37 $GLOBALS['LANG'] = $this->getMockBuilder(\TYPO3\CMS\Core\Localization\LanguageService::class)
38 ->setMethods(['sL'])
39 ->getMock();
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(
61 $this->returnValue([$file])
62 );
63
64 /** @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility $subject */
65 $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class)
66 ->setMethods(['addFlashMessage'])
67 ->getMock();
68
69 // prophetizing the DB query
70 $expressionBuilderProphet = $this->prophesize(ExpressionBuilder::class);
71 $expressionBuilderProphet->eq(Argument::cetera())->willReturn('1 = 1');
72 $expressionBuilderProphet->neq(Argument::cetera())->willReturn('1 != 1');
73 $expressionBuilderProphet->in(Argument::cetera())->willReturn('uid IN (1)');
74 $databaseStatementProphet = $this->prophesize(Statement::class);
75 $databaseStatementProphet->fetchColumn(Argument::cetera())->willReturn(1);
76 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
77 $queryBuilderProphet->getRestrictions()->willReturn(GeneralUtility::makeInstance(DefaultRestrictionContainer::class));
78 $queryBuilderProphet->count(Argument::cetera())->willReturn($queryBuilderProphet);
79 $queryBuilderProphet->from(Argument::cetera())->willReturn($queryBuilderProphet);
80 $queryBuilderProphet->where(Argument::cetera())->willReturn($queryBuilderProphet);
81 $queryBuilderProphet->createNamedParameter(Argument::cetera())->willReturn(Argument::type('string'));
82 $queryBuilderProphet->execute()->willReturn($databaseStatementProphet);
83 $queryBuilderProphet->expr()->willReturn($expressionBuilderProphet->reveal());
84 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
85 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
86 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
87
88 $GLOBALS['LANG']->expects($this->at(0))->method('sL')
89 ->with('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.description.folderNotDeletedHasFilesWithReferences')
90 ->will($this->returnValue('folderNotDeletedHasFilesWithReferences'));
91 $GLOBALS['LANG']->expects($this->at(1))->method('sL')
92 ->with('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.header.folderNotDeletedHasFilesWithReferences')
93 ->will($this->returnValue('folderNotDeletedHasFilesWithReferences'));
94
95 $result = $subject->folderHasFilesInUse($folder);
96 $this->assertTrue($result);
97 }
98
99 /**
100 * @test
101 */
102 public function folderHasFilesInUseReturnsFalseIfItHasNoFiles()
103 {
104 $folder = $this->getMockBuilder(Folder::class)
105 ->setMethods(['getFiles'])
106 ->disableOriginalConstructor()
107 ->getMock();
108 $folder->expects($this->once())->method('getFiles')->with(0, 0, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, true)->will(
109 $this->returnValue([])
110 );
111
112 /** @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility $subject */
113 $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class)
114 ->setMethods(['addFlashMessage'])
115 ->getMock();
116 $this->assertFalse($subject->folderHasFilesInUse($folder));
117 }
118 }