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->filesToPrune
[] = $op['dst']; # avoid file leaking
896 $this->tearDownFiles();
898 $this->backend
= $this->multiBackend
;
899 $this->tearDownFiles();
900 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
901 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
902 $this->tearDownFiles();
905 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
906 $backendName = $this->backendClass();
911 foreach ( $srcs as $i => $source ) {
912 $this->prepare( array( 'dir' => dirname( $source ) ) );
914 'op' => 'create', // operation
915 'dst' => $source, // source
916 'content' => $srcsContent[$i]
918 $expContent .= $srcsContent[$i];
920 $status = $this->backend
->doOperations( $ops );
922 $this->assertGoodStatus( $status,
923 "Creation of source files succeeded ($backendName)." );
925 $dest = $params['dst'];
926 if ( $alreadyExists ) {
927 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
928 $this->assertEquals( true, $ok,
929 "Creation of file at $dest succeeded ($backendName)." );
931 $ok = file_put_contents( $dest, '' ) !== false;
932 $this->assertEquals( true, $ok,
933 "Creation of 0-byte file at $dest succeeded ($backendName)." );
936 // Combine the files into one
937 $status = $this->backend
->concatenate( $params );
939 $this->assertGoodStatus( $status,
940 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
941 $this->assertEquals( true, $status->isOK(),
942 "Creation of concat file at $dest succeeded ($backendName)." );
944 $this->assertEquals( false, $status->isOK(),
945 "Creation of concat file at $dest failed ($backendName)." );
949 $this->assertEquals( true, is_file( $dest ),
950 "Dest concat file $dest exists after creation ($backendName)." );
952 $this->assertEquals( true, is_file( $dest ),
953 "Dest concat file $dest exists after failed creation ($backendName)." );
956 $contents = file_get_contents( $dest );
957 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
960 $this->assertEquals( $expContent, $contents,
961 "Concat file at $dest has correct contents ($backendName)." );
963 $this->assertNotEquals( $expContent, $contents,
964 "Concat file at $dest has correct contents ($backendName)." );
968 public static function provider_testConcatenate() {
971 $rand = mt_rand( 0, 2000000000 ) . time();
972 $dest = wfTempDir() . "/randomfile!$rand.txt";
974 self
::baseStorePath() . '/unittest-cont1/e/file1.txt',
975 self
::baseStorePath() . '/unittest-cont1/e/file2.txt',
976 self
::baseStorePath() . '/unittest-cont1/e/file3.txt',
977 self
::baseStorePath() . '/unittest-cont1/e/file4.txt',
978 self
::baseStorePath() . '/unittest-cont1/e/file5.txt',
979 self
::baseStorePath() . '/unittest-cont1/e/file6.txt',
980 self
::baseStorePath() . '/unittest-cont1/e/file7.txt',
981 self
::baseStorePath() . '/unittest-cont1/e/file8.txt',
982 self
::baseStorePath() . '/unittest-cont1/e/file9.txt',
983 self
::baseStorePath() . '/unittest-cont1/e/file10.txt'
997 $params = array( 'srcs' => $srcs, 'dst' => $dest );
1000 $params, // operation
1002 $content, // content for each source
1003 false, // no dest already exists
1008 $params, // operation
1010 $content, // content for each source
1011 true, // dest already exists
1019 * @dataProvider provider_testGetFileStat
1021 public function testGetFileStat( $path, $content, $alreadyExists ) {
1022 $this->backend
= $this->singleBackend
;
1023 $this->tearDownFiles();
1024 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1025 $this->tearDownFiles();
1027 $this->backend
= $this->multiBackend
;
1028 $this->tearDownFiles();
1029 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1030 $this->tearDownFiles();
1033 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
1034 $backendName = $this->backendClass();
1036 if ( $alreadyExists ) {
1037 $this->prepare( array( 'dir' => dirname( $path ) ) );
1038 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
1039 $this->assertGoodStatus( $status,
1040 "Creation of file at $path succeeded ($backendName)." );
1042 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1043 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1044 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
1046 $this->assertEquals( strlen( $content ), $size,
1047 "Correct file size of '$path'" );
1048 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1049 "Correct file timestamp of '$path'" );
1051 $size = $stat['size'];
1052 $time = $stat['mtime'];
1053 $this->assertEquals( strlen( $content ), $size,
1054 "Correct file size of '$path'" );
1055 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1056 "Correct file timestamp of '$path'" );
1058 $this->backend
->clearCache( array( $path ) );
1060 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1062 $this->assertEquals( strlen( $content ), $size,
1063 "Correct file size of '$path'" );
1065 $this->backend
->preloadCache( array( $path ) );
1067 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1069 $this->assertEquals( strlen( $content ), $size,
1070 "Correct file size of '$path'" );
1072 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1073 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1074 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
1076 $this->assertFalse( $size, "Correct file size of '$path'" );
1077 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1078 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1082 public static function provider_testGetFileStat() {
1085 $base = self
::baseStorePath();
1086 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
1087 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
1088 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
1094 * @dataProvider provider_testGetFileContents
1096 public function testGetFileContents( $source, $content ) {
1097 $this->backend
= $this->singleBackend
;
1098 $this->tearDownFiles();
1099 $this->doTestGetFileContents( $source, $content );
1100 $this->tearDownFiles();
1102 $this->backend
= $this->multiBackend
;
1103 $this->tearDownFiles();
1104 $this->doTestGetFileContents( $source, $content );
1105 $this->tearDownFiles();
1108 private function doTestGetFileContents( $source, $content ) {
1109 $backendName = $this->backendClass();
1111 $srcs = (array)$source;
1112 $content = (array)$content;
1113 foreach ( $srcs as $i => $src ) {
1114 $this->prepare( array( 'dir' => dirname( $src ) ) );
1115 $status = $this->backend
->doOperation(
1116 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1117 $this->assertGoodStatus( $status,
1118 "Creation of file at $src succeeded ($backendName)." );
1121 if ( is_array( $source ) ) {
1122 $contents = $this->backend
->getFileContentsMulti( array( 'srcs' => $source ) );
1123 foreach ( $contents as $path => $data ) {
1124 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1125 $this->assertEquals( current( $content ), $data, "Contents of $path is correct ($backendName)." );
1128 $this->assertEquals( $source, array_keys( $contents ), "Contents in right order ($backendName)." );
1129 $this->assertEquals( count( $source ), count( $contents ), "Contents array size correct ($backendName)." );
1131 $data = $this->backend
->getFileContents( array( 'src' => $source ) );
1132 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1133 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1137 public static function provider_testGetFileContents() {
1140 $base = self
::baseStorePath();
1141 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
1142 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
1144 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1145 "$base/unittest-cont1/e/a/z.txt" ),
1146 array( "contents xx", "contents xy", "contents xz" )
1153 * @dataProvider provider_testGetLocalCopy
1155 public function testGetLocalCopy( $source, $content ) {
1156 $this->backend
= $this->singleBackend
;
1157 $this->tearDownFiles();
1158 $this->doTestGetLocalCopy( $source, $content );
1159 $this->tearDownFiles();
1161 $this->backend
= $this->multiBackend
;
1162 $this->tearDownFiles();
1163 $this->doTestGetLocalCopy( $source, $content );
1164 $this->tearDownFiles();
1167 private function doTestGetLocalCopy( $source, $content ) {
1168 $backendName = $this->backendClass();
1170 $srcs = (array)$source;
1171 $content = (array)$content;
1172 foreach ( $srcs as $i => $src ) {
1173 $this->prepare( array( 'dir' => dirname( $src ) ) );
1174 $status = $this->backend
->doOperation(
1175 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1176 $this->assertGoodStatus( $status,
1177 "Creation of file at $src succeeded ($backendName)." );
1180 if ( is_array( $source ) ) {
1181 $tmpFiles = $this->backend
->getLocalCopyMulti( array( 'srcs' => $source ) );
1182 foreach ( $tmpFiles as $path => $tmpFile ) {
1183 $this->assertNotNull( $tmpFile,
1184 "Creation of local copy of $path succeeded ($backendName)." );
1185 $contents = file_get_contents( $tmpFile->getPath() );
1186 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1187 $this->assertEquals( current( $content ), $contents, "Local copy of $path is correct ($backendName)." );
1190 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local copies in right order ($backendName)." );
1191 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local copies array size correct ($backendName)." );
1193 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
1194 $this->assertNotNull( $tmpFile,
1195 "Creation of local copy of $source succeeded ($backendName)." );
1196 $contents = file_get_contents( $tmpFile->getPath() );
1197 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1198 $this->assertEquals( $content[0], $contents, "Local copy of $source is correct ($backendName)." );
1201 $obj = new stdClass();
1202 $tmpFile->bind( $obj );
1205 public static function provider_testGetLocalCopy() {
1208 $base = self
::baseStorePath();
1209 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1210 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1211 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1213 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1214 "$base/unittest-cont1/e/a/z.txt" ),
1215 array( "contents xx", "contents xy", "contents xz" )
1222 * @dataProvider provider_testGetLocalReference
1224 public function testGetLocalReference( $source, $content ) {
1225 $this->backend
= $this->singleBackend
;
1226 $this->tearDownFiles();
1227 $this->doTestGetLocalReference( $source, $content );
1228 $this->tearDownFiles();
1230 $this->backend
= $this->multiBackend
;
1231 $this->tearDownFiles();
1232 $this->doTestGetLocalReference( $source, $content );
1233 $this->tearDownFiles();
1236 private function doTestGetLocalReference( $source, $content ) {
1237 $backendName = $this->backendClass();
1239 $srcs = (array)$source;
1240 $content = (array)$content;
1241 foreach ( $srcs as $i => $src ) {
1242 $this->prepare( array( 'dir' => dirname( $src ) ) );
1243 $status = $this->backend
->doOperation(
1244 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1245 $this->assertGoodStatus( $status,
1246 "Creation of file at $src succeeded ($backendName)." );
1249 if ( is_array( $source ) ) {
1250 $tmpFiles = $this->backend
->getLocalReferenceMulti( array( 'srcs' => $source ) );
1251 foreach ( $tmpFiles as $path => $tmpFile ) {
1252 $this->assertNotNull( $tmpFile,
1253 "Creation of local copy of $path succeeded ($backendName)." );
1254 $contents = file_get_contents( $tmpFile->getPath() );
1255 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1256 $this->assertEquals( current( $content ), $contents, "Local ref of $path is correct ($backendName)." );
1259 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local refs in right order ($backendName)." );
1260 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local refs array size correct ($backendName)." );
1262 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
1263 $this->assertNotNull( $tmpFile,
1264 "Creation of local copy of $source succeeded ($backendName)." );
1265 $contents = file_get_contents( $tmpFile->getPath() );
1266 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1267 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1271 public static function provider_testGetLocalReference() {
1274 $base = self
::baseStorePath();
1275 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1276 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1277 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1279 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1280 "$base/unittest-cont1/e/a/z.txt" ),
1281 array( "contents xx", "contents xy", "contents xz" )
1287 public function testGetLocalCopyAndReference404() {
1288 $this->backend
= $this->singleBackend
;
1289 $this->tearDownFiles();
1290 $this->doTestGetLocalCopyAndReference404();
1291 $this->tearDownFiles();
1293 $this->backend
= $this->multiBackend
;
1294 $this->tearDownFiles();
1295 $this->doTestGetLocalCopyAndReference404();
1296 $this->tearDownFiles();
1299 public function doTestGetLocalCopyAndReference404() {
1300 $backendName = $this->backendClass();
1302 $base = self
::baseStorePath();
1304 $tmpFile = $this->backend
->getLocalCopy( array(
1305 'src' => "$base/unittest-cont1/not-there" ) );
1306 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1308 $tmpFile = $this->backend
->getLocalReference( array(
1309 'src' => "$base/unittest-cont1/not-there" ) );
1310 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1314 * @dataProvider provider_testGetFileHttpUrl
1316 public function testGetFileHttpUrl( $source, $content ) {
1317 $this->backend
= $this->singleBackend
;
1318 $this->tearDownFiles();
1319 $this->doTestGetFileHttpUrl( $source, $content );
1320 $this->tearDownFiles();
1322 $this->backend
= $this->multiBackend
;
1323 $this->tearDownFiles();
1324 $this->doTestGetFileHttpUrl( $source, $content );
1325 $this->tearDownFiles();
1328 private function doTestGetFileHttpUrl( $source, $content ) {
1329 $backendName = $this->backendClass();
1331 $this->prepare( array( 'dir' => dirname( $source ) ) );
1332 $status = $this->backend
->doOperation(
1333 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
1334 $this->assertGoodStatus( $status,
1335 "Creation of file at $source succeeded ($backendName)." );
1337 $url = $this->backend
->getFileHttpUrl( array( 'src' => $source ) );
1339 if ( $url !== null ) { // supported
1340 $data = Http
::request( "GET", $url );
1341 $this->assertEquals( $content, $data,
1342 "HTTP GET of URL has right contents ($backendName)." );
1346 public static function provider_testGetFileHttpUrl() {
1349 $base = self
::baseStorePath();
1350 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1351 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1352 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1358 * @dataProvider provider_testPrepareAndClean
1360 public function testPrepareAndClean( $path, $isOK ) {
1361 $this->backend
= $this->singleBackend
;
1362 $this->doTestPrepareAndClean( $path, $isOK );
1363 $this->tearDownFiles();
1365 $this->backend
= $this->multiBackend
;
1366 $this->doTestPrepareAndClean( $path, $isOK );
1367 $this->tearDownFiles();
1370 public static function provider_testPrepareAndClean() {
1371 $base = self
::baseStorePath();
1373 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1374 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1375 # Specific to FS backend with no basePath field set
1376 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1380 private function doTestPrepareAndClean( $path, $isOK ) {
1381 $backendName = $this->backendClass();
1383 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1385 $this->assertGoodStatus( $status,
1386 "Preparing dir $path succeeded without warnings ($backendName)." );
1387 $this->assertEquals( true, $status->isOK(),
1388 "Preparing dir $path succeeded ($backendName)." );
1390 $this->assertEquals( false, $status->isOK(),
1391 "Preparing dir $path failed ($backendName)." );
1394 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1396 $this->assertGoodStatus( $status,
1397 "Cleaning dir $path succeeded without warnings ($backendName)." );
1398 $this->assertEquals( true, $status->isOK(),
1399 "Cleaning dir $path succeeded ($backendName)." );
1401 $this->assertEquals( false, $status->isOK(),
1402 "Cleaning dir $path failed ($backendName)." );
1406 public function testRecursiveClean() {
1407 $this->backend
= $this->singleBackend
;
1408 $this->doTestRecursiveClean();
1409 $this->tearDownFiles();
1411 $this->backend
= $this->multiBackend
;
1412 $this->doTestRecursiveClean();
1413 $this->tearDownFiles();
1416 private function doTestRecursiveClean() {
1417 $backendName = $this->backendClass();
1419 $base = self
::baseStorePath();
1421 "$base/unittest-cont1",
1422 "$base/unittest-cont1/e",
1423 "$base/unittest-cont1/e/a",
1424 "$base/unittest-cont1/e/a/b",
1425 "$base/unittest-cont1/e/a/b/c",
1426 "$base/unittest-cont1/e/a/b/c/d0",
1427 "$base/unittest-cont1/e/a/b/c/d1",
1428 "$base/unittest-cont1/e/a/b/c/d2",
1429 "$base/unittest-cont1/e/a/b/c/d0/1",
1430 "$base/unittest-cont1/e/a/b/c/d0/2",
1431 "$base/unittest-cont1/e/a/b/c/d1/3",
1432 "$base/unittest-cont1/e/a/b/c/d1/4",
1433 "$base/unittest-cont1/e/a/b/c/d2/5",
1434 "$base/unittest-cont1/e/a/b/c/d2/6"
1436 foreach ( $dirs as $dir ) {
1437 $status = $this->prepare( array( 'dir' => $dir ) );
1438 $this->assertGoodStatus( $status,
1439 "Preparing dir $dir succeeded without warnings ($backendName)." );
1442 if ( $this->backend
instanceof FSFileBackend
) {
1443 foreach ( $dirs as $dir ) {
1444 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1445 "Dir $dir exists ($backendName)." );
1449 $status = $this->backend
->clean(
1450 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1451 $this->assertGoodStatus( $status,
1452 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1454 foreach ( $dirs as $dir ) {
1455 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1456 "Dir $dir no longer exists ($backendName)." );
1460 // @TODO: testSecure
1462 public function testDoOperations() {
1463 $this->backend
= $this->singleBackend
;
1464 $this->tearDownFiles();
1465 $this->doTestDoOperations();
1466 $this->tearDownFiles();
1468 $this->backend
= $this->multiBackend
;
1469 $this->tearDownFiles();
1470 $this->doTestDoOperations();
1471 $this->tearDownFiles();
1474 private function doTestDoOperations() {
1475 $base = self
::baseStorePath();
1477 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1478 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1479 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1480 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1481 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1482 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1483 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1485 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1486 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1487 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1488 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1489 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1490 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1491 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1493 $status = $this->backend
->doOperations( array(
1494 array( 'op' => 'describe', 'src' => $fileA,
1495 'headers' => array( 'X-Content-Length' => '91.3' ), 'disposition' => 'inline' ),
1496 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1497 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1498 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1499 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1500 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1501 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1502 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1503 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1504 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1505 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1506 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1507 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1508 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1509 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1510 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1511 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1512 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1514 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1516 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1518 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1520 array( 'op' => 'null' ),
1524 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1525 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1526 $this->assertEquals( 14, count( $status->success
),
1527 "Operation batch has correct success array" );
1529 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1530 "File does not exist at $fileA" );
1531 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1532 "File does not exist at $fileB" );
1533 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1534 "File does not exist at $fileD" );
1536 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1537 "File exists at $fileC" );
1538 $this->assertEquals( $fileBContents,
1539 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1540 "Correct file contents of $fileC" );
1541 $this->assertEquals( strlen( $fileBContents ),
1542 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1543 "Correct file size of $fileC" );
1544 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1545 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1546 "Correct file SHA-1 of $fileC" );
1549 public function testDoOperationsPipeline() {
1550 $this->backend
= $this->singleBackend
;
1551 $this->tearDownFiles();
1552 $this->doTestDoOperationsPipeline();
1553 $this->tearDownFiles();
1555 $this->backend
= $this->multiBackend
;
1556 $this->tearDownFiles();
1557 $this->doTestDoOperationsPipeline();
1558 $this->tearDownFiles();
1561 // concurrency orientated
1562 private function doTestDoOperationsPipeline() {
1563 $base = self
::baseStorePath();
1565 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1566 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1567 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1569 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1570 file_put_contents( $tmpNameA, $fileAContents );
1571 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1572 file_put_contents( $tmpNameB, $fileBContents );
1573 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1574 file_put_contents( $tmpNameC, $fileCContents );
1576 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1577 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1578 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1580 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1581 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1582 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1583 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1585 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1586 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1587 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1588 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1589 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1591 $status = $this->backend
->doOperations( array(
1592 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1593 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1594 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1595 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1596 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1597 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1598 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1599 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1600 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1601 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1602 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1603 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1604 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1605 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1606 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1607 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1608 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1609 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1610 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1611 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1613 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1615 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1617 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1619 array( 'op' => 'null' ),
1623 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1624 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1625 $this->assertEquals( 16, count( $status->success
),
1626 "Operation batch has correct success array" );
1628 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1629 "File does not exist at $fileA" );
1630 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1631 "File does not exist at $fileB" );
1632 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1633 "File does not exist at $fileD" );
1635 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1636 "File exists at $fileC" );
1637 $this->assertEquals( $fileBContents,
1638 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1639 "Correct file contents of $fileC" );
1640 $this->assertEquals( strlen( $fileBContents ),
1641 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1642 "Correct file size of $fileC" );
1643 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1644 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1645 "Correct file SHA-1 of $fileC" );
1648 public function testDoOperationsFailing() {
1649 $this->backend
= $this->singleBackend
;
1650 $this->tearDownFiles();
1651 $this->doTestDoOperationsFailing();
1652 $this->tearDownFiles();
1654 $this->backend
= $this->multiBackend
;
1655 $this->tearDownFiles();
1656 $this->doTestDoOperationsFailing();
1657 $this->tearDownFiles();
1660 private function doTestDoOperationsFailing() {
1661 $base = self
::baseStorePath();
1663 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1664 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1665 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1666 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1667 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1668 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1669 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1671 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1672 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1673 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1674 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1675 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1676 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1678 $status = $this->backend
->doOperations( array(
1679 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1680 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1681 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1682 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1683 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1684 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1685 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1686 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1687 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1688 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1689 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1690 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1691 array( 'op' => 'delete', 'src' => $fileD ),
1692 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1693 array( 'op' => 'null' ),
1695 ), array( 'force' => 1 ) );
1697 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1698 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1699 $this->assertEquals( 8, count( $status->success
),
1700 "Operation batch has correct success array" );
1702 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1703 "File does not exist at $fileB" );
1704 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1705 "File does not exist at $fileD" );
1707 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1708 "File does not exist at $fileA" );
1709 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1710 "File exists at $fileC" );
1711 $this->assertEquals( $fileBContents,
1712 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1713 "Correct file contents of $fileA" );
1714 $this->assertEquals( strlen( $fileBContents ),
1715 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1716 "Correct file size of $fileA" );
1717 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1718 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1719 "Correct file SHA-1 of $fileA" );
1722 public function testGetFileList() {
1723 $this->backend
= $this->singleBackend
;
1724 $this->tearDownFiles();
1725 $this->doTestGetFileList();
1726 $this->tearDownFiles();
1728 $this->backend
= $this->multiBackend
;
1729 $this->tearDownFiles();
1730 $this->doTestGetFileList();
1731 $this->tearDownFiles();
1734 private function doTestGetFileList() {
1735 $backendName = $this->backendClass();
1736 $base = self
::baseStorePath();
1738 // Should have no errors
1739 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1742 "$base/unittest-cont1/e/test1.txt",
1743 "$base/unittest-cont1/e/test2.txt",
1744 "$base/unittest-cont1/e/test3.txt",
1745 "$base/unittest-cont1/e/subdir1/test1.txt",
1746 "$base/unittest-cont1/e/subdir1/test2.txt",
1747 "$base/unittest-cont1/e/subdir2/test3.txt",
1748 "$base/unittest-cont1/e/subdir2/test4.txt",
1749 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1750 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1751 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1752 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1753 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1754 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1755 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1760 foreach ( $files as $file ) {
1761 $this->prepare( array( 'dir' => dirname( $file ) ) );
1762 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1764 $status = $this->backend
->doQuickOperations( $ops );
1765 $this->assertGoodStatus( $status,
1766 "Creation of files succeeded ($backendName)." );
1767 $this->assertEquals( true, $status->isOK(),
1768 "Creation of files succeeded with OK status ($backendName)." );
1775 "e/subdir1/test1.txt",
1776 "e/subdir1/test2.txt",
1777 "e/subdir2/test3.txt",
1778 "e/subdir2/test4.txt",
1779 "e/subdir2/subdir/test1.txt",
1780 "e/subdir2/subdir/test2.txt",
1781 "e/subdir2/subdir/test3.txt",
1782 "e/subdir2/subdir/test4.txt",
1783 "e/subdir2/subdir/test5.txt",
1784 "e/subdir2/subdir/sub/test0.txt",
1785 "e/subdir2/subdir/sub/120-px-file.txt",
1789 // Actual listing (no trailing slash)
1791 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1792 foreach ( $iter as $file ) {
1797 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1799 // Actual listing (with trailing slash)
1801 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1802 foreach ( $iter as $file ) {
1807 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1817 "sub/120-px-file.txt",
1821 // Actual listing (no trailing slash)
1823 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1824 foreach ( $iter as $file ) {
1829 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1831 // Actual listing (with trailing slash)
1833 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1834 foreach ( $iter as $file ) {
1839 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1841 // Actual listing (using iterator second time)
1843 foreach ( $iter as $file ) {
1848 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1850 // Expected listing (top files only)
1860 // Actual listing (top files only)
1862 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1863 foreach ( $iter as $file ) {
1868 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1870 foreach ( $files as $file ) { // clean up
1871 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1874 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1875 foreach ( $iter as $iter ) {
1880 public function testGetDirectoryList() {
1881 $this->backend
= $this->singleBackend
;
1882 $this->tearDownFiles();
1883 $this->doTestGetDirectoryList();
1884 $this->tearDownFiles();
1886 $this->backend
= $this->multiBackend
;
1887 $this->tearDownFiles();
1888 $this->doTestGetDirectoryList();
1889 $this->tearDownFiles();
1892 private function doTestGetDirectoryList() {
1893 $backendName = $this->backendClass();
1895 $base = self
::baseStorePath();
1897 "$base/unittest-cont1/e/test1.txt",
1898 "$base/unittest-cont1/e/test2.txt",
1899 "$base/unittest-cont1/e/test3.txt",
1900 "$base/unittest-cont1/e/subdir1/test1.txt",
1901 "$base/unittest-cont1/e/subdir1/test2.txt",
1902 "$base/unittest-cont1/e/subdir2/test3.txt",
1903 "$base/unittest-cont1/e/subdir2/test4.txt",
1904 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1905 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1906 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1907 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1908 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1909 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1910 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1915 foreach ( $files as $file ) {
1916 $this->prepare( array( 'dir' => dirname( $file ) ) );
1917 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1919 $status = $this->backend
->doQuickOperations( $ops );
1920 $this->assertGoodStatus( $status,
1921 "Creation of files succeeded ($backendName)." );
1922 $this->assertEquals( true, $status->isOK(),
1923 "Creation of files succeeded with OK status ($backendName)." );
1925 $this->assertEquals( true,
1926 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1927 "Directory exists in ($backendName)." );
1928 $this->assertEquals( true,
1929 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1930 "Directory exists in ($backendName)." );
1931 $this->assertEquals( false,
1932 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1933 "Directory does not exists in ($backendName)." );
1941 // Actual listing (no trailing slash)
1943 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1944 foreach ( $iter as $file ) {
1949 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1960 // Actual listing (no trailing slash)
1962 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1963 foreach ( $iter as $file ) {
1968 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1970 // Actual listing (with trailing slash)
1972 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1973 foreach ( $iter as $file ) {
1978 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1986 // Actual listing (no trailing slash)
1988 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1989 foreach ( $iter as $file ) {
1994 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1996 // Actual listing (with trailing slash)
1998 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1999 foreach ( $iter as $file ) {
2004 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2006 // Actual listing (using iterator second time)
2008 foreach ( $iter as $file ) {
2013 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
2015 // Expected listing (recursive)
2025 "e/subdir4/subdir/sub",
2029 // Actual listing (recursive)
2031 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
2032 foreach ( $iter as $file ) {
2037 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2039 // Expected listing (recursive)
2046 // Actual listing (recursive)
2048 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
2049 foreach ( $iter as $file ) {
2054 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2056 // Actual listing (recursive, second time)
2058 foreach ( $iter as $file ) {
2063 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2065 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) );
2066 $items = is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2067 $this->assertEquals( array(), $items, "Directory listing is empty." );
2069 foreach ( $files as $file ) { // clean up
2070 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
2073 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
2074 foreach ( $iter as $file ) {
2078 $items = is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2079 $this->assertEquals( array(), $items, "Directory listing is empty." );
2081 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/not/exists" ) );
2082 $items = is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2083 $this->assertEquals( array(), $items, "Directory listing is empty." );
2086 public function testLockCalls() {
2087 $this->backend
= $this->singleBackend
;
2088 $this->doTestLockCalls();
2091 private function doTestLockCalls() {
2092 $backendName = $this->backendClass();
2099 "subdir1", // duplicate
2100 "subdir1/test1.txt",
2101 "subdir1/test2.txt",
2103 "subdir2", // duplicate
2104 "subdir2/test3.txt",
2105 "subdir2/test4.txt",
2107 "subdir2/subdir/test1.txt",
2108 "subdir2/subdir/test2.txt",
2109 "subdir2/subdir/test3.txt",
2110 "subdir2/subdir/test4.txt",
2111 "subdir2/subdir/test5.txt",
2112 "subdir2/subdir/sub",
2113 "subdir2/subdir/sub/test0.txt",
2114 "subdir2/subdir/sub/120-px-file.txt",
2117 for ( $i = 0; $i < 25; $i++
) {
2118 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2119 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2120 "Locking of files succeeded ($backendName) ($i)." );
2121 $this->assertEquals( true, $status->isOK(),
2122 "Locking of files succeeded with OK status ($backendName) ($i)." );
2124 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2125 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2126 "Locking of files succeeded ($backendName) ($i)." );
2127 $this->assertEquals( true, $status->isOK(),
2128 "Locking of files succeeded with OK status ($backendName) ($i)." );
2130 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2131 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2132 "Locking of files succeeded ($backendName) ($i)." );
2133 $this->assertEquals( true, $status->isOK(),
2134 "Locking of files succeeded with OK status ($backendName) ($i)." );
2136 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2137 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2138 "Locking of files succeeded ($backendName). ($i)" );
2139 $this->assertEquals( true, $status->isOK(),
2140 "Locking of files succeeded with OK status ($backendName) ($i)." );
2142 ## Flip the acquire/release ordering around ##
2144 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2145 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2146 "Locking of files succeeded ($backendName) ($i)." );
2147 $this->assertEquals( true, $status->isOK(),
2148 "Locking of files succeeded with OK status ($backendName) ($i)." );
2150 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2151 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2152 "Locking of files succeeded ($backendName) ($i)." );
2153 $this->assertEquals( true, $status->isOK(),
2154 "Locking of files succeeded with OK status ($backendName) ($i)." );
2156 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2157 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2158 "Locking of files succeeded ($backendName). ($i)" );
2159 $this->assertEquals( true, $status->isOK(),
2160 "Locking of files succeeded with OK status ($backendName) ($i)." );
2162 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2163 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2164 "Locking of files succeeded ($backendName) ($i)." );
2165 $this->assertEquals( true, $status->isOK(),
2166 "Locking of files succeeded with OK status ($backendName) ($i)." );
2169 $status = Status
::newGood();
2170 $sl = $this->backend
->getScopedFileLocks( $paths, LockManager
::LOCK_EX
, $status );
2171 $this->assertType( 'ScopedLock', $sl,
2172 "Scoped locking of files succeeded ($backendName)." );
2173 $this->assertEquals( array(), $status->errors
,
2174 "Scoped locking of files succeeded ($backendName)." );
2175 $this->assertEquals( true, $status->isOK(),
2176 "Scoped locking of files succeeded with OK status ($backendName)." );
2178 ScopedLock
::release( $sl );
2179 $this->assertEquals( null, $sl,
2180 "Scoped unlocking of files succeeded ($backendName)." );
2181 $this->assertEquals( array(), $status->errors
,
2182 "Scoped unlocking of files succeeded ($backendName)." );
2183 $this->assertEquals( true, $status->isOK(),
2184 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2187 // test helper wrapper for backend prepare() function
2188 private function prepare( array $params ) {
2189 return $this->backend
->prepare( $params );
2192 // test helper wrapper for backend prepare() function
2193 private function create( array $params ) {
2194 $params['op'] = 'create';
2195 return $this->backend
->doQuickOperations( array( $params ) );
2198 function tearDownFiles() {
2199 foreach ( $this->filesToPrune
as $file ) {
2200 if ( is_file( $file ) ) {
2204 $containers = array( 'unittest-cont1', 'unittest-cont2' );
2205 foreach ( $containers as $container ) {
2206 $this->deleteFiles( $container );
2208 $this->filesToPrune
= array();
2211 private function deleteFiles( $container ) {
2212 $base = self
::baseStorePath();
2213 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
2215 foreach ( $iter as $file ) {
2216 $this->backend
->quickDelete( array( 'src' => "$base/$container/$file" ) );
2218 // free the directory, to avoid Permission denied under windows on rmdir
2221 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
2224 function assertBackendPathsConsistent( array $paths ) {
2225 if ( $this->backend
instanceof FileBackendMultiWrite
) {
2226 $status = $this->backend
->consistencyCheck( $paths );
2227 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2231 function assertGoodStatus( $status, $msg ) {
2232 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );