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"
947 foreach ( $files as $path ) {
948 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
949 $this->assertGoodStatus( $status,
950 "Preparing $path succeeded without warnings ($backendName)." );
951 $createOps[] = [ 'op' => 'create', 'dst' => $path, 'content' => mt_rand( 0, 50000 ) ];
952 $copyOps[] = [ 'op' => 'copy', 'src' => $path, 'dst' => "$path-2" ];
953 $moveOps[] = [ 'op' => 'move', 'src' => "$path-2", 'dst' => "$path-3" ];
954 $delOps[] = [ 'op' => 'delete', 'src' => $path ];
955 $delOps[] = [ 'op' => 'delete', 'src' => "$path-3" ];
956 $delOps[] = [ 'op' => 'delete', 'src' => "$path-gone", 'ignoreMissingSource' => true ];
958 $delOps[] = [ 'op' => 'null' ];
960 $this->assertGoodStatus(
961 $this->backend
->doQuickOperations( $createOps ),
962 "Creation of source files succeeded ($backendName)." );
963 foreach ( $files as $file ) {
964 $this->assertTrue( $this->backend
->fileExists( [ 'src' => $file ] ),
965 "File $file exists." );
968 $this->assertGoodStatus(
969 $this->backend
->doQuickOperations( $copyOps ),
970 "Quick copy of source files succeeded ($backendName)." );
971 foreach ( $files as $file ) {
972 $this->assertTrue( $this->backend
->fileExists( [ 'src' => "$file-2" ] ),
973 "File $file-2 exists." );
976 $this->assertGoodStatus(
977 $this->backend
->doQuickOperations( $moveOps ),
978 "Quick move of source files succeeded ($backendName)." );
979 foreach ( $files as $file ) {
980 $this->assertTrue( $this->backend
->fileExists( [ 'src' => "$file-3" ] ),
981 "File $file-3 move in." );
982 $this->assertFalse( $this->backend
->fileExists( [ 'src' => "$file-2" ] ),
983 "File $file-2 moved away." );
986 $this->assertGoodStatus(
987 $this->backend
->quickCopy( [ 'src' => $files[0], 'dst' => $files[0] ] ),
988 "Copy of file {$files[0]} over itself succeeded ($backendName)." );
989 $this->assertTrue( $this->backend
->fileExists( [ 'src' => $files[0] ] ),
990 "File {$files[0]} still exists." );
992 $this->assertGoodStatus(
993 $this->backend
->quickMove( [ 'src' => $files[0], 'dst' => $files[0] ] ),
994 "Move of file {$files[0]} over itself succeeded ($backendName)." );
995 $this->assertTrue( $this->backend
->fileExists( [ 'src' => $files[0] ] ),
996 "File {$files[0]} still exists." );
998 $this->assertGoodStatus(
999 $this->backend
->doQuickOperations( $delOps ),
1000 "Quick deletion of source files succeeded ($backendName)." );
1001 foreach ( $files as $file ) {
1002 $this->assertFalse( $this->backend
->fileExists( [ 'src' => $file ] ),
1003 "File $file purged." );
1004 $this->assertFalse( $this->backend
->fileExists( [ 'src' => "$file-3" ] ),
1005 "File $file-3 purged." );
1010 * @dataProvider provider_testConcatenate
1012 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
1013 $this->backend
= $this->singleBackend
;
1014 $this->tearDownFiles();
1015 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
1016 $this->tearDownFiles();
1018 $this->backend
= $this->multiBackend
;
1019 $this->tearDownFiles();
1020 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
1021 $this->tearDownFiles();
1024 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
1025 $backendName = $this->backendClass();
1030 foreach ( $srcs as $i => $source ) {
1031 $this->prepare( [ 'dir' => dirname( $source ) ] );
1033 'op' => 'create', // operation
1034 'dst' => $source, // source
1035 'content' => $srcsContent[$i]
1037 $expContent .= $srcsContent[$i];
1039 $status = $this->backend
->doOperations( $ops );
1041 $this->assertGoodStatus( $status,
1042 "Creation of source files succeeded ($backendName)." );
1044 $dest = $params['dst'] = $this->getNewTempFile();
1045 if ( $alreadyExists ) {
1046 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
1047 $this->assertEquals( true, $ok,
1048 "Creation of file at $dest succeeded ($backendName)." );
1050 $ok = file_put_contents( $dest, '' ) !== false;
1051 $this->assertEquals( true, $ok,
1052 "Creation of 0-byte file at $dest succeeded ($backendName)." );
1055 // Combine the files into one
1056 $status = $this->backend
->concatenate( $params );
1058 $this->assertGoodStatus( $status,
1059 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
1060 $this->assertEquals( true, $status->isOK(),
1061 "Creation of concat file at $dest succeeded ($backendName)." );
1063 $this->assertEquals( false, $status->isOK(),
1064 "Creation of concat file at $dest failed ($backendName)." );
1068 $this->assertEquals( true, is_file( $dest ),
1069 "Dest concat file $dest exists after creation ($backendName)." );
1071 $this->assertEquals( true, is_file( $dest ),
1072 "Dest concat file $dest exists after failed creation ($backendName)." );
1075 $contents = file_get_contents( $dest );
1076 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
1079 $this->assertEquals( $expContent, $contents,
1080 "Concat file at $dest has correct contents ($backendName)." );
1082 $this->assertNotEquals( $expContent, $contents,
1083 "Concat file at $dest has correct contents ($backendName)." );
1087 public static function provider_testConcatenate() {
1091 self
::baseStorePath() . '/unittest-cont1/e/file1.txt',
1092 self
::baseStorePath() . '/unittest-cont1/e/file2.txt',
1093 self
::baseStorePath() . '/unittest-cont1/e/file3.txt',
1094 self
::baseStorePath() . '/unittest-cont1/e/file4.txt',
1095 self
::baseStorePath() . '/unittest-cont1/e/file5.txt',
1096 self
::baseStorePath() . '/unittest-cont1/e/file6.txt',
1097 self
::baseStorePath() . '/unittest-cont1/e/file7.txt',
1098 self
::baseStorePath() . '/unittest-cont1/e/file8.txt',
1099 self
::baseStorePath() . '/unittest-cont1/e/file9.txt',
1100 self
::baseStorePath() . '/unittest-cont1/e/file10.txt'
1114 $params = [ 'srcs' => $srcs ];
1117 $params, // operation
1119 $content, // content for each source
1120 false, // no dest already exists
1125 $params, // operation
1127 $content, // content for each source
1128 true, // dest already exists
1136 * @dataProvider provider_testGetFileStat
1138 public function testGetFileStat( $path, $content, $alreadyExists ) {
1139 $this->backend
= $this->singleBackend
;
1140 $this->tearDownFiles();
1141 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1142 $this->tearDownFiles();
1144 $this->backend
= $this->multiBackend
;
1145 $this->tearDownFiles();
1146 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1147 $this->tearDownFiles();
1150 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
1151 $backendName = $this->backendClass();
1153 if ( $alreadyExists ) {
1154 $this->prepare( [ 'dir' => dirname( $path ) ] );
1155 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1156 $this->assertGoodStatus( $status,
1157 "Creation of file at $path succeeded ($backendName)." );
1159 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1160 $time = $this->backend
->getFileTimestamp( [ 'src' => $path ] );
1161 $stat = $this->backend
->getFileStat( [ 'src' => $path ] );
1163 $this->assertEquals( strlen( $content ), $size,
1164 "Correct file size of '$path'" );
1165 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1166 "Correct file timestamp of '$path'" );
1168 $size = $stat['size'];
1169 $time = $stat['mtime'];
1170 $this->assertEquals( strlen( $content ), $size,
1171 "Correct file size of '$path'" );
1172 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1173 "Correct file timestamp of '$path'" );
1175 $this->backend
->clearCache( [ $path ] );
1177 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1179 $this->assertEquals( strlen( $content ), $size,
1180 "Correct file size of '$path'" );
1182 $this->backend
->preloadCache( [ $path ] );
1184 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1186 $this->assertEquals( strlen( $content ), $size,
1187 "Correct file size of '$path'" );
1189 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1190 $time = $this->backend
->getFileTimestamp( [ 'src' => $path ] );
1191 $stat = $this->backend
->getFileStat( [ 'src' => $path ] );
1193 $this->assertFalse( $size, "Correct file size of '$path'" );
1194 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1195 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1199 public static function provider_testGetFileStat() {
1202 $base = self
::baseStorePath();
1203 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true ];
1204 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "", true ];
1205 $cases[] = [ "$base/unittest-cont1/e/b/some-diff_file.txt", null, false ];
1211 * @dataProvider provider_testGetFileStat
1213 public function testStreamFile( $path, $content, $alreadyExists ) {
1214 $this->backend
= $this->singleBackend
;
1215 $this->tearDownFiles();
1216 $this->doTestStreamFile( $path, $content, $alreadyExists );
1217 $this->tearDownFiles();
1219 $this->backend
= $this->multiBackend
;
1220 $this->tearDownFiles();
1221 $this->doTestStreamFile( $path, $content, $alreadyExists );
1222 $this->tearDownFiles();
1225 private function doTestStreamFile( $path, $content ) {
1226 $backendName = $this->backendClass();
1228 if ( $content !== null ) {
1229 $this->prepare( [ 'dir' => dirname( $path ) ] );
1230 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1231 $this->assertGoodStatus( $status,
1232 "Creation of file at $path succeeded ($backendName)." );
1235 $this->backend
->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
1236 $data = ob_get_contents();
1239 $this->assertEquals( $content, $data, "Correct content streamed from '$path'" );
1240 } else { // 404 case
1242 $this->backend
->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
1243 $data = ob_get_contents();
1246 $this->assertRegExp( '#<h1>File not found</h1>#', $data,
1247 "Correct content streamed from '$path' ($backendName)" );
1251 public static function provider_testStreamFile() {
1254 $base = self
::baseStorePath();
1255 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
1256 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", null ];
1261 public function testStreamFileRange() {
1262 $this->backend
= $this->singleBackend
;
1263 $this->tearDownFiles();
1264 $this->doTestStreamFileRange();
1265 $this->tearDownFiles();
1267 $this->backend
= $this->multiBackend
;
1268 $this->tearDownFiles();
1269 $this->doTestStreamFileRange();
1270 $this->tearDownFiles();
1273 private function doTestStreamFileRange() {
1274 $backendName = $this->backendClass();
1276 $base = self
::baseStorePath();
1277 $path = "$base/unittest-cont1/e/b/z/range_file.txt";
1278 $content = "0123456789ABCDEF";
1280 $this->prepare( [ 'dir' => dirname( $path ) ] );
1281 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1282 $this->assertGoodStatus( $status,
1283 "Creation of file at $path succeeded ($backendName)." );
1287 'bytes=0-3' => '0123',
1288 'bytes=4-8' => '45678',
1289 'bytes=15-15' => 'F',
1290 'bytes=14-15' => 'EF',
1291 'bytes=-5' => 'BCDEF',
1293 'bytes=10-16' => 'ABCDEF',
1294 'bytes=10-99' => 'ABCDEF',
1297 foreach ( $ranges as $range => $chunk ) {
1299 $this->backend
->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1,
1300 'options' => [ 'range' => $range ] ] );
1301 $data = ob_get_contents();
1304 $this->assertEquals( $chunk, $data, "Correct chunk streamed from '$path' for '$range'" );
1309 * @dataProvider provider_testGetFileContents
1311 public function testGetFileContents( $source, $content ) {
1312 $this->backend
= $this->singleBackend
;
1313 $this->tearDownFiles();
1314 $this->doTestGetFileContents( $source, $content );
1315 $this->tearDownFiles();
1317 $this->backend
= $this->multiBackend
;
1318 $this->tearDownFiles();
1319 $this->doTestGetFileContents( $source, $content );
1320 $this->tearDownFiles();
1323 private function doTestGetFileContents( $source, $content ) {
1324 $backendName = $this->backendClass();
1326 $srcs = (array)$source;
1327 $content = (array)$content;
1328 foreach ( $srcs as $i => $src ) {
1329 $this->prepare( [ 'dir' => dirname( $src ) ] );
1330 $status = $this->backend
->doOperation(
1331 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1332 $this->assertGoodStatus( $status,
1333 "Creation of file at $src succeeded ($backendName)." );
1336 if ( is_array( $source ) ) {
1337 $contents = $this->backend
->getFileContentsMulti( [ 'srcs' => $source ] );
1338 foreach ( $contents as $path => $data ) {
1339 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1340 $this->assertEquals(
1341 current( $content ),
1343 "Contents of $path is correct ($backendName)."
1347 $this->assertEquals(
1349 array_keys( $contents ),
1350 "Contents in right order ($backendName)."
1352 $this->assertEquals(
1355 "Contents array size correct ($backendName)."
1358 $data = $this->backend
->getFileContents( [ 'src' => $source ] );
1359 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1360 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1364 public static function provider_testGetFileContents() {
1367 $base = self
::baseStorePath();
1368 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
1369 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" ];
1371 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1372 "$base/unittest-cont1/e/a/z.txt" ],
1373 [ "contents xx", "contents xy", "contents xz" ]
1380 * @dataProvider provider_testGetLocalCopy
1382 public function testGetLocalCopy( $source, $content ) {
1383 $this->backend
= $this->singleBackend
;
1384 $this->tearDownFiles();
1385 $this->doTestGetLocalCopy( $source, $content );
1386 $this->tearDownFiles();
1388 $this->backend
= $this->multiBackend
;
1389 $this->tearDownFiles();
1390 $this->doTestGetLocalCopy( $source, $content );
1391 $this->tearDownFiles();
1394 private function doTestGetLocalCopy( $source, $content ) {
1395 $backendName = $this->backendClass();
1397 $srcs = (array)$source;
1398 $content = (array)$content;
1399 foreach ( $srcs as $i => $src ) {
1400 $this->prepare( [ 'dir' => dirname( $src ) ] );
1401 $status = $this->backend
->doOperation(
1402 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1403 $this->assertGoodStatus( $status,
1404 "Creation of file at $src succeeded ($backendName)." );
1407 if ( is_array( $source ) ) {
1408 $tmpFiles = $this->backend
->getLocalCopyMulti( [ 'srcs' => $source ] );
1409 foreach ( $tmpFiles as $path => $tmpFile ) {
1410 $this->assertNotNull( $tmpFile,
1411 "Creation of local copy of $path succeeded ($backendName)." );
1412 $contents = file_get_contents( $tmpFile->getPath() );
1413 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1414 $this->assertEquals(
1415 current( $content ),
1417 "Local copy of $path is correct ($backendName)."
1421 $this->assertEquals(
1423 array_keys( $tmpFiles ),
1424 "Local copies in right order ($backendName)."
1426 $this->assertEquals(
1429 "Local copies array size correct ($backendName)."
1432 $tmpFile = $this->backend
->getLocalCopy( [ 'src' => $source ] );
1433 $this->assertNotNull( $tmpFile,
1434 "Creation of local copy of $source succeeded ($backendName)." );
1435 $contents = file_get_contents( $tmpFile->getPath() );
1436 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1437 $this->assertEquals(
1440 "Local copy of $source is correct ($backendName)."
1444 $obj = new stdClass();
1445 $tmpFile->bind( $obj );
1448 public static function provider_testGetLocalCopy() {
1451 $base = self
::baseStorePath();
1452 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1453 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1454 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1456 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1457 "$base/unittest-cont1/e/a/z.txt" ],
1458 [ "contents xx $", "contents xy 111", "contents xz" ]
1465 * @dataProvider provider_testGetLocalReference
1467 public function testGetLocalReference( $source, $content ) {
1468 $this->backend
= $this->singleBackend
;
1469 $this->tearDownFiles();
1470 $this->doTestGetLocalReference( $source, $content );
1471 $this->tearDownFiles();
1473 $this->backend
= $this->multiBackend
;
1474 $this->tearDownFiles();
1475 $this->doTestGetLocalReference( $source, $content );
1476 $this->tearDownFiles();
1479 private function doTestGetLocalReference( $source, $content ) {
1480 $backendName = $this->backendClass();
1482 $srcs = (array)$source;
1483 $content = (array)$content;
1484 foreach ( $srcs as $i => $src ) {
1485 $this->prepare( [ 'dir' => dirname( $src ) ] );
1486 $status = $this->backend
->doOperation(
1487 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1488 $this->assertGoodStatus( $status,
1489 "Creation of file at $src succeeded ($backendName)." );
1492 if ( is_array( $source ) ) {
1493 $tmpFiles = $this->backend
->getLocalReferenceMulti( [ 'srcs' => $source ] );
1494 foreach ( $tmpFiles as $path => $tmpFile ) {
1495 $this->assertNotNull( $tmpFile,
1496 "Creation of local copy of $path succeeded ($backendName)." );
1497 $contents = file_get_contents( $tmpFile->getPath() );
1498 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1499 $this->assertEquals(
1500 current( $content ),
1502 "Local ref of $path is correct ($backendName)."
1506 $this->assertEquals(
1508 array_keys( $tmpFiles ),
1509 "Local refs in right order ($backendName)."
1511 $this->assertEquals(
1514 "Local refs array size correct ($backendName)."
1517 $tmpFile = $this->backend
->getLocalReference( [ 'src' => $source ] );
1518 $this->assertNotNull( $tmpFile,
1519 "Creation of local copy of $source succeeded ($backendName)." );
1520 $contents = file_get_contents( $tmpFile->getPath() );
1521 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1522 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1526 public static function provider_testGetLocalReference() {
1529 $base = self
::baseStorePath();
1530 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1531 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1532 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1534 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1535 "$base/unittest-cont1/e/a/z.txt" ],
1536 [ "contents xx 1111", "contents xy %", "contents xz $" ]
1542 public function testGetLocalCopyAndReference404() {
1543 $this->backend
= $this->singleBackend
;
1544 $this->tearDownFiles();
1545 $this->doTestGetLocalCopyAndReference404();
1546 $this->tearDownFiles();
1548 $this->backend
= $this->multiBackend
;
1549 $this->tearDownFiles();
1550 $this->doTestGetLocalCopyAndReference404();
1551 $this->tearDownFiles();
1554 public function doTestGetLocalCopyAndReference404() {
1555 $backendName = $this->backendClass();
1557 $base = self
::baseStorePath();
1559 $tmpFile = $this->backend
->getLocalCopy( [
1560 'src' => "$base/unittest-cont1/not-there" ] );
1561 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1563 $tmpFile = $this->backend
->getLocalReference( [
1564 'src' => "$base/unittest-cont1/not-there" ] );
1565 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1569 * @dataProvider provider_testGetFileHttpUrl
1571 public function testGetFileHttpUrl( $source, $content ) {
1572 $this->backend
= $this->singleBackend
;
1573 $this->tearDownFiles();
1574 $this->doTestGetFileHttpUrl( $source, $content );
1575 $this->tearDownFiles();
1577 $this->backend
= $this->multiBackend
;
1578 $this->tearDownFiles();
1579 $this->doTestGetFileHttpUrl( $source, $content );
1580 $this->tearDownFiles();
1583 private function doTestGetFileHttpUrl( $source, $content ) {
1584 $backendName = $this->backendClass();
1586 $this->prepare( [ 'dir' => dirname( $source ) ] );
1587 $status = $this->backend
->doOperation(
1588 [ 'op' => 'create', 'content' => $content, 'dst' => $source ] );
1589 $this->assertGoodStatus( $status,
1590 "Creation of file at $source succeeded ($backendName)." );
1592 $url = $this->backend
->getFileHttpUrl( [ 'src' => $source ] );
1594 if ( $url !== null ) { // supported
1595 $data = MediaWikiServices
::getInstance()->getHttpRequestFactory()->
1596 get( $url, [], __METHOD__
);
1597 $this->assertEquals( $content, $data,
1598 "HTTP GET of URL has right contents ($backendName)." );
1602 public static function provider_testGetFileHttpUrl() {
1605 $base = self
::baseStorePath();
1606 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1607 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1608 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1614 * @dataProvider provider_testPrepareAndClean
1616 public function testPrepareAndClean( $path, $isOK ) {
1617 $this->backend
= $this->singleBackend
;
1618 $this->doTestPrepareAndClean( $path, $isOK );
1619 $this->tearDownFiles();
1621 $this->backend
= $this->multiBackend
;
1622 $this->doTestPrepareAndClean( $path, $isOK );
1623 $this->tearDownFiles();
1626 public static function provider_testPrepareAndClean() {
1627 $base = self
::baseStorePath();
1630 [ "$base/unittest-cont1/e/a/z/some_file1.txt", true ],
1631 [ "$base/unittest-cont2/a/z/some_file2.txt", true ],
1632 # Specific to FS backend with no basePath field set
1633 # [ "$base/unittest-cont3/a/z/some_file3.txt", false ],
1637 private function doTestPrepareAndClean( $path, $isOK ) {
1638 $backendName = $this->backendClass();
1640 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
1642 $this->assertGoodStatus( $status,
1643 "Preparing dir $path succeeded without warnings ($backendName)." );
1644 $this->assertEquals( true, $status->isOK(),
1645 "Preparing dir $path succeeded ($backendName)." );
1647 $this->assertEquals( false, $status->isOK(),
1648 "Preparing dir $path failed ($backendName)." );
1651 $status = $this->backend
->secure( [ 'dir' => dirname( $path ) ] );
1653 $this->assertGoodStatus( $status,
1654 "Securing dir $path succeeded without warnings ($backendName)." );
1655 $this->assertEquals( true, $status->isOK(),
1656 "Securing dir $path succeeded ($backendName)." );
1658 $this->assertEquals( false, $status->isOK(),
1659 "Securing dir $path failed ($backendName)." );
1662 $status = $this->backend
->publish( [ 'dir' => dirname( $path ) ] );
1664 $this->assertGoodStatus( $status,
1665 "Publishing dir $path succeeded without warnings ($backendName)." );
1666 $this->assertEquals( true, $status->isOK(),
1667 "Publishing dir $path succeeded ($backendName)." );
1669 $this->assertEquals( false, $status->isOK(),
1670 "Publishing dir $path failed ($backendName)." );
1673 $status = $this->backend
->clean( [ 'dir' => dirname( $path ) ] );
1675 $this->assertGoodStatus( $status,
1676 "Cleaning dir $path succeeded without warnings ($backendName)." );
1677 $this->assertEquals( true, $status->isOK(),
1678 "Cleaning dir $path succeeded ($backendName)." );
1680 $this->assertEquals( false, $status->isOK(),
1681 "Cleaning dir $path failed ($backendName)." );
1685 public function testRecursiveClean() {
1686 $this->backend
= $this->singleBackend
;
1687 $this->doTestRecursiveClean();
1688 $this->tearDownFiles();
1690 $this->backend
= $this->multiBackend
;
1691 $this->doTestRecursiveClean();
1692 $this->tearDownFiles();
1695 private function doTestRecursiveClean() {
1696 $backendName = $this->backendClass();
1698 $base = self
::baseStorePath();
1700 "$base/unittest-cont1",
1701 "$base/unittest-cont1/e",
1702 "$base/unittest-cont1/e/a",
1703 "$base/unittest-cont1/e/a/b",
1704 "$base/unittest-cont1/e/a/b/c",
1705 "$base/unittest-cont1/e/a/b/c/d0",
1706 "$base/unittest-cont1/e/a/b/c/d1",
1707 "$base/unittest-cont1/e/a/b/c/d2",
1708 "$base/unittest-cont1/e/a/b/c/d0/1",
1709 "$base/unittest-cont1/e/a/b/c/d0/2",
1710 "$base/unittest-cont1/e/a/b/c/d1/3",
1711 "$base/unittest-cont1/e/a/b/c/d1/4",
1712 "$base/unittest-cont1/e/a/b/c/d2/5",
1713 "$base/unittest-cont1/e/a/b/c/d2/6"
1715 foreach ( $dirs as $dir ) {
1716 $status = $this->prepare( [ 'dir' => $dir ] );
1717 $this->assertGoodStatus( $status,
1718 "Preparing dir $dir succeeded without warnings ($backendName)." );
1721 if ( $this->backend
instanceof FSFileBackend
) {
1722 foreach ( $dirs as $dir ) {
1723 $this->assertEquals( true, $this->backend
->directoryExists( [ 'dir' => $dir ] ),
1724 "Dir $dir exists ($backendName)." );
1728 $status = $this->backend
->clean(
1729 [ 'dir' => "$base/unittest-cont1", 'recursive' => 1 ] );
1730 $this->assertGoodStatus( $status,
1731 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1733 foreach ( $dirs as $dir ) {
1734 $this->assertEquals( false, $this->backend
->directoryExists( [ 'dir' => $dir ] ),
1735 "Dir $dir no longer exists ($backendName)." );
1739 public function testDoOperations() {
1740 $this->backend
= $this->singleBackend
;
1741 $this->tearDownFiles();
1742 $this->doTestDoOperations();
1743 $this->tearDownFiles();
1745 $this->backend
= $this->multiBackend
;
1746 $this->tearDownFiles();
1747 $this->doTestDoOperations();
1748 $this->tearDownFiles();
1751 private function doTestDoOperations() {
1752 $base = self
::baseStorePath();
1754 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1755 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1756 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1757 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1758 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1759 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1760 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1762 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1763 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1764 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1765 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
1766 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1767 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
1768 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1770 $status = $this->backend
->doOperations( [
1771 [ 'op' => 'describe', 'src' => $fileA,
1772 'headers' => [ 'X-Content-Length' => '91.3' ], 'disposition' => 'inline' ],
1773 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1774 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1775 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1776 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1777 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1778 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1779 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1780 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1781 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1782 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1783 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1784 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1785 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1786 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1787 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1788 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1789 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1791 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1793 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1795 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1801 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1802 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1803 $this->assertEquals( 14, count( $status->success
),
1804 "Operation batch has correct success array" );
1806 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileA ] ),
1807 "File does not exist at $fileA" );
1808 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileB ] ),
1809 "File does not exist at $fileB" );
1810 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileD ] ),
1811 "File does not exist at $fileD" );
1813 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileC ] ),
1814 "File exists at $fileC" );
1815 $this->assertEquals( $fileBContents,
1816 $this->backend
->getFileContents( [ 'src' => $fileC ] ),
1817 "Correct file contents of $fileC" );
1818 $this->assertEquals( strlen( $fileBContents ),
1819 $this->backend
->getFileSize( [ 'src' => $fileC ] ),
1820 "Correct file size of $fileC" );
1821 $this->assertEquals( Wikimedia\base_convert
( sha1( $fileBContents ), 16, 36, 31 ),
1822 $this->backend
->getFileSha1Base36( [ 'src' => $fileC ] ),
1823 "Correct file SHA-1 of $fileC" );
1826 public function testDoOperationsPipeline() {
1827 $this->backend
= $this->singleBackend
;
1828 $this->tearDownFiles();
1829 $this->doTestDoOperationsPipeline();
1830 $this->tearDownFiles();
1832 $this->backend
= $this->multiBackend
;
1833 $this->tearDownFiles();
1834 $this->doTestDoOperationsPipeline();
1835 $this->tearDownFiles();
1838 // concurrency orientated
1839 private function doTestDoOperationsPipeline() {
1840 $base = self
::baseStorePath();
1842 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1843 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1844 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1846 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1847 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1848 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1849 $this->addTmpFiles( [ $tmpNameA, $tmpNameB, $tmpNameC ] );
1850 file_put_contents( $tmpNameA, $fileAContents );
1851 file_put_contents( $tmpNameB, $fileBContents );
1852 file_put_contents( $tmpNameC, $fileCContents );
1854 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1855 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1856 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1857 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1859 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1860 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1861 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1862 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1863 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1865 $status = $this->backend
->doOperations( [
1866 [ 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ],
1867 [ 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ],
1868 [ 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ],
1869 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1870 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1871 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1872 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1873 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1874 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1875 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1876 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1877 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1878 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1879 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1880 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1881 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1882 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1883 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1884 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1885 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1887 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1889 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1891 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1897 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1898 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1899 $this->assertEquals( 16, count( $status->success
),
1900 "Operation batch has correct success array" );
1902 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileA ] ),
1903 "File does not exist at $fileA" );
1904 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileB ] ),
1905 "File does not exist at $fileB" );
1906 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileD ] ),
1907 "File does not exist at $fileD" );
1909 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileC ] ),
1910 "File exists at $fileC" );
1911 $this->assertEquals( $fileBContents,
1912 $this->backend
->getFileContents( [ 'src' => $fileC ] ),
1913 "Correct file contents of $fileC" );
1914 $this->assertEquals( strlen( $fileBContents ),
1915 $this->backend
->getFileSize( [ 'src' => $fileC ] ),
1916 "Correct file size of $fileC" );
1917 $this->assertEquals( Wikimedia\base_convert
( sha1( $fileBContents ), 16, 36, 31 ),
1918 $this->backend
->getFileSha1Base36( [ 'src' => $fileC ] ),
1919 "Correct file SHA-1 of $fileC" );
1922 public function testDoOperationsFailing() {
1923 $this->backend
= $this->singleBackend
;
1924 $this->tearDownFiles();
1925 $this->doTestDoOperationsFailing();
1926 $this->tearDownFiles();
1928 $this->backend
= $this->multiBackend
;
1929 $this->tearDownFiles();
1930 $this->doTestDoOperationsFailing();
1931 $this->tearDownFiles();
1934 private function doTestDoOperationsFailing() {
1935 $base = self
::baseStorePath();
1937 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1938 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1939 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1940 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1941 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1942 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1943 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1945 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1946 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1947 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1948 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
1949 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1950 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
1952 $status = $this->backend
->doOperations( [
1953 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1954 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1955 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1956 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1957 [ 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ],
1958 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1959 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ],
1960 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1961 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ],
1962 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1963 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ],
1964 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1965 [ 'op' => 'delete', 'src' => $fileD ],
1966 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1969 ], [ 'force' => 1 ] );
1971 $this->assertNotEquals( [], $status->getErrors(), "Operation had warnings" );
1972 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1973 $this->assertEquals( 8, count( $status->success
),
1974 "Operation batch has correct success array" );
1976 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileB ] ),
1977 "File does not exist at $fileB" );
1978 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileD ] ),
1979 "File does not exist at $fileD" );
1981 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileA ] ),
1982 "File does not exist at $fileA" );
1983 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileC ] ),
1984 "File exists at $fileC" );
1985 $this->assertEquals( $fileBContents,
1986 $this->backend
->getFileContents( [ 'src' => $fileA ] ),
1987 "Correct file contents of $fileA" );
1988 $this->assertEquals( strlen( $fileBContents ),
1989 $this->backend
->getFileSize( [ 'src' => $fileA ] ),
1990 "Correct file size of $fileA" );
1991 $this->assertEquals( Wikimedia\base_convert
( sha1( $fileBContents ), 16, 36, 31 ),
1992 $this->backend
->getFileSha1Base36( [ 'src' => $fileA ] ),
1993 "Correct file SHA-1 of $fileA" );
1996 public function testGetFileList() {
1997 $this->backend
= $this->singleBackend
;
1998 $this->tearDownFiles();
1999 $this->doTestGetFileList();
2000 $this->tearDownFiles();
2002 $this->backend
= $this->multiBackend
;
2003 $this->tearDownFiles();
2004 $this->doTestGetFileList();
2005 $this->tearDownFiles();
2008 private function doTestGetFileList() {
2009 $backendName = $this->backendClass();
2010 $base = self
::baseStorePath();
2012 // Should have no errors
2013 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont-notexists" ] );
2016 "$base/unittest-cont1/e/test1.txt",
2017 "$base/unittest-cont1/e/test2.txt",
2018 "$base/unittest-cont1/e/test3.txt",
2019 "$base/unittest-cont1/e/subdir1/test1.txt",
2020 "$base/unittest-cont1/e/subdir1/test2.txt",
2021 "$base/unittest-cont1/e/subdir2/test3.txt",
2022 "$base/unittest-cont1/e/subdir2/test4.txt",
2023 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
2024 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
2025 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
2026 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
2027 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
2028 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
2029 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
2034 foreach ( $files as $file ) {
2035 $this->prepare( [ 'dir' => dirname( $file ) ] );
2036 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
2038 $status = $this->backend
->doQuickOperations( $ops );
2039 $this->assertGoodStatus( $status,
2040 "Creation of files succeeded ($backendName)." );
2041 $this->assertEquals( true, $status->isOK(),
2042 "Creation of files succeeded with OK status ($backendName)." );
2044 // Expected listing at root
2049 "e/subdir1/test1.txt",
2050 "e/subdir1/test2.txt",
2051 "e/subdir2/test3.txt",
2052 "e/subdir2/test4.txt",
2053 "e/subdir2/subdir/test1.txt",
2054 "e/subdir2/subdir/test2.txt",
2055 "e/subdir2/subdir/test3.txt",
2056 "e/subdir2/subdir/test4.txt",
2057 "e/subdir2/subdir/test5.txt",
2058 "e/subdir2/subdir/sub/test0.txt",
2059 "e/subdir2/subdir/sub/120-px-file.txt",
2063 // Actual listing (no trailing slash) at root
2064 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1" ] );
2065 $list = $this->listToArray( $iter );
2067 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2069 // Actual listing (no trailing slash) at root with advise
2070 $iter = $this->backend
->getFileList( [
2071 'dir' => "$base/unittest-cont1",
2074 $list = $this->listToArray( $iter );
2076 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2078 // Actual listing (with trailing slash) at root
2080 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/" ] );
2081 foreach ( $iter as $file ) {
2085 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2087 // Expected listing at subdir
2095 "sub/120-px-file.txt",
2099 // Actual listing (no trailing slash) at subdir
2100 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] );
2101 $list = $this->listToArray( $iter );
2103 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2105 // Actual listing (no trailing slash) at subdir with advise
2106 $iter = $this->backend
->getFileList( [
2107 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2110 $list = $this->listToArray( $iter );
2112 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2114 // Actual listing (with trailing slash) at subdir
2116 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ] );
2117 foreach ( $iter as $file ) {
2121 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2123 // Actual listing (using iterator second time)
2124 $list = $this->listToArray( $iter );
2126 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
2128 // Actual listing (top files only) at root
2129 $iter = $this->backend
->getTopFileList( [ 'dir' => "$base/unittest-cont1" ] );
2130 $list = $this->listToArray( $iter );
2132 $this->assertEquals( [], $list, "Correct top file listing ($backendName)." );
2134 // Expected listing (top files only) at subdir
2144 // Actual listing (top files only) at subdir
2145 $iter = $this->backend
->getTopFileList(
2146 [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ]
2148 $list = $this->listToArray( $iter );
2150 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2152 // Actual listing (top files only) at subdir with advise
2153 $iter = $this->backend
->getTopFileList( [
2154 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2157 $list = $this->listToArray( $iter );
2159 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2161 foreach ( $files as $file ) { // clean up
2162 $this->backend
->doOperation( [ 'op' => 'delete', 'src' => $file ] );
2165 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
2166 foreach ( $iter as $iter ) {
2171 public function testGetDirectoryList() {
2172 $this->backend
= $this->singleBackend
;
2173 $this->tearDownFiles();
2174 $this->doTestGetDirectoryList();
2175 $this->tearDownFiles();
2177 $this->backend
= $this->multiBackend
;
2178 $this->tearDownFiles();
2179 $this->doTestGetDirectoryList();
2180 $this->tearDownFiles();
2183 private function doTestGetDirectoryList() {
2184 $backendName = $this->backendClass();
2186 $base = self
::baseStorePath();
2188 "$base/unittest-cont1/e/test1.txt",
2189 "$base/unittest-cont1/e/test2.txt",
2190 "$base/unittest-cont1/e/test3.txt",
2191 "$base/unittest-cont1/e/subdir1/test1.txt",
2192 "$base/unittest-cont1/e/subdir1/test2.txt",
2193 "$base/unittest-cont1/e/subdir2/test3.txt",
2194 "$base/unittest-cont1/e/subdir2/test4.txt",
2195 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
2196 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
2197 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
2198 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
2199 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
2200 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
2201 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
2206 foreach ( $files as $file ) {
2207 $this->prepare( [ 'dir' => dirname( $file ) ] );
2208 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
2210 $status = $this->backend
->doQuickOperations( $ops );
2211 $this->assertGoodStatus( $status,
2212 "Creation of files succeeded ($backendName)." );
2213 $this->assertEquals( true, $status->isOK(),
2214 "Creation of files succeeded with OK status ($backendName)." );
2216 $this->assertEquals( true,
2217 $this->backend
->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] ),
2218 "Directory exists in ($backendName)." );
2219 $this->assertEquals( true,
2220 $this->backend
->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] ),
2221 "Directory exists in ($backendName)." );
2222 $this->assertEquals( false,
2223 $this->backend
->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ] ),
2224 "Directory does not exists in ($backendName)." );
2232 // Actual listing (no trailing slash)
2234 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1" ] );
2235 foreach ( $iter as $file ) {
2240 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2251 // Actual listing (no trailing slash)
2253 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e" ] );
2254 foreach ( $iter as $file ) {
2259 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2261 // Actual listing (with trailing slash)
2263 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/" ] );
2264 foreach ( $iter as $file ) {
2269 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2277 // Actual listing (no trailing slash)
2279 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir2" ] );
2280 foreach ( $iter as $file ) {
2285 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2287 // Actual listing (with trailing slash)
2289 $iter = $this->backend
->getTopDirectoryList(
2290 [ 'dir' => "$base/unittest-cont1/e/subdir2/" ]
2293 foreach ( $iter as $file ) {
2298 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2300 // Actual listing (using iterator second time)
2302 foreach ( $iter as $file ) {
2307 $this->assertEquals(
2310 "Correct top dir listing ($backendName), second iteration."
2313 // Expected listing (recursive)
2323 "e/subdir4/subdir/sub",
2327 // Actual listing (recursive)
2329 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/" ] );
2330 foreach ( $iter as $file ) {
2335 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2337 // Expected listing (recursive)
2344 // Actual listing (recursive)
2346 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir4" ] );
2347 foreach ( $iter as $file ) {
2352 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2354 // Actual listing (recursive, second time)
2356 foreach ( $iter as $file ) {
2361 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2363 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] );
2364 $items = $this->listToArray( $iter );
2365 $this->assertEquals( [], $items, "Directory listing is empty." );
2367 foreach ( $files as $file ) { // clean up
2368 $this->backend
->doOperation( [ 'op' => 'delete', 'src' => $file ] );
2371 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
2372 foreach ( $iter as $file ) {
2376 $items = $this->listToArray( $iter );
2377 $this->assertEquals( [], $items, "Directory listing is empty." );
2379 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/not/exists" ] );
2380 $items = $this->listToArray( $iter );
2381 $this->assertEquals( [], $items, "Directory listing is empty." );
2384 public function testLockCalls() {
2385 $this->backend
= $this->singleBackend
;
2386 $this->doTestLockCalls();
2389 private function doTestLockCalls() {
2390 $backendName = $this->backendClass();
2391 $base = $this->backend
->getContainerStoragePath( 'test' );
2398 "$base/subdir1", // duplicate
2399 "$base/subdir1/test1.txt",
2400 "$base/subdir1/test2.txt",
2402 "$base/subdir2", // duplicate
2403 "$base/subdir2/test3.txt",
2404 "$base/subdir2/test4.txt",
2405 "$base/subdir2/subdir",
2406 "$base/subdir2/subdir/test1.txt",
2407 "$base/subdir2/subdir/test2.txt",
2408 "$base/subdir2/subdir/test3.txt",
2409 "$base/subdir2/subdir/test4.txt",
2410 "$base/subdir2/subdir/test5.txt",
2411 "$base/subdir2/subdir/sub",
2412 "$base/subdir2/subdir/sub/test0.txt",
2413 "$base/subdir2/subdir/sub/120-px-file.txt",
2416 for ( $i = 0; $i < 25; $i++
) {
2417 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2418 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2419 "Locking of files succeeded ($backendName) ($i)." );
2420 $this->assertEquals( true, $status->isOK(),
2421 "Locking of files succeeded with OK status ($backendName) ($i)." );
2423 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2424 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2425 "Locking of files succeeded ($backendName) ($i)." );
2426 $this->assertEquals( true, $status->isOK(),
2427 "Locking of files succeeded with OK status ($backendName) ($i)." );
2429 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2430 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2431 "Locking of files succeeded ($backendName) ($i)." );
2432 $this->assertEquals( true, $status->isOK(),
2433 "Locking of files succeeded with OK status ($backendName) ($i)." );
2435 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2436 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2437 "Locking of files succeeded ($backendName). ($i)" );
2438 $this->assertEquals( true, $status->isOK(),
2439 "Locking of files succeeded with OK status ($backendName) ($i)." );
2441 # # Flip the acquire/release ordering around ##
2443 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2444 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2445 "Locking of files succeeded ($backendName) ($i)." );
2446 $this->assertEquals( true, $status->isOK(),
2447 "Locking of files succeeded with OK status ($backendName) ($i)." );
2449 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2450 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2451 "Locking of files succeeded ($backendName) ($i)." );
2452 $this->assertEquals( true, $status->isOK(),
2453 "Locking of files succeeded with OK status ($backendName) ($i)." );
2455 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2456 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2457 "Locking of files succeeded ($backendName). ($i)" );
2458 $this->assertEquals( true, $status->isOK(),
2459 "Locking of files succeeded with OK status ($backendName) ($i)." );
2461 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2462 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2463 "Locking of files succeeded ($backendName) ($i)." );
2464 $this->assertEquals( true, $status->isOK(),
2465 "Locking of files succeeded with OK status ($backendName) ($i)." );
2468 $status = Status
::newGood();
2469 $sl = $this->backend
->getScopedFileLocks( $paths, LockManager
::LOCK_EX
, $status );
2470 $this->assertInstanceOf( ScopedLock
::class, $sl,
2471 "Scoped locking of files succeeded ($backendName)." );
2472 $this->assertEquals( [], $status->getErrors(),
2473 "Scoped locking of files succeeded ($backendName)." );
2474 $this->assertEquals( true, $status->isOK(),
2475 "Scoped locking of files succeeded with OK status ($backendName)." );
2477 ScopedLock
::release( $sl );
2478 $this->assertEquals( null, $sl,
2479 "Scoped unlocking of files succeeded ($backendName)." );
2480 $this->assertEquals( [], $status->getErrors(),
2481 "Scoped unlocking of files succeeded ($backendName)." );
2482 $this->assertEquals( true, $status->isOK(),
2483 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2487 * @dataProvider provider_testGetContentType
2489 public function testGetContentType( $mimeCallback, $mimeFromString ) {
2492 $be = TestingAccessWrapper
::newFromObject( new MemoryFileBackend(
2494 'name' => 'testing',
2495 'class' => MemoryFileBackend
::class,
2497 'mimeCallback' => $mimeCallback
2501 $dst = 'mwstore://testing/container/path/to/file_no_ext';
2502 $src = "$IP/tests/phpunit/data/media/srgb.jpg";
2503 $this->assertEquals( 'image/jpeg', $be->getContentType( $dst, null, $src ) );
2504 $this->assertEquals(
2505 $mimeFromString ?
'image/jpeg' : 'unknown/unknown',
2506 $be->getContentType( $dst, file_get_contents( $src ), null ) );
2508 $src = "$IP/tests/phpunit/data/media/Png-native-test.png";
2509 $this->assertEquals( 'image/png', $be->getContentType( $dst, null, $src ) );
2510 $this->assertEquals(
2511 $mimeFromString ?
'image/png' : 'unknown/unknown',
2512 $be->getContentType( $dst, file_get_contents( $src ), null ) );
2515 public static function provider_testGetContentType() {
2518 [ [ FileBackendGroup
::singleton(), 'guessMimeInternal' ], true ]
2522 public function testReadAffinity() {
2523 $be = TestingAccessWrapper
::newFromObject(
2524 new FileBackendMultiWrite( [
2525 'name' => 'localtesting',
2526 'wikiId' => wfWikiID() . mt_rand(),
2529 'name' => 'multitesting0',
2530 'class' => MemoryFileBackend
::class,
2531 'isMultiMaster' => false,
2532 'readAffinity' => true
2535 'name' => 'multitesting1',
2536 'class' => MemoryFileBackend
::class,
2537 'isMultiMaster' => true
2543 $this->assertEquals(
2545 $be->getReadIndexFromParams( [ 'latest' => 1 ] ),
2546 'Reads with "latest" flag use backend 1'
2548 $this->assertEquals(
2550 $be->getReadIndexFromParams( [ 'latest' => 0 ] ),
2551 'Reads without "latest" flag use backend 0'
2554 $p = 'container/test-cont/file.txt';
2555 $be->backends
[0]->quickCreate( [
2556 'dst' => "mwstore://multitesting0/$p", 'content' => 'cattitude' ] );
2557 $be->backends
[1]->quickCreate( [
2558 'dst' => "mwstore://multitesting1/$p", 'content' => 'princess of power' ] );
2560 $this->assertEquals(
2562 $be->getFileContents( [ 'src' => "mwstore://localtesting/$p" ] ),
2563 "Non-latest read came from backend 0"
2565 $this->assertEquals(
2566 'princess of power',
2567 $be->getFileContents( [ 'src' => "mwstore://localtesting/$p", 'latest' => 1 ] ),
2568 "Latest read came from backend1"
2572 public function testAsyncWrites() {
2573 $be = TestingAccessWrapper
::newFromObject(
2574 new FileBackendMultiWrite( [
2575 'name' => 'localtesting',
2576 'wikiId' => wfWikiID() . mt_rand(),
2579 'name' => 'multitesting0',
2580 'class' => MemoryFileBackend
::class,
2581 'isMultiMaster' => false
2584 'name' => 'multitesting1',
2585 'class' => MemoryFileBackend
::class,
2586 'isMultiMaster' => true
2589 'replication' => 'async'
2593 $this->setMwGlobals( 'wgCommandLineMode', false );
2595 $p = 'container/test-cont/file.txt';
2597 'dst' => "mwstore://localtesting/$p", 'content' => 'cattitude' ] );
2599 $this->assertEquals(
2601 $be->backends
[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
2602 "File not yet written to backend 0"
2604 $this->assertEquals(
2606 $be->backends
[1]->getFileContents( [ 'src' => "mwstore://multitesting1/$p" ] ),
2607 "File already written to backend 1"
2610 DeferredUpdates
::doUpdates();
2612 $this->assertEquals(
2614 $be->backends
[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
2615 "File now written to backend 0"
2619 public function testSanitizeOpHeaders() {
2620 $be = TestingAccessWrapper
::newFromObject( new MemoryFileBackend( [
2621 'name' => 'localtesting',
2622 'wikiId' => wfWikiID()
2627 'content-Disposition' => FileBackend
::makeContentDisposition( 'inline', 'name' ),
2628 'Content-dUration' => 25.6,
2629 'X-LONG-VALUE' => str_pad( '0', 300 ),
2630 'CONTENT-LENGTH' => 855055,
2635 'content-disposition' => FileBackend
::makeContentDisposition( 'inline', 'name' ),
2636 'content-duration' => 25.6,
2637 'content-length' => 855055
2641 Wikimedia\
suppressWarnings();
2642 $actual = $be->sanitizeOpHeaders( $input );
2643 Wikimedia\restoreWarnings
();
2645 $this->assertEquals( $expected, $actual, "Header sanitized properly" );
2649 private function listToArray( $iter ) {
2650 return is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2653 // test helper wrapper for backend prepare() function
2654 private function prepare( array $params ) {
2655 return $this->backend
->prepare( $params );
2658 // test helper wrapper for backend prepare() function
2659 private function create( array $params ) {
2660 $params['op'] = 'create';
2662 return $this->backend
->doQuickOperations( [ $params ] );
2665 function tearDownFiles() {
2666 $containers = [ 'unittest-cont1', 'unittest-cont2' ];
2667 foreach ( $containers as $container ) {
2668 $this->deleteFiles( $container );
2672 private function deleteFiles( $container ) {
2673 $base = self
::baseStorePath();
2674 $iter = $this->backend
->getFileList( [ 'dir' => "$base/$container" ] );
2676 foreach ( $iter as $file ) {
2677 $this->backend
->quickDelete( [ 'src' => "$base/$container/$file" ] );
2679 // free the directory, to avoid Permission denied under windows on rmdir
2682 $this->backend
->clean( [ 'dir' => "$base/$container", 'recursive' => 1 ] );
2685 private function assertBackendPathsConsistent( array $paths, $okSyncStatus ) {
2686 if ( !$this->backend
instanceof FileBackendMultiWrite
) {
2690 $status = $this->backend
->consistencyCheck( $paths );
2691 if ( $okSyncStatus ) {
2692 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2694 $this->assertBadStatus( $status, "Files not synced: " . implode( ',', $paths ) );
2698 private function assertGoodStatus( StatusValue
$status, $msg ) {
2699 $this->assertEquals( print_r( [], 1 ), print_r( $status->getErrors(), 1 ), $msg );
2702 private function assertBadStatus( StatusValue
$status, $msg ) {
2703 $this->assertNotEquals( print_r( [], 1 ), print_r( $status->getErrors(), 1 ), $msg );