[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / FolderTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource;
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
17 use org\bovigo\vfs\vfsStream;
18
19 /**
20 * Testcase for the storage collection class of the TYPO3 FAL
21 */
22 class FolderTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
23 {
24 /**
25 * @var array A backup of registered singleton instances
26 */
27 protected $singletonInstances = [];
28
29 protected $basedir = 'basedir';
30
31 protected function setUp()
32 {
33 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
34 vfsStream::setup($this->basedir);
35 }
36
37 protected function tearDown()
38 {
39 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
40 parent::tearDown();
41 }
42
43 protected function createFolderFixture($path, $name, $mockedStorage = null)
44 {
45 if ($mockedStorage === null) {
46 $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
47 }
48 return new \TYPO3\CMS\Core\Resource\Folder($mockedStorage, $path, $name, 0);
49 }
50
51 /**
52 * @test
53 */
54 public function constructorArgumentsAreAvailableAtRuntime()
55 {
56 $path = $this->getUniqueId();
57 $name = $this->getUniqueId();
58 $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
59 $fixture = $this->createFolderFixture($path, $name, $mockedStorage);
60 $this->assertSame($mockedStorage, $fixture->getStorage());
61 $this->assertStringStartsWith($path, $fixture->getIdentifier());
62 $this->assertSame($name, $fixture->getName());
63 }
64
65 /**
66 * @test
67 */
68 public function propertiesCanBeUpdated()
69 {
70 $fixture = $this->createFolderFixture('/somePath', 'someName');
71 $fixture->updateProperties(['identifier' => '/someOtherPath', 'name' => 'someNewName']);
72 $this->assertSame('someNewName', $fixture->getName());
73 $this->assertSame('/someOtherPath', $fixture->getIdentifier());
74 }
75
76 /**
77 * @test
78 */
79 public function propertiesAreNotUpdatedIfNotSetInInput()
80 {
81 $fixture = $this->createFolderFixture('/somePath/someName/', 'someName');
82 $fixture->updateProperties(['identifier' => '/someOtherPath']);
83 $this->assertSame('someName', $fixture->getName());
84 }
85
86 /**
87 * @test
88 */
89 public function getFilesReturnsArrayWithFilenamesAsKeys()
90 {
91 $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
92 $mockedStorage->expects($this->once())->method('getFilesInFolder')->will($this->returnValue([
93 'somefile.png' => [
94 'name' => 'somefile.png'
95 ],
96 'somefile.jpg' => [
97 'name' => 'somefile.jpg'
98 ]
99 ]
100 ));
101 $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
102
103 $fileList = $fixture->getFiles();
104
105 $this->assertSame(['somefile.png', 'somefile.jpg'], array_keys($fileList));
106 }
107
108 /**
109 * @test
110 */
111 public function getFilesHandsOverRecursiveFALSEifNotExplicitlySet()
112 {
113 $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
114 $mockedStorage
115 ->expects($this->once())
116 ->method('getFilesInFolder')
117 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), false)
118 ->will($this->returnValue([]));
119
120 $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
121 $fixture->getFiles();
122 }
123
124 /**
125 * @test
126 */
127 public function getFilesHandsOverRecursiveTRUEifSet()
128 {
129 $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
130 $mockedStorage
131 ->expects($this->once())
132 ->method('getFilesInFolder')
133 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), true)
134 ->will($this->returnValue([]));
135
136 $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
137 $fixture->getFiles(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, true);
138 }
139
140 /**
141 * @test
142 */
143 public function getSubfolderCallsFactoryWithCorrectArguments()
144 {
145 $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
146 $mockedStorage->expects($this->once())->method('hasFolderInFolder')->with($this->equalTo('someSubfolder'))->will($this->returnValue(true));
147 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Resource\ResourceFactory $mockedFactory */
148 $mockedFactory = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
149 $folderFixture = $this->createFolderFixture(
150 '/somePath/someFolder/',
151 'someFolder',
152 $mockedStorage
153 );
154 $subfolderFixture = $this->createFolderFixture(
155 '/somePath/someSubfolder/',
156 'someSubfolder',
157 $mockedStorage
158 );
159 $mockedStorage->expects($this->once())->method('getFolderInFolder')->will($this->returnValue($subfolderFixture));
160 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance(
161 \TYPO3\CMS\Core\Resource\ResourceFactory::class,
162 $mockedFactory
163 );
164 $this->assertEquals($subfolderFixture, $folderFixture->getSubfolder('someSubfolder'));
165 }
166
167 /**
168 * @test
169 */
170 public function getParentFolderGetsParentFolderFromStorage()
171 {
172 $parentIdentifier = '/parent/';
173 $currentIdentifier = '/parent/current/';
174
175 $parentFolderFixture = $this->createFolderFixture($parentIdentifier, 'parent');
176 $mockedStorage = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\ResourceStorage::class)
177 ->setMethods(['getFolderIdentifierFromFileIdentifier', 'getFolder'])
178 ->disableOriginalConstructor()
179 ->getMock();
180 $mockedStorage->expects($this->once())->method('getFolderIdentifierFromFileIdentifier')->with($currentIdentifier)->will($this->returnValue($parentIdentifier));
181 $mockedStorage->expects($this->once())->method('getFolder')->with($parentIdentifier)->will($this->returnValue($parentFolderFixture));
182
183 $currentFolderFixture = $this->createFolderFixture($currentIdentifier, 'current', $mockedStorage);
184
185 $this->assertSame($parentFolderFixture, $currentFolderFixture->getParentFolder());
186 }
187 }