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)." );
322 $status = $this->backend
->doOperation(
323 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
324 $this->assertGoodStatus( $status,
325 "Creation of file at $source succeeded ($backendName)." );
327 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
328 $this->backend
->copy( $op );
331 $status = $this->backend
->doOperation( $op );
333 $this->assertGoodStatus( $status,
334 "Copy from $source to $dest succeeded without warnings ($backendName)." );
335 $this->assertEquals( true, $status->isOK(),
336 "Copy from $source to $dest succeeded ($backendName)." );
337 $this->assertEquals( array( 0 => true ), $status->success
,
338 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
339 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
340 "Source file $source still exists ($backendName)." );
341 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
342 "Destination file $dest exists after copy ($backendName)." );
345 $this->backend
->getFileSize( array( 'src' => $source ) ),
346 $this->backend
->getFileSize( array( 'src' => $dest ) ),
347 "Destination file $dest has correct size ($backendName)." );
349 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
350 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
351 $this->assertEquals( $props1, $props2,
352 "Source and destination have the same props ($backendName)." );
354 $this->assertBackendPathsConsistent( array( $source, $dest ) );
357 public static function provider_testCopy() {
360 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
361 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
363 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
371 $op2['overwrite'] = true;
379 $op2['overwriteSame'] = true;
387 $op2['ignoreMissingSource'] = true;
395 $op2['ignoreMissingSource'] = true;
398 self
::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
406 * @dataProvider provider_testMove
408 public function testMove( $op ) {
409 $this->backend
= $this->singleBackend
;
410 $this->tearDownFiles();
411 $this->doTestMove( $op );
412 $this->tearDownFiles();
414 $this->backend
= $this->multiBackend
;
415 $this->tearDownFiles();
416 $this->doTestMove( $op );
417 $this->tearDownFiles();
420 private function doTestMove( $op ) {
421 $backendName = $this->backendClass();
423 $source = $op['src'];
425 $this->prepare( array( 'dir' => dirname( $source ) ) );
426 $this->prepare( array( 'dir' => dirname( $dest ) ) );
428 if ( isset( $op['ignoreMissingSource'] ) ) {
429 $status = $this->backend
->doOperation( $op );
430 $this->assertGoodStatus( $status,
431 "Move from $source to $dest succeeded without warnings ($backendName)." );
432 $this->assertEquals( array( 0 => true ), $status->success
,
433 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
434 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
435 "Source file $source does not exist ($backendName)." );
436 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $dest ) ),
437 "Destination file $dest does not exist ($backendName)." );
442 $status = $this->backend
->doOperation(
443 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
444 $this->assertGoodStatus( $status,
445 "Creation of file at $source succeeded ($backendName)." );
447 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
448 $this->backend
->copy( $op );
451 $status = $this->backend
->doOperation( $op );
452 $this->assertGoodStatus( $status,
453 "Move from $source to $dest succeeded without warnings ($backendName)." );
454 $this->assertEquals( true, $status->isOK(),
455 "Move from $source to $dest succeeded ($backendName)." );
456 $this->assertEquals( array( 0 => true ), $status->success
,
457 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
458 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
459 "Source file $source does not still exists ($backendName)." );
460 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
461 "Destination file $dest exists after move ($backendName)." );
463 $this->assertNotEquals(
464 $this->backend
->getFileSize( array( 'src' => $source ) ),
465 $this->backend
->getFileSize( array( 'src' => $dest ) ),
466 "Destination file $dest has correct size ($backendName)." );
468 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
469 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
470 $this->assertEquals( false, $props1['fileExists'],
471 "Source file does not exist accourding to props ($backendName)." );
472 $this->assertEquals( true, $props2['fileExists'],
473 "Destination file exists accourding to props ($backendName)." );
475 $this->assertBackendPathsConsistent( array( $source, $dest ) );
478 public static function provider_testMove() {
481 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
482 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
484 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
492 $op2['overwrite'] = true;
500 $op2['overwriteSame'] = true;
508 $op2['ignoreMissingSource'] = true;
516 $op2['ignoreMissingSource'] = true;
519 self
::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
527 * @dataProvider provider_testDelete
529 public function testDelete( $op, $withSource, $okStatus ) {
530 $this->backend
= $this->singleBackend
;
531 $this->tearDownFiles();
532 $this->doTestDelete( $op, $withSource, $okStatus );
533 $this->tearDownFiles();
535 $this->backend
= $this->multiBackend
;
536 $this->tearDownFiles();
537 $this->doTestDelete( $op, $withSource, $okStatus );
538 $this->tearDownFiles();
541 private function doTestDelete( $op, $withSource, $okStatus ) {
542 $backendName = $this->backendClass();
544 $source = $op['src'];
545 $this->prepare( array( 'dir' => dirname( $source ) ) );
548 $status = $this->backend
->doOperation(
549 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
550 $this->assertGoodStatus( $status,
551 "Creation of file at $source succeeded ($backendName)." );
554 $status = $this->backend
->doOperation( $op );
556 $this->assertGoodStatus( $status,
557 "Deletion of file at $source succeeded without warnings ($backendName)." );
558 $this->assertEquals( true, $status->isOK(),
559 "Deletion of file at $source succeeded ($backendName)." );
560 $this->assertEquals( array( 0 => true ), $status->success
,
561 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
563 $this->assertEquals( false, $status->isOK(),
564 "Deletion of file at $source failed ($backendName)." );
567 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
568 "Source file $source does not exist after move ($backendName)." );
571 $this->backend
->getFileSize( array( 'src' => $source ) ),
572 "Source file $source has correct size (false) ($backendName)." );
574 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
575 $this->assertFalse( $props1['fileExists'],
576 "Source file $source does not exist according to props ($backendName)." );
578 $this->assertBackendPathsConsistent( array( $source ) );
581 public static function provider_testDelete() {
584 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
586 $op = array( 'op' => 'delete', 'src' => $source );
595 false, // without source
599 $op['ignoreMissingSource'] = true;
602 false, // without source
606 $op['ignoreMissingSource'] = true;
607 $op['src'] = self
::baseStorePath() . '/unittest-cont-bad/e/file.txt';
610 false, // without source
618 * @dataProvider provider_testDescribe
620 public function testDescribe( $op, $withSource, $okStatus ) {
621 $this->backend
= $this->singleBackend
;
622 $this->tearDownFiles();
623 $this->doTestDescribe( $op, $withSource, $okStatus );
624 $this->tearDownFiles();
626 $this->backend
= $this->multiBackend
;
627 $this->tearDownFiles();
628 $this->doTestDescribe( $op, $withSource, $okStatus );
629 $this->tearDownFiles();
632 private function doTestDescribe( $op, $withSource, $okStatus ) {
633 $backendName = $this->backendClass();
635 $source = $op['src'];
636 $this->prepare( array( 'dir' => dirname( $source ) ) );
639 $status = $this->backend
->doOperation(
640 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
641 $this->assertGoodStatus( $status,
642 "Creation of file at $source succeeded ($backendName)." );
645 $status = $this->backend
->doOperation( $op );
647 $this->assertGoodStatus( $status,
648 "Describe of file at $source succeeded without warnings ($backendName)." );
649 $this->assertEquals( true, $status->isOK(),
650 "Describe of file at $source succeeded ($backendName)." );
651 $this->assertEquals( array( 0 => true ), $status->success
,
652 "Describe of file at $source has proper 'success' field in Status ($backendName)." );
654 $this->assertEquals( false, $status->isOK(),
655 "Describe of file at $source failed ($backendName)." );
658 $this->assertBackendPathsConsistent( array( $source ) );
661 public static function provider_testDescribe() {
664 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
666 $op = array( 'op' => 'describe', 'src' => $source,
667 'headers' => array( 'X-Content-Length' => '91.3', 'Content-Old-Header' => '' ),
668 'disposition' => 'inline' );
677 false, // without source
685 * @dataProvider provider_testCreate
687 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
688 $this->backend
= $this->singleBackend
;
689 $this->tearDownFiles();
690 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
691 $this->tearDownFiles();
693 $this->backend
= $this->multiBackend
;
694 $this->tearDownFiles();
695 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
696 $this->tearDownFiles();
699 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
700 $backendName = $this->backendClass();
703 $this->prepare( array( 'dir' => dirname( $dest ) ) );
705 $oldText = 'blah...blah...waahwaah';
706 if ( $alreadyExists ) {
707 $status = $this->backend
->doOperation(
708 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
709 $this->assertGoodStatus( $status,
710 "Creation of file at $dest succeeded ($backendName)." );
713 $status = $this->backend
->doOperation( $op );
715 $this->assertGoodStatus( $status,
716 "Creation of file at $dest succeeded without warnings ($backendName)." );
717 $this->assertEquals( true, $status->isOK(),
718 "Creation of file at $dest succeeded ($backendName)." );
719 $this->assertEquals( array( 0 => true ), $status->success
,
720 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
722 $this->assertEquals( false, $status->isOK(),
723 "Creation of file at $dest failed ($backendName)." );
726 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
727 "Destination file $dest exists after creation ($backendName)." );
729 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
730 $this->assertEquals( true, $props1['fileExists'],
731 "Destination file $dest exists according to props ($backendName)." );
732 if ( $okStatus ) { // file content is what we saved
733 $this->assertEquals( $newSize, $props1['size'],
734 "Destination file $dest has expected size according to props ($backendName)." );
735 $this->assertEquals( $newSize,
736 $this->backend
->getFileSize( array( 'src' => $dest ) ),
737 "Destination file $dest has correct size ($backendName)." );
738 } else { // file content is some other previous text
739 $this->assertEquals( strlen( $oldText ), $props1['size'],
740 "Destination file $dest has original size according to props ($backendName)." );
741 $this->assertEquals( strlen( $oldText ),
742 $this->backend
->getFileSize( array( 'src' => $dest ) ),
743 "Destination file $dest has original size according to props ($backendName)." );
746 $this->assertBackendPathsConsistent( array( $dest ) );
750 * @dataProvider provider_testCreate
752 public static function provider_testCreate() {
755 $dest = self
::baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
757 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
760 false, // no dest already exists
762 strlen( $op['content'] )
766 $op2['content'] = "\n";
769 false, // no dest already exists
771 strlen( $op2['content'] )
775 $op2['content'] = "fsf\n waf 3kt";
778 true, // dest already exists
780 strlen( $op2['content'] )
784 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
785 $op2['overwrite'] = true;
788 true, // dest already exists
790 strlen( $op2['content'] )
794 $op2['content'] = "39qjmg3-qg";
795 $op2['overwriteSame'] = true;
798 true, // dest already exists
800 strlen( $op2['content'] )
806 public function testDoQuickOperations() {
807 $this->backend
= $this->singleBackend
;
808 $this->doTestDoQuickOperations();
809 $this->tearDownFiles();
811 $this->backend
= $this->multiBackend
;
812 $this->doTestDoQuickOperations();
813 $this->tearDownFiles();
816 private function doTestDoQuickOperations() {
817 $backendName = $this->backendClass();
819 $base = self
::baseStorePath();
821 "$base/unittest-cont1/e/fileA.a",
822 "$base/unittest-cont1/e/fileB.a",
823 "$base/unittest-cont1/e/fileC.a"
825 $createOps = array();
827 foreach ( $files as $path ) {
828 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
829 $this->assertGoodStatus( $status,
830 "Preparing $path succeeded without warnings ($backendName)." );
831 $createOps[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand( 0, 50000 ) );
832 $copyOps[] = array( 'op' => 'copy', 'src' => $path, 'dst' => "$path-2" );
833 $moveOps[] = array( 'op' => 'move', 'src' => "$path-2", 'dst' => "$path-3" );
834 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
835 $purgeOps[] = array( 'op' => 'delete', 'src' => "$path-3" );
837 $purgeOps[] = array( 'op' => 'null' );
839 $this->assertGoodStatus(
840 $this->backend
->doQuickOperations( $createOps ),
841 "Creation of source files succeeded ($backendName)." );
842 foreach ( $files as $file ) {
843 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $file ) ),
844 "File $file exists." );
847 $this->assertGoodStatus(
848 $this->backend
->doQuickOperations( $copyOps ),
849 "Quick copy of source files succeeded ($backendName)." );
850 foreach ( $files as $file ) {
851 $this->assertTrue( $this->backend
->fileExists( array( 'src' => "$file-2" ) ),
852 "File $file-2 exists." );
855 $this->assertGoodStatus(
856 $this->backend
->doQuickOperations( $moveOps ),
857 "Quick move of source files succeeded ($backendName)." );
858 foreach ( $files as $file ) {
859 $this->assertTrue( $this->backend
->fileExists( array( 'src' => "$file-3" ) ),
860 "File $file-3 move in." );
861 $this->assertFalse( $this->backend
->fileExists( array( 'src' => "$file-2" ) ),
862 "File $file-2 moved away." );
865 $this->assertGoodStatus(
866 $this->backend
->quickCopy( array( 'src' => $files[0], 'dst' => $files[0] ) ),
867 "Copy of file {$files[0]} over itself succeeded ($backendName)." );
868 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $files[0] ) ),
869 "File {$files[0]} still exists." );
871 $this->assertGoodStatus(
872 $this->backend
->quickMove( array( 'src' => $files[0], 'dst' => $files[0] ) ),
873 "Move of file {$files[0]} over itself succeeded ($backendName)." );
874 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $files[0] ) ),
875 "File {$files[0]} still exists." );
877 $this->assertGoodStatus(
878 $this->backend
->doQuickOperations( $purgeOps ),
879 "Quick deletion of source files succeeded ($backendName)." );
880 foreach ( $files as $file ) {
881 $this->assertFalse( $this->backend
->fileExists( array( 'src' => $file ) ),
882 "File $file purged." );
883 $this->assertFalse( $this->backend
->fileExists( array( 'src' => "$file-3" ) ),
884 "File $file-3 purged." );
889 * @dataProvider provider_testConcatenate
891 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
892 $this->filesToPrune
[] = $op['dst'];
894 $this->backend
= $this->singleBackend
;
895 $this->tearDownFiles();
896 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
897 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
898 $this->tearDownFiles();
900 $this->backend
= $this->multiBackend
;
901 $this->tearDownFiles();
902 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
903 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
904 $this->tearDownFiles();
907 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
908 $backendName = $this->backendClass();
913 foreach ( $srcs as $i => $source ) {
914 $this->prepare( array( 'dir' => dirname( $source ) ) );
916 'op' => 'create', // operation
917 'dst' => $source, // source
918 'content' => $srcsContent[$i]
920 $expContent .= $srcsContent[$i];
922 $status = $this->backend
->doOperations( $ops );
924 $this->assertGoodStatus( $status,
925 "Creation of source files succeeded ($backendName)." );
927 $dest = $params['dst'];
928 if ( $alreadyExists ) {
929 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
930 $this->assertEquals( true, $ok,
931 "Creation of file at $dest succeeded ($backendName)." );
933 $ok = file_put_contents( $dest, '' ) !== false;
934 $this->assertEquals( true, $ok,
935 "Creation of 0-byte file at $dest succeeded ($backendName)." );
938 // Combine the files into one
939 $status = $this->backend
->concatenate( $params );
941 $this->assertGoodStatus( $status,
942 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
943 $this->assertEquals( true, $status->isOK(),
944 "Creation of concat file at $dest succeeded ($backendName)." );
946 $this->assertEquals( false, $status->isOK(),
947 "Creation of concat file at $dest failed ($backendName)." );
951 $this->assertEquals( true, is_file( $dest ),
952 "Dest concat file $dest exists after creation ($backendName)." );
954 $this->assertEquals( true, is_file( $dest ),
955 "Dest concat file $dest exists after failed creation ($backendName)." );
958 $contents = file_get_contents( $dest );
959 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
962 $this->assertEquals( $expContent, $contents,
963 "Concat file at $dest has correct contents ($backendName)." );
965 $this->assertNotEquals( $expContent, $contents,
966 "Concat file at $dest has correct contents ($backendName)." );
970 public static function provider_testConcatenate() {
973 $rand = mt_rand( 0, 2000000000 ) . time();
974 $dest = wfTempDir() . "/randomfile!$rand.txt";
976 self
::baseStorePath() . '/unittest-cont1/e/file1.txt',
977 self
::baseStorePath() . '/unittest-cont1/e/file2.txt',
978 self
::baseStorePath() . '/unittest-cont1/e/file3.txt',
979 self
::baseStorePath() . '/unittest-cont1/e/file4.txt',
980 self
::baseStorePath() . '/unittest-cont1/e/file5.txt',
981 self
::baseStorePath() . '/unittest-cont1/e/file6.txt',
982 self
::baseStorePath() . '/unittest-cont1/e/file7.txt',
983 self
::baseStorePath() . '/unittest-cont1/e/file8.txt',
984 self
::baseStorePath() . '/unittest-cont1/e/file9.txt',
985 self
::baseStorePath() . '/unittest-cont1/e/file10.txt'
999 $params = array( 'srcs' => $srcs, 'dst' => $dest );
1002 $params, // operation
1004 $content, // content for each source
1005 false, // no dest already exists
1010 $params, // operation
1012 $content, // content for each source
1013 true, // dest already exists
1021 * @dataProvider provider_testGetFileStat
1023 public function testGetFileStat( $path, $content, $alreadyExists ) {
1024 $this->backend
= $this->singleBackend
;
1025 $this->tearDownFiles();
1026 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1027 $this->tearDownFiles();
1029 $this->backend
= $this->multiBackend
;
1030 $this->tearDownFiles();
1031 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1032 $this->tearDownFiles();
1035 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
1036 $backendName = $this->backendClass();
1038 if ( $alreadyExists ) {
1039 $this->prepare( array( 'dir' => dirname( $path ) ) );
1040 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
1041 $this->assertGoodStatus( $status,
1042 "Creation of file at $path succeeded ($backendName)." );
1044 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1045 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1046 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
1048 $this->assertEquals( strlen( $content ), $size,
1049 "Correct file size of '$path'" );
1050 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1051 "Correct file timestamp of '$path'" );
1053 $size = $stat['size'];
1054 $time = $stat['mtime'];
1055 $this->assertEquals( strlen( $content ), $size,
1056 "Correct file size of '$path'" );
1057 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1058 "Correct file timestamp of '$path'" );
1060 $this->backend
->clearCache( array( $path ) );
1062 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1064 $this->assertEquals( strlen( $content ), $size,
1065 "Correct file size of '$path'" );
1067 $this->backend
->preloadCache( array( $path ) );
1069 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1071 $this->assertEquals( strlen( $content ), $size,
1072 "Correct file size of '$path'" );
1074 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1075 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1076 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
1078 $this->assertFalse( $size, "Correct file size of '$path'" );
1079 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1080 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1084 public static function provider_testGetFileStat() {
1087 $base = self
::baseStorePath();
1088 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
1089 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
1090 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
1096 * @dataProvider provider_testGetFileContents
1098 public function testGetFileContents( $source, $content ) {
1099 $this->backend
= $this->singleBackend
;
1100 $this->tearDownFiles();
1101 $this->doTestGetFileContents( $source, $content );
1102 $this->tearDownFiles();
1104 $this->backend
= $this->multiBackend
;
1105 $this->tearDownFiles();
1106 $this->doTestGetFileContents( $source, $content );
1107 $this->tearDownFiles();
1110 private function doTestGetFileContents( $source, $content ) {
1111 $backendName = $this->backendClass();
1113 $srcs = (array)$source;
1114 $content = (array)$content;
1115 foreach ( $srcs as $i => $src ) {
1116 $this->prepare( array( 'dir' => dirname( $src ) ) );
1117 $status = $this->backend
->doOperation(
1118 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1119 $this->assertGoodStatus( $status,
1120 "Creation of file at $src succeeded ($backendName)." );
1123 if ( is_array( $source ) ) {
1124 $contents = $this->backend
->getFileContentsMulti( array( 'srcs' => $source ) );
1125 foreach ( $contents as $path => $data ) {
1126 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1127 $this->assertEquals( current( $content ), $data, "Contents of $path is correct ($backendName)." );
1130 $this->assertEquals( $source, array_keys( $contents ), "Contents in right order ($backendName)." );
1131 $this->assertEquals( count( $source ), count( $contents ), "Contents array size correct ($backendName)." );
1133 $data = $this->backend
->getFileContents( array( 'src' => $source ) );
1134 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1135 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1139 public static function provider_testGetFileContents() {
1142 $base = self
::baseStorePath();
1143 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
1144 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
1146 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1147 "$base/unittest-cont1/e/a/z.txt" ),
1148 array( "contents xx", "contents xy", "contents xz" )
1155 * @dataProvider provider_testGetLocalCopy
1157 public function testGetLocalCopy( $source, $content ) {
1158 $this->backend
= $this->singleBackend
;
1159 $this->tearDownFiles();
1160 $this->doTestGetLocalCopy( $source, $content );
1161 $this->tearDownFiles();
1163 $this->backend
= $this->multiBackend
;
1164 $this->tearDownFiles();
1165 $this->doTestGetLocalCopy( $source, $content );
1166 $this->tearDownFiles();
1169 private function doTestGetLocalCopy( $source, $content ) {
1170 $backendName = $this->backendClass();
1172 $srcs = (array)$source;
1173 $content = (array)$content;
1174 foreach ( $srcs as $i => $src ) {
1175 $this->prepare( array( 'dir' => dirname( $src ) ) );
1176 $status = $this->backend
->doOperation(
1177 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1178 $this->assertGoodStatus( $status,
1179 "Creation of file at $src succeeded ($backendName)." );
1182 if ( is_array( $source ) ) {
1183 $tmpFiles = $this->backend
->getLocalCopyMulti( array( 'srcs' => $source ) );
1184 foreach ( $tmpFiles as $path => $tmpFile ) {
1185 $this->assertNotNull( $tmpFile,
1186 "Creation of local copy of $path succeeded ($backendName)." );
1187 $contents = file_get_contents( $tmpFile->getPath() );
1188 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1189 $this->assertEquals( current( $content ), $contents, "Local copy of $path is correct ($backendName)." );
1192 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local copies in right order ($backendName)." );
1193 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local copies array size correct ($backendName)." );
1195 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
1196 $this->assertNotNull( $tmpFile,
1197 "Creation of local copy of $source succeeded ($backendName)." );
1198 $contents = file_get_contents( $tmpFile->getPath() );
1199 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1200 $this->assertEquals( $content[0], $contents, "Local copy of $source is correct ($backendName)." );
1203 $obj = new stdClass();
1204 $tmpFile->bind( $obj );
1207 public static function provider_testGetLocalCopy() {
1210 $base = self
::baseStorePath();
1211 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1212 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1213 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1215 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1216 "$base/unittest-cont1/e/a/z.txt" ),
1217 array( "contents xx", "contents xy", "contents xz" )
1224 * @dataProvider provider_testGetLocalReference
1226 public function testGetLocalReference( $source, $content ) {
1227 $this->backend
= $this->singleBackend
;
1228 $this->tearDownFiles();
1229 $this->doTestGetLocalReference( $source, $content );
1230 $this->tearDownFiles();
1232 $this->backend
= $this->multiBackend
;
1233 $this->tearDownFiles();
1234 $this->doTestGetLocalReference( $source, $content );
1235 $this->tearDownFiles();
1238 private function doTestGetLocalReference( $source, $content ) {
1239 $backendName = $this->backendClass();
1241 $srcs = (array)$source;
1242 $content = (array)$content;
1243 foreach ( $srcs as $i => $src ) {
1244 $this->prepare( array( 'dir' => dirname( $src ) ) );
1245 $status = $this->backend
->doOperation(
1246 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1247 $this->assertGoodStatus( $status,
1248 "Creation of file at $src succeeded ($backendName)." );
1251 if ( is_array( $source ) ) {
1252 $tmpFiles = $this->backend
->getLocalReferenceMulti( array( 'srcs' => $source ) );
1253 foreach ( $tmpFiles as $path => $tmpFile ) {
1254 $this->assertNotNull( $tmpFile,
1255 "Creation of local copy of $path succeeded ($backendName)." );
1256 $contents = file_get_contents( $tmpFile->getPath() );
1257 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1258 $this->assertEquals( current( $content ), $contents, "Local ref of $path is correct ($backendName)." );
1261 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local refs in right order ($backendName)." );
1262 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local refs array size correct ($backendName)." );
1264 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
1265 $this->assertNotNull( $tmpFile,
1266 "Creation of local copy of $source succeeded ($backendName)." );
1267 $contents = file_get_contents( $tmpFile->getPath() );
1268 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1269 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1273 public static function provider_testGetLocalReference() {
1276 $base = self
::baseStorePath();
1277 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1278 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1279 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1281 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1282 "$base/unittest-cont1/e/a/z.txt" ),
1283 array( "contents xx", "contents xy", "contents xz" )
1289 public function testGetLocalCopyAndReference404() {
1290 $this->backend
= $this->singleBackend
;
1291 $this->tearDownFiles();
1292 $this->doTestGetLocalCopyAndReference404();
1293 $this->tearDownFiles();
1295 $this->backend
= $this->multiBackend
;
1296 $this->tearDownFiles();
1297 $this->doTestGetLocalCopyAndReference404();
1298 $this->tearDownFiles();
1301 public function doTestGetLocalCopyAndReference404() {
1302 $backendName = $this->backendClass();
1304 $base = self
::baseStorePath();
1306 $tmpFile = $this->backend
->getLocalCopy( array(
1307 'src' => "$base/unittest-cont1/not-there" ) );
1308 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1310 $tmpFile = $this->backend
->getLocalReference( array(
1311 'src' => "$base/unittest-cont1/not-there" ) );
1312 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1316 * @dataProvider provider_testGetFileHttpUrl
1318 public function testGetFileHttpUrl( $source, $content ) {
1319 $this->backend
= $this->singleBackend
;
1320 $this->tearDownFiles();
1321 $this->doTestGetFileHttpUrl( $source, $content );
1322 $this->tearDownFiles();
1324 $this->backend
= $this->multiBackend
;
1325 $this->tearDownFiles();
1326 $this->doTestGetFileHttpUrl( $source, $content );
1327 $this->tearDownFiles();
1330 private function doTestGetFileHttpUrl( $source, $content ) {
1331 $backendName = $this->backendClass();
1333 $this->prepare( array( 'dir' => dirname( $source ) ) );
1334 $status = $this->backend
->doOperation(
1335 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
1336 $this->assertGoodStatus( $status,
1337 "Creation of file at $source succeeded ($backendName)." );
1339 $url = $this->backend
->getFileHttpUrl( array( 'src' => $source ) );
1341 if ( $url !== null ) { // supported
1342 $data = Http
::request( "GET", $url );
1343 $this->assertEquals( $content, $data,
1344 "HTTP GET of URL has right contents ($backendName)." );
1348 public static function provider_testGetFileHttpUrl() {
1351 $base = self
::baseStorePath();
1352 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1353 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1354 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1360 * @dataProvider provider_testPrepareAndClean
1362 public function testPrepareAndClean( $path, $isOK ) {
1363 $this->backend
= $this->singleBackend
;
1364 $this->doTestPrepareAndClean( $path, $isOK );
1365 $this->tearDownFiles();
1367 $this->backend
= $this->multiBackend
;
1368 $this->doTestPrepareAndClean( $path, $isOK );
1369 $this->tearDownFiles();
1372 public static function provider_testPrepareAndClean() {
1373 $base = self
::baseStorePath();
1376 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1377 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1378 # Specific to FS backend with no basePath field set
1379 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1383 private function doTestPrepareAndClean( $path, $isOK ) {
1384 $backendName = $this->backendClass();
1386 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1388 $this->assertGoodStatus( $status,
1389 "Preparing dir $path succeeded without warnings ($backendName)." );
1390 $this->assertEquals( true, $status->isOK(),
1391 "Preparing dir $path succeeded ($backendName)." );
1393 $this->assertEquals( false, $status->isOK(),
1394 "Preparing dir $path failed ($backendName)." );
1397 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1399 $this->assertGoodStatus( $status,
1400 "Cleaning dir $path succeeded without warnings ($backendName)." );
1401 $this->assertEquals( true, $status->isOK(),
1402 "Cleaning dir $path succeeded ($backendName)." );
1404 $this->assertEquals( false, $status->isOK(),
1405 "Cleaning dir $path failed ($backendName)." );
1409 public function testRecursiveClean() {
1410 $this->backend
= $this->singleBackend
;
1411 $this->doTestRecursiveClean();
1412 $this->tearDownFiles();
1414 $this->backend
= $this->multiBackend
;
1415 $this->doTestRecursiveClean();
1416 $this->tearDownFiles();
1419 private function doTestRecursiveClean() {
1420 $backendName = $this->backendClass();
1422 $base = self
::baseStorePath();
1424 "$base/unittest-cont1",
1425 "$base/unittest-cont1/e",
1426 "$base/unittest-cont1/e/a",
1427 "$base/unittest-cont1/e/a/b",
1428 "$base/unittest-cont1/e/a/b/c",
1429 "$base/unittest-cont1/e/a/b/c/d0",
1430 "$base/unittest-cont1/e/a/b/c/d1",
1431 "$base/unittest-cont1/e/a/b/c/d2",
1432 "$base/unittest-cont1/e/a/b/c/d0/1",
1433 "$base/unittest-cont1/e/a/b/c/d0/2",
1434 "$base/unittest-cont1/e/a/b/c/d1/3",
1435 "$base/unittest-cont1/e/a/b/c/d1/4",
1436 "$base/unittest-cont1/e/a/b/c/d2/5",
1437 "$base/unittest-cont1/e/a/b/c/d2/6"
1439 foreach ( $dirs as $dir ) {
1440 $status = $this->prepare( array( 'dir' => $dir ) );
1441 $this->assertGoodStatus( $status,
1442 "Preparing dir $dir succeeded without warnings ($backendName)." );
1445 if ( $this->backend
instanceof FSFileBackend
) {
1446 foreach ( $dirs as $dir ) {
1447 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1448 "Dir $dir exists ($backendName)." );
1452 $status = $this->backend
->clean(
1453 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1454 $this->assertGoodStatus( $status,
1455 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1457 foreach ( $dirs as $dir ) {
1458 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1459 "Dir $dir no longer exists ($backendName)." );
1465 public function testDoOperations() {
1466 $this->backend
= $this->singleBackend
;
1467 $this->tearDownFiles();
1468 $this->doTestDoOperations();
1469 $this->tearDownFiles();
1471 $this->backend
= $this->multiBackend
;
1472 $this->tearDownFiles();
1473 $this->doTestDoOperations();
1474 $this->tearDownFiles();
1477 private function doTestDoOperations() {
1478 $base = self
::baseStorePath();
1480 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1481 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1482 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1483 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1484 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1485 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1486 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1488 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1489 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1490 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1491 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1492 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1493 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1494 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1496 $status = $this->backend
->doOperations( array(
1497 array( 'op' => 'describe', 'src' => $fileA,
1498 'headers' => array( 'X-Content-Length' => '91.3' ), 'disposition' => 'inline' ),
1499 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1500 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1501 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1502 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1503 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1504 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1505 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1506 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1507 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1508 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1509 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1510 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1511 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1512 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1513 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1514 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1515 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1517 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1519 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1521 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1523 array( 'op' => 'null' ),
1527 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1528 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1529 $this->assertEquals( 14, count( $status->success
),
1530 "Operation batch has correct success array" );
1532 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1533 "File does not exist at $fileA" );
1534 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1535 "File does not exist at $fileB" );
1536 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1537 "File does not exist at $fileD" );
1539 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1540 "File exists at $fileC" );
1541 $this->assertEquals( $fileBContents,
1542 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1543 "Correct file contents of $fileC" );
1544 $this->assertEquals( strlen( $fileBContents ),
1545 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1546 "Correct file size of $fileC" );
1547 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1548 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1549 "Correct file SHA-1 of $fileC" );
1552 public function testDoOperationsPipeline() {
1553 $this->backend
= $this->singleBackend
;
1554 $this->tearDownFiles();
1555 $this->doTestDoOperationsPipeline();
1556 $this->tearDownFiles();
1558 $this->backend
= $this->multiBackend
;
1559 $this->tearDownFiles();
1560 $this->doTestDoOperationsPipeline();
1561 $this->tearDownFiles();
1564 // concurrency orientated
1565 private function doTestDoOperationsPipeline() {
1566 $base = self
::baseStorePath();
1568 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1569 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1570 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1572 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1573 file_put_contents( $tmpNameA, $fileAContents );
1574 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1575 file_put_contents( $tmpNameB, $fileBContents );
1576 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1577 file_put_contents( $tmpNameC, $fileCContents );
1579 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1580 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1581 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1583 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1584 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1585 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1586 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1588 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1589 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1590 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1591 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1592 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1594 $status = $this->backend
->doOperations( array(
1595 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1596 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1597 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1598 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1599 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1600 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1601 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1602 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1603 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1604 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1605 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1606 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1607 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1608 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1609 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1610 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1611 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1612 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1613 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1614 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1616 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1618 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1620 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1622 array( 'op' => 'null' ),
1626 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1627 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1628 $this->assertEquals( 16, count( $status->success
),
1629 "Operation batch has correct success array" );
1631 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1632 "File does not exist at $fileA" );
1633 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1634 "File does not exist at $fileB" );
1635 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1636 "File does not exist at $fileD" );
1638 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1639 "File exists at $fileC" );
1640 $this->assertEquals( $fileBContents,
1641 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1642 "Correct file contents of $fileC" );
1643 $this->assertEquals( strlen( $fileBContents ),
1644 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1645 "Correct file size of $fileC" );
1646 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1647 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1648 "Correct file SHA-1 of $fileC" );
1651 public function testDoOperationsFailing() {
1652 $this->backend
= $this->singleBackend
;
1653 $this->tearDownFiles();
1654 $this->doTestDoOperationsFailing();
1655 $this->tearDownFiles();
1657 $this->backend
= $this->multiBackend
;
1658 $this->tearDownFiles();
1659 $this->doTestDoOperationsFailing();
1660 $this->tearDownFiles();
1663 private function doTestDoOperationsFailing() {
1664 $base = self
::baseStorePath();
1666 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1667 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1668 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1669 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1670 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1671 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1672 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1674 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1675 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1676 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1677 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1678 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1679 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1681 $status = $this->backend
->doOperations( array(
1682 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1683 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1684 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1685 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1686 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1687 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1688 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1689 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1690 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1691 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1692 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1693 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1694 array( 'op' => 'delete', 'src' => $fileD ),
1695 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1696 array( 'op' => 'null' ),
1698 ), array( 'force' => 1 ) );
1700 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1701 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1702 $this->assertEquals( 8, count( $status->success
),
1703 "Operation batch has correct success array" );
1705 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1706 "File does not exist at $fileB" );
1707 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1708 "File does not exist at $fileD" );
1710 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1711 "File does not exist at $fileA" );
1712 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1713 "File exists at $fileC" );
1714 $this->assertEquals( $fileBContents,
1715 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1716 "Correct file contents of $fileA" );
1717 $this->assertEquals( strlen( $fileBContents ),
1718 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1719 "Correct file size of $fileA" );
1720 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1721 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1722 "Correct file SHA-1 of $fileA" );
1725 public function testGetFileList() {
1726 $this->backend
= $this->singleBackend
;
1727 $this->tearDownFiles();
1728 $this->doTestGetFileList();
1729 $this->tearDownFiles();
1731 $this->backend
= $this->multiBackend
;
1732 $this->tearDownFiles();
1733 $this->doTestGetFileList();
1734 $this->tearDownFiles();
1737 private function doTestGetFileList() {
1738 $backendName = $this->backendClass();
1739 $base = self
::baseStorePath();
1741 // Should have no errors
1742 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1745 "$base/unittest-cont1/e/test1.txt",
1746 "$base/unittest-cont1/e/test2.txt",
1747 "$base/unittest-cont1/e/test3.txt",
1748 "$base/unittest-cont1/e/subdir1/test1.txt",
1749 "$base/unittest-cont1/e/subdir1/test2.txt",
1750 "$base/unittest-cont1/e/subdir2/test3.txt",
1751 "$base/unittest-cont1/e/subdir2/test4.txt",
1752 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1753 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1754 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1755 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1756 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1757 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1758 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1763 foreach ( $files as $file ) {
1764 $this->prepare( array( 'dir' => dirname( $file ) ) );
1765 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1767 $status = $this->backend
->doQuickOperations( $ops );
1768 $this->assertGoodStatus( $status,
1769 "Creation of files succeeded ($backendName)." );
1770 $this->assertEquals( true, $status->isOK(),
1771 "Creation of files succeeded with OK status ($backendName)." );
1773 // Expected listing at root
1778 "e/subdir1/test1.txt",
1779 "e/subdir1/test2.txt",
1780 "e/subdir2/test3.txt",
1781 "e/subdir2/test4.txt",
1782 "e/subdir2/subdir/test1.txt",
1783 "e/subdir2/subdir/test2.txt",
1784 "e/subdir2/subdir/test3.txt",
1785 "e/subdir2/subdir/test4.txt",
1786 "e/subdir2/subdir/test5.txt",
1787 "e/subdir2/subdir/sub/test0.txt",
1788 "e/subdir2/subdir/sub/120-px-file.txt",
1792 // Actual listing (no trailing slash) at root
1793 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1794 $list = $this->listToArray( $iter );
1796 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1798 // Actual listing (no trailing slash) at root with advise
1799 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1", 'adviseStat' => 1 ) );
1800 $list = $this->listToArray( $iter );
1802 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1804 // Actual listing (with trailing slash) at root
1806 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1807 foreach ( $iter as $file ) {
1811 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1813 // Expected listing at subdir
1821 "sub/120-px-file.txt",
1825 // Actual listing (no trailing slash) at subdir
1826 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1827 $list = $this->listToArray( $iter );
1829 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1831 // Actual listing (no trailing slash) at subdir with advise
1832 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir", 'adviseStat' => 1 ) );
1833 $list = $this->listToArray( $iter );
1835 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1837 // Actual listing (with trailing slash) at subdir
1839 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1840 foreach ( $iter as $file ) {
1844 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1846 // Actual listing (using iterator second time)
1847 $list = $this->listToArray( $iter );
1849 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1851 // Actual listing (top files only) at root
1852 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1" ) );
1853 $list = $this->listToArray( $iter );
1855 $this->assertEquals( array(), $list, "Correct top file listing ($backendName)." );
1857 // Expected listing (top files only) at subdir
1867 // Actual listing (top files only) at subdir
1868 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1869 $list = $this->listToArray( $iter );
1871 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1873 // Actual listing (top files only) at subdir with advise
1874 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir", 'adviseStat' => 1 ) );
1875 $list = $this->listToArray( $iter );
1877 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1879 foreach ( $files as $file ) { // clean up
1880 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1883 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1884 foreach ( $iter as $iter ) {
1889 public function testGetDirectoryList() {
1890 $this->backend
= $this->singleBackend
;
1891 $this->tearDownFiles();
1892 $this->doTestGetDirectoryList();
1893 $this->tearDownFiles();
1895 $this->backend
= $this->multiBackend
;
1896 $this->tearDownFiles();
1897 $this->doTestGetDirectoryList();
1898 $this->tearDownFiles();
1901 private function doTestGetDirectoryList() {
1902 $backendName = $this->backendClass();
1904 $base = self
::baseStorePath();
1906 "$base/unittest-cont1/e/test1.txt",
1907 "$base/unittest-cont1/e/test2.txt",
1908 "$base/unittest-cont1/e/test3.txt",
1909 "$base/unittest-cont1/e/subdir1/test1.txt",
1910 "$base/unittest-cont1/e/subdir1/test2.txt",
1911 "$base/unittest-cont1/e/subdir2/test3.txt",
1912 "$base/unittest-cont1/e/subdir2/test4.txt",
1913 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1914 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1915 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1916 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1917 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1918 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1919 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1924 foreach ( $files as $file ) {
1925 $this->prepare( array( 'dir' => dirname( $file ) ) );
1926 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1928 $status = $this->backend
->doQuickOperations( $ops );
1929 $this->assertGoodStatus( $status,
1930 "Creation of files succeeded ($backendName)." );
1931 $this->assertEquals( true, $status->isOK(),
1932 "Creation of files succeeded with OK status ($backendName)." );
1934 $this->assertEquals( true,
1935 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1936 "Directory exists in ($backendName)." );
1937 $this->assertEquals( true,
1938 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1939 "Directory exists in ($backendName)." );
1940 $this->assertEquals( false,
1941 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1942 "Directory does not exists in ($backendName)." );
1950 // Actual listing (no trailing slash)
1952 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1953 foreach ( $iter as $file ) {
1958 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1969 // Actual listing (no 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)." );
1979 // Actual listing (with trailing slash)
1981 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1982 foreach ( $iter as $file ) {
1987 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1995 // Actual listing (no 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 (with trailing slash)
2007 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
2008 foreach ( $iter as $file ) {
2013 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2015 // Actual listing (using iterator second time)
2017 foreach ( $iter as $file ) {
2022 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
2024 // Expected listing (recursive)
2034 "e/subdir4/subdir/sub",
2038 // Actual listing (recursive)
2040 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
2041 foreach ( $iter as $file ) {
2046 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2048 // Expected listing (recursive)
2055 // Actual listing (recursive)
2057 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
2058 foreach ( $iter as $file ) {
2063 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2065 // Actual listing (recursive, second time)
2067 foreach ( $iter as $file ) {
2072 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2074 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) );
2075 $items = $this->listToArray( $iter );
2076 $this->assertEquals( array(), $items, "Directory listing is empty." );
2078 foreach ( $files as $file ) { // clean up
2079 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
2082 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
2083 foreach ( $iter as $file ) {
2087 $items = $this->listToArray( $iter );
2088 $this->assertEquals( array(), $items, "Directory listing is empty." );
2090 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/not/exists" ) );
2091 $items = $this->listToArray( $iter );
2092 $this->assertEquals( array(), $items, "Directory listing is empty." );
2095 public function testLockCalls() {
2096 $this->backend
= $this->singleBackend
;
2097 $this->doTestLockCalls();
2100 private function doTestLockCalls() {
2101 $backendName = $this->backendClass();
2108 "subdir1", // duplicate
2109 "subdir1/test1.txt",
2110 "subdir1/test2.txt",
2112 "subdir2", // duplicate
2113 "subdir2/test3.txt",
2114 "subdir2/test4.txt",
2116 "subdir2/subdir/test1.txt",
2117 "subdir2/subdir/test2.txt",
2118 "subdir2/subdir/test3.txt",
2119 "subdir2/subdir/test4.txt",
2120 "subdir2/subdir/test5.txt",
2121 "subdir2/subdir/sub",
2122 "subdir2/subdir/sub/test0.txt",
2123 "subdir2/subdir/sub/120-px-file.txt",
2126 for ( $i = 0; $i < 25; $i++
) {
2127 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2128 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2129 "Locking of files succeeded ($backendName) ($i)." );
2130 $this->assertEquals( true, $status->isOK(),
2131 "Locking of files succeeded with OK status ($backendName) ($i)." );
2133 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2134 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2135 "Locking of files succeeded ($backendName) ($i)." );
2136 $this->assertEquals( true, $status->isOK(),
2137 "Locking of files succeeded with OK status ($backendName) ($i)." );
2139 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2140 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2141 "Locking of files succeeded ($backendName) ($i)." );
2142 $this->assertEquals( true, $status->isOK(),
2143 "Locking of files succeeded with OK status ($backendName) ($i)." );
2145 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2146 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2147 "Locking of files succeeded ($backendName). ($i)" );
2148 $this->assertEquals( true, $status->isOK(),
2149 "Locking of files succeeded with OK status ($backendName) ($i)." );
2151 ## Flip the acquire/release ordering around ##
2153 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2154 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2155 "Locking of files succeeded ($backendName) ($i)." );
2156 $this->assertEquals( true, $status->isOK(),
2157 "Locking of files succeeded with OK status ($backendName) ($i)." );
2159 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2160 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2161 "Locking of files succeeded ($backendName) ($i)." );
2162 $this->assertEquals( true, $status->isOK(),
2163 "Locking of files succeeded with OK status ($backendName) ($i)." );
2165 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2166 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2167 "Locking of files succeeded ($backendName). ($i)" );
2168 $this->assertEquals( true, $status->isOK(),
2169 "Locking of files succeeded with OK status ($backendName) ($i)." );
2171 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2172 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2173 "Locking of files succeeded ($backendName) ($i)." );
2174 $this->assertEquals( true, $status->isOK(),
2175 "Locking of files succeeded with OK status ($backendName) ($i)." );
2178 $status = Status
::newGood();
2179 $sl = $this->backend
->getScopedFileLocks( $paths, LockManager
::LOCK_EX
, $status );
2180 $this->assertType( 'ScopedLock', $sl,
2181 "Scoped locking of files succeeded ($backendName)." );
2182 $this->assertEquals( array(), $status->errors
,
2183 "Scoped locking of files succeeded ($backendName)." );
2184 $this->assertEquals( true, $status->isOK(),
2185 "Scoped locking of files succeeded with OK status ($backendName)." );
2187 ScopedLock
::release( $sl );
2188 $this->assertEquals( null, $sl,
2189 "Scoped unlocking of files succeeded ($backendName)." );
2190 $this->assertEquals( array(), $status->errors
,
2191 "Scoped unlocking of files succeeded ($backendName)." );
2192 $this->assertEquals( true, $status->isOK(),
2193 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2197 private function listToArray( $iter ) {
2198 return is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2201 // test helper wrapper for backend prepare() function
2202 private function prepare( array $params ) {
2203 return $this->backend
->prepare( $params );
2206 // test helper wrapper for backend prepare() function
2207 private function create( array $params ) {
2208 $params['op'] = 'create';
2210 return $this->backend
->doQuickOperations( array( $params ) );
2213 function tearDownFiles() {
2214 foreach ( $this->filesToPrune
as $file ) {
2215 if ( is_file( $file ) ) {
2219 $containers = array( 'unittest-cont1', 'unittest-cont2' );
2220 foreach ( $containers as $container ) {
2221 $this->deleteFiles( $container );
2223 $this->filesToPrune
= array();
2226 private function deleteFiles( $container ) {
2227 $base = self
::baseStorePath();
2228 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
2230 foreach ( $iter as $file ) {
2231 $this->backend
->quickDelete( array( 'src' => "$base/$container/$file" ) );
2233 // free the directory, to avoid Permission denied under windows on rmdir
2236 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
2239 function assertBackendPathsConsistent( array $paths ) {
2240 if ( $this->backend
instanceof FileBackendMultiWrite
) {
2241 $status = $this->backend
->consistencyCheck( $paths );
2242 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2246 function assertGoodStatus( $status, $msg ) {
2247 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );