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