8 class FileBackendTest
extends MediaWikiTestCase
{
10 /** @var FileBackend */
12 /** @var FileBackendMultiWrite */
13 private $multiBackend;
14 /** @var FSFileBackend */
15 public $singleBackend;
16 private $filesToPrune = array();
17 private static $backendToUse;
19 protected function setUp() {
20 global $wgFileBackends;
22 $uniqueId = time() . '-' . mt_rand();
23 $tmpPrefix = wfTempDir() . '/filebackend-unittest-' . $uniqueId;
24 if ( $this->getCliArg( 'use-filebackend=' ) ) {
25 if ( self
::$backendToUse ) {
26 $this->singleBackend
= self
::$backendToUse;
28 $name = $this->getCliArg( 'use-filebackend=' );
30 foreach ( $wgFileBackends as $conf ) {
31 if ( $conf['name'] == $name ) {
36 $useConfig['name'] = 'localtesting'; // swap name
37 $useConfig['shardViaHashLevels'] = array( // test sharding
38 'unittest-cont1' => array( 'levels' => 1, 'base' => 16, 'repeat' => 1 )
40 $class = $useConfig['class'];
41 self
::$backendToUse = new $class( $useConfig );
42 $this->singleBackend
= self
::$backendToUse;
45 $this->singleBackend
= new FSFileBackend( array(
46 'name' => 'localtesting',
47 'lockManager' => 'fsLockManager',
48 #'parallelize' => 'implicit',
49 'wikiId' => wfWikiID() . $uniqueId,
50 'containerPaths' => array(
51 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
52 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
55 $this->multiBackend
= new FileBackendMultiWrite( array(
56 'name' => 'localtesting',
57 'lockManager' => 'fsLockManager',
58 'parallelize' => 'implicit',
59 'wikiId' => wfWikiId() . $uniqueId,
62 'name' => 'localmultitesting1',
63 'class' => 'FSFileBackend',
64 'lockManager' => 'nullLockManager',
65 'containerPaths' => array(
66 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
67 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
68 'isMultiMaster' => false
71 'name' => 'localmultitesting2',
72 'class' => 'FSFileBackend',
73 'lockManager' => 'nullLockManager',
74 'containerPaths' => array(
75 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
76 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
77 'isMultiMaster' => true
81 $this->filesToPrune
= array();
84 private static function baseStorePath() {
85 return 'mwstore://localtesting';
88 private function backendClass() {
89 return get_class( $this->backend
);
93 * @dataProvider provider_testIsStoragePath
94 * @covers FileBackend::isStoragePath
96 public function testIsStoragePath( $path, $isStorePath ) {
97 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
98 "FileBackend::isStoragePath on path '$path'" );
101 public static function provider_testIsStoragePath() {
103 array( 'mwstore://', true ),
104 array( 'mwstore://backend', true ),
105 array( 'mwstore://backend/container', true ),
106 array( 'mwstore://backend/container/', true ),
107 array( 'mwstore://backend/container/path', true ),
108 array( 'mwstore://backend//container/', true ),
109 array( 'mwstore://backend//container//', true ),
110 array( 'mwstore://backend//container//path', true ),
111 array( 'mwstore:///', true ),
112 array( 'mwstore:/', false ),
113 array( 'mwstore:', false ),
118 * @dataProvider provider_testSplitStoragePath
119 * @covers FileBackend::splitStoragePath
121 public function testSplitStoragePath( $path, $res ) {
122 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
123 "FileBackend::splitStoragePath on path '$path'" );
126 public static function provider_testSplitStoragePath() {
128 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
129 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
130 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
131 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
132 array( 'mwstore://backend//container/path', array( null, null, null ) ),
133 array( 'mwstore://backend//container//path', array( null, null, null ) ),
134 array( 'mwstore://', array( null, null, null ) ),
135 array( 'mwstore://backend', array( null, null, null ) ),
136 array( 'mwstore:///', array( null, null, null ) ),
137 array( 'mwstore:/', array( null, null, null ) ),
138 array( 'mwstore:', array( null, null, null ) )
143 * @dataProvider provider_normalizeStoragePath
144 * @covers FileBackend::normalizeStoragePath
146 public function testNormalizeStoragePath( $path, $res ) {
147 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
148 "FileBackend::normalizeStoragePath on path '$path'" );
151 public static function provider_normalizeStoragePath() {
153 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
154 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
155 array( 'mwstore://backend/container/path', 'mwstore://backend/container/path' ),
156 array( 'mwstore://backend/container//path', 'mwstore://backend/container/path' ),
157 array( 'mwstore://backend/container///path', 'mwstore://backend/container/path' ),
158 array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj' ),
159 array( 'mwstore://', null ),
160 array( 'mwstore://backend', null ),
161 array( 'mwstore://backend//container/path', null ),
162 array( 'mwstore://backend//container//path', null ),
163 array( 'mwstore:///', null ),
164 array( 'mwstore:/', null ),
165 array( 'mwstore:', null ),
170 * @dataProvider provider_testParentStoragePath
171 * @covers FileBackend::parentStoragePath
173 public function testParentStoragePath( $path, $res ) {
174 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
175 "FileBackend::parentStoragePath on path '$path'" );
178 public static function provider_testParentStoragePath() {
180 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
181 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
182 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
183 array( 'mwstore://backend/container', null ),
184 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
185 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
186 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
187 array( 'mwstore://backend/container/', null ),
192 * @dataProvider provider_testExtensionFromPath
193 * @covers FileBackend::extensionFromPath
195 public function testExtensionFromPath( $path, $res ) {
196 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
197 "FileBackend::extensionFromPath on path '$path'" );
200 public static function provider_testExtensionFromPath() {
202 array( 'mwstore://backend/container/path.txt', 'txt' ),
203 array( 'mwstore://backend/container/path.svg.png', 'png' ),
204 array( 'mwstore://backend/container/path', '' ),
205 array( 'mwstore://backend/container/path.', '' ),
210 * @dataProvider provider_testStore
212 public function testStore( $op ) {
213 $this->filesToPrune
[] = $op['src'];
215 $this->backend
= $this->singleBackend
;
216 $this->tearDownFiles();
217 $this->doTestStore( $op );
218 $this->tearDownFiles();
220 $this->backend
= $this->multiBackend
;
221 $this->tearDownFiles();
222 $this->doTestStore( $op );
223 $this->filesToPrune
[] = $op['src']; # avoid file leaking
224 $this->tearDownFiles();
228 * @covers FileBackend::doOperation
230 private function doTestStore( $op ) {
231 $backendName = $this->backendClass();
233 $source = $op['src'];
235 $this->prepare( array( 'dir' => dirname( $dest ) ) );
237 file_put_contents( $source, "Unit test file" );
239 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
240 $this->backend
->store( $op );
243 $status = $this->backend
->doOperation( $op );
245 $this->assertGoodStatus( $status,
246 "Store from $source to $dest succeeded without warnings ($backendName)." );
247 $this->assertEquals( true, $status->isOK(),
248 "Store from $source to $dest succeeded ($backendName)." );
249 $this->assertEquals( array( 0 => true ), $status->success
,
250 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
251 $this->assertEquals( true, file_exists( $source ),
252 "Source file $source still exists ($backendName)." );
253 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
254 "Destination file $dest exists ($backendName)." );
256 $this->assertEquals( filesize( $source ),
257 $this->backend
->getFileSize( array( 'src' => $dest ) ),
258 "Destination file $dest has correct size ($backendName)." );
260 $props1 = FSFile
::getPropsFromPath( $source );
261 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
262 $this->assertEquals( $props1, $props2,
263 "Source and destination have the same props ($backendName)." );
265 $this->assertBackendPathsConsistent( array( $dest ) );
268 public static function provider_testStore() {
271 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
272 $toPath = self
::baseStorePath() . '/unittest-cont1/e/fun/obj1.txt';
273 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
281 $op2['overwrite'] = true;
289 $op2['overwriteSame'] = true;
300 * @dataProvider provider_testCopy
301 * @covers FileBackend::doOperation
303 public function testCopy( $op ) {
304 $this->backend
= $this->singleBackend
;
305 $this->tearDownFiles();
306 $this->doTestCopy( $op );
307 $this->tearDownFiles();
309 $this->backend
= $this->multiBackend
;
310 $this->tearDownFiles();
311 $this->doTestCopy( $op );
312 $this->tearDownFiles();
315 private function doTestCopy( $op ) {
316 $backendName = $this->backendClass();
318 $source = $op['src'];
320 $this->prepare( array( 'dir' => dirname( $source ) ) );
321 $this->prepare( array( 'dir' => dirname( $dest ) ) );
323 if ( isset( $op['ignoreMissingSource'] ) ) {
324 $status = $this->backend
->doOperation( $op );
325 $this->assertGoodStatus( $status,
326 "Move from $source to $dest succeeded without warnings ($backendName)." );
327 $this->assertEquals( array( 0 => true ), $status->success
,
328 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
329 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
330 "Source file $source does not exist ($backendName)." );
331 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $dest ) ),
332 "Destination file $dest does not exist ($backendName)." );
337 $status = $this->backend
->doOperation(
338 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
339 $this->assertGoodStatus( $status,
340 "Creation of file at $source succeeded ($backendName)." );
342 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
343 $this->backend
->copy( $op );
346 $status = $this->backend
->doOperation( $op );
348 $this->assertGoodStatus( $status,
349 "Copy from $source to $dest succeeded without warnings ($backendName)." );
350 $this->assertEquals( true, $status->isOK(),
351 "Copy from $source to $dest succeeded ($backendName)." );
352 $this->assertEquals( array( 0 => true ), $status->success
,
353 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
354 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
355 "Source file $source still exists ($backendName)." );
356 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
357 "Destination file $dest exists after copy ($backendName)." );
360 $this->backend
->getFileSize( array( 'src' => $source ) ),
361 $this->backend
->getFileSize( array( 'src' => $dest ) ),
362 "Destination file $dest has correct size ($backendName)." );
364 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
365 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
366 $this->assertEquals( $props1, $props2,
367 "Source and destination have the same props ($backendName)." );
369 $this->assertBackendPathsConsistent( array( $source, $dest ) );
372 public static function provider_testCopy() {
375 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
376 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
378 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
386 $op2['overwrite'] = true;
394 $op2['overwriteSame'] = true;
402 $op2['ignoreMissingSource'] = true;
410 $op2['ignoreMissingSource'] = true;
413 self
::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
421 * @dataProvider provider_testMove
422 * @covers FileBackend::doOperation
424 public function testMove( $op ) {
425 $this->backend
= $this->singleBackend
;
426 $this->tearDownFiles();
427 $this->doTestMove( $op );
428 $this->tearDownFiles();
430 $this->backend
= $this->multiBackend
;
431 $this->tearDownFiles();
432 $this->doTestMove( $op );
433 $this->tearDownFiles();
436 private function doTestMove( $op ) {
437 $backendName = $this->backendClass();
439 $source = $op['src'];
441 $this->prepare( array( 'dir' => dirname( $source ) ) );
442 $this->prepare( array( 'dir' => dirname( $dest ) ) );
444 if ( isset( $op['ignoreMissingSource'] ) ) {
445 $status = $this->backend
->doOperation( $op );
446 $this->assertGoodStatus( $status,
447 "Move from $source to $dest succeeded without warnings ($backendName)." );
448 $this->assertEquals( array( 0 => true ), $status->success
,
449 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
450 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
451 "Source file $source does not exist ($backendName)." );
452 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $dest ) ),
453 "Destination file $dest does not exist ($backendName)." );
458 $status = $this->backend
->doOperation(
459 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
460 $this->assertGoodStatus( $status,
461 "Creation of file at $source succeeded ($backendName)." );
463 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
464 $this->backend
->copy( $op );
467 $status = $this->backend
->doOperation( $op );
468 $this->assertGoodStatus( $status,
469 "Move from $source to $dest succeeded without warnings ($backendName)." );
470 $this->assertEquals( true, $status->isOK(),
471 "Move from $source to $dest succeeded ($backendName)." );
472 $this->assertEquals( array( 0 => true ), $status->success
,
473 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
474 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
475 "Source file $source does not still exists ($backendName)." );
476 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
477 "Destination file $dest exists after move ($backendName)." );
479 $this->assertNotEquals(
480 $this->backend
->getFileSize( array( 'src' => $source ) ),
481 $this->backend
->getFileSize( array( 'src' => $dest ) ),
482 "Destination file $dest has correct size ($backendName)." );
484 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
485 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
486 $this->assertEquals( false, $props1['fileExists'],
487 "Source file does not exist accourding to props ($backendName)." );
488 $this->assertEquals( true, $props2['fileExists'],
489 "Destination file exists accourding to props ($backendName)." );
491 $this->assertBackendPathsConsistent( array( $source, $dest ) );
494 public static function provider_testMove() {
497 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
498 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
500 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
508 $op2['overwrite'] = true;
516 $op2['overwriteSame'] = true;
524 $op2['ignoreMissingSource'] = true;
532 $op2['ignoreMissingSource'] = true;
535 self
::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
543 * @dataProvider provider_testDelete
544 * @covers FileBackend::doOperation
546 public function testDelete( $op, $withSource, $okStatus ) {
547 $this->backend
= $this->singleBackend
;
548 $this->tearDownFiles();
549 $this->doTestDelete( $op, $withSource, $okStatus );
550 $this->tearDownFiles();
552 $this->backend
= $this->multiBackend
;
553 $this->tearDownFiles();
554 $this->doTestDelete( $op, $withSource, $okStatus );
555 $this->tearDownFiles();
558 private function doTestDelete( $op, $withSource, $okStatus ) {
559 $backendName = $this->backendClass();
561 $source = $op['src'];
562 $this->prepare( array( 'dir' => dirname( $source ) ) );
565 $status = $this->backend
->doOperation(
566 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
567 $this->assertGoodStatus( $status,
568 "Creation of file at $source succeeded ($backendName)." );
571 $status = $this->backend
->doOperation( $op );
573 $this->assertGoodStatus( $status,
574 "Deletion of file at $source succeeded without warnings ($backendName)." );
575 $this->assertEquals( true, $status->isOK(),
576 "Deletion of file at $source succeeded ($backendName)." );
577 $this->assertEquals( array( 0 => true ), $status->success
,
578 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
580 $this->assertEquals( false, $status->isOK(),
581 "Deletion of file at $source failed ($backendName)." );
584 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
585 "Source file $source does not exist after move ($backendName)." );
588 $this->backend
->getFileSize( array( 'src' => $source ) ),
589 "Source file $source has correct size (false) ($backendName)." );
591 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
592 $this->assertFalse( $props1['fileExists'],
593 "Source file $source does not exist according to props ($backendName)." );
595 $this->assertBackendPathsConsistent( array( $source ) );
598 public static function provider_testDelete() {
601 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
603 $op = array( 'op' => 'delete', 'src' => $source );
612 false, // without source
616 $op['ignoreMissingSource'] = true;
619 false, // without source
623 $op['ignoreMissingSource'] = true;
624 $op['src'] = self
::baseStorePath() . '/unittest-cont-bad/e/file.txt';
627 false, // without source
635 * @dataProvider provider_testDescribe
636 * @covers FileBackend::doOperation
638 public function testDescribe( $op, $withSource, $okStatus ) {
639 $this->backend
= $this->singleBackend
;
640 $this->tearDownFiles();
641 $this->doTestDescribe( $op, $withSource, $okStatus );
642 $this->tearDownFiles();
644 $this->backend
= $this->multiBackend
;
645 $this->tearDownFiles();
646 $this->doTestDescribe( $op, $withSource, $okStatus );
647 $this->tearDownFiles();
650 private function doTestDescribe( $op, $withSource, $okStatus ) {
651 $backendName = $this->backendClass();
653 $source = $op['src'];
654 $this->prepare( array( 'dir' => dirname( $source ) ) );
657 $status = $this->backend
->doOperation(
658 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
659 $this->assertGoodStatus( $status,
660 "Creation of file at $source succeeded ($backendName)." );
663 $status = $this->backend
->doOperation( $op );
665 $this->assertGoodStatus( $status,
666 "Describe of file at $source succeeded without warnings ($backendName)." );
667 $this->assertEquals( true, $status->isOK(),
668 "Describe of file at $source succeeded ($backendName)." );
669 $this->assertEquals( array( 0 => true ), $status->success
,
670 "Describe of file at $source has proper 'success' field in Status ($backendName)." );
672 $this->assertEquals( false, $status->isOK(),
673 "Describe of file at $source failed ($backendName)." );
676 $this->assertBackendPathsConsistent( array( $source ) );
679 public static function provider_testDescribe() {
682 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
684 $op = array( 'op' => 'describe', 'src' => $source,
685 'headers' => array( 'X-Content-Length' => '91.3', 'Content-Old-Header' => '' ),
686 'disposition' => 'inline' );
695 false, // without source
703 * @dataProvider provider_testCreate
704 * @covers FileBackend::doOperation
706 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
707 $this->backend
= $this->singleBackend
;
708 $this->tearDownFiles();
709 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
710 $this->tearDownFiles();
712 $this->backend
= $this->multiBackend
;
713 $this->tearDownFiles();
714 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
715 $this->tearDownFiles();
718 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
719 $backendName = $this->backendClass();
722 $this->prepare( array( 'dir' => dirname( $dest ) ) );
724 $oldText = 'blah...blah...waahwaah';
725 if ( $alreadyExists ) {
726 $status = $this->backend
->doOperation(
727 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
728 $this->assertGoodStatus( $status,
729 "Creation of file at $dest succeeded ($backendName)." );
732 $status = $this->backend
->doOperation( $op );
734 $this->assertGoodStatus( $status,
735 "Creation of file at $dest succeeded without warnings ($backendName)." );
736 $this->assertEquals( true, $status->isOK(),
737 "Creation of file at $dest succeeded ($backendName)." );
738 $this->assertEquals( array( 0 => true ), $status->success
,
739 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
741 $this->assertEquals( false, $status->isOK(),
742 "Creation of file at $dest failed ($backendName)." );
745 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
746 "Destination file $dest exists after creation ($backendName)." );
748 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
749 $this->assertEquals( true, $props1['fileExists'],
750 "Destination file $dest exists according to props ($backendName)." );
751 if ( $okStatus ) { // file content is what we saved
752 $this->assertEquals( $newSize, $props1['size'],
753 "Destination file $dest has expected size according to props ($backendName)." );
754 $this->assertEquals( $newSize,
755 $this->backend
->getFileSize( array( 'src' => $dest ) ),
756 "Destination file $dest has correct size ($backendName)." );
757 } else { // file content is some other previous text
758 $this->assertEquals( strlen( $oldText ), $props1['size'],
759 "Destination file $dest has original size according to props ($backendName)." );
760 $this->assertEquals( strlen( $oldText ),
761 $this->backend
->getFileSize( array( 'src' => $dest ) ),
762 "Destination file $dest has original size according to props ($backendName)." );
765 $this->assertBackendPathsConsistent( array( $dest ) );
769 * @dataProvider provider_testCreate
771 public static function provider_testCreate() {
774 $dest = self
::baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
776 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
779 false, // no dest already exists
781 strlen( $op['content'] )
785 $op2['content'] = "\n";
788 false, // no dest already exists
790 strlen( $op2['content'] )
794 $op2['content'] = "fsf\n waf 3kt";
797 true, // dest already exists
799 strlen( $op2['content'] )
803 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
804 $op2['overwrite'] = true;
807 true, // dest already exists
809 strlen( $op2['content'] )
813 $op2['content'] = "39qjmg3-qg";
814 $op2['overwriteSame'] = true;
817 true, // dest already exists
819 strlen( $op2['content'] )
826 * @covers FileBackend::doQuickOperations
828 public function testDoQuickOperations() {
829 $this->backend
= $this->singleBackend
;
830 $this->doTestDoQuickOperations();
831 $this->tearDownFiles();
833 $this->backend
= $this->multiBackend
;
834 $this->doTestDoQuickOperations();
835 $this->tearDownFiles();
838 private function doTestDoQuickOperations() {
839 $backendName = $this->backendClass();
841 $base = self
::baseStorePath();
843 "$base/unittest-cont1/e/fileA.a",
844 "$base/unittest-cont1/e/fileB.a",
845 "$base/unittest-cont1/e/fileC.a"
847 $createOps = array();
849 foreach ( $files as $path ) {
850 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
851 $this->assertGoodStatus( $status,
852 "Preparing $path succeeded without warnings ($backendName)." );
853 $createOps[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand( 0, 50000 ) );
854 $copyOps[] = array( 'op' => 'copy', 'src' => $path, 'dst' => "$path-2" );
855 $moveOps[] = array( 'op' => 'move', 'src' => "$path-2", 'dst' => "$path-3" );
856 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
857 $purgeOps[] = array( 'op' => 'delete', 'src' => "$path-3" );
859 $purgeOps[] = array( 'op' => 'null' );
861 $this->assertGoodStatus(
862 $this->backend
->doQuickOperations( $createOps ),
863 "Creation of source files succeeded ($backendName)." );
864 foreach ( $files as $file ) {
865 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $file ) ),
866 "File $file exists." );
869 $this->assertGoodStatus(
870 $this->backend
->doQuickOperations( $copyOps ),
871 "Quick copy of source files succeeded ($backendName)." );
872 foreach ( $files as $file ) {
873 $this->assertTrue( $this->backend
->fileExists( array( 'src' => "$file-2" ) ),
874 "File $file-2 exists." );
877 $this->assertGoodStatus(
878 $this->backend
->doQuickOperations( $moveOps ),
879 "Quick move of source files succeeded ($backendName)." );
880 foreach ( $files as $file ) {
881 $this->assertTrue( $this->backend
->fileExists( array( 'src' => "$file-3" ) ),
882 "File $file-3 move in." );
883 $this->assertFalse( $this->backend
->fileExists( array( 'src' => "$file-2" ) ),
884 "File $file-2 moved away." );
887 $this->assertGoodStatus(
888 $this->backend
->quickCopy( array( 'src' => $files[0], 'dst' => $files[0] ) ),
889 "Copy of file {$files[0]} over itself succeeded ($backendName)." );
890 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $files[0] ) ),
891 "File {$files[0]} still exists." );
893 $this->assertGoodStatus(
894 $this->backend
->quickMove( array( 'src' => $files[0], 'dst' => $files[0] ) ),
895 "Move of file {$files[0]} over itself succeeded ($backendName)." );
896 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $files[0] ) ),
897 "File {$files[0]} still exists." );
899 $this->assertGoodStatus(
900 $this->backend
->doQuickOperations( $purgeOps ),
901 "Quick deletion of source files succeeded ($backendName)." );
902 foreach ( $files as $file ) {
903 $this->assertFalse( $this->backend
->fileExists( array( 'src' => $file ) ),
904 "File $file purged." );
905 $this->assertFalse( $this->backend
->fileExists( array( 'src' => "$file-3" ) ),
906 "File $file-3 purged." );
911 * @dataProvider provider_testConcatenate
913 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
914 $this->filesToPrune
[] = $op['dst'];
916 $this->backend
= $this->singleBackend
;
917 $this->tearDownFiles();
918 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
919 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
920 $this->tearDownFiles();
922 $this->backend
= $this->multiBackend
;
923 $this->tearDownFiles();
924 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
925 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
926 $this->tearDownFiles();
929 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
930 $backendName = $this->backendClass();
935 foreach ( $srcs as $i => $source ) {
936 $this->prepare( array( 'dir' => dirname( $source ) ) );
938 'op' => 'create', // operation
939 'dst' => $source, // source
940 'content' => $srcsContent[$i]
942 $expContent .= $srcsContent[$i];
944 $status = $this->backend
->doOperations( $ops );
946 $this->assertGoodStatus( $status,
947 "Creation of source files succeeded ($backendName)." );
949 $dest = $params['dst'];
950 if ( $alreadyExists ) {
951 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
952 $this->assertEquals( true, $ok,
953 "Creation of file at $dest succeeded ($backendName)." );
955 $ok = file_put_contents( $dest, '' ) !== false;
956 $this->assertEquals( true, $ok,
957 "Creation of 0-byte file at $dest succeeded ($backendName)." );
960 // Combine the files into one
961 $status = $this->backend
->concatenate( $params );
963 $this->assertGoodStatus( $status,
964 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
965 $this->assertEquals( true, $status->isOK(),
966 "Creation of concat file at $dest succeeded ($backendName)." );
968 $this->assertEquals( false, $status->isOK(),
969 "Creation of concat file at $dest failed ($backendName)." );
973 $this->assertEquals( true, is_file( $dest ),
974 "Dest concat file $dest exists after creation ($backendName)." );
976 $this->assertEquals( true, is_file( $dest ),
977 "Dest concat file $dest exists after failed creation ($backendName)." );
980 $contents = file_get_contents( $dest );
981 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
984 $this->assertEquals( $expContent, $contents,
985 "Concat file at $dest has correct contents ($backendName)." );
987 $this->assertNotEquals( $expContent, $contents,
988 "Concat file at $dest has correct contents ($backendName)." );
992 public static function provider_testConcatenate() {
995 $rand = mt_rand( 0, 2000000000 ) . time();
996 $dest = wfTempDir() . "/randomfile!$rand.txt";
998 self
::baseStorePath() . '/unittest-cont1/e/file1.txt',
999 self
::baseStorePath() . '/unittest-cont1/e/file2.txt',
1000 self
::baseStorePath() . '/unittest-cont1/e/file3.txt',
1001 self
::baseStorePath() . '/unittest-cont1/e/file4.txt',
1002 self
::baseStorePath() . '/unittest-cont1/e/file5.txt',
1003 self
::baseStorePath() . '/unittest-cont1/e/file6.txt',
1004 self
::baseStorePath() . '/unittest-cont1/e/file7.txt',
1005 self
::baseStorePath() . '/unittest-cont1/e/file8.txt',
1006 self
::baseStorePath() . '/unittest-cont1/e/file9.txt',
1007 self
::baseStorePath() . '/unittest-cont1/e/file10.txt'
1021 $params = array( 'srcs' => $srcs, 'dst' => $dest );
1024 $params, // operation
1026 $content, // content for each source
1027 false, // no dest already exists
1032 $params, // operation
1034 $content, // content for each source
1035 true, // dest already exists
1043 * @dataProvider provider_testGetFileStat
1044 * @covers FileBackend::getFileStat
1046 public function testGetFileStat( $path, $content, $alreadyExists ) {
1047 $this->backend
= $this->singleBackend
;
1048 $this->tearDownFiles();
1049 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1050 $this->tearDownFiles();
1052 $this->backend
= $this->multiBackend
;
1053 $this->tearDownFiles();
1054 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1055 $this->tearDownFiles();
1058 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
1059 $backendName = $this->backendClass();
1061 if ( $alreadyExists ) {
1062 $this->prepare( array( 'dir' => dirname( $path ) ) );
1063 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
1064 $this->assertGoodStatus( $status,
1065 "Creation of file at $path succeeded ($backendName)." );
1067 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1068 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1069 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
1071 $this->assertEquals( strlen( $content ), $size,
1072 "Correct file size of '$path'" );
1073 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1074 "Correct file timestamp of '$path'" );
1076 $size = $stat['size'];
1077 $time = $stat['mtime'];
1078 $this->assertEquals( strlen( $content ), $size,
1079 "Correct file size of '$path'" );
1080 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1081 "Correct file timestamp of '$path'" );
1083 $this->backend
->clearCache( array( $path ) );
1085 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1087 $this->assertEquals( strlen( $content ), $size,
1088 "Correct file size of '$path'" );
1090 $this->backend
->preloadCache( array( $path ) );
1092 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1094 $this->assertEquals( strlen( $content ), $size,
1095 "Correct file size of '$path'" );
1097 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1098 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1099 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
1101 $this->assertFalse( $size, "Correct file size of '$path'" );
1102 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1103 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1107 public static function provider_testGetFileStat() {
1110 $base = self
::baseStorePath();
1111 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
1112 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
1113 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
1119 * @dataProvider provider_testGetFileContents
1120 * @covers FileBackend::getFileContents
1121 * @covers FileBackend::getFileContentsMulti
1123 public function testGetFileContents( $source, $content ) {
1124 $this->backend
= $this->singleBackend
;
1125 $this->tearDownFiles();
1126 $this->doTestGetFileContents( $source, $content );
1127 $this->tearDownFiles();
1129 $this->backend
= $this->multiBackend
;
1130 $this->tearDownFiles();
1131 $this->doTestGetFileContents( $source, $content );
1132 $this->tearDownFiles();
1135 private function doTestGetFileContents( $source, $content ) {
1136 $backendName = $this->backendClass();
1138 $srcs = (array)$source;
1139 $content = (array)$content;
1140 foreach ( $srcs as $i => $src ) {
1141 $this->prepare( array( 'dir' => dirname( $src ) ) );
1142 $status = $this->backend
->doOperation(
1143 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1144 $this->assertGoodStatus( $status,
1145 "Creation of file at $src succeeded ($backendName)." );
1148 if ( is_array( $source ) ) {
1149 $contents = $this->backend
->getFileContentsMulti( array( 'srcs' => $source ) );
1150 foreach ( $contents as $path => $data ) {
1151 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1152 $this->assertEquals( current( $content ), $data, "Contents of $path is correct ($backendName)." );
1155 $this->assertEquals( $source, array_keys( $contents ), "Contents in right order ($backendName)." );
1156 $this->assertEquals( count( $source ), count( $contents ), "Contents array size correct ($backendName)." );
1158 $data = $this->backend
->getFileContents( array( 'src' => $source ) );
1159 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1160 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1164 public static function provider_testGetFileContents() {
1167 $base = self
::baseStorePath();
1168 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
1169 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
1171 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1172 "$base/unittest-cont1/e/a/z.txt" ),
1173 array( "contents xx", "contents xy", "contents xz" )
1180 * @dataProvider provider_testGetLocalCopy
1181 * @covers FileBackend::getLocalCopy
1183 public function testGetLocalCopy( $source, $content ) {
1184 $this->backend
= $this->singleBackend
;
1185 $this->tearDownFiles();
1186 $this->doTestGetLocalCopy( $source, $content );
1187 $this->tearDownFiles();
1189 $this->backend
= $this->multiBackend
;
1190 $this->tearDownFiles();
1191 $this->doTestGetLocalCopy( $source, $content );
1192 $this->tearDownFiles();
1195 private function doTestGetLocalCopy( $source, $content ) {
1196 $backendName = $this->backendClass();
1198 $srcs = (array)$source;
1199 $content = (array)$content;
1200 foreach ( $srcs as $i => $src ) {
1201 $this->prepare( array( 'dir' => dirname( $src ) ) );
1202 $status = $this->backend
->doOperation(
1203 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1204 $this->assertGoodStatus( $status,
1205 "Creation of file at $src succeeded ($backendName)." );
1208 if ( is_array( $source ) ) {
1209 $tmpFiles = $this->backend
->getLocalCopyMulti( array( 'srcs' => $source ) );
1210 foreach ( $tmpFiles as $path => $tmpFile ) {
1211 $this->assertNotNull( $tmpFile,
1212 "Creation of local copy of $path succeeded ($backendName)." );
1213 $contents = file_get_contents( $tmpFile->getPath() );
1214 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1215 $this->assertEquals( current( $content ), $contents, "Local copy of $path is correct ($backendName)." );
1218 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local copies in right order ($backendName)." );
1219 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local copies array size correct ($backendName)." );
1221 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
1222 $this->assertNotNull( $tmpFile,
1223 "Creation of local copy of $source succeeded ($backendName)." );
1224 $contents = file_get_contents( $tmpFile->getPath() );
1225 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1226 $this->assertEquals( $content[0], $contents, "Local copy of $source is correct ($backendName)." );
1229 $obj = new stdClass();
1230 $tmpFile->bind( $obj );
1233 public static function provider_testGetLocalCopy() {
1236 $base = self
::baseStorePath();
1237 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1238 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1239 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1241 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1242 "$base/unittest-cont1/e/a/z.txt" ),
1243 array( "contents xx", "contents xy", "contents xz" )
1250 * @dataProvider provider_testGetLocalReference
1251 * @covers FileBackend::getLocalReference
1253 public function testGetLocalReference( $source, $content ) {
1254 $this->backend
= $this->singleBackend
;
1255 $this->tearDownFiles();
1256 $this->doTestGetLocalReference( $source, $content );
1257 $this->tearDownFiles();
1259 $this->backend
= $this->multiBackend
;
1260 $this->tearDownFiles();
1261 $this->doTestGetLocalReference( $source, $content );
1262 $this->tearDownFiles();
1265 private function doTestGetLocalReference( $source, $content ) {
1266 $backendName = $this->backendClass();
1268 $srcs = (array)$source;
1269 $content = (array)$content;
1270 foreach ( $srcs as $i => $src ) {
1271 $this->prepare( array( 'dir' => dirname( $src ) ) );
1272 $status = $this->backend
->doOperation(
1273 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1274 $this->assertGoodStatus( $status,
1275 "Creation of file at $src succeeded ($backendName)." );
1278 if ( is_array( $source ) ) {
1279 $tmpFiles = $this->backend
->getLocalReferenceMulti( array( 'srcs' => $source ) );
1280 foreach ( $tmpFiles as $path => $tmpFile ) {
1281 $this->assertNotNull( $tmpFile,
1282 "Creation of local copy of $path succeeded ($backendName)." );
1283 $contents = file_get_contents( $tmpFile->getPath() );
1284 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1285 $this->assertEquals( current( $content ), $contents, "Local ref of $path is correct ($backendName)." );
1288 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local refs in right order ($backendName)." );
1289 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local refs array size correct ($backendName)." );
1291 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
1292 $this->assertNotNull( $tmpFile,
1293 "Creation of local copy of $source succeeded ($backendName)." );
1294 $contents = file_get_contents( $tmpFile->getPath() );
1295 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1296 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1300 public static function provider_testGetLocalReference() {
1303 $base = self
::baseStorePath();
1304 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1305 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1306 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1308 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1309 "$base/unittest-cont1/e/a/z.txt" ),
1310 array( "contents xx", "contents xy", "contents xz" )
1317 * @covers FileBackend::getLocalCopy
1318 * @covers FileBackend::getLocalReference
1320 public function testGetLocalCopyAndReference404() {
1321 $this->backend
= $this->singleBackend
;
1322 $this->tearDownFiles();
1323 $this->doTestGetLocalCopyAndReference404();
1324 $this->tearDownFiles();
1326 $this->backend
= $this->multiBackend
;
1327 $this->tearDownFiles();
1328 $this->doTestGetLocalCopyAndReference404();
1329 $this->tearDownFiles();
1332 public function doTestGetLocalCopyAndReference404() {
1333 $backendName = $this->backendClass();
1335 $base = self
::baseStorePath();
1337 $tmpFile = $this->backend
->getLocalCopy( array(
1338 'src' => "$base/unittest-cont1/not-there" ) );
1339 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1341 $tmpFile = $this->backend
->getLocalReference( array(
1342 'src' => "$base/unittest-cont1/not-there" ) );
1343 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1347 * @dataProvider provider_testGetFileHttpUrl
1348 * @covers FileBackend::getFileHttpUrl
1350 public function testGetFileHttpUrl( $source, $content ) {
1351 $this->backend
= $this->singleBackend
;
1352 $this->tearDownFiles();
1353 $this->doTestGetFileHttpUrl( $source, $content );
1354 $this->tearDownFiles();
1356 $this->backend
= $this->multiBackend
;
1357 $this->tearDownFiles();
1358 $this->doTestGetFileHttpUrl( $source, $content );
1359 $this->tearDownFiles();
1362 private function doTestGetFileHttpUrl( $source, $content ) {
1363 $backendName = $this->backendClass();
1365 $this->prepare( array( 'dir' => dirname( $source ) ) );
1366 $status = $this->backend
->doOperation(
1367 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
1368 $this->assertGoodStatus( $status,
1369 "Creation of file at $source succeeded ($backendName)." );
1371 $url = $this->backend
->getFileHttpUrl( array( 'src' => $source ) );
1373 if ( $url !== null ) { // supported
1374 $data = Http
::request( "GET", $url );
1375 $this->assertEquals( $content, $data,
1376 "HTTP GET of URL has right contents ($backendName)." );
1380 public static function provider_testGetFileHttpUrl() {
1383 $base = self
::baseStorePath();
1384 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1385 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1386 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1392 * @dataProvider provider_testPrepareAndClean
1393 * @covers FileBackend::prepare
1394 * @covers FileBackend::clean
1396 public function testPrepareAndClean( $path, $isOK ) {
1397 $this->backend
= $this->singleBackend
;
1398 $this->doTestPrepareAndClean( $path, $isOK );
1399 $this->tearDownFiles();
1401 $this->backend
= $this->multiBackend
;
1402 $this->doTestPrepareAndClean( $path, $isOK );
1403 $this->tearDownFiles();
1406 public static function provider_testPrepareAndClean() {
1407 $base = self
::baseStorePath();
1410 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1411 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1412 # Specific to FS backend with no basePath field set
1413 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1417 private function doTestPrepareAndClean( $path, $isOK ) {
1418 $backendName = $this->backendClass();
1420 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1422 $this->assertGoodStatus( $status,
1423 "Preparing dir $path succeeded without warnings ($backendName)." );
1424 $this->assertEquals( true, $status->isOK(),
1425 "Preparing dir $path succeeded ($backendName)." );
1427 $this->assertEquals( false, $status->isOK(),
1428 "Preparing dir $path failed ($backendName)." );
1431 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1433 $this->assertGoodStatus( $status,
1434 "Cleaning dir $path succeeded without warnings ($backendName)." );
1435 $this->assertEquals( true, $status->isOK(),
1436 "Cleaning dir $path succeeded ($backendName)." );
1438 $this->assertEquals( false, $status->isOK(),
1439 "Cleaning dir $path failed ($backendName)." );
1443 public function testRecursiveClean() {
1444 $this->backend
= $this->singleBackend
;
1445 $this->doTestRecursiveClean();
1446 $this->tearDownFiles();
1448 $this->backend
= $this->multiBackend
;
1449 $this->doTestRecursiveClean();
1450 $this->tearDownFiles();
1454 * @covers FileBackend::clean
1456 private function doTestRecursiveClean() {
1457 $backendName = $this->backendClass();
1459 $base = self
::baseStorePath();
1461 "$base/unittest-cont1",
1462 "$base/unittest-cont1/e",
1463 "$base/unittest-cont1/e/a",
1464 "$base/unittest-cont1/e/a/b",
1465 "$base/unittest-cont1/e/a/b/c",
1466 "$base/unittest-cont1/e/a/b/c/d0",
1467 "$base/unittest-cont1/e/a/b/c/d1",
1468 "$base/unittest-cont1/e/a/b/c/d2",
1469 "$base/unittest-cont1/e/a/b/c/d0/1",
1470 "$base/unittest-cont1/e/a/b/c/d0/2",
1471 "$base/unittest-cont1/e/a/b/c/d1/3",
1472 "$base/unittest-cont1/e/a/b/c/d1/4",
1473 "$base/unittest-cont1/e/a/b/c/d2/5",
1474 "$base/unittest-cont1/e/a/b/c/d2/6"
1476 foreach ( $dirs as $dir ) {
1477 $status = $this->prepare( array( 'dir' => $dir ) );
1478 $this->assertGoodStatus( $status,
1479 "Preparing dir $dir succeeded without warnings ($backendName)." );
1482 if ( $this->backend
instanceof FSFileBackend
) {
1483 foreach ( $dirs as $dir ) {
1484 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1485 "Dir $dir exists ($backendName)." );
1489 $status = $this->backend
->clean(
1490 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1491 $this->assertGoodStatus( $status,
1492 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1494 foreach ( $dirs as $dir ) {
1495 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1496 "Dir $dir no longer exists ($backendName)." );
1503 * @covers FileBackend::doOperations
1505 public function testDoOperations() {
1506 $this->backend
= $this->singleBackend
;
1507 $this->tearDownFiles();
1508 $this->doTestDoOperations();
1509 $this->tearDownFiles();
1511 $this->backend
= $this->multiBackend
;
1512 $this->tearDownFiles();
1513 $this->doTestDoOperations();
1514 $this->tearDownFiles();
1517 private function doTestDoOperations() {
1518 $base = self
::baseStorePath();
1520 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1521 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1522 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1523 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1524 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1525 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1526 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1528 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1529 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1530 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1531 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1532 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1533 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1534 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1536 $status = $this->backend
->doOperations( array(
1537 array( 'op' => 'describe', 'src' => $fileA,
1538 'headers' => array( 'X-Content-Length' => '91.3' ), 'disposition' => 'inline' ),
1539 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1540 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1541 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1542 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1543 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1544 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1545 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1546 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1547 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1548 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1549 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1550 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1551 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1552 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1553 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1554 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1555 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1557 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1559 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1561 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1563 array( 'op' => 'null' ),
1567 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1568 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1569 $this->assertEquals( 14, count( $status->success
),
1570 "Operation batch has correct success array" );
1572 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1573 "File does not exist at $fileA" );
1574 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1575 "File does not exist at $fileB" );
1576 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1577 "File does not exist at $fileD" );
1579 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1580 "File exists at $fileC" );
1581 $this->assertEquals( $fileBContents,
1582 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1583 "Correct file contents of $fileC" );
1584 $this->assertEquals( strlen( $fileBContents ),
1585 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1586 "Correct file size of $fileC" );
1587 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1588 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1589 "Correct file SHA-1 of $fileC" );
1593 * @covers FileBackend::doOperations
1595 public function testDoOperationsPipeline() {
1596 $this->backend
= $this->singleBackend
;
1597 $this->tearDownFiles();
1598 $this->doTestDoOperationsPipeline();
1599 $this->tearDownFiles();
1601 $this->backend
= $this->multiBackend
;
1602 $this->tearDownFiles();
1603 $this->doTestDoOperationsPipeline();
1604 $this->tearDownFiles();
1607 // concurrency orientated
1608 private function doTestDoOperationsPipeline() {
1609 $base = self
::baseStorePath();
1611 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1612 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1613 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1615 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1616 file_put_contents( $tmpNameA, $fileAContents );
1617 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1618 file_put_contents( $tmpNameB, $fileBContents );
1619 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1620 file_put_contents( $tmpNameC, $fileCContents );
1622 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1623 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1624 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1626 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1627 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1628 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1629 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1631 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1632 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1633 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1634 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1635 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1637 $status = $this->backend
->doOperations( array(
1638 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1639 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1640 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1641 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1642 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1643 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1644 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1645 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1646 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1647 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1648 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1649 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1650 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1651 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1652 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1653 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1654 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1655 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1656 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1657 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1659 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1661 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1663 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1665 array( 'op' => 'null' ),
1669 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1670 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1671 $this->assertEquals( 16, count( $status->success
),
1672 "Operation batch has correct success array" );
1674 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1675 "File does not exist at $fileA" );
1676 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1677 "File does not exist at $fileB" );
1678 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1679 "File does not exist at $fileD" );
1681 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1682 "File exists at $fileC" );
1683 $this->assertEquals( $fileBContents,
1684 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1685 "Correct file contents of $fileC" );
1686 $this->assertEquals( strlen( $fileBContents ),
1687 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1688 "Correct file size of $fileC" );
1689 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1690 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1691 "Correct file SHA-1 of $fileC" );
1695 * @covers FileBackend::doOperations
1697 public function testDoOperationsFailing() {
1698 $this->backend
= $this->singleBackend
;
1699 $this->tearDownFiles();
1700 $this->doTestDoOperationsFailing();
1701 $this->tearDownFiles();
1703 $this->backend
= $this->multiBackend
;
1704 $this->tearDownFiles();
1705 $this->doTestDoOperationsFailing();
1706 $this->tearDownFiles();
1709 private function doTestDoOperationsFailing() {
1710 $base = self
::baseStorePath();
1712 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1713 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1714 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1715 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1716 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1717 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1718 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1720 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1721 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1722 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1723 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1724 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1725 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1727 $status = $this->backend
->doOperations( array(
1728 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1729 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1730 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1731 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1732 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1733 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1734 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1735 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1736 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1737 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1738 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1739 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1740 array( 'op' => 'delete', 'src' => $fileD ),
1741 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1742 array( 'op' => 'null' ),
1744 ), array( 'force' => 1 ) );
1746 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1747 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1748 $this->assertEquals( 8, count( $status->success
),
1749 "Operation batch has correct success array" );
1751 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1752 "File does not exist at $fileB" );
1753 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1754 "File does not exist at $fileD" );
1756 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1757 "File does not exist at $fileA" );
1758 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1759 "File exists at $fileC" );
1760 $this->assertEquals( $fileBContents,
1761 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1762 "Correct file contents of $fileA" );
1763 $this->assertEquals( strlen( $fileBContents ),
1764 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1765 "Correct file size of $fileA" );
1766 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1767 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1768 "Correct file SHA-1 of $fileA" );
1772 * @covers FileBackend::getFileList
1774 public function testGetFileList() {
1775 $this->backend
= $this->singleBackend
;
1776 $this->tearDownFiles();
1777 $this->doTestGetFileList();
1778 $this->tearDownFiles();
1780 $this->backend
= $this->multiBackend
;
1781 $this->tearDownFiles();
1782 $this->doTestGetFileList();
1783 $this->tearDownFiles();
1786 private function doTestGetFileList() {
1787 $backendName = $this->backendClass();
1788 $base = self
::baseStorePath();
1790 // Should have no errors
1791 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1794 "$base/unittest-cont1/e/test1.txt",
1795 "$base/unittest-cont1/e/test2.txt",
1796 "$base/unittest-cont1/e/test3.txt",
1797 "$base/unittest-cont1/e/subdir1/test1.txt",
1798 "$base/unittest-cont1/e/subdir1/test2.txt",
1799 "$base/unittest-cont1/e/subdir2/test3.txt",
1800 "$base/unittest-cont1/e/subdir2/test4.txt",
1801 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1802 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1803 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1804 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1805 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1806 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1807 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1812 foreach ( $files as $file ) {
1813 $this->prepare( array( 'dir' => dirname( $file ) ) );
1814 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1816 $status = $this->backend
->doQuickOperations( $ops );
1817 $this->assertGoodStatus( $status,
1818 "Creation of files succeeded ($backendName)." );
1819 $this->assertEquals( true, $status->isOK(),
1820 "Creation of files succeeded with OK status ($backendName)." );
1822 // Expected listing at root
1827 "e/subdir1/test1.txt",
1828 "e/subdir1/test2.txt",
1829 "e/subdir2/test3.txt",
1830 "e/subdir2/test4.txt",
1831 "e/subdir2/subdir/test1.txt",
1832 "e/subdir2/subdir/test2.txt",
1833 "e/subdir2/subdir/test3.txt",
1834 "e/subdir2/subdir/test4.txt",
1835 "e/subdir2/subdir/test5.txt",
1836 "e/subdir2/subdir/sub/test0.txt",
1837 "e/subdir2/subdir/sub/120-px-file.txt",
1841 // Actual listing (no trailing slash) at root
1842 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1843 $list = $this->listToArray( $iter );
1845 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1847 // Actual listing (no trailing slash) at root with advise
1848 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1", 'adviseStat' => 1 ) );
1849 $list = $this->listToArray( $iter );
1851 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1853 // Actual listing (with trailing slash) at root
1855 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1856 foreach ( $iter as $file ) {
1860 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1862 // Expected listing at subdir
1870 "sub/120-px-file.txt",
1874 // Actual listing (no trailing slash) at subdir
1875 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1876 $list = $this->listToArray( $iter );
1878 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1880 // Actual listing (no trailing slash) at subdir with advise
1881 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir", 'adviseStat' => 1 ) );
1882 $list = $this->listToArray( $iter );
1884 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1886 // Actual listing (with trailing slash) at subdir
1888 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1889 foreach ( $iter as $file ) {
1893 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1895 // Actual listing (using iterator second time)
1896 $list = $this->listToArray( $iter );
1898 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1900 // Actual listing (top files only) at root
1901 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1" ) );
1902 $list = $this->listToArray( $iter );
1904 $this->assertEquals( array(), $list, "Correct top file listing ($backendName)." );
1906 // Expected listing (top files only) at subdir
1916 // Actual listing (top files only) at subdir
1917 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1918 $list = $this->listToArray( $iter );
1920 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1922 // Actual listing (top files only) at subdir with advise
1923 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir", 'adviseStat' => 1 ) );
1924 $list = $this->listToArray( $iter );
1926 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1928 foreach ( $files as $file ) { // clean up
1929 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1932 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1933 foreach ( $iter as $iter ) {
1939 * @covers FileBackend::getTopDirectoryList
1940 * @covers FileBackend::getDirectoryList
1942 public function testGetDirectoryList() {
1943 $this->backend
= $this->singleBackend
;
1944 $this->tearDownFiles();
1945 $this->doTestGetDirectoryList();
1946 $this->tearDownFiles();
1948 $this->backend
= $this->multiBackend
;
1949 $this->tearDownFiles();
1950 $this->doTestGetDirectoryList();
1951 $this->tearDownFiles();
1954 private function doTestGetDirectoryList() {
1955 $backendName = $this->backendClass();
1957 $base = self
::baseStorePath();
1959 "$base/unittest-cont1/e/test1.txt",
1960 "$base/unittest-cont1/e/test2.txt",
1961 "$base/unittest-cont1/e/test3.txt",
1962 "$base/unittest-cont1/e/subdir1/test1.txt",
1963 "$base/unittest-cont1/e/subdir1/test2.txt",
1964 "$base/unittest-cont1/e/subdir2/test3.txt",
1965 "$base/unittest-cont1/e/subdir2/test4.txt",
1966 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1967 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1968 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1969 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1970 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1971 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1972 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1977 foreach ( $files as $file ) {
1978 $this->prepare( array( 'dir' => dirname( $file ) ) );
1979 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1981 $status = $this->backend
->doQuickOperations( $ops );
1982 $this->assertGoodStatus( $status,
1983 "Creation of files succeeded ($backendName)." );
1984 $this->assertEquals( true, $status->isOK(),
1985 "Creation of files succeeded with OK status ($backendName)." );
1987 $this->assertEquals( true,
1988 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1989 "Directory exists in ($backendName)." );
1990 $this->assertEquals( true,
1991 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1992 "Directory exists in ($backendName)." );
1993 $this->assertEquals( false,
1994 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1995 "Directory does not exists in ($backendName)." );
2003 // Actual listing (no trailing slash)
2005 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
2006 foreach ( $iter as $file ) {
2011 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2022 // Actual listing (no trailing slash)
2024 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
2025 foreach ( $iter as $file ) {
2030 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2032 // Actual listing (with trailing slash)
2034 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
2035 foreach ( $iter as $file ) {
2040 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2048 // Actual listing (no trailing slash)
2050 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
2051 foreach ( $iter as $file ) {
2056 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2058 // Actual listing (with trailing slash)
2060 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
2061 foreach ( $iter as $file ) {
2066 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2068 // Actual listing (using iterator second time)
2070 foreach ( $iter as $file ) {
2075 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
2077 // Expected listing (recursive)
2087 "e/subdir4/subdir/sub",
2091 // Actual listing (recursive)
2093 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
2094 foreach ( $iter as $file ) {
2099 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2101 // Expected listing (recursive)
2108 // Actual listing (recursive)
2110 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
2111 foreach ( $iter as $file ) {
2116 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2118 // Actual listing (recursive, second time)
2120 foreach ( $iter as $file ) {
2125 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2127 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) );
2128 $items = $this->listToArray( $iter );
2129 $this->assertEquals( array(), $items, "Directory listing is empty." );
2131 foreach ( $files as $file ) { // clean up
2132 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
2135 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
2136 foreach ( $iter as $file ) {
2140 $items = $this->listToArray( $iter );
2141 $this->assertEquals( array(), $items, "Directory listing is empty." );
2143 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/not/exists" ) );
2144 $items = $this->listToArray( $iter );
2145 $this->assertEquals( array(), $items, "Directory listing is empty." );
2149 * @covers FileBackend::lockFiles
2150 * @covers FileBackend::unlockFiles
2152 public function testLockCalls() {
2153 $this->backend
= $this->singleBackend
;
2154 $this->doTestLockCalls();
2157 private function doTestLockCalls() {
2158 $backendName = $this->backendClass();
2165 "subdir1", // duplicate
2166 "subdir1/test1.txt",
2167 "subdir1/test2.txt",
2169 "subdir2", // duplicate
2170 "subdir2/test3.txt",
2171 "subdir2/test4.txt",
2173 "subdir2/subdir/test1.txt",
2174 "subdir2/subdir/test2.txt",
2175 "subdir2/subdir/test3.txt",
2176 "subdir2/subdir/test4.txt",
2177 "subdir2/subdir/test5.txt",
2178 "subdir2/subdir/sub",
2179 "subdir2/subdir/sub/test0.txt",
2180 "subdir2/subdir/sub/120-px-file.txt",
2183 for ( $i = 0; $i < 25; $i++
) {
2184 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2185 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2186 "Locking of files succeeded ($backendName) ($i)." );
2187 $this->assertEquals( true, $status->isOK(),
2188 "Locking of files succeeded with OK status ($backendName) ($i)." );
2190 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2191 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2192 "Locking of files succeeded ($backendName) ($i)." );
2193 $this->assertEquals( true, $status->isOK(),
2194 "Locking of files succeeded with OK status ($backendName) ($i)." );
2196 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2197 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2198 "Locking of files succeeded ($backendName) ($i)." );
2199 $this->assertEquals( true, $status->isOK(),
2200 "Locking of files succeeded with OK status ($backendName) ($i)." );
2202 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2203 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2204 "Locking of files succeeded ($backendName). ($i)" );
2205 $this->assertEquals( true, $status->isOK(),
2206 "Locking of files succeeded with OK status ($backendName) ($i)." );
2208 ## Flip the acquire/release ordering around ##
2210 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2211 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2212 "Locking of files succeeded ($backendName) ($i)." );
2213 $this->assertEquals( true, $status->isOK(),
2214 "Locking of files succeeded with OK status ($backendName) ($i)." );
2216 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2217 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2218 "Locking of files succeeded ($backendName) ($i)." );
2219 $this->assertEquals( true, $status->isOK(),
2220 "Locking of files succeeded with OK status ($backendName) ($i)." );
2222 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2223 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2224 "Locking of files succeeded ($backendName). ($i)" );
2225 $this->assertEquals( true, $status->isOK(),
2226 "Locking of files succeeded with OK status ($backendName) ($i)." );
2228 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2229 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2230 "Locking of files succeeded ($backendName) ($i)." );
2231 $this->assertEquals( true, $status->isOK(),
2232 "Locking of files succeeded with OK status ($backendName) ($i)." );
2235 $status = Status
::newGood();
2236 $sl = $this->backend
->getScopedFileLocks( $paths, LockManager
::LOCK_EX
, $status );
2237 $this->assertType( 'ScopedLock', $sl,
2238 "Scoped locking of files succeeded ($backendName)." );
2239 $this->assertEquals( array(), $status->errors
,
2240 "Scoped locking of files succeeded ($backendName)." );
2241 $this->assertEquals( true, $status->isOK(),
2242 "Scoped locking of files succeeded with OK status ($backendName)." );
2244 ScopedLock
::release( $sl );
2245 $this->assertEquals( null, $sl,
2246 "Scoped unlocking of files succeeded ($backendName)." );
2247 $this->assertEquals( array(), $status->errors
,
2248 "Scoped unlocking of files succeeded ($backendName)." );
2249 $this->assertEquals( true, $status->isOK(),
2250 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2254 private function listToArray( $iter ) {
2255 return is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2258 // test helper wrapper for backend prepare() function
2259 private function prepare( array $params ) {
2260 return $this->backend
->prepare( $params );
2263 // test helper wrapper for backend prepare() function
2264 private function create( array $params ) {
2265 $params['op'] = 'create';
2267 return $this->backend
->doQuickOperations( array( $params ) );
2270 function tearDownFiles() {
2271 foreach ( $this->filesToPrune
as $file ) {
2272 if ( is_file( $file ) ) {
2276 $containers = array( 'unittest-cont1', 'unittest-cont2' );
2277 foreach ( $containers as $container ) {
2278 $this->deleteFiles( $container );
2280 $this->filesToPrune
= array();
2283 private function deleteFiles( $container ) {
2284 $base = self
::baseStorePath();
2285 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
2287 foreach ( $iter as $file ) {
2288 $this->backend
->quickDelete( array( 'src' => "$base/$container/$file" ) );
2290 // free the directory, to avoid Permission denied under windows on rmdir
2293 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
2296 function assertBackendPathsConsistent( array $paths ) {
2297 if ( $this->backend
instanceof FileBackendMultiWrite
) {
2298 $status = $this->backend
->consistencyCheck( $paths );
2299 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2303 function assertGoodStatus( $status, $msg ) {
2304 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );