[BUGFIX] Do not append a "/" to the folder identifier
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / ResourceFactoryTest.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 /**
18 * Testcase for the factory of FAL
19 */
20 class ResourceFactoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
21 {
22 /**
23 * @var array A backup of registered singleton instances
24 */
25 protected $singletonInstances = array();
26
27 /**
28 * @var \TYPO3\CMS\Core\Resource\ResourceFactory
29 */
30 protected $subject;
31
32 /**
33 * @var array
34 */
35 protected $filesCreated = array();
36
37 protected function setUp()
38 {
39 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
40 $this->subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Resource\ResourceFactory::class, array('dummy'), array(), '', false);
41 }
42
43 protected function tearDown()
44 {
45 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
46 foreach ($this->filesCreated as $file) {
47 unlink($file);
48 }
49 parent::tearDown();
50 }
51
52 /**********************************
53 * Storage Collections
54 **********************************/
55 /**
56 * @test
57 */
58 public function createStorageCollectionObjectCreatesCollectionWithCorrectArguments()
59 {
60 $mockedMount = $this->getMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class, array(), array(), '', false);
61 $path = $this->getUniqueId();
62 $name = $this->getUniqueId();
63 $storageCollection = $this->subject->createFolderObject($mockedMount, $path, $name, 0);
64 $this->assertSame($mockedMount, $storageCollection->getStorage());
65 $this->assertEquals($path, $storageCollection->getIdentifier());
66 $this->assertEquals($name, $storageCollection->getName());
67 }
68
69 /**********************************
70 * Drivers
71 **********************************/
72 /**
73 * @test
74 */
75 public function getDriverObjectAcceptsDriverClassName()
76 {
77 $mockedDriver = $this->getMockForAbstractClass(\TYPO3\CMS\Core\Resource\Driver\AbstractDriver::class);
78 $driverFixtureClass = get_class($mockedDriver);
79 \TYPO3\CMS\Core\Utility\GeneralUtility::addInstance($driverFixtureClass, $mockedDriver);
80 $mockedMount = $this->getMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class, array(), array(), '', false);
81 $mockedRegistry = $this->getMock(\TYPO3\CMS\Core\Resource\Driver\DriverRegistry::class);
82 $mockedRegistry->expects($this->once())->method('getDriverClass')->with($this->equalTo($driverFixtureClass))->will($this->returnValue($driverFixtureClass));
83 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Resource\Driver\DriverRegistry::class, $mockedRegistry);
84 $obj = $this->subject->getDriverObject($driverFixtureClass, array());
85 $this->assertInstanceOf(\TYPO3\CMS\Core\Resource\Driver\AbstractDriver::class, $obj);
86 }
87
88 /***********************************
89 * File Handling
90 ***********************************/
91
92 /**
93 * @test
94 */
95 public function retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithRelativePath()
96 {
97 /** @var $subject \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\TYPO3\CMS\Core\Resource\ResourceFactory */
98 $subject = $this->getAccessibleMock(
99 \TYPO3\CMS\Core\Resource\ResourceFactory::class,
100 array('getFolderObjectFromCombinedIdentifier'),
101 array(),
102 '',
103 false
104 );
105 $subject
106 ->expects($this->once())
107 ->method('getFolderObjectFromCombinedIdentifier')
108 ->with('typo3');
109 $subject->retrieveFileOrFolderObject('typo3');
110 }
111
112 /**
113 * @test
114 */
115 public function retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithAbsolutePath()
116 {
117 /** @var $subject \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\TYPO3\CMS\Core\Resource\ResourceFactory */
118 $subject = $this->getAccessibleMock(
119 \TYPO3\CMS\Core\Resource\ResourceFactory::class,
120 array('getFolderObjectFromCombinedIdentifier'),
121 array(),
122 '',
123 false
124 );
125 $subject
126 ->expects($this->once())
127 ->method('getFolderObjectFromCombinedIdentifier')
128 ->with('typo3');
129 $subject->retrieveFileOrFolderObject(PATH_site . 'typo3');
130 }
131
132 /**
133 * @test
134 */
135 public function retrieveFileOrFolderObjectReturnsFileIfPathIsGiven()
136 {
137 $this->subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Resource\ResourceFactory::class, array('getFileObjectFromCombinedIdentifier'), array(), '', false);
138 $filename = 'typo3temp/4711.txt';
139 $this->subject->expects($this->once())
140 ->method('getFileObjectFromCombinedIdentifier')
141 ->with($filename);
142 // Create and prepare test file
143 \TYPO3\CMS\Core\Utility\GeneralUtility::writeFileToTypo3tempDir(PATH_site . $filename, '42');
144 $this->filesCreated[] = PATH_site . $filename;
145 $this->subject->retrieveFileOrFolderObject($filename);
146 }
147
148 /***********************************
149 * Storage AutoDetection
150 ***********************************/
151
152 /**
153 * @param array $storageConfiguration
154 * @param string $path
155 * @param int $expectedStorageId
156 * @test
157 * @dataProvider storageDetectionDataProvider
158 */
159 public function findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, $path, $expectedStorageId)
160 {
161 $this->subject->_set('localDriverStorageCache', $storageConfiguration);
162 $this->assertSame($expectedStorageId, $this->subject->_callRef('findBestMatchingStorageByLocalPath', $path));
163 }
164
165 /**
166 * @return array
167 */
168 public function storageDetectionDataProvider()
169 {
170 return array(
171 'NoLocalStoragesReturnDefaultStorage' => array(
172 array(),
173 'my/dummy/Image.png',
174 0
175 ),
176 'NoMatchReturnsDefaultStorage' => array(
177 array(1 => 'fileadmin/', 2 => 'fileadmin2/public/'),
178 'my/dummy/Image.png',
179 0
180 ),
181 'MatchReturnsTheMatch' => array(
182 array(1 => 'fileadmin/', 2 => 'other/public/'),
183 'fileadmin/dummy/Image.png',
184 1
185 ),
186 'TwoFoldersWithSameStartReturnsCorrect' => array(
187 array(1 => 'fileadmin/', 2 => 'fileadmin/public/'),
188 'fileadmin/dummy/Image.png',
189 1
190 ),
191 'NestedStorageReallyReturnsTheBestMatching' => array(
192 array(1 => 'fileadmin/', 2 => 'fileadmin/public/'),
193 'fileadmin/public/Image.png',
194 2
195 ),
196 'CommonPrefixButWrongPath' => array(
197 array(1 => 'fileadmin/', 2 => 'uploads/test/'),
198 'uploads/bogus/dummy.png',
199 0
200 ),
201 'CommonPrefixRightPath' => array(
202 array(1 => 'fileadmin/', 2 => 'uploads/test/'),
203 'uploads/test/dummy.png',
204 2
205 ),
206 'FindStorageFromWindowsPath' => array(
207 array(1 => 'fileadmin/', 2 => 'uploads/test/'),
208 'uploads\\test\\dummy.png',
209 2
210 ),
211 );
212 }
213 }