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 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 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 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 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"
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 $ops[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand(0, 50000) );
827 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
829 $purgeOps[] = array( 'op' => 'null' );
830 $status = $this->backend
->doQuickOperations( $ops );
831 $this->assertGoodStatus( $status,
832 "Creation of source files succeeded ($backendName)." );
834 foreach ( $files as $file ) {
835 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $file ) ),
836 "File $file exists." );
839 $status = $this->backend
->doQuickOperations( $purgeOps );
840 $this->assertGoodStatus( $status,
841 "Quick deletion of source files succeeded ($backendName)." );
843 foreach ( $files as $file ) {
844 $this->assertFalse( $this->backend
->fileExists( array( 'src' => $file ) ),
845 "File $file purged." );
850 * @dataProvider provider_testConcatenate
852 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
853 $this->filesToPrune
[] = $op['dst'];
855 $this->backend
= $this->singleBackend
;
856 $this->tearDownFiles();
857 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
858 $this->tearDownFiles();
860 $this->backend
= $this->multiBackend
;
861 $this->tearDownFiles();
862 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
863 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
864 $this->tearDownFiles();
867 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
868 $backendName = $this->backendClass();
873 foreach ( $srcs as $i => $source ) {
874 $this->prepare( array( 'dir' => dirname( $source ) ) );
876 'op' => 'create', // operation
877 'dst' => $source, // source
878 'content' => $srcsContent[$i]
880 $expContent .= $srcsContent[$i];
882 $status = $this->backend
->doOperations( $ops );
884 $this->assertGoodStatus( $status,
885 "Creation of source files succeeded ($backendName)." );
887 $dest = $params['dst'];
888 if ( $alreadyExists ) {
889 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
890 $this->assertEquals( true, $ok,
891 "Creation of file at $dest succeeded ($backendName)." );
893 $ok = file_put_contents( $dest, '' ) !== false;
894 $this->assertEquals( true, $ok,
895 "Creation of 0-byte file at $dest succeeded ($backendName)." );
898 // Combine the files into one
899 $status = $this->backend
->concatenate( $params );
901 $this->assertGoodStatus( $status,
902 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
903 $this->assertEquals( true, $status->isOK(),
904 "Creation of concat file at $dest succeeded ($backendName)." );
906 $this->assertEquals( false, $status->isOK(),
907 "Creation of concat file at $dest failed ($backendName)." );
911 $this->assertEquals( true, is_file( $dest ),
912 "Dest concat file $dest exists after creation ($backendName)." );
914 $this->assertEquals( true, is_file( $dest ),
915 "Dest concat file $dest exists after failed creation ($backendName)." );
918 $contents = file_get_contents( $dest );
919 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
922 $this->assertEquals( $expContent, $contents,
923 "Concat file at $dest has correct contents ($backendName)." );
925 $this->assertNotEquals( $expContent, $contents,
926 "Concat file at $dest has correct contents ($backendName)." );
930 function provider_testConcatenate() {
933 $rand = mt_rand( 0, 2000000000 ) . time();
934 $dest = wfTempDir() . "/randomfile!$rand.txt";
936 self
::baseStorePath() . '/unittest-cont1/e/file1.txt',
937 self
::baseStorePath() . '/unittest-cont1/e/file2.txt',
938 self
::baseStorePath() . '/unittest-cont1/e/file3.txt',
939 self
::baseStorePath() . '/unittest-cont1/e/file4.txt',
940 self
::baseStorePath() . '/unittest-cont1/e/file5.txt',
941 self
::baseStorePath() . '/unittest-cont1/e/file6.txt',
942 self
::baseStorePath() . '/unittest-cont1/e/file7.txt',
943 self
::baseStorePath() . '/unittest-cont1/e/file8.txt',
944 self
::baseStorePath() . '/unittest-cont1/e/file9.txt',
945 self
::baseStorePath() . '/unittest-cont1/e/file10.txt'
959 $params = array( 'srcs' => $srcs, 'dst' => $dest );
962 $params, // operation
964 $content, // content for each source
965 false, // no dest already exists
970 $params, // operation
972 $content, // content for each source
973 true, // dest already exists
981 * @dataProvider provider_testGetFileStat
983 public function testGetFileStat( $path, $content, $alreadyExists ) {
984 $this->backend
= $this->singleBackend
;
985 $this->tearDownFiles();
986 $this->doTestGetFileStat( $path, $content, $alreadyExists );
987 $this->tearDownFiles();
989 $this->backend
= $this->multiBackend
;
990 $this->tearDownFiles();
991 $this->doTestGetFileStat( $path, $content, $alreadyExists );
992 $this->tearDownFiles();
995 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
996 $backendName = $this->backendClass();
998 if ( $alreadyExists ) {
999 $this->prepare( array( 'dir' => dirname( $path ) ) );
1000 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
1001 $this->assertGoodStatus( $status,
1002 "Creation of file at $path succeeded ($backendName)." );
1004 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1005 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1006 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
1008 $this->assertEquals( strlen( $content ), $size,
1009 "Correct file size of '$path'" );
1010 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1011 "Correct file timestamp of '$path'" );
1013 $size = $stat['size'];
1014 $time = $stat['mtime'];
1015 $this->assertEquals( strlen( $content ), $size,
1016 "Correct file size of '$path'" );
1017 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1018 "Correct file timestamp of '$path'" );
1020 $this->backend
->clearCache( array( $path ) );
1022 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1024 $this->assertEquals( strlen( $content ), $size,
1025 "Correct file size of '$path'" );
1027 $this->backend
->preloadCache( array( $path ) );
1029 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1031 $this->assertEquals( strlen( $content ), $size,
1032 "Correct file size of '$path'" );
1034 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1035 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1036 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
1038 $this->assertFalse( $size, "Correct file size of '$path'" );
1039 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1040 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1044 function provider_testGetFileStat() {
1047 $base = self
::baseStorePath();
1048 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
1049 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
1050 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
1056 * @dataProvider provider_testGetFileContents
1058 public function testGetFileContents( $source, $content ) {
1059 $this->backend
= $this->singleBackend
;
1060 $this->tearDownFiles();
1061 $this->doTestGetFileContents( $source, $content );
1062 $this->tearDownFiles();
1064 $this->backend
= $this->multiBackend
;
1065 $this->tearDownFiles();
1066 $this->doTestGetFileContents( $source, $content );
1067 $this->tearDownFiles();
1070 private function doTestGetFileContents( $source, $content ) {
1071 $backendName = $this->backendClass();
1073 $srcs = (array)$source;
1074 $content = (array)$content;
1075 foreach ( $srcs as $i => $src ) {
1076 $this->prepare( array( 'dir' => dirname( $src ) ) );
1077 $status = $this->backend
->doOperation(
1078 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1079 $this->assertGoodStatus( $status,
1080 "Creation of file at $src succeeded ($backendName)." );
1083 if ( is_array( $source ) ) {
1084 $contents = $this->backend
->getFileContentsMulti( array( 'srcs' => $source ) );
1085 foreach ( $contents as $path => $data ) {
1086 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1087 $this->assertEquals( current( $content ), $data, "Contents of $path is correct ($backendName)." );
1090 $this->assertEquals( $source, array_keys( $contents ), "Contents in right order ($backendName)." );
1091 $this->assertEquals( count( $source ), count( $contents ), "Contents array size correct ($backendName)." );
1093 $data = $this->backend
->getFileContents( array( 'src' => $source ) );
1094 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1095 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1099 function provider_testGetFileContents() {
1102 $base = self
::baseStorePath();
1103 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
1104 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
1106 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1107 "$base/unittest-cont1/e/a/z.txt" ),
1108 array( "contents xx", "contents xy", "contents xz" )
1115 * @dataProvider provider_testGetLocalCopy
1117 public function testGetLocalCopy( $source, $content ) {
1118 $this->backend
= $this->singleBackend
;
1119 $this->tearDownFiles();
1120 $this->doTestGetLocalCopy( $source, $content );
1121 $this->tearDownFiles();
1123 $this->backend
= $this->multiBackend
;
1124 $this->tearDownFiles();
1125 $this->doTestGetLocalCopy( $source, $content );
1126 $this->tearDownFiles();
1129 private function doTestGetLocalCopy( $source, $content ) {
1130 $backendName = $this->backendClass();
1132 $srcs = (array)$source;
1133 $content = (array)$content;
1134 foreach ( $srcs as $i => $src ) {
1135 $this->prepare( array( 'dir' => dirname( $src ) ) );
1136 $status = $this->backend
->doOperation(
1137 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1138 $this->assertGoodStatus( $status,
1139 "Creation of file at $src succeeded ($backendName)." );
1142 if ( is_array( $source ) ) {
1143 $tmpFiles = $this->backend
->getLocalCopyMulti( array( 'srcs' => $source ) );
1144 foreach ( $tmpFiles as $path => $tmpFile ) {
1145 $this->assertNotNull( $tmpFile,
1146 "Creation of local copy of $path succeeded ($backendName)." );
1147 $contents = file_get_contents( $tmpFile->getPath() );
1148 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1149 $this->assertEquals( current( $content ), $contents, "Local copy of $path is correct ($backendName)." );
1152 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local copies in right order ($backendName)." );
1153 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local copies array size correct ($backendName)." );
1155 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
1156 $this->assertNotNull( $tmpFile,
1157 "Creation of local copy of $source succeeded ($backendName)." );
1158 $contents = file_get_contents( $tmpFile->getPath() );
1159 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1160 $this->assertEquals( $content[0], $contents, "Local copy of $source is correct ($backendName)." );
1163 $obj = new stdClass();
1164 $tmpFile->bind( $obj );
1167 function provider_testGetLocalCopy() {
1170 $base = self
::baseStorePath();
1171 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1172 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1173 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1175 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1176 "$base/unittest-cont1/e/a/z.txt" ),
1177 array( "contents xx", "contents xy", "contents xz" )
1184 * @dataProvider provider_testGetLocalReference
1186 public function testGetLocalReference( $source, $content ) {
1187 $this->backend
= $this->singleBackend
;
1188 $this->tearDownFiles();
1189 $this->doTestGetLocalReference( $source, $content );
1190 $this->tearDownFiles();
1192 $this->backend
= $this->multiBackend
;
1193 $this->tearDownFiles();
1194 $this->doTestGetLocalReference( $source, $content );
1195 $this->tearDownFiles();
1198 private function doTestGetLocalReference( $source, $content ) {
1199 $backendName = $this->backendClass();
1201 $srcs = (array)$source;
1202 $content = (array)$content;
1203 foreach ( $srcs as $i => $src ) {
1204 $this->prepare( array( 'dir' => dirname( $src ) ) );
1205 $status = $this->backend
->doOperation(
1206 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1207 $this->assertGoodStatus( $status,
1208 "Creation of file at $src succeeded ($backendName)." );
1211 if ( is_array( $source ) ) {
1212 $tmpFiles = $this->backend
->getLocalReferenceMulti( array( 'srcs' => $source ) );
1213 foreach ( $tmpFiles as $path => $tmpFile ) {
1214 $this->assertNotNull( $tmpFile,
1215 "Creation of local copy of $path succeeded ($backendName)." );
1216 $contents = file_get_contents( $tmpFile->getPath() );
1217 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1218 $this->assertEquals( current( $content ), $contents, "Local ref of $path is correct ($backendName)." );
1221 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local refs in right order ($backendName)." );
1222 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local refs array size correct ($backendName)." );
1224 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
1225 $this->assertNotNull( $tmpFile,
1226 "Creation of local copy of $source succeeded ($backendName)." );
1227 $contents = file_get_contents( $tmpFile->getPath() );
1228 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1229 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1233 function provider_testGetLocalReference() {
1236 $base = self
::baseStorePath();
1237 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1238 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1239 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1241 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1242 "$base/unittest-cont1/e/a/z.txt" ),
1243 array( "contents xx", "contents xy", "contents xz" )
1249 public function testGetLocalCopyAndReference404() {
1250 $this->backend
= $this->singleBackend
;
1251 $this->tearDownFiles();
1252 $this->doTestGetLocalCopyAndReference404();
1253 $this->tearDownFiles();
1255 $this->backend
= $this->multiBackend
;
1256 $this->tearDownFiles();
1257 $this->doTestGetLocalCopyAndReference404();
1258 $this->tearDownFiles();
1261 public function doTestGetLocalCopyAndReference404() {
1262 $backendName = $this->backendClass();
1264 $base = self
::baseStorePath();
1266 $tmpFile = $this->backend
->getLocalCopy( array(
1267 'src' => "$base/unittest-cont1/not-there" ) );
1268 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1270 $tmpFile = $this->backend
->getLocalReference( array(
1271 'src' => "$base/unittest-cont1/not-there" ) );
1272 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1276 * @dataProvider provider_testGetFileHttpUrl
1278 public function testGetFileHttpUrl( $source, $content ) {
1279 $this->backend
= $this->singleBackend
;
1280 $this->tearDownFiles();
1281 $this->doTestGetFileHttpUrl( $source, $content );
1282 $this->tearDownFiles();
1284 $this->backend
= $this->multiBackend
;
1285 $this->tearDownFiles();
1286 $this->doTestGetFileHttpUrl( $source, $content );
1287 $this->tearDownFiles();
1290 private function doTestGetFileHttpUrl( $source, $content ) {
1291 $backendName = $this->backendClass();
1293 $this->prepare( array( 'dir' => dirname( $source ) ) );
1294 $status = $this->backend
->doOperation(
1295 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
1296 $this->assertGoodStatus( $status,
1297 "Creation of file at $source succeeded ($backendName)." );
1299 $url = $this->backend
->getFileHttpUrl( array( 'src' => $source ) );
1301 if ( $url !== null ) { // supported
1302 $data = Http
::request( "GET", $url );
1303 $this->assertEquals( $content, $data,
1304 "HTTP GET of URL has right contents ($backendName)." );
1308 function provider_testGetFileHttpUrl() {
1311 $base = self
::baseStorePath();
1312 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1313 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1314 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1320 * @dataProvider provider_testPrepareAndClean
1322 public function testPrepareAndClean( $path, $isOK ) {
1323 $this->backend
= $this->singleBackend
;
1324 $this->doTestPrepareAndClean( $path, $isOK );
1325 $this->tearDownFiles();
1327 $this->backend
= $this->multiBackend
;
1328 $this->doTestPrepareAndClean( $path, $isOK );
1329 $this->tearDownFiles();
1332 function provider_testPrepareAndClean() {
1333 $base = self
::baseStorePath();
1335 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1336 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1337 # Specific to FS backend with no basePath field set
1338 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1342 private function doTestPrepareAndClean( $path, $isOK ) {
1343 $backendName = $this->backendClass();
1345 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1347 $this->assertGoodStatus( $status,
1348 "Preparing dir $path succeeded without warnings ($backendName)." );
1349 $this->assertEquals( true, $status->isOK(),
1350 "Preparing dir $path succeeded ($backendName)." );
1352 $this->assertEquals( false, $status->isOK(),
1353 "Preparing dir $path failed ($backendName)." );
1356 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1358 $this->assertGoodStatus( $status,
1359 "Cleaning dir $path succeeded without warnings ($backendName)." );
1360 $this->assertEquals( true, $status->isOK(),
1361 "Cleaning dir $path succeeded ($backendName)." );
1363 $this->assertEquals( false, $status->isOK(),
1364 "Cleaning dir $path failed ($backendName)." );
1368 public function testRecursiveClean() {
1369 $this->backend
= $this->singleBackend
;
1370 $this->doTestRecursiveClean();
1371 $this->tearDownFiles();
1373 $this->backend
= $this->multiBackend
;
1374 $this->doTestRecursiveClean();
1375 $this->tearDownFiles();
1378 private function doTestRecursiveClean() {
1379 $backendName = $this->backendClass();
1381 $base = self
::baseStorePath();
1383 "$base/unittest-cont1",
1384 "$base/unittest-cont1/e",
1385 "$base/unittest-cont1/e/a",
1386 "$base/unittest-cont1/e/a/b",
1387 "$base/unittest-cont1/e/a/b/c",
1388 "$base/unittest-cont1/e/a/b/c/d0",
1389 "$base/unittest-cont1/e/a/b/c/d1",
1390 "$base/unittest-cont1/e/a/b/c/d2",
1391 "$base/unittest-cont1/e/a/b/c/d0/1",
1392 "$base/unittest-cont1/e/a/b/c/d0/2",
1393 "$base/unittest-cont1/e/a/b/c/d1/3",
1394 "$base/unittest-cont1/e/a/b/c/d1/4",
1395 "$base/unittest-cont1/e/a/b/c/d2/5",
1396 "$base/unittest-cont1/e/a/b/c/d2/6"
1398 foreach ( $dirs as $dir ) {
1399 $status = $this->prepare( array( 'dir' => $dir ) );
1400 $this->assertGoodStatus( $status,
1401 "Preparing dir $dir succeeded without warnings ($backendName)." );
1404 if ( $this->backend
instanceof FSFileBackend
) {
1405 foreach ( $dirs as $dir ) {
1406 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1407 "Dir $dir exists ($backendName)." );
1411 $status = $this->backend
->clean(
1412 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1413 $this->assertGoodStatus( $status,
1414 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1416 foreach ( $dirs as $dir ) {
1417 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1418 "Dir $dir no longer exists ($backendName)." );
1422 // @TODO: testSecure
1424 public function testDoOperations() {
1425 $this->backend
= $this->singleBackend
;
1426 $this->tearDownFiles();
1427 $this->doTestDoOperations();
1428 $this->tearDownFiles();
1430 $this->backend
= $this->multiBackend
;
1431 $this->tearDownFiles();
1432 $this->doTestDoOperations();
1433 $this->tearDownFiles();
1436 private function doTestDoOperations() {
1437 $base = self
::baseStorePath();
1439 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1440 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1441 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1442 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1443 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1444 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1445 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1447 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1448 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1449 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1450 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1451 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1452 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1453 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1455 $status = $this->backend
->doOperations( array(
1456 array( 'op' => 'describe', 'src' => $fileA,
1457 'headers' => array( 'X-Content-Length' => '91.3' ), 'disposition' => 'inline' ),
1458 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1459 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1460 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1461 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1462 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1463 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1464 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1465 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1466 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1467 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1468 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1469 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1470 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1471 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1472 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1473 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1474 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1476 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1478 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1480 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1482 array( 'op' => 'null' ),
1486 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1487 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1488 $this->assertEquals( 14, count( $status->success
),
1489 "Operation batch has correct success array" );
1491 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1492 "File does not exist at $fileA" );
1493 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1494 "File does not exist at $fileB" );
1495 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1496 "File does not exist at $fileD" );
1498 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1499 "File exists at $fileC" );
1500 $this->assertEquals( $fileBContents,
1501 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1502 "Correct file contents of $fileC" );
1503 $this->assertEquals( strlen( $fileBContents ),
1504 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1505 "Correct file size of $fileC" );
1506 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1507 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1508 "Correct file SHA-1 of $fileC" );
1511 public function testDoOperationsPipeline() {
1512 $this->backend
= $this->singleBackend
;
1513 $this->tearDownFiles();
1514 $this->doTestDoOperationsPipeline();
1515 $this->tearDownFiles();
1517 $this->backend
= $this->multiBackend
;
1518 $this->tearDownFiles();
1519 $this->doTestDoOperationsPipeline();
1520 $this->tearDownFiles();
1523 // concurrency orientated
1524 private function doTestDoOperationsPipeline() {
1525 $base = self
::baseStorePath();
1527 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1528 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1529 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1531 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1532 file_put_contents( $tmpNameA, $fileAContents );
1533 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1534 file_put_contents( $tmpNameB, $fileBContents );
1535 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1536 file_put_contents( $tmpNameC, $fileCContents );
1538 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1539 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1540 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1542 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1543 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1544 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1545 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1547 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1548 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1549 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1550 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1551 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1553 $status = $this->backend
->doOperations( array(
1554 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1555 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1556 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1557 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1558 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1559 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1560 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1561 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1562 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1563 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1564 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1565 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1566 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1567 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1568 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1569 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1570 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1571 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1572 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1573 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1575 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1577 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1579 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1581 array( 'op' => 'null' ),
1585 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1586 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1587 $this->assertEquals( 16, count( $status->success
),
1588 "Operation batch has correct success array" );
1590 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1591 "File does not exist at $fileA" );
1592 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1593 "File does not exist at $fileB" );
1594 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1595 "File does not exist at $fileD" );
1597 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1598 "File exists at $fileC" );
1599 $this->assertEquals( $fileBContents,
1600 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1601 "Correct file contents of $fileC" );
1602 $this->assertEquals( strlen( $fileBContents ),
1603 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1604 "Correct file size of $fileC" );
1605 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1606 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1607 "Correct file SHA-1 of $fileC" );
1610 public function testDoOperationsFailing() {
1611 $this->backend
= $this->singleBackend
;
1612 $this->tearDownFiles();
1613 $this->doTestDoOperationsFailing();
1614 $this->tearDownFiles();
1616 $this->backend
= $this->multiBackend
;
1617 $this->tearDownFiles();
1618 $this->doTestDoOperationsFailing();
1619 $this->tearDownFiles();
1622 private function doTestDoOperationsFailing() {
1623 $base = self
::baseStorePath();
1625 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1626 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1627 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1628 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1629 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1630 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1631 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1633 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1634 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1635 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1636 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1637 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1638 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1640 $status = $this->backend
->doOperations( array(
1641 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1642 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1643 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1644 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1645 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1646 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1647 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1648 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1649 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1650 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1651 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1652 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1653 array( 'op' => 'delete', 'src' => $fileD ),
1654 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1655 array( 'op' => 'null' ),
1657 ), array( 'force' => 1 ) );
1659 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1660 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1661 $this->assertEquals( 8, count( $status->success
),
1662 "Operation batch has correct success array" );
1664 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1665 "File does not exist at $fileB" );
1666 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1667 "File does not exist at $fileD" );
1669 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1670 "File does not exist at $fileA" );
1671 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1672 "File exists at $fileC" );
1673 $this->assertEquals( $fileBContents,
1674 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1675 "Correct file contents of $fileA" );
1676 $this->assertEquals( strlen( $fileBContents ),
1677 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1678 "Correct file size of $fileA" );
1679 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1680 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1681 "Correct file SHA-1 of $fileA" );
1684 public function testGetFileList() {
1685 $this->backend
= $this->singleBackend
;
1686 $this->tearDownFiles();
1687 $this->doTestGetFileList();
1688 $this->tearDownFiles();
1690 $this->backend
= $this->multiBackend
;
1691 $this->tearDownFiles();
1692 $this->doTestGetFileList();
1693 $this->tearDownFiles();
1696 private function doTestGetFileList() {
1697 $backendName = $this->backendClass();
1698 $base = self
::baseStorePath();
1700 // Should have no errors
1701 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1704 "$base/unittest-cont1/e/test1.txt",
1705 "$base/unittest-cont1/e/test2.txt",
1706 "$base/unittest-cont1/e/test3.txt",
1707 "$base/unittest-cont1/e/subdir1/test1.txt",
1708 "$base/unittest-cont1/e/subdir1/test2.txt",
1709 "$base/unittest-cont1/e/subdir2/test3.txt",
1710 "$base/unittest-cont1/e/subdir2/test4.txt",
1711 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1712 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1713 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1714 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1715 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1716 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1717 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1722 foreach ( $files as $file ) {
1723 $this->prepare( array( 'dir' => dirname( $file ) ) );
1724 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1726 $status = $this->backend
->doQuickOperations( $ops );
1727 $this->assertGoodStatus( $status,
1728 "Creation of files succeeded ($backendName)." );
1729 $this->assertEquals( true, $status->isOK(),
1730 "Creation of files succeeded with OK status ($backendName)." );
1737 "e/subdir1/test1.txt",
1738 "e/subdir1/test2.txt",
1739 "e/subdir2/test3.txt",
1740 "e/subdir2/test4.txt",
1741 "e/subdir2/subdir/test1.txt",
1742 "e/subdir2/subdir/test2.txt",
1743 "e/subdir2/subdir/test3.txt",
1744 "e/subdir2/subdir/test4.txt",
1745 "e/subdir2/subdir/test5.txt",
1746 "e/subdir2/subdir/sub/test0.txt",
1747 "e/subdir2/subdir/sub/120-px-file.txt",
1751 // Actual listing (no trailing slash)
1753 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1754 foreach ( $iter as $file ) {
1759 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1761 // Actual listing (with trailing slash)
1763 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1764 foreach ( $iter as $file ) {
1769 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1779 "sub/120-px-file.txt",
1783 // Actual listing (no trailing slash)
1785 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1786 foreach ( $iter as $file ) {
1791 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1793 // Actual listing (with trailing slash)
1795 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1796 foreach ( $iter as $file ) {
1801 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1803 // Actual listing (using iterator second time)
1805 foreach ( $iter as $file ) {
1810 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1812 // Expected listing (top files only)
1822 // Actual listing (top files only)
1824 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1825 foreach ( $iter as $file ) {
1830 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1832 foreach ( $files as $file ) { // clean up
1833 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1836 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1837 foreach ( $iter as $iter ) {} // no errors
1840 public function testGetDirectoryList() {
1841 $this->backend
= $this->singleBackend
;
1842 $this->tearDownFiles();
1843 $this->doTestGetDirectoryList();
1844 $this->tearDownFiles();
1846 $this->backend
= $this->multiBackend
;
1847 $this->tearDownFiles();
1848 $this->doTestGetDirectoryList();
1849 $this->tearDownFiles();
1852 private function doTestGetDirectoryList() {
1853 $backendName = $this->backendClass();
1855 $base = self
::baseStorePath();
1857 "$base/unittest-cont1/e/test1.txt",
1858 "$base/unittest-cont1/e/test2.txt",
1859 "$base/unittest-cont1/e/test3.txt",
1860 "$base/unittest-cont1/e/subdir1/test1.txt",
1861 "$base/unittest-cont1/e/subdir1/test2.txt",
1862 "$base/unittest-cont1/e/subdir2/test3.txt",
1863 "$base/unittest-cont1/e/subdir2/test4.txt",
1864 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1865 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1866 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1867 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1868 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1869 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1870 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1875 foreach ( $files as $file ) {
1876 $this->prepare( array( 'dir' => dirname( $file ) ) );
1877 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1879 $status = $this->backend
->doQuickOperations( $ops );
1880 $this->assertGoodStatus( $status,
1881 "Creation of files succeeded ($backendName)." );
1882 $this->assertEquals( true, $status->isOK(),
1883 "Creation of files succeeded with OK status ($backendName)." );
1885 $this->assertEquals( true,
1886 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1887 "Directory exists in ($backendName)." );
1888 $this->assertEquals( true,
1889 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1890 "Directory exists in ($backendName)." );
1891 $this->assertEquals( false,
1892 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1893 "Directory does not exists in ($backendName)." );
1901 // Actual listing (no trailing slash)
1903 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1904 foreach ( $iter as $file ) {
1909 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1920 // Actual listing (no trailing slash)
1922 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1923 foreach ( $iter as $file ) {
1928 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1930 // Actual listing (with trailing slash)
1932 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1933 foreach ( $iter as $file ) {
1938 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1946 // Actual listing (no trailing slash)
1948 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1949 foreach ( $iter as $file ) {
1954 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1956 // Actual listing (with trailing slash)
1958 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1959 foreach ( $iter as $file ) {
1964 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1966 // Actual listing (using iterator second time)
1968 foreach ( $iter as $file ) {
1973 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1975 // Expected listing (recursive)
1985 "e/subdir4/subdir/sub",
1989 // Actual listing (recursive)
1991 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1992 foreach ( $iter as $file ) {
1997 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1999 // Expected listing (recursive)
2006 // Actual listing (recursive)
2008 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
2009 foreach ( $iter as $file ) {
2014 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2016 // Actual listing (recursive, second time)
2018 foreach ( $iter as $file ) {
2023 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2025 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) );
2026 $items = is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2027 $this->assertEquals( array(), $items, "Directory listing is empty." );
2029 foreach ( $files as $file ) { // clean up
2030 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
2033 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
2034 foreach ( $iter as $file ) {} // no errors
2035 $items = is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2036 $this->assertEquals( array(), $items, "Directory listing is empty." );
2038 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/not/exists" ) );
2039 $items = is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2040 $this->assertEquals( array(), $items, "Directory listing is empty." );
2043 public function testLockCalls() {
2044 $this->backend
= $this->singleBackend
;
2045 $this->doTestLockCalls();
2048 private function doTestLockCalls() {
2049 $backendName = $this->backendClass();
2056 "subdir1", // duplicate
2057 "subdir1/test1.txt",
2058 "subdir1/test2.txt",
2060 "subdir2", // duplicate
2061 "subdir2/test3.txt",
2062 "subdir2/test4.txt",
2064 "subdir2/subdir/test1.txt",
2065 "subdir2/subdir/test2.txt",
2066 "subdir2/subdir/test3.txt",
2067 "subdir2/subdir/test4.txt",
2068 "subdir2/subdir/test5.txt",
2069 "subdir2/subdir/sub",
2070 "subdir2/subdir/sub/test0.txt",
2071 "subdir2/subdir/sub/120-px-file.txt",
2074 for ( $i=0; $i<25; $i++
) {
2075 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2076 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2077 "Locking of files succeeded ($backendName) ($i)." );
2078 $this->assertEquals( true, $status->isOK(),
2079 "Locking of files succeeded with OK status ($backendName) ($i)." );
2081 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2082 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2083 "Locking of files succeeded ($backendName) ($i)." );
2084 $this->assertEquals( true, $status->isOK(),
2085 "Locking of files succeeded with OK status ($backendName) ($i)." );
2087 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2088 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2089 "Locking of files succeeded ($backendName) ($i)." );
2090 $this->assertEquals( true, $status->isOK(),
2091 "Locking of files succeeded with OK status ($backendName) ($i)." );
2093 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2094 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2095 "Locking of files succeeded ($backendName). ($i)" );
2096 $this->assertEquals( true, $status->isOK(),
2097 "Locking of files succeeded with OK status ($backendName) ($i)." );
2099 ## Flip the acquire/release ordering around ##
2101 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2102 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2103 "Locking of files succeeded ($backendName) ($i)." );
2104 $this->assertEquals( true, $status->isOK(),
2105 "Locking of files succeeded with OK status ($backendName) ($i)." );
2107 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2108 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2109 "Locking of files succeeded ($backendName) ($i)." );
2110 $this->assertEquals( true, $status->isOK(),
2111 "Locking of files succeeded with OK status ($backendName) ($i)." );
2113 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2114 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2115 "Locking of files succeeded ($backendName). ($i)" );
2116 $this->assertEquals( true, $status->isOK(),
2117 "Locking of files succeeded with OK status ($backendName) ($i)." );
2119 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2120 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2121 "Locking of files succeeded ($backendName) ($i)." );
2122 $this->assertEquals( true, $status->isOK(),
2123 "Locking of files succeeded with OK status ($backendName) ($i)." );
2126 $status = Status
::newGood();
2127 $sl = $this->backend
->getScopedFileLocks( $paths, LockManager
::LOCK_EX
, $status );
2128 $this->assertType( 'ScopedLock', $sl,
2129 "Scoped locking of files succeeded ($backendName)." );
2130 $this->assertEquals( array(), $status->errors
,
2131 "Scoped locking of files succeeded ($backendName)." );
2132 $this->assertEquals( true, $status->isOK(),
2133 "Scoped locking of files succeeded with OK status ($backendName)." );
2135 ScopedLock
::release( $sl );
2136 $this->assertEquals( null, $sl,
2137 "Scoped unlocking of files succeeded ($backendName)." );
2138 $this->assertEquals( array(), $status->errors
,
2139 "Scoped unlocking of files succeeded ($backendName)." );
2140 $this->assertEquals( true, $status->isOK(),
2141 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2144 // test helper wrapper for backend prepare() function
2145 private function prepare( array $params ) {
2146 return $this->backend
->prepare( $params );
2149 // test helper wrapper for backend prepare() function
2150 private function create( array $params ) {
2151 $params['op'] = 'create';
2152 return $this->backend
->doQuickOperations( array( $params ) );
2155 function tearDownFiles() {
2156 foreach ( $this->filesToPrune
as $file ) {
2159 $containers = array( 'unittest-cont1', 'unittest-cont2' );
2160 foreach ( $containers as $container ) {
2161 $this->deleteFiles( $container );
2163 $this->filesToPrune
= array();
2166 private function deleteFiles( $container ) {
2167 $base = self
::baseStorePath();
2168 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
2170 foreach ( $iter as $file ) {
2171 $this->backend
->quickDelete( array( 'src' => "$base/$container/$file" ) );
2173 // free the directory, to avoid Permission denied under windows on rmdir
2176 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
2179 function assertBackendPathsConsistent( array $paths ) {
2180 if ( $this->backend
instanceof FileBackendMultiWrite
) {
2181 $status = $this->backend
->consistencyCheck( $paths );
2182 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2186 function assertGoodStatus( $status, $msg ) {
2187 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );