[TASK] Removes extra empty lines
[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
160 public function findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, $path, $expectedStorageId)
161 {
162 $this->subject->_set('localDriverStorageCache', $storageConfiguration);
163 $this->assertSame($expectedStorageId, $this->subject->_callRef('findBestMatchingStorageByLocalPath', $path));
164 }
165
166 /**
167 * @return array
168 */
169 public function storageDetectionDataProvider()
170 {
171 return array(
172 'NoLocalStoragesReturnDefaultStorage' => array(
173 array(),
174 'my/dummy/Image.png',
175 0
176 ),
177 'NoMatchReturnsDefaultStorage' => array(
178 array(1 => 'fileadmin/', 2 => 'fileadmin2/public/'),
179 'my/dummy/Image.png',
180 0
181 ),
182 'MatchReturnsTheMatch' => array(
183 array(1 => 'fileadmin/', 2 => 'other/public/'),
184 'fileadmin/dummy/Image.png',
185 1
186 ),
187 'TwoFoldersWithSameStartReturnsCorrect' => array(
188 array(1 => 'fileadmin/', 2 => 'fileadmin/public/'),
189 'fileadmin/dummy/Image.png',
190 1
191 ),
192 'NestedStorageReallyReturnsTheBestMatching' => array(
193 array(1 => 'fileadmin/', 2 => 'fileadmin/public/'),
194 'fileadmin/public/Image.png',
195 2
196 ),
197 'CommonPrefixButWrongPath' => array(
198 array(1 => 'fileadmin/', 2 => 'uploads/test/'),
199 'uploads/bogus/dummy.png',
200 0
201 ),
202 'CommonPrefixRightPath' => array(
203 array(1 => 'fileadmin/', 2 => 'uploads/test/'),
204 'uploads/test/dummy.png',
205 2
206 ),
207 'FindStorageFromWindowsPath' => array(
208 array(1 => 'fileadmin/', 2 => 'uploads/test/'),
209 'uploads\\test\\dummy.png',
210 2
211 ),
212 );
213 }
214 }