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 if ( isset( $useConfig['fileJournal'] ) ) {
41 $useConfig['fileJournal'] = FileJournal
::factory( $useConfig['fileJournal'], $name );
43 $useConfig['lockManager'] = LockManagerGroup
::singleton()->get( $useConfig['lockManager'] );
44 $class = $useConfig['class'];
45 self
::$backendToUse = new $class( $useConfig );
46 $this->singleBackend
= self
::$backendToUse;
49 $this->singleBackend
= new FSFileBackend( array(
50 'name' => 'localtesting',
51 'lockManager' => LockManagerGroup
::singleton()->get( 'fsLockManager' ),
52 'wikiId' => wfWikiID(),
53 'containerPaths' => array(
54 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
55 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
58 $this->multiBackend
= new FileBackendMultiWrite( array(
59 'name' => 'localtesting',
60 'lockManager' => LockManagerGroup
::singleton()->get( 'fsLockManager' ),
61 'parallelize' => 'implicit',
62 'wikiId' => wfWikiId() . $uniqueId,
65 'name' => 'localmultitesting1',
66 'class' => 'FSFileBackend',
67 'containerPaths' => array(
68 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
69 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
70 'isMultiMaster' => false
73 'name' => 'localmultitesting2',
74 'class' => 'FSFileBackend',
75 'containerPaths' => array(
76 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
77 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
78 'isMultiMaster' => true
82 $this->filesToPrune
= array();
85 private static function baseStorePath() {
86 return 'mwstore://localtesting';
89 private function backendClass() {
90 return get_class( $this->backend
);
94 * @dataProvider provider_testIsStoragePath
95 * @covers FileBackend::isStoragePath
97 public function testIsStoragePath( $path, $isStorePath ) {
98 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
99 "FileBackend::isStoragePath on path '$path'" );
102 public static function provider_testIsStoragePath() {
104 array( 'mwstore://', true ),
105 array( 'mwstore://backend', true ),
106 array( 'mwstore://backend/container', true ),
107 array( 'mwstore://backend/container/', true ),
108 array( 'mwstore://backend/container/path', true ),
109 array( 'mwstore://backend//container/', true ),
110 array( 'mwstore://backend//container//', true ),
111 array( 'mwstore://backend//container//path', true ),
112 array( 'mwstore:///', true ),
113 array( 'mwstore:/', false ),
114 array( 'mwstore:', false ),
119 * @dataProvider provider_testSplitStoragePath
120 * @covers FileBackend::splitStoragePath
122 public function testSplitStoragePath( $path, $res ) {
123 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
124 "FileBackend::splitStoragePath on path '$path'" );
127 public static function provider_testSplitStoragePath() {
129 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
130 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
131 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
132 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
133 array( 'mwstore://backend//container/path', array( null, null, null ) ),
134 array( 'mwstore://backend//container//path', array( null, null, null ) ),
135 array( 'mwstore://', array( null, null, null ) ),
136 array( 'mwstore://backend', array( null, null, null ) ),
137 array( 'mwstore:///', array( null, null, null ) ),
138 array( 'mwstore:/', array( null, null, null ) ),
139 array( 'mwstore:', array( null, null, null ) )
144 * @dataProvider provider_normalizeStoragePath
145 * @covers FileBackend::normalizeStoragePath
147 public function testNormalizeStoragePath( $path, $res ) {
148 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
149 "FileBackend::normalizeStoragePath on path '$path'" );
152 public static function provider_normalizeStoragePath() {
154 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
155 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
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', 'mwstore://backend/container/path' ),
160 'mwstore://backend/container///path//to///obj',
161 'mwstore://backend/container/path/to/obj'
163 array( 'mwstore://', null ),
164 array( 'mwstore://backend', null ),
165 array( 'mwstore://backend//container/path', null ),
166 array( 'mwstore://backend//container//path', null ),
167 array( 'mwstore:///', null ),
168 array( 'mwstore:/', null ),
169 array( 'mwstore:', null ),
174 * @dataProvider provider_testParentStoragePath
175 * @covers FileBackend::parentStoragePath
177 public function testParentStoragePath( $path, $res ) {
178 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
179 "FileBackend::parentStoragePath on path '$path'" );
182 public static function provider_testParentStoragePath() {
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 ),
188 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
189 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
190 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
191 array( 'mwstore://backend/container/', null ),
196 * @dataProvider provider_testExtensionFromPath
197 * @covers FileBackend::extensionFromPath
199 public function testExtensionFromPath( $path, $res ) {
200 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
201 "FileBackend::extensionFromPath on path '$path'" );
204 public static function provider_testExtensionFromPath() {
206 array( 'mwstore://backend/container/path.txt', 'txt' ),
207 array( 'mwstore://backend/container/path.svg.png', 'png' ),
208 array( 'mwstore://backend/container/path', '' ),
209 array( 'mwstore://backend/container/path.', '' ),
214 * @dataProvider provider_testStore
216 public function testStore( $op ) {
217 $this->filesToPrune
[] = $op['src'];
219 $this->backend
= $this->singleBackend
;
220 $this->tearDownFiles();
221 $this->doTestStore( $op );
222 $this->tearDownFiles();
224 $this->backend
= $this->multiBackend
;
225 $this->tearDownFiles();
226 $this->doTestStore( $op );
227 $this->filesToPrune
[] = $op['src']; # avoid file leaking
228 $this->tearDownFiles();
232 * @covers FileBackend::doOperation
234 private function doTestStore( $op ) {
235 $backendName = $this->backendClass();
237 $source = $op['src'];
239 $this->prepare( array( 'dir' => dirname( $dest ) ) );
241 file_put_contents( $source, "Unit test file" );
243 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
244 $this->backend
->store( $op );
247 $status = $this->backend
->doOperation( $op );
249 $this->assertGoodStatus( $status,
250 "Store from $source to $dest succeeded without warnings ($backendName)." );
251 $this->assertEquals( true, $status->isOK(),
252 "Store from $source to $dest succeeded ($backendName)." );
253 $this->assertEquals( array( 0 => true ), $status->success
,
254 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
255 $this->assertEquals( true, file_exists( $source ),
256 "Source file $source still exists ($backendName)." );
257 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
258 "Destination file $dest exists ($backendName)." );
260 $this->assertEquals( filesize( $source ),
261 $this->backend
->getFileSize( array( 'src' => $dest ) ),
262 "Destination file $dest has correct size ($backendName)." );
264 $props1 = FSFile
::getPropsFromPath( $source );
265 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
266 $this->assertEquals( $props1, $props2,
267 "Source and destination have the same props ($backendName)." );
269 $this->assertBackendPathsConsistent( array( $dest ) );
272 public static function provider_testStore() {
275 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
276 $toPath = self
::baseStorePath() . '/unittest-cont1/e/fun/obj1.txt';
277 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
285 $op2['overwrite'] = true;
293 $op2['overwriteSame'] = true;
304 * @dataProvider provider_testCopy
305 * @covers FileBackend::doOperation
307 public function testCopy( $op ) {
308 $this->backend
= $this->singleBackend
;
309 $this->tearDownFiles();
310 $this->doTestCopy( $op );
311 $this->tearDownFiles();
313 $this->backend
= $this->multiBackend
;
314 $this->tearDownFiles();
315 $this->doTestCopy( $op );
316 $this->tearDownFiles();
319 private function doTestCopy( $op ) {
320 $backendName = $this->backendClass();
322 $source = $op['src'];
324 $this->prepare( array( 'dir' => dirname( $source ) ) );
325 $this->prepare( array( 'dir' => dirname( $dest ) ) );
327 if ( isset( $op['ignoreMissingSource'] ) ) {
328 $status = $this->backend
->doOperation( $op );
329 $this->assertGoodStatus( $status,
330 "Move from $source to $dest succeeded without warnings ($backendName)." );
331 $this->assertEquals( array( 0 => true ), $status->success
,
332 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
333 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
334 "Source file $source does not exist ($backendName)." );
335 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $dest ) ),
336 "Destination file $dest does not exist ($backendName)." );
341 $status = $this->backend
->doOperation(
342 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
343 $this->assertGoodStatus( $status,
344 "Creation of file at $source succeeded ($backendName)." );
346 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
347 $this->backend
->copy( $op );
350 $status = $this->backend
->doOperation( $op );
352 $this->assertGoodStatus( $status,
353 "Copy from $source to $dest succeeded without warnings ($backendName)." );
354 $this->assertEquals( true, $status->isOK(),
355 "Copy from $source to $dest succeeded ($backendName)." );
356 $this->assertEquals( array( 0 => true ), $status->success
,
357 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
358 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
359 "Source file $source still exists ($backendName)." );
360 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
361 "Destination file $dest exists after copy ($backendName)." );
364 $this->backend
->getFileSize( array( 'src' => $source ) ),
365 $this->backend
->getFileSize( array( 'src' => $dest ) ),
366 "Destination file $dest has correct size ($backendName)." );
368 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
369 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
370 $this->assertEquals( $props1, $props2,
371 "Source and destination have the same props ($backendName)." );
373 $this->assertBackendPathsConsistent( array( $source, $dest ) );
376 public static function provider_testCopy() {
379 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
380 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
382 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
390 $op2['overwrite'] = true;
398 $op2['overwriteSame'] = true;
406 $op2['ignoreMissingSource'] = true;
414 $op2['ignoreMissingSource'] = true;
417 self
::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
425 * @dataProvider provider_testMove
426 * @covers FileBackend::doOperation
428 public function testMove( $op ) {
429 $this->backend
= $this->singleBackend
;
430 $this->tearDownFiles();
431 $this->doTestMove( $op );
432 $this->tearDownFiles();
434 $this->backend
= $this->multiBackend
;
435 $this->tearDownFiles();
436 $this->doTestMove( $op );
437 $this->tearDownFiles();
440 private function doTestMove( $op ) {
441 $backendName = $this->backendClass();
443 $source = $op['src'];
445 $this->prepare( array( 'dir' => dirname( $source ) ) );
446 $this->prepare( array( 'dir' => dirname( $dest ) ) );
448 if ( isset( $op['ignoreMissingSource'] ) ) {
449 $status = $this->backend
->doOperation( $op );
450 $this->assertGoodStatus( $status,
451 "Move from $source to $dest succeeded without warnings ($backendName)." );
452 $this->assertEquals( array( 0 => true ), $status->success
,
453 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
454 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
455 "Source file $source does not exist ($backendName)." );
456 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $dest ) ),
457 "Destination file $dest does not exist ($backendName)." );
462 $status = $this->backend
->doOperation(
463 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
464 $this->assertGoodStatus( $status,
465 "Creation of file at $source succeeded ($backendName)." );
467 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
468 $this->backend
->copy( $op );
471 $status = $this->backend
->doOperation( $op );
472 $this->assertGoodStatus( $status,
473 "Move from $source to $dest succeeded without warnings ($backendName)." );
474 $this->assertEquals( true, $status->isOK(),
475 "Move from $source to $dest succeeded ($backendName)." );
476 $this->assertEquals( array( 0 => true ), $status->success
,
477 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
478 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
479 "Source file $source does not still exists ($backendName)." );
480 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
481 "Destination file $dest exists after move ($backendName)." );
483 $this->assertNotEquals(
484 $this->backend
->getFileSize( array( 'src' => $source ) ),
485 $this->backend
->getFileSize( array( 'src' => $dest ) ),
486 "Destination file $dest has correct size ($backendName)." );
488 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
489 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
490 $this->assertEquals( false, $props1['fileExists'],
491 "Source file does not exist accourding to props ($backendName)." );
492 $this->assertEquals( true, $props2['fileExists'],
493 "Destination file exists accourding to props ($backendName)." );
495 $this->assertBackendPathsConsistent( array( $source, $dest ) );
498 public static function provider_testMove() {
501 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
502 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
504 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
512 $op2['overwrite'] = true;
520 $op2['overwriteSame'] = true;
528 $op2['ignoreMissingSource'] = true;
536 $op2['ignoreMissingSource'] = true;
539 self
::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
547 * @dataProvider provider_testDelete
548 * @covers FileBackend::doOperation
550 public function testDelete( $op, $withSource, $okStatus ) {
551 $this->backend
= $this->singleBackend
;
552 $this->tearDownFiles();
553 $this->doTestDelete( $op, $withSource, $okStatus );
554 $this->tearDownFiles();
556 $this->backend
= $this->multiBackend
;
557 $this->tearDownFiles();
558 $this->doTestDelete( $op, $withSource, $okStatus );
559 $this->tearDownFiles();
562 private function doTestDelete( $op, $withSource, $okStatus ) {
563 $backendName = $this->backendClass();
565 $source = $op['src'];
566 $this->prepare( array( 'dir' => dirname( $source ) ) );
569 $status = $this->backend
->doOperation(
570 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
571 $this->assertGoodStatus( $status,
572 "Creation of file at $source succeeded ($backendName)." );
575 $status = $this->backend
->doOperation( $op );
577 $this->assertGoodStatus( $status,
578 "Deletion of file at $source succeeded without warnings ($backendName)." );
579 $this->assertEquals( true, $status->isOK(),
580 "Deletion of file at $source succeeded ($backendName)." );
581 $this->assertEquals( array( 0 => true ), $status->success
,
582 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
584 $this->assertEquals( false, $status->isOK(),
585 "Deletion of file at $source failed ($backendName)." );
588 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
589 "Source file $source does not exist after move ($backendName)." );
592 $this->backend
->getFileSize( array( 'src' => $source ) ),
593 "Source file $source has correct size (false) ($backendName)." );
595 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
596 $this->assertFalse( $props1['fileExists'],
597 "Source file $source does not exist according to props ($backendName)." );
599 $this->assertBackendPathsConsistent( array( $source ) );
602 public static function provider_testDelete() {
605 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
607 $op = array( 'op' => 'delete', 'src' => $source );
616 false, // without source
620 $op['ignoreMissingSource'] = true;
623 false, // without source
627 $op['ignoreMissingSource'] = true;
628 $op['src'] = self
::baseStorePath() . '/unittest-cont-bad/e/file.txt';
631 false, // without source
639 * @dataProvider provider_testDescribe
640 * @covers FileBackend::doOperation
642 public function testDescribe( $op, $withSource, $okStatus ) {
643 $this->backend
= $this->singleBackend
;
644 $this->tearDownFiles();
645 $this->doTestDescribe( $op, $withSource, $okStatus );
646 $this->tearDownFiles();
648 $this->backend
= $this->multiBackend
;
649 $this->tearDownFiles();
650 $this->doTestDescribe( $op, $withSource, $okStatus );
651 $this->tearDownFiles();
654 private function doTestDescribe( $op, $withSource, $okStatus ) {
655 $backendName = $this->backendClass();
657 $source = $op['src'];
658 $this->prepare( array( 'dir' => dirname( $source ) ) );
661 $status = $this->backend
->doOperation(
662 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source,
663 'headers' => array( 'Content-Disposition' => 'xxx' ) ) );
664 $this->assertGoodStatus( $status,
665 "Creation of file at $source succeeded ($backendName)." );
666 if ( $this->backend
->hasFeatures( FileBackend
::ATTR_HEADERS
) ) {
667 $attr = $this->backend
->getFileXAttributes( array( 'src' => $source ) );
668 $this->assertHasHeaders( array( 'Content-Disposition' => 'xxx' ), $attr );
671 $status = $this->backend
->describe( array( 'src' => $source,
672 'headers' => array( 'Content-Disposition' => '' ) ) ); // remove
673 $this->assertGoodStatus( $status,
674 "Removal of header for $source succeeded ($backendName)." );
676 if ( $this->backend
->hasFeatures( FileBackend
::ATTR_HEADERS
) ) {
677 $attr = $this->backend
->getFileXAttributes( array( 'src' => $source ) );
678 $this->assertFalse( isset( $attr['headers']['content-disposition'] ),
679 "File 'Content-Disposition' header removed." );
683 $status = $this->backend
->doOperation( $op );
685 $this->assertGoodStatus( $status,
686 "Describe of file at $source succeeded without warnings ($backendName)." );
687 $this->assertEquals( true, $status->isOK(),
688 "Describe of file at $source succeeded ($backendName)." );
689 $this->assertEquals( array( 0 => true ), $status->success
,
690 "Describe of file at $source has proper 'success' field in Status ($backendName)." );
691 if ( $this->backend
->hasFeatures( FileBackend
::ATTR_HEADERS
) ) {
692 $attr = $this->backend
->getFileXAttributes( array( 'src' => $source ) );
693 $this->assertHasHeaders( $op['headers'], $attr );
696 $this->assertEquals( false, $status->isOK(),
697 "Describe of file at $source failed ($backendName)." );
700 $this->assertBackendPathsConsistent( array( $source ) );
703 private function assertHasHeaders( array $headers, array $attr ) {
704 foreach ( $headers as $n => $v ) {
706 $this->assertTrue( isset( $attr['headers'][strtolower( $n )] ),
707 "File has '$n' header." );
708 $this->assertEquals( $v, $attr['headers'][strtolower( $n )],
709 "File has '$n' header value." );
711 $this->assertFalse( isset( $attr['headers'][strtolower( $n )] ),
712 "File does not have '$n' header." );
717 public static function provider_testDescribe() {
720 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
722 $op = array( 'op' => 'describe', 'src' => $source,
723 'headers' => array( 'Content-Disposition' => 'inline' ), );
732 false, // without source
740 * @dataProvider provider_testCreate
741 * @covers FileBackend::doOperation
743 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
744 $this->backend
= $this->singleBackend
;
745 $this->tearDownFiles();
746 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
747 $this->tearDownFiles();
749 $this->backend
= $this->multiBackend
;
750 $this->tearDownFiles();
751 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
752 $this->tearDownFiles();
755 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
756 $backendName = $this->backendClass();
759 $this->prepare( array( 'dir' => dirname( $dest ) ) );
761 $oldText = 'blah...blah...waahwaah';
762 if ( $alreadyExists ) {
763 $status = $this->backend
->doOperation(
764 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
765 $this->assertGoodStatus( $status,
766 "Creation of file at $dest succeeded ($backendName)." );
769 $status = $this->backend
->doOperation( $op );
771 $this->assertGoodStatus( $status,
772 "Creation of file at $dest succeeded without warnings ($backendName)." );
773 $this->assertEquals( true, $status->isOK(),
774 "Creation of file at $dest succeeded ($backendName)." );
775 $this->assertEquals( array( 0 => true ), $status->success
,
776 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
778 $this->assertEquals( false, $status->isOK(),
779 "Creation of file at $dest failed ($backendName)." );
782 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
783 "Destination file $dest exists after creation ($backendName)." );
785 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
786 $this->assertEquals( true, $props1['fileExists'],
787 "Destination file $dest exists according to props ($backendName)." );
788 if ( $okStatus ) { // file content is what we saved
789 $this->assertEquals( $newSize, $props1['size'],
790 "Destination file $dest has expected size according to props ($backendName)." );
791 $this->assertEquals( $newSize,
792 $this->backend
->getFileSize( array( 'src' => $dest ) ),
793 "Destination file $dest has correct size ($backendName)." );
794 } else { // file content is some other previous text
795 $this->assertEquals( strlen( $oldText ), $props1['size'],
796 "Destination file $dest has original size according to props ($backendName)." );
797 $this->assertEquals( strlen( $oldText ),
798 $this->backend
->getFileSize( array( 'src' => $dest ) ),
799 "Destination file $dest has original size according to props ($backendName)." );
802 $this->assertBackendPathsConsistent( array( $dest ) );
806 * @dataProvider provider_testCreate
808 public static function provider_testCreate() {
811 $dest = self
::baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
813 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
816 false, // no dest already exists
818 strlen( $op['content'] )
822 $op2['content'] = "\n";
825 false, // no dest already exists
827 strlen( $op2['content'] )
831 $op2['content'] = "fsf\n waf 3kt";
834 true, // dest already exists
836 strlen( $op2['content'] )
840 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
841 $op2['overwrite'] = true;
844 true, // dest already exists
846 strlen( $op2['content'] )
850 $op2['content'] = "39qjmg3-qg";
851 $op2['overwriteSame'] = true;
854 true, // dest already exists
856 strlen( $op2['content'] )
863 * @covers FileBackend::doQuickOperations
865 public function testDoQuickOperations() {
866 $this->backend
= $this->singleBackend
;
867 $this->doTestDoQuickOperations();
868 $this->tearDownFiles();
870 $this->backend
= $this->multiBackend
;
871 $this->doTestDoQuickOperations();
872 $this->tearDownFiles();
875 private function doTestDoQuickOperations() {
876 $backendName = $this->backendClass();
878 $base = self
::baseStorePath();
880 "$base/unittest-cont1/e/fileA.a",
881 "$base/unittest-cont1/e/fileB.a",
882 "$base/unittest-cont1/e/fileC.a"
884 $createOps = array();
886 foreach ( $files as $path ) {
887 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
888 $this->assertGoodStatus( $status,
889 "Preparing $path succeeded without warnings ($backendName)." );
890 $createOps[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand( 0, 50000 ) );
891 $copyOps[] = array( 'op' => 'copy', 'src' => $path, 'dst' => "$path-2" );
892 $moveOps[] = array( 'op' => 'move', 'src' => "$path-2", 'dst' => "$path-3" );
893 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
894 $purgeOps[] = array( 'op' => 'delete', 'src' => "$path-3" );
896 $purgeOps[] = array( 'op' => 'null' );
898 $this->assertGoodStatus(
899 $this->backend
->doQuickOperations( $createOps ),
900 "Creation of source files succeeded ($backendName)." );
901 foreach ( $files as $file ) {
902 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $file ) ),
903 "File $file exists." );
906 $this->assertGoodStatus(
907 $this->backend
->doQuickOperations( $copyOps ),
908 "Quick copy of source files succeeded ($backendName)." );
909 foreach ( $files as $file ) {
910 $this->assertTrue( $this->backend
->fileExists( array( 'src' => "$file-2" ) ),
911 "File $file-2 exists." );
914 $this->assertGoodStatus(
915 $this->backend
->doQuickOperations( $moveOps ),
916 "Quick move of source files succeeded ($backendName)." );
917 foreach ( $files as $file ) {
918 $this->assertTrue( $this->backend
->fileExists( array( 'src' => "$file-3" ) ),
919 "File $file-3 move in." );
920 $this->assertFalse( $this->backend
->fileExists( array( 'src' => "$file-2" ) ),
921 "File $file-2 moved away." );
924 $this->assertGoodStatus(
925 $this->backend
->quickCopy( array( 'src' => $files[0], 'dst' => $files[0] ) ),
926 "Copy of file {$files[0]} over itself succeeded ($backendName)." );
927 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $files[0] ) ),
928 "File {$files[0]} still exists." );
930 $this->assertGoodStatus(
931 $this->backend
->quickMove( array( 'src' => $files[0], 'dst' => $files[0] ) ),
932 "Move of file {$files[0]} over itself succeeded ($backendName)." );
933 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $files[0] ) ),
934 "File {$files[0]} still exists." );
936 $this->assertGoodStatus(
937 $this->backend
->doQuickOperations( $purgeOps ),
938 "Quick deletion of source files succeeded ($backendName)." );
939 foreach ( $files as $file ) {
940 $this->assertFalse( $this->backend
->fileExists( array( 'src' => $file ) ),
941 "File $file purged." );
942 $this->assertFalse( $this->backend
->fileExists( array( 'src' => "$file-3" ) ),
943 "File $file-3 purged." );
948 * @dataProvider provider_testConcatenate
950 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
951 $this->filesToPrune
[] = $op['dst'];
953 $this->backend
= $this->singleBackend
;
954 $this->tearDownFiles();
955 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
956 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
957 $this->tearDownFiles();
959 $this->backend
= $this->multiBackend
;
960 $this->tearDownFiles();
961 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
962 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
963 $this->tearDownFiles();
966 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
967 $backendName = $this->backendClass();
972 foreach ( $srcs as $i => $source ) {
973 $this->prepare( array( 'dir' => dirname( $source ) ) );
975 'op' => 'create', // operation
976 'dst' => $source, // source
977 'content' => $srcsContent[$i]
979 $expContent .= $srcsContent[$i];
981 $status = $this->backend
->doOperations( $ops );
983 $this->assertGoodStatus( $status,
984 "Creation of source files succeeded ($backendName)." );
986 $dest = $params['dst'];
987 if ( $alreadyExists ) {
988 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
989 $this->assertEquals( true, $ok,
990 "Creation of file at $dest succeeded ($backendName)." );
992 $ok = file_put_contents( $dest, '' ) !== false;
993 $this->assertEquals( true, $ok,
994 "Creation of 0-byte file at $dest succeeded ($backendName)." );
997 // Combine the files into one
998 $status = $this->backend
->concatenate( $params );
1000 $this->assertGoodStatus( $status,
1001 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
1002 $this->assertEquals( true, $status->isOK(),
1003 "Creation of concat file at $dest succeeded ($backendName)." );
1005 $this->assertEquals( false, $status->isOK(),
1006 "Creation of concat file at $dest failed ($backendName)." );
1010 $this->assertEquals( true, is_file( $dest ),
1011 "Dest concat file $dest exists after creation ($backendName)." );
1013 $this->assertEquals( true, is_file( $dest ),
1014 "Dest concat file $dest exists after failed creation ($backendName)." );
1017 $contents = file_get_contents( $dest );
1018 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
1021 $this->assertEquals( $expContent, $contents,
1022 "Concat file at $dest has correct contents ($backendName)." );
1024 $this->assertNotEquals( $expContent, $contents,
1025 "Concat file at $dest has correct contents ($backendName)." );
1029 public static function provider_testConcatenate() {
1032 $rand = mt_rand( 0, 2000000000 ) . time();
1033 $dest = wfTempDir() . "/randomfile!$rand.txt";
1035 self
::baseStorePath() . '/unittest-cont1/e/file1.txt',
1036 self
::baseStorePath() . '/unittest-cont1/e/file2.txt',
1037 self
::baseStorePath() . '/unittest-cont1/e/file3.txt',
1038 self
::baseStorePath() . '/unittest-cont1/e/file4.txt',
1039 self
::baseStorePath() . '/unittest-cont1/e/file5.txt',
1040 self
::baseStorePath() . '/unittest-cont1/e/file6.txt',
1041 self
::baseStorePath() . '/unittest-cont1/e/file7.txt',
1042 self
::baseStorePath() . '/unittest-cont1/e/file8.txt',
1043 self
::baseStorePath() . '/unittest-cont1/e/file9.txt',
1044 self
::baseStorePath() . '/unittest-cont1/e/file10.txt'
1058 $params = array( 'srcs' => $srcs, 'dst' => $dest );
1061 $params, // operation
1063 $content, // content for each source
1064 false, // no dest already exists
1069 $params, // operation
1071 $content, // content for each source
1072 true, // dest already exists
1080 * @dataProvider provider_testGetFileStat
1081 * @covers FileBackend::getFileStat
1083 public function testGetFileStat( $path, $content, $alreadyExists ) {
1084 $this->backend
= $this->singleBackend
;
1085 $this->tearDownFiles();
1086 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1087 $this->tearDownFiles();
1089 $this->backend
= $this->multiBackend
;
1090 $this->tearDownFiles();
1091 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1092 $this->tearDownFiles();
1095 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
1096 $backendName = $this->backendClass();
1098 if ( $alreadyExists ) {
1099 $this->prepare( array( 'dir' => dirname( $path ) ) );
1100 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
1101 $this->assertGoodStatus( $status,
1102 "Creation of file at $path succeeded ($backendName)." );
1104 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1105 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1106 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
1108 $this->assertEquals( strlen( $content ), $size,
1109 "Correct file size of '$path'" );
1110 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1111 "Correct file timestamp of '$path'" );
1113 $size = $stat['size'];
1114 $time = $stat['mtime'];
1115 $this->assertEquals( strlen( $content ), $size,
1116 "Correct file size of '$path'" );
1117 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1118 "Correct file timestamp of '$path'" );
1120 $this->backend
->clearCache( array( $path ) );
1122 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1124 $this->assertEquals( strlen( $content ), $size,
1125 "Correct file size of '$path'" );
1127 $this->backend
->preloadCache( array( $path ) );
1129 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1131 $this->assertEquals( strlen( $content ), $size,
1132 "Correct file size of '$path'" );
1134 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
1135 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1136 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
1138 $this->assertFalse( $size, "Correct file size of '$path'" );
1139 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1140 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1144 public static function provider_testGetFileStat() {
1147 $base = self
::baseStorePath();
1148 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
1149 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
1150 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
1156 * @dataProvider provider_testGetFileStat
1157 * @covers FileBackend::streamFile
1159 public function testStreamFile( $path, $content, $alreadyExists ) {
1160 $this->backend
= $this->singleBackend
;
1161 $this->tearDownFiles();
1162 $this->doTestStreamFile( $path, $content, $alreadyExists );
1163 $this->tearDownFiles();
1166 private function doTestStreamFile( $path, $content ) {
1167 $backendName = $this->backendClass();
1169 // Test doStreamFile() directly to avoid header madness
1170 $class = new ReflectionClass( $this->backend
);
1171 $method = $class->getMethod( 'doStreamFile' );
1172 $method->setAccessible( true );
1174 if ( $content !== null ) {
1175 $this->prepare( array( 'dir' => dirname( $path ) ) );
1176 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
1177 $this->assertGoodStatus( $status,
1178 "Creation of file at $path succeeded ($backendName)." );
1181 $method->invokeArgs( $this->backend
, array( array( 'src' => $path ) ) );
1182 $data = ob_get_contents();
1185 $this->assertEquals( $content, $data, "Correct content streamed from '$path'" );
1186 } else { // 404 case
1188 $method->invokeArgs( $this->backend
, array( array( 'src' => $path ) ) );
1189 $data = ob_get_contents();
1192 $this->assertEquals( '', $data, "Correct content streamed from '$path' ($backendName)" );
1196 public static function provider_testStreamFile() {
1199 $base = self
::baseStorePath();
1200 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
1201 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", null );
1207 * @dataProvider provider_testGetFileContents
1208 * @covers FileBackend::getFileContents
1209 * @covers FileBackend::getFileContentsMulti
1211 public function testGetFileContents( $source, $content ) {
1212 $this->backend
= $this->singleBackend
;
1213 $this->tearDownFiles();
1214 $this->doTestGetFileContents( $source, $content );
1215 $this->tearDownFiles();
1217 $this->backend
= $this->multiBackend
;
1218 $this->tearDownFiles();
1219 $this->doTestGetFileContents( $source, $content );
1220 $this->tearDownFiles();
1223 private function doTestGetFileContents( $source, $content ) {
1224 $backendName = $this->backendClass();
1226 $srcs = (array)$source;
1227 $content = (array)$content;
1228 foreach ( $srcs as $i => $src ) {
1229 $this->prepare( array( 'dir' => dirname( $src ) ) );
1230 $status = $this->backend
->doOperation(
1231 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1232 $this->assertGoodStatus( $status,
1233 "Creation of file at $src succeeded ($backendName)." );
1236 if ( is_array( $source ) ) {
1237 $contents = $this->backend
->getFileContentsMulti( array( 'srcs' => $source ) );
1238 foreach ( $contents as $path => $data ) {
1239 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1240 $this->assertEquals(
1241 current( $content ),
1243 "Contents of $path is correct ($backendName)."
1247 $this->assertEquals(
1249 array_keys( $contents ),
1250 "Contents in right order ($backendName)."
1252 $this->assertEquals(
1255 "Contents array size correct ($backendName)."
1258 $data = $this->backend
->getFileContents( array( 'src' => $source ) );
1259 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1260 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1264 public static function provider_testGetFileContents() {
1267 $base = self
::baseStorePath();
1268 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
1269 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
1271 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1272 "$base/unittest-cont1/e/a/z.txt" ),
1273 array( "contents xx", "contents xy", "contents xz" )
1280 * @dataProvider provider_testGetLocalCopy
1281 * @covers FileBackend::getLocalCopy
1283 public function testGetLocalCopy( $source, $content ) {
1284 $this->backend
= $this->singleBackend
;
1285 $this->tearDownFiles();
1286 $this->doTestGetLocalCopy( $source, $content );
1287 $this->tearDownFiles();
1289 $this->backend
= $this->multiBackend
;
1290 $this->tearDownFiles();
1291 $this->doTestGetLocalCopy( $source, $content );
1292 $this->tearDownFiles();
1295 private function doTestGetLocalCopy( $source, $content ) {
1296 $backendName = $this->backendClass();
1298 $srcs = (array)$source;
1299 $content = (array)$content;
1300 foreach ( $srcs as $i => $src ) {
1301 $this->prepare( array( 'dir' => dirname( $src ) ) );
1302 $status = $this->backend
->doOperation(
1303 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1304 $this->assertGoodStatus( $status,
1305 "Creation of file at $src succeeded ($backendName)." );
1308 if ( is_array( $source ) ) {
1309 $tmpFiles = $this->backend
->getLocalCopyMulti( array( 'srcs' => $source ) );
1310 foreach ( $tmpFiles as $path => $tmpFile ) {
1311 $this->assertNotNull( $tmpFile,
1312 "Creation of local copy of $path succeeded ($backendName)." );
1313 $contents = file_get_contents( $tmpFile->getPath() );
1314 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1315 $this->assertEquals(
1316 current( $content ),
1318 "Local copy of $path is correct ($backendName)."
1322 $this->assertEquals(
1324 array_keys( $tmpFiles ),
1325 "Local copies in right order ($backendName)."
1327 $this->assertEquals(
1330 "Local copies array size correct ($backendName)."
1333 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
1334 $this->assertNotNull( $tmpFile,
1335 "Creation of local copy of $source succeeded ($backendName)." );
1336 $contents = file_get_contents( $tmpFile->getPath() );
1337 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1338 $this->assertEquals(
1341 "Local copy of $source is correct ($backendName)."
1345 $obj = new stdClass();
1346 $tmpFile->bind( $obj );
1349 public static function provider_testGetLocalCopy() {
1352 $base = self
::baseStorePath();
1353 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1354 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1355 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1357 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1358 "$base/unittest-cont1/e/a/z.txt" ),
1359 array( "contents xx $", "contents xy 111", "contents xz" )
1366 * @dataProvider provider_testGetLocalReference
1367 * @covers FileBackend::getLocalReference
1369 public function testGetLocalReference( $source, $content ) {
1370 $this->backend
= $this->singleBackend
;
1371 $this->tearDownFiles();
1372 $this->doTestGetLocalReference( $source, $content );
1373 $this->tearDownFiles();
1375 $this->backend
= $this->multiBackend
;
1376 $this->tearDownFiles();
1377 $this->doTestGetLocalReference( $source, $content );
1378 $this->tearDownFiles();
1381 private function doTestGetLocalReference( $source, $content ) {
1382 $backendName = $this->backendClass();
1384 $srcs = (array)$source;
1385 $content = (array)$content;
1386 foreach ( $srcs as $i => $src ) {
1387 $this->prepare( array( 'dir' => dirname( $src ) ) );
1388 $status = $this->backend
->doOperation(
1389 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1390 $this->assertGoodStatus( $status,
1391 "Creation of file at $src succeeded ($backendName)." );
1394 if ( is_array( $source ) ) {
1395 $tmpFiles = $this->backend
->getLocalReferenceMulti( array( 'srcs' => $source ) );
1396 foreach ( $tmpFiles as $path => $tmpFile ) {
1397 $this->assertNotNull( $tmpFile,
1398 "Creation of local copy of $path succeeded ($backendName)." );
1399 $contents = file_get_contents( $tmpFile->getPath() );
1400 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1401 $this->assertEquals(
1402 current( $content ),
1404 "Local ref of $path is correct ($backendName)."
1408 $this->assertEquals(
1410 array_keys( $tmpFiles ),
1411 "Local refs in right order ($backendName)."
1413 $this->assertEquals(
1416 "Local refs array size correct ($backendName)."
1419 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
1420 $this->assertNotNull( $tmpFile,
1421 "Creation of local copy of $source succeeded ($backendName)." );
1422 $contents = file_get_contents( $tmpFile->getPath() );
1423 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1424 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1428 public static function provider_testGetLocalReference() {
1431 $base = self
::baseStorePath();
1432 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1433 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1434 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1436 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1437 "$base/unittest-cont1/e/a/z.txt" ),
1438 array( "contents xx 1111", "contents xy %", "contents xz $" )
1445 * @covers FileBackend::getLocalCopy
1446 * @covers FileBackend::getLocalReference
1448 public function testGetLocalCopyAndReference404() {
1449 $this->backend
= $this->singleBackend
;
1450 $this->tearDownFiles();
1451 $this->doTestGetLocalCopyAndReference404();
1452 $this->tearDownFiles();
1454 $this->backend
= $this->multiBackend
;
1455 $this->tearDownFiles();
1456 $this->doTestGetLocalCopyAndReference404();
1457 $this->tearDownFiles();
1460 public function doTestGetLocalCopyAndReference404() {
1461 $backendName = $this->backendClass();
1463 $base = self
::baseStorePath();
1465 $tmpFile = $this->backend
->getLocalCopy( array(
1466 'src' => "$base/unittest-cont1/not-there" ) );
1467 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1469 $tmpFile = $this->backend
->getLocalReference( array(
1470 'src' => "$base/unittest-cont1/not-there" ) );
1471 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1475 * @dataProvider provider_testGetFileHttpUrl
1476 * @covers FileBackend::getFileHttpUrl
1478 public function testGetFileHttpUrl( $source, $content ) {
1479 $this->backend
= $this->singleBackend
;
1480 $this->tearDownFiles();
1481 $this->doTestGetFileHttpUrl( $source, $content );
1482 $this->tearDownFiles();
1484 $this->backend
= $this->multiBackend
;
1485 $this->tearDownFiles();
1486 $this->doTestGetFileHttpUrl( $source, $content );
1487 $this->tearDownFiles();
1490 private function doTestGetFileHttpUrl( $source, $content ) {
1491 $backendName = $this->backendClass();
1493 $this->prepare( array( 'dir' => dirname( $source ) ) );
1494 $status = $this->backend
->doOperation(
1495 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
1496 $this->assertGoodStatus( $status,
1497 "Creation of file at $source succeeded ($backendName)." );
1499 $url = $this->backend
->getFileHttpUrl( array( 'src' => $source ) );
1501 if ( $url !== null ) { // supported
1502 $data = Http
::request( "GET", $url );
1503 $this->assertEquals( $content, $data,
1504 "HTTP GET of URL has right contents ($backendName)." );
1508 public static function provider_testGetFileHttpUrl() {
1511 $base = self
::baseStorePath();
1512 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1513 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1514 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1520 * @dataProvider provider_testPrepareAndClean
1521 * @covers FileBackend::prepare
1522 * @covers FileBackend::clean
1524 public function testPrepareAndClean( $path, $isOK ) {
1525 $this->backend
= $this->singleBackend
;
1526 $this->doTestPrepareAndClean( $path, $isOK );
1527 $this->tearDownFiles();
1529 $this->backend
= $this->multiBackend
;
1530 $this->doTestPrepareAndClean( $path, $isOK );
1531 $this->tearDownFiles();
1534 public static function provider_testPrepareAndClean() {
1535 $base = self
::baseStorePath();
1538 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1539 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1540 # Specific to FS backend with no basePath field set
1541 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1545 private function doTestPrepareAndClean( $path, $isOK ) {
1546 $backendName = $this->backendClass();
1548 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1550 $this->assertGoodStatus( $status,
1551 "Preparing dir $path succeeded without warnings ($backendName)." );
1552 $this->assertEquals( true, $status->isOK(),
1553 "Preparing dir $path succeeded ($backendName)." );
1555 $this->assertEquals( false, $status->isOK(),
1556 "Preparing dir $path failed ($backendName)." );
1559 $status = $this->backend
->secure( array( 'dir' => dirname( $path ) ) );
1561 $this->assertGoodStatus( $status,
1562 "Securing dir $path succeeded without warnings ($backendName)." );
1563 $this->assertEquals( true, $status->isOK(),
1564 "Securing dir $path succeeded ($backendName)." );
1566 $this->assertEquals( false, $status->isOK(),
1567 "Securing dir $path failed ($backendName)." );
1570 $status = $this->backend
->publish( array( 'dir' => dirname( $path ) ) );
1572 $this->assertGoodStatus( $status,
1573 "Publishing dir $path succeeded without warnings ($backendName)." );
1574 $this->assertEquals( true, $status->isOK(),
1575 "Publishing dir $path succeeded ($backendName)." );
1577 $this->assertEquals( false, $status->isOK(),
1578 "Publishing dir $path failed ($backendName)." );
1581 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1583 $this->assertGoodStatus( $status,
1584 "Cleaning dir $path succeeded without warnings ($backendName)." );
1585 $this->assertEquals( true, $status->isOK(),
1586 "Cleaning dir $path succeeded ($backendName)." );
1588 $this->assertEquals( false, $status->isOK(),
1589 "Cleaning dir $path failed ($backendName)." );
1593 public function testRecursiveClean() {
1594 $this->backend
= $this->singleBackend
;
1595 $this->doTestRecursiveClean();
1596 $this->tearDownFiles();
1598 $this->backend
= $this->multiBackend
;
1599 $this->doTestRecursiveClean();
1600 $this->tearDownFiles();
1604 * @covers FileBackend::clean
1606 private function doTestRecursiveClean() {
1607 $backendName = $this->backendClass();
1609 $base = self
::baseStorePath();
1611 "$base/unittest-cont1",
1612 "$base/unittest-cont1/e",
1613 "$base/unittest-cont1/e/a",
1614 "$base/unittest-cont1/e/a/b",
1615 "$base/unittest-cont1/e/a/b/c",
1616 "$base/unittest-cont1/e/a/b/c/d0",
1617 "$base/unittest-cont1/e/a/b/c/d1",
1618 "$base/unittest-cont1/e/a/b/c/d2",
1619 "$base/unittest-cont1/e/a/b/c/d0/1",
1620 "$base/unittest-cont1/e/a/b/c/d0/2",
1621 "$base/unittest-cont1/e/a/b/c/d1/3",
1622 "$base/unittest-cont1/e/a/b/c/d1/4",
1623 "$base/unittest-cont1/e/a/b/c/d2/5",
1624 "$base/unittest-cont1/e/a/b/c/d2/6"
1626 foreach ( $dirs as $dir ) {
1627 $status = $this->prepare( array( 'dir' => $dir ) );
1628 $this->assertGoodStatus( $status,
1629 "Preparing dir $dir succeeded without warnings ($backendName)." );
1632 if ( $this->backend
instanceof FSFileBackend
) {
1633 foreach ( $dirs as $dir ) {
1634 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1635 "Dir $dir exists ($backendName)." );
1639 $status = $this->backend
->clean(
1640 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1641 $this->assertGoodStatus( $status,
1642 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1644 foreach ( $dirs as $dir ) {
1645 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1646 "Dir $dir no longer exists ($backendName)." );
1651 * @covers FileBackend::doOperations
1653 public function testDoOperations() {
1654 $this->backend
= $this->singleBackend
;
1655 $this->tearDownFiles();
1656 $this->doTestDoOperations();
1657 $this->tearDownFiles();
1659 $this->backend
= $this->multiBackend
;
1660 $this->tearDownFiles();
1661 $this->doTestDoOperations();
1662 $this->tearDownFiles();
1665 private function doTestDoOperations() {
1666 $base = self
::baseStorePath();
1668 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1669 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1670 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1671 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1672 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1673 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1674 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1676 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1677 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1678 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1679 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1680 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1681 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1682 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1684 $status = $this->backend
->doOperations( array(
1685 array( 'op' => 'describe', 'src' => $fileA,
1686 'headers' => array( 'X-Content-Length' => '91.3' ), 'disposition' => 'inline' ),
1687 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1688 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1689 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1690 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1691 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1692 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1693 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1694 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1695 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1696 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1697 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1698 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1699 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1700 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1701 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1702 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1703 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1705 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1707 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1709 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1711 array( 'op' => 'null' ),
1715 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1716 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1717 $this->assertEquals( 14, count( $status->success
),
1718 "Operation batch has correct success array" );
1720 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1721 "File does not exist at $fileA" );
1722 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1723 "File does not exist at $fileB" );
1724 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1725 "File does not exist at $fileD" );
1727 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1728 "File exists at $fileC" );
1729 $this->assertEquals( $fileBContents,
1730 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1731 "Correct file contents of $fileC" );
1732 $this->assertEquals( strlen( $fileBContents ),
1733 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1734 "Correct file size of $fileC" );
1735 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1736 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1737 "Correct file SHA-1 of $fileC" );
1741 * @covers FileBackend::doOperations
1743 public function testDoOperationsPipeline() {
1744 $this->backend
= $this->singleBackend
;
1745 $this->tearDownFiles();
1746 $this->doTestDoOperationsPipeline();
1747 $this->tearDownFiles();
1749 $this->backend
= $this->multiBackend
;
1750 $this->tearDownFiles();
1751 $this->doTestDoOperationsPipeline();
1752 $this->tearDownFiles();
1755 // concurrency orientated
1756 private function doTestDoOperationsPipeline() {
1757 $base = self
::baseStorePath();
1759 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1760 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1761 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1763 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1764 file_put_contents( $tmpNameA, $fileAContents );
1765 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1766 file_put_contents( $tmpNameB, $fileBContents );
1767 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1768 file_put_contents( $tmpNameC, $fileCContents );
1770 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1771 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1772 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1774 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1775 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1776 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1777 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1779 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1780 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1781 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1782 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1783 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1785 $status = $this->backend
->doOperations( array(
1786 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1787 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1788 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1789 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1790 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1791 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1792 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1793 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1794 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1795 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1796 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1797 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1798 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1799 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1800 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1801 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1802 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1803 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1804 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1805 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1807 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1809 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1811 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1813 array( 'op' => 'null' ),
1817 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1818 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1819 $this->assertEquals( 16, count( $status->success
),
1820 "Operation batch has correct success array" );
1822 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1823 "File does not exist at $fileA" );
1824 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1825 "File does not exist at $fileB" );
1826 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1827 "File does not exist at $fileD" );
1829 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1830 "File exists at $fileC" );
1831 $this->assertEquals( $fileBContents,
1832 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1833 "Correct file contents of $fileC" );
1834 $this->assertEquals( strlen( $fileBContents ),
1835 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1836 "Correct file size of $fileC" );
1837 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1838 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1839 "Correct file SHA-1 of $fileC" );
1843 * @covers FileBackend::doOperations
1845 public function testDoOperationsFailing() {
1846 $this->backend
= $this->singleBackend
;
1847 $this->tearDownFiles();
1848 $this->doTestDoOperationsFailing();
1849 $this->tearDownFiles();
1851 $this->backend
= $this->multiBackend
;
1852 $this->tearDownFiles();
1853 $this->doTestDoOperationsFailing();
1854 $this->tearDownFiles();
1857 private function doTestDoOperationsFailing() {
1858 $base = self
::baseStorePath();
1860 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1861 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1862 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1863 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1864 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1865 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1866 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1868 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1869 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1870 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1871 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1872 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1873 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1875 $status = $this->backend
->doOperations( array(
1876 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1877 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1878 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1879 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1880 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1881 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1882 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1883 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1884 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1885 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1886 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1887 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1888 array( 'op' => 'delete', 'src' => $fileD ),
1889 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1890 array( 'op' => 'null' ),
1892 ), array( 'force' => 1 ) );
1894 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1895 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1896 $this->assertEquals( 8, count( $status->success
),
1897 "Operation batch has correct success array" );
1899 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1900 "File does not exist at $fileB" );
1901 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1902 "File does not exist at $fileD" );
1904 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1905 "File does not exist at $fileA" );
1906 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1907 "File exists at $fileC" );
1908 $this->assertEquals( $fileBContents,
1909 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1910 "Correct file contents of $fileA" );
1911 $this->assertEquals( strlen( $fileBContents ),
1912 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1913 "Correct file size of $fileA" );
1914 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1915 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1916 "Correct file SHA-1 of $fileA" );
1920 * @covers FileBackend::getFileList
1922 public function testGetFileList() {
1923 $this->backend
= $this->singleBackend
;
1924 $this->tearDownFiles();
1925 $this->doTestGetFileList();
1926 $this->tearDownFiles();
1928 $this->backend
= $this->multiBackend
;
1929 $this->tearDownFiles();
1930 $this->doTestGetFileList();
1931 $this->tearDownFiles();
1934 private function doTestGetFileList() {
1935 $backendName = $this->backendClass();
1936 $base = self
::baseStorePath();
1938 // Should have no errors
1939 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1942 "$base/unittest-cont1/e/test1.txt",
1943 "$base/unittest-cont1/e/test2.txt",
1944 "$base/unittest-cont1/e/test3.txt",
1945 "$base/unittest-cont1/e/subdir1/test1.txt",
1946 "$base/unittest-cont1/e/subdir1/test2.txt",
1947 "$base/unittest-cont1/e/subdir2/test3.txt",
1948 "$base/unittest-cont1/e/subdir2/test4.txt",
1949 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1950 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1951 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1952 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1953 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1954 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1955 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1960 foreach ( $files as $file ) {
1961 $this->prepare( array( 'dir' => dirname( $file ) ) );
1962 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1964 $status = $this->backend
->doQuickOperations( $ops );
1965 $this->assertGoodStatus( $status,
1966 "Creation of files succeeded ($backendName)." );
1967 $this->assertEquals( true, $status->isOK(),
1968 "Creation of files succeeded with OK status ($backendName)." );
1970 // Expected listing at root
1975 "e/subdir1/test1.txt",
1976 "e/subdir1/test2.txt",
1977 "e/subdir2/test3.txt",
1978 "e/subdir2/test4.txt",
1979 "e/subdir2/subdir/test1.txt",
1980 "e/subdir2/subdir/test2.txt",
1981 "e/subdir2/subdir/test3.txt",
1982 "e/subdir2/subdir/test4.txt",
1983 "e/subdir2/subdir/test5.txt",
1984 "e/subdir2/subdir/sub/test0.txt",
1985 "e/subdir2/subdir/sub/120-px-file.txt",
1989 // Actual listing (no trailing slash) at root
1990 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1991 $list = $this->listToArray( $iter );
1993 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1995 // Actual listing (no trailing slash) at root with advise
1996 $iter = $this->backend
->getFileList( array(
1997 'dir' => "$base/unittest-cont1",
2000 $list = $this->listToArray( $iter );
2002 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2004 // Actual listing (with trailing slash) at root
2006 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
2007 foreach ( $iter as $file ) {
2011 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2013 // Expected listing at subdir
2021 "sub/120-px-file.txt",
2025 // Actual listing (no trailing slash) at subdir
2026 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
2027 $list = $this->listToArray( $iter );
2029 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2031 // Actual listing (no trailing slash) at subdir with advise
2032 $iter = $this->backend
->getFileList( array(
2033 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2036 $list = $this->listToArray( $iter );
2038 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2040 // Actual listing (with trailing slash) at subdir
2042 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
2043 foreach ( $iter as $file ) {
2047 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2049 // Actual listing (using iterator second time)
2050 $list = $this->listToArray( $iter );
2052 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
2054 // Actual listing (top files only) at root
2055 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1" ) );
2056 $list = $this->listToArray( $iter );
2058 $this->assertEquals( array(), $list, "Correct top file listing ($backendName)." );
2060 // Expected listing (top files only) at subdir
2070 // Actual listing (top files only) at subdir
2071 $iter = $this->backend
->getTopFileList(
2072 array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" )
2074 $list = $this->listToArray( $iter );
2076 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2078 // Actual listing (top files only) at subdir with advise
2079 $iter = $this->backend
->getTopFileList( array(
2080 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2083 $list = $this->listToArray( $iter );
2085 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2087 foreach ( $files as $file ) { // clean up
2088 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
2091 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
2092 foreach ( $iter as $iter ) {
2098 * @covers FileBackend::getTopDirectoryList
2099 * @covers FileBackend::getDirectoryList
2101 public function testGetDirectoryList() {
2102 $this->backend
= $this->singleBackend
;
2103 $this->tearDownFiles();
2104 $this->doTestGetDirectoryList();
2105 $this->tearDownFiles();
2107 $this->backend
= $this->multiBackend
;
2108 $this->tearDownFiles();
2109 $this->doTestGetDirectoryList();
2110 $this->tearDownFiles();
2113 private function doTestGetDirectoryList() {
2114 $backendName = $this->backendClass();
2116 $base = self
::baseStorePath();
2118 "$base/unittest-cont1/e/test1.txt",
2119 "$base/unittest-cont1/e/test2.txt",
2120 "$base/unittest-cont1/e/test3.txt",
2121 "$base/unittest-cont1/e/subdir1/test1.txt",
2122 "$base/unittest-cont1/e/subdir1/test2.txt",
2123 "$base/unittest-cont1/e/subdir2/test3.txt",
2124 "$base/unittest-cont1/e/subdir2/test4.txt",
2125 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
2126 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
2127 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
2128 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
2129 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
2130 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
2131 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
2136 foreach ( $files as $file ) {
2137 $this->prepare( array( 'dir' => dirname( $file ) ) );
2138 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
2140 $status = $this->backend
->doQuickOperations( $ops );
2141 $this->assertGoodStatus( $status,
2142 "Creation of files succeeded ($backendName)." );
2143 $this->assertEquals( true, $status->isOK(),
2144 "Creation of files succeeded with OK status ($backendName)." );
2146 $this->assertEquals( true,
2147 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
2148 "Directory exists in ($backendName)." );
2149 $this->assertEquals( true,
2150 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
2151 "Directory exists in ($backendName)." );
2152 $this->assertEquals( false,
2153 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
2154 "Directory does not exists in ($backendName)." );
2162 // Actual listing (no trailing slash)
2164 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
2165 foreach ( $iter as $file ) {
2170 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2181 // Actual listing (no trailing slash)
2183 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
2184 foreach ( $iter as $file ) {
2189 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2191 // Actual listing (with trailing slash)
2193 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
2194 foreach ( $iter as $file ) {
2199 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2207 // Actual listing (no trailing slash)
2209 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
2210 foreach ( $iter as $file ) {
2215 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2217 // Actual listing (with trailing slash)
2219 $iter = $this->backend
->getTopDirectoryList(
2220 array( 'dir' => "$base/unittest-cont1/e/subdir2/" )
2223 foreach ( $iter as $file ) {
2228 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2230 // Actual listing (using iterator second time)
2232 foreach ( $iter as $file ) {
2237 $this->assertEquals(
2240 "Correct top dir listing ($backendName), second iteration."
2243 // Expected listing (recursive)
2253 "e/subdir4/subdir/sub",
2257 // Actual listing (recursive)
2259 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
2260 foreach ( $iter as $file ) {
2265 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2267 // Expected listing (recursive)
2274 // Actual listing (recursive)
2276 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
2277 foreach ( $iter as $file ) {
2282 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2284 // Actual listing (recursive, second time)
2286 foreach ( $iter as $file ) {
2291 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2293 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) );
2294 $items = $this->listToArray( $iter );
2295 $this->assertEquals( array(), $items, "Directory listing is empty." );
2297 foreach ( $files as $file ) { // clean up
2298 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
2301 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
2302 foreach ( $iter as $file ) {
2306 $items = $this->listToArray( $iter );
2307 $this->assertEquals( array(), $items, "Directory listing is empty." );
2309 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/not/exists" ) );
2310 $items = $this->listToArray( $iter );
2311 $this->assertEquals( array(), $items, "Directory listing is empty." );
2315 * @covers FileBackend::lockFiles
2316 * @covers FileBackend::unlockFiles
2318 public function testLockCalls() {
2319 $this->backend
= $this->singleBackend
;
2320 $this->doTestLockCalls();
2323 private function doTestLockCalls() {
2324 $backendName = $this->backendClass();
2331 "subdir1", // duplicate
2332 "subdir1/test1.txt",
2333 "subdir1/test2.txt",
2335 "subdir2", // duplicate
2336 "subdir2/test3.txt",
2337 "subdir2/test4.txt",
2339 "subdir2/subdir/test1.txt",
2340 "subdir2/subdir/test2.txt",
2341 "subdir2/subdir/test3.txt",
2342 "subdir2/subdir/test4.txt",
2343 "subdir2/subdir/test5.txt",
2344 "subdir2/subdir/sub",
2345 "subdir2/subdir/sub/test0.txt",
2346 "subdir2/subdir/sub/120-px-file.txt",
2349 for ( $i = 0; $i < 25; $i++
) {
2350 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2351 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2352 "Locking of files succeeded ($backendName) ($i)." );
2353 $this->assertEquals( true, $status->isOK(),
2354 "Locking of files succeeded with OK status ($backendName) ($i)." );
2356 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2357 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2358 "Locking of files succeeded ($backendName) ($i)." );
2359 $this->assertEquals( true, $status->isOK(),
2360 "Locking of files succeeded with OK status ($backendName) ($i)." );
2362 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2363 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2364 "Locking of files succeeded ($backendName) ($i)." );
2365 $this->assertEquals( true, $status->isOK(),
2366 "Locking of files succeeded with OK status ($backendName) ($i)." );
2368 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2369 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2370 "Locking of files succeeded ($backendName). ($i)" );
2371 $this->assertEquals( true, $status->isOK(),
2372 "Locking of files succeeded with OK status ($backendName) ($i)." );
2374 ## Flip the acquire/release ordering around ##
2376 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2377 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2378 "Locking of files succeeded ($backendName) ($i)." );
2379 $this->assertEquals( true, $status->isOK(),
2380 "Locking of files succeeded with OK status ($backendName) ($i)." );
2382 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2383 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2384 "Locking of files succeeded ($backendName) ($i)." );
2385 $this->assertEquals( true, $status->isOK(),
2386 "Locking of files succeeded with OK status ($backendName) ($i)." );
2388 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2389 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2390 "Locking of files succeeded ($backendName). ($i)" );
2391 $this->assertEquals( true, $status->isOK(),
2392 "Locking of files succeeded with OK status ($backendName) ($i)." );
2394 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2395 $this->assertEquals( print_r( array(), true ), print_r( $status->errors
, true ),
2396 "Locking of files succeeded ($backendName) ($i)." );
2397 $this->assertEquals( true, $status->isOK(),
2398 "Locking of files succeeded with OK status ($backendName) ($i)." );
2401 $status = Status
::newGood();
2402 $sl = $this->backend
->getScopedFileLocks( $paths, LockManager
::LOCK_EX
, $status );
2403 $this->assertType( 'ScopedLock', $sl,
2404 "Scoped locking of files succeeded ($backendName)." );
2405 $this->assertEquals( array(), $status->errors
,
2406 "Scoped locking of files succeeded ($backendName)." );
2407 $this->assertEquals( true, $status->isOK(),
2408 "Scoped locking of files succeeded with OK status ($backendName)." );
2410 ScopedLock
::release( $sl );
2411 $this->assertEquals( null, $sl,
2412 "Scoped unlocking of files succeeded ($backendName)." );
2413 $this->assertEquals( array(), $status->errors
,
2414 "Scoped unlocking of files succeeded ($backendName)." );
2415 $this->assertEquals( true, $status->isOK(),
2416 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2420 private function listToArray( $iter ) {
2421 return is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2424 // test helper wrapper for backend prepare() function
2425 private function prepare( array $params ) {
2426 return $this->backend
->prepare( $params );
2429 // test helper wrapper for backend prepare() function
2430 private function create( array $params ) {
2431 $params['op'] = 'create';
2433 return $this->backend
->doQuickOperations( array( $params ) );
2436 function tearDownFiles() {
2437 foreach ( $this->filesToPrune
as $file ) {
2438 if ( is_file( $file ) ) {
2442 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont-bad' );
2443 foreach ( $containers as $container ) {
2444 $this->deleteFiles( $container );
2446 $this->filesToPrune
= array();
2449 private function deleteFiles( $container ) {
2450 $base = self
::baseStorePath();
2451 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
2453 foreach ( $iter as $file ) {
2454 $this->backend
->quickDelete( array( 'src' => "$base/$container/$file" ) );
2456 // free the directory, to avoid Permission denied under windows on rmdir
2459 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
2462 function assertBackendPathsConsistent( array $paths ) {
2463 if ( $this->backend
instanceof FileBackendMultiWrite
) {
2464 $status = $this->backend
->consistencyCheck( $paths );
2465 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2469 function assertGoodStatus( $status, $msg ) {
2470 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );