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'] )
927 * @dataProvider provider_quickOperations
929 public function testDoQuickOperations(
930 $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, $batchSize
932 $this->backend
= $this->singleBackend
;
933 $this->doTestDoQuickOperations(
934 $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, $batchSize
936 $this->tearDownFiles();
938 $this->backend
= $this->multiBackend
;
939 $this->doTestDoQuickOperations(
940 $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, $batchSize
942 $this->tearDownFiles();
945 private function doTestDoQuickOperations(
946 $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, $batchSize
948 $backendName = $this->backendClass();
950 foreach ( $files as $path ) {
951 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
952 $this->assertGoodStatus( $status,
953 "Preparing $path succeeded without warnings ($backendName)." );
956 foreach ( array_chunk( $createOps, $batchSize ) as $batchOps ) {
957 $this->assertGoodStatus(
958 $this->backend
->doQuickOperations( $batchOps ),
959 "Creation of source files succeeded ($backendName)."
962 foreach ( $files as $file ) {
964 $this->backend
->fileExists( [ 'src' => $file ] ),
969 foreach ( array_chunk( $copyOps, $batchSize ) as $batchOps ) {
970 $this->assertGoodStatus(
971 $this->backend
->doQuickOperations( $batchOps ),
972 "Quick copy of source files succeeded ($backendName)."
975 foreach ( $files as $file ) {
977 $this->backend
->fileExists( [ 'src' => "$file-2" ] ),
978 "File $file-2 exists."
982 foreach ( array_chunk( $moveOps, $batchSize ) as $batchOps ) {
983 $this->assertGoodStatus(
984 $this->backend
->doQuickOperations( $batchOps ),
985 "Quick move of source files succeeded ($backendName)."
988 foreach ( $files as $file ) {
990 $this->backend
->fileExists( [ 'src' => "$file-3" ] ),
991 "File $file-3 move in."
994 $this->backend
->fileExists( [ 'src' => "$file-2" ] ),
995 "File $file-2 moved away."
999 foreach ( array_chunk( $overSelfOps, $batchSize ) as $batchOps ) {
1000 $this->assertGoodStatus(
1001 $this->backend
->doQuickOperations( $batchOps ),
1002 "Quick copy/move of source files over themselves succeeded ($backendName)."
1005 foreach ( $files as $file ) {
1007 $this->backend
->fileExists( [ 'src' => $file ] ),
1008 "File $file still exists after copy/move over self."
1012 foreach ( array_chunk( $deleteOps, $batchSize ) as $batchOps ) {
1013 $this->assertGoodStatus(
1014 $this->backend
->doQuickOperations( $batchOps ),
1015 "Quick deletion of source files succeeded ($backendName)."
1018 foreach ( $files as $file ) {
1019 $this->assertFalse( $this->backend
->fileExists( [ 'src' => $file ] ),
1020 "File $file purged." );
1021 $this->assertFalse( $this->backend
->fileExists( [ 'src' => "$file-3" ] ),
1022 "File $file-3 purged." );
1026 function provider_quickOperations() {
1027 $base = self
::baseStorePath();
1029 "$base/unittest-cont1/e/fileA.a",
1030 "$base/unittest-cont1/e/fileB.a",
1031 "$base/unittest-cont1/e/fileC.a"
1034 $createOps = $copyOps = $moveOps = $overSelfOps = $deleteOps = [];
1035 foreach ( $files as $path ) {
1036 $createOps[] = [ 'op' => 'create', 'dst' => $path, 'content' => 52525 ];
1037 $createOps[] = [ 'op' => 'create', 'dst' => "$path-x", 'content' => 832 ];
1038 $createOps[] = [ 'op' => 'null' ];
1040 $copyOps[] = [ 'op' => 'copy', 'src' => $path, 'dst' => "$path-2" ];
1043 'src' => "$path-nothing",
1044 'dst' => "$path-nowhere",
1045 'ignoreMissingSource' => true
1047 $copyOps[] = [ 'op' => 'null' ];
1049 $moveOps[] = [ 'op' => 'move', 'src' => "$path-2", 'dst' => "$path-3" ];
1052 'src' => "$path-nothing",
1053 'dst' => "$path-nowhere",
1054 'ignoreMissingSource' => true
1056 $moveOps[] = [ 'op' => 'null' ];
1058 $overSelfOps[] = [ 'op' => 'copy', 'src' => $path, 'dst' => $path ];
1059 $overSelfOps[] = [ 'op' => 'move', 'src' => $path, 'dst' => $path ];
1061 $deleteOps[] = [ 'op' => 'delete', 'src' => $path ];
1062 $deleteOps[] = [ 'op' => 'delete', 'src' => "$path-3" ];
1065 'src' => "$path-gone",
1066 'ignoreMissingSource' => true
1068 $deleteOps[] = [ 'op' => 'null' ];
1072 [ $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, 1 ],
1073 [ $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, 2 ],
1074 [ $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, 100 ]
1079 * @dataProvider provider_testConcatenate
1081 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
1082 $this->backend
= $this->singleBackend
;
1083 $this->tearDownFiles();
1084 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
1085 $this->tearDownFiles();
1087 $this->backend
= $this->multiBackend
;
1088 $this->tearDownFiles();
1089 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
1090 $this->tearDownFiles();
1093 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
1094 $backendName = $this->backendClass();
1099 foreach ( $srcs as $i => $source ) {
1100 $this->prepare( [ 'dir' => dirname( $source ) ] );
1102 'op' => 'create', // operation
1103 'dst' => $source, // source
1104 'content' => $srcsContent[$i]
1106 $expContent .= $srcsContent[$i];
1108 $status = $this->backend
->doOperations( $ops );
1110 $this->assertGoodStatus( $status,
1111 "Creation of source files succeeded ($backendName)." );
1113 $dest = $params['dst'] = $this->getNewTempFile();
1114 if ( $alreadyExists ) {
1115 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
1116 $this->assertEquals( true, $ok,
1117 "Creation of file at $dest succeeded ($backendName)." );
1119 $ok = file_put_contents( $dest, '' ) !== false;
1120 $this->assertEquals( true, $ok,
1121 "Creation of 0-byte file at $dest succeeded ($backendName)." );
1124 // Combine the files into one
1125 $status = $this->backend
->concatenate( $params );
1127 $this->assertGoodStatus( $status,
1128 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
1129 $this->assertEquals( true, $status->isOK(),
1130 "Creation of concat file at $dest succeeded ($backendName)." );
1132 $this->assertEquals( false, $status->isOK(),
1133 "Creation of concat file at $dest failed ($backendName)." );
1137 $this->assertEquals( true, is_file( $dest ),
1138 "Dest concat file $dest exists after creation ($backendName)." );
1140 $this->assertEquals( true, is_file( $dest ),
1141 "Dest concat file $dest exists after failed creation ($backendName)." );
1144 $contents = file_get_contents( $dest );
1145 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
1148 $this->assertEquals( $expContent, $contents,
1149 "Concat file at $dest has correct contents ($backendName)." );
1151 $this->assertNotEquals( $expContent, $contents,
1152 "Concat file at $dest has correct contents ($backendName)." );
1156 public static function provider_testConcatenate() {
1160 self
::baseStorePath() . '/unittest-cont1/e/file1.txt',
1161 self
::baseStorePath() . '/unittest-cont1/e/file2.txt',
1162 self
::baseStorePath() . '/unittest-cont1/e/file3.txt',
1163 self
::baseStorePath() . '/unittest-cont1/e/file4.txt',
1164 self
::baseStorePath() . '/unittest-cont1/e/file5.txt',
1165 self
::baseStorePath() . '/unittest-cont1/e/file6.txt',
1166 self
::baseStorePath() . '/unittest-cont1/e/file7.txt',
1167 self
::baseStorePath() . '/unittest-cont1/e/file8.txt',
1168 self
::baseStorePath() . '/unittest-cont1/e/file9.txt',
1169 self
::baseStorePath() . '/unittest-cont1/e/file10.txt'
1183 $params = [ 'srcs' => $srcs ];
1186 $params, // operation
1188 $content, // content for each source
1189 false, // no dest already exists
1194 $params, // operation
1196 $content, // content for each source
1197 true, // dest already exists
1205 * @dataProvider provider_testGetFileStat
1207 public function testGetFileStat( $path, $content, $alreadyExists ) {
1208 $this->backend
= $this->singleBackend
;
1209 $this->tearDownFiles();
1210 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1211 $this->tearDownFiles();
1213 $this->backend
= $this->multiBackend
;
1214 $this->tearDownFiles();
1215 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1216 $this->tearDownFiles();
1219 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
1220 $backendName = $this->backendClass();
1222 if ( $alreadyExists ) {
1223 $this->prepare( [ 'dir' => dirname( $path ) ] );
1224 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1225 $this->assertGoodStatus( $status,
1226 "Creation of file at $path succeeded ($backendName)." );
1228 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1229 $time = $this->backend
->getFileTimestamp( [ 'src' => $path ] );
1230 $stat = $this->backend
->getFileStat( [ 'src' => $path ] );
1232 $this->assertEquals( strlen( $content ), $size,
1233 "Correct file size of '$path'" );
1234 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1235 "Correct file timestamp of '$path'" );
1237 $size = $stat['size'];
1238 $time = $stat['mtime'];
1239 $this->assertEquals( strlen( $content ), $size,
1240 "Correct file size of '$path'" );
1241 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1242 "Correct file timestamp of '$path'" );
1244 $this->backend
->clearCache( [ $path ] );
1246 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1248 $this->assertEquals( strlen( $content ), $size,
1249 "Correct file size of '$path'" );
1251 $this->backend
->preloadCache( [ $path ] );
1253 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1255 $this->assertEquals( strlen( $content ), $size,
1256 "Correct file size of '$path'" );
1258 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1259 $time = $this->backend
->getFileTimestamp( [ 'src' => $path ] );
1260 $stat = $this->backend
->getFileStat( [ 'src' => $path ] );
1262 $this->assertFalse( $size, "Correct file size of '$path'" );
1263 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1264 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1268 public static function provider_testGetFileStat() {
1271 $base = self
::baseStorePath();
1272 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true ];
1273 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "", true ];
1274 $cases[] = [ "$base/unittest-cont1/e/b/some-diff_file.txt", null, false ];
1280 * @dataProvider provider_testGetFileStat
1282 public function testStreamFile( $path, $content, $alreadyExists ) {
1283 $this->backend
= $this->singleBackend
;
1284 $this->tearDownFiles();
1285 $this->doTestStreamFile( $path, $content, $alreadyExists );
1286 $this->tearDownFiles();
1288 $this->backend
= $this->multiBackend
;
1289 $this->tearDownFiles();
1290 $this->doTestStreamFile( $path, $content, $alreadyExists );
1291 $this->tearDownFiles();
1294 private function doTestStreamFile( $path, $content ) {
1295 $backendName = $this->backendClass();
1297 if ( $content !== null ) {
1298 $this->prepare( [ 'dir' => dirname( $path ) ] );
1299 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1300 $this->assertGoodStatus( $status,
1301 "Creation of file at $path succeeded ($backendName)." );
1304 $this->backend
->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
1305 $data = ob_get_contents();
1308 $this->assertEquals( $content, $data, "Correct content streamed from '$path'" );
1309 } else { // 404 case
1311 $this->backend
->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
1312 $data = ob_get_contents();
1315 $this->assertRegExp( '#<h1>File not found</h1>#', $data,
1316 "Correct content streamed from '$path' ($backendName)" );
1320 public static function provider_testStreamFile() {
1323 $base = self
::baseStorePath();
1324 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
1325 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", null ];
1330 public function testStreamFileRange() {
1331 $this->backend
= $this->singleBackend
;
1332 $this->tearDownFiles();
1333 $this->doTestStreamFileRange();
1334 $this->tearDownFiles();
1336 $this->backend
= $this->multiBackend
;
1337 $this->tearDownFiles();
1338 $this->doTestStreamFileRange();
1339 $this->tearDownFiles();
1342 private function doTestStreamFileRange() {
1343 $backendName = $this->backendClass();
1345 $base = self
::baseStorePath();
1346 $path = "$base/unittest-cont1/e/b/z/range_file.txt";
1347 $content = "0123456789ABCDEF";
1349 $this->prepare( [ 'dir' => dirname( $path ) ] );
1350 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1351 $this->assertGoodStatus( $status,
1352 "Creation of file at $path succeeded ($backendName)." );
1356 'bytes=0-3' => '0123',
1357 'bytes=4-8' => '45678',
1358 'bytes=15-15' => 'F',
1359 'bytes=14-15' => 'EF',
1360 'bytes=-5' => 'BCDEF',
1362 'bytes=10-16' => 'ABCDEF',
1363 'bytes=10-99' => 'ABCDEF',
1366 foreach ( $ranges as $range => $chunk ) {
1368 $this->backend
->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1,
1369 'options' => [ 'range' => $range ] ] );
1370 $data = ob_get_contents();
1373 $this->assertEquals( $chunk, $data, "Correct chunk streamed from '$path' for '$range'" );
1378 * @dataProvider provider_testGetFileContents
1380 public function testGetFileContents( $source, $content ) {
1381 $this->backend
= $this->singleBackend
;
1382 $this->tearDownFiles();
1383 $this->doTestGetFileContents( $source, $content );
1384 $this->tearDownFiles();
1386 $this->backend
= $this->multiBackend
;
1387 $this->tearDownFiles();
1388 $this->doTestGetFileContents( $source, $content );
1389 $this->tearDownFiles();
1392 private function doTestGetFileContents( $source, $content ) {
1393 $backendName = $this->backendClass();
1395 $srcs = (array)$source;
1396 $content = (array)$content;
1397 foreach ( $srcs as $i => $src ) {
1398 $this->prepare( [ 'dir' => dirname( $src ) ] );
1399 $status = $this->backend
->doOperation(
1400 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1401 $this->assertGoodStatus( $status,
1402 "Creation of file at $src succeeded ($backendName)." );
1405 if ( is_array( $source ) ) {
1406 $contents = $this->backend
->getFileContentsMulti( [ 'srcs' => $source ] );
1407 foreach ( $contents as $path => $data ) {
1408 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1409 $this->assertEquals(
1410 current( $content ),
1412 "Contents of $path is correct ($backendName)."
1416 $this->assertEquals(
1418 array_keys( $contents ),
1419 "Contents in right order ($backendName)."
1421 $this->assertEquals(
1424 "Contents array size correct ($backendName)."
1427 $data = $this->backend
->getFileContents( [ 'src' => $source ] );
1428 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1429 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1433 public static function provider_testGetFileContents() {
1436 $base = self
::baseStorePath();
1437 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
1438 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" ];
1440 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1441 "$base/unittest-cont1/e/a/z.txt" ],
1442 [ "contents xx", "contents xy", "contents xz" ]
1449 * @dataProvider provider_testGetLocalCopy
1451 public function testGetLocalCopy( $source, $content ) {
1452 $this->backend
= $this->singleBackend
;
1453 $this->tearDownFiles();
1454 $this->doTestGetLocalCopy( $source, $content );
1455 $this->tearDownFiles();
1457 $this->backend
= $this->multiBackend
;
1458 $this->tearDownFiles();
1459 $this->doTestGetLocalCopy( $source, $content );
1460 $this->tearDownFiles();
1463 private function doTestGetLocalCopy( $source, $content ) {
1464 $backendName = $this->backendClass();
1466 $srcs = (array)$source;
1467 $content = (array)$content;
1468 foreach ( $srcs as $i => $src ) {
1469 $this->prepare( [ 'dir' => dirname( $src ) ] );
1470 $status = $this->backend
->doOperation(
1471 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1472 $this->assertGoodStatus( $status,
1473 "Creation of file at $src succeeded ($backendName)." );
1476 if ( is_array( $source ) ) {
1477 $tmpFiles = $this->backend
->getLocalCopyMulti( [ 'srcs' => $source ] );
1478 foreach ( $tmpFiles as $path => $tmpFile ) {
1479 $this->assertNotNull( $tmpFile,
1480 "Creation of local copy of $path succeeded ($backendName)." );
1481 $contents = file_get_contents( $tmpFile->getPath() );
1482 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1483 $this->assertEquals(
1484 current( $content ),
1486 "Local copy of $path is correct ($backendName)."
1490 $this->assertEquals(
1492 array_keys( $tmpFiles ),
1493 "Local copies in right order ($backendName)."
1495 $this->assertEquals(
1498 "Local copies array size correct ($backendName)."
1501 $tmpFile = $this->backend
->getLocalCopy( [ 'src' => $source ] );
1502 $this->assertNotNull( $tmpFile,
1503 "Creation of local copy of $source succeeded ($backendName)." );
1504 $contents = file_get_contents( $tmpFile->getPath() );
1505 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1506 $this->assertEquals(
1509 "Local copy of $source is correct ($backendName)."
1513 $obj = new stdClass();
1514 $tmpFile->bind( $obj );
1517 public static function provider_testGetLocalCopy() {
1520 $base = self
::baseStorePath();
1521 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1522 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1523 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1525 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1526 "$base/unittest-cont1/e/a/z.txt" ],
1527 [ "contents xx $", "contents xy 111", "contents xz" ]
1534 * @dataProvider provider_testGetLocalReference
1536 public function testGetLocalReference( $source, $content ) {
1537 $this->backend
= $this->singleBackend
;
1538 $this->tearDownFiles();
1539 $this->doTestGetLocalReference( $source, $content );
1540 $this->tearDownFiles();
1542 $this->backend
= $this->multiBackend
;
1543 $this->tearDownFiles();
1544 $this->doTestGetLocalReference( $source, $content );
1545 $this->tearDownFiles();
1548 private function doTestGetLocalReference( $source, $content ) {
1549 $backendName = $this->backendClass();
1551 $srcs = (array)$source;
1552 $content = (array)$content;
1553 foreach ( $srcs as $i => $src ) {
1554 $this->prepare( [ 'dir' => dirname( $src ) ] );
1555 $status = $this->backend
->doOperation(
1556 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1557 $this->assertGoodStatus( $status,
1558 "Creation of file at $src succeeded ($backendName)." );
1561 if ( is_array( $source ) ) {
1562 $tmpFiles = $this->backend
->getLocalReferenceMulti( [ 'srcs' => $source ] );
1563 foreach ( $tmpFiles as $path => $tmpFile ) {
1564 $this->assertNotNull( $tmpFile,
1565 "Creation of local copy of $path succeeded ($backendName)." );
1566 $contents = file_get_contents( $tmpFile->getPath() );
1567 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1568 $this->assertEquals(
1569 current( $content ),
1571 "Local ref of $path is correct ($backendName)."
1575 $this->assertEquals(
1577 array_keys( $tmpFiles ),
1578 "Local refs in right order ($backendName)."
1580 $this->assertEquals(
1583 "Local refs array size correct ($backendName)."
1586 $tmpFile = $this->backend
->getLocalReference( [ 'src' => $source ] );
1587 $this->assertNotNull( $tmpFile,
1588 "Creation of local copy of $source succeeded ($backendName)." );
1589 $contents = file_get_contents( $tmpFile->getPath() );
1590 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1591 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1595 public static function provider_testGetLocalReference() {
1598 $base = self
::baseStorePath();
1599 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1600 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1601 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1603 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1604 "$base/unittest-cont1/e/a/z.txt" ],
1605 [ "contents xx 1111", "contents xy %", "contents xz $" ]
1611 public function testGetLocalCopyAndReference404() {
1612 $this->backend
= $this->singleBackend
;
1613 $this->tearDownFiles();
1614 $this->doTestGetLocalCopyAndReference404();
1615 $this->tearDownFiles();
1617 $this->backend
= $this->multiBackend
;
1618 $this->tearDownFiles();
1619 $this->doTestGetLocalCopyAndReference404();
1620 $this->tearDownFiles();
1623 public function doTestGetLocalCopyAndReference404() {
1624 $backendName = $this->backendClass();
1626 $base = self
::baseStorePath();
1628 $tmpFile = $this->backend
->getLocalCopy( [
1629 'src' => "$base/unittest-cont1/not-there" ] );
1630 $this->assertNull( $tmpFile, "Local copy of not existing file is null ($backendName)." );
1632 $tmpFile = $this->backend
->getLocalReference( [
1633 'src' => "$base/unittest-cont1/not-there" ] );
1634 $this->assertNull( $tmpFile, "Local ref of not existing file is null ($backendName)." );
1638 * @dataProvider provider_testGetFileHttpUrl
1640 public function testGetFileHttpUrl( $source, $content ) {
1641 $this->backend
= $this->singleBackend
;
1642 $this->tearDownFiles();
1643 $this->doTestGetFileHttpUrl( $source, $content );
1644 $this->tearDownFiles();
1646 $this->backend
= $this->multiBackend
;
1647 $this->tearDownFiles();
1648 $this->doTestGetFileHttpUrl( $source, $content );
1649 $this->tearDownFiles();
1652 private function doTestGetFileHttpUrl( $source, $content ) {
1653 $backendName = $this->backendClass();
1655 $this->prepare( [ 'dir' => dirname( $source ) ] );
1656 $status = $this->backend
->doOperation(
1657 [ 'op' => 'create', 'content' => $content, 'dst' => $source ] );
1658 $this->assertGoodStatus( $status,
1659 "Creation of file at $source succeeded ($backendName)." );
1661 $url = $this->backend
->getFileHttpUrl( [ 'src' => $source ] );
1663 if ( $url !== null ) { // supported
1664 $data = MediaWikiServices
::getInstance()->getHttpRequestFactory()->
1665 get( $url, [], __METHOD__
);
1666 $this->assertEquals( $content, $data,
1667 "HTTP GET of URL has right contents ($backendName)." );
1671 public static function provider_testGetFileHttpUrl() {
1674 $base = self
::baseStorePath();
1675 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1676 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1677 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1683 * @dataProvider provider_testPrepareAndClean
1685 public function testPrepareAndClean( $path, $isOK ) {
1686 $this->backend
= $this->singleBackend
;
1687 $this->doTestPrepareAndClean( $path, $isOK );
1688 $this->tearDownFiles();
1690 $this->backend
= $this->multiBackend
;
1691 $this->doTestPrepareAndClean( $path, $isOK );
1692 $this->tearDownFiles();
1695 public static function provider_testPrepareAndClean() {
1696 $base = self
::baseStorePath();
1699 [ "$base/unittest-cont1/e/a/z/some_file1.txt", true ],
1700 [ "$base/unittest-cont2/a/z/some_file2.txt", true ],
1701 # Specific to FS backend with no basePath field set
1702 # [ "$base/unittest-cont3/a/z/some_file3.txt", false ],
1706 private function doTestPrepareAndClean( $path, $isOK ) {
1707 $backendName = $this->backendClass();
1709 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
1711 $this->assertGoodStatus( $status,
1712 "Preparing dir $path succeeded without warnings ($backendName)." );
1713 $this->assertEquals( true, $status->isOK(),
1714 "Preparing dir $path succeeded ($backendName)." );
1716 $this->assertEquals( false, $status->isOK(),
1717 "Preparing dir $path failed ($backendName)." );
1720 $status = $this->backend
->secure( [ 'dir' => dirname( $path ) ] );
1722 $this->assertGoodStatus( $status,
1723 "Securing dir $path succeeded without warnings ($backendName)." );
1724 $this->assertEquals( true, $status->isOK(),
1725 "Securing dir $path succeeded ($backendName)." );
1727 $this->assertEquals( false, $status->isOK(),
1728 "Securing dir $path failed ($backendName)." );
1731 $status = $this->backend
->publish( [ 'dir' => dirname( $path ) ] );
1733 $this->assertGoodStatus( $status,
1734 "Publishing dir $path succeeded without warnings ($backendName)." );
1735 $this->assertEquals( true, $status->isOK(),
1736 "Publishing dir $path succeeded ($backendName)." );
1738 $this->assertEquals( false, $status->isOK(),
1739 "Publishing dir $path failed ($backendName)." );
1742 $status = $this->backend
->clean( [ 'dir' => dirname( $path ) ] );
1744 $this->assertGoodStatus( $status,
1745 "Cleaning dir $path succeeded without warnings ($backendName)." );
1746 $this->assertEquals( true, $status->isOK(),
1747 "Cleaning dir $path succeeded ($backendName)." );
1749 $this->assertEquals( false, $status->isOK(),
1750 "Cleaning dir $path failed ($backendName)." );
1754 public function testRecursiveClean() {
1755 $this->backend
= $this->singleBackend
;
1756 $this->doTestRecursiveClean();
1757 $this->tearDownFiles();
1759 $this->backend
= $this->multiBackend
;
1760 $this->doTestRecursiveClean();
1761 $this->tearDownFiles();
1764 private function doTestRecursiveClean() {
1765 $backendName = $this->backendClass();
1767 $base = self
::baseStorePath();
1769 "$base/unittest-cont1",
1770 "$base/unittest-cont1/e",
1771 "$base/unittest-cont1/e/a",
1772 "$base/unittest-cont1/e/a/b",
1773 "$base/unittest-cont1/e/a/b/c",
1774 "$base/unittest-cont1/e/a/b/c/d0",
1775 "$base/unittest-cont1/e/a/b/c/d1",
1776 "$base/unittest-cont1/e/a/b/c/d2",
1777 "$base/unittest-cont1/e/a/b/c/d0/1",
1778 "$base/unittest-cont1/e/a/b/c/d0/2",
1779 "$base/unittest-cont1/e/a/b/c/d1/3",
1780 "$base/unittest-cont1/e/a/b/c/d1/4",
1781 "$base/unittest-cont1/e/a/b/c/d2/5",
1782 "$base/unittest-cont1/e/a/b/c/d2/6"
1784 foreach ( $dirs as $dir ) {
1785 $status = $this->prepare( [ 'dir' => $dir ] );
1786 $this->assertGoodStatus( $status,
1787 "Preparing dir $dir succeeded without warnings ($backendName)." );
1790 if ( $this->backend
instanceof FSFileBackend
) {
1791 foreach ( $dirs as $dir ) {
1792 $this->assertEquals( true, $this->backend
->directoryExists( [ 'dir' => $dir ] ),
1793 "Dir $dir exists ($backendName)." );
1797 $status = $this->backend
->clean(
1798 [ 'dir' => "$base/unittest-cont1", 'recursive' => 1 ] );
1799 $this->assertGoodStatus( $status,
1800 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1802 foreach ( $dirs as $dir ) {
1803 $this->assertEquals( false, $this->backend
->directoryExists( [ 'dir' => $dir ] ),
1804 "Dir $dir no longer exists ($backendName)." );
1808 public function testDoOperations() {
1809 $this->backend
= $this->singleBackend
;
1810 $this->tearDownFiles();
1811 $this->doTestDoOperations();
1812 $this->tearDownFiles();
1814 $this->backend
= $this->multiBackend
;
1815 $this->tearDownFiles();
1816 $this->doTestDoOperations();
1817 $this->tearDownFiles();
1820 private function doTestDoOperations() {
1821 $base = self
::baseStorePath();
1823 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1824 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1825 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1826 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1827 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1828 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1829 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1831 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1832 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1833 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1834 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
1835 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1836 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
1837 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1839 $status = $this->backend
->doOperations( [
1840 [ 'op' => 'describe', 'src' => $fileA,
1841 'headers' => [ 'X-Content-Length' => '91.3' ], 'disposition' => 'inline' ],
1842 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1843 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1844 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1845 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1846 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1847 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1848 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1849 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1850 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1851 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1852 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1853 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1854 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1855 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1856 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1857 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1858 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1860 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1862 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1864 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1870 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1871 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1872 $this->assertEquals( 14, count( $status->success
),
1873 "Operation batch has correct success array" );
1875 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileA ] ),
1876 "File does not exist at $fileA" );
1877 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileB ] ),
1878 "File does not exist at $fileB" );
1879 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileD ] ),
1880 "File does not exist at $fileD" );
1882 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileC ] ),
1883 "File exists at $fileC" );
1884 $this->assertEquals( $fileBContents,
1885 $this->backend
->getFileContents( [ 'src' => $fileC ] ),
1886 "Correct file contents of $fileC" );
1887 $this->assertEquals( strlen( $fileBContents ),
1888 $this->backend
->getFileSize( [ 'src' => $fileC ] ),
1889 "Correct file size of $fileC" );
1890 $this->assertEquals( Wikimedia\base_convert
( sha1( $fileBContents ), 16, 36, 31 ),
1891 $this->backend
->getFileSha1Base36( [ 'src' => $fileC ] ),
1892 "Correct file SHA-1 of $fileC" );
1895 public function testDoOperationsPipeline() {
1896 $this->backend
= $this->singleBackend
;
1897 $this->tearDownFiles();
1898 $this->doTestDoOperationsPipeline();
1899 $this->tearDownFiles();
1901 $this->backend
= $this->multiBackend
;
1902 $this->tearDownFiles();
1903 $this->doTestDoOperationsPipeline();
1904 $this->tearDownFiles();
1907 // concurrency orientated
1908 private function doTestDoOperationsPipeline() {
1909 $base = self
::baseStorePath();
1911 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1912 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1913 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1915 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1916 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1917 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1918 $this->addTmpFiles( [ $tmpNameA, $tmpNameB, $tmpNameC ] );
1919 file_put_contents( $tmpNameA, $fileAContents );
1920 file_put_contents( $tmpNameB, $fileBContents );
1921 file_put_contents( $tmpNameC, $fileCContents );
1923 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1924 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1925 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1926 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1928 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1929 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1930 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1931 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1932 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1934 $status = $this->backend
->doOperations( [
1935 [ 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ],
1936 [ 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ],
1937 [ 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ],
1938 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1939 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1940 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1941 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1942 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1943 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1944 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1945 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1946 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1947 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1948 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1949 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1950 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1951 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1952 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1953 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1954 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1956 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1958 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1960 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1966 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1967 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1968 $this->assertEquals( 16, count( $status->success
),
1969 "Operation batch has correct success array" );
1971 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileA ] ),
1972 "File does not exist at $fileA" );
1973 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileB ] ),
1974 "File does not exist at $fileB" );
1975 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileD ] ),
1976 "File does not exist at $fileD" );
1978 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileC ] ),
1979 "File exists at $fileC" );
1980 $this->assertEquals( $fileBContents,
1981 $this->backend
->getFileContents( [ 'src' => $fileC ] ),
1982 "Correct file contents of $fileC" );
1983 $this->assertEquals( strlen( $fileBContents ),
1984 $this->backend
->getFileSize( [ 'src' => $fileC ] ),
1985 "Correct file size of $fileC" );
1986 $this->assertEquals( Wikimedia\base_convert
( sha1( $fileBContents ), 16, 36, 31 ),
1987 $this->backend
->getFileSha1Base36( [ 'src' => $fileC ] ),
1988 "Correct file SHA-1 of $fileC" );
1991 public function testDoOperationsFailing() {
1992 $this->backend
= $this->singleBackend
;
1993 $this->tearDownFiles();
1994 $this->doTestDoOperationsFailing();
1995 $this->tearDownFiles();
1997 $this->backend
= $this->multiBackend
;
1998 $this->tearDownFiles();
1999 $this->doTestDoOperationsFailing();
2000 $this->tearDownFiles();
2003 private function doTestDoOperationsFailing() {
2004 $base = self
::baseStorePath();
2006 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
2007 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
2008 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
2009 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
2010 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
2011 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
2012 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
2014 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
2015 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
2016 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
2017 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
2018 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
2019 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
2021 $status = $this->backend
->doOperations( [
2022 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
2023 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
2024 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
2025 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
2026 [ 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ],
2027 // Now: A:<A>, B:<B>, C:<A>, D:<B>
2028 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ],
2029 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
2030 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ],
2031 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
2032 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ],
2033 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
2034 [ 'op' => 'delete', 'src' => $fileD ],
2035 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
2038 ], [ 'force' => 1 ] );
2040 $this->assertNotEquals( [], $status->getErrors(), "Operation had warnings" );
2041 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
2042 $this->assertEquals( 8, count( $status->success
),
2043 "Operation batch has correct success array" );
2045 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileB ] ),
2046 "File does not exist at $fileB" );
2047 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileD ] ),
2048 "File does not exist at $fileD" );
2050 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileA ] ),
2051 "File does not exist at $fileA" );
2052 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileC ] ),
2053 "File exists at $fileC" );
2054 $this->assertEquals( $fileBContents,
2055 $this->backend
->getFileContents( [ 'src' => $fileA ] ),
2056 "Correct file contents of $fileA" );
2057 $this->assertEquals( strlen( $fileBContents ),
2058 $this->backend
->getFileSize( [ 'src' => $fileA ] ),
2059 "Correct file size of $fileA" );
2060 $this->assertEquals( Wikimedia\base_convert
( sha1( $fileBContents ), 16, 36, 31 ),
2061 $this->backend
->getFileSha1Base36( [ 'src' => $fileA ] ),
2062 "Correct file SHA-1 of $fileA" );
2065 public function testGetFileList() {
2066 $this->backend
= $this->singleBackend
;
2067 $this->tearDownFiles();
2068 $this->doTestGetFileList();
2069 $this->tearDownFiles();
2071 $this->backend
= $this->multiBackend
;
2072 $this->tearDownFiles();
2073 $this->doTestGetFileList();
2074 $this->tearDownFiles();
2077 private function doTestGetFileList() {
2078 $backendName = $this->backendClass();
2079 $base = self
::baseStorePath();
2081 // Should have no errors
2082 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont-notexists" ] );
2085 "$base/unittest-cont1/e/test1.txt",
2086 "$base/unittest-cont1/e/test2.txt",
2087 "$base/unittest-cont1/e/test3.txt",
2088 "$base/unittest-cont1/e/subdir1/test1.txt",
2089 "$base/unittest-cont1/e/subdir1/test2.txt",
2090 "$base/unittest-cont1/e/subdir2/test3.txt",
2091 "$base/unittest-cont1/e/subdir2/test4.txt",
2092 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
2093 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
2094 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
2095 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
2096 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
2097 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
2098 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
2103 foreach ( $files as $file ) {
2104 $this->prepare( [ 'dir' => dirname( $file ) ] );
2105 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
2107 $status = $this->backend
->doQuickOperations( $ops );
2108 $this->assertGoodStatus( $status,
2109 "Creation of files succeeded ($backendName)." );
2110 $this->assertEquals( true, $status->isOK(),
2111 "Creation of files succeeded with OK status ($backendName)." );
2113 // Expected listing at root
2118 "e/subdir1/test1.txt",
2119 "e/subdir1/test2.txt",
2120 "e/subdir2/test3.txt",
2121 "e/subdir2/test4.txt",
2122 "e/subdir2/subdir/test1.txt",
2123 "e/subdir2/subdir/test2.txt",
2124 "e/subdir2/subdir/test3.txt",
2125 "e/subdir2/subdir/test4.txt",
2126 "e/subdir2/subdir/test5.txt",
2127 "e/subdir2/subdir/sub/test0.txt",
2128 "e/subdir2/subdir/sub/120-px-file.txt",
2132 // Actual listing (no trailing slash) at root
2133 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1" ] );
2134 $list = $this->listToArray( $iter );
2136 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2138 // Actual listing (no trailing slash) at root with advise
2139 $iter = $this->backend
->getFileList( [
2140 'dir' => "$base/unittest-cont1",
2143 $list = $this->listToArray( $iter );
2145 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2147 // Actual listing (with trailing slash) at root
2149 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/" ] );
2150 foreach ( $iter as $file ) {
2154 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2156 // Expected listing at subdir
2164 "sub/120-px-file.txt",
2168 // Actual listing (no trailing slash) at subdir
2169 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] );
2170 $list = $this->listToArray( $iter );
2172 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2174 // Actual listing (no trailing slash) at subdir with advise
2175 $iter = $this->backend
->getFileList( [
2176 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2179 $list = $this->listToArray( $iter );
2181 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2183 // Actual listing (with trailing slash) at subdir
2185 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ] );
2186 foreach ( $iter as $file ) {
2190 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2192 // Actual listing (using iterator second time)
2193 $list = $this->listToArray( $iter );
2195 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
2197 // Actual listing (top files only) at root
2198 $iter = $this->backend
->getTopFileList( [ 'dir' => "$base/unittest-cont1" ] );
2199 $list = $this->listToArray( $iter );
2201 $this->assertEquals( [], $list, "Correct top file listing ($backendName)." );
2203 // Expected listing (top files only) at subdir
2213 // Actual listing (top files only) at subdir
2214 $iter = $this->backend
->getTopFileList(
2215 [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ]
2217 $list = $this->listToArray( $iter );
2219 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2221 // Actual listing (top files only) at subdir with advise
2222 $iter = $this->backend
->getTopFileList( [
2223 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2226 $list = $this->listToArray( $iter );
2228 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2230 foreach ( $files as $file ) { // clean up
2231 $this->backend
->doOperation( [ 'op' => 'delete', 'src' => $file ] );
2234 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
2235 foreach ( $iter as $iter ) {
2240 public function testGetDirectoryList() {
2241 $this->backend
= $this->singleBackend
;
2242 $this->tearDownFiles();
2243 $this->doTestGetDirectoryList();
2244 $this->tearDownFiles();
2246 $this->backend
= $this->multiBackend
;
2247 $this->tearDownFiles();
2248 $this->doTestGetDirectoryList();
2249 $this->tearDownFiles();
2252 private function doTestGetDirectoryList() {
2253 $backendName = $this->backendClass();
2255 $base = self
::baseStorePath();
2257 "$base/unittest-cont1/e/test1.txt",
2258 "$base/unittest-cont1/e/test2.txt",
2259 "$base/unittest-cont1/e/test3.txt",
2260 "$base/unittest-cont1/e/subdir1/test1.txt",
2261 "$base/unittest-cont1/e/subdir1/test2.txt",
2262 "$base/unittest-cont1/e/subdir2/test3.txt",
2263 "$base/unittest-cont1/e/subdir2/test4.txt",
2264 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
2265 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
2266 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
2267 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
2268 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
2269 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
2270 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
2275 foreach ( $files as $file ) {
2276 $this->prepare( [ 'dir' => dirname( $file ) ] );
2277 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
2279 $status = $this->backend
->doQuickOperations( $ops );
2280 $this->assertGoodStatus( $status,
2281 "Creation of files succeeded ($backendName)." );
2282 $this->assertEquals( true, $status->isOK(),
2283 "Creation of files succeeded with OK status ($backendName)." );
2285 $this->assertEquals( true,
2286 $this->backend
->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] ),
2287 "Directory exists in ($backendName)." );
2288 $this->assertEquals( true,
2289 $this->backend
->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] ),
2290 "Directory exists in ($backendName)." );
2291 $this->assertEquals( false,
2292 $this->backend
->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ] ),
2293 "Directory does not exists in ($backendName)." );
2301 // Actual listing (no trailing slash)
2303 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1" ] );
2304 foreach ( $iter as $file ) {
2309 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2320 // Actual listing (no trailing slash)
2322 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e" ] );
2323 foreach ( $iter as $file ) {
2328 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2330 // Actual listing (with trailing slash)
2332 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/" ] );
2333 foreach ( $iter as $file ) {
2338 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2346 // Actual listing (no trailing slash)
2348 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir2" ] );
2349 foreach ( $iter as $file ) {
2354 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2356 // Actual listing (with trailing slash)
2358 $iter = $this->backend
->getTopDirectoryList(
2359 [ 'dir' => "$base/unittest-cont1/e/subdir2/" ]
2362 foreach ( $iter as $file ) {
2367 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2369 // Actual listing (using iterator second time)
2371 foreach ( $iter as $file ) {
2376 $this->assertEquals(
2379 "Correct top dir listing ($backendName), second iteration."
2382 // Expected listing (recursive)
2392 "e/subdir4/subdir/sub",
2396 // Actual listing (recursive)
2398 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/" ] );
2399 foreach ( $iter as $file ) {
2404 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2406 // Expected listing (recursive)
2413 // Actual listing (recursive)
2415 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir4" ] );
2416 foreach ( $iter as $file ) {
2421 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2423 // Actual listing (recursive, second time)
2425 foreach ( $iter as $file ) {
2430 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2432 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] );
2433 $items = $this->listToArray( $iter );
2434 $this->assertEquals( [], $items, "Directory listing is empty." );
2436 foreach ( $files as $file ) { // clean up
2437 $this->backend
->doOperation( [ 'op' => 'delete', 'src' => $file ] );
2440 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
2441 foreach ( $iter as $file ) {
2445 $items = $this->listToArray( $iter );
2446 $this->assertEquals( [], $items, "Directory listing is empty." );
2448 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/not/exists" ] );
2449 $items = $this->listToArray( $iter );
2450 $this->assertEquals( [], $items, "Directory listing is empty." );
2453 public function testLockCalls() {
2454 $this->backend
= $this->singleBackend
;
2455 $this->doTestLockCalls();
2458 private function doTestLockCalls() {
2459 $backendName = $this->backendClass();
2460 $base = $this->backend
->getContainerStoragePath( 'test' );
2467 "$base/subdir1", // duplicate
2468 "$base/subdir1/test1.txt",
2469 "$base/subdir1/test2.txt",
2471 "$base/subdir2", // duplicate
2472 "$base/subdir2/test3.txt",
2473 "$base/subdir2/test4.txt",
2474 "$base/subdir2/subdir",
2475 "$base/subdir2/subdir/test1.txt",
2476 "$base/subdir2/subdir/test2.txt",
2477 "$base/subdir2/subdir/test3.txt",
2478 "$base/subdir2/subdir/test4.txt",
2479 "$base/subdir2/subdir/test5.txt",
2480 "$base/subdir2/subdir/sub",
2481 "$base/subdir2/subdir/sub/test0.txt",
2482 "$base/subdir2/subdir/sub/120-px-file.txt",
2485 for ( $i = 0; $i < 2; $i++
) {
2486 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2487 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2488 "Locking of files succeeded ($backendName) ($i)." );
2489 $this->assertEquals( true, $status->isOK(),
2490 "Locking of files succeeded with OK status ($backendName) ($i)." );
2492 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2493 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2494 "Locking of files succeeded ($backendName) ($i)." );
2495 $this->assertEquals( true, $status->isOK(),
2496 "Locking of files succeeded with OK status ($backendName) ($i)." );
2498 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2499 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2500 "Locking of files succeeded ($backendName) ($i)." );
2501 $this->assertEquals( true, $status->isOK(),
2502 "Locking of files succeeded with OK status ($backendName) ($i)." );
2504 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2505 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2506 "Locking of files succeeded ($backendName). ($i)" );
2507 $this->assertEquals( true, $status->isOK(),
2508 "Locking of files succeeded with OK status ($backendName) ($i)." );
2510 # # Flip the acquire/release ordering around ##
2512 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2513 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2514 "Locking of files succeeded ($backendName) ($i)." );
2515 $this->assertEquals( true, $status->isOK(),
2516 "Locking of files succeeded with OK status ($backendName) ($i)." );
2518 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2519 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2520 "Locking of files succeeded ($backendName) ($i)." );
2521 $this->assertEquals( true, $status->isOK(),
2522 "Locking of files succeeded with OK status ($backendName) ($i)." );
2524 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2525 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2526 "Locking of files succeeded ($backendName). ($i)" );
2527 $this->assertEquals( true, $status->isOK(),
2528 "Locking of files succeeded with OK status ($backendName) ($i)." );
2530 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2531 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2532 "Locking of files succeeded ($backendName) ($i)." );
2533 $this->assertEquals( true, $status->isOK(),
2534 "Locking of files succeeded with OK status ($backendName) ($i)." );
2537 $status = Status
::newGood();
2538 $sl = $this->backend
->getScopedFileLocks( $paths, LockManager
::LOCK_EX
, $status );
2539 $this->assertInstanceOf( ScopedLock
::class, $sl,
2540 "Scoped locking of files succeeded ($backendName)." );
2541 $this->assertEquals( [], $status->getErrors(),
2542 "Scoped locking of files succeeded ($backendName)." );
2543 $this->assertEquals( true, $status->isOK(),
2544 "Scoped locking of files succeeded with OK status ($backendName)." );
2546 ScopedLock
::release( $sl );
2547 $this->assertNull( $sl,
2548 "Scoped unlocking of files succeeded ($backendName)." );
2549 $this->assertEquals( [], $status->getErrors(),
2550 "Scoped unlocking of files succeeded ($backendName)." );
2551 $this->assertEquals( true, $status->isOK(),
2552 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2556 * @dataProvider provider_testGetContentType
2558 public function testGetContentType( $mimeCallback, $mimeFromString ) {
2561 $be = TestingAccessWrapper
::newFromObject( new MemoryFileBackend(
2563 'name' => 'testing',
2564 'class' => MemoryFileBackend
::class,
2566 'mimeCallback' => $mimeCallback
2570 $dst = 'mwstore://testing/container/path/to/file_no_ext';
2571 $src = "$IP/tests/phpunit/data/media/srgb.jpg";
2572 $this->assertEquals( 'image/jpeg', $be->getContentType( $dst, null, $src ) );
2573 $this->assertEquals(
2574 $mimeFromString ?
'image/jpeg' : 'unknown/unknown',
2575 $be->getContentType( $dst, file_get_contents( $src ), null ) );
2577 $src = "$IP/tests/phpunit/data/media/Png-native-test.png";
2578 $this->assertEquals( 'image/png', $be->getContentType( $dst, null, $src ) );
2579 $this->assertEquals(
2580 $mimeFromString ?
'image/png' : 'unknown/unknown',
2581 $be->getContentType( $dst, file_get_contents( $src ), null ) );
2584 public static function provider_testGetContentType() {
2587 [ [ FileBackendGroup
::singleton(), 'guessMimeInternal' ], true ]
2591 public function testReadAffinity() {
2592 $be = TestingAccessWrapper
::newFromObject(
2593 new FileBackendMultiWrite( [
2594 'name' => 'localtesting',
2595 'wikiId' => wfWikiID() . mt_rand(),
2598 'name' => 'multitesting0',
2599 'class' => MemoryFileBackend
::class,
2600 'isMultiMaster' => false,
2601 'readAffinity' => true
2604 'name' => 'multitesting1',
2605 'class' => MemoryFileBackend
::class,
2606 'isMultiMaster' => true
2612 $this->assertEquals(
2614 $be->getReadIndexFromParams( [ 'latest' => 1 ] ),
2615 'Reads with "latest" flag use backend 1'
2619 $be->getReadIndexFromParams( [ 'latest' => 0 ] ),
2620 'Reads without "latest" flag use backend 0'
2623 $p = 'container/test-cont/file.txt';
2624 $be->backends
[0]->quickCreate( [
2625 'dst' => "mwstore://multitesting0/$p", 'content' => 'cattitude' ] );
2626 $be->backends
[1]->quickCreate( [
2627 'dst' => "mwstore://multitesting1/$p", 'content' => 'princess of power' ] );
2629 $this->assertEquals(
2631 $be->getFileContents( [ 'src' => "mwstore://localtesting/$p" ] ),
2632 "Non-latest read came from backend 0"
2634 $this->assertEquals(
2635 'princess of power',
2636 $be->getFileContents( [ 'src' => "mwstore://localtesting/$p", 'latest' => 1 ] ),
2637 "Latest read came from backend1"
2641 public function testAsyncWrites() {
2642 $be = TestingAccessWrapper
::newFromObject(
2643 new FileBackendMultiWrite( [
2644 'name' => 'localtesting',
2645 'wikiId' => wfWikiID() . mt_rand(),
2648 'name' => 'multitesting0',
2649 'class' => MemoryFileBackend
::class,
2650 'isMultiMaster' => false
2653 'name' => 'multitesting1',
2654 'class' => MemoryFileBackend
::class,
2655 'isMultiMaster' => true
2658 'replication' => 'async'
2662 $this->setMwGlobals( 'wgCommandLineMode', false );
2664 $p = 'container/test-cont/file.txt';
2666 'dst' => "mwstore://localtesting/$p", 'content' => 'cattitude' ] );
2668 $this->assertEquals(
2670 $be->backends
[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
2671 "File not yet written to backend 0"
2673 $this->assertEquals(
2675 $be->backends
[1]->getFileContents( [ 'src' => "mwstore://multitesting1/$p" ] ),
2676 "File already written to backend 1"
2679 DeferredUpdates
::doUpdates();
2681 $this->assertEquals(
2683 $be->backends
[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
2684 "File now written to backend 0"
2688 public function testSanitizeOpHeaders() {
2689 $be = TestingAccessWrapper
::newFromObject( new MemoryFileBackend( [
2690 'name' => 'localtesting',
2691 'wikiId' => wfWikiID()
2696 'content-Disposition' => FileBackend
::makeContentDisposition( 'inline', 'name' ),
2697 'Content-dUration' => 25.6,
2698 'X-LONG-VALUE' => str_pad( '0', 300 ),
2699 'CONTENT-LENGTH' => 855055,
2704 'content-disposition' => FileBackend
::makeContentDisposition( 'inline', 'name' ),
2705 'content-duration' => 25.6,
2706 'content-length' => 855055
2710 Wikimedia\
suppressWarnings();
2711 $actual = $be->sanitizeOpHeaders( $input );
2712 Wikimedia\restoreWarnings
();
2714 $this->assertEquals( $expected, $actual, "Header sanitized properly" );
2718 private function listToArray( $iter ) {
2719 return is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2722 // test helper wrapper for backend prepare() function
2723 private function prepare( array $params ) {
2724 return $this->backend
->prepare( $params );
2727 // test helper wrapper for backend prepare() function
2728 private function create( array $params ) {
2729 $params['op'] = 'create';
2731 return $this->backend
->doQuickOperations( [ $params ] );
2734 function tearDownFiles() {
2735 $containers = [ 'unittest-cont1', 'unittest-cont2' ];
2736 foreach ( $containers as $container ) {
2737 $this->deleteFiles( $container );
2741 private function deleteFiles( $container ) {
2742 $base = self
::baseStorePath();
2743 $iter = $this->backend
->getFileList( [ 'dir' => "$base/$container" ] );
2745 foreach ( $iter as $file ) {
2746 $this->backend
->quickDelete( [ 'src' => "$base/$container/$file" ] );
2748 // free the directory, to avoid Permission denied under windows on rmdir
2751 $this->backend
->clean( [ 'dir' => "$base/$container", 'recursive' => 1 ] );
2754 private function assertBackendPathsConsistent( array $paths, $okSyncStatus ) {
2755 if ( !$this->backend
instanceof FileBackendMultiWrite
) {
2759 $status = $this->backend
->consistencyCheck( $paths );
2760 if ( $okSyncStatus ) {
2761 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2763 $this->assertBadStatus( $status, "Files not synced: " . implode( ',', $paths ) );
2767 private function assertGoodStatus( StatusValue
$status, $msg ) {
2768 $this->assertEquals( print_r( [], 1 ), print_r( $status->getErrors(), 1 ), $msg );
2771 private function assertBadStatus( StatusValue
$status, $msg ) {
2772 $this->assertNotEquals( print_r( [], 1 ), print_r( $status->getErrors(), 1 ), $msg );