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