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 $uniqueId = time() . '-' . mt_rand();
17 $tmpPrefix = wfTempDir() . '/filebackend-unittest-' . $uniqueId;
18 if ( $this->getCliArg( 'use-filebackend=' ) ) {
19 if ( self
::$backendToUse ) {
20 $this->singleBackend
= self
::$backendToUse;
22 $name = $this->getCliArg( 'use-filebackend=' );
24 foreach ( $wgFileBackends as $conf ) {
25 if ( $conf['name'] == $name ) {
30 $useConfig['name'] = 'localtesting'; // swap name
31 $useConfig['shardViaHashLevels'] = array( // test sharding
32 'unittest-cont1' => array( 'levels' => 1, 'base' => 16, 'repeat' => 1 )
34 $class = $useConfig['class'];
35 self
::$backendToUse = new $class( $useConfig );
36 $this->singleBackend
= self
::$backendToUse;
39 $this->singleBackend
= new FSFileBackend( array(
40 'name' => 'localtesting',
41 'lockManager' => 'fsLockManager',
42 #'parallelize' => 'implicit',
43 'wikiId' => wfWikiID() . $uniqueId,
44 'containerPaths' => array(
45 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
46 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
49 $this->multiBackend
= new FileBackendMultiWrite( array(
50 'name' => 'localtesting',
51 'lockManager' => 'fsLockManager',
52 'parallelize' => 'implicit',
53 'wikiId' => wfWikiId() . $uniqueId,
56 'name' => 'localmultitesting1',
57 'class' => 'FSFileBackend',
58 'lockManager' => 'nullLockManager',
59 'containerPaths' => array(
60 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
61 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
62 'isMultiMaster' => false
65 'name' => 'localmultitesting2',
66 'class' => 'FSFileBackend',
67 'lockManager' => 'nullLockManager',
68 'containerPaths' => array(
69 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
70 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
71 'isMultiMaster' => true
75 $this->filesToPrune
= array();
78 private static function baseStorePath() {
79 return 'mwstore://localtesting';
82 private function backendClass() {
83 return get_class( $this->backend
);
87 * @dataProvider provider_testIsStoragePath
89 public function testIsStoragePath( $path, $isStorePath ) {
90 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
91 "FileBackend::isStoragePath on path '$path'" );
94 public static function provider_testIsStoragePath() {
96 array( 'mwstore://', true ),
97 array( 'mwstore://backend', true ),
98 array( 'mwstore://backend/container', true ),
99 array( 'mwstore://backend/container/', true ),
100 array( 'mwstore://backend/container/path', true ),
101 array( 'mwstore://backend//container/', true ),
102 array( 'mwstore://backend//container//', true ),
103 array( 'mwstore://backend//container//path', true ),
104 array( 'mwstore:///', true ),
105 array( 'mwstore:/', false ),
106 array( 'mwstore:', false ),
111 * @dataProvider provider_testSplitStoragePath
113 public function testSplitStoragePath( $path, $res ) {
114 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
115 "FileBackend::splitStoragePath on path '$path'" );
118 public static function provider_testSplitStoragePath() {
120 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
121 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
122 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
123 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
124 array( 'mwstore://backend//container/path', array( null, null, null ) ),
125 array( 'mwstore://backend//container//path', array( null, null, null ) ),
126 array( 'mwstore://', array( null, null, null ) ),
127 array( 'mwstore://backend', array( null, null, null ) ),
128 array( 'mwstore:///', array( null, null, null ) ),
129 array( 'mwstore:/', array( null, null, null ) ),
130 array( 'mwstore:', array( null, null, null ) )
135 * @dataProvider provider_normalizeStoragePath
137 public function testNormalizeStoragePath( $path, $res ) {
138 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
139 "FileBackend::normalizeStoragePath on path '$path'" );
142 public static function provider_normalizeStoragePath() {
144 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
145 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
146 array( 'mwstore://backend/container/path', 'mwstore://backend/container/path' ),
147 array( 'mwstore://backend/container//path', 'mwstore://backend/container/path' ),
148 array( 'mwstore://backend/container///path', 'mwstore://backend/container/path' ),
149 array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj' ),
150 array( 'mwstore://', null ),
151 array( 'mwstore://backend', null ),
152 array( 'mwstore://backend//container/path', null ),
153 array( 'mwstore://backend//container//path', null ),
154 array( 'mwstore:///', null ),
155 array( 'mwstore:/', null ),
156 array( 'mwstore:', null ),
161 * @dataProvider provider_testParentStoragePath
163 public function testParentStoragePath( $path, $res ) {
164 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
165 "FileBackend::parentStoragePath on path '$path'" );
168 public static function provider_testParentStoragePath() {
170 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
171 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
172 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
173 array( 'mwstore://backend/container', null ),
174 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
175 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
176 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
177 array( 'mwstore://backend/container/', null ),
182 * @dataProvider provider_testExtensionFromPath
184 public function testExtensionFromPath( $path, $res ) {
185 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
186 "FileBackend::extensionFromPath on path '$path'" );
189 public static function provider_testExtensionFromPath() {
191 array( 'mwstore://backend/container/path.txt', 'txt' ),
192 array( 'mwstore://backend/container/path.svg.png', 'png' ),
193 array( 'mwstore://backend/container/path', '' ),
194 array( 'mwstore://backend/container/path.', '' ),
199 * @dataProvider provider_testStore
201 public function testStore( $op ) {
202 $this->filesToPrune
[] = $op['src'];
204 $this->backend
= $this->singleBackend
;
205 $this->tearDownFiles();
206 $this->doTestStore( $op );
207 $this->tearDownFiles();
209 $this->backend
= $this->multiBackend
;
210 $this->tearDownFiles();
211 $this->doTestStore( $op );
212 $this->filesToPrune
[] = $op['src']; # avoid file leaking
213 $this->tearDownFiles();
216 private function doTestStore( $op ) {
217 $backendName = $this->backendClass();
219 $source = $op['src'];
221 $this->prepare( array( 'dir' => dirname( $dest ) ) );
223 file_put_contents( $source, "Unit test file" );
225 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
226 $this->backend
->store( $op );
229 $status = $this->backend
->doOperation( $op );
231 $this->assertGoodStatus( $status,
232 "Store from $source to $dest succeeded without warnings ($backendName)." );
233 $this->assertEquals( true, $status->isOK(),
234 "Store from $source to $dest succeeded ($backendName)." );
235 $this->assertEquals( array( 0 => true ), $status->success
,
236 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
237 $this->assertEquals( true, file_exists( $source ),
238 "Source file $source still exists ($backendName)." );
239 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
240 "Destination file $dest exists ($backendName)." );
242 $this->assertEquals( filesize( $source ),
243 $this->backend
->getFileSize( array( 'src' => $dest ) ),
244 "Destination file $dest has correct size ($backendName)." );
246 $props1 = FSFile
::getPropsFromPath( $source );
247 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
248 $this->assertEquals( $props1, $props2,
249 "Source and destination have the same props ($backendName)." );
251 $this->assertBackendPathsConsistent( array( $dest ) );
254 public static function provider_testStore() {
257 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
258 $toPath = self
::baseStorePath() . '/unittest-cont1/e/fun/obj1.txt';
259 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
267 $op2['overwrite'] = true;
275 $op2['overwriteSame'] = true;
286 * @dataProvider provider_testCopy
288 public function testCopy( $op ) {
289 $this->backend
= $this->singleBackend
;
290 $this->tearDownFiles();
291 $this->doTestCopy( $op );
292 $this->tearDownFiles();
294 $this->backend
= $this->multiBackend
;
295 $this->tearDownFiles();
296 $this->doTestCopy( $op );
297 $this->tearDownFiles();
300 private function doTestCopy( $op ) {
301 $backendName = $this->backendClass();
303 $source = $op['src'];
305 $this->prepare( array( 'dir' => dirname( $source ) ) );
306 $this->prepare( array( 'dir' => dirname( $dest ) ) );
308 if ( isset( $op['ignoreMissingSource'] ) ) {
309 $status = $this->backend
->doOperation( $op );
310 $this->assertGoodStatus( $status,
311 "Move from $source to $dest succeeded without warnings ($backendName)." );
312 $this->assertEquals( array( 0 => true ), $status->success
,
313 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
314 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
315 "Source file $source does not exist ($backendName)." );
316 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $dest ) ),
317 "Destination file $dest does not exist ($backendName)." );
321 $status = $this->backend
->doOperation(
322 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
323 $this->assertGoodStatus( $status,
324 "Creation of file at $source succeeded ($backendName)." );
326 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
327 $this->backend
->copy( $op );
330 $status = $this->backend
->doOperation( $op );
332 $this->assertGoodStatus( $status,
333 "Copy from $source to $dest succeeded without warnings ($backendName)." );
334 $this->assertEquals( true, $status->isOK(),
335 "Copy from $source to $dest succeeded ($backendName)." );
336 $this->assertEquals( array( 0 => true ), $status->success
,
337 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
338 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
339 "Source file $source still exists ($backendName)." );
340 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
341 "Destination file $dest exists after copy ($backendName)." );
344 $this->backend
->getFileSize( array( 'src' => $source ) ),
345 $this->backend
->getFileSize( array( 'src' => $dest ) ),
346 "Destination file $dest has correct size ($backendName)." );
348 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
349 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
350 $this->assertEquals( $props1, $props2,
351 "Source and destination have the same props ($backendName)." );
353 $this->assertBackendPathsConsistent( array( $source, $dest ) );
356 public static function provider_testCopy() {
359 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
360 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
362 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
370 $op2['overwrite'] = true;
378 $op2['overwriteSame'] = true;
386 $op2['ignoreMissingSource'] = true;
394 $op2['ignoreMissingSource'] = true;
397 self
::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
405 * @dataProvider provider_testMove
407 public function testMove( $op ) {
408 $this->backend
= $this->singleBackend
;
409 $this->tearDownFiles();
410 $this->doTestMove( $op );
411 $this->tearDownFiles();
413 $this->backend
= $this->multiBackend
;
414 $this->tearDownFiles();
415 $this->doTestMove( $op );
416 $this->tearDownFiles();
419 private function doTestMove( $op ) {
420 $backendName = $this->backendClass();
422 $source = $op['src'];
424 $this->prepare( array( 'dir' => dirname( $source ) ) );
425 $this->prepare( array( 'dir' => dirname( $dest ) ) );
427 if ( isset( $op['ignoreMissingSource'] ) ) {
428 $status = $this->backend
->doOperation( $op );
429 $this->assertGoodStatus( $status,
430 "Move from $source to $dest succeeded without warnings ($backendName)." );
431 $this->assertEquals( array( 0 => true ), $status->success
,
432 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
433 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
434 "Source file $source does not exist ($backendName)." );
435 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $dest ) ),
436 "Destination file $dest does not exist ($backendName)." );
440 $status = $this->backend
->doOperation(
441 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
442 $this->assertGoodStatus( $status,
443 "Creation of file at $source succeeded ($backendName)." );
445 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
446 $this->backend
->copy( $op );
449 $status = $this->backend
->doOperation( $op );
450 $this->assertGoodStatus( $status,
451 "Move from $source to $dest succeeded without warnings ($backendName)." );
452 $this->assertEquals( true, $status->isOK(),
453 "Move from $source to $dest succeeded ($backendName)." );
454 $this->assertEquals( array( 0 => true ), $status->success
,
455 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
456 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
457 "Source file $source does not still exists ($backendName)." );
458 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
459 "Destination file $dest exists after move ($backendName)." );
461 $this->assertNotEquals(
462 $this->backend
->getFileSize( array( 'src' => $source ) ),
463 $this->backend
->getFileSize( array( 'src' => $dest ) ),
464 "Destination file $dest has correct size ($backendName)." );
466 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
467 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
468 $this->assertEquals( false, $props1['fileExists'],
469 "Source file does not exist accourding to props ($backendName)." );
470 $this->assertEquals( true, $props2['fileExists'],
471 "Destination file exists accourding to props ($backendName)." );
473 $this->assertBackendPathsConsistent( array( $source, $dest ) );
476 public static function provider_testMove() {
479 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
480 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
482 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
490 $op2['overwrite'] = true;
498 $op2['overwriteSame'] = true;
506 $op2['ignoreMissingSource'] = true;
514 $op2['ignoreMissingSource'] = true;
517 self
::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
525 * @dataProvider provider_testDelete
527 public function testDelete( $op, $withSource, $okStatus ) {
528 $this->backend
= $this->singleBackend
;
529 $this->tearDownFiles();
530 $this->doTestDelete( $op, $withSource, $okStatus );
531 $this->tearDownFiles();
533 $this->backend
= $this->multiBackend
;
534 $this->tearDownFiles();
535 $this->doTestDelete( $op, $withSource, $okStatus );
536 $this->tearDownFiles();
539 private function doTestDelete( $op, $withSource, $okStatus ) {
540 $backendName = $this->backendClass();
542 $source = $op['src'];
543 $this->prepare( array( 'dir' => dirname( $source ) ) );
546 $status = $this->backend
->doOperation(
547 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
548 $this->assertGoodStatus( $status,
549 "Creation of file at $source succeeded ($backendName)." );
552 $status = $this->backend
->doOperation( $op );
554 $this->assertGoodStatus( $status,
555 "Deletion of file at $source succeeded without warnings ($backendName)." );
556 $this->assertEquals( true, $status->isOK(),
557 "Deletion of file at $source succeeded ($backendName)." );
558 $this->assertEquals( array( 0 => true ), $status->success
,
559 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
561 $this->assertEquals( false, $status->isOK(),
562 "Deletion of file at $source failed ($backendName)." );
565 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
566 "Source file $source does not exist after move ($backendName)." );
569 $this->backend
->getFileSize( array( 'src' => $source ) ),
570 "Source file $source has correct size (false) ($backendName)." );
572 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
573 $this->assertFalse( $props1['fileExists'],
574 "Source file $source does not exist according to props ($backendName)." );
576 $this->assertBackendPathsConsistent( array( $source ) );
579 public static function provider_testDelete() {
582 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
584 $op = array( 'op' => 'delete', 'src' => $source );
593 false, // without source
597 $op['ignoreMissingSource'] = true;
600 false, // without source
604 $op['ignoreMissingSource'] = true;
605 $op['src'] = self
::baseStorePath() . '/unittest-cont-bad/e/file.txt';
608 false, // without source
616 * @dataProvider provider_testDescribe
618 public function testDescribe( $op, $withSource, $okStatus ) {
619 $this->backend
= $this->singleBackend
;
620 $this->tearDownFiles();
621 $this->doTestDescribe( $op, $withSource, $okStatus );
622 $this->tearDownFiles();
624 $this->backend
= $this->multiBackend
;
625 $this->tearDownFiles();
626 $this->doTestDescribe( $op, $withSource, $okStatus );
627 $this->tearDownFiles();
630 private function doTestDescribe( $op, $withSource, $okStatus ) {
631 $backendName = $this->backendClass();
633 $source = $op['src'];
634 $this->prepare( array( 'dir' => dirname( $source ) ) );
637 $status = $this->backend
->doOperation(
638 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
639 $this->assertGoodStatus( $status,
640 "Creation of file at $source succeeded ($backendName)." );
643 $status = $this->backend
->doOperation( $op );
645 $this->assertGoodStatus( $status,
646 "Describe of file at $source succeeded without warnings ($backendName)." );
647 $this->assertEquals( true, $status->isOK(),
648 "Describe of file at $source succeeded ($backendName)." );
649 $this->assertEquals( array( 0 => true ), $status->success
,
650 "Describe of file at $source has proper 'success' field in Status ($backendName)." );
652 $this->assertEquals( false, $status->isOK(),
653 "Describe of file at $source failed ($backendName)." );
656 $this->assertBackendPathsConsistent( array( $source ) );
659 public static function provider_testDescribe() {
662 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
664 $op = array( 'op' => 'describe', 'src' => $source,
665 'headers' => array( 'X-Content-Length' => '91.3', 'Content-Old-Header' => '' ),
666 'disposition' => 'inline' );
675 false, // without source
683 * @dataProvider provider_testCreate
685 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
686 $this->backend
= $this->singleBackend
;
687 $this->tearDownFiles();
688 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
689 $this->tearDownFiles();
691 $this->backend
= $this->multiBackend
;
692 $this->tearDownFiles();
693 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
694 $this->tearDownFiles();
697 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
698 $backendName = $this->backendClass();
701 $this->prepare( array( 'dir' => dirname( $dest ) ) );
703 $oldText = 'blah...blah...waahwaah';
704 if ( $alreadyExists ) {
705 $status = $this->backend
->doOperation(
706 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
707 $this->assertGoodStatus( $status,
708 "Creation of file at $dest succeeded ($backendName)." );
711 $status = $this->backend
->doOperation( $op );
713 $this->assertGoodStatus( $status,
714 "Creation of file at $dest succeeded without warnings ($backendName)." );
715 $this->assertEquals( true, $status->isOK(),
716 "Creation of file at $dest succeeded ($backendName)." );
717 $this->assertEquals( array( 0 => true ), $status->success
,
718 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
720 $this->assertEquals( false, $status->isOK(),
721 "Creation of file at $dest failed ($backendName)." );
724 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
725 "Destination file $dest exists after creation ($backendName)." );
727 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
728 $this->assertEquals( true, $props1['fileExists'],
729 "Destination file $dest exists according to props ($backendName)." );
730 if ( $okStatus ) { // file content is what we saved
731 $this->assertEquals( $newSize, $props1['size'],
732 "Destination file $dest has expected size according to props ($backendName)." );
733 $this->assertEquals( $newSize,
734 $this->backend
->getFileSize( array( 'src' => $dest ) ),
735 "Destination file $dest has correct size ($backendName)." );
736 } else { // file content is some other previous text
737 $this->assertEquals( strlen( $oldText ), $props1['size'],
738 "Destination file $dest has original size according to props ($backendName)." );
739 $this->assertEquals( strlen( $oldText ),
740 $this->backend
->getFileSize( array( 'src' => $dest ) ),
741 "Destination file $dest has original size according to props ($backendName)." );
744 $this->assertBackendPathsConsistent( array( $dest ) );
748 * @dataProvider provider_testCreate
750 public static function provider_testCreate() {
753 $dest = self
::baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
755 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
758 false, // no dest already exists
760 strlen( $op['content'] )
764 $op2['content'] = "\n";
767 false, // no dest already exists
769 strlen( $op2['content'] )
773 $op2['content'] = "fsf\n waf 3kt";
776 true, // dest already exists
778 strlen( $op2['content'] )
782 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
783 $op2['overwrite'] = true;
786 true, // dest already exists
788 strlen( $op2['content'] )
792 $op2['content'] = "39qjmg3-qg";
793 $op2['overwriteSame'] = true;
796 true, // dest already exists
798 strlen( $op2['content'] )
804 public function testDoQuickOperations() {
805 $this->backend
= $this->singleBackend
;
806 $this->doTestDoQuickOperations();
807 $this->tearDownFiles();
809 $this->backend
= $this->multiBackend
;
810 $this->doTestDoQuickOperations();
811 $this->tearDownFiles();
814 private function doTestDoQuickOperations() {
815 $backendName = $this->backendClass();
817 $base = self
::baseStorePath();
819 "$base/unittest-cont1/e/fileA.a",
820 "$base/unittest-cont1/e/fileB.a",
821 "$base/unittest-cont1/e/fileC.a"
823 $createOps = array();
825 foreach ( $files as $path ) {
826 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
827 $this->assertGoodStatus( $status,
828 "Preparing $path succeeded without warnings ($backendName)." );
829 $createOps[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand( 0, 50000 ) );
830 $copyOps[] = array( 'op' => 'copy', 'src' => $path, 'dst' => "$path-2" );
831 $moveOps[] = array( 'op' => 'move', 'src' => "$path-2", 'dst' => "$path-3" );
832 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
833 $purgeOps[] = array( 'op' => 'delete', 'src' => "$path-3" );
835 $purgeOps[] = array( 'op' => 'null' );
837 $this->assertGoodStatus(
838 $this->backend
->doQuickOperations( $createOps ),
839 "Creation of source files succeeded ($backendName)." );
840 foreach ( $files as $file ) {
841 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $file ) ),
842 "File $file exists." );
845 $this->assertGoodStatus(
846 $this->backend
->doQuickOperations( $copyOps ),
847 "Quick copy of source files succeeded ($backendName)." );
848 foreach ( $files as $file ) {
849 $this->assertTrue( $this->backend
->fileExists( array( 'src' => "$file-2" ) ),
850 "File $file-2 exists." );
853 $this->assertGoodStatus(
854 $this->backend
->doQuickOperations( $moveOps ),
855 "Quick move of source files succeeded ($backendName)." );
856 foreach ( $files as $file ) {
857 $this->assertTrue( $this->backend
->fileExists( array( 'src' => "$file-3" ) ),
858 "File $file-3 move in." );
859 $this->assertFalse( $this->backend
->fileExists( array( 'src' => "$file-2" ) ),
860 "File $file-2 moved away." );
863 $this->assertGoodStatus(
864 $this->backend
->quickCopy( array( 'src' => $files[0], 'dst' => $files[0] ) ),
865 "Copy of file {$files[0]} over itself succeeded ($backendName)." );
866 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $files[0] ) ),
867 "File {$files[0]} still exists." );
869 $this->assertGoodStatus(
870 $this->backend
->quickMove( array( 'src' => $files[0], 'dst' => $files[0] ) ),
871 "Move of file {$files[0]} over itself succeeded ($backendName)." );
872 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $files[0] ) ),
873 "File {$files[0]} still exists." );
875 $this->assertGoodStatus(
876 $this->backend
->doQuickOperations( $purgeOps ),
877 "Quick deletion of source files succeeded ($backendName)." );
878 foreach ( $files as $file ) {
879 $this->assertFalse( $this->backend
->fileExists( array( 'src' => $file ) ),
880 "File $file purged." );
881 $this->assertFalse( $this->backend
->fileExists( array( 'src' => "$file-3" ) ),
882 "File $file-3 purged." );
887 * @dataProvider provider_testConcatenate
889 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
890 $this->filesToPrune
[] = $op['dst'];
892 $this->backend
= $this->singleBackend
;
893 $this->tearDownFiles();
894 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
895 $this->tearDownFiles();
897 $this->backend
= $this->multiBackend
;
898 $this->tearDownFiles();
899 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
900 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
901 $this->tearDownFiles();
904 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
905 $backendName = $this->backendClass();
910 foreach ( $srcs as $i => $source ) {
911 $this->prepare( array( 'dir' => dirname( $source ) ) );
913 'op' => 'create', // operation
914 'dst' => $source, // source
915 'content' => $srcsContent[$i]
917 $expContent .= $srcsContent[$i];
919 $status = $this->backend
->doOperations( $ops );
921 $this->assertGoodStatus( $status,
922 "Creation of source files succeeded ($backendName)." );
924 $dest = $params['dst'];
925 if ( $alreadyExists ) {
926 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
927 $this->assertEquals( true, $ok,
928 "Creation of file at $dest succeeded ($backendName)." );
930 $ok = file_put_contents( $dest, '' ) !== false;
931 $this->assertEquals( true, $ok,
932 "Creation of 0-byte file at $dest succeeded ($backendName)." );
935 // Combine the files into one
936 $status = $this->backend
->concatenate( $params );
938 $this->assertGoodStatus( $status,
939 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
940 $this->assertEquals( true, $status->isOK(),
941 "Creation of concat file at $dest succeeded ($backendName)." );
943 $this->assertEquals( false, $status->isOK(),
944 "Creation of concat file at $dest failed ($backendName)." );
948 $this->assertEquals( true, is_file( $dest ),
949 "Dest concat file $dest exists after creation ($backendName)." );
951 $this->assertEquals( true, is_file( $dest ),
952 "Dest concat file $dest exists after failed creation ($backendName)." );
955 $contents = file_get_contents( $dest );
956 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
959 $this->assertEquals( $expContent, $contents,
960 "Concat file at $dest has correct contents ($backendName)." );
962 $this->assertNotEquals( $expContent, $contents,
963 "Concat file at $dest has correct contents ($backendName)." );
967 public static function provider_testConcatenate() {
970 $rand = mt_rand( 0, 2000000000 ) . time();
971 $dest = wfTempDir() . "/randomfile!$rand.txt";
973 self
::baseStorePath() . '/unittest-cont1/e/file1.txt',
974 self
::baseStorePath() . '/unittest-cont1/e/file2.txt',
975 self
::baseStorePath() . '/unittest-cont1/e/file3.txt',
976 self
::baseStorePath() . '/unittest-cont1/e/file4.txt',
977 self
::baseStorePath() . '/unittest-cont1/e/file5.txt',
978 self
::baseStorePath() . '/unittest-cont1/e/file6.txt',
979 self
::baseStorePath() . '/unittest-cont1/e/file7.txt',
980 self
::baseStorePath() . '/unittest-cont1/e/file8.txt',
981 self
::baseStorePath() . '/unittest-cont1/e/file9.txt',
982 self
::baseStorePath() . '/unittest-cont1/e/file10.txt'
996 $params = array( 'srcs' => $srcs, 'dst' => $dest );
999 $params, // operation
1001 $content, // content for each source
1002 false, // no dest already exists
1007 $params, // operation
1009 $content, // content for each source
1010 true, // dest already exists
1018 * @dataProvider provider_testGetFileStat
1020 public function testGetFileStat( $path, $content, $alreadyExists ) {
1021 $this->backend
= $this->singleBackend
;
1022 $this->tearDownFiles();
1023 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1024 $this->tearDownFiles();
1026 $this->backend
= $this->multiBackend
;
1027 $this->tearDownFiles();
1028 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1029 $this->tearDownFiles();
1032 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
1033 $backendName = $this->backendClass();
1035 if ( $alreadyExists ) {
1036 $this->prepare( array( 'dir' => dirname( $path ) ) );
1037 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
1038 $this->assertGoodStatus( $status,
1039 "Creation of file at $path succeeded ($backendName)." );
1041 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1042 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1043 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
1045 $this->assertEquals( strlen( $content ), $size,
1046 "Correct file size of '$path'" );
1047 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1048 "Correct file timestamp of '$path'" );
1050 $size = $stat['size'];
1051 $time = $stat['mtime'];
1052 $this->assertEquals( strlen( $content ), $size,
1053 "Correct file size of '$path'" );
1054 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1055 "Correct file timestamp of '$path'" );
1057 $this->backend
->clearCache( array( $path ) );
1059 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1061 $this->assertEquals( strlen( $content ), $size,
1062 "Correct file size of '$path'" );
1064 $this->backend
->preloadCache( array( $path ) );
1066 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1068 $this->assertEquals( strlen( $content ), $size,
1069 "Correct file size of '$path'" );
1071 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1072 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1073 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
1075 $this->assertFalse( $size, "Correct file size of '$path'" );
1076 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1077 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1081 public static function provider_testGetFileStat() {
1084 $base = self
::baseStorePath();
1085 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
1086 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
1087 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
1093 * @dataProvider provider_testGetFileContents
1095 public function testGetFileContents( $source, $content ) {
1096 $this->backend
= $this->singleBackend
;
1097 $this->tearDownFiles();
1098 $this->doTestGetFileContents( $source, $content );
1099 $this->tearDownFiles();
1101 $this->backend
= $this->multiBackend
;
1102 $this->tearDownFiles();
1103 $this->doTestGetFileContents( $source, $content );
1104 $this->tearDownFiles();
1107 private function doTestGetFileContents( $source, $content ) {
1108 $backendName = $this->backendClass();
1110 $srcs = (array)$source;
1111 $content = (array)$content;
1112 foreach ( $srcs as $i => $src ) {
1113 $this->prepare( array( 'dir' => dirname( $src ) ) );
1114 $status = $this->backend
->doOperation(
1115 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1116 $this->assertGoodStatus( $status,
1117 "Creation of file at $src succeeded ($backendName)." );
1120 if ( is_array( $source ) ) {
1121 $contents = $this->backend
->getFileContentsMulti( array( 'srcs' => $source ) );
1122 foreach ( $contents as $path => $data ) {
1123 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1124 $this->assertEquals( current( $content ), $data, "Contents of $path is correct ($backendName)." );
1127 $this->assertEquals( $source, array_keys( $contents ), "Contents in right order ($backendName)." );
1128 $this->assertEquals( count( $source ), count( $contents ), "Contents array size correct ($backendName)." );
1130 $data = $this->backend
->getFileContents( array( 'src' => $source ) );
1131 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1132 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1136 public static function provider_testGetFileContents() {
1139 $base = self
::baseStorePath();
1140 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
1141 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
1143 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1144 "$base/unittest-cont1/e/a/z.txt" ),
1145 array( "contents xx", "contents xy", "contents xz" )
1152 * @dataProvider provider_testGetLocalCopy
1154 public function testGetLocalCopy( $source, $content ) {
1155 $this->backend
= $this->singleBackend
;
1156 $this->tearDownFiles();
1157 $this->doTestGetLocalCopy( $source, $content );
1158 $this->tearDownFiles();
1160 $this->backend
= $this->multiBackend
;
1161 $this->tearDownFiles();
1162 $this->doTestGetLocalCopy( $source, $content );
1163 $this->tearDownFiles();
1166 private function doTestGetLocalCopy( $source, $content ) {
1167 $backendName = $this->backendClass();
1169 $srcs = (array)$source;
1170 $content = (array)$content;
1171 foreach ( $srcs as $i => $src ) {
1172 $this->prepare( array( 'dir' => dirname( $src ) ) );
1173 $status = $this->backend
->doOperation(
1174 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1175 $this->assertGoodStatus( $status,
1176 "Creation of file at $src succeeded ($backendName)." );
1179 if ( is_array( $source ) ) {
1180 $tmpFiles = $this->backend
->getLocalCopyMulti( array( 'srcs' => $source ) );
1181 foreach ( $tmpFiles as $path => $tmpFile ) {
1182 $this->assertNotNull( $tmpFile,
1183 "Creation of local copy of $path succeeded ($backendName)." );
1184 $contents = file_get_contents( $tmpFile->getPath() );
1185 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1186 $this->assertEquals( current( $content ), $contents, "Local copy of $path is correct ($backendName)." );
1189 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local copies in right order ($backendName)." );
1190 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local copies array size correct ($backendName)." );
1192 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
1193 $this->assertNotNull( $tmpFile,
1194 "Creation of local copy of $source succeeded ($backendName)." );
1195 $contents = file_get_contents( $tmpFile->getPath() );
1196 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1197 $this->assertEquals( $content[0], $contents, "Local copy of $source is correct ($backendName)." );
1200 $obj = new stdClass();
1201 $tmpFile->bind( $obj );
1204 public static function provider_testGetLocalCopy() {
1207 $base = self
::baseStorePath();
1208 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1209 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1210 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1212 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1213 "$base/unittest-cont1/e/a/z.txt" ),
1214 array( "contents xx", "contents xy", "contents xz" )
1221 * @dataProvider provider_testGetLocalReference
1223 public function testGetLocalReference( $source, $content ) {
1224 $this->backend
= $this->singleBackend
;
1225 $this->tearDownFiles();
1226 $this->doTestGetLocalReference( $source, $content );
1227 $this->tearDownFiles();
1229 $this->backend
= $this->multiBackend
;
1230 $this->tearDownFiles();
1231 $this->doTestGetLocalReference( $source, $content );
1232 $this->tearDownFiles();
1235 private function doTestGetLocalReference( $source, $content ) {
1236 $backendName = $this->backendClass();
1238 $srcs = (array)$source;
1239 $content = (array)$content;
1240 foreach ( $srcs as $i => $src ) {
1241 $this->prepare( array( 'dir' => dirname( $src ) ) );
1242 $status = $this->backend
->doOperation(
1243 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1244 $this->assertGoodStatus( $status,
1245 "Creation of file at $src succeeded ($backendName)." );
1248 if ( is_array( $source ) ) {
1249 $tmpFiles = $this->backend
->getLocalReferenceMulti( array( 'srcs' => $source ) );
1250 foreach ( $tmpFiles as $path => $tmpFile ) {
1251 $this->assertNotNull( $tmpFile,
1252 "Creation of local copy of $path succeeded ($backendName)." );
1253 $contents = file_get_contents( $tmpFile->getPath() );
1254 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1255 $this->assertEquals( current( $content ), $contents, "Local ref of $path is correct ($backendName)." );
1258 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local refs in right order ($backendName)." );
1259 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local refs array size correct ($backendName)." );
1261 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
1262 $this->assertNotNull( $tmpFile,
1263 "Creation of local copy of $source succeeded ($backendName)." );
1264 $contents = file_get_contents( $tmpFile->getPath() );
1265 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1266 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1270 public static function provider_testGetLocalReference() {
1273 $base = self
::baseStorePath();
1274 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1275 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1276 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1278 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1279 "$base/unittest-cont1/e/a/z.txt" ),
1280 array( "contents xx", "contents xy", "contents xz" )
1286 public function testGetLocalCopyAndReference404() {
1287 $this->backend
= $this->singleBackend
;
1288 $this->tearDownFiles();
1289 $this->doTestGetLocalCopyAndReference404();
1290 $this->tearDownFiles();
1292 $this->backend
= $this->multiBackend
;
1293 $this->tearDownFiles();
1294 $this->doTestGetLocalCopyAndReference404();
1295 $this->tearDownFiles();
1298 public function doTestGetLocalCopyAndReference404() {
1299 $backendName = $this->backendClass();
1301 $base = self
::baseStorePath();
1303 $tmpFile = $this->backend
->getLocalCopy( array(
1304 'src' => "$base/unittest-cont1/not-there" ) );
1305 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1307 $tmpFile = $this->backend
->getLocalReference( array(
1308 'src' => "$base/unittest-cont1/not-there" ) );
1309 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1313 * @dataProvider provider_testGetFileHttpUrl
1315 public function testGetFileHttpUrl( $source, $content ) {
1316 $this->backend
= $this->singleBackend
;
1317 $this->tearDownFiles();
1318 $this->doTestGetFileHttpUrl( $source, $content );
1319 $this->tearDownFiles();
1321 $this->backend
= $this->multiBackend
;
1322 $this->tearDownFiles();
1323 $this->doTestGetFileHttpUrl( $source, $content );
1324 $this->tearDownFiles();
1327 private function doTestGetFileHttpUrl( $source, $content ) {
1328 $backendName = $this->backendClass();
1330 $this->prepare( array( 'dir' => dirname( $source ) ) );
1331 $status = $this->backend
->doOperation(
1332 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
1333 $this->assertGoodStatus( $status,
1334 "Creation of file at $source succeeded ($backendName)." );
1336 $url = $this->backend
->getFileHttpUrl( array( 'src' => $source ) );
1338 if ( $url !== null ) { // supported
1339 $data = Http
::request( "GET", $url );
1340 $this->assertEquals( $content, $data,
1341 "HTTP GET of URL has right contents ($backendName)." );
1345 public static function provider_testGetFileHttpUrl() {
1348 $base = self
::baseStorePath();
1349 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1350 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1351 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1357 * @dataProvider provider_testPrepareAndClean
1359 public function testPrepareAndClean( $path, $isOK ) {
1360 $this->backend
= $this->singleBackend
;
1361 $this->doTestPrepareAndClean( $path, $isOK );
1362 $this->tearDownFiles();
1364 $this->backend
= $this->multiBackend
;
1365 $this->doTestPrepareAndClean( $path, $isOK );
1366 $this->tearDownFiles();
1369 public static function provider_testPrepareAndClean() {
1370 $base = self
::baseStorePath();
1372 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1373 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1374 # Specific to FS backend with no basePath field set
1375 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1379 private function doTestPrepareAndClean( $path, $isOK ) {
1380 $backendName = $this->backendClass();
1382 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1384 $this->assertGoodStatus( $status,
1385 "Preparing dir $path succeeded without warnings ($backendName)." );
1386 $this->assertEquals( true, $status->isOK(),
1387 "Preparing dir $path succeeded ($backendName)." );
1389 $this->assertEquals( false, $status->isOK(),
1390 "Preparing dir $path failed ($backendName)." );
1393 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1395 $this->assertGoodStatus( $status,
1396 "Cleaning dir $path succeeded without warnings ($backendName)." );
1397 $this->assertEquals( true, $status->isOK(),
1398 "Cleaning dir $path succeeded ($backendName)." );
1400 $this->assertEquals( false, $status->isOK(),
1401 "Cleaning dir $path failed ($backendName)." );
1405 public function testRecursiveClean() {
1406 $this->backend
= $this->singleBackend
;
1407 $this->doTestRecursiveClean();
1408 $this->tearDownFiles();
1410 $this->backend
= $this->multiBackend
;
1411 $this->doTestRecursiveClean();
1412 $this->tearDownFiles();
1415 private function doTestRecursiveClean() {
1416 $backendName = $this->backendClass();
1418 $base = self
::baseStorePath();
1420 "$base/unittest-cont1",
1421 "$base/unittest-cont1/e",
1422 "$base/unittest-cont1/e/a",
1423 "$base/unittest-cont1/e/a/b",
1424 "$base/unittest-cont1/e/a/b/c",
1425 "$base/unittest-cont1/e/a/b/c/d0",
1426 "$base/unittest-cont1/e/a/b/c/d1",
1427 "$base/unittest-cont1/e/a/b/c/d2",
1428 "$base/unittest-cont1/e/a/b/c/d0/1",
1429 "$base/unittest-cont1/e/a/b/c/d0/2",
1430 "$base/unittest-cont1/e/a/b/c/d1/3",
1431 "$base/unittest-cont1/e/a/b/c/d1/4",
1432 "$base/unittest-cont1/e/a/b/c/d2/5",
1433 "$base/unittest-cont1/e/a/b/c/d2/6"
1435 foreach ( $dirs as $dir ) {
1436 $status = $this->prepare( array( 'dir' => $dir ) );
1437 $this->assertGoodStatus( $status,
1438 "Preparing dir $dir succeeded without warnings ($backendName)." );
1441 if ( $this->backend
instanceof FSFileBackend
) {
1442 foreach ( $dirs as $dir ) {
1443 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1444 "Dir $dir exists ($backendName)." );
1448 $status = $this->backend
->clean(
1449 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1450 $this->assertGoodStatus( $status,
1451 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1453 foreach ( $dirs as $dir ) {
1454 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1455 "Dir $dir no longer exists ($backendName)." );
1459 // @TODO: testSecure
1461 public function testDoOperations() {
1462 $this->backend
= $this->singleBackend
;
1463 $this->tearDownFiles();
1464 $this->doTestDoOperations();
1465 $this->tearDownFiles();
1467 $this->backend
= $this->multiBackend
;
1468 $this->tearDownFiles();
1469 $this->doTestDoOperations();
1470 $this->tearDownFiles();
1473 private function doTestDoOperations() {
1474 $base = self
::baseStorePath();
1476 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1477 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1478 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1479 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1480 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1481 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1482 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1484 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1485 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1486 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1487 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1488 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1489 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1490 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1492 $status = $this->backend
->doOperations( array(
1493 array( 'op' => 'describe', 'src' => $fileA,
1494 'headers' => array( 'X-Content-Length' => '91.3' ), 'disposition' => 'inline' ),
1495 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1496 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1497 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1498 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1499 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1500 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1501 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1502 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1503 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1504 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1505 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1506 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1507 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1508 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1509 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1510 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1511 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1513 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1515 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1517 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1519 array( 'op' => 'null' ),
1523 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1524 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1525 $this->assertEquals( 14, count( $status->success
),
1526 "Operation batch has correct success array" );
1528 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1529 "File does not exist at $fileA" );
1530 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1531 "File does not exist at $fileB" );
1532 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1533 "File does not exist at $fileD" );
1535 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1536 "File exists at $fileC" );
1537 $this->assertEquals( $fileBContents,
1538 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1539 "Correct file contents of $fileC" );
1540 $this->assertEquals( strlen( $fileBContents ),
1541 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1542 "Correct file size of $fileC" );
1543 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1544 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1545 "Correct file SHA-1 of $fileC" );
1548 public function testDoOperationsPipeline() {
1549 $this->backend
= $this->singleBackend
;
1550 $this->tearDownFiles();
1551 $this->doTestDoOperationsPipeline();
1552 $this->tearDownFiles();
1554 $this->backend
= $this->multiBackend
;
1555 $this->tearDownFiles();
1556 $this->doTestDoOperationsPipeline();
1557 $this->tearDownFiles();
1560 // concurrency orientated
1561 private function doTestDoOperationsPipeline() {
1562 $base = self
::baseStorePath();
1564 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1565 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1566 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1568 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1569 file_put_contents( $tmpNameA, $fileAContents );
1570 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1571 file_put_contents( $tmpNameB, $fileBContents );
1572 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1573 file_put_contents( $tmpNameC, $fileCContents );
1575 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1576 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1577 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1579 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1580 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1581 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1582 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1584 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1585 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1586 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1587 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1588 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1590 $status = $this->backend
->doOperations( array(
1591 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1592 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1593 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1594 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1595 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1596 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1597 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1598 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1599 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1600 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1601 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1602 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1603 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1604 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1605 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1606 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1607 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1608 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1609 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1610 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1612 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1614 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1616 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1618 array( 'op' => 'null' ),
1622 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1623 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1624 $this->assertEquals( 16, count( $status->success
),
1625 "Operation batch has correct success array" );
1627 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1628 "File does not exist at $fileA" );
1629 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1630 "File does not exist at $fileB" );
1631 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1632 "File does not exist at $fileD" );
1634 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1635 "File exists at $fileC" );
1636 $this->assertEquals( $fileBContents,
1637 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1638 "Correct file contents of $fileC" );
1639 $this->assertEquals( strlen( $fileBContents ),
1640 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1641 "Correct file size of $fileC" );
1642 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1643 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1644 "Correct file SHA-1 of $fileC" );
1647 public function testDoOperationsFailing() {
1648 $this->backend
= $this->singleBackend
;
1649 $this->tearDownFiles();
1650 $this->doTestDoOperationsFailing();
1651 $this->tearDownFiles();
1653 $this->backend
= $this->multiBackend
;
1654 $this->tearDownFiles();
1655 $this->doTestDoOperationsFailing();
1656 $this->tearDownFiles();
1659 private function doTestDoOperationsFailing() {
1660 $base = self
::baseStorePath();
1662 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1663 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1664 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1665 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1666 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1667 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1668 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1670 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1671 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1672 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1673 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1674 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1675 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1677 $status = $this->backend
->doOperations( array(
1678 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1679 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1680 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1681 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1682 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1683 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1684 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1685 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1686 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1687 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1688 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1689 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1690 array( 'op' => 'delete', 'src' => $fileD ),
1691 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1692 array( 'op' => 'null' ),
1694 ), array( 'force' => 1 ) );
1696 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1697 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1698 $this->assertEquals( 8, count( $status->success
),
1699 "Operation batch has correct success array" );
1701 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1702 "File does not exist at $fileB" );
1703 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1704 "File does not exist at $fileD" );
1706 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1707 "File does not exist at $fileA" );
1708 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1709 "File exists at $fileC" );
1710 $this->assertEquals( $fileBContents,
1711 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1712 "Correct file contents of $fileA" );
1713 $this->assertEquals( strlen( $fileBContents ),
1714 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1715 "Correct file size of $fileA" );
1716 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1717 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1718 "Correct file SHA-1 of $fileA" );
1721 public function testGetFileList() {
1722 $this->backend
= $this->singleBackend
;
1723 $this->tearDownFiles();
1724 $this->doTestGetFileList();
1725 $this->tearDownFiles();
1727 $this->backend
= $this->multiBackend
;
1728 $this->tearDownFiles();
1729 $this->doTestGetFileList();
1730 $this->tearDownFiles();
1733 private function doTestGetFileList() {
1734 $backendName = $this->backendClass();
1735 $base = self
::baseStorePath();
1737 // Should have no errors
1738 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1741 "$base/unittest-cont1/e/test1.txt",
1742 "$base/unittest-cont1/e/test2.txt",
1743 "$base/unittest-cont1/e/test3.txt",
1744 "$base/unittest-cont1/e/subdir1/test1.txt",
1745 "$base/unittest-cont1/e/subdir1/test2.txt",
1746 "$base/unittest-cont1/e/subdir2/test3.txt",
1747 "$base/unittest-cont1/e/subdir2/test4.txt",
1748 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1749 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1750 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1751 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1752 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1753 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1754 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1759 foreach ( $files as $file ) {
1760 $this->prepare( array( 'dir' => dirname( $file ) ) );
1761 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1763 $status = $this->backend
->doQuickOperations( $ops );
1764 $this->assertGoodStatus( $status,
1765 "Creation of files succeeded ($backendName)." );
1766 $this->assertEquals( true, $status->isOK(),
1767 "Creation of files succeeded with OK status ($backendName)." );
1774 "e/subdir1/test1.txt",
1775 "e/subdir1/test2.txt",
1776 "e/subdir2/test3.txt",
1777 "e/subdir2/test4.txt",
1778 "e/subdir2/subdir/test1.txt",
1779 "e/subdir2/subdir/test2.txt",
1780 "e/subdir2/subdir/test3.txt",
1781 "e/subdir2/subdir/test4.txt",
1782 "e/subdir2/subdir/test5.txt",
1783 "e/subdir2/subdir/sub/test0.txt",
1784 "e/subdir2/subdir/sub/120-px-file.txt",
1788 // Actual listing (no trailing slash)
1790 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1791 foreach ( $iter as $file ) {
1796 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1798 // Actual listing (with trailing slash)
1800 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1801 foreach ( $iter as $file ) {
1806 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1816 "sub/120-px-file.txt",
1820 // Actual listing (no trailing slash)
1822 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1823 foreach ( $iter as $file ) {
1828 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1830 // Actual listing (with trailing slash)
1832 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1833 foreach ( $iter as $file ) {
1838 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1840 // Actual listing (using iterator second time)
1842 foreach ( $iter as $file ) {
1847 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1849 // Expected listing (top files only)
1859 // Actual listing (top files only)
1861 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1862 foreach ( $iter as $file ) {
1867 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1869 foreach ( $files as $file ) { // clean up
1870 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1873 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1874 foreach ( $iter as $iter ) {
1879 public function testGetDirectoryList() {
1880 $this->backend
= $this->singleBackend
;
1881 $this->tearDownFiles();
1882 $this->doTestGetDirectoryList();
1883 $this->tearDownFiles();
1885 $this->backend
= $this->multiBackend
;
1886 $this->tearDownFiles();
1887 $this->doTestGetDirectoryList();
1888 $this->tearDownFiles();
1891 private function doTestGetDirectoryList() {
1892 $backendName = $this->backendClass();
1894 $base = self
::baseStorePath();
1896 "$base/unittest-cont1/e/test1.txt",
1897 "$base/unittest-cont1/e/test2.txt",
1898 "$base/unittest-cont1/e/test3.txt",
1899 "$base/unittest-cont1/e/subdir1/test1.txt",
1900 "$base/unittest-cont1/e/subdir1/test2.txt",
1901 "$base/unittest-cont1/e/subdir2/test3.txt",
1902 "$base/unittest-cont1/e/subdir2/test4.txt",
1903 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1904 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1905 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1906 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1907 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1908 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1909 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1914 foreach ( $files as $file ) {
1915 $this->prepare( array( 'dir' => dirname( $file ) ) );
1916 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1918 $status = $this->backend
->doQuickOperations( $ops );
1919 $this->assertGoodStatus( $status,
1920 "Creation of files succeeded ($backendName)." );
1921 $this->assertEquals( true, $status->isOK(),
1922 "Creation of files succeeded with OK status ($backendName)." );
1924 $this->assertEquals( true,
1925 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1926 "Directory exists in ($backendName)." );
1927 $this->assertEquals( true,
1928 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1929 "Directory exists in ($backendName)." );
1930 $this->assertEquals( false,
1931 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1932 "Directory does not exists in ($backendName)." );
1940 // Actual listing (no trailing slash)
1942 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1943 foreach ( $iter as $file ) {
1948 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1959 // Actual listing (no trailing slash)
1961 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1962 foreach ( $iter as $file ) {
1967 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1969 // Actual listing (with trailing slash)
1971 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1972 foreach ( $iter as $file ) {
1977 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1985 // Actual listing (no trailing slash)
1987 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1988 foreach ( $iter as $file ) {
1993 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1995 // Actual listing (with trailing slash)
1997 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1998 foreach ( $iter as $file ) {
2003 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2005 // Actual listing (using iterator second time)
2007 foreach ( $iter as $file ) {
2012 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
2014 // Expected listing (recursive)
2024 "e/subdir4/subdir/sub",
2028 // Actual listing (recursive)
2030 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
2031 foreach ( $iter as $file ) {
2036 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2038 // Expected listing (recursive)
2045 // Actual listing (recursive)
2047 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
2048 foreach ( $iter as $file ) {
2053 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2055 // Actual listing (recursive, second time)
2057 foreach ( $iter as $file ) {
2062 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2064 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) );
2065 $items = is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2066 $this->assertEquals( array(), $items, "Directory listing is empty." );
2068 foreach ( $files as $file ) { // clean up
2069 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
2072 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
2073 foreach ( $iter as $file ) {
2077 $items = is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2078 $this->assertEquals( array(), $items, "Directory listing is empty." );
2080 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/not/exists" ) );
2081 $items = is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2082 $this->assertEquals( array(), $items, "Directory listing is empty." );
2085 public function testLockCalls() {
2086 $this->backend
= $this->singleBackend
;
2087 $this->doTestLockCalls();
2090 private function doTestLockCalls() {
2091 $backendName = $this->backendClass();
2098 "subdir1", // duplicate
2099 "subdir1/test1.txt",
2100 "subdir1/test2.txt",
2102 "subdir2", // duplicate
2103 "subdir2/test3.txt",
2104 "subdir2/test4.txt",
2106 "subdir2/subdir/test1.txt",
2107 "subdir2/subdir/test2.txt",
2108 "subdir2/subdir/test3.txt",
2109 "subdir2/subdir/test4.txt",
2110 "subdir2/subdir/test5.txt",
2111 "subdir2/subdir/sub",
2112 "subdir2/subdir/sub/test0.txt",
2113 "subdir2/subdir/sub/120-px-file.txt",
2116 for ( $i = 0; $i < 25; $i++
) {
2117 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2118 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2119 "Locking of files succeeded ($backendName) ($i)." );
2120 $this->assertEquals( true, $status->isOK(),
2121 "Locking of files succeeded with OK status ($backendName) ($i)." );
2123 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2124 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2125 "Locking of files succeeded ($backendName) ($i)." );
2126 $this->assertEquals( true, $status->isOK(),
2127 "Locking of files succeeded with OK status ($backendName) ($i)." );
2129 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2130 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2131 "Locking of files succeeded ($backendName) ($i)." );
2132 $this->assertEquals( true, $status->isOK(),
2133 "Locking of files succeeded with OK status ($backendName) ($i)." );
2135 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
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 ## Flip the acquire/release ordering around ##
2143 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2144 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2145 "Locking of files succeeded ($backendName) ($i)." );
2146 $this->assertEquals( true, $status->isOK(),
2147 "Locking of files succeeded with OK status ($backendName) ($i)." );
2149 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2150 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2151 "Locking of files succeeded ($backendName) ($i)." );
2152 $this->assertEquals( true, $status->isOK(),
2153 "Locking of files succeeded with OK status ($backendName) ($i)." );
2155 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2156 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2157 "Locking of files succeeded ($backendName). ($i)" );
2158 $this->assertEquals( true, $status->isOK(),
2159 "Locking of files succeeded with OK status ($backendName) ($i)." );
2161 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2162 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2163 "Locking of files succeeded ($backendName) ($i)." );
2164 $this->assertEquals( true, $status->isOK(),
2165 "Locking of files succeeded with OK status ($backendName) ($i)." );
2168 $status = Status
::newGood();
2169 $sl = $this->backend
->getScopedFileLocks( $paths, LockManager
::LOCK_EX
, $status );
2170 $this->assertType( 'ScopedLock', $sl,
2171 "Scoped locking of files succeeded ($backendName)." );
2172 $this->assertEquals( array(), $status->errors
,
2173 "Scoped locking of files succeeded ($backendName)." );
2174 $this->assertEquals( true, $status->isOK(),
2175 "Scoped locking of files succeeded with OK status ($backendName)." );
2177 ScopedLock
::release( $sl );
2178 $this->assertEquals( null, $sl,
2179 "Scoped unlocking of files succeeded ($backendName)." );
2180 $this->assertEquals( array(), $status->errors
,
2181 "Scoped unlocking of files succeeded ($backendName)." );
2182 $this->assertEquals( true, $status->isOK(),
2183 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2186 // test helper wrapper for backend prepare() function
2187 private function prepare( array $params ) {
2188 return $this->backend
->prepare( $params );
2191 // test helper wrapper for backend prepare() function
2192 private function create( array $params ) {
2193 $params['op'] = 'create';
2194 return $this->backend
->doQuickOperations( array( $params ) );
2197 function tearDownFiles() {
2198 foreach ( $this->filesToPrune
as $file ) {
2201 $containers = array( 'unittest-cont1', 'unittest-cont2' );
2202 foreach ( $containers as $container ) {
2203 $this->deleteFiles( $container );
2205 $this->filesToPrune
= array();
2208 private function deleteFiles( $container ) {
2209 $base = self
::baseStorePath();
2210 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
2212 foreach ( $iter as $file ) {
2213 $this->backend
->quickDelete( array( 'src' => "$base/$container/$file" ) );
2215 // free the directory, to avoid Permission denied under windows on rmdir
2218 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
2221 function assertBackendPathsConsistent( array $paths ) {
2222 if ( $this->backend
instanceof FileBackendMultiWrite
) {
2223 $status = $this->backend
->consistencyCheck( $paths );
2224 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2228 function assertGoodStatus( $status, $msg ) {
2229 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );