3 use MediaWiki\MediaWikiServices
;
4 use Wikimedia\TestingAccessWrapper
;
14 * @covers CreateFileOp
15 * @covers DeleteFileOp
16 * @covers DescribeFileOp
18 * @covers FSFileBackend
19 * @covers FSFileBackendDirList
20 * @covers FSFileBackendFileList
21 * @covers FSFileBackendList
22 * @covers FSFileOpHandle
23 * @covers FileBackendDBRepoWrapper
24 * @covers FileBackendError
25 * @covers FileBackendGroup
26 * @covers FileBackendMultiWrite
27 * @covers FileBackendStore
28 * @covers FileBackendStoreOpHandle
29 * @covers FileBackendStoreShardDirIterator
30 * @covers FileBackendStoreShardFileIterator
31 * @covers FileBackendStoreShardListIterator
34 * @covers HTTPFileStreamer
35 * @covers LockManagerGroup
36 * @covers MemoryFileBackend
38 * @covers MySqlLockManager
43 * @covers FSLockManager
45 * @covers NullLockManager
47 class FileBackendTest
extends MediaWikiTestCase
{
49 /** @var FileBackend */
51 /** @var FileBackendMultiWrite */
52 private $multiBackend;
53 /** @var FSFileBackend */
54 public $singleBackend;
55 private static $backendToUse;
57 protected function setUp() {
58 global $wgFileBackends;
60 $tmpDir = $this->getNewTempDirectory();
61 if ( $this->getCliArg( 'use-filebackend' ) ) {
62 if ( self
::$backendToUse ) {
63 $this->singleBackend
= self
::$backendToUse;
65 $name = $this->getCliArg( 'use-filebackend' );
67 foreach ( $wgFileBackends as $conf ) {
68 if ( $conf['name'] == $name ) {
73 $useConfig['name'] = 'localtesting'; // swap name
74 $useConfig['shardViaHashLevels'] = [ // test sharding
75 'unittest-cont1' => [ 'levels' => 1, 'base' => 16, 'repeat' => 1 ]
77 if ( isset( $useConfig['fileJournal'] ) ) {
78 $useConfig['fileJournal'] = FileJournal
::factory( $useConfig['fileJournal'], $name );
80 $useConfig['lockManager'] = LockManagerGroup
::singleton()->get( $useConfig['lockManager'] );
81 $class = $useConfig['class'];
82 self
::$backendToUse = new $class( $useConfig );
83 $this->singleBackend
= self
::$backendToUse;
86 $this->singleBackend
= new FSFileBackend( [
87 'name' => 'localtesting',
88 'lockManager' => LockManagerGroup
::singleton()->get( 'fsLockManager' ),
89 'wikiId' => wfWikiID(),
91 'unittest-cont1' => "{$tmpDir}/localtesting-cont1",
92 'unittest-cont2' => "{$tmpDir}/localtesting-cont2" ]
95 $this->multiBackend
= new FileBackendMultiWrite( [
96 'name' => 'localtesting',
97 'lockManager' => LockManagerGroup
::singleton()->get( 'fsLockManager' ),
98 'parallelize' => 'implicit',
102 'name' => 'localmultitesting1',
103 'class' => FSFileBackend
::class,
104 'containerPaths' => [
105 'unittest-cont1' => "{$tmpDir}/localtestingmulti1-cont1",
106 'unittest-cont2' => "{$tmpDir}/localtestingmulti1-cont2" ],
107 'isMultiMaster' => false
110 'name' => 'localmultitesting2',
111 'class' => FSFileBackend
::class,
112 'containerPaths' => [
113 'unittest-cont1' => "{$tmpDir}/localtestingmulti2-cont1",
114 'unittest-cont2' => "{$tmpDir}/localtestingmulti2-cont2" ],
115 'isMultiMaster' => true
121 private static function baseStorePath() {
122 return 'mwstore://localtesting';
125 private function backendClass() {
126 return get_class( $this->backend
);
130 * @dataProvider provider_testIsStoragePath
132 public function testIsStoragePath( $path, $isStorePath ) {
133 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
134 "FileBackend::isStoragePath on path '$path'" );
137 public static function provider_testIsStoragePath() {
139 [ 'mwstore://', true ],
140 [ 'mwstore://backend', true ],
141 [ 'mwstore://backend/container', true ],
142 [ 'mwstore://backend/container/', true ],
143 [ 'mwstore://backend/container/path', true ],
144 [ 'mwstore://backend//container/', true ],
145 [ 'mwstore://backend//container//', true ],
146 [ 'mwstore://backend//container//path', true ],
147 [ 'mwstore:///', true ],
148 [ 'mwstore:/', false ],
149 [ 'mwstore:', false ],
154 * @dataProvider provider_testSplitStoragePath
156 public function testSplitStoragePath( $path, $res ) {
157 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
158 "FileBackend::splitStoragePath on path '$path'" );
161 public static function provider_testSplitStoragePath() {
163 [ 'mwstore://backend/container', [ 'backend', 'container', '' ] ],
164 [ 'mwstore://backend/container/', [ 'backend', 'container', '' ] ],
165 [ 'mwstore://backend/container/path', [ 'backend', 'container', 'path' ] ],
166 [ 'mwstore://backend/container//path', [ 'backend', 'container', '/path' ] ],
167 [ 'mwstore://backend//container/path', [ null, null, null ] ],
168 [ 'mwstore://backend//container//path', [ null, null, null ] ],
169 [ 'mwstore://', [ null, null, null ] ],
170 [ 'mwstore://backend', [ null, null, null ] ],
171 [ 'mwstore:///', [ null, null, null ] ],
172 [ 'mwstore:/', [ null, null, null ] ],
173 [ 'mwstore:', [ null, null, null ] ]
178 * @dataProvider provider_normalizeStoragePath
180 public function testNormalizeStoragePath( $path, $res ) {
181 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
182 "FileBackend::normalizeStoragePath on path '$path'" );
185 public static function provider_normalizeStoragePath() {
187 [ 'mwstore://backend/container', 'mwstore://backend/container' ],
188 [ 'mwstore://backend/container/', 'mwstore://backend/container' ],
189 [ 'mwstore://backend/container/path', 'mwstore://backend/container/path' ],
190 [ 'mwstore://backend/container//path', 'mwstore://backend/container/path' ],
191 [ 'mwstore://backend/container///path', 'mwstore://backend/container/path' ],
193 'mwstore://backend/container///path//to///obj',
194 'mwstore://backend/container/path/to/obj'
196 [ 'mwstore://', null ],
197 [ 'mwstore://backend', null ],
198 [ 'mwstore://backend//container/path', null ],
199 [ 'mwstore://backend//container//path', null ],
200 [ 'mwstore:///', null ],
201 [ 'mwstore:/', null ],
202 [ 'mwstore:', null ],
207 * @dataProvider provider_testParentStoragePath
209 public function testParentStoragePath( $path, $res ) {
210 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
211 "FileBackend::parentStoragePath on path '$path'" );
214 public static function provider_testParentStoragePath() {
216 [ 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ],
217 [ 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ],
218 [ 'mwstore://backend/container/path', 'mwstore://backend/container' ],
219 [ 'mwstore://backend/container', null ],
220 [ 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ],
221 [ 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ],
222 [ 'mwstore://backend/container/path/', 'mwstore://backend/container' ],
223 [ 'mwstore://backend/container/', null ],
228 * @dataProvider provider_testExtensionFromPath
230 public function testExtensionFromPath( $path, $res ) {
231 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
232 "FileBackend::extensionFromPath on path '$path'" );
235 public static function provider_testExtensionFromPath() {
237 [ 'mwstore://backend/container/path.txt', 'txt' ],
238 [ 'mwstore://backend/container/path.svg.png', 'png' ],
239 [ 'mwstore://backend/container/path', '' ],
240 [ 'mwstore://backend/container/path.', '' ],
245 * @dataProvider provider_testStore
247 public function testStore( $op ) {
248 $this->addTmpFiles( $op['src'] );
250 $this->backend
= $this->singleBackend
;
251 $this->tearDownFiles();
252 $this->doTestStore( $op );
253 $this->tearDownFiles();
255 $this->backend
= $this->multiBackend
;
256 $this->tearDownFiles();
257 $this->doTestStore( $op );
258 $this->tearDownFiles();
261 private function doTestStore( $op ) {
262 $backendName = $this->backendClass();
264 $source = $op['src'];
266 $this->prepare( [ 'dir' => dirname( $dest ) ] );
268 file_put_contents( $source, "Unit test file" );
270 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
271 $this->backend
->store( $op );
274 $status = $this->backend
->doOperation( $op );
276 $this->assertGoodStatus( $status,
277 "Store from $source to $dest succeeded without warnings ($backendName)." );
278 $this->assertEquals( true, $status->isOK(),
279 "Store from $source to $dest succeeded ($backendName)." );
280 $this->assertEquals( [ 0 => true ], $status->success
,
281 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
282 $this->assertEquals( true, file_exists( $source ),
283 "Source file $source still exists ($backendName)." );
284 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $dest ] ),
285 "Destination file $dest exists ($backendName)." );
287 $this->assertEquals( filesize( $source ),
288 $this->backend
->getFileSize( [ 'src' => $dest ] ),
289 "Destination file $dest has correct size ($backendName)." );
291 $props1 = FSFile
::getPropsFromPath( $source );
292 $props2 = $this->backend
->getFileProps( [ 'src' => $dest ] );
293 $this->assertEquals( $props1, $props2,
294 "Source and destination have the same props ($backendName)." );
296 $this->assertBackendPathsConsistent( [ $dest ], true );
299 public static function provider_testStore() {
302 $tmpName = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
303 $toPath = self
::baseStorePath() . '/unittest-cont1/e/fun/obj1.txt';
304 $op = [ 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath ];
308 $op2['overwrite'] = true;
312 $op3['overwriteSame'] = true;
319 * @dataProvider provider_testCopy
321 public function testCopy( $op, $srcContent, $dstContent, $okStatus, $okSyncStatus ) {
322 $this->backend
= $this->singleBackend
;
323 $this->tearDownFiles();
324 $this->doTestCopy( $op, $srcContent, $dstContent, $okStatus, $okSyncStatus );
325 $this->tearDownFiles();
327 $this->backend
= $this->multiBackend
;
328 $this->tearDownFiles();
329 $this->doTestCopy( $op, $srcContent, $dstContent, $okStatus, $okSyncStatus );
330 $this->tearDownFiles();
333 private function doTestCopy( $op, $srcContent, $dstContent, $okStatus, $okSyncStatus ) {
334 $backendName = $this->backendClass();
336 $source = $op['src'];
338 $this->prepare( [ 'dir' => dirname( $source ) ] );
339 $this->prepare( [ 'dir' => dirname( $dest ) ] );
341 if ( is_string( $srcContent ) ) {
342 $status = $this->backend
->create( [ 'content' => $srcContent, 'dst' => $source ] );
343 $this->assertGoodStatus( $status, "Creation of $source succeeded ($backendName)." );
345 if ( is_string( $dstContent ) ) {
346 $status = $this->backend
->create( [ 'content' => $dstContent, 'dst' => $dest ] );
347 $this->assertGoodStatus( $status, "Creation of $dest succeeded ($backendName)." );
350 $status = $this->backend
->doOperation( $op );
353 $this->assertGoodStatus(
355 "Copy from $source to $dest succeeded without warnings ($backendName)." );
356 $this->assertEquals( true, $status->isOK(),
357 "Copy from $source to $dest succeeded ($backendName)." );
358 $this->assertEquals( [ 0 => true ], $status->success
,
359 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
360 if ( !is_string( $srcContent ) ) {
362 is_string( $dstContent ),
363 $this->backend
->fileExists( [ 'src' => $dest ] ),
364 "Destination file $dest unchanged after no-op copy ($backendName)." );
367 $this->backend
->getFileContents( [ 'src' => $dest ] ),
368 "Destination file $dest unchanged after no-op copy ($backendName)." );
371 $this->backend
->getFileSize( [ 'src' => $source ] ),
372 $this->backend
->getFileSize( [ 'src' => $dest ] ),
373 "Destination file $dest has correct size ($backendName)." );
374 $props1 = $this->backend
->getFileProps( [ 'src' => $source ] );
375 $props2 = $this->backend
->getFileProps( [ 'src' => $dest ] );
379 "Source and destination have the same props ($backendName)." );
382 $this->assertBadStatus(
384 "Copy from $source to $dest fails ($backendName)." );
386 is_string( $dstContent ),
387 (bool)$this->backend
->fileExists( [ 'src' => $dest ] ),
388 "Destination file $dest unchanged after failed copy ($backendName)." );
391 $this->backend
->getFileContents( [ 'src' => $dest ] ),
392 "Destination file $dest unchanged after failed copy ($backendName)." );
396 is_string( $srcContent ),
397 (bool)$this->backend
->fileExists( [ 'src' => $source ] ),
398 "Source file $source unchanged after copy ($backendName)."
402 $this->backend
->getFileContents( [ 'src' => $source ] ),
403 "Source file $source unchanged after copy ($backendName)."
405 if ( is_string( $dstContent ) ) {
407 (bool)$this->backend
->fileExists( [ 'src' => $dest ] ),
408 "Destination file $dest exists after copy ($backendName)." );
411 $this->assertBackendPathsConsistent( [ $source, $dest ], $okSyncStatus );
415 * @return array (op, source exists, dest exists, op succeeds, sync check succeeds)
417 public static function provider_testCopy() {
420 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
421 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileCopied.txt';
422 $opBase = [ 'op' => 'copy', 'src' => $source, 'dst' => $dest ];
425 $cases[] = [ $op, 'yyy', false, true, true ];
428 $op['overwrite'] = true;
429 $cases[] = [ $op, 'yyy', false, true, true ];
432 $op['overwrite'] = true;
433 $cases[] = [ $op, 'yyy', 'xxx', true, true ];
436 $op['overwriteSame'] = true;
437 $cases[] = [ $op, 'yyy', false, true, true ];
440 $op['overwriteSame'] = true;
441 $cases[] = [ $op, 'yyy', 'yyy', true, true ];
444 $op['overwriteSame'] = true;
445 $cases[] = [ $op, 'yyy', 'zzz', false, true ];
448 $op['ignoreMissingSource'] = true;
449 $cases[] = [ $op, 'xxx', false, true, true ];
452 $op['ignoreMissingSource'] = true;
453 $cases[] = [ $op, false, false, true, true ];
456 $op['ignoreMissingSource'] = true;
457 $cases[] = [ $op, false, 'xxx', true, true ];
460 $op['src'] = 'mwstore://wrongbackend/unittest-cont1/e/file.txt';
461 $op['ignoreMissingSource'] = true;
462 $cases[] = [ $op, false, false, false, false ];
468 * @dataProvider provider_testMove
470 public function testMove( $op, $srcContent, $dstContent, $okStatus, $okSyncStatus ) {
471 $this->backend
= $this->singleBackend
;
472 $this->tearDownFiles();
473 $this->doTestMove( $op, $srcContent, $dstContent, $okStatus, $okSyncStatus );
474 $this->tearDownFiles();
476 $this->backend
= $this->multiBackend
;
477 $this->tearDownFiles();
478 $this->doTestMove( $op, $srcContent, $dstContent, $okStatus, $okSyncStatus );
479 $this->tearDownFiles();
482 private function doTestMove( $op, $srcContent, $dstContent, $okStatus, $okSyncStatus ) {
483 $backendName = $this->backendClass();
485 $source = $op['src'];
487 $this->prepare( [ 'dir' => dirname( $source ) ] );
488 $this->prepare( [ 'dir' => dirname( $dest ) ] );
490 if ( is_string( $srcContent ) ) {
491 $status = $this->backend
->create( [ 'content' => $srcContent, 'dst' => $source ] );
492 $this->assertGoodStatus( $status, "Creation of $source succeeded ($backendName)." );
494 if ( is_string( $dstContent ) ) {
495 $status = $this->backend
->create( [ 'content' => $dstContent, 'dst' => $dest ] );
496 $this->assertGoodStatus( $status, "Creation of $dest succeeded ($backendName)." );
499 $oldSrcProps = $this->backend
->getFileProps( [ 'src' => $source ] );
501 $status = $this->backend
->doOperation( $op );
504 $this->assertGoodStatus(
506 "Move from $source to $dest succeeded without warnings ($backendName)." );
507 $this->assertEquals( true, $status->isOK(),
508 "Move from $source to $dest succeeded ($backendName)." );
509 $this->assertEquals( [ 0 => true ], $status->success
,
510 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
511 if ( !is_string( $srcContent ) ) {
513 is_string( $dstContent ),
514 $this->backend
->fileExists( [ 'src' => $dest ] ),
515 "Destination file $dest unchanged after no-op move ($backendName)." );
518 $this->backend
->getFileContents( [ 'src' => $dest ] ),
519 "Destination file $dest unchanged after no-op move ($backendName)." );
522 $this->backend
->getFileSize( [ 'src' => $dest ] ),
523 strlen( $srcContent ),
524 "Destination file $dest has correct size ($backendName)." );
527 $this->backend
->getFileProps( [ 'src' => $dest ] ),
528 "Source and destination have the same props ($backendName)." );
531 $this->assertBadStatus(
533 "Move from $source to $dest fails ($backendName)." );
535 is_string( $dstContent ),
536 (bool)$this->backend
->fileExists( [ 'src' => $dest ] ),
537 "Destination file $dest unchanged after failed move ($backendName)." );
540 $this->backend
->getFileContents( [ 'src' => $dest ] ),
541 "Destination file $dest unchanged after failed move ($backendName)." );
543 is_string( $srcContent ),
544 (bool)$this->backend
->fileExists( [ 'src' => $source ] ),
545 "Source file $source unchanged after failed move ($backendName)."
549 $this->backend
->getFileContents( [ 'src' => $source ] ),
550 "Source file $source unchanged after failed move ($backendName)."
554 if ( is_string( $dstContent ) ) {
556 (bool)$this->backend
->fileExists( [ 'src' => $dest ] ),
557 "Destination file $dest exists after move ($backendName)." );
560 $this->assertBackendPathsConsistent( [ $source, $dest ], $okSyncStatus );
564 * @return array (op, source exists, dest exists, op succeeds, sync check succeeds)
566 public static function provider_testMove() {
569 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
570 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
571 $opBase = [ 'op' => 'move', 'src' => $source, 'dst' => $dest ];
574 $cases[] = [ $op, 'yyy', false, true, true ];
577 $op['overwrite'] = true;
578 $cases[] = [ $op, 'yyy', false, true, true ];
581 $op['overwrite'] = true;
582 $cases[] = [ $op, 'yyy', 'xxx', true, true ];
585 $op['overwriteSame'] = true;
586 $cases[] = [ $op, 'yyy', false, true, true ];
589 $op['overwriteSame'] = true;
590 $cases[] = [ $op, 'yyy', 'yyy', true, true ];
593 $op['overwriteSame'] = true;
594 $cases[] = [ $op, 'yyy', 'zzz', false, true ];
597 $op['ignoreMissingSource'] = true;
598 $cases[] = [ $op, 'xxx', false, true, true ];
601 $op['ignoreMissingSource'] = true;
602 $cases[] = [ $op, false, false, true, true ];
605 $op['ignoreMissingSource'] = true;
606 $cases[] = [ $op, false, 'xxx', true, true ];
609 $op['src'] = 'mwstore://wrongbackend/unittest-cont1/e/file.txt';
610 $op['ignoreMissingSource'] = true;
611 $cases[] = [ $op, false, false, false, false ];
617 * @dataProvider provider_testDelete
619 public function testDelete( $op, $srcContent, $okStatus, $okSyncStatus ) {
620 $this->backend
= $this->singleBackend
;
621 $this->tearDownFiles();
622 $this->doTestDelete( $op, $srcContent, $okStatus, $okSyncStatus );
623 $this->tearDownFiles();
625 $this->backend
= $this->multiBackend
;
626 $this->tearDownFiles();
627 $this->doTestDelete( $op, $srcContent, $okStatus, $okSyncStatus );
628 $this->tearDownFiles();
631 private function doTestDelete( $op, $srcContent, $okStatus, $okSyncStatus ) {
632 $backendName = $this->backendClass();
634 $source = $op['src'];
635 $this->prepare( [ 'dir' => dirname( $source ) ] );
637 if ( is_string( $srcContent ) ) {
638 $status = $this->backend
->doOperation(
639 [ 'op' => 'create', 'content' => $srcContent, 'dst' => $source ] );
640 $this->assertGoodStatus( $status,
641 "Creation of file at $source succeeded ($backendName)." );
644 $status = $this->backend
->doOperation( $op );
646 $this->assertGoodStatus( $status,
647 "Deletion of file at $source succeeded without warnings ($backendName)." );
648 $this->assertEquals( true, $status->isOK(),
649 "Deletion of file at $source succeeded ($backendName)." );
650 $this->assertEquals( [ 0 => true ], $status->success
,
651 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
653 $this->assertEquals( false, $status->isOK(),
654 "Deletion of file at $source failed ($backendName)." );
658 (bool)$this->backend
->fileExists( [ 'src' => $source ] ),
659 "Source file $source does not exist after move ($backendName)." );
662 $this->backend
->getFileSize( [ 'src' => $source ] ),
663 "Source file $source has correct size (false) ($backendName)." );
665 $props1 = $this->backend
->getFileProps( [ 'src' => $source ] );
667 $props1['fileExists'],
668 "Source file $source does not exist according to props ($backendName)." );
670 $this->assertBackendPathsConsistent( [ $source ], $okSyncStatus );
674 * @return array (op, source content, op succeeds, sync check succeeds)
676 public static function provider_testDelete() {
679 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
680 $baseOp = [ 'op' => 'delete', 'src' => $source ];
683 $cases[] = [ $op, 'xxx', true, true ];
686 $op['ignoreMissingSource'] = true;
687 $cases[] = [ $op, 'xxx', true, true ];
690 $cases[] = [ $op, false, false, true ];
693 $op['ignoreMissingSource'] = true;
694 $cases[] = [ $op, false, true, true ];
697 $op['ignoreMissingSource'] = true;
698 $op['src'] = 'mwstore://wrongbackend/unittest-cont1/e/file.txt';
699 $cases[] = [ $op, false, false, false ];
705 * @dataProvider provider_testDescribe
707 public function testDescribe( $op, $withSource, $okStatus ) {
708 $this->backend
= $this->singleBackend
;
709 $this->tearDownFiles();
710 $this->doTestDescribe( $op, $withSource, $okStatus );
711 $this->tearDownFiles();
713 $this->backend
= $this->multiBackend
;
714 $this->tearDownFiles();
715 $this->doTestDescribe( $op, $withSource, $okStatus );
716 $this->tearDownFiles();
719 private function doTestDescribe( $op, $withSource, $okStatus ) {
720 $backendName = $this->backendClass();
722 $source = $op['src'];
723 $this->prepare( [ 'dir' => dirname( $source ) ] );
726 $status = $this->backend
->doOperation(
727 [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source,
728 'headers' => [ 'Content-Disposition' => 'xxx' ] ] );
729 $this->assertGoodStatus( $status,
730 "Creation of file at $source succeeded ($backendName)." );
731 if ( $this->backend
->hasFeatures( FileBackend
::ATTR_HEADERS
) ) {
732 $attr = $this->backend
->getFileXAttributes( [ 'src' => $source ] );
733 $this->assertHasHeaders( [ 'Content-Disposition' => 'xxx' ], $attr );
736 $status = $this->backend
->describe( [ 'src' => $source,
737 'headers' => [ 'Content-Disposition' => '' ] ] ); // remove
738 $this->assertGoodStatus( $status,
739 "Removal of header for $source succeeded ($backendName)." );
741 if ( $this->backend
->hasFeatures( FileBackend
::ATTR_HEADERS
) ) {
742 $attr = $this->backend
->getFileXAttributes( [ 'src' => $source ] );
743 $this->assertFalse( isset( $attr['headers']['content-disposition'] ),
744 "File 'Content-Disposition' header removed." );
748 $status = $this->backend
->doOperation( $op );
750 $this->assertGoodStatus( $status,
751 "Describe of file at $source succeeded without warnings ($backendName)." );
752 $this->assertEquals( true, $status->isOK(),
753 "Describe of file at $source succeeded ($backendName)." );
754 $this->assertEquals( [ 0 => true ], $status->success
,
755 "Describe of file at $source has proper 'success' field in Status ($backendName)." );
756 if ( $this->backend
->hasFeatures( FileBackend
::ATTR_HEADERS
) ) {
757 $attr = $this->backend
->getFileXAttributes( [ 'src' => $source ] );
758 $this->assertHasHeaders( $op['headers'], $attr );
761 $this->assertEquals( false, $status->isOK(),
762 "Describe of file at $source failed ($backendName)." );
765 $this->assertBackendPathsConsistent( [ $source ], true );
768 private function assertHasHeaders( array $headers, array $attr ) {
769 foreach ( $headers as $n => $v ) {
771 $this->assertTrue( isset( $attr['headers'][strtolower( $n )] ),
772 "File has '$n' header." );
773 $this->assertEquals( $v, $attr['headers'][strtolower( $n )],
774 "File has '$n' header value." );
776 $this->assertFalse( isset( $attr['headers'][strtolower( $n )] ),
777 "File does not have '$n' header." );
782 public static function provider_testDescribe() {
785 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
787 $op = [ 'op' => 'describe', 'src' => $source,
788 'headers' => [ 'Content-Disposition' => 'inline' ], ];
797 false, // without source
805 * @dataProvider provider_testCreate
807 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
808 $this->backend
= $this->singleBackend
;
809 $this->tearDownFiles();
810 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
811 $this->tearDownFiles();
813 $this->backend
= $this->multiBackend
;
814 $this->tearDownFiles();
815 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
816 $this->tearDownFiles();
819 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
820 $backendName = $this->backendClass();
823 $this->prepare( [ 'dir' => dirname( $dest ) ] );
825 $oldText = 'blah...blah...waahwaah';
826 if ( $alreadyExists ) {
827 $status = $this->backend
->doOperation(
828 [ 'op' => 'create', 'content' => $oldText, 'dst' => $dest ] );
829 $this->assertGoodStatus( $status,
830 "Creation of file at $dest succeeded ($backendName)." );
833 $status = $this->backend
->doOperation( $op );
835 $this->assertGoodStatus( $status,
836 "Creation of file at $dest succeeded without warnings ($backendName)." );
837 $this->assertEquals( true, $status->isOK(),
838 "Creation of file at $dest succeeded ($backendName)." );
839 $this->assertEquals( [ 0 => true ], $status->success
,
840 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
842 $this->assertEquals( false, $status->isOK(),
843 "Creation of file at $dest failed ($backendName)." );
846 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $dest ] ),
847 "Destination file $dest exists after creation ($backendName)." );
849 $props1 = $this->backend
->getFileProps( [ 'src' => $dest ] );
850 $this->assertEquals( true, $props1['fileExists'],
851 "Destination file $dest exists according to props ($backendName)." );
852 if ( $okStatus ) { // file content is what we saved
853 $this->assertEquals( $newSize, $props1['size'],
854 "Destination file $dest has expected size according to props ($backendName)." );
855 $this->assertEquals( $newSize,
856 $this->backend
->getFileSize( [ 'src' => $dest ] ),
857 "Destination file $dest has correct size ($backendName)." );
858 } else { // file content is some other previous text
859 $this->assertEquals( strlen( $oldText ), $props1['size'],
860 "Destination file $dest has original size according to props ($backendName)." );
861 $this->assertEquals( strlen( $oldText ),
862 $this->backend
->getFileSize( [ 'src' => $dest ] ),
863 "Destination file $dest has original size according to props ($backendName)." );
866 $this->assertBackendPathsConsistent( [ $dest ], true );
870 * @dataProvider provider_testCreate
872 public static function provider_testCreate() {
875 $dest = self
::baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
877 $op = [ 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest ];
880 false, // no dest already exists
882 strlen( $op['content'] )
886 $op2['content'] = "\n";
889 false, // no dest already exists
891 strlen( $op2['content'] )
895 $op2['content'] = "fsf\n waf 3kt";
898 true, // dest already exists
900 strlen( $op2['content'] )
904 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
905 $op2['overwrite'] = true;
908 true, // dest already exists
910 strlen( $op2['content'] )
914 $op2['content'] = "39qjmg3-qg";
915 $op2['overwriteSame'] = true;
918 true, // dest already exists
920 strlen( $op2['content'] )
926 public function testDoQuickOperations() {
927 $this->backend
= $this->singleBackend
;
928 $this->doTestDoQuickOperations();
929 $this->tearDownFiles();
931 $this->backend
= $this->multiBackend
;
932 $this->doTestDoQuickOperations();
933 $this->tearDownFiles();
936 private function doTestDoQuickOperations() {
937 $backendName = $this->backendClass();
939 $base = self
::baseStorePath();
941 "$base/unittest-cont1/e/fileA.a",
942 "$base/unittest-cont1/e/fileB.a",
943 "$base/unittest-cont1/e/fileC.a"
945 $createOps = $copyOps = $moveOps = $deleteOps = [];
946 foreach ( $files as $path ) {
947 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
948 $this->assertGoodStatus( $status,
949 "Preparing $path succeeded without warnings ($backendName)." );
950 $createOps[] = [ 'op' => 'create', 'dst' => $path, 'content' => mt_rand( 0, 50000 ) ];
951 $copyOps[] = [ 'op' => 'copy', 'src' => $path, 'dst' => "$path-2" ];
952 $moveOps[] = [ 'op' => 'move', 'src' => "$path-2", 'dst' => "$path-3" ];
955 'src' => "$path-nothing",
956 'dst' => "$path-nowhere",
957 'ignoreMissingSource' => true
959 $deleteOps[] = [ 'op' => 'delete', 'src' => $path ];
960 $deleteOps[] = [ 'op' => 'delete', 'src' => "$path-3" ];
963 'src' => "$path-gone",
964 'ignoreMissingSource' => true
967 $deleteOps[] = [ 'op' => 'null' ];
969 $this->assertGoodStatus(
970 $this->backend
->doQuickOperations( $createOps ),
971 "Creation of source files succeeded ($backendName)." );
972 foreach ( $files as $file ) {
973 $this->assertTrue( $this->backend
->fileExists( [ 'src' => $file ] ),
974 "File $file exists." );
977 $this->assertGoodStatus(
978 $this->backend
->doQuickOperations( $copyOps ),
979 "Quick copy of source files succeeded ($backendName)." );
980 foreach ( $files as $file ) {
981 $this->assertTrue( $this->backend
->fileExists( [ 'src' => "$file-2" ] ),
982 "File $file-2 exists." );
985 $this->assertGoodStatus(
986 $this->backend
->doQuickOperations( $moveOps ),
987 "Quick move of source files succeeded ($backendName)." );
988 foreach ( $files as $file ) {
989 $this->assertTrue( $this->backend
->fileExists( [ 'src' => "$file-3" ] ),
990 "File $file-3 move in." );
991 $this->assertFalse( $this->backend
->fileExists( [ 'src' => "$file-2" ] ),
992 "File $file-2 moved away." );
995 $this->assertGoodStatus(
996 $this->backend
->quickCopy( [ 'src' => $files[0], 'dst' => $files[0] ] ),
997 "Copy of file {$files[0]} over itself succeeded ($backendName)." );
998 $this->assertTrue( $this->backend
->fileExists( [ 'src' => $files[0] ] ),
999 "File {$files[0]} still exists." );
1001 $this->assertGoodStatus(
1002 $this->backend
->quickMove( [ 'src' => $files[0], 'dst' => $files[0] ] ),
1003 "Move of file {$files[0]} over itself succeeded ($backendName)." );
1004 $this->assertTrue( $this->backend
->fileExists( [ 'src' => $files[0] ] ),
1005 "File {$files[0]} still exists." );
1007 $this->assertGoodStatus(
1008 $this->backend
->doQuickOperations( $deleteOps ),
1009 "Quick deletion of source files succeeded ($backendName)." );
1010 foreach ( $files as $file ) {
1011 $this->assertFalse( $this->backend
->fileExists( [ 'src' => $file ] ),
1012 "File $file purged." );
1013 $this->assertFalse( $this->backend
->fileExists( [ 'src' => "$file-3" ] ),
1014 "File $file-3 purged." );
1019 * @dataProvider provider_testConcatenate
1021 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
1022 $this->backend
= $this->singleBackend
;
1023 $this->tearDownFiles();
1024 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
1025 $this->tearDownFiles();
1027 $this->backend
= $this->multiBackend
;
1028 $this->tearDownFiles();
1029 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
1030 $this->tearDownFiles();
1033 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
1034 $backendName = $this->backendClass();
1039 foreach ( $srcs as $i => $source ) {
1040 $this->prepare( [ 'dir' => dirname( $source ) ] );
1042 'op' => 'create', // operation
1043 'dst' => $source, // source
1044 'content' => $srcsContent[$i]
1046 $expContent .= $srcsContent[$i];
1048 $status = $this->backend
->doOperations( $ops );
1050 $this->assertGoodStatus( $status,
1051 "Creation of source files succeeded ($backendName)." );
1053 $dest = $params['dst'] = $this->getNewTempFile();
1054 if ( $alreadyExists ) {
1055 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
1056 $this->assertEquals( true, $ok,
1057 "Creation of file at $dest succeeded ($backendName)." );
1059 $ok = file_put_contents( $dest, '' ) !== false;
1060 $this->assertEquals( true, $ok,
1061 "Creation of 0-byte file at $dest succeeded ($backendName)." );
1064 // Combine the files into one
1065 $status = $this->backend
->concatenate( $params );
1067 $this->assertGoodStatus( $status,
1068 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
1069 $this->assertEquals( true, $status->isOK(),
1070 "Creation of concat file at $dest succeeded ($backendName)." );
1072 $this->assertEquals( false, $status->isOK(),
1073 "Creation of concat file at $dest failed ($backendName)." );
1077 $this->assertEquals( true, is_file( $dest ),
1078 "Dest concat file $dest exists after creation ($backendName)." );
1080 $this->assertEquals( true, is_file( $dest ),
1081 "Dest concat file $dest exists after failed creation ($backendName)." );
1084 $contents = file_get_contents( $dest );
1085 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
1088 $this->assertEquals( $expContent, $contents,
1089 "Concat file at $dest has correct contents ($backendName)." );
1091 $this->assertNotEquals( $expContent, $contents,
1092 "Concat file at $dest has correct contents ($backendName)." );
1096 public static function provider_testConcatenate() {
1100 self
::baseStorePath() . '/unittest-cont1/e/file1.txt',
1101 self
::baseStorePath() . '/unittest-cont1/e/file2.txt',
1102 self
::baseStorePath() . '/unittest-cont1/e/file3.txt',
1103 self
::baseStorePath() . '/unittest-cont1/e/file4.txt',
1104 self
::baseStorePath() . '/unittest-cont1/e/file5.txt',
1105 self
::baseStorePath() . '/unittest-cont1/e/file6.txt',
1106 self
::baseStorePath() . '/unittest-cont1/e/file7.txt',
1107 self
::baseStorePath() . '/unittest-cont1/e/file8.txt',
1108 self
::baseStorePath() . '/unittest-cont1/e/file9.txt',
1109 self
::baseStorePath() . '/unittest-cont1/e/file10.txt'
1123 $params = [ 'srcs' => $srcs ];
1126 $params, // operation
1128 $content, // content for each source
1129 false, // no dest already exists
1134 $params, // operation
1136 $content, // content for each source
1137 true, // dest already exists
1145 * @dataProvider provider_testGetFileStat
1147 public function testGetFileStat( $path, $content, $alreadyExists ) {
1148 $this->backend
= $this->singleBackend
;
1149 $this->tearDownFiles();
1150 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1151 $this->tearDownFiles();
1153 $this->backend
= $this->multiBackend
;
1154 $this->tearDownFiles();
1155 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1156 $this->tearDownFiles();
1159 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
1160 $backendName = $this->backendClass();
1162 if ( $alreadyExists ) {
1163 $this->prepare( [ 'dir' => dirname( $path ) ] );
1164 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1165 $this->assertGoodStatus( $status,
1166 "Creation of file at $path succeeded ($backendName)." );
1168 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1169 $time = $this->backend
->getFileTimestamp( [ 'src' => $path ] );
1170 $stat = $this->backend
->getFileStat( [ 'src' => $path ] );
1172 $this->assertEquals( strlen( $content ), $size,
1173 "Correct file size of '$path'" );
1174 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1175 "Correct file timestamp of '$path'" );
1177 $size = $stat['size'];
1178 $time = $stat['mtime'];
1179 $this->assertEquals( strlen( $content ), $size,
1180 "Correct file size of '$path'" );
1181 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1182 "Correct file timestamp of '$path'" );
1184 $this->backend
->clearCache( [ $path ] );
1186 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1188 $this->assertEquals( strlen( $content ), $size,
1189 "Correct file size of '$path'" );
1191 $this->backend
->preloadCache( [ $path ] );
1193 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1195 $this->assertEquals( strlen( $content ), $size,
1196 "Correct file size of '$path'" );
1198 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1199 $time = $this->backend
->getFileTimestamp( [ 'src' => $path ] );
1200 $stat = $this->backend
->getFileStat( [ 'src' => $path ] );
1202 $this->assertFalse( $size, "Correct file size of '$path'" );
1203 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1204 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1208 public static function provider_testGetFileStat() {
1211 $base = self
::baseStorePath();
1212 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true ];
1213 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "", true ];
1214 $cases[] = [ "$base/unittest-cont1/e/b/some-diff_file.txt", null, false ];
1220 * @dataProvider provider_testGetFileStat
1222 public function testStreamFile( $path, $content, $alreadyExists ) {
1223 $this->backend
= $this->singleBackend
;
1224 $this->tearDownFiles();
1225 $this->doTestStreamFile( $path, $content, $alreadyExists );
1226 $this->tearDownFiles();
1228 $this->backend
= $this->multiBackend
;
1229 $this->tearDownFiles();
1230 $this->doTestStreamFile( $path, $content, $alreadyExists );
1231 $this->tearDownFiles();
1234 private function doTestStreamFile( $path, $content ) {
1235 $backendName = $this->backendClass();
1237 if ( $content !== null ) {
1238 $this->prepare( [ 'dir' => dirname( $path ) ] );
1239 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1240 $this->assertGoodStatus( $status,
1241 "Creation of file at $path succeeded ($backendName)." );
1244 $this->backend
->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
1245 $data = ob_get_contents();
1248 $this->assertEquals( $content, $data, "Correct content streamed from '$path'" );
1249 } else { // 404 case
1251 $this->backend
->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
1252 $data = ob_get_contents();
1255 $this->assertRegExp( '#<h1>File not found</h1>#', $data,
1256 "Correct content streamed from '$path' ($backendName)" );
1260 public static function provider_testStreamFile() {
1263 $base = self
::baseStorePath();
1264 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
1265 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", null ];
1270 public function testStreamFileRange() {
1271 $this->backend
= $this->singleBackend
;
1272 $this->tearDownFiles();
1273 $this->doTestStreamFileRange();
1274 $this->tearDownFiles();
1276 $this->backend
= $this->multiBackend
;
1277 $this->tearDownFiles();
1278 $this->doTestStreamFileRange();
1279 $this->tearDownFiles();
1282 private function doTestStreamFileRange() {
1283 $backendName = $this->backendClass();
1285 $base = self
::baseStorePath();
1286 $path = "$base/unittest-cont1/e/b/z/range_file.txt";
1287 $content = "0123456789ABCDEF";
1289 $this->prepare( [ 'dir' => dirname( $path ) ] );
1290 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1291 $this->assertGoodStatus( $status,
1292 "Creation of file at $path succeeded ($backendName)." );
1296 'bytes=0-3' => '0123',
1297 'bytes=4-8' => '45678',
1298 'bytes=15-15' => 'F',
1299 'bytes=14-15' => 'EF',
1300 'bytes=-5' => 'BCDEF',
1302 'bytes=10-16' => 'ABCDEF',
1303 'bytes=10-99' => 'ABCDEF',
1306 foreach ( $ranges as $range => $chunk ) {
1308 $this->backend
->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1,
1309 'options' => [ 'range' => $range ] ] );
1310 $data = ob_get_contents();
1313 $this->assertEquals( $chunk, $data, "Correct chunk streamed from '$path' for '$range'" );
1318 * @dataProvider provider_testGetFileContents
1320 public function testGetFileContents( $source, $content ) {
1321 $this->backend
= $this->singleBackend
;
1322 $this->tearDownFiles();
1323 $this->doTestGetFileContents( $source, $content );
1324 $this->tearDownFiles();
1326 $this->backend
= $this->multiBackend
;
1327 $this->tearDownFiles();
1328 $this->doTestGetFileContents( $source, $content );
1329 $this->tearDownFiles();
1332 private function doTestGetFileContents( $source, $content ) {
1333 $backendName = $this->backendClass();
1335 $srcs = (array)$source;
1336 $content = (array)$content;
1337 foreach ( $srcs as $i => $src ) {
1338 $this->prepare( [ 'dir' => dirname( $src ) ] );
1339 $status = $this->backend
->doOperation(
1340 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1341 $this->assertGoodStatus( $status,
1342 "Creation of file at $src succeeded ($backendName)." );
1345 if ( is_array( $source ) ) {
1346 $contents = $this->backend
->getFileContentsMulti( [ 'srcs' => $source ] );
1347 foreach ( $contents as $path => $data ) {
1348 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1349 $this->assertEquals(
1350 current( $content ),
1352 "Contents of $path is correct ($backendName)."
1356 $this->assertEquals(
1358 array_keys( $contents ),
1359 "Contents in right order ($backendName)."
1361 $this->assertEquals(
1364 "Contents array size correct ($backendName)."
1367 $data = $this->backend
->getFileContents( [ 'src' => $source ] );
1368 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1369 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1373 public static function provider_testGetFileContents() {
1376 $base = self
::baseStorePath();
1377 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
1378 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" ];
1380 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1381 "$base/unittest-cont1/e/a/z.txt" ],
1382 [ "contents xx", "contents xy", "contents xz" ]
1389 * @dataProvider provider_testGetLocalCopy
1391 public function testGetLocalCopy( $source, $content ) {
1392 $this->backend
= $this->singleBackend
;
1393 $this->tearDownFiles();
1394 $this->doTestGetLocalCopy( $source, $content );
1395 $this->tearDownFiles();
1397 $this->backend
= $this->multiBackend
;
1398 $this->tearDownFiles();
1399 $this->doTestGetLocalCopy( $source, $content );
1400 $this->tearDownFiles();
1403 private function doTestGetLocalCopy( $source, $content ) {
1404 $backendName = $this->backendClass();
1406 $srcs = (array)$source;
1407 $content = (array)$content;
1408 foreach ( $srcs as $i => $src ) {
1409 $this->prepare( [ 'dir' => dirname( $src ) ] );
1410 $status = $this->backend
->doOperation(
1411 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1412 $this->assertGoodStatus( $status,
1413 "Creation of file at $src succeeded ($backendName)." );
1416 if ( is_array( $source ) ) {
1417 $tmpFiles = $this->backend
->getLocalCopyMulti( [ 'srcs' => $source ] );
1418 foreach ( $tmpFiles as $path => $tmpFile ) {
1419 $this->assertNotNull( $tmpFile,
1420 "Creation of local copy of $path succeeded ($backendName)." );
1421 $contents = file_get_contents( $tmpFile->getPath() );
1422 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1423 $this->assertEquals(
1424 current( $content ),
1426 "Local copy of $path is correct ($backendName)."
1430 $this->assertEquals(
1432 array_keys( $tmpFiles ),
1433 "Local copies in right order ($backendName)."
1435 $this->assertEquals(
1438 "Local copies array size correct ($backendName)."
1441 $tmpFile = $this->backend
->getLocalCopy( [ 'src' => $source ] );
1442 $this->assertNotNull( $tmpFile,
1443 "Creation of local copy of $source succeeded ($backendName)." );
1444 $contents = file_get_contents( $tmpFile->getPath() );
1445 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1446 $this->assertEquals(
1449 "Local copy of $source is correct ($backendName)."
1453 $obj = new stdClass();
1454 $tmpFile->bind( $obj );
1457 public static function provider_testGetLocalCopy() {
1460 $base = self
::baseStorePath();
1461 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1462 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1463 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1465 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1466 "$base/unittest-cont1/e/a/z.txt" ],
1467 [ "contents xx $", "contents xy 111", "contents xz" ]
1474 * @dataProvider provider_testGetLocalReference
1476 public function testGetLocalReference( $source, $content ) {
1477 $this->backend
= $this->singleBackend
;
1478 $this->tearDownFiles();
1479 $this->doTestGetLocalReference( $source, $content );
1480 $this->tearDownFiles();
1482 $this->backend
= $this->multiBackend
;
1483 $this->tearDownFiles();
1484 $this->doTestGetLocalReference( $source, $content );
1485 $this->tearDownFiles();
1488 private function doTestGetLocalReference( $source, $content ) {
1489 $backendName = $this->backendClass();
1491 $srcs = (array)$source;
1492 $content = (array)$content;
1493 foreach ( $srcs as $i => $src ) {
1494 $this->prepare( [ 'dir' => dirname( $src ) ] );
1495 $status = $this->backend
->doOperation(
1496 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1497 $this->assertGoodStatus( $status,
1498 "Creation of file at $src succeeded ($backendName)." );
1501 if ( is_array( $source ) ) {
1502 $tmpFiles = $this->backend
->getLocalReferenceMulti( [ 'srcs' => $source ] );
1503 foreach ( $tmpFiles as $path => $tmpFile ) {
1504 $this->assertNotNull( $tmpFile,
1505 "Creation of local copy of $path succeeded ($backendName)." );
1506 $contents = file_get_contents( $tmpFile->getPath() );
1507 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1508 $this->assertEquals(
1509 current( $content ),
1511 "Local ref of $path is correct ($backendName)."
1515 $this->assertEquals(
1517 array_keys( $tmpFiles ),
1518 "Local refs in right order ($backendName)."
1520 $this->assertEquals(
1523 "Local refs array size correct ($backendName)."
1526 $tmpFile = $this->backend
->getLocalReference( [ 'src' => $source ] );
1527 $this->assertNotNull( $tmpFile,
1528 "Creation of local copy of $source succeeded ($backendName)." );
1529 $contents = file_get_contents( $tmpFile->getPath() );
1530 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1531 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1535 public static function provider_testGetLocalReference() {
1538 $base = self
::baseStorePath();
1539 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1540 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1541 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1543 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1544 "$base/unittest-cont1/e/a/z.txt" ],
1545 [ "contents xx 1111", "contents xy %", "contents xz $" ]
1551 public function testGetLocalCopyAndReference404() {
1552 $this->backend
= $this->singleBackend
;
1553 $this->tearDownFiles();
1554 $this->doTestGetLocalCopyAndReference404();
1555 $this->tearDownFiles();
1557 $this->backend
= $this->multiBackend
;
1558 $this->tearDownFiles();
1559 $this->doTestGetLocalCopyAndReference404();
1560 $this->tearDownFiles();
1563 public function doTestGetLocalCopyAndReference404() {
1564 $backendName = $this->backendClass();
1566 $base = self
::baseStorePath();
1568 $tmpFile = $this->backend
->getLocalCopy( [
1569 'src' => "$base/unittest-cont1/not-there" ] );
1570 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1572 $tmpFile = $this->backend
->getLocalReference( [
1573 'src' => "$base/unittest-cont1/not-there" ] );
1574 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1578 * @dataProvider provider_testGetFileHttpUrl
1580 public function testGetFileHttpUrl( $source, $content ) {
1581 $this->backend
= $this->singleBackend
;
1582 $this->tearDownFiles();
1583 $this->doTestGetFileHttpUrl( $source, $content );
1584 $this->tearDownFiles();
1586 $this->backend
= $this->multiBackend
;
1587 $this->tearDownFiles();
1588 $this->doTestGetFileHttpUrl( $source, $content );
1589 $this->tearDownFiles();
1592 private function doTestGetFileHttpUrl( $source, $content ) {
1593 $backendName = $this->backendClass();
1595 $this->prepare( [ 'dir' => dirname( $source ) ] );
1596 $status = $this->backend
->doOperation(
1597 [ 'op' => 'create', 'content' => $content, 'dst' => $source ] );
1598 $this->assertGoodStatus( $status,
1599 "Creation of file at $source succeeded ($backendName)." );
1601 $url = $this->backend
->getFileHttpUrl( [ 'src' => $source ] );
1603 if ( $url !== null ) { // supported
1604 $data = MediaWikiServices
::getInstance()->getHttpRequestFactory()->
1605 get( $url, [], __METHOD__
);
1606 $this->assertEquals( $content, $data,
1607 "HTTP GET of URL has right contents ($backendName)." );
1611 public static function provider_testGetFileHttpUrl() {
1614 $base = self
::baseStorePath();
1615 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1616 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1617 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1623 * @dataProvider provider_testPrepareAndClean
1625 public function testPrepareAndClean( $path, $isOK ) {
1626 $this->backend
= $this->singleBackend
;
1627 $this->doTestPrepareAndClean( $path, $isOK );
1628 $this->tearDownFiles();
1630 $this->backend
= $this->multiBackend
;
1631 $this->doTestPrepareAndClean( $path, $isOK );
1632 $this->tearDownFiles();
1635 public static function provider_testPrepareAndClean() {
1636 $base = self
::baseStorePath();
1639 [ "$base/unittest-cont1/e/a/z/some_file1.txt", true ],
1640 [ "$base/unittest-cont2/a/z/some_file2.txt", true ],
1641 # Specific to FS backend with no basePath field set
1642 # [ "$base/unittest-cont3/a/z/some_file3.txt", false ],
1646 private function doTestPrepareAndClean( $path, $isOK ) {
1647 $backendName = $this->backendClass();
1649 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
1651 $this->assertGoodStatus( $status,
1652 "Preparing dir $path succeeded without warnings ($backendName)." );
1653 $this->assertEquals( true, $status->isOK(),
1654 "Preparing dir $path succeeded ($backendName)." );
1656 $this->assertEquals( false, $status->isOK(),
1657 "Preparing dir $path failed ($backendName)." );
1660 $status = $this->backend
->secure( [ 'dir' => dirname( $path ) ] );
1662 $this->assertGoodStatus( $status,
1663 "Securing dir $path succeeded without warnings ($backendName)." );
1664 $this->assertEquals( true, $status->isOK(),
1665 "Securing dir $path succeeded ($backendName)." );
1667 $this->assertEquals( false, $status->isOK(),
1668 "Securing dir $path failed ($backendName)." );
1671 $status = $this->backend
->publish( [ 'dir' => dirname( $path ) ] );
1673 $this->assertGoodStatus( $status,
1674 "Publishing dir $path succeeded without warnings ($backendName)." );
1675 $this->assertEquals( true, $status->isOK(),
1676 "Publishing dir $path succeeded ($backendName)." );
1678 $this->assertEquals( false, $status->isOK(),
1679 "Publishing dir $path failed ($backendName)." );
1682 $status = $this->backend
->clean( [ 'dir' => dirname( $path ) ] );
1684 $this->assertGoodStatus( $status,
1685 "Cleaning dir $path succeeded without warnings ($backendName)." );
1686 $this->assertEquals( true, $status->isOK(),
1687 "Cleaning dir $path succeeded ($backendName)." );
1689 $this->assertEquals( false, $status->isOK(),
1690 "Cleaning dir $path failed ($backendName)." );
1694 public function testRecursiveClean() {
1695 $this->backend
= $this->singleBackend
;
1696 $this->doTestRecursiveClean();
1697 $this->tearDownFiles();
1699 $this->backend
= $this->multiBackend
;
1700 $this->doTestRecursiveClean();
1701 $this->tearDownFiles();
1704 private function doTestRecursiveClean() {
1705 $backendName = $this->backendClass();
1707 $base = self
::baseStorePath();
1709 "$base/unittest-cont1",
1710 "$base/unittest-cont1/e",
1711 "$base/unittest-cont1/e/a",
1712 "$base/unittest-cont1/e/a/b",
1713 "$base/unittest-cont1/e/a/b/c",
1714 "$base/unittest-cont1/e/a/b/c/d0",
1715 "$base/unittest-cont1/e/a/b/c/d1",
1716 "$base/unittest-cont1/e/a/b/c/d2",
1717 "$base/unittest-cont1/e/a/b/c/d0/1",
1718 "$base/unittest-cont1/e/a/b/c/d0/2",
1719 "$base/unittest-cont1/e/a/b/c/d1/3",
1720 "$base/unittest-cont1/e/a/b/c/d1/4",
1721 "$base/unittest-cont1/e/a/b/c/d2/5",
1722 "$base/unittest-cont1/e/a/b/c/d2/6"
1724 foreach ( $dirs as $dir ) {
1725 $status = $this->prepare( [ 'dir' => $dir ] );
1726 $this->assertGoodStatus( $status,
1727 "Preparing dir $dir succeeded without warnings ($backendName)." );
1730 if ( $this->backend
instanceof FSFileBackend
) {
1731 foreach ( $dirs as $dir ) {
1732 $this->assertEquals( true, $this->backend
->directoryExists( [ 'dir' => $dir ] ),
1733 "Dir $dir exists ($backendName)." );
1737 $status = $this->backend
->clean(
1738 [ 'dir' => "$base/unittest-cont1", 'recursive' => 1 ] );
1739 $this->assertGoodStatus( $status,
1740 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1742 foreach ( $dirs as $dir ) {
1743 $this->assertEquals( false, $this->backend
->directoryExists( [ 'dir' => $dir ] ),
1744 "Dir $dir no longer exists ($backendName)." );
1748 public function testDoOperations() {
1749 $this->backend
= $this->singleBackend
;
1750 $this->tearDownFiles();
1751 $this->doTestDoOperations();
1752 $this->tearDownFiles();
1754 $this->backend
= $this->multiBackend
;
1755 $this->tearDownFiles();
1756 $this->doTestDoOperations();
1757 $this->tearDownFiles();
1760 private function doTestDoOperations() {
1761 $base = self
::baseStorePath();
1763 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1764 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1765 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1766 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1767 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1768 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1769 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1771 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1772 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1773 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1774 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
1775 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1776 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
1777 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1779 $status = $this->backend
->doOperations( [
1780 [ 'op' => 'describe', 'src' => $fileA,
1781 'headers' => [ 'X-Content-Length' => '91.3' ], 'disposition' => 'inline' ],
1782 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1783 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1784 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1785 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1786 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1787 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1788 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1789 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1790 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1791 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1792 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1793 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1794 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1795 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1796 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1797 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1798 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1800 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1802 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1804 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1810 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1811 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1812 $this->assertEquals( 14, count( $status->success
),
1813 "Operation batch has correct success array" );
1815 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileA ] ),
1816 "File does not exist at $fileA" );
1817 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileB ] ),
1818 "File does not exist at $fileB" );
1819 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileD ] ),
1820 "File does not exist at $fileD" );
1822 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileC ] ),
1823 "File exists at $fileC" );
1824 $this->assertEquals( $fileBContents,
1825 $this->backend
->getFileContents( [ 'src' => $fileC ] ),
1826 "Correct file contents of $fileC" );
1827 $this->assertEquals( strlen( $fileBContents ),
1828 $this->backend
->getFileSize( [ 'src' => $fileC ] ),
1829 "Correct file size of $fileC" );
1830 $this->assertEquals( Wikimedia\base_convert
( sha1( $fileBContents ), 16, 36, 31 ),
1831 $this->backend
->getFileSha1Base36( [ 'src' => $fileC ] ),
1832 "Correct file SHA-1 of $fileC" );
1835 public function testDoOperationsPipeline() {
1836 $this->backend
= $this->singleBackend
;
1837 $this->tearDownFiles();
1838 $this->doTestDoOperationsPipeline();
1839 $this->tearDownFiles();
1841 $this->backend
= $this->multiBackend
;
1842 $this->tearDownFiles();
1843 $this->doTestDoOperationsPipeline();
1844 $this->tearDownFiles();
1847 // concurrency orientated
1848 private function doTestDoOperationsPipeline() {
1849 $base = self
::baseStorePath();
1851 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1852 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1853 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1855 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1856 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1857 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1858 $this->addTmpFiles( [ $tmpNameA, $tmpNameB, $tmpNameC ] );
1859 file_put_contents( $tmpNameA, $fileAContents );
1860 file_put_contents( $tmpNameB, $fileBContents );
1861 file_put_contents( $tmpNameC, $fileCContents );
1863 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1864 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1865 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1866 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1868 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1869 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1870 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1871 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1872 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1874 $status = $this->backend
->doOperations( [
1875 [ 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ],
1876 [ 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ],
1877 [ 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ],
1878 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1879 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1880 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1881 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1882 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1883 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1884 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1885 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1886 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1887 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1888 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1889 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1890 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1891 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1892 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1893 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1894 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1896 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1898 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1900 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1906 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1907 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1908 $this->assertEquals( 16, count( $status->success
),
1909 "Operation batch has correct success array" );
1911 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileA ] ),
1912 "File does not exist at $fileA" );
1913 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileB ] ),
1914 "File does not exist at $fileB" );
1915 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileD ] ),
1916 "File does not exist at $fileD" );
1918 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileC ] ),
1919 "File exists at $fileC" );
1920 $this->assertEquals( $fileBContents,
1921 $this->backend
->getFileContents( [ 'src' => $fileC ] ),
1922 "Correct file contents of $fileC" );
1923 $this->assertEquals( strlen( $fileBContents ),
1924 $this->backend
->getFileSize( [ 'src' => $fileC ] ),
1925 "Correct file size of $fileC" );
1926 $this->assertEquals( Wikimedia\base_convert
( sha1( $fileBContents ), 16, 36, 31 ),
1927 $this->backend
->getFileSha1Base36( [ 'src' => $fileC ] ),
1928 "Correct file SHA-1 of $fileC" );
1931 public function testDoOperationsFailing() {
1932 $this->backend
= $this->singleBackend
;
1933 $this->tearDownFiles();
1934 $this->doTestDoOperationsFailing();
1935 $this->tearDownFiles();
1937 $this->backend
= $this->multiBackend
;
1938 $this->tearDownFiles();
1939 $this->doTestDoOperationsFailing();
1940 $this->tearDownFiles();
1943 private function doTestDoOperationsFailing() {
1944 $base = self
::baseStorePath();
1946 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1947 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1948 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1949 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1950 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1951 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1952 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1954 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1955 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1956 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1957 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
1958 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1959 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
1961 $status = $this->backend
->doOperations( [
1962 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1963 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1964 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1965 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1966 [ 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ],
1967 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1968 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ],
1969 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1970 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ],
1971 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1972 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ],
1973 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1974 [ 'op' => 'delete', 'src' => $fileD ],
1975 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1978 ], [ 'force' => 1 ] );
1980 $this->assertNotEquals( [], $status->getErrors(), "Operation had warnings" );
1981 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1982 $this->assertEquals( 8, count( $status->success
),
1983 "Operation batch has correct success array" );
1985 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileB ] ),
1986 "File does not exist at $fileB" );
1987 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileD ] ),
1988 "File does not exist at $fileD" );
1990 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileA ] ),
1991 "File does not exist at $fileA" );
1992 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileC ] ),
1993 "File exists at $fileC" );
1994 $this->assertEquals( $fileBContents,
1995 $this->backend
->getFileContents( [ 'src' => $fileA ] ),
1996 "Correct file contents of $fileA" );
1997 $this->assertEquals( strlen( $fileBContents ),
1998 $this->backend
->getFileSize( [ 'src' => $fileA ] ),
1999 "Correct file size of $fileA" );
2000 $this->assertEquals( Wikimedia\base_convert
( sha1( $fileBContents ), 16, 36, 31 ),
2001 $this->backend
->getFileSha1Base36( [ 'src' => $fileA ] ),
2002 "Correct file SHA-1 of $fileA" );
2005 public function testGetFileList() {
2006 $this->backend
= $this->singleBackend
;
2007 $this->tearDownFiles();
2008 $this->doTestGetFileList();
2009 $this->tearDownFiles();
2011 $this->backend
= $this->multiBackend
;
2012 $this->tearDownFiles();
2013 $this->doTestGetFileList();
2014 $this->tearDownFiles();
2017 private function doTestGetFileList() {
2018 $backendName = $this->backendClass();
2019 $base = self
::baseStorePath();
2021 // Should have no errors
2022 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont-notexists" ] );
2025 "$base/unittest-cont1/e/test1.txt",
2026 "$base/unittest-cont1/e/test2.txt",
2027 "$base/unittest-cont1/e/test3.txt",
2028 "$base/unittest-cont1/e/subdir1/test1.txt",
2029 "$base/unittest-cont1/e/subdir1/test2.txt",
2030 "$base/unittest-cont1/e/subdir2/test3.txt",
2031 "$base/unittest-cont1/e/subdir2/test4.txt",
2032 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
2033 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
2034 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
2035 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
2036 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
2037 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
2038 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
2043 foreach ( $files as $file ) {
2044 $this->prepare( [ 'dir' => dirname( $file ) ] );
2045 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
2047 $status = $this->backend
->doQuickOperations( $ops );
2048 $this->assertGoodStatus( $status,
2049 "Creation of files succeeded ($backendName)." );
2050 $this->assertEquals( true, $status->isOK(),
2051 "Creation of files succeeded with OK status ($backendName)." );
2053 // Expected listing at root
2058 "e/subdir1/test1.txt",
2059 "e/subdir1/test2.txt",
2060 "e/subdir2/test3.txt",
2061 "e/subdir2/test4.txt",
2062 "e/subdir2/subdir/test1.txt",
2063 "e/subdir2/subdir/test2.txt",
2064 "e/subdir2/subdir/test3.txt",
2065 "e/subdir2/subdir/test4.txt",
2066 "e/subdir2/subdir/test5.txt",
2067 "e/subdir2/subdir/sub/test0.txt",
2068 "e/subdir2/subdir/sub/120-px-file.txt",
2072 // Actual listing (no trailing slash) at root
2073 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1" ] );
2074 $list = $this->listToArray( $iter );
2076 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2078 // Actual listing (no trailing slash) at root with advise
2079 $iter = $this->backend
->getFileList( [
2080 'dir' => "$base/unittest-cont1",
2083 $list = $this->listToArray( $iter );
2085 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2087 // Actual listing (with trailing slash) at root
2089 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/" ] );
2090 foreach ( $iter as $file ) {
2094 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2096 // Expected listing at subdir
2104 "sub/120-px-file.txt",
2108 // Actual listing (no trailing slash) at subdir
2109 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] );
2110 $list = $this->listToArray( $iter );
2112 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2114 // Actual listing (no trailing slash) at subdir with advise
2115 $iter = $this->backend
->getFileList( [
2116 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2119 $list = $this->listToArray( $iter );
2121 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2123 // Actual listing (with trailing slash) at subdir
2125 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ] );
2126 foreach ( $iter as $file ) {
2130 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2132 // Actual listing (using iterator second time)
2133 $list = $this->listToArray( $iter );
2135 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
2137 // Actual listing (top files only) at root
2138 $iter = $this->backend
->getTopFileList( [ 'dir' => "$base/unittest-cont1" ] );
2139 $list = $this->listToArray( $iter );
2141 $this->assertEquals( [], $list, "Correct top file listing ($backendName)." );
2143 // Expected listing (top files only) at subdir
2153 // Actual listing (top files only) at subdir
2154 $iter = $this->backend
->getTopFileList(
2155 [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ]
2157 $list = $this->listToArray( $iter );
2159 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2161 // Actual listing (top files only) at subdir with advise
2162 $iter = $this->backend
->getTopFileList( [
2163 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2166 $list = $this->listToArray( $iter );
2168 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2170 foreach ( $files as $file ) { // clean up
2171 $this->backend
->doOperation( [ 'op' => 'delete', 'src' => $file ] );
2174 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
2175 foreach ( $iter as $iter ) {
2180 public function testGetDirectoryList() {
2181 $this->backend
= $this->singleBackend
;
2182 $this->tearDownFiles();
2183 $this->doTestGetDirectoryList();
2184 $this->tearDownFiles();
2186 $this->backend
= $this->multiBackend
;
2187 $this->tearDownFiles();
2188 $this->doTestGetDirectoryList();
2189 $this->tearDownFiles();
2192 private function doTestGetDirectoryList() {
2193 $backendName = $this->backendClass();
2195 $base = self
::baseStorePath();
2197 "$base/unittest-cont1/e/test1.txt",
2198 "$base/unittest-cont1/e/test2.txt",
2199 "$base/unittest-cont1/e/test3.txt",
2200 "$base/unittest-cont1/e/subdir1/test1.txt",
2201 "$base/unittest-cont1/e/subdir1/test2.txt",
2202 "$base/unittest-cont1/e/subdir2/test3.txt",
2203 "$base/unittest-cont1/e/subdir2/test4.txt",
2204 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
2205 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
2206 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
2207 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
2208 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
2209 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
2210 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
2215 foreach ( $files as $file ) {
2216 $this->prepare( [ 'dir' => dirname( $file ) ] );
2217 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
2219 $status = $this->backend
->doQuickOperations( $ops );
2220 $this->assertGoodStatus( $status,
2221 "Creation of files succeeded ($backendName)." );
2222 $this->assertEquals( true, $status->isOK(),
2223 "Creation of files succeeded with OK status ($backendName)." );
2225 $this->assertEquals( true,
2226 $this->backend
->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] ),
2227 "Directory exists in ($backendName)." );
2228 $this->assertEquals( true,
2229 $this->backend
->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] ),
2230 "Directory exists in ($backendName)." );
2231 $this->assertEquals( false,
2232 $this->backend
->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ] ),
2233 "Directory does not exists in ($backendName)." );
2241 // Actual listing (no trailing slash)
2243 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1" ] );
2244 foreach ( $iter as $file ) {
2249 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2260 // Actual listing (no trailing slash)
2262 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e" ] );
2263 foreach ( $iter as $file ) {
2268 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2270 // Actual listing (with trailing slash)
2272 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/" ] );
2273 foreach ( $iter as $file ) {
2278 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2286 // Actual listing (no trailing slash)
2288 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir2" ] );
2289 foreach ( $iter as $file ) {
2294 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2296 // Actual listing (with trailing slash)
2298 $iter = $this->backend
->getTopDirectoryList(
2299 [ 'dir' => "$base/unittest-cont1/e/subdir2/" ]
2302 foreach ( $iter as $file ) {
2307 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2309 // Actual listing (using iterator second time)
2311 foreach ( $iter as $file ) {
2316 $this->assertEquals(
2319 "Correct top dir listing ($backendName), second iteration."
2322 // Expected listing (recursive)
2332 "e/subdir4/subdir/sub",
2336 // Actual listing (recursive)
2338 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/" ] );
2339 foreach ( $iter as $file ) {
2344 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2346 // Expected listing (recursive)
2353 // Actual listing (recursive)
2355 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir4" ] );
2356 foreach ( $iter as $file ) {
2361 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2363 // Actual listing (recursive, second time)
2365 foreach ( $iter as $file ) {
2370 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2372 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] );
2373 $items = $this->listToArray( $iter );
2374 $this->assertEquals( [], $items, "Directory listing is empty." );
2376 foreach ( $files as $file ) { // clean up
2377 $this->backend
->doOperation( [ 'op' => 'delete', 'src' => $file ] );
2380 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
2381 foreach ( $iter as $file ) {
2385 $items = $this->listToArray( $iter );
2386 $this->assertEquals( [], $items, "Directory listing is empty." );
2388 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/not/exists" ] );
2389 $items = $this->listToArray( $iter );
2390 $this->assertEquals( [], $items, "Directory listing is empty." );
2393 public function testLockCalls() {
2394 $this->backend
= $this->singleBackend
;
2395 $this->doTestLockCalls();
2398 private function doTestLockCalls() {
2399 $backendName = $this->backendClass();
2400 $base = $this->backend
->getContainerStoragePath( 'test' );
2407 "$base/subdir1", // duplicate
2408 "$base/subdir1/test1.txt",
2409 "$base/subdir1/test2.txt",
2411 "$base/subdir2", // duplicate
2412 "$base/subdir2/test3.txt",
2413 "$base/subdir2/test4.txt",
2414 "$base/subdir2/subdir",
2415 "$base/subdir2/subdir/test1.txt",
2416 "$base/subdir2/subdir/test2.txt",
2417 "$base/subdir2/subdir/test3.txt",
2418 "$base/subdir2/subdir/test4.txt",
2419 "$base/subdir2/subdir/test5.txt",
2420 "$base/subdir2/subdir/sub",
2421 "$base/subdir2/subdir/sub/test0.txt",
2422 "$base/subdir2/subdir/sub/120-px-file.txt",
2425 for ( $i = 0; $i < 2; $i++
) {
2426 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2427 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2428 "Locking of files succeeded ($backendName) ($i)." );
2429 $this->assertEquals( true, $status->isOK(),
2430 "Locking of files succeeded with OK status ($backendName) ($i)." );
2432 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2433 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2434 "Locking of files succeeded ($backendName) ($i)." );
2435 $this->assertEquals( true, $status->isOK(),
2436 "Locking of files succeeded with OK status ($backendName) ($i)." );
2438 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2439 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2440 "Locking of files succeeded ($backendName) ($i)." );
2441 $this->assertEquals( true, $status->isOK(),
2442 "Locking of files succeeded with OK status ($backendName) ($i)." );
2444 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2445 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2446 "Locking of files succeeded ($backendName). ($i)" );
2447 $this->assertEquals( true, $status->isOK(),
2448 "Locking of files succeeded with OK status ($backendName) ($i)." );
2450 # # Flip the acquire/release ordering around ##
2452 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2453 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2454 "Locking of files succeeded ($backendName) ($i)." );
2455 $this->assertEquals( true, $status->isOK(),
2456 "Locking of files succeeded with OK status ($backendName) ($i)." );
2458 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2459 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2460 "Locking of files succeeded ($backendName) ($i)." );
2461 $this->assertEquals( true, $status->isOK(),
2462 "Locking of files succeeded with OK status ($backendName) ($i)." );
2464 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2465 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2466 "Locking of files succeeded ($backendName). ($i)" );
2467 $this->assertEquals( true, $status->isOK(),
2468 "Locking of files succeeded with OK status ($backendName) ($i)." );
2470 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2471 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2472 "Locking of files succeeded ($backendName) ($i)." );
2473 $this->assertEquals( true, $status->isOK(),
2474 "Locking of files succeeded with OK status ($backendName) ($i)." );
2477 $status = Status
::newGood();
2478 $sl = $this->backend
->getScopedFileLocks( $paths, LockManager
::LOCK_EX
, $status );
2479 $this->assertInstanceOf( ScopedLock
::class, $sl,
2480 "Scoped locking of files succeeded ($backendName)." );
2481 $this->assertEquals( [], $status->getErrors(),
2482 "Scoped locking of files succeeded ($backendName)." );
2483 $this->assertEquals( true, $status->isOK(),
2484 "Scoped locking of files succeeded with OK status ($backendName)." );
2486 ScopedLock
::release( $sl );
2487 $this->assertEquals( null, $sl,
2488 "Scoped unlocking of files succeeded ($backendName)." );
2489 $this->assertEquals( [], $status->getErrors(),
2490 "Scoped unlocking of files succeeded ($backendName)." );
2491 $this->assertEquals( true, $status->isOK(),
2492 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2496 * @dataProvider provider_testGetContentType
2498 public function testGetContentType( $mimeCallback, $mimeFromString ) {
2501 $be = TestingAccessWrapper
::newFromObject( new MemoryFileBackend(
2503 'name' => 'testing',
2504 'class' => MemoryFileBackend
::class,
2506 'mimeCallback' => $mimeCallback
2510 $dst = 'mwstore://testing/container/path/to/file_no_ext';
2511 $src = "$IP/tests/phpunit/data/media/srgb.jpg";
2512 $this->assertEquals( 'image/jpeg', $be->getContentType( $dst, null, $src ) );
2513 $this->assertEquals(
2514 $mimeFromString ?
'image/jpeg' : 'unknown/unknown',
2515 $be->getContentType( $dst, file_get_contents( $src ), null ) );
2517 $src = "$IP/tests/phpunit/data/media/Png-native-test.png";
2518 $this->assertEquals( 'image/png', $be->getContentType( $dst, null, $src ) );
2519 $this->assertEquals(
2520 $mimeFromString ?
'image/png' : 'unknown/unknown',
2521 $be->getContentType( $dst, file_get_contents( $src ), null ) );
2524 public static function provider_testGetContentType() {
2527 [ [ FileBackendGroup
::singleton(), 'guessMimeInternal' ], true ]
2531 public function testReadAffinity() {
2532 $be = TestingAccessWrapper
::newFromObject(
2533 new FileBackendMultiWrite( [
2534 'name' => 'localtesting',
2535 'wikiId' => wfWikiID() . mt_rand(),
2538 'name' => 'multitesting0',
2539 'class' => MemoryFileBackend
::class,
2540 'isMultiMaster' => false,
2541 'readAffinity' => true
2544 'name' => 'multitesting1',
2545 'class' => MemoryFileBackend
::class,
2546 'isMultiMaster' => true
2552 $this->assertEquals(
2554 $be->getReadIndexFromParams( [ 'latest' => 1 ] ),
2555 'Reads with "latest" flag use backend 1'
2559 $be->getReadIndexFromParams( [ 'latest' => 0 ] ),
2560 'Reads without "latest" flag use backend 0'
2563 $p = 'container/test-cont/file.txt';
2564 $be->backends
[0]->quickCreate( [
2565 'dst' => "mwstore://multitesting0/$p", 'content' => 'cattitude' ] );
2566 $be->backends
[1]->quickCreate( [
2567 'dst' => "mwstore://multitesting1/$p", 'content' => 'princess of power' ] );
2569 $this->assertEquals(
2571 $be->getFileContents( [ 'src' => "mwstore://localtesting/$p" ] ),
2572 "Non-latest read came from backend 0"
2574 $this->assertEquals(
2575 'princess of power',
2576 $be->getFileContents( [ 'src' => "mwstore://localtesting/$p", 'latest' => 1 ] ),
2577 "Latest read came from backend1"
2581 public function testAsyncWrites() {
2582 $be = TestingAccessWrapper
::newFromObject(
2583 new FileBackendMultiWrite( [
2584 'name' => 'localtesting',
2585 'wikiId' => wfWikiID() . mt_rand(),
2588 'name' => 'multitesting0',
2589 'class' => MemoryFileBackend
::class,
2590 'isMultiMaster' => false
2593 'name' => 'multitesting1',
2594 'class' => MemoryFileBackend
::class,
2595 'isMultiMaster' => true
2598 'replication' => 'async'
2602 $this->setMwGlobals( 'wgCommandLineMode', false );
2604 $p = 'container/test-cont/file.txt';
2606 'dst' => "mwstore://localtesting/$p", 'content' => 'cattitude' ] );
2608 $this->assertEquals(
2610 $be->backends
[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
2611 "File not yet written to backend 0"
2613 $this->assertEquals(
2615 $be->backends
[1]->getFileContents( [ 'src' => "mwstore://multitesting1/$p" ] ),
2616 "File already written to backend 1"
2619 DeferredUpdates
::doUpdates();
2621 $this->assertEquals(
2623 $be->backends
[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
2624 "File now written to backend 0"
2628 public function testSanitizeOpHeaders() {
2629 $be = TestingAccessWrapper
::newFromObject( new MemoryFileBackend( [
2630 'name' => 'localtesting',
2631 'wikiId' => wfWikiID()
2636 'content-Disposition' => FileBackend
::makeContentDisposition( 'inline', 'name' ),
2637 'Content-dUration' => 25.6,
2638 'X-LONG-VALUE' => str_pad( '0', 300 ),
2639 'CONTENT-LENGTH' => 855055,
2644 'content-disposition' => FileBackend
::makeContentDisposition( 'inline', 'name' ),
2645 'content-duration' => 25.6,
2646 'content-length' => 855055
2650 Wikimedia\
suppressWarnings();
2651 $actual = $be->sanitizeOpHeaders( $input );
2652 Wikimedia\restoreWarnings
();
2654 $this->assertEquals( $expected, $actual, "Header sanitized properly" );
2658 private function listToArray( $iter ) {
2659 return is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2662 // test helper wrapper for backend prepare() function
2663 private function prepare( array $params ) {
2664 return $this->backend
->prepare( $params );
2667 // test helper wrapper for backend prepare() function
2668 private function create( array $params ) {
2669 $params['op'] = 'create';
2671 return $this->backend
->doQuickOperations( [ $params ] );
2674 function tearDownFiles() {
2675 $containers = [ 'unittest-cont1', 'unittest-cont2' ];
2676 foreach ( $containers as $container ) {
2677 $this->deleteFiles( $container );
2681 private function deleteFiles( $container ) {
2682 $base = self
::baseStorePath();
2683 $iter = $this->backend
->getFileList( [ 'dir' => "$base/$container" ] );
2685 foreach ( $iter as $file ) {
2686 $this->backend
->quickDelete( [ 'src' => "$base/$container/$file" ] );
2688 // free the directory, to avoid Permission denied under windows on rmdir
2691 $this->backend
->clean( [ 'dir' => "$base/$container", 'recursive' => 1 ] );
2694 private function assertBackendPathsConsistent( array $paths, $okSyncStatus ) {
2695 if ( !$this->backend
instanceof FileBackendMultiWrite
) {
2699 $status = $this->backend
->consistencyCheck( $paths );
2700 if ( $okSyncStatus ) {
2701 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2703 $this->assertBadStatus( $status, "Files not synced: " . implode( ',', $paths ) );
2707 private function assertGoodStatus( StatusValue
$status, $msg ) {
2708 $this->assertEquals( print_r( [], 1 ), print_r( $status->getErrors(), 1 ), $msg );
2711 private function assertBadStatus( StatusValue
$status, $msg ) {
2712 $this->assertNotEquals( print_r( [], 1 ), print_r( $status->getErrors(), 1 ), $msg );