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