8 class FileBackendTest
extends MediaWikiTestCase
{
9 private $backend, $multiBackend;
10 private $filesToPrune = array();
11 private static $backendToUse;
13 protected function setUp() {
14 global $wgFileBackends;
16 $tmpPrefix = wfTempDir() . '/filebackend-unittest-' . time() . '-' . mt_rand();
17 if ( $this->getCliArg( 'use-filebackend=' ) ) {
18 if ( self
::$backendToUse ) {
19 $this->singleBackend
= self
::$backendToUse;
21 $name = $this->getCliArg( 'use-filebackend=' );
23 foreach ( $wgFileBackends as $conf ) {
24 if ( $conf['name'] == $name ) {
29 $useConfig['name'] = 'localtesting'; // swap name
30 $useConfig['shardViaHashLevels'] = array( // test sharding
31 'unittest-cont1' => array( 'levels' => 1, 'base' => 16, 'repeat' => 1 )
33 $class = $useConfig['class'];
34 self
::$backendToUse = new $class( $useConfig );
35 $this->singleBackend
= self
::$backendToUse;
38 $this->singleBackend
= new FSFileBackend( array(
39 'name' => 'localtesting',
40 'lockManager' => 'fsLockManager',
41 #'parallelize' => 'implicit',
42 'containerPaths' => array(
43 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
44 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
47 $this->multiBackend
= new FileBackendMultiWrite( array(
48 'name' => 'localtesting',
49 'lockManager' => 'fsLockManager',
50 'parallelize' => 'implicit',
53 'name' => 'localmultitesting1',
54 'class' => 'FSFileBackend',
55 'lockManager' => 'nullLockManager',
56 'containerPaths' => array(
57 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
58 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
59 'isMultiMaster' => false
62 'name' => 'localmultitesting2',
63 'class' => 'FSFileBackend',
64 'lockManager' => 'nullLockManager',
65 'containerPaths' => array(
66 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
67 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
68 'isMultiMaster' => true
72 $this->filesToPrune
= array();
75 private static function baseStorePath() {
76 return 'mwstore://localtesting';
79 private function backendClass() {
80 return get_class( $this->backend
);
84 * @dataProvider provider_testIsStoragePath
86 public function testIsStoragePath( $path, $isStorePath ) {
87 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
88 "FileBackend::isStoragePath on path '$path'" );
91 public static function provider_testIsStoragePath() {
93 array( 'mwstore://', true ),
94 array( 'mwstore://backend', true ),
95 array( 'mwstore://backend/container', true ),
96 array( 'mwstore://backend/container/', true ),
97 array( 'mwstore://backend/container/path', true ),
98 array( 'mwstore://backend//container/', true ),
99 array( 'mwstore://backend//container//', true ),
100 array( 'mwstore://backend//container//path', true ),
101 array( 'mwstore:///', true ),
102 array( 'mwstore:/', false ),
103 array( 'mwstore:', false ),
108 * @dataProvider provider_testSplitStoragePath
110 public function testSplitStoragePath( $path, $res ) {
111 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
112 "FileBackend::splitStoragePath on path '$path'" );
115 public static function provider_testSplitStoragePath() {
117 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
118 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
119 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
120 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
121 array( 'mwstore://backend//container/path', array( null, null, null ) ),
122 array( 'mwstore://backend//container//path', array( null, null, null ) ),
123 array( 'mwstore://', array( null, null, null ) ),
124 array( 'mwstore://backend', array( null, null, null ) ),
125 array( 'mwstore:///', array( null, null, null ) ),
126 array( 'mwstore:/', array( null, null, null ) ),
127 array( 'mwstore:', array( null, null, null ) )
132 * @dataProvider provider_normalizeStoragePath
134 public function testNormalizeStoragePath( $path, $res ) {
135 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
136 "FileBackend::normalizeStoragePath on path '$path'" );
139 public static function provider_normalizeStoragePath() {
141 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
142 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
143 array( 'mwstore://backend/container/path', 'mwstore://backend/container/path' ),
144 array( 'mwstore://backend/container//path', 'mwstore://backend/container/path' ),
145 array( 'mwstore://backend/container///path', 'mwstore://backend/container/path' ),
146 array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj' ),
147 array( 'mwstore://', null ),
148 array( 'mwstore://backend', null ),
149 array( 'mwstore://backend//container/path', null ),
150 array( 'mwstore://backend//container//path', null ),
151 array( 'mwstore:///', null ),
152 array( 'mwstore:/', null ),
153 array( 'mwstore:', null ),
158 * @dataProvider provider_testParentStoragePath
160 public function testParentStoragePath( $path, $res ) {
161 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
162 "FileBackend::parentStoragePath on path '$path'" );
165 public static function provider_testParentStoragePath() {
167 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
168 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
169 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
170 array( 'mwstore://backend/container', null ),
171 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
172 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
173 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
174 array( 'mwstore://backend/container/', null ),
179 * @dataProvider provider_testExtensionFromPath
181 public function testExtensionFromPath( $path, $res ) {
182 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
183 "FileBackend::extensionFromPath on path '$path'" );
186 public static function provider_testExtensionFromPath() {
188 array( 'mwstore://backend/container/path.txt', 'txt' ),
189 array( 'mwstore://backend/container/path.svg.png', 'png' ),
190 array( 'mwstore://backend/container/path', '' ),
191 array( 'mwstore://backend/container/path.', '' ),
196 * @dataProvider provider_testStore
198 public function testStore( $op ) {
199 $this->filesToPrune
[] = $op['src'];
201 $this->backend
= $this->singleBackend
;
202 $this->tearDownFiles();
203 $this->doTestStore( $op );
204 $this->tearDownFiles();
206 $this->backend
= $this->multiBackend
;
207 $this->tearDownFiles();
208 $this->doTestStore( $op );
209 $this->filesToPrune
[] = $op['src']; # avoid file leaking
210 $this->tearDownFiles();
213 private function doTestStore( $op ) {
214 $backendName = $this->backendClass();
216 $source = $op['src'];
218 $this->prepare( array( 'dir' => dirname( $dest ) ) );
220 file_put_contents( $source, "Unit test file" );
222 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
223 $this->backend
->store( $op );
226 $status = $this->backend
->doOperation( $op );
228 $this->assertGoodStatus( $status,
229 "Store from $source to $dest succeeded without warnings ($backendName)." );
230 $this->assertEquals( true, $status->isOK(),
231 "Store from $source to $dest succeeded ($backendName)." );
232 $this->assertEquals( array( 0 => true ), $status->success
,
233 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
234 $this->assertEquals( true, file_exists( $source ),
235 "Source file $source still exists ($backendName)." );
236 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
237 "Destination file $dest exists ($backendName)." );
239 $this->assertEquals( filesize( $source ),
240 $this->backend
->getFileSize( array( 'src' => $dest ) ),
241 "Destination file $dest has correct size ($backendName)." );
243 $props1 = FSFile
::getPropsFromPath( $source );
244 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
245 $this->assertEquals( $props1, $props2,
246 "Source and destination have the same props ($backendName)." );
248 $this->assertBackendPathsConsistent( array( $dest ) );
251 public static function provider_testStore() {
254 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
255 $toPath = self
::baseStorePath() . '/unittest-cont1/e/fun/obj1.txt';
256 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
264 $op2['overwrite'] = true;
272 $op2['overwriteSame'] = true;
283 * @dataProvider provider_testCopy
285 public function testCopy( $op ) {
286 $this->backend
= $this->singleBackend
;
287 $this->tearDownFiles();
288 $this->doTestCopy( $op );
289 $this->tearDownFiles();
291 $this->backend
= $this->multiBackend
;
292 $this->tearDownFiles();
293 $this->doTestCopy( $op );
294 $this->tearDownFiles();
297 private function doTestCopy( $op ) {
298 $backendName = $this->backendClass();
300 $source = $op['src'];
302 $this->prepare( array( 'dir' => dirname( $source ) ) );
303 $this->prepare( array( 'dir' => dirname( $dest ) ) );
305 if ( isset( $op['ignoreMissingSource'] ) ) {
306 $status = $this->backend
->doOperation( $op );
307 $this->assertGoodStatus( $status,
308 "Move from $source to $dest succeeded without warnings ($backendName)." );
309 $this->assertEquals( array( 0 => true ), $status->success
,
310 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
311 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
312 "Source file $source does not exist ($backendName)." );
313 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $dest ) ),
314 "Destination file $dest does not exist ($backendName)." );
318 $status = $this->backend
->doOperation(
319 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
320 $this->assertGoodStatus( $status,
321 "Creation of file at $source succeeded ($backendName)." );
323 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
324 $this->backend
->copy( $op );
327 $status = $this->backend
->doOperation( $op );
329 $this->assertGoodStatus( $status,
330 "Copy from $source to $dest succeeded without warnings ($backendName)." );
331 $this->assertEquals( true, $status->isOK(),
332 "Copy from $source to $dest succeeded ($backendName)." );
333 $this->assertEquals( array( 0 => true ), $status->success
,
334 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
335 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
336 "Source file $source still exists ($backendName)." );
337 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
338 "Destination file $dest exists after copy ($backendName)." );
341 $this->backend
->getFileSize( array( 'src' => $source ) ),
342 $this->backend
->getFileSize( array( 'src' => $dest ) ),
343 "Destination file $dest has correct size ($backendName)." );
345 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
346 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
347 $this->assertEquals( $props1, $props2,
348 "Source and destination have the same props ($backendName)." );
350 $this->assertBackendPathsConsistent( array( $source, $dest ) );
353 public static function provider_testCopy() {
356 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
357 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
359 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
367 $op2['overwrite'] = true;
375 $op2['overwriteSame'] = true;
383 $op2['ignoreMissingSource'] = true;
391 $op2['ignoreMissingSource'] = true;
394 self
::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
402 * @dataProvider provider_testMove
404 public function testMove( $op ) {
405 $this->backend
= $this->singleBackend
;
406 $this->tearDownFiles();
407 $this->doTestMove( $op );
408 $this->tearDownFiles();
410 $this->backend
= $this->multiBackend
;
411 $this->tearDownFiles();
412 $this->doTestMove( $op );
413 $this->tearDownFiles();
416 private function doTestMove( $op ) {
417 $backendName = $this->backendClass();
419 $source = $op['src'];
421 $this->prepare( array( 'dir' => dirname( $source ) ) );
422 $this->prepare( array( 'dir' => dirname( $dest ) ) );
424 if ( isset( $op['ignoreMissingSource'] ) ) {
425 $status = $this->backend
->doOperation( $op );
426 $this->assertGoodStatus( $status,
427 "Move from $source to $dest succeeded without warnings ($backendName)." );
428 $this->assertEquals( array( 0 => true ), $status->success
,
429 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
430 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
431 "Source file $source does not exist ($backendName)." );
432 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $dest ) ),
433 "Destination file $dest does not exist ($backendName)." );
437 $status = $this->backend
->doOperation(
438 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
439 $this->assertGoodStatus( $status,
440 "Creation of file at $source succeeded ($backendName)." );
442 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
443 $this->backend
->copy( $op );
446 $status = $this->backend
->doOperation( $op );
447 $this->assertGoodStatus( $status,
448 "Move from $source to $dest succeeded without warnings ($backendName)." );
449 $this->assertEquals( true, $status->isOK(),
450 "Move from $source to $dest succeeded ($backendName)." );
451 $this->assertEquals( array( 0 => true ), $status->success
,
452 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
453 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
454 "Source file $source does not still exists ($backendName)." );
455 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
456 "Destination file $dest exists after move ($backendName)." );
458 $this->assertNotEquals(
459 $this->backend
->getFileSize( array( 'src' => $source ) ),
460 $this->backend
->getFileSize( array( 'src' => $dest ) ),
461 "Destination file $dest has correct size ($backendName)." );
463 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
464 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
465 $this->assertEquals( false, $props1['fileExists'],
466 "Source file does not exist accourding to props ($backendName)." );
467 $this->assertEquals( true, $props2['fileExists'],
468 "Destination file exists accourding to props ($backendName)." );
470 $this->assertBackendPathsConsistent( array( $source, $dest ) );
473 public static function provider_testMove() {
476 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
477 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
479 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
487 $op2['overwrite'] = true;
495 $op2['overwriteSame'] = true;
503 $op2['ignoreMissingSource'] = true;
511 $op2['ignoreMissingSource'] = true;
514 self
::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
522 * @dataProvider provider_testDelete
524 public function testDelete( $op, $withSource, $okStatus ) {
525 $this->backend
= $this->singleBackend
;
526 $this->tearDownFiles();
527 $this->doTestDelete( $op, $withSource, $okStatus );
528 $this->tearDownFiles();
530 $this->backend
= $this->multiBackend
;
531 $this->tearDownFiles();
532 $this->doTestDelete( $op, $withSource, $okStatus );
533 $this->tearDownFiles();
536 private function doTestDelete( $op, $withSource, $okStatus ) {
537 $backendName = $this->backendClass();
539 $source = $op['src'];
540 $this->prepare( array( 'dir' => dirname( $source ) ) );
543 $status = $this->backend
->doOperation(
544 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
545 $this->assertGoodStatus( $status,
546 "Creation of file at $source succeeded ($backendName)." );
549 $status = $this->backend
->doOperation( $op );
551 $this->assertGoodStatus( $status,
552 "Deletion of file at $source succeeded without warnings ($backendName)." );
553 $this->assertEquals( true, $status->isOK(),
554 "Deletion of file at $source succeeded ($backendName)." );
555 $this->assertEquals( array( 0 => true ), $status->success
,
556 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
558 $this->assertEquals( false, $status->isOK(),
559 "Deletion of file at $source failed ($backendName)." );
562 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
563 "Source file $source does not exist after move ($backendName)." );
566 $this->backend
->getFileSize( array( 'src' => $source ) ),
567 "Source file $source has correct size (false) ($backendName)." );
569 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
570 $this->assertFalse( $props1['fileExists'],
571 "Source file $source does not exist according to props ($backendName)." );
573 $this->assertBackendPathsConsistent( array( $source ) );
576 public static function provider_testDelete() {
579 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
581 $op = array( 'op' => 'delete', 'src' => $source );
590 false, // without source
594 $op['ignoreMissingSource'] = true;
597 false, // without source
601 $op['ignoreMissingSource'] = true;
602 $op['src'] = self
::baseStorePath() . '/unittest-cont-bad/e/file.txt';
605 false, // without source
613 * @dataProvider provider_testDescribe
615 public function testDescribe( $op, $withSource, $okStatus ) {
616 $this->backend
= $this->singleBackend
;
617 $this->tearDownFiles();
618 $this->doTestDescribe( $op, $withSource, $okStatus );
619 $this->tearDownFiles();
621 $this->backend
= $this->multiBackend
;
622 $this->tearDownFiles();
623 $this->doTestDescribe( $op, $withSource, $okStatus );
624 $this->tearDownFiles();
627 private function doTestDescribe( $op, $withSource, $okStatus ) {
628 $backendName = $this->backendClass();
630 $source = $op['src'];
631 $this->prepare( array( 'dir' => dirname( $source ) ) );
634 $status = $this->backend
->doOperation(
635 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
636 $this->assertGoodStatus( $status,
637 "Creation of file at $source succeeded ($backendName)." );
640 $status = $this->backend
->doOperation( $op );
642 $this->assertGoodStatus( $status,
643 "Describe of file at $source succeeded without warnings ($backendName)." );
644 $this->assertEquals( true, $status->isOK(),
645 "Describe of file at $source succeeded ($backendName)." );
646 $this->assertEquals( array( 0 => true ), $status->success
,
647 "Describe of file at $source has proper 'success' field in Status ($backendName)." );
649 $this->assertEquals( false, $status->isOK(),
650 "Describe of file at $source failed ($backendName)." );
653 $this->assertBackendPathsConsistent( array( $source ) );
656 public static function provider_testDescribe() {
659 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
661 $op = array( 'op' => 'describe', 'src' => $source,
662 'headers' => array( 'X-Content-Length' => '91.3', 'Content-Old-Header' => '' ),
663 'disposition' => 'inline' );
672 false, // without source
680 * @dataProvider provider_testCreate
682 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
683 $this->backend
= $this->singleBackend
;
684 $this->tearDownFiles();
685 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
686 $this->tearDownFiles();
688 $this->backend
= $this->multiBackend
;
689 $this->tearDownFiles();
690 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
691 $this->tearDownFiles();
694 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
695 $backendName = $this->backendClass();
698 $this->prepare( array( 'dir' => dirname( $dest ) ) );
700 $oldText = 'blah...blah...waahwaah';
701 if ( $alreadyExists ) {
702 $status = $this->backend
->doOperation(
703 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
704 $this->assertGoodStatus( $status,
705 "Creation of file at $dest succeeded ($backendName)." );
708 $status = $this->backend
->doOperation( $op );
710 $this->assertGoodStatus( $status,
711 "Creation of file at $dest succeeded without warnings ($backendName)." );
712 $this->assertEquals( true, $status->isOK(),
713 "Creation of file at $dest succeeded ($backendName)." );
714 $this->assertEquals( array( 0 => true ), $status->success
,
715 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
717 $this->assertEquals( false, $status->isOK(),
718 "Creation of file at $dest failed ($backendName)." );
721 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
722 "Destination file $dest exists after creation ($backendName)." );
724 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
725 $this->assertEquals( true, $props1['fileExists'],
726 "Destination file $dest exists according to props ($backendName)." );
727 if ( $okStatus ) { // file content is what we saved
728 $this->assertEquals( $newSize, $props1['size'],
729 "Destination file $dest has expected size according to props ($backendName)." );
730 $this->assertEquals( $newSize,
731 $this->backend
->getFileSize( array( 'src' => $dest ) ),
732 "Destination file $dest has correct size ($backendName)." );
733 } else { // file content is some other previous text
734 $this->assertEquals( strlen( $oldText ), $props1['size'],
735 "Destination file $dest has original size according to props ($backendName)." );
736 $this->assertEquals( strlen( $oldText ),
737 $this->backend
->getFileSize( array( 'src' => $dest ) ),
738 "Destination file $dest has original size according to props ($backendName)." );
741 $this->assertBackendPathsConsistent( array( $dest ) );
745 * @dataProvider provider_testCreate
747 public static function provider_testCreate() {
750 $dest = self
::baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
752 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
755 false, // no dest already exists
757 strlen( $op['content'] )
761 $op2['content'] = "\n";
764 false, // no dest already exists
766 strlen( $op2['content'] )
770 $op2['content'] = "fsf\n waf 3kt";
773 true, // dest already exists
775 strlen( $op2['content'] )
779 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
780 $op2['overwrite'] = true;
783 true, // dest already exists
785 strlen( $op2['content'] )
789 $op2['content'] = "39qjmg3-qg";
790 $op2['overwriteSame'] = true;
793 true, // dest already exists
795 strlen( $op2['content'] )
801 public function testDoQuickOperations() {
802 $this->backend
= $this->singleBackend
;
803 $this->doTestDoQuickOperations();
804 $this->tearDownFiles();
806 $this->backend
= $this->multiBackend
;
807 $this->doTestDoQuickOperations();
808 $this->tearDownFiles();
811 private function doTestDoQuickOperations() {
812 $backendName = $this->backendClass();
814 $base = self
::baseStorePath();
816 "$base/unittest-cont1/e/fileA.a",
817 "$base/unittest-cont1/e/fileB.a",
818 "$base/unittest-cont1/e/fileC.a"
820 $createOps = array();
822 foreach ( $files as $path ) {
823 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
824 $this->assertGoodStatus( $status,
825 "Preparing $path succeeded without warnings ($backendName)." );
826 $createOps[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand(0, 50000) );
827 $copyOps[] = array( 'op' => 'copy', 'src' => $path, 'dst' => "$path-2" );
828 $moveOps[] = array( 'op' => 'move', 'src' => "$path-2", 'dst' => "$path-3" );
829 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
830 $purgeOps[] = array( 'op' => 'delete', 'src' => "$path-3" );
832 $purgeOps[] = array( 'op' => 'null' );
834 $this->assertGoodStatus(
835 $this->backend
->doQuickOperations( $createOps ),
836 "Creation of source files succeeded ($backendName)." );
837 foreach ( $files as $file ) {
838 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $file ) ),
839 "File $file exists." );
842 $this->assertGoodStatus(
843 $this->backend
->doQuickOperations( $copyOps ),
844 "Quick copy of source files succeeded ($backendName)." );
845 foreach ( $files as $file ) {
846 $this->assertTrue( $this->backend
->fileExists( array( 'src' => "$file-2" ) ),
847 "File $file-2 exists." );
850 $this->assertGoodStatus(
851 $this->backend
->doQuickOperations( $moveOps ),
852 "Quick move of source files succeeded ($backendName)." );
853 foreach ( $files as $file ) {
854 $this->assertTrue( $this->backend
->fileExists( array( 'src' => "$file-3" ) ),
855 "File $file-3 move in." );
856 $this->assertFalse( $this->backend
->fileExists( array( 'src' => "$file-2" ) ),
857 "File $file-2 moved away." );
860 $this->assertGoodStatus(
861 $this->backend
->quickCopy( array( 'src' => $files[0], 'dst' => $files[0] ) ),
862 "Copy of file {$files[0]} over itself succeeded ($backendName)." );
863 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $files[0] ) ),
864 "File {$files[0]} still exists." );
866 $this->assertGoodStatus(
867 $this->backend
->quickMove( array( 'src' => $files[0], 'dst' => $files[0] ) ),
868 "Move of file {$files[0]} over itself succeeded ($backendName)." );
869 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $files[0] ) ),
870 "File {$files[0]} still exists." );
872 $this->assertGoodStatus(
873 $this->backend
->doQuickOperations( $purgeOps ),
874 "Quick deletion of source files succeeded ($backendName)." );
875 foreach ( $files as $file ) {
876 $this->assertFalse( $this->backend
->fileExists( array( 'src' => $file ) ),
877 "File $file purged." );
878 $this->assertFalse( $this->backend
->fileExists( array( 'src' => "$file-3" ) ),
879 "File $file-3 purged." );
884 * @dataProvider provider_testConcatenate
886 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
887 $this->filesToPrune
[] = $op['dst'];
889 $this->backend
= $this->singleBackend
;
890 $this->tearDownFiles();
891 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
892 $this->tearDownFiles();
894 $this->backend
= $this->multiBackend
;
895 $this->tearDownFiles();
896 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
897 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
898 $this->tearDownFiles();
901 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
902 $backendName = $this->backendClass();
907 foreach ( $srcs as $i => $source ) {
908 $this->prepare( array( 'dir' => dirname( $source ) ) );
910 'op' => 'create', // operation
911 'dst' => $source, // source
912 'content' => $srcsContent[$i]
914 $expContent .= $srcsContent[$i];
916 $status = $this->backend
->doOperations( $ops );
918 $this->assertGoodStatus( $status,
919 "Creation of source files succeeded ($backendName)." );
921 $dest = $params['dst'];
922 if ( $alreadyExists ) {
923 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
924 $this->assertEquals( true, $ok,
925 "Creation of file at $dest succeeded ($backendName)." );
927 $ok = file_put_contents( $dest, '' ) !== false;
928 $this->assertEquals( true, $ok,
929 "Creation of 0-byte file at $dest succeeded ($backendName)." );
932 // Combine the files into one
933 $status = $this->backend
->concatenate( $params );
935 $this->assertGoodStatus( $status,
936 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
937 $this->assertEquals( true, $status->isOK(),
938 "Creation of concat file at $dest succeeded ($backendName)." );
940 $this->assertEquals( false, $status->isOK(),
941 "Creation of concat file at $dest failed ($backendName)." );
945 $this->assertEquals( true, is_file( $dest ),
946 "Dest concat file $dest exists after creation ($backendName)." );
948 $this->assertEquals( true, is_file( $dest ),
949 "Dest concat file $dest exists after failed creation ($backendName)." );
952 $contents = file_get_contents( $dest );
953 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
956 $this->assertEquals( $expContent, $contents,
957 "Concat file at $dest has correct contents ($backendName)." );
959 $this->assertNotEquals( $expContent, $contents,
960 "Concat file at $dest has correct contents ($backendName)." );
964 public static function provider_testConcatenate() {
967 $rand = mt_rand( 0, 2000000000 ) . time();
968 $dest = wfTempDir() . "/randomfile!$rand.txt";
970 self
::baseStorePath() . '/unittest-cont1/e/file1.txt',
971 self
::baseStorePath() . '/unittest-cont1/e/file2.txt',
972 self
::baseStorePath() . '/unittest-cont1/e/file3.txt',
973 self
::baseStorePath() . '/unittest-cont1/e/file4.txt',
974 self
::baseStorePath() . '/unittest-cont1/e/file5.txt',
975 self
::baseStorePath() . '/unittest-cont1/e/file6.txt',
976 self
::baseStorePath() . '/unittest-cont1/e/file7.txt',
977 self
::baseStorePath() . '/unittest-cont1/e/file8.txt',
978 self
::baseStorePath() . '/unittest-cont1/e/file9.txt',
979 self
::baseStorePath() . '/unittest-cont1/e/file10.txt'
993 $params = array( 'srcs' => $srcs, 'dst' => $dest );
996 $params, // operation
998 $content, // content for each source
999 false, // no dest already exists
1004 $params, // operation
1006 $content, // content for each source
1007 true, // dest already exists
1015 * @dataProvider provider_testGetFileStat
1017 public function testGetFileStat( $path, $content, $alreadyExists ) {
1018 $this->backend
= $this->singleBackend
;
1019 $this->tearDownFiles();
1020 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1021 $this->tearDownFiles();
1023 $this->backend
= $this->multiBackend
;
1024 $this->tearDownFiles();
1025 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1026 $this->tearDownFiles();
1029 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
1030 $backendName = $this->backendClass();
1032 if ( $alreadyExists ) {
1033 $this->prepare( array( 'dir' => dirname( $path ) ) );
1034 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
1035 $this->assertGoodStatus( $status,
1036 "Creation of file at $path succeeded ($backendName)." );
1038 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1039 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1040 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
1042 $this->assertEquals( strlen( $content ), $size,
1043 "Correct file size of '$path'" );
1044 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1045 "Correct file timestamp of '$path'" );
1047 $size = $stat['size'];
1048 $time = $stat['mtime'];
1049 $this->assertEquals( strlen( $content ), $size,
1050 "Correct file size of '$path'" );
1051 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1052 "Correct file timestamp of '$path'" );
1054 $this->backend
->clearCache( array( $path ) );
1056 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1058 $this->assertEquals( strlen( $content ), $size,
1059 "Correct file size of '$path'" );
1061 $this->backend
->preloadCache( array( $path ) );
1063 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1065 $this->assertEquals( strlen( $content ), $size,
1066 "Correct file size of '$path'" );
1068 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1069 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1070 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
1072 $this->assertFalse( $size, "Correct file size of '$path'" );
1073 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1074 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1078 public static function provider_testGetFileStat() {
1081 $base = self
::baseStorePath();
1082 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
1083 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
1084 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
1090 * @dataProvider provider_testGetFileContents
1092 public function testGetFileContents( $source, $content ) {
1093 $this->backend
= $this->singleBackend
;
1094 $this->tearDownFiles();
1095 $this->doTestGetFileContents( $source, $content );
1096 $this->tearDownFiles();
1098 $this->backend
= $this->multiBackend
;
1099 $this->tearDownFiles();
1100 $this->doTestGetFileContents( $source, $content );
1101 $this->tearDownFiles();
1104 private function doTestGetFileContents( $source, $content ) {
1105 $backendName = $this->backendClass();
1107 $srcs = (array)$source;
1108 $content = (array)$content;
1109 foreach ( $srcs as $i => $src ) {
1110 $this->prepare( array( 'dir' => dirname( $src ) ) );
1111 $status = $this->backend
->doOperation(
1112 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1113 $this->assertGoodStatus( $status,
1114 "Creation of file at $src succeeded ($backendName)." );
1117 if ( is_array( $source ) ) {
1118 $contents = $this->backend
->getFileContentsMulti( array( 'srcs' => $source ) );
1119 foreach ( $contents as $path => $data ) {
1120 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1121 $this->assertEquals( current( $content ), $data, "Contents of $path is correct ($backendName)." );
1124 $this->assertEquals( $source, array_keys( $contents ), "Contents in right order ($backendName)." );
1125 $this->assertEquals( count( $source ), count( $contents ), "Contents array size correct ($backendName)." );
1127 $data = $this->backend
->getFileContents( array( 'src' => $source ) );
1128 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1129 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1133 public static function provider_testGetFileContents() {
1136 $base = self
::baseStorePath();
1137 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
1138 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
1140 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1141 "$base/unittest-cont1/e/a/z.txt" ),
1142 array( "contents xx", "contents xy", "contents xz" )
1149 * @dataProvider provider_testGetLocalCopy
1151 public function testGetLocalCopy( $source, $content ) {
1152 $this->backend
= $this->singleBackend
;
1153 $this->tearDownFiles();
1154 $this->doTestGetLocalCopy( $source, $content );
1155 $this->tearDownFiles();
1157 $this->backend
= $this->multiBackend
;
1158 $this->tearDownFiles();
1159 $this->doTestGetLocalCopy( $source, $content );
1160 $this->tearDownFiles();
1163 private function doTestGetLocalCopy( $source, $content ) {
1164 $backendName = $this->backendClass();
1166 $srcs = (array)$source;
1167 $content = (array)$content;
1168 foreach ( $srcs as $i => $src ) {
1169 $this->prepare( array( 'dir' => dirname( $src ) ) );
1170 $status = $this->backend
->doOperation(
1171 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1172 $this->assertGoodStatus( $status,
1173 "Creation of file at $src succeeded ($backendName)." );
1176 if ( is_array( $source ) ) {
1177 $tmpFiles = $this->backend
->getLocalCopyMulti( array( 'srcs' => $source ) );
1178 foreach ( $tmpFiles as $path => $tmpFile ) {
1179 $this->assertNotNull( $tmpFile,
1180 "Creation of local copy of $path succeeded ($backendName)." );
1181 $contents = file_get_contents( $tmpFile->getPath() );
1182 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1183 $this->assertEquals( current( $content ), $contents, "Local copy of $path is correct ($backendName)." );
1186 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local copies in right order ($backendName)." );
1187 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local copies array size correct ($backendName)." );
1189 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
1190 $this->assertNotNull( $tmpFile,
1191 "Creation of local copy of $source succeeded ($backendName)." );
1192 $contents = file_get_contents( $tmpFile->getPath() );
1193 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1194 $this->assertEquals( $content[0], $contents, "Local copy of $source is correct ($backendName)." );
1197 $obj = new stdClass();
1198 $tmpFile->bind( $obj );
1201 public static function provider_testGetLocalCopy() {
1204 $base = self
::baseStorePath();
1205 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1206 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1207 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1209 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1210 "$base/unittest-cont1/e/a/z.txt" ),
1211 array( "contents xx", "contents xy", "contents xz" )
1218 * @dataProvider provider_testGetLocalReference
1220 public function testGetLocalReference( $source, $content ) {
1221 $this->backend
= $this->singleBackend
;
1222 $this->tearDownFiles();
1223 $this->doTestGetLocalReference( $source, $content );
1224 $this->tearDownFiles();
1226 $this->backend
= $this->multiBackend
;
1227 $this->tearDownFiles();
1228 $this->doTestGetLocalReference( $source, $content );
1229 $this->tearDownFiles();
1232 private function doTestGetLocalReference( $source, $content ) {
1233 $backendName = $this->backendClass();
1235 $srcs = (array)$source;
1236 $content = (array)$content;
1237 foreach ( $srcs as $i => $src ) {
1238 $this->prepare( array( 'dir' => dirname( $src ) ) );
1239 $status = $this->backend
->doOperation(
1240 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1241 $this->assertGoodStatus( $status,
1242 "Creation of file at $src succeeded ($backendName)." );
1245 if ( is_array( $source ) ) {
1246 $tmpFiles = $this->backend
->getLocalReferenceMulti( array( 'srcs' => $source ) );
1247 foreach ( $tmpFiles as $path => $tmpFile ) {
1248 $this->assertNotNull( $tmpFile,
1249 "Creation of local copy of $path succeeded ($backendName)." );
1250 $contents = file_get_contents( $tmpFile->getPath() );
1251 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1252 $this->assertEquals( current( $content ), $contents, "Local ref of $path is correct ($backendName)." );
1255 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local refs in right order ($backendName)." );
1256 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local refs array size correct ($backendName)." );
1258 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
1259 $this->assertNotNull( $tmpFile,
1260 "Creation of local copy of $source succeeded ($backendName)." );
1261 $contents = file_get_contents( $tmpFile->getPath() );
1262 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1263 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1267 public static function provider_testGetLocalReference() {
1270 $base = self
::baseStorePath();
1271 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1272 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1273 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1275 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1276 "$base/unittest-cont1/e/a/z.txt" ),
1277 array( "contents xx", "contents xy", "contents xz" )
1283 public function testGetLocalCopyAndReference404() {
1284 $this->backend
= $this->singleBackend
;
1285 $this->tearDownFiles();
1286 $this->doTestGetLocalCopyAndReference404();
1287 $this->tearDownFiles();
1289 $this->backend
= $this->multiBackend
;
1290 $this->tearDownFiles();
1291 $this->doTestGetLocalCopyAndReference404();
1292 $this->tearDownFiles();
1295 public function doTestGetLocalCopyAndReference404() {
1296 $backendName = $this->backendClass();
1298 $base = self
::baseStorePath();
1300 $tmpFile = $this->backend
->getLocalCopy( array(
1301 'src' => "$base/unittest-cont1/not-there" ) );
1302 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1304 $tmpFile = $this->backend
->getLocalReference( array(
1305 'src' => "$base/unittest-cont1/not-there" ) );
1306 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1310 * @dataProvider provider_testGetFileHttpUrl
1312 public function testGetFileHttpUrl( $source, $content ) {
1313 $this->backend
= $this->singleBackend
;
1314 $this->tearDownFiles();
1315 $this->doTestGetFileHttpUrl( $source, $content );
1316 $this->tearDownFiles();
1318 $this->backend
= $this->multiBackend
;
1319 $this->tearDownFiles();
1320 $this->doTestGetFileHttpUrl( $source, $content );
1321 $this->tearDownFiles();
1324 private function doTestGetFileHttpUrl( $source, $content ) {
1325 $backendName = $this->backendClass();
1327 $this->prepare( array( 'dir' => dirname( $source ) ) );
1328 $status = $this->backend
->doOperation(
1329 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
1330 $this->assertGoodStatus( $status,
1331 "Creation of file at $source succeeded ($backendName)." );
1333 $url = $this->backend
->getFileHttpUrl( array( 'src' => $source ) );
1335 if ( $url !== null ) { // supported
1336 $data = Http
::request( "GET", $url );
1337 $this->assertEquals( $content, $data,
1338 "HTTP GET of URL has right contents ($backendName)." );
1342 public static function provider_testGetFileHttpUrl() {
1345 $base = self
::baseStorePath();
1346 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1347 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1348 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1354 * @dataProvider provider_testPrepareAndClean
1356 public function testPrepareAndClean( $path, $isOK ) {
1357 $this->backend
= $this->singleBackend
;
1358 $this->doTestPrepareAndClean( $path, $isOK );
1359 $this->tearDownFiles();
1361 $this->backend
= $this->multiBackend
;
1362 $this->doTestPrepareAndClean( $path, $isOK );
1363 $this->tearDownFiles();
1366 public static function provider_testPrepareAndClean() {
1367 $base = self
::baseStorePath();
1369 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1370 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1371 # Specific to FS backend with no basePath field set
1372 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1376 private function doTestPrepareAndClean( $path, $isOK ) {
1377 $backendName = $this->backendClass();
1379 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1381 $this->assertGoodStatus( $status,
1382 "Preparing dir $path succeeded without warnings ($backendName)." );
1383 $this->assertEquals( true, $status->isOK(),
1384 "Preparing dir $path succeeded ($backendName)." );
1386 $this->assertEquals( false, $status->isOK(),
1387 "Preparing dir $path failed ($backendName)." );
1390 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1392 $this->assertGoodStatus( $status,
1393 "Cleaning dir $path succeeded without warnings ($backendName)." );
1394 $this->assertEquals( true, $status->isOK(),
1395 "Cleaning dir $path succeeded ($backendName)." );
1397 $this->assertEquals( false, $status->isOK(),
1398 "Cleaning dir $path failed ($backendName)." );
1402 public function testRecursiveClean() {
1403 $this->backend
= $this->singleBackend
;
1404 $this->doTestRecursiveClean();
1405 $this->tearDownFiles();
1407 $this->backend
= $this->multiBackend
;
1408 $this->doTestRecursiveClean();
1409 $this->tearDownFiles();
1412 private function doTestRecursiveClean() {
1413 $backendName = $this->backendClass();
1415 $base = self
::baseStorePath();
1417 "$base/unittest-cont1",
1418 "$base/unittest-cont1/e",
1419 "$base/unittest-cont1/e/a",
1420 "$base/unittest-cont1/e/a/b",
1421 "$base/unittest-cont1/e/a/b/c",
1422 "$base/unittest-cont1/e/a/b/c/d0",
1423 "$base/unittest-cont1/e/a/b/c/d1",
1424 "$base/unittest-cont1/e/a/b/c/d2",
1425 "$base/unittest-cont1/e/a/b/c/d0/1",
1426 "$base/unittest-cont1/e/a/b/c/d0/2",
1427 "$base/unittest-cont1/e/a/b/c/d1/3",
1428 "$base/unittest-cont1/e/a/b/c/d1/4",
1429 "$base/unittest-cont1/e/a/b/c/d2/5",
1430 "$base/unittest-cont1/e/a/b/c/d2/6"
1432 foreach ( $dirs as $dir ) {
1433 $status = $this->prepare( array( 'dir' => $dir ) );
1434 $this->assertGoodStatus( $status,
1435 "Preparing dir $dir succeeded without warnings ($backendName)." );
1438 if ( $this->backend
instanceof FSFileBackend
) {
1439 foreach ( $dirs as $dir ) {
1440 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1441 "Dir $dir exists ($backendName)." );
1445 $status = $this->backend
->clean(
1446 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1447 $this->assertGoodStatus( $status,
1448 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1450 foreach ( $dirs as $dir ) {
1451 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1452 "Dir $dir no longer exists ($backendName)." );
1456 // @TODO: testSecure
1458 public function testDoOperations() {
1459 $this->backend
= $this->singleBackend
;
1460 $this->tearDownFiles();
1461 $this->doTestDoOperations();
1462 $this->tearDownFiles();
1464 $this->backend
= $this->multiBackend
;
1465 $this->tearDownFiles();
1466 $this->doTestDoOperations();
1467 $this->tearDownFiles();
1470 private function doTestDoOperations() {
1471 $base = self
::baseStorePath();
1473 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1474 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1475 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1476 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1477 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1478 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1479 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1481 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1482 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1483 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1484 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1485 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1486 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1487 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1489 $status = $this->backend
->doOperations( array(
1490 array( 'op' => 'describe', 'src' => $fileA,
1491 'headers' => array( 'X-Content-Length' => '91.3' ), 'disposition' => 'inline' ),
1492 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1493 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1494 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1495 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1496 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1497 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1498 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1499 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1500 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1501 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1502 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1503 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1504 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1505 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1506 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1507 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1508 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1510 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1512 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1514 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1516 array( 'op' => 'null' ),
1520 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1521 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1522 $this->assertEquals( 14, count( $status->success
),
1523 "Operation batch has correct success array" );
1525 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1526 "File does not exist at $fileA" );
1527 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1528 "File does not exist at $fileB" );
1529 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1530 "File does not exist at $fileD" );
1532 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1533 "File exists at $fileC" );
1534 $this->assertEquals( $fileBContents,
1535 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1536 "Correct file contents of $fileC" );
1537 $this->assertEquals( strlen( $fileBContents ),
1538 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1539 "Correct file size of $fileC" );
1540 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1541 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1542 "Correct file SHA-1 of $fileC" );
1545 public function testDoOperationsPipeline() {
1546 $this->backend
= $this->singleBackend
;
1547 $this->tearDownFiles();
1548 $this->doTestDoOperationsPipeline();
1549 $this->tearDownFiles();
1551 $this->backend
= $this->multiBackend
;
1552 $this->tearDownFiles();
1553 $this->doTestDoOperationsPipeline();
1554 $this->tearDownFiles();
1557 // concurrency orientated
1558 private function doTestDoOperationsPipeline() {
1559 $base = self
::baseStorePath();
1561 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1562 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1563 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1565 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1566 file_put_contents( $tmpNameA, $fileAContents );
1567 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1568 file_put_contents( $tmpNameB, $fileBContents );
1569 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1570 file_put_contents( $tmpNameC, $fileCContents );
1572 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1573 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1574 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1576 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1577 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1578 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1579 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1581 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1582 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1583 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1584 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1585 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1587 $status = $this->backend
->doOperations( array(
1588 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1589 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1590 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1591 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1592 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1593 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1594 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1595 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1596 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1597 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1598 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1599 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1600 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1601 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1602 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1603 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1604 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1605 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1606 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1607 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1609 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1611 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1613 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1615 array( 'op' => 'null' ),
1619 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1620 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1621 $this->assertEquals( 16, count( $status->success
),
1622 "Operation batch has correct success array" );
1624 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1625 "File does not exist at $fileA" );
1626 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1627 "File does not exist at $fileB" );
1628 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1629 "File does not exist at $fileD" );
1631 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1632 "File exists at $fileC" );
1633 $this->assertEquals( $fileBContents,
1634 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1635 "Correct file contents of $fileC" );
1636 $this->assertEquals( strlen( $fileBContents ),
1637 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1638 "Correct file size of $fileC" );
1639 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1640 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1641 "Correct file SHA-1 of $fileC" );
1644 public function testDoOperationsFailing() {
1645 $this->backend
= $this->singleBackend
;
1646 $this->tearDownFiles();
1647 $this->doTestDoOperationsFailing();
1648 $this->tearDownFiles();
1650 $this->backend
= $this->multiBackend
;
1651 $this->tearDownFiles();
1652 $this->doTestDoOperationsFailing();
1653 $this->tearDownFiles();
1656 private function doTestDoOperationsFailing() {
1657 $base = self
::baseStorePath();
1659 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1660 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1661 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1662 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1663 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1664 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1665 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1667 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1668 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1669 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1670 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1671 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1672 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1674 $status = $this->backend
->doOperations( array(
1675 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1676 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1677 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1678 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1679 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1680 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1681 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1682 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1683 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1684 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1685 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1686 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1687 array( 'op' => 'delete', 'src' => $fileD ),
1688 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1689 array( 'op' => 'null' ),
1691 ), array( 'force' => 1 ) );
1693 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1694 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1695 $this->assertEquals( 8, count( $status->success
),
1696 "Operation batch has correct success array" );
1698 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1699 "File does not exist at $fileB" );
1700 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1701 "File does not exist at $fileD" );
1703 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1704 "File does not exist at $fileA" );
1705 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1706 "File exists at $fileC" );
1707 $this->assertEquals( $fileBContents,
1708 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1709 "Correct file contents of $fileA" );
1710 $this->assertEquals( strlen( $fileBContents ),
1711 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1712 "Correct file size of $fileA" );
1713 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1714 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1715 "Correct file SHA-1 of $fileA" );
1718 public function testGetFileList() {
1719 $this->backend
= $this->singleBackend
;
1720 $this->tearDownFiles();
1721 $this->doTestGetFileList();
1722 $this->tearDownFiles();
1724 $this->backend
= $this->multiBackend
;
1725 $this->tearDownFiles();
1726 $this->doTestGetFileList();
1727 $this->tearDownFiles();
1730 private function doTestGetFileList() {
1731 $backendName = $this->backendClass();
1732 $base = self
::baseStorePath();
1734 // Should have no errors
1735 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1738 "$base/unittest-cont1/e/test1.txt",
1739 "$base/unittest-cont1/e/test2.txt",
1740 "$base/unittest-cont1/e/test3.txt",
1741 "$base/unittest-cont1/e/subdir1/test1.txt",
1742 "$base/unittest-cont1/e/subdir1/test2.txt",
1743 "$base/unittest-cont1/e/subdir2/test3.txt",
1744 "$base/unittest-cont1/e/subdir2/test4.txt",
1745 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1746 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1747 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1748 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1749 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1750 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1751 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1756 foreach ( $files as $file ) {
1757 $this->prepare( array( 'dir' => dirname( $file ) ) );
1758 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1760 $status = $this->backend
->doQuickOperations( $ops );
1761 $this->assertGoodStatus( $status,
1762 "Creation of files succeeded ($backendName)." );
1763 $this->assertEquals( true, $status->isOK(),
1764 "Creation of files succeeded with OK status ($backendName)." );
1771 "e/subdir1/test1.txt",
1772 "e/subdir1/test2.txt",
1773 "e/subdir2/test3.txt",
1774 "e/subdir2/test4.txt",
1775 "e/subdir2/subdir/test1.txt",
1776 "e/subdir2/subdir/test2.txt",
1777 "e/subdir2/subdir/test3.txt",
1778 "e/subdir2/subdir/test4.txt",
1779 "e/subdir2/subdir/test5.txt",
1780 "e/subdir2/subdir/sub/test0.txt",
1781 "e/subdir2/subdir/sub/120-px-file.txt",
1785 // Actual listing (no trailing slash)
1787 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1788 foreach ( $iter as $file ) {
1793 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1795 // Actual listing (with trailing slash)
1797 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1798 foreach ( $iter as $file ) {
1803 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1813 "sub/120-px-file.txt",
1817 // Actual listing (no trailing slash)
1819 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1820 foreach ( $iter as $file ) {
1825 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1827 // Actual listing (with trailing slash)
1829 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1830 foreach ( $iter as $file ) {
1835 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1837 // Actual listing (using iterator second time)
1839 foreach ( $iter as $file ) {
1844 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1846 // Expected listing (top files only)
1856 // Actual listing (top files only)
1858 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1859 foreach ( $iter as $file ) {
1864 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1866 foreach ( $files as $file ) { // clean up
1867 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1870 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1871 foreach ( $iter as $iter ) {} // no errors
1874 public function testGetDirectoryList() {
1875 $this->backend
= $this->singleBackend
;
1876 $this->tearDownFiles();
1877 $this->doTestGetDirectoryList();
1878 $this->tearDownFiles();
1880 $this->backend
= $this->multiBackend
;
1881 $this->tearDownFiles();
1882 $this->doTestGetDirectoryList();
1883 $this->tearDownFiles();
1886 private function doTestGetDirectoryList() {
1887 $backendName = $this->backendClass();
1889 $base = self
::baseStorePath();
1891 "$base/unittest-cont1/e/test1.txt",
1892 "$base/unittest-cont1/e/test2.txt",
1893 "$base/unittest-cont1/e/test3.txt",
1894 "$base/unittest-cont1/e/subdir1/test1.txt",
1895 "$base/unittest-cont1/e/subdir1/test2.txt",
1896 "$base/unittest-cont1/e/subdir2/test3.txt",
1897 "$base/unittest-cont1/e/subdir2/test4.txt",
1898 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1899 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1900 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1901 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1902 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1903 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1904 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1909 foreach ( $files as $file ) {
1910 $this->prepare( array( 'dir' => dirname( $file ) ) );
1911 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1913 $status = $this->backend
->doQuickOperations( $ops );
1914 $this->assertGoodStatus( $status,
1915 "Creation of files succeeded ($backendName)." );
1916 $this->assertEquals( true, $status->isOK(),
1917 "Creation of files succeeded with OK status ($backendName)." );
1919 $this->assertEquals( true,
1920 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1921 "Directory exists in ($backendName)." );
1922 $this->assertEquals( true,
1923 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1924 "Directory exists in ($backendName)." );
1925 $this->assertEquals( false,
1926 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1927 "Directory does not exists in ($backendName)." );
1935 // Actual listing (no trailing slash)
1937 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1938 foreach ( $iter as $file ) {
1943 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1954 // Actual listing (no trailing slash)
1956 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1957 foreach ( $iter as $file ) {
1962 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1964 // Actual listing (with trailing slash)
1966 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1967 foreach ( $iter as $file ) {
1972 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1980 // Actual listing (no trailing slash)
1982 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1983 foreach ( $iter as $file ) {
1988 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1990 // Actual listing (with trailing slash)
1992 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1993 foreach ( $iter as $file ) {
1998 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2000 // Actual listing (using iterator second time)
2002 foreach ( $iter as $file ) {
2007 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
2009 // Expected listing (recursive)
2019 "e/subdir4/subdir/sub",
2023 // Actual listing (recursive)
2025 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
2026 foreach ( $iter as $file ) {
2031 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2033 // Expected listing (recursive)
2040 // Actual listing (recursive)
2042 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
2043 foreach ( $iter as $file ) {
2048 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2050 // Actual listing (recursive, second time)
2052 foreach ( $iter as $file ) {
2057 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2059 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) );
2060 $items = is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2061 $this->assertEquals( array(), $items, "Directory listing is empty." );
2063 foreach ( $files as $file ) { // clean up
2064 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
2067 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
2068 foreach ( $iter as $file ) {} // no errors
2069 $items = is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2070 $this->assertEquals( array(), $items, "Directory listing is empty." );
2072 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/not/exists" ) );
2073 $items = is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2074 $this->assertEquals( array(), $items, "Directory listing is empty." );
2077 public function testLockCalls() {
2078 $this->backend
= $this->singleBackend
;
2079 $this->doTestLockCalls();
2082 private function doTestLockCalls() {
2083 $backendName = $this->backendClass();
2090 "subdir1", // duplicate
2091 "subdir1/test1.txt",
2092 "subdir1/test2.txt",
2094 "subdir2", // duplicate
2095 "subdir2/test3.txt",
2096 "subdir2/test4.txt",
2098 "subdir2/subdir/test1.txt",
2099 "subdir2/subdir/test2.txt",
2100 "subdir2/subdir/test3.txt",
2101 "subdir2/subdir/test4.txt",
2102 "subdir2/subdir/test5.txt",
2103 "subdir2/subdir/sub",
2104 "subdir2/subdir/sub/test0.txt",
2105 "subdir2/subdir/sub/120-px-file.txt",
2108 for ( $i=0; $i<25; $i++
) {
2109 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2110 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2111 "Locking of files succeeded ($backendName) ($i)." );
2112 $this->assertEquals( true, $status->isOK(),
2113 "Locking of files succeeded with OK status ($backendName) ($i)." );
2115 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2116 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2117 "Locking of files succeeded ($backendName) ($i)." );
2118 $this->assertEquals( true, $status->isOK(),
2119 "Locking of files succeeded with OK status ($backendName) ($i)." );
2121 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2122 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2123 "Locking of files succeeded ($backendName) ($i)." );
2124 $this->assertEquals( true, $status->isOK(),
2125 "Locking of files succeeded with OK status ($backendName) ($i)." );
2127 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2128 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2129 "Locking of files succeeded ($backendName). ($i)" );
2130 $this->assertEquals( true, $status->isOK(),
2131 "Locking of files succeeded with OK status ($backendName) ($i)." );
2133 ## Flip the acquire/release ordering around ##
2135 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2136 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2137 "Locking of files succeeded ($backendName) ($i)." );
2138 $this->assertEquals( true, $status->isOK(),
2139 "Locking of files succeeded with OK status ($backendName) ($i)." );
2141 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2142 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2143 "Locking of files succeeded ($backendName) ($i)." );
2144 $this->assertEquals( true, $status->isOK(),
2145 "Locking of files succeeded with OK status ($backendName) ($i)." );
2147 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2148 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2149 "Locking of files succeeded ($backendName). ($i)" );
2150 $this->assertEquals( true, $status->isOK(),
2151 "Locking of files succeeded with OK status ($backendName) ($i)." );
2153 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2154 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2155 "Locking of files succeeded ($backendName) ($i)." );
2156 $this->assertEquals( true, $status->isOK(),
2157 "Locking of files succeeded with OK status ($backendName) ($i)." );
2160 $status = Status
::newGood();
2161 $sl = $this->backend
->getScopedFileLocks( $paths, LockManager
::LOCK_EX
, $status );
2162 $this->assertType( 'ScopedLock', $sl,
2163 "Scoped locking of files succeeded ($backendName)." );
2164 $this->assertEquals( array(), $status->errors
,
2165 "Scoped locking of files succeeded ($backendName)." );
2166 $this->assertEquals( true, $status->isOK(),
2167 "Scoped locking of files succeeded with OK status ($backendName)." );
2169 ScopedLock
::release( $sl );
2170 $this->assertEquals( null, $sl,
2171 "Scoped unlocking of files succeeded ($backendName)." );
2172 $this->assertEquals( array(), $status->errors
,
2173 "Scoped unlocking of files succeeded ($backendName)." );
2174 $this->assertEquals( true, $status->isOK(),
2175 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2178 // test helper wrapper for backend prepare() function
2179 private function prepare( array $params ) {
2180 return $this->backend
->prepare( $params );
2183 // test helper wrapper for backend prepare() function
2184 private function create( array $params ) {
2185 $params['op'] = 'create';
2186 return $this->backend
->doQuickOperations( array( $params ) );
2189 function tearDownFiles() {
2190 foreach ( $this->filesToPrune
as $file ) {
2193 $containers = array( 'unittest-cont1', 'unittest-cont2' );
2194 foreach ( $containers as $container ) {
2195 $this->deleteFiles( $container );
2197 $this->filesToPrune
= array();
2200 private function deleteFiles( $container ) {
2201 $base = self
::baseStorePath();
2202 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
2204 foreach ( $iter as $file ) {
2205 $this->backend
->quickDelete( array( 'src' => "$base/$container/$file" ) );
2207 // free the directory, to avoid Permission denied under windows on rmdir
2210 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
2213 function assertBackendPathsConsistent( array $paths ) {
2214 if ( $this->backend
instanceof FileBackendMultiWrite
) {
2215 $status = $this->backend
->consistencyCheck( $paths );
2216 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2220 function assertGoodStatus( $status, $msg ) {
2221 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );