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