[FileBackend] Made doOperations() Status handling align with documentation as well...
[lhc/web/wiklou.git] / tests / phpunit / includes / filerepo / FileBackendTest.php
1 <?php
2
3 /**
4 * @group FileRepo
5 * @group FileBackend
6 */
7 class FileBackendTest extends MediaWikiTestCase {
8 private $backend, $multiBackend;
9 private $filesToPrune = array();
10 private $dirsToPrune = array();
11 private static $backendToUse;
12
13 function setUp() {
14 global $wgFileBackends;
15 parent::setUp();
16 $tmpPrefix = wfTempDir() . '/filebackend-unittest-' . time() . '-' . mt_rand();
17 if ( $this->getCliArg( 'use-filebackend=' ) ) {
18 if ( self::$backendToUse ) {
19 $this->singleBackend = self::$backendToUse;
20 } else {
21 $name = $this->getCliArg( 'use-filebackend=' );
22 $useConfig = array();
23 foreach ( $wgFileBackends as $conf ) {
24 if ( $conf['name'] == $name ) {
25 $useConfig = $conf;
26 }
27 }
28 $useConfig['name'] = 'localtesting'; // swap name
29 $class = $conf['class'];
30 self::$backendToUse = new $class( $useConfig );
31 $this->singleBackend = self::$backendToUse;
32 }
33 } else {
34 $this->singleBackend = new FSFileBackend( array(
35 'name' => 'localtesting',
36 'lockManager' => 'fsLockManager',
37 'containerPaths' => array(
38 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
39 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
40 ) );
41 }
42 $this->multiBackend = new FileBackendMultiWrite( array(
43 'name' => 'localtesting',
44 'lockManager' => 'fsLockManager',
45 'backends' => array(
46 array(
47 'name' => 'localmutlitesting1',
48 'class' => 'FSFileBackend',
49 'lockManager' => 'nullLockManager',
50 'containerPaths' => array(
51 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
52 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
53 'isMultiMaster' => false
54 ),
55 array(
56 'name' => 'localmutlitesting2',
57 'class' => 'FSFileBackend',
58 'lockManager' => 'nullLockManager',
59 'containerPaths' => array(
60 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
61 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
62 'isMultiMaster' => true
63 )
64 )
65 ) );
66 $this->filesToPrune = array();
67 }
68
69 private function baseStorePath() {
70 return 'mwstore://localtesting';
71 }
72
73 private function backendClass() {
74 return get_class( $this->backend );
75 }
76
77 /**
78 * @dataProvider provider_testIsStoragePath
79 */
80 public function testIsStoragePath( $path, $isStorePath ) {
81 $this->assertEquals( $isStorePath, FileBackend::isStoragePath( $path ),
82 "FileBackend::isStoragePath on path '$path'" );
83 }
84
85 function provider_testIsStoragePath() {
86 return array(
87 array( 'mwstore://', true ),
88 array( 'mwstore://backend', true ),
89 array( 'mwstore://backend/container', true ),
90 array( 'mwstore://backend/container/', true ),
91 array( 'mwstore://backend/container/path', true ),
92 array( 'mwstore://backend//container/', true ),
93 array( 'mwstore://backend//container//', true ),
94 array( 'mwstore://backend//container//path', true ),
95 array( 'mwstore:///', true ),
96 array( 'mwstore:/', false ),
97 array( 'mwstore:', false ),
98 );
99 }
100
101 /**
102 * @dataProvider provider_testSplitStoragePath
103 */
104 public function testSplitStoragePath( $path, $res ) {
105 $this->assertEquals( $res, FileBackend::splitStoragePath( $path ),
106 "FileBackend::splitStoragePath on path '$path'" );
107 }
108
109 function provider_testSplitStoragePath() {
110 return array(
111 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
112 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
113 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
114 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
115 array( 'mwstore://backend//container/path', array( null, null, null ) ),
116 array( 'mwstore://backend//container//path', array( null, null, null ) ),
117 array( 'mwstore://', array( null, null, null ) ),
118 array( 'mwstore://backend', array( null, null, null ) ),
119 array( 'mwstore:///', array( null, null, null ) ),
120 array( 'mwstore:/', array( null, null, null ) ),
121 array( 'mwstore:', array( null, null, null ) )
122 );
123 }
124
125 /**
126 * @dataProvider provider_normalizeStoragePath
127 */
128 public function testNormalizeStoragePath( $path, $res ) {
129 $this->assertEquals( $res, FileBackend::normalizeStoragePath( $path ),
130 "FileBackend::normalizeStoragePath on path '$path'" );
131 }
132
133 function provider_normalizeStoragePath() {
134 return array(
135 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
136 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
137 array( 'mwstore://backend/container/path', 'mwstore://backend/container/path' ),
138 array( 'mwstore://backend/container//path', 'mwstore://backend/container/path' ),
139 array( 'mwstore://backend/container///path', 'mwstore://backend/container/path' ),
140 array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj',
141 array( 'mwstore://', null ),
142 array( 'mwstore://backend', null ),
143 array( 'mwstore://backend//container/path', null ),
144 array( 'mwstore://backend//container//path', null ),
145 array( 'mwstore:///', null ),
146 array( 'mwstore:/', null ),
147 array( 'mwstore:', null ), )
148 );
149 }
150
151 /**
152 * @dataProvider provider_testParentStoragePath
153 */
154 public function testParentStoragePath( $path, $res ) {
155 $this->assertEquals( $res, FileBackend::parentStoragePath( $path ),
156 "FileBackend::parentStoragePath on path '$path'" );
157 }
158
159 function provider_testParentStoragePath() {
160 return array(
161 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
162 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
163 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
164 array( 'mwstore://backend/container', null ),
165 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
166 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
167 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
168 array( 'mwstore://backend/container/', null ),
169 );
170 }
171
172 /**
173 * @dataProvider provider_testExtensionFromPath
174 */
175 public function testExtensionFromPath( $path, $res ) {
176 $this->assertEquals( $res, FileBackend::extensionFromPath( $path ),
177 "FileBackend::extensionFromPath on path '$path'" );
178 }
179
180 function provider_testExtensionFromPath() {
181 return array(
182 array( 'mwstore://backend/container/path.txt', 'txt' ),
183 array( 'mwstore://backend/container/path.svg.png', 'png' ),
184 array( 'mwstore://backend/container/path', '' ),
185 array( 'mwstore://backend/container/path.', '' ),
186 );
187 }
188
189 /**
190 * @dataProvider provider_testStore
191 */
192 public function testStore( $op ) {
193 $this->filesToPrune[] = $op['src'];
194
195 $this->backend = $this->singleBackend;
196 $this->tearDownFiles();
197 $this->doTestStore( $op );
198 $this->tearDownFiles();
199
200 $this->backend = $this->multiBackend;
201 $this->tearDownFiles();
202 $this->doTestStore( $op );
203 $this->filesToPrune[] = $op['src']; # avoid file leaking
204 $this->tearDownFiles();
205 }
206
207 function doTestStore( $op ) {
208 $backendName = $this->backendClass();
209
210 $source = $op['src'];
211 $dest = $op['dst'];
212 $this->prepare( array( 'dir' => dirname( $dest ) ) );
213
214 file_put_contents( $source, "Unit test file" );
215
216 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
217 $this->backend->store( $op );
218 }
219
220 $status = $this->backend->doOperation( $op );
221
222 $this->assertEquals( array(), $status->errors,
223 "Store from $source to $dest succeeded without warnings ($backendName)." );
224 $this->assertEquals( array(), $status->errors,
225 "Store from $source to $dest succeeded ($backendName)." );
226 $this->assertEquals( array( 0 => true ), $status->success,
227 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
228 $this->assertEquals( true, file_exists( $source ),
229 "Source file $source still exists ($backendName)." );
230 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
231 "Destination file $dest exists ($backendName)." );
232
233 $this->assertEquals( filesize( $source ),
234 $this->backend->getFileSize( array( 'src' => $dest ) ),
235 "Destination file $dest has correct size ($backendName)." );
236
237 $props1 = FSFile::getPropsFromPath( $source );
238 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
239 $this->assertEquals( $props1, $props2,
240 "Source and destination have the same props ($backendName)." );
241 }
242
243 public function provider_testStore() {
244 $cases = array();
245
246 $tmpName = TempFSFile::factory( "unittests_", 'txt' )->getPath();
247 $toPath = $this->baseStorePath() . '/unittest-cont1/fun/obj1.txt';
248 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
249 $cases[] = array(
250 $op, // operation
251 $tmpName, // source
252 $toPath, // dest
253 );
254
255 $op2 = $op;
256 $op2['overwrite'] = true;
257 $cases[] = array(
258 $op2, // operation
259 $tmpName, // source
260 $toPath, // dest
261 );
262
263 $op2 = $op;
264 $op2['overwriteSame'] = true;
265 $cases[] = array(
266 $op2, // operation
267 $tmpName, // source
268 $toPath, // dest
269 );
270
271 return $cases;
272 }
273
274 /**
275 * @dataProvider provider_testCopy
276 */
277 public function testCopy( $op ) {
278 $this->backend = $this->singleBackend;
279 $this->tearDownFiles();
280 $this->doTestCopy( $op );
281 $this->tearDownFiles();
282
283 $this->backend = $this->multiBackend;
284 $this->tearDownFiles();
285 $this->doTestCopy( $op );
286 $this->tearDownFiles();
287 }
288
289 function doTestCopy( $op ) {
290 $backendName = $this->backendClass();
291
292 $source = $op['src'];
293 $dest = $op['dst'];
294 $this->prepare( array( 'dir' => dirname( $source ) ) );
295 $this->prepare( array( 'dir' => dirname( $dest ) ) );
296
297 $status = $this->backend->doOperation(
298 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
299 $this->assertEquals( array(), $status->errors,
300 "Creation of file at $source succeeded ($backendName)." );
301
302 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
303 $this->backend->copy( $op );
304 }
305
306 $status = $this->backend->doOperation( $op );
307
308 $this->assertEquals( array(), $status->errors,
309 "Copy from $source to $dest succeeded without warnings ($backendName)." );
310 $this->assertEquals( true, $status->isOK(),
311 "Copy from $source to $dest succeeded ($backendName)." );
312 $this->assertEquals( array( 0 => true ), $status->success,
313 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
314 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $source ) ),
315 "Source file $source still exists ($backendName)." );
316 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
317 "Destination file $dest exists after copy ($backendName)." );
318
319 $this->assertEquals(
320 $this->backend->getFileSize( array( 'src' => $source ) ),
321 $this->backend->getFileSize( array( 'src' => $dest ) ),
322 "Destination file $dest has correct size ($backendName)." );
323
324 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
325 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
326 $this->assertEquals( $props1, $props2,
327 "Source and destination have the same props ($backendName)." );
328 }
329
330 public function provider_testCopy() {
331 $cases = array();
332
333 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
334 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
335
336 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
337 $cases[] = array(
338 $op, // operation
339 $source, // source
340 $dest, // dest
341 );
342
343 $op2 = $op;
344 $op2['overwrite'] = true;
345 $cases[] = array(
346 $op2, // operation
347 $source, // source
348 $dest, // dest
349 );
350
351 $op2 = $op;
352 $op2['overwriteSame'] = true;
353 $cases[] = array(
354 $op2, // operation
355 $source, // source
356 $dest, // dest
357 );
358
359 return $cases;
360 }
361
362 /**
363 * @dataProvider provider_testMove
364 */
365 public function testMove( $op ) {
366 $this->backend = $this->singleBackend;
367 $this->tearDownFiles();
368 $this->doTestMove( $op );
369 $this->tearDownFiles();
370
371 $this->backend = $this->multiBackend;
372 $this->tearDownFiles();
373 $this->doTestMove( $op );
374 $this->tearDownFiles();
375 }
376
377 private function doTestMove( $op ) {
378 $backendName = $this->backendClass();
379
380 $source = $op['src'];
381 $dest = $op['dst'];
382 $this->prepare( array( 'dir' => dirname( $source ) ) );
383 $this->prepare( array( 'dir' => dirname( $dest ) ) );
384
385 $status = $this->backend->doOperation(
386 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
387 $this->assertEquals( array(), $status->errors,
388 "Creation of file at $source succeeded ($backendName)." );
389
390 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
391 $this->backend->copy( $op );
392 }
393
394 $status = $this->backend->doOperation( $op );
395 $this->assertEquals( array(), $status->errors,
396 "Move from $source to $dest succeeded without warnings ($backendName)." );
397 $this->assertEquals( true, $status->isOK(),
398 "Move from $source to $dest succeeded ($backendName)." );
399 $this->assertEquals( array( 0 => true ), $status->success,
400 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
401 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
402 "Source file $source does not still exists ($backendName)." );
403 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
404 "Destination file $dest exists after move ($backendName)." );
405
406 $this->assertNotEquals(
407 $this->backend->getFileSize( array( 'src' => $source ) ),
408 $this->backend->getFileSize( array( 'src' => $dest ) ),
409 "Destination file $dest has correct size ($backendName)." );
410
411 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
412 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
413 $this->assertEquals( false, $props1['fileExists'],
414 "Source file does not exist accourding to props ($backendName)." );
415 $this->assertEquals( true, $props2['fileExists'],
416 "Destination file exists accourding to props ($backendName)." );
417 }
418
419 public function provider_testMove() {
420 $cases = array();
421
422 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
423 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
424
425 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
426 $cases[] = array(
427 $op, // operation
428 $source, // source
429 $dest, // dest
430 );
431
432 $op2 = $op;
433 $op2['overwrite'] = true;
434 $cases[] = array(
435 $op2, // operation
436 $source, // source
437 $dest, // dest
438 );
439
440 $op2 = $op;
441 $op2['overwriteSame'] = true;
442 $cases[] = array(
443 $op2, // operation
444 $source, // source
445 $dest, // dest
446 );
447
448 return $cases;
449 }
450
451 /**
452 * @dataProvider provider_testDelete
453 */
454 public function testDelete( $op, $withSource, $okStatus ) {
455 $this->backend = $this->singleBackend;
456 $this->tearDownFiles();
457 $this->doTestDelete( $op, $withSource, $okStatus );
458 $this->tearDownFiles();
459
460 $this->backend = $this->multiBackend;
461 $this->tearDownFiles();
462 $this->doTestDelete( $op, $withSource, $okStatus );
463 $this->tearDownFiles();
464 }
465
466 private function doTestDelete( $op, $withSource, $okStatus ) {
467 $backendName = $this->backendClass();
468
469 $source = $op['src'];
470 $this->prepare( array( 'dir' => dirname( $source ) ) );
471
472 if ( $withSource ) {
473 $status = $this->backend->doOperation(
474 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
475 $this->assertEquals( array(), $status->errors,
476 "Creation of file at $source succeeded ($backendName)." );
477 }
478
479 $status = $this->backend->doOperation( $op );
480 if ( $okStatus ) {
481 $this->assertEquals( array(), $status->errors,
482 "Deletion of file at $source succeeded without warnings ($backendName)." );
483 $this->assertEquals( true, $status->isOK(),
484 "Deletion of file at $source succeeded ($backendName)." );
485 $this->assertEquals( array( 0 => true ), $status->success,
486 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
487 } else {
488 $this->assertEquals( false, $status->isOK(),
489 "Deletion of file at $source failed ($backendName)." );
490 }
491
492 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
493 "Source file $source does not exist after move ($backendName)." );
494
495 $this->assertFalse(
496 $this->backend->getFileSize( array( 'src' => $source ) ),
497 "Source file $source has correct size (false) ($backendName)." );
498
499 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
500 $this->assertFalse( $props1['fileExists'],
501 "Source file $source does not exist according to props ($backendName)." );
502 }
503
504 public function provider_testDelete() {
505 $cases = array();
506
507 $source = $this->baseStorePath() . '/unittest-cont1/myfacefile.txt';
508
509 $op = array( 'op' => 'delete', 'src' => $source );
510 $cases[] = array(
511 $op, // operation
512 true, // with source
513 true // succeeds
514 );
515
516 $cases[] = array(
517 $op, // operation
518 false, // without source
519 false // fails
520 );
521
522 $op['ignoreMissingSource'] = true;
523 $cases[] = array(
524 $op, // operation
525 false, // without source
526 true // succeeds
527 );
528
529 return $cases;
530 }
531
532 /**
533 * @dataProvider provider_testCreate
534 */
535 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
536 $this->backend = $this->singleBackend;
537 $this->tearDownFiles();
538 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
539 $this->tearDownFiles();
540
541 $this->backend = $this->multiBackend;
542 $this->tearDownFiles();
543 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
544 $this->tearDownFiles();
545 }
546
547 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
548 $backendName = $this->backendClass();
549
550 $dest = $op['dst'];
551 $this->prepare( array( 'dir' => dirname( $dest ) ) );
552
553 $oldText = 'blah...blah...waahwaah';
554 if ( $alreadyExists ) {
555 $status = $this->backend->doOperation(
556 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
557 $this->assertEquals( array(), $status->errors,
558 "Creation of file at $dest succeeded ($backendName)." );
559 }
560
561 $status = $this->backend->doOperation( $op );
562 if ( $okStatus ) {
563 $this->assertEquals( array(), $status->errors,
564 "Creation of file at $dest succeeded without warnings ($backendName)." );
565 $this->assertEquals( true, $status->isOK(),
566 "Creation of file at $dest succeeded ($backendName)." );
567 $this->assertEquals( array( 0 => true ), $status->success,
568 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
569 } else {
570 $this->assertEquals( false, $status->isOK(),
571 "Creation of file at $dest failed ($backendName)." );
572 }
573
574 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
575 "Destination file $dest exists after creation ($backendName)." );
576
577 $props1 = $this->backend->getFileProps( array( 'src' => $dest ) );
578 $this->assertEquals( true, $props1['fileExists'],
579 "Destination file $dest exists according to props ($backendName)." );
580 if ( $okStatus ) { // file content is what we saved
581 $this->assertEquals( $newSize, $props1['size'],
582 "Destination file $dest has expected size according to props ($backendName)." );
583 $this->assertEquals( $newSize,
584 $this->backend->getFileSize( array( 'src' => $dest ) ),
585 "Destination file $dest has correct size ($backendName)." );
586 } else { // file content is some other previous text
587 $this->assertEquals( strlen( $oldText ), $props1['size'],
588 "Destination file $dest has original size according to props ($backendName)." );
589 $this->assertEquals( strlen( $oldText ),
590 $this->backend->getFileSize( array( 'src' => $dest ) ),
591 "Destination file $dest has original size according to props ($backendName)." );
592 }
593 }
594
595 /**
596 * @dataProvider provider_testCreate
597 */
598 public function provider_testCreate() {
599 $cases = array();
600
601 $dest = $this->baseStorePath() . '/unittest-cont2/myspacefile.txt';
602
603 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
604 $cases[] = array(
605 $op, // operation
606 false, // no dest already exists
607 true, // succeeds
608 strlen( $op['content'] )
609 );
610
611 $op2 = $op;
612 $op2['content'] = "\n";
613 $cases[] = array(
614 $op2, // operation
615 false, // no dest already exists
616 true, // succeeds
617 strlen( $op2['content'] )
618 );
619
620 $op2 = $op;
621 $op2['content'] = "fsf\n waf 3kt";
622 $cases[] = array(
623 $op2, // operation
624 true, // dest already exists
625 false, // fails
626 strlen( $op2['content'] )
627 );
628
629 $op2 = $op;
630 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
631 $op2['overwrite'] = true;
632 $cases[] = array(
633 $op2, // operation
634 true, // dest already exists
635 true, // succeeds
636 strlen( $op2['content'] )
637 );
638
639 $op2 = $op;
640 $op2['content'] = "39qjmg3-qg";
641 $op2['overwriteSame'] = true;
642 $cases[] = array(
643 $op2, // operation
644 true, // dest already exists
645 false, // succeeds
646 strlen( $op2['content'] )
647 );
648
649 return $cases;
650 }
651
652 /**
653 * @dataProvider provider_testConcatenate
654 */
655 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
656 $this->filesToPrune[] = $op['dst'];
657
658 $this->backend = $this->singleBackend;
659 $this->tearDownFiles();
660 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
661 $this->tearDownFiles();
662
663 $this->backend = $this->multiBackend;
664 $this->tearDownFiles();
665 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
666 $this->filesToPrune[] = $op['dst']; # avoid file leaking
667 $this->tearDownFiles();
668 }
669
670 public function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
671 $backendName = $this->backendClass();
672
673 $expContent = '';
674 // Create sources
675 $ops = array();
676 foreach ( $srcs as $i => $source ) {
677 $this->prepare( array( 'dir' => dirname( $source ) ) );
678 $ops[] = array(
679 'op' => 'create', // operation
680 'dst' => $source, // source
681 'content' => $srcsContent[$i]
682 );
683 $expContent .= $srcsContent[$i];
684 }
685 $status = $this->backend->doOperations( $ops );
686
687 $this->assertEquals( array(), $status->errors,
688 "Creation of source files succeeded ($backendName)." );
689
690 $dest = $params['dst'];
691 if ( $alreadyExists ) {
692 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
693 $this->assertEquals( true, $ok,
694 "Creation of file at $dest succeeded ($backendName)." );
695 } else {
696 $ok = file_put_contents( $dest, '' ) !== false;
697 $this->assertEquals( true, $ok,
698 "Creation of 0-byte file at $dest succeeded ($backendName)." );
699 }
700
701 // Combine the files into one
702 $status = $this->backend->concatenate( $params );
703 if ( $okStatus ) {
704 $this->assertEquals( array(), $status->errors,
705 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
706 $this->assertEquals( true, $status->isOK(),
707 "Creation of concat file at $dest succeeded ($backendName)." );
708 } else {
709 $this->assertEquals( false, $status->isOK(),
710 "Creation of concat file at $dest failed ($backendName)." );
711 }
712
713 if ( $okStatus ) {
714 $this->assertEquals( true, is_file( $dest ),
715 "Dest concat file $dest exists after creation ($backendName)." );
716 } else {
717 $this->assertEquals( true, is_file( $dest ),
718 "Dest concat file $dest exists after failed creation ($backendName)." );
719 }
720
721 $contents = file_get_contents( $dest );
722 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
723
724 if ( $okStatus ) {
725 $this->assertEquals( $expContent, $contents,
726 "Concat file at $dest has correct contents ($backendName)." );
727 } else {
728 $this->assertNotEquals( $expContent, $contents,
729 "Concat file at $dest has correct contents ($backendName)." );
730 }
731 }
732
733 function provider_testConcatenate() {
734 $cases = array();
735
736 $rand = mt_rand( 0, 2000000000 ) . time();
737 $dest = wfTempDir() . "/randomfile!$rand.txt";
738 $srcs = array(
739 $this->baseStorePath() . '/unittest-cont1/file1.txt',
740 $this->baseStorePath() . '/unittest-cont1/file2.txt',
741 $this->baseStorePath() . '/unittest-cont1/file3.txt',
742 $this->baseStorePath() . '/unittest-cont1/file4.txt',
743 $this->baseStorePath() . '/unittest-cont1/file5.txt',
744 $this->baseStorePath() . '/unittest-cont1/file6.txt',
745 $this->baseStorePath() . '/unittest-cont1/file7.txt',
746 $this->baseStorePath() . '/unittest-cont1/file8.txt',
747 $this->baseStorePath() . '/unittest-cont1/file9.txt',
748 $this->baseStorePath() . '/unittest-cont1/file10.txt'
749 );
750 $content = array(
751 'egfage',
752 'ageageag',
753 'rhokohlr',
754 'shgmslkg',
755 'kenga',
756 'owagmal',
757 'kgmae',
758 'g eak;g',
759 'lkaem;a',
760 'legma'
761 );
762 $params = array( 'srcs' => $srcs, 'dst' => $dest );
763
764 $cases[] = array(
765 $params, // operation
766 $srcs, // sources
767 $content, // content for each source
768 false, // no dest already exists
769 true, // succeeds
770 );
771
772 $cases[] = array(
773 $params, // operation
774 $srcs, // sources
775 $content, // content for each source
776 true, // dest already exists
777 false, // succeeds
778 );
779
780 return $cases;
781 }
782
783 /**
784 * @dataProvider provider_testGetFileStat
785 */
786 public function testGetFileStat( $path, $content, $alreadyExists ) {
787 $this->backend = $this->singleBackend;
788 $this->tearDownFiles();
789 $this->doTestGetFileStat( $path, $content, $alreadyExists );
790 $this->tearDownFiles();
791
792 $this->backend = $this->multiBackend;
793 $this->tearDownFiles();
794 $this->doTestGetFileStat( $path, $content, $alreadyExists );
795 $this->tearDownFiles();
796 }
797
798 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
799 $backendName = $this->backendClass();
800
801 if ( $alreadyExists ) {
802 $this->prepare( array( 'dir' => dirname( $path ) ) );
803 $status = $this->backend->create( array( 'dst' => $path, 'content' => $content ) );
804 $this->assertEquals( array(), $status->errors,
805 "Creation of file at $path succeeded ($backendName)." );
806
807 $size = $this->backend->getFileSize( array( 'src' => $path ) );
808 $time = $this->backend->getFileTimestamp( array( 'src' => $path ) );
809 $stat = $this->backend->getFileStat( array( 'src' => $path ) );
810
811 $this->assertEquals( strlen( $content ), $size,
812 "Correct file size of '$path'" );
813 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 5,
814 "Correct file timestamp of '$path'" );
815
816 $size = $stat['size'];
817 $time = $stat['mtime'];
818 $this->assertEquals( strlen( $content ), $size,
819 "Correct file size of '$path'" );
820 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 5,
821 "Correct file timestamp of '$path'" );
822 } else {
823 $size = $this->backend->getFileSize( array( 'src' => $path ) );
824 $time = $this->backend->getFileTimestamp( array( 'src' => $path ) );
825 $stat = $this->backend->getFileStat( array( 'src' => $path ) );
826
827 $this->assertFalse( $size, "Correct file size of '$path'" );
828 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
829 $this->assertFalse( $stat, "Correct file stat of '$path'" );
830 }
831 }
832
833 function provider_testGetFileStat() {
834 $cases = array();
835
836 $base = $this->baseStorePath();
837 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents", true );
838 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "", true );
839 $cases[] = array( "$base/unittest-cont1/b/some-diff_file.txt", null, false );
840
841 return $cases;
842 }
843
844 /**
845 * @dataProvider provider_testGetFileContents
846 */
847 public function testGetFileContents( $source, $content ) {
848 $this->backend = $this->singleBackend;
849 $this->tearDownFiles();
850 $this->doTestGetFileContents( $source, $content );
851 $this->tearDownFiles();
852
853 $this->backend = $this->multiBackend;
854 $this->tearDownFiles();
855 $this->doTestGetFileContents( $source, $content );
856 $this->tearDownFiles();
857 }
858
859 public function doTestGetFileContents( $source, $content ) {
860 $backendName = $this->backendClass();
861
862 $this->prepare( array( 'dir' => dirname( $source ) ) );
863
864 $status = $this->backend->doOperation(
865 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
866 $this->assertEquals( array(), $status->errors,
867 "Creation of file at $source succeeded ($backendName)." );
868 $this->assertEquals( true, $status->isOK(),
869 "Creation of file at $source succeeded with OK status ($backendName)." );
870
871 $newContents = $this->backend->getFileContents( array( 'src' => $source, 'latest' => 1 ) );
872 $this->assertNotEquals( false, $newContents,
873 "Read of file at $source succeeded ($backendName)." );
874
875 $this->assertEquals( $content, $newContents,
876 "Contents read match data at $source ($backendName)." );
877 }
878
879 function provider_testGetFileContents() {
880 $cases = array();
881
882 $base = $this->baseStorePath();
883 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents" );
884 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "more file contents" );
885
886 return $cases;
887 }
888
889 /**
890 * @dataProvider provider_testGetLocalCopy
891 */
892 public function testGetLocalCopy( $source, $content ) {
893 $this->backend = $this->singleBackend;
894 $this->tearDownFiles();
895 $this->doTestGetLocalCopy( $source, $content );
896 $this->tearDownFiles();
897
898 $this->backend = $this->multiBackend;
899 $this->tearDownFiles();
900 $this->doTestGetLocalCopy( $source, $content );
901 $this->tearDownFiles();
902 }
903
904 public function doTestGetLocalCopy( $source, $content ) {
905 $backendName = $this->backendClass();
906
907 $this->prepare( array( 'dir' => dirname( $source ) ) );
908
909 $status = $this->backend->doOperation(
910 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
911 $this->assertEquals( array(), $status->errors,
912 "Creation of file at $source succeeded ($backendName)." );
913
914 $tmpFile = $this->backend->getLocalCopy( array( 'src' => $source ) );
915 $this->assertNotNull( $tmpFile,
916 "Creation of local copy of $source succeeded ($backendName)." );
917
918 $contents = file_get_contents( $tmpFile->getPath() );
919 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
920 }
921
922 function provider_testGetLocalCopy() {
923 $cases = array();
924
925 $base = $this->baseStorePath();
926 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
927 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
928
929 return $cases;
930 }
931
932 /**
933 * @dataProvider provider_testGetLocalReference
934 */
935 public function testGetLocalReference( $source, $content ) {
936 $this->backend = $this->singleBackend;
937 $this->tearDownFiles();
938 $this->doTestGetLocalReference( $source, $content );
939 $this->tearDownFiles();
940
941 $this->backend = $this->multiBackend;
942 $this->tearDownFiles();
943 $this->doTestGetLocalReference( $source, $content );
944 $this->tearDownFiles();
945 }
946
947 private function doTestGetLocalReference( $source, $content ) {
948 $backendName = $this->backendClass();
949
950 $this->prepare( array( 'dir' => dirname( $source ) ) );
951
952 $status = $this->backend->doOperation(
953 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
954 $this->assertEquals( array(), $status->errors,
955 "Creation of file at $source succeeded ($backendName)." );
956
957 $tmpFile = $this->backend->getLocalReference( array( 'src' => $source ) );
958 $this->assertNotNull( $tmpFile,
959 "Creation of local copy of $source succeeded ($backendName)." );
960
961 $contents = file_get_contents( $tmpFile->getPath() );
962 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
963 }
964
965 function provider_testGetLocalReference() {
966 $cases = array();
967
968 $base = $this->baseStorePath();
969 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
970 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
971
972 return $cases;
973 }
974
975 /**
976 * @dataProvider provider_testPrepareAndClean
977 */
978 public function testPrepareAndClean( $path, $isOK ) {
979 $this->backend = $this->singleBackend;
980 $this->doTestPrepareAndClean( $path, $isOK );
981 $this->tearDownFiles();
982
983 $this->backend = $this->multiBackend;
984 $this->doTestPrepareAndClean( $path, $isOK );
985 $this->tearDownFiles();
986 }
987
988 function provider_testPrepareAndClean() {
989 $base = $this->baseStorePath();
990 return array(
991 array( "$base/unittest-cont1/a/z/some_file1.txt", true ),
992 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
993 # Specific to FS backend with no basePath field set
994 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
995 );
996 }
997
998 function doTestPrepareAndClean( $path, $isOK ) {
999 $backendName = $this->backendClass();
1000
1001 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1002 if ( $isOK ) {
1003 $this->assertEquals( array(), $status->errors,
1004 "Preparing dir $path succeeded without warnings ($backendName)." );
1005 $this->assertEquals( true, $status->isOK(),
1006 "Preparing dir $path succeeded ($backendName)." );
1007 } else {
1008 $this->assertEquals( false, $status->isOK(),
1009 "Preparing dir $path failed ($backendName)." );
1010 }
1011
1012 $status = $this->backend->clean( array( 'dir' => dirname( $path ) ) );
1013 if ( $isOK ) {
1014 $this->assertEquals( array(), $status->errors,
1015 "Cleaning dir $path succeeded without warnings ($backendName)." );
1016 $this->assertEquals( true, $status->isOK(),
1017 "Cleaning dir $path succeeded ($backendName)." );
1018 } else {
1019 $this->assertEquals( false, $status->isOK(),
1020 "Cleaning dir $path failed ($backendName)." );
1021 }
1022 }
1023
1024 // @TODO: testSecure
1025
1026 public function testDoOperations() {
1027 $this->backend = $this->singleBackend;
1028 $this->tearDownFiles();
1029 $this->doTestDoOperations();
1030 $this->tearDownFiles();
1031
1032 $this->backend = $this->multiBackend;
1033 $this->tearDownFiles();
1034 $this->doTestDoOperations();
1035 $this->tearDownFiles();
1036
1037 $this->backend = $this->singleBackend;
1038 $this->tearDownFiles();
1039 $this->doTestDoOperationsFailing();
1040 $this->tearDownFiles();
1041
1042 $this->backend = $this->multiBackend;
1043 $this->tearDownFiles();
1044 $this->doTestDoOperationsFailing();
1045 $this->tearDownFiles();
1046
1047 // @TODO: test some cases where the ops should fail
1048 }
1049
1050 function doTestDoOperations() {
1051 $base = $this->baseStorePath();
1052
1053 $fileA = "$base/unittest-cont1/a/b/fileA.txt";
1054 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1055 $fileB = "$base/unittest-cont1/a/b/fileB.txt";
1056 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1057 $fileC = "$base/unittest-cont1/a/b/fileC.txt";
1058 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1059 $fileD = "$base/unittest-cont1/a/b/fileD.txt";
1060
1061 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1062 $this->backend->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1063 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1064 $this->backend->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1065 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1066 $this->backend->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1067
1068 $status = $this->backend->doOperations( array(
1069 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1070 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1071 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1072 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1073 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1074 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1075 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1076 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1077 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1078 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1079 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1080 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1081 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1082 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1083 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1084 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1085 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1086 // Does nothing
1087 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1088 // Does nothing
1089 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1090 // Does nothing
1091 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1092 // Does nothing
1093 array( 'op' => 'null' ),
1094 // Does nothing
1095 ) );
1096
1097 $this->assertEquals( array(), $status->errors, "Operation batch succeeded" );
1098 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1099 $this->assertEquals( 13, count( $status->success ),
1100 "Operation batch has correct success array" );
1101
1102 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
1103 "File does not exist at $fileA" );
1104 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1105 "File does not exist at $fileB" );
1106 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1107 "File does not exist at $fileD" );
1108
1109 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1110 "File exists at $fileC" );
1111 $this->assertEquals( $fileBContents,
1112 $this->backend->getFileContents( array( 'src' => $fileC ) ),
1113 "Correct file contents of $fileC" );
1114 $this->assertEquals( strlen( $fileBContents ),
1115 $this->backend->getFileSize( array( 'src' => $fileC ) ),
1116 "Correct file size of $fileC" );
1117 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1118 $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
1119 "Correct file SHA-1 of $fileC" );
1120 }
1121
1122 function doTestDoOperationsFailing() {
1123 $base = $this->baseStorePath();
1124
1125 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1126 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1127 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1128 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1129 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1130 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1131 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1132
1133 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1134 $this->backend->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1135 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1136 $this->backend->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1137 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1138 $this->backend->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1139
1140 $status = $this->backend->doOperations( array(
1141 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1142 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1143 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1144 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1145 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1146 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1147 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1148 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1149 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1150 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1151 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1152 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1153 array( 'op' => 'delete', 'src' => $fileD ),
1154 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1155 array( 'op' => 'null' ),
1156 // Does nothing
1157 ), array( 'force' => 1 ) );
1158
1159 $this->assertNotEquals( array(), $status->errors, "Operation had warnings" );
1160 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1161 $this->assertEquals( 8, count( $status->success ),
1162 "Operation batch has correct success array" );
1163
1164 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1165 "File does not exist at $fileB" );
1166 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1167 "File does not exist at $fileD" );
1168
1169 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileA ) ),
1170 "File does not exist at $fileA" );
1171 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1172 "File exists at $fileC" );
1173 $this->assertEquals( $fileBContents,
1174 $this->backend->getFileContents( array( 'src' => $fileA ) ),
1175 "Correct file contents of $fileA" );
1176 $this->assertEquals( strlen( $fileBContents ),
1177 $this->backend->getFileSize( array( 'src' => $fileA ) ),
1178 "Correct file size of $fileA" );
1179 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1180 $this->backend->getFileSha1Base36( array( 'src' => $fileA ) ),
1181 "Correct file SHA-1 of $fileA" );
1182 }
1183
1184 public function testGetFileList() {
1185 $this->backend = $this->singleBackend;
1186 $this->tearDownFiles();
1187 $this->doTestGetFileList();
1188 $this->tearDownFiles();
1189
1190 $this->backend = $this->multiBackend;
1191 $this->tearDownFiles();
1192 $this->doTestGetFileList();
1193 $this->tearDownFiles();
1194 }
1195
1196 private function doTestGetFileList() {
1197 $backendName = $this->backendClass();
1198
1199 $base = $this->baseStorePath();
1200 $files = array(
1201 "$base/unittest-cont1/test1.txt",
1202 "$base/unittest-cont1/test2.txt",
1203 "$base/unittest-cont1/test3.txt",
1204 "$base/unittest-cont1/subdir1/test1.txt",
1205 "$base/unittest-cont1/subdir1/test2.txt",
1206 "$base/unittest-cont1/subdir2/test3.txt",
1207 "$base/unittest-cont1/subdir2/test4.txt",
1208 "$base/unittest-cont1/subdir2/subdir/test1.txt",
1209 "$base/unittest-cont1/subdir2/subdir/test2.txt",
1210 "$base/unittest-cont1/subdir2/subdir/test3.txt",
1211 "$base/unittest-cont1/subdir2/subdir/test4.txt",
1212 "$base/unittest-cont1/subdir2/subdir/test5.txt",
1213 "$base/unittest-cont1/subdir2/subdir/sub/test0.txt",
1214 "$base/unittest-cont1/subdir2/subdir/sub/120-px-file.txt",
1215 );
1216
1217 // Add the files
1218 $ops = array();
1219 foreach ( $files as $file ) {
1220 $this->prepare( array( 'dir' => dirname( $file ) ) );
1221 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1222 }
1223 $status = $this->backend->doOperations( $ops );
1224 $this->assertEquals( array(), $status->errors,
1225 "Creation of files succeeded ($backendName)." );
1226 $this->assertEquals( true, $status->isOK(),
1227 "Creation of files succeeded with OK status ($backendName)." );
1228
1229 // Expected listing
1230 $expected = array(
1231 "test1.txt",
1232 "test2.txt",
1233 "test3.txt",
1234 "subdir1/test1.txt",
1235 "subdir1/test2.txt",
1236 "subdir2/test3.txt",
1237 "subdir2/test4.txt",
1238 "subdir2/subdir/test1.txt",
1239 "subdir2/subdir/test2.txt",
1240 "subdir2/subdir/test3.txt",
1241 "subdir2/subdir/test4.txt",
1242 "subdir2/subdir/test5.txt",
1243 "subdir2/subdir/sub/test0.txt",
1244 "subdir2/subdir/sub/120-px-file.txt",
1245 );
1246 sort( $expected );
1247
1248 // Actual listing (no trailing slash)
1249 $list = array();
1250 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1251 foreach ( $iter as $file ) {
1252 $list[] = $file;
1253 }
1254 sort( $list );
1255
1256 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1257
1258 // Actual listing (with trailing slash)
1259 $list = array();
1260 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1261 foreach ( $iter as $file ) {
1262 $list[] = $file;
1263 }
1264 sort( $list );
1265
1266 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1267
1268 // Expected listing
1269 $expected = array(
1270 "test1.txt",
1271 "test2.txt",
1272 "test3.txt",
1273 "test4.txt",
1274 "test5.txt",
1275 "sub/test0.txt",
1276 "sub/120-px-file.txt",
1277 );
1278 sort( $expected );
1279
1280 // Actual listing (no trailing slash)
1281 $list = array();
1282 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) );
1283 foreach ( $iter as $file ) {
1284 $list[] = $file;
1285 }
1286 sort( $list );
1287
1288 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1289
1290 // Actual listing (with trailing slash)
1291 $list = array();
1292 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir/" ) );
1293 foreach ( $iter as $file ) {
1294 $list[] = $file;
1295 }
1296 sort( $list );
1297
1298 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1299
1300 // Actual listing (using iterator second time)
1301 $list = array();
1302 foreach ( $iter as $file ) {
1303 $list[] = $file;
1304 }
1305 sort( $list );
1306
1307 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1308
1309 foreach ( $files as $file ) { // clean up
1310 $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1311 }
1312
1313 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1314 foreach ( $iter as $iter ) {} // no errors
1315 }
1316
1317 // test helper wrapper for backend prepare() function
1318 private function prepare( array $params ) {
1319 $this->dirsToPrune[] = $params['dir'];
1320 return $this->backend->prepare( $params );
1321 }
1322
1323 function tearDownFiles() {
1324 foreach ( $this->filesToPrune as $file ) {
1325 @unlink( $file );
1326 }
1327 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1328 foreach ( $containers as $container ) {
1329 $this->deleteFiles( $container );
1330 }
1331 foreach ( $this->dirsToPrune as $dir ) {
1332 $this->recursiveClean( $dir );
1333 }
1334 $this->filesToPrune = $this->dirsToPrune = array();
1335 }
1336
1337 private function deleteFiles( $container ) {
1338 $base = $this->baseStorePath();
1339 $iter = $this->backend->getFileList( array( 'dir' => "$base/$container" ) );
1340 if ( $iter ) {
1341 foreach ( $iter as $file ) {
1342 $this->backend->delete( array( 'src' => "$base/$container/$file" ), array( 'force' => 1 ) );
1343 }
1344 }
1345 }
1346
1347 private function recursiveClean( $dir ) {
1348 do {
1349 if ( !$this->backend->clean( array( 'dir' => $dir ) )->isOK() ) {
1350 break;
1351 }
1352 } while ( $dir = FileBackend::parentStoragePath( $dir ) );
1353 }
1354
1355 function tearDown() {
1356 parent::tearDown();
1357 }
1358 }