24f6d422b8e47595913ca11d349c376cf1e2c34e
[Packages/TYPO3.CMS.git] / tests / t3lib / file / FileTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Andreas Wolf <andreas.wolf@ikt-werk.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 require_once 'vfsStream/vfsStream.php';
27
28 /**
29 * Testcase for the file class of the TYPO3 FAL
30 *
31 * @package TYPO3
32 * @subpackage t3lib
33 *
34 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
35 * @todo Many, many, many tests are skipped in this test case...
36 */
37 class t3lib_file_FileTest extends Tx_Phpunit_TestCase {
38
39 /**
40 * @return t3lib_file_File
41 */
42 protected function prepareFixture() {
43 $fixture = new t3lib_file_File('testfile');
44
45 return $fixture;
46 }
47
48 /**
49 * @test
50 * @todo Old code getAvailableProperties() needs to be replaced by current behaviour
51 */
52 public function propertiesPassedToConstructorAreAvailableViaGenericGetter() {
53 $this->markTestSkipped('t3lib_file_File::getAvailableProperties() does not exist');
54
55 $properties = array(
56 uniqid() => uniqid(), uniqid() => uniqid(), 'uid' => 1
57 );
58 $fixture = new t3lib_file_File($properties);
59 $availablePropertiesBackup = t3lib_file_File::getAvailableProperties();
60 t3lib_file_File::setAvailableProperties(array_keys($properties));
61
62 foreach ($properties as $key => $value) {
63 $this->assertTrue($fixture->hasProperty($key));
64 $this->assertEquals($value, $fixture->getProperty($key));
65 }
66 $this->assertFalse($fixture->hasProperty(uniqid()));
67
68 t3lib_file_File::setAvailableProperties($availablePropertiesBackup);
69
70 $this->setExpectedException('InvalidArgumentException', '', 1314226805);
71 $fixture->getProperty(uniqid());
72 }
73
74 /**
75 * @test
76 */
77 public function commonPropertiesAreAvailableWithOwnGetters() {
78 $properties = array(
79 'name' => uniqid(),
80 'storage' => $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE),
81 'size' => 1024
82 );
83
84 $fixture = new t3lib_file_File($properties);
85 foreach ($properties as $key => $value) {
86 $this->assertEquals($value, call_user_func(array($fixture, 'get' . $key)));
87 }
88 }
89
90 /**
91 * @test
92 */
93 public function fileAsksRepositoryForIndexStatus() {
94 $mockedRepository = $this->getMock('t3lib_file_Repository_FileRepository');
95 $mockedRepository->expects($this->once())->method('getFileIndexRecord')->will($this->returnValue(array()));
96 t3lib_div::setSingletonInstance('t3lib_file_Repository_FileRepository', $mockedRepository);
97
98 $fixture = new t3lib_file_File(array());
99 $this->assertTrue($fixture->isIndexed());
100 }
101
102 /**
103 * Tests if a file is seen as indexed if the record has a uid
104 *
105 * @test
106 */
107 public function fileIndexStatusIsTrueIfUidIsSet() {
108 $fixture = new t3lib_file_File(array('uid' => 1));
109 $this->assertTrue($fixture->isIndexed());
110 }
111
112 /**
113 * @test
114 */
115 public function updatePropertiesUpdatesFileProperties() {
116 $identifier = '/' . uniqid();
117 $fixture = new t3lib_file_File(array('uid' => 1, 'identifier' => '/test'));
118 $fixture->updateProperties(array('identifier' => $identifier));
119
120 $this->assertEquals($identifier, $fixture->getIdentifier());
121 }
122
123 /**
124 * @test
125 */
126 public function updatePropertiesLeavesPropertiesUntouchedIfNotSetInNewProperties() {
127 $fixture = new t3lib_file_File(array('uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'));
128 $fixture->updateProperties(array('foo' => 'foobar'));
129
130 $this->assertEquals('/test', $fixture->getIdentifier());
131 $this->assertEquals('/test', $fixture->getProperty('identifier'));
132 }
133
134 /**
135 * @test
136 */
137 public function updatePropertiesDiscardsUidIfAlreadySet() {
138 $fixture = new t3lib_file_File(array('uid' => 1, 'identifier' => '/test'));
139 $fixture->updateProperties(array('uid' => 3));
140
141 $this->assertEquals(1, $fixture->getUid());
142 }
143
144 /**
145 * @test
146 */
147 public function updatePropertiesRecordsNamesOfChangedProperties() {
148 $fixture = new t3lib_file_File(array('uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test'));
149 $fixture->updateProperties(array('foo' => 'foobar', 'baz' => 'foobaz'));
150
151 $this->assertEquals(array('foo', 'baz'), $fixture->getUpdatedProperties());
152 }
153
154 /**
155 * @test
156 */
157 public function updatePropertiesDoesNotRecordPropertyNameIfSameValueIsProvided() {
158 $fixture = new t3lib_file_File(array('uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'));
159 $fixture->updateProperties(array('foo' => 'asdf'));
160
161 $this->assertEmpty($fixture->getUpdatedProperties());
162 }
163
164 /**
165 * @test
166 */
167 public function updatePropertiesMarksPropertyAsChangedOnlyOnce() {
168 $fixture = new t3lib_file_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
172 $this->assertEquals(array('foo', 'baz'), $fixture->getUpdatedProperties());
173 }
174
175 /**
176 * @test
177 */
178 public function updatePropertiesReloadsStorageObjectIfStorageChanges() {
179 $mockedNewStorage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
180 $mockedOldStorage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
181 $fixture = new t3lib_file_File(array('uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test', 'storage' => $mockedOldStorage));
182
183 $mockedRepository = $this->getMock('t3lib_file_Repository_StorageRepository');
184 $mockedRepository->expects($this->once())->method('findByUid')->with($this->equalTo(2))->will($this->returnValue($mockedNewStorage));
185 t3lib_div::setSingletonInstance('t3lib_file_Repository_StorageRepository', $mockedRepository);
186
187 $fixture->updateProperties(array('storage' => 2));
188 $this->assertSame($mockedNewStorage, $fixture->getStorage());
189 }
190
191 /**
192 * @test
193 */
194 public function fetchingIndexedPropertyCausesFileObjectToLoadIndexRecord() {
195 $fixture = new t3lib_file_File(array('identifier' => '/test', 'storage' => 1));
196
197 $mockedRepository = $this->getMock('t3lib_file_Repository_FileRepository');
198 $mockedRepository->expects($this->once())->method('getFileIndexRecord')->will($this->returnValue(array('uid' => 10)));
199 t3lib_div::setSingletonInstance('t3lib_file_Repository_FileRepository', $mockedRepository);
200
201 $this->assertEquals(10, $fixture->getProperty('uid'));
202 }
203
204 /**
205 * @test
206 */
207 public function isIndexedTriggersIndexingIfFileIsNotIndexedAlready() {
208 $fixture = new t3lib_file_File(array('identifier' => '/test', 'storage' => 1));
209
210 $mockedRepository = $this->getMock('t3lib_file_Repository_FileRepository');
211 $mockedRepository->expects($this->once())->method('getFileIndexRecord')->will($this->returnValue(FALSE));
212 $mockedRepository->expects($this->once())->method('addToIndex')->will($this->returnValue(array('uid' => 10)));
213 t3lib_div::setSingletonInstance('t3lib_file_Repository_FileRepository', $mockedRepository);
214
215 $fixture->isIndexed();
216 }
217
218 /**
219 * @test
220 */
221 public function fileIsAutomaticallyIndexedOnPropertyAccessIfNotAlreadyIndexed() {
222 $fixture = new t3lib_file_File(array('identifier' => '/test', 'storage' => 1));
223
224 $mockedRepository = $this->getMock('t3lib_file_Repository_FileRepository');
225 $mockedRepository->expects($this->once())->method('getFileIndexRecord')->will($this->returnValue(FALSE));
226 $mockedRepository->expects($this->once())->method('addToIndex')->will($this->returnValue(array('uid' => 10)));
227 t3lib_div::setSingletonInstance('t3lib_file_Repository_FileRepository', $mockedRepository);
228
229 $this->assertEquals(10, $fixture->getProperty('uid'));
230 }
231
232 /**
233 * @test
234 */
235 public function copyToCallsCopyOperationOnTargetFolderStorage() {
236 $targetStorage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
237 $targetFolder = $this->getMock('t3lib_file_Folder', array(), array(), '', FALSE);
238 $targetFolder->expects($this->any())->method('getStorage')->will($this->returnValue($targetStorage));
239
240 $fixture = new t3lib_file_File(array());
241
242 $targetStorage->expects($this->once())->method('copyFile')->with($this->equalTo($fixture), $this->equalTo($targetFolder));
243 $fixture->copyTo($targetFolder);
244 }
245
246 /**
247 * @test
248 */
249 public function moveToCallsMoveOperationOnTargetFolderStorage() {
250 $targetStorage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
251 $targetFolder = $this->getMock('t3lib_file_Folder', array(), array(), '', FALSE);
252 $targetFolder->expects($this->any())->method('getStorage')->will($this->returnValue($targetStorage));
253
254 $fixture = new t3lib_file_File(array());
255
256 $targetStorage->expects($this->once())->method('moveFile')->with($this->equalTo($fixture), $this->equalTo($targetFolder));
257 $fixture->moveTo($targetFolder);
258 }
259
260 /**
261 * @test
262 */
263 public function openCorrectlyOpensFileInDriver() {
264 $this->markTestSkipped();
265 $fixture = $this->prepareFixture();
266 $fileMode = 'invalidMode';
267
268 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
269 $mockDriver->expects($this->atLeastOnce())->method('getFileHandle')->with($this->equalTo($fixture), $this->equalTo($fileMode));
270
271 $fixture->setStorageDriver($mockDriver);
272 $fixture->open($fileMode);
273 }
274
275 /**
276 * @test
277 */
278 public function isOpenReturnsCorrectValuesForClosedAndOpenFile() {
279 $this->markTestSkipped();
280 $fixture = $this->prepareFixture();
281 $fileMode = 'r';
282
283 $mockFileHandle = $this->getMock('t3lib_file_FileHandle', array(), array(), '', FALSE);
284 $mockFileHandle->expects($this->any())->method('isOpen')->will($this->returnValue(TRUE));
285 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
286 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
287
288 $fixture->setStorageDriver($mockDriver);
289
290 $this->assertFalse($fixture->isOpen());
291 $fixture->open($fileMode);
292 $this->assertTrue($fixture->isOpen());
293 }
294
295 /**
296 * @test
297 */
298 public function fileIsCorrectlyClosed() {
299 $this->markTestSkipped();
300 $fixture = $this->prepareFixture();
301 $fileMode = 'r';
302
303 $mockFileHandle = $this->getMock('t3lib_file_FileHandle', array(), array(), '', FALSE);
304 $mockFileHandle->expects($this->once())->method('close');
305 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
306 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
307
308 $fixture->setStorageDriver($mockDriver);
309
310 $fixture->open($fileMode);
311 $fixture->close();
312 $this->assertFalse($fixture->isOpen());
313 }
314
315 /**
316 * @test
317 */
318 public function readReturnsRequestedContentsFromDriver() {
319 $this->markTestSkipped();
320 $fixture = $this->prepareFixture();
321 $fileMode = 'r';
322 $fileContents = 'Some random file contents.';
323 $bytesToRead = 10;
324
325 $mockFileHandle = $this->getMock('t3lib_file_FileHandle', array(), array(), '', FALSE);
326 $mockFileHandle->expects($this->any())->method('isOpen')->will($this->returnValue(TRUE));
327 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
328 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
329 $mockDriver->expects($this->once())->method('readFromFile')->with($this->anything(), $this->equalTo($bytesToRead))
330 ->will($this->returnValue(substr($fileContents, 0, $bytesToRead)));
331
332 $fixture->setStorageDriver($mockDriver);
333
334 $fixture->open($fileMode);
335 $this->assertEquals(substr($fileContents, 0, $bytesToRead), $fixture->read($bytesToRead));
336 }
337
338 /**
339 * @test
340 */
341 public function readFailsIfFileIsClosed() {
342 $this->markTestSkipped();
343 $this->setExpectedException('RuntimeException', '', 1299863431);
344
345 $fixture = $this->prepareFixture();
346
347 $mockFileHandle = $this->getMock('t3lib_file_FileHandle', array(), array(), '', FALSE);
348 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
349 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
350
351 $fixture->setStorageDriver($mockDriver);
352
353 $fixture->read(1);
354 }
355
356 /**
357 * @test
358 */
359 public function writePassesContentsToDriver() {
360 $this->markTestSkipped();
361 $fixture = $this->prepareFixture();
362 $fileMode = 'r+';
363 $fileContents = 'Some random file contents.';
364
365 $mockFileHandle = $this->getMock('t3lib_file_FileHandle', array(), array(), '', FALSE);
366 $mockFileHandle->expects($this->any())->method('isOpen')->will($this->returnValue(TRUE));
367 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
368 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
369 $mockDriver->expects($this->once())->method('writeToFile')->with($this->anything(), $this->equalTo($fileContents))
370 ->will($this->returnValue(TRUE));
371
372 $fixture->setStorageDriver($mockDriver);
373
374 $fixture->open($fileMode);
375 $this->assertTrue($fixture->write($fileContents));
376 }
377
378 /**
379 * @test
380 */
381 public function writeFailsIfFileIsClosed() {
382 $this->markTestSkipped();
383 $this->setExpectedException('RuntimeException', '', 1299863432);
384
385 $fixture = $this->prepareFixture();
386
387 $mockFileHandle = $this->getMock('t3lib_file_FileHandle', array(), array(), '', FALSE);
388 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
389 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
390
391 $fixture->setStorageDriver($mockDriver);
392
393 $fixture->write('asdf');
394 }
395 }
396 ?>