[BUGFIX] LocalDriver can be instantiated without basePath
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / FileTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 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 require_once 'vfsStream/vfsStream.php';
28
29 /**
30 * Testcase for the file class of the TYPO3 FAL
31 *
32 * @package TYPO3
33 * @subpackage t3lib
34 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
35 * @todo Many, many, many tests are skipped in this test case...
36 */
37 class FileTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
38
39 /**
40 * @var array A backup of registered singleton instances
41 */
42 protected $singletonInstances = array();
43
44 public function setUp() {
45 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
46 \TYPO3\CMS\Core\Utility\GeneralUtility::purgeInstances();
47 }
48
49 public function tearDown() {
50 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
51 }
52
53 /**
54 * @return \TYPO3\CMS\Core\Resource\File
55 */
56 protected function prepareFixture() {
57 $fixture = new \TYPO3\CMS\Core\Resource\File('testfile');
58 return $fixture;
59 }
60
61 /**
62 * @test
63 * @todo Old code getAvailableProperties() needs to be replaced by current behaviour
64 */
65 public function propertiesPassedToConstructorAreAvailableViaGenericGetter() {
66 $this->markTestSkipped('TYPO3\\CMS\\Core\\Resource\\File::getAvailableProperties() does not exist');
67 $properties = array(
68 uniqid() => uniqid(),
69 uniqid() => uniqid(),
70 'uid' => 1
71 );
72 $fixture = new \TYPO3\CMS\Core\Resource\File($properties);
73 $availablePropertiesBackup = \TYPO3\CMS\Core\Resource\File::getAvailableProperties();
74 \TYPO3\CMS\Core\Resource\File::setAvailableProperties(array_keys($properties));
75 foreach ($properties as $key => $value) {
76 $this->assertTrue($fixture->hasProperty($key));
77 $this->assertEquals($value, $fixture->getProperty($key));
78 }
79 $this->assertFalse($fixture->hasProperty(uniqid()));
80 \TYPO3\CMS\Core\Resource\File::setAvailableProperties($availablePropertiesBackup);
81 $this->setExpectedException('InvalidArgumentException', '', 1314226805);
82 $fixture->getProperty(uniqid());
83 }
84
85 /**
86 * @test
87 */
88 public function commonPropertiesAreAvailableWithOwnGetters() {
89 $properties = array(
90 'name' => uniqid(),
91 'storage' => $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE),
92 'size' => 1024
93 );
94 $fixture = new \TYPO3\CMS\Core\Resource\File($properties);
95 foreach ($properties as $key => $value) {
96 $this->assertEquals($value, call_user_func(array($fixture, 'get' . $key)));
97 }
98 }
99
100 /**
101 * @test
102 */
103 public function fileAsksRepositoryForIndexStatus() {
104 $mockedRepository = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileRepository');
105 $mockedRepository->expects($this->once())->method('getFileIndexRecord')->will($this->returnValue(array()));
106 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository', $mockedRepository);
107 $fixture = new \TYPO3\CMS\Core\Resource\File(array());
108 $this->assertTrue($fixture->isIndexed());
109 }
110
111 /**
112 * Tests if a file is seen as indexed if the record has a uid
113 *
114 * @test
115 */
116 public function fileIndexStatusIsTrueIfUidIsSet() {
117 $fixture = new \TYPO3\CMS\Core\Resource\File(array('uid' => 1));
118 $this->assertTrue($fixture->isIndexed());
119 }
120
121 /**
122 * @test
123 */
124 public function updatePropertiesUpdatesFileProperties() {
125 $identifier = '/' . uniqid();
126 $fixture = new \TYPO3\CMS\Core\Resource\File(array('uid' => 1, 'identifier' => '/test'));
127 $fixture->updateProperties(array('identifier' => $identifier));
128 $this->assertEquals($identifier, $fixture->getIdentifier());
129 }
130
131 /**
132 * @test
133 */
134 public function updatePropertiesLeavesPropertiesUntouchedIfNotSetInNewProperties() {
135 $fixture = new \TYPO3\CMS\Core\Resource\File(array('uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'));
136 $fixture->updateProperties(array('foo' => 'foobar'));
137 $this->assertEquals('/test', $fixture->getIdentifier());
138 $this->assertEquals('/test', $fixture->getProperty('identifier'));
139 }
140
141 /**
142 * @test
143 */
144 public function updatePropertiesDiscardsUidIfAlreadySet() {
145 $fixture = new \TYPO3\CMS\Core\Resource\File(array('uid' => 1, 'identifier' => '/test'));
146 $fixture->updateProperties(array('uid' => 3));
147 $this->assertEquals(1, $fixture->getUid());
148 }
149
150 /**
151 * @test
152 */
153 public function updatePropertiesRecordsNamesOfChangedProperties() {
154 $fixture = new \TYPO3\CMS\Core\Resource\File(array('uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test'));
155 $fixture->updateProperties(array('foo' => 'foobar', 'baz' => 'foobaz'));
156 $this->assertEquals(array('foo', 'baz'), $fixture->getUpdatedProperties());
157 }
158
159 /**
160 * @test
161 */
162 public function updatePropertiesDoesNotRecordPropertyNameIfSameValueIsProvided() {
163 $fixture = new \TYPO3\CMS\Core\Resource\File(array('uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'));
164 $fixture->updateProperties(array('foo' => 'asdf'));
165 $this->assertEmpty($fixture->getUpdatedProperties());
166 }
167
168 /**
169 * @test
170 */
171 public function updatePropertiesMarksPropertyAsChangedOnlyOnce() {
172 $fixture = new \TYPO3\CMS\Core\Resource\File(array('uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test'));
173 $fixture->updateProperties(array('foo' => 'foobar', 'baz' => 'foobaz'));
174 $fixture->updateProperties(array('foo' => 'fdsw', 'baz' => 'asdf'));
175 $this->assertEquals(array('foo', 'baz'), $fixture->getUpdatedProperties());
176 }
177
178 /**
179 * @test
180 */
181 public function updatePropertiesReloadsStorageObjectIfStorageChanges() {
182 $mockedNewStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
183 $mockedOldStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
184 $fixture = new \TYPO3\CMS\Core\Resource\File(array('uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test', 'storage' => $mockedOldStorage));
185 $mockedRepository = $this->getMock('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
186 $mockedRepository->expects($this->once())->method('findByUid')->with(2)->will($this->returnValue($mockedNewStorage));
187 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository', $mockedRepository);
188 $fixture->updateProperties(array('storage' => 2));
189 $this->assertSame($mockedNewStorage, $fixture->getStorage());
190 }
191
192 /**
193 * @test
194 */
195 public function fetchingIndexedPropertyCausesFileObjectToLoadIndexRecord() {
196 $fixture = new \TYPO3\CMS\Core\Resource\File(array('identifier' => '/test', 'storage' => 1));
197 $mockedRepository = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileRepository');
198 $mockedRepository->expects($this->once())->method('getFileIndexRecord')->will($this->returnValue(array('uid' => 10)));
199 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository', $mockedRepository);
200 $this->assertEquals(10, $fixture->getProperty('uid'));
201 }
202
203 /**
204 * @test
205 */
206 public function isIndexedTriggersIndexingIfFileIsNotIndexedAlready() {
207 $fixture = new \TYPO3\CMS\Core\Resource\File(array('identifier' => '/test', 'storage' => 1));
208 $mockedRepository = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileRepository');
209 $mockedRepository->expects($this->once())->method('getFileIndexRecord')->will($this->returnValue(FALSE));
210 $mockedRepository->expects($this->once())->method('addToIndex')->will($this->returnValue(array('uid' => 10)));
211 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository', $mockedRepository);
212 $fixture->isIndexed();
213 }
214
215 /**
216 * @test
217 */
218 public function fileIsAutomaticallyIndexedOnPropertyAccessIfNotAlreadyIndexed() {
219 $fixture = new \TYPO3\CMS\Core\Resource\File(array('identifier' => '/test', 'storage' => 1));
220 $mockedRepository = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileRepository');
221 $mockedRepository->expects($this->once())->method('getFileIndexRecord')->will($this->returnValue(FALSE));
222 $mockedRepository->expects($this->once())->method('addToIndex')->will($this->returnValue(array('uid' => 10)));
223 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository', $mockedRepository);
224 $this->assertEquals(10, $fixture->getProperty('uid'));
225 }
226
227 /**
228 * @test
229 */
230 public function copyToCallsCopyOperationOnTargetFolderStorage() {
231 $targetStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
232 $targetFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
233 $targetFolder->expects($this->any())->method('getStorage')->will($this->returnValue($targetStorage));
234 $fixture = new \TYPO3\CMS\Core\Resource\File(array());
235 $targetStorage->expects($this->once())->method('copyFile')->with($this->equalTo($fixture), $this->equalTo($targetFolder));
236 $fixture->copyTo($targetFolder);
237 }
238
239 /**
240 * @test
241 */
242 public function moveToCallsMoveOperationOnTargetFolderStorage() {
243 $targetStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
244 $targetFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
245 $targetFolder->expects($this->any())->method('getStorage')->will($this->returnValue($targetStorage));
246 $fixture = new \TYPO3\CMS\Core\Resource\File(array());
247 $targetStorage->expects($this->once())->method('moveFile')->with($this->equalTo($fixture), $this->equalTo($targetFolder));
248 $fixture->moveTo($targetFolder);
249 }
250
251 /**
252 * @test
253 */
254 public function openCorrectlyOpensFileInDriver() {
255 $this->markTestSkipped();
256 $fixture = $this->prepareFixture();
257 $fileMode = 'invalidMode';
258 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
259 $mockDriver->expects($this->atLeastOnce())->method('getFileHandle')->with($this->equalTo($fixture), $this->equalTo($fileMode));
260 $fixture->setStorageDriver($mockDriver);
261 $fixture->open($fileMode);
262 }
263
264 /**
265 * @test
266 */
267 public function isOpenReturnsCorrectValuesForClosedAndOpenFile() {
268 $this->markTestSkipped();
269 $fixture = $this->prepareFixture();
270 $fileMode = 'r';
271 $mockFileHandle = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileHandle', array(), array(), '', FALSE);
272 $mockFileHandle->expects($this->any())->method('isOpen')->will($this->returnValue(TRUE));
273 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
274 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
275 $fixture->setStorageDriver($mockDriver);
276 $this->assertFalse($fixture->isOpen());
277 $fixture->open($fileMode);
278 $this->assertTrue($fixture->isOpen());
279 }
280
281 /**
282 * @test
283 */
284 public function fileIsCorrectlyClosed() {
285 $this->markTestSkipped();
286 $fixture = $this->prepareFixture();
287 $fileMode = 'r';
288 $mockFileHandle = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileHandle', array(), array(), '', FALSE);
289 $mockFileHandle->expects($this->once())->method('close');
290 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
291 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
292 $fixture->setStorageDriver($mockDriver);
293 $fixture->open($fileMode);
294 $fixture->close();
295 $this->assertFalse($fixture->isOpen());
296 }
297
298 /**
299 * @test
300 */
301 public function readReturnsRequestedContentsFromDriver() {
302 $this->markTestSkipped();
303 $fixture = $this->prepareFixture();
304 $fileMode = 'r';
305 $fileContents = 'Some random file contents.';
306 $bytesToRead = 10;
307 $mockFileHandle = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileHandle', array(), array(), '', FALSE);
308 $mockFileHandle->expects($this->any())->method('isOpen')->will($this->returnValue(TRUE));
309 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
310 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
311 $mockDriver->expects($this->once())->method('readFromFile')->with($this->anything(), $this->equalTo($bytesToRead))->will($this->returnValue(substr($fileContents, 0, $bytesToRead)));
312 $fixture->setStorageDriver($mockDriver);
313 $fixture->open($fileMode);
314 $this->assertEquals(substr($fileContents, 0, $bytesToRead), $fixture->read($bytesToRead));
315 }
316
317 /**
318 * @test
319 */
320 public function readFailsIfFileIsClosed() {
321 $this->markTestSkipped();
322 $this->setExpectedException('\RuntimeException', '', 1299863431);
323 $fixture = $this->prepareFixture();
324 $mockFileHandle = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileHandle', array(), array(), '', FALSE);
325 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
326 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
327 $fixture->setStorageDriver($mockDriver);
328 $fixture->read(1);
329 }
330
331 /**
332 * @test
333 */
334 public function writePassesContentsToDriver() {
335 $this->markTestSkipped();
336 $fixture = $this->prepareFixture();
337 $fileMode = 'r+';
338 $fileContents = 'Some random file contents.';
339 $mockFileHandle = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileHandle', array(), array(), '', FALSE);
340 $mockFileHandle->expects($this->any())->method('isOpen')->will($this->returnValue(TRUE));
341 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
342 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
343 $mockDriver->expects($this->once())->method('writeToFile')->with($this->anything(), $this->equalTo($fileContents))->will($this->returnValue(TRUE));
344 $fixture->setStorageDriver($mockDriver);
345 $fixture->open($fileMode);
346 $this->assertTrue($fixture->write($fileContents));
347 }
348
349 /**
350 * @test
351 */
352 public function writeFailsIfFileIsClosed() {
353 $this->markTestSkipped();
354 $this->setExpectedException('\RuntimeException', '', 1299863432);
355 $fixture = $this->prepareFixture();
356 $mockFileHandle = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileHandle', array(), array(), '', FALSE);
357 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
358 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
359 $fixture->setStorageDriver($mockDriver);
360 $fixture->write('asdf');
361 }
362
363 public function filenameExtensionDataProvider() {
364 return array(
365 array('somefile.jpg', 'somefile', 'jpg'),
366 array('SomeFile.PNG', 'SomeFile', 'png'),
367 array('somefile', 'somefile', ''),
368 array('somefile.tar.gz', 'somefile.tar', 'gz'),
369 array('somefile.tar.bz2', 'somefile.tar', 'bz2'),
370 );
371 }
372
373 /**
374 * @test
375 * @dataProvider filenameExtensionDataProvider
376 */
377 public function getNameWithoutExtensionReturnsCorrectName($originalFilename, $expectedBasename) {
378 $fixture = new \TYPO3\CMS\Core\Resource\File(array(
379 'name' => $originalFilename,
380 'identifier' => '/' . $originalFilename
381 ));
382 $this->assertSame($expectedBasename, $fixture->getNameWithoutExtension());
383 }
384
385 /**
386 * @test
387 * @dataProvider filenameExtensionDataProvider
388 */
389 public function getExtensionReturnsCorrectExtension($originalFilename, $expectedBasename, $expectedExtension) {
390 $fixture = new \TYPO3\CMS\Core\Resource\File(array(
391 'name' => $originalFilename,
392 'identifier' => '/' . $originalFilename
393 ));
394 $this->assertSame($expectedExtension, $fixture->getExtension());
395 }
396
397
398 }
399
400 ?>