[BUGFIX] Correct storage selection (common prefixes)
[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 * Copyright notice
6 *
7 * (c) 2011-2013 Andreas Wolf <andreas.wolf@ikt-werk.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Testcase for the factory of FAL
29 *
30 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
31 */
32 class FactoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
33
34 /**
35 * @var array A backup of registered singleton instances
36 */
37 protected $singletonInstances = array();
38
39 /**
40 * @var \TYPO3\CMS\Core\Resource\ResourceFactory
41 */
42 private $fixture;
43
44 /**
45 * @var boolean
46 */
47 private $objectCreated = FALSE;
48
49 /**
50 * @var array
51 */
52 private $filesCreated = array();
53
54 public function setUp() {
55 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
56 $this->fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\ResourceFactory', array('dummy'), array(), '', FALSE);
57 }
58
59 public function tearDown() {
60 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
61 foreach ($this->filesCreated as $file) {
62 unlink($file);
63 }
64 }
65
66 /**********************************
67 * Storage Collections
68 **********************************/
69 /**
70 * @test
71 */
72 public function createStorageCollectionObjectCreatesCollectionWithCorrectArguments() {
73 $mockedMount = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
74 $path = uniqid();
75 $name = uniqid();
76 $storageCollection = $this->fixture->createFolderObject($mockedMount, $path, $name, 0);
77 $this->assertSame($mockedMount, $storageCollection->getStorage());
78 $this->assertEquals($path . '/', $storageCollection->getIdentifier());
79 $this->assertEquals($name, $storageCollection->getName());
80 }
81
82 /**********************************
83 * Drivers
84 **********************************/
85 /**
86 * @test
87 */
88 public function getDriverObjectAcceptsDriverClassName() {
89 $mockedDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', array(), array(), '', FALSE);
90 $driverFixtureClass = get_class($mockedDriver);
91 \TYPO3\CMS\Core\Utility\GeneralUtility::addInstance($driverFixtureClass, $mockedDriver);
92 $mockedMount = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
93 $mockedRegistry = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\DriverRegistry');
94 $mockedRegistry->expects($this->once())->method('getDriverClass')->with($this->equalTo($driverFixtureClass))->will($this->returnValue($driverFixtureClass));
95 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\Driver\\DriverRegistry', $mockedRegistry);
96 $obj = $this->fixture->getDriverObject($driverFixtureClass, array());
97 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', $obj);
98 }
99
100 /***********************************
101 * File Handling
102 ***********************************/
103
104 public function directoryDataProviderValidFolderValues() {
105 return array(
106 'relative path' => array('typo3'),
107 'path with PATH_site' => array(PATH_site . 'typo3')
108 );
109 }
110
111 /**
112 * @test
113 * @dataProvider directoryDataProviderValidFolderValues
114 */
115 public function retrieveFileOrFolderObjectReturnsFolderIfPathIsGiven($source) {
116 $storage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('getFile', 'getFolder'), array(), '', FALSE);
117 $storage->expects($this->once())
118 ->method('getFolder')
119 ->with('typo3');
120 $this->fixture->_set('storageInstances', array(0 => $storage));
121 $this->fixture->retrieveFileOrFolderObject($source);
122 }
123
124 /**
125 * @test
126 */
127 public function retrieveFileOrFolderObjectReturnsFileIfPathIsGiven() {
128 $filename = 'typo3temp/4711.txt';
129 $storage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('getFile', 'getFolder'), array(), '', FALSE);
130 $storage->expects($this->once())
131 ->method('getFile')
132 ->with($filename);
133 $this->fixture->_set('storageInstances', array(0 => $storage));
134 // Create and prepare test file
135 \TYPO3\CMS\Core\Utility\GeneralUtility::writeFileToTypo3tempDir(PATH_site . $filename, '42');
136 $this->filesCreated[] = PATH_site . $filename;
137 $this->fixture->retrieveFileOrFolderObject($filename);
138 }
139
140 /***********************************
141 * Storage AutoDetection
142 ***********************************/
143
144 /**
145 * @param array $storageConfiguration
146 * @param string $path
147 * @param integer $expectedStorageId
148 * @test
149 * @dataProvider storageDetectionDataProvider
150 */
151
152 public function findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, $path, $expectedStorageId) {
153 $this->fixture->_set('localDriverStorageCache', $storageConfiguration);
154 $this->assertSame($expectedStorageId, $this->fixture->_callRef('findBestMatchingStorageByLocalPath', $path));
155 }
156
157
158
159 /**
160 * @return array
161 */
162 public function storageDetectionDataProvider() {
163 return array(
164 'NoLocalStoragesReturnDefaultStorage' => array(
165 array(),
166 'my/dummy/Image.png',
167 0
168 ),
169 'NoMatchReturnsDefaultStorage' => array(
170 array(1 => 'fileadmin/', 2 => 'fileadmin2/public/'),
171 'my/dummy/Image.png',
172 0
173 ),
174 'MatchReturnsTheMatch' => array(
175 array(1 => 'fileadmin/', 2 => 'other/public/'),
176 'fileadmin/dummy/Image.png',
177 1
178 ),
179 'TwoFoldersWithSameStartReturnsCorrect' => array(
180 array(1 => 'fileadmin/', 2 => 'fileadmin/public/'),
181 'fileadmin/dummy/Image.png',
182 1
183 ),
184 'NestedStorageReallyReturnsTheBestMatching' => array(
185 array(1 => 'fileadmin/', 2 => 'fileadmin/public/'),
186 'fileadmin/public/Image.png',
187 2
188 ),
189 'CommonPrefixButWrongPath' => array(
190 array(1 => 'fileadmin/', 2 => 'uploads/test/'),
191 'uploads/bogus/dummy.png',
192 0
193 ),
194 'CommonPrefixRightPath' => array(
195 array(1 => 'fileadmin/', 2 => 'uploads/test/'),
196 'uploads/test/dummy.png',
197 2
198 ),
199 );
200 }
201 }
202
203 ?>