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 ] );
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 ) {
322 $this->backend
= $this->singleBackend
;
323 $this->tearDownFiles();
324 $this->doTestCopy( $op );
325 $this->tearDownFiles();
327 $this->backend
= $this->multiBackend
;
328 $this->tearDownFiles();
329 $this->doTestCopy( $op );
330 $this->tearDownFiles();
333 private function doTestCopy( $op ) {
334 $backendName = $this->backendClass();
336 $source = $op['src'];
338 $this->prepare( [ 'dir' => dirname( $source ) ] );
339 $this->prepare( [ 'dir' => dirname( $dest ) ] );
341 if ( isset( $op['ignoreMissingSource'] ) ) {
342 $status = $this->backend
->doOperation( $op );
343 $this->assertGoodStatus( $status,
344 "Move from $source to $dest succeeded without warnings ($backendName)." );
345 $this->assertEquals( [ 0 => true ], $status->success
,
346 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
347 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $source ] ),
348 "Source file $source does not exist ($backendName)." );
349 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $dest ] ),
350 "Destination file $dest does not exist ($backendName)." );
355 $status = $this->backend
->doOperation(
356 [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ] );
357 $this->assertGoodStatus( $status,
358 "Creation of file at $source succeeded ($backendName)." );
360 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
361 $this->backend
->copy( $op );
364 $status = $this->backend
->doOperation( $op );
366 $this->assertGoodStatus( $status,
367 "Copy from $source to $dest succeeded without warnings ($backendName)." );
368 $this->assertEquals( true, $status->isOK(),
369 "Copy from $source to $dest succeeded ($backendName)." );
370 $this->assertEquals( [ 0 => true ], $status->success
,
371 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
372 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $source ] ),
373 "Source file $source still exists ($backendName)." );
374 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $dest ] ),
375 "Destination file $dest exists after copy ($backendName)." );
378 $this->backend
->getFileSize( [ 'src' => $source ] ),
379 $this->backend
->getFileSize( [ 'src' => $dest ] ),
380 "Destination file $dest has correct size ($backendName)." );
382 $props1 = $this->backend
->getFileProps( [ 'src' => $source ] );
383 $props2 = $this->backend
->getFileProps( [ 'src' => $dest ] );
384 $this->assertEquals( $props1, $props2,
385 "Source and destination have the same props ($backendName)." );
387 $this->assertBackendPathsConsistent( [ $source, $dest ] );
390 public static function provider_testCopy() {
393 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
394 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
396 $op = [ 'op' => 'copy', 'src' => $source, 'dst' => $dest ];
404 $op2['overwrite'] = true;
412 $op2['overwriteSame'] = true;
420 $op2['ignoreMissingSource'] = true;
428 $op2['ignoreMissingSource'] = true;
431 self
::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
439 * @dataProvider provider_testMove
441 public function testMove( $op ) {
442 $this->backend
= $this->singleBackend
;
443 $this->tearDownFiles();
444 $this->doTestMove( $op );
445 $this->tearDownFiles();
447 $this->backend
= $this->multiBackend
;
448 $this->tearDownFiles();
449 $this->doTestMove( $op );
450 $this->tearDownFiles();
453 private function doTestMove( $op ) {
454 $backendName = $this->backendClass();
456 $source = $op['src'];
458 $this->prepare( [ 'dir' => dirname( $source ) ] );
459 $this->prepare( [ 'dir' => dirname( $dest ) ] );
461 if ( isset( $op['ignoreMissingSource'] ) ) {
462 $status = $this->backend
->doOperation( $op );
463 $this->assertGoodStatus( $status,
464 "Move from $source to $dest succeeded without warnings ($backendName)." );
465 $this->assertEquals( [ 0 => true ], $status->success
,
466 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
467 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $source ] ),
468 "Source file $source does not exist ($backendName)." );
469 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $dest ] ),
470 "Destination file $dest does not exist ($backendName)." );
475 $status = $this->backend
->doOperation(
476 [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ] );
477 $this->assertGoodStatus( $status,
478 "Creation of file at $source succeeded ($backendName)." );
480 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
481 $this->backend
->copy( $op );
484 $status = $this->backend
->doOperation( $op );
485 $this->assertGoodStatus( $status,
486 "Move from $source to $dest succeeded without warnings ($backendName)." );
487 $this->assertEquals( true, $status->isOK(),
488 "Move from $source to $dest succeeded ($backendName)." );
489 $this->assertEquals( [ 0 => true ], $status->success
,
490 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
491 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $source ] ),
492 "Source file $source does not still exists ($backendName)." );
493 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $dest ] ),
494 "Destination file $dest exists after move ($backendName)." );
496 $this->assertNotEquals(
497 $this->backend
->getFileSize( [ 'src' => $source ] ),
498 $this->backend
->getFileSize( [ 'src' => $dest ] ),
499 "Destination file $dest has correct size ($backendName)." );
501 $props1 = $this->backend
->getFileProps( [ 'src' => $source ] );
502 $props2 = $this->backend
->getFileProps( [ 'src' => $dest ] );
503 $this->assertEquals( false, $props1['fileExists'],
504 "Source file does not exist accourding to props ($backendName)." );
505 $this->assertEquals( true, $props2['fileExists'],
506 "Destination file exists accourding to props ($backendName)." );
508 $this->assertBackendPathsConsistent( [ $source, $dest ] );
511 public static function provider_testMove() {
514 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
515 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
517 $op = [ 'op' => 'move', 'src' => $source, 'dst' => $dest ];
525 $op2['overwrite'] = true;
533 $op2['overwriteSame'] = true;
541 $op2['ignoreMissingSource'] = true;
549 $op2['ignoreMissingSource'] = true;
552 self
::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
560 * @dataProvider provider_testDelete
562 public function testDelete( $op, $withSource, $okStatus ) {
563 $this->backend
= $this->singleBackend
;
564 $this->tearDownFiles();
565 $this->doTestDelete( $op, $withSource, $okStatus );
566 $this->tearDownFiles();
568 $this->backend
= $this->multiBackend
;
569 $this->tearDownFiles();
570 $this->doTestDelete( $op, $withSource, $okStatus );
571 $this->tearDownFiles();
574 private function doTestDelete( $op, $withSource, $okStatus ) {
575 $backendName = $this->backendClass();
577 $source = $op['src'];
578 $this->prepare( [ 'dir' => dirname( $source ) ] );
581 $status = $this->backend
->doOperation(
582 [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ] );
583 $this->assertGoodStatus( $status,
584 "Creation of file at $source succeeded ($backendName)." );
587 $status = $this->backend
->doOperation( $op );
589 $this->assertGoodStatus( $status,
590 "Deletion of file at $source succeeded without warnings ($backendName)." );
591 $this->assertEquals( true, $status->isOK(),
592 "Deletion of file at $source succeeded ($backendName)." );
593 $this->assertEquals( [ 0 => true ], $status->success
,
594 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
596 $this->assertEquals( false, $status->isOK(),
597 "Deletion of file at $source failed ($backendName)." );
600 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $source ] ),
601 "Source file $source does not exist after move ($backendName)." );
604 $this->backend
->getFileSize( [ 'src' => $source ] ),
605 "Source file $source has correct size (false) ($backendName)." );
607 $props1 = $this->backend
->getFileProps( [ 'src' => $source ] );
608 $this->assertFalse( $props1['fileExists'],
609 "Source file $source does not exist according to props ($backendName)." );
611 $this->assertBackendPathsConsistent( [ $source ] );
614 public static function provider_testDelete() {
617 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
619 $op = [ 'op' => 'delete', 'src' => $source ];
628 false, // without source
632 $op['ignoreMissingSource'] = true;
635 false, // without source
639 $op['ignoreMissingSource'] = true;
640 $op['src'] = self
::baseStorePath() . '/unittest-cont-bad/e/file.txt';
643 false, // without source
651 * @dataProvider provider_testDescribe
653 public function testDescribe( $op, $withSource, $okStatus ) {
654 $this->backend
= $this->singleBackend
;
655 $this->tearDownFiles();
656 $this->doTestDescribe( $op, $withSource, $okStatus );
657 $this->tearDownFiles();
659 $this->backend
= $this->multiBackend
;
660 $this->tearDownFiles();
661 $this->doTestDescribe( $op, $withSource, $okStatus );
662 $this->tearDownFiles();
665 private function doTestDescribe( $op, $withSource, $okStatus ) {
666 $backendName = $this->backendClass();
668 $source = $op['src'];
669 $this->prepare( [ 'dir' => dirname( $source ) ] );
672 $status = $this->backend
->doOperation(
673 [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source,
674 'headers' => [ 'Content-Disposition' => 'xxx' ] ] );
675 $this->assertGoodStatus( $status,
676 "Creation of file at $source succeeded ($backendName)." );
677 if ( $this->backend
->hasFeatures( FileBackend
::ATTR_HEADERS
) ) {
678 $attr = $this->backend
->getFileXAttributes( [ 'src' => $source ] );
679 $this->assertHasHeaders( [ 'Content-Disposition' => 'xxx' ], $attr );
682 $status = $this->backend
->describe( [ 'src' => $source,
683 'headers' => [ 'Content-Disposition' => '' ] ] ); // remove
684 $this->assertGoodStatus( $status,
685 "Removal of header for $source succeeded ($backendName)." );
687 if ( $this->backend
->hasFeatures( FileBackend
::ATTR_HEADERS
) ) {
688 $attr = $this->backend
->getFileXAttributes( [ 'src' => $source ] );
689 $this->assertFalse( isset( $attr['headers']['content-disposition'] ),
690 "File 'Content-Disposition' header removed." );
694 $status = $this->backend
->doOperation( $op );
696 $this->assertGoodStatus( $status,
697 "Describe of file at $source succeeded without warnings ($backendName)." );
698 $this->assertEquals( true, $status->isOK(),
699 "Describe of file at $source succeeded ($backendName)." );
700 $this->assertEquals( [ 0 => true ], $status->success
,
701 "Describe of file at $source has proper 'success' field in Status ($backendName)." );
702 if ( $this->backend
->hasFeatures( FileBackend
::ATTR_HEADERS
) ) {
703 $attr = $this->backend
->getFileXAttributes( [ 'src' => $source ] );
704 $this->assertHasHeaders( $op['headers'], $attr );
707 $this->assertEquals( false, $status->isOK(),
708 "Describe of file at $source failed ($backendName)." );
711 $this->assertBackendPathsConsistent( [ $source ] );
714 private function assertHasHeaders( array $headers, array $attr ) {
715 foreach ( $headers as $n => $v ) {
717 $this->assertTrue( isset( $attr['headers'][strtolower( $n )] ),
718 "File has '$n' header." );
719 $this->assertEquals( $v, $attr['headers'][strtolower( $n )],
720 "File has '$n' header value." );
722 $this->assertFalse( isset( $attr['headers'][strtolower( $n )] ),
723 "File does not have '$n' header." );
728 public static function provider_testDescribe() {
731 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
733 $op = [ 'op' => 'describe', 'src' => $source,
734 'headers' => [ 'Content-Disposition' => 'inline' ], ];
743 false, // without source
751 * @dataProvider provider_testCreate
753 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
754 $this->backend
= $this->singleBackend
;
755 $this->tearDownFiles();
756 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
757 $this->tearDownFiles();
759 $this->backend
= $this->multiBackend
;
760 $this->tearDownFiles();
761 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
762 $this->tearDownFiles();
765 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
766 $backendName = $this->backendClass();
769 $this->prepare( [ 'dir' => dirname( $dest ) ] );
771 $oldText = 'blah...blah...waahwaah';
772 if ( $alreadyExists ) {
773 $status = $this->backend
->doOperation(
774 [ 'op' => 'create', 'content' => $oldText, 'dst' => $dest ] );
775 $this->assertGoodStatus( $status,
776 "Creation of file at $dest succeeded ($backendName)." );
779 $status = $this->backend
->doOperation( $op );
781 $this->assertGoodStatus( $status,
782 "Creation of file at $dest succeeded without warnings ($backendName)." );
783 $this->assertEquals( true, $status->isOK(),
784 "Creation of file at $dest succeeded ($backendName)." );
785 $this->assertEquals( [ 0 => true ], $status->success
,
786 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
788 $this->assertEquals( false, $status->isOK(),
789 "Creation of file at $dest failed ($backendName)." );
792 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $dest ] ),
793 "Destination file $dest exists after creation ($backendName)." );
795 $props1 = $this->backend
->getFileProps( [ 'src' => $dest ] );
796 $this->assertEquals( true, $props1['fileExists'],
797 "Destination file $dest exists according to props ($backendName)." );
798 if ( $okStatus ) { // file content is what we saved
799 $this->assertEquals( $newSize, $props1['size'],
800 "Destination file $dest has expected size according to props ($backendName)." );
801 $this->assertEquals( $newSize,
802 $this->backend
->getFileSize( [ 'src' => $dest ] ),
803 "Destination file $dest has correct size ($backendName)." );
804 } else { // file content is some other previous text
805 $this->assertEquals( strlen( $oldText ), $props1['size'],
806 "Destination file $dest has original size according to props ($backendName)." );
807 $this->assertEquals( strlen( $oldText ),
808 $this->backend
->getFileSize( [ 'src' => $dest ] ),
809 "Destination file $dest has original size according to props ($backendName)." );
812 $this->assertBackendPathsConsistent( [ $dest ] );
816 * @dataProvider provider_testCreate
818 public static function provider_testCreate() {
821 $dest = self
::baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
823 $op = [ 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest ];
826 false, // no dest already exists
828 strlen( $op['content'] )
832 $op2['content'] = "\n";
835 false, // no dest already exists
837 strlen( $op2['content'] )
841 $op2['content'] = "fsf\n waf 3kt";
844 true, // dest already exists
846 strlen( $op2['content'] )
850 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
851 $op2['overwrite'] = true;
854 true, // dest already exists
856 strlen( $op2['content'] )
860 $op2['content'] = "39qjmg3-qg";
861 $op2['overwriteSame'] = true;
864 true, // dest already exists
866 strlen( $op2['content'] )
872 public function testDoQuickOperations() {
873 $this->backend
= $this->singleBackend
;
874 $this->doTestDoQuickOperations();
875 $this->tearDownFiles();
877 $this->backend
= $this->multiBackend
;
878 $this->doTestDoQuickOperations();
879 $this->tearDownFiles();
882 private function doTestDoQuickOperations() {
883 $backendName = $this->backendClass();
885 $base = self
::baseStorePath();
887 "$base/unittest-cont1/e/fileA.a",
888 "$base/unittest-cont1/e/fileB.a",
889 "$base/unittest-cont1/e/fileC.a"
893 foreach ( $files as $path ) {
894 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
895 $this->assertGoodStatus( $status,
896 "Preparing $path succeeded without warnings ($backendName)." );
897 $createOps[] = [ 'op' => 'create', 'dst' => $path, 'content' => mt_rand( 0, 50000 ) ];
898 $copyOps[] = [ 'op' => 'copy', 'src' => $path, 'dst' => "$path-2" ];
899 $moveOps[] = [ 'op' => 'move', 'src' => "$path-2", 'dst' => "$path-3" ];
900 $purgeOps[] = [ 'op' => 'delete', 'src' => $path ];
901 $purgeOps[] = [ 'op' => 'delete', 'src' => "$path-3" ];
903 $purgeOps[] = [ 'op' => 'null' ];
905 $this->assertGoodStatus(
906 $this->backend
->doQuickOperations( $createOps ),
907 "Creation of source files succeeded ($backendName)." );
908 foreach ( $files as $file ) {
909 $this->assertTrue( $this->backend
->fileExists( [ 'src' => $file ] ),
910 "File $file exists." );
913 $this->assertGoodStatus(
914 $this->backend
->doQuickOperations( $copyOps ),
915 "Quick copy of source files succeeded ($backendName)." );
916 foreach ( $files as $file ) {
917 $this->assertTrue( $this->backend
->fileExists( [ 'src' => "$file-2" ] ),
918 "File $file-2 exists." );
921 $this->assertGoodStatus(
922 $this->backend
->doQuickOperations( $moveOps ),
923 "Quick move of source files succeeded ($backendName)." );
924 foreach ( $files as $file ) {
925 $this->assertTrue( $this->backend
->fileExists( [ 'src' => "$file-3" ] ),
926 "File $file-3 move in." );
927 $this->assertFalse( $this->backend
->fileExists( [ 'src' => "$file-2" ] ),
928 "File $file-2 moved away." );
931 $this->assertGoodStatus(
932 $this->backend
->quickCopy( [ 'src' => $files[0], 'dst' => $files[0] ] ),
933 "Copy of file {$files[0]} over itself succeeded ($backendName)." );
934 $this->assertTrue( $this->backend
->fileExists( [ 'src' => $files[0] ] ),
935 "File {$files[0]} still exists." );
937 $this->assertGoodStatus(
938 $this->backend
->quickMove( [ 'src' => $files[0], 'dst' => $files[0] ] ),
939 "Move of file {$files[0]} over itself succeeded ($backendName)." );
940 $this->assertTrue( $this->backend
->fileExists( [ 'src' => $files[0] ] ),
941 "File {$files[0]} still exists." );
943 $this->assertGoodStatus(
944 $this->backend
->doQuickOperations( $purgeOps ),
945 "Quick deletion of source files succeeded ($backendName)." );
946 foreach ( $files as $file ) {
947 $this->assertFalse( $this->backend
->fileExists( [ 'src' => $file ] ),
948 "File $file purged." );
949 $this->assertFalse( $this->backend
->fileExists( [ 'src' => "$file-3" ] ),
950 "File $file-3 purged." );
955 * @dataProvider provider_testConcatenate
957 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
958 $this->backend
= $this->singleBackend
;
959 $this->tearDownFiles();
960 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
961 $this->tearDownFiles();
963 $this->backend
= $this->multiBackend
;
964 $this->tearDownFiles();
965 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
966 $this->tearDownFiles();
969 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
970 $backendName = $this->backendClass();
975 foreach ( $srcs as $i => $source ) {
976 $this->prepare( [ 'dir' => dirname( $source ) ] );
978 'op' => 'create', // operation
979 'dst' => $source, // source
980 'content' => $srcsContent[$i]
982 $expContent .= $srcsContent[$i];
984 $status = $this->backend
->doOperations( $ops );
986 $this->assertGoodStatus( $status,
987 "Creation of source files succeeded ($backendName)." );
989 $dest = $params['dst'] = $this->getNewTempFile();
990 if ( $alreadyExists ) {
991 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
992 $this->assertEquals( true, $ok,
993 "Creation of file at $dest succeeded ($backendName)." );
995 $ok = file_put_contents( $dest, '' ) !== false;
996 $this->assertEquals( true, $ok,
997 "Creation of 0-byte file at $dest succeeded ($backendName)." );
1000 // Combine the files into one
1001 $status = $this->backend
->concatenate( $params );
1003 $this->assertGoodStatus( $status,
1004 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
1005 $this->assertEquals( true, $status->isOK(),
1006 "Creation of concat file at $dest succeeded ($backendName)." );
1008 $this->assertEquals( false, $status->isOK(),
1009 "Creation of concat file at $dest failed ($backendName)." );
1013 $this->assertEquals( true, is_file( $dest ),
1014 "Dest concat file $dest exists after creation ($backendName)." );
1016 $this->assertEquals( true, is_file( $dest ),
1017 "Dest concat file $dest exists after failed creation ($backendName)." );
1020 $contents = file_get_contents( $dest );
1021 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
1024 $this->assertEquals( $expContent, $contents,
1025 "Concat file at $dest has correct contents ($backendName)." );
1027 $this->assertNotEquals( $expContent, $contents,
1028 "Concat file at $dest has correct contents ($backendName)." );
1032 public static function provider_testConcatenate() {
1036 self
::baseStorePath() . '/unittest-cont1/e/file1.txt',
1037 self
::baseStorePath() . '/unittest-cont1/e/file2.txt',
1038 self
::baseStorePath() . '/unittest-cont1/e/file3.txt',
1039 self
::baseStorePath() . '/unittest-cont1/e/file4.txt',
1040 self
::baseStorePath() . '/unittest-cont1/e/file5.txt',
1041 self
::baseStorePath() . '/unittest-cont1/e/file6.txt',
1042 self
::baseStorePath() . '/unittest-cont1/e/file7.txt',
1043 self
::baseStorePath() . '/unittest-cont1/e/file8.txt',
1044 self
::baseStorePath() . '/unittest-cont1/e/file9.txt',
1045 self
::baseStorePath() . '/unittest-cont1/e/file10.txt'
1059 $params = [ 'srcs' => $srcs ];
1062 $params, // operation
1064 $content, // content for each source
1065 false, // no dest already exists
1070 $params, // operation
1072 $content, // content for each source
1073 true, // dest already exists
1081 * @dataProvider provider_testGetFileStat
1083 public function testGetFileStat( $path, $content, $alreadyExists ) {
1084 $this->backend
= $this->singleBackend
;
1085 $this->tearDownFiles();
1086 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1087 $this->tearDownFiles();
1089 $this->backend
= $this->multiBackend
;
1090 $this->tearDownFiles();
1091 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1092 $this->tearDownFiles();
1095 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
1096 $backendName = $this->backendClass();
1098 if ( $alreadyExists ) {
1099 $this->prepare( [ 'dir' => dirname( $path ) ] );
1100 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1101 $this->assertGoodStatus( $status,
1102 "Creation of file at $path succeeded ($backendName)." );
1104 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1105 $time = $this->backend
->getFileTimestamp( [ 'src' => $path ] );
1106 $stat = $this->backend
->getFileStat( [ 'src' => $path ] );
1108 $this->assertEquals( strlen( $content ), $size,
1109 "Correct file size of '$path'" );
1110 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1111 "Correct file timestamp of '$path'" );
1113 $size = $stat['size'];
1114 $time = $stat['mtime'];
1115 $this->assertEquals( strlen( $content ), $size,
1116 "Correct file size of '$path'" );
1117 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1118 "Correct file timestamp of '$path'" );
1120 $this->backend
->clearCache( [ $path ] );
1122 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1124 $this->assertEquals( strlen( $content ), $size,
1125 "Correct file size of '$path'" );
1127 $this->backend
->preloadCache( [ $path ] );
1129 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1131 $this->assertEquals( strlen( $content ), $size,
1132 "Correct file size of '$path'" );
1134 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1135 $time = $this->backend
->getFileTimestamp( [ 'src' => $path ] );
1136 $stat = $this->backend
->getFileStat( [ 'src' => $path ] );
1138 $this->assertFalse( $size, "Correct file size of '$path'" );
1139 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1140 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1144 public static function provider_testGetFileStat() {
1147 $base = self
::baseStorePath();
1148 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true ];
1149 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "", true ];
1150 $cases[] = [ "$base/unittest-cont1/e/b/some-diff_file.txt", null, false ];
1156 * @dataProvider provider_testGetFileStat
1158 public function testStreamFile( $path, $content, $alreadyExists ) {
1159 $this->backend
= $this->singleBackend
;
1160 $this->tearDownFiles();
1161 $this->doTestStreamFile( $path, $content, $alreadyExists );
1162 $this->tearDownFiles();
1164 $this->backend
= $this->multiBackend
;
1165 $this->tearDownFiles();
1166 $this->doTestStreamFile( $path, $content, $alreadyExists );
1167 $this->tearDownFiles();
1170 private function doTestStreamFile( $path, $content ) {
1171 $backendName = $this->backendClass();
1173 if ( $content !== null ) {
1174 $this->prepare( [ 'dir' => dirname( $path ) ] );
1175 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1176 $this->assertGoodStatus( $status,
1177 "Creation of file at $path succeeded ($backendName)." );
1180 $this->backend
->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
1181 $data = ob_get_contents();
1184 $this->assertEquals( $content, $data, "Correct content streamed from '$path'" );
1185 } else { // 404 case
1187 $this->backend
->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
1188 $data = ob_get_contents();
1191 $this->assertRegExp( '#<h1>File not found</h1>#', $data,
1192 "Correct content streamed from '$path' ($backendName)" );
1196 public static function provider_testStreamFile() {
1199 $base = self
::baseStorePath();
1200 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
1201 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", null ];
1206 public function testStreamFileRange() {
1207 $this->backend
= $this->singleBackend
;
1208 $this->tearDownFiles();
1209 $this->doTestStreamFileRange();
1210 $this->tearDownFiles();
1212 $this->backend
= $this->multiBackend
;
1213 $this->tearDownFiles();
1214 $this->doTestStreamFileRange();
1215 $this->tearDownFiles();
1218 private function doTestStreamFileRange() {
1219 $backendName = $this->backendClass();
1221 $base = self
::baseStorePath();
1222 $path = "$base/unittest-cont1/e/b/z/range_file.txt";
1223 $content = "0123456789ABCDEF";
1225 $this->prepare( [ 'dir' => dirname( $path ) ] );
1226 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1227 $this->assertGoodStatus( $status,
1228 "Creation of file at $path succeeded ($backendName)." );
1232 'bytes=0-3' => '0123',
1233 'bytes=4-8' => '45678',
1234 'bytes=15-15' => 'F',
1235 'bytes=14-15' => 'EF',
1236 'bytes=-5' => 'BCDEF',
1238 'bytes=10-16' => 'ABCDEF',
1239 'bytes=10-99' => 'ABCDEF',
1242 foreach ( $ranges as $range => $chunk ) {
1244 $this->backend
->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1,
1245 'options' => [ 'range' => $range ] ] );
1246 $data = ob_get_contents();
1249 $this->assertEquals( $chunk, $data, "Correct chunk streamed from '$path' for '$range'" );
1254 * @dataProvider provider_testGetFileContents
1256 public function testGetFileContents( $source, $content ) {
1257 $this->backend
= $this->singleBackend
;
1258 $this->tearDownFiles();
1259 $this->doTestGetFileContents( $source, $content );
1260 $this->tearDownFiles();
1262 $this->backend
= $this->multiBackend
;
1263 $this->tearDownFiles();
1264 $this->doTestGetFileContents( $source, $content );
1265 $this->tearDownFiles();
1268 private function doTestGetFileContents( $source, $content ) {
1269 $backendName = $this->backendClass();
1271 $srcs = (array)$source;
1272 $content = (array)$content;
1273 foreach ( $srcs as $i => $src ) {
1274 $this->prepare( [ 'dir' => dirname( $src ) ] );
1275 $status = $this->backend
->doOperation(
1276 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1277 $this->assertGoodStatus( $status,
1278 "Creation of file at $src succeeded ($backendName)." );
1281 if ( is_array( $source ) ) {
1282 $contents = $this->backend
->getFileContentsMulti( [ 'srcs' => $source ] );
1283 foreach ( $contents as $path => $data ) {
1284 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1285 $this->assertEquals(
1286 current( $content ),
1288 "Contents of $path is correct ($backendName)."
1292 $this->assertEquals(
1294 array_keys( $contents ),
1295 "Contents in right order ($backendName)."
1297 $this->assertEquals(
1300 "Contents array size correct ($backendName)."
1303 $data = $this->backend
->getFileContents( [ 'src' => $source ] );
1304 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1305 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1309 public static function provider_testGetFileContents() {
1312 $base = self
::baseStorePath();
1313 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
1314 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" ];
1316 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1317 "$base/unittest-cont1/e/a/z.txt" ],
1318 [ "contents xx", "contents xy", "contents xz" ]
1325 * @dataProvider provider_testGetLocalCopy
1327 public function testGetLocalCopy( $source, $content ) {
1328 $this->backend
= $this->singleBackend
;
1329 $this->tearDownFiles();
1330 $this->doTestGetLocalCopy( $source, $content );
1331 $this->tearDownFiles();
1333 $this->backend
= $this->multiBackend
;
1334 $this->tearDownFiles();
1335 $this->doTestGetLocalCopy( $source, $content );
1336 $this->tearDownFiles();
1339 private function doTestGetLocalCopy( $source, $content ) {
1340 $backendName = $this->backendClass();
1342 $srcs = (array)$source;
1343 $content = (array)$content;
1344 foreach ( $srcs as $i => $src ) {
1345 $this->prepare( [ 'dir' => dirname( $src ) ] );
1346 $status = $this->backend
->doOperation(
1347 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1348 $this->assertGoodStatus( $status,
1349 "Creation of file at $src succeeded ($backendName)." );
1352 if ( is_array( $source ) ) {
1353 $tmpFiles = $this->backend
->getLocalCopyMulti( [ 'srcs' => $source ] );
1354 foreach ( $tmpFiles as $path => $tmpFile ) {
1355 $this->assertNotNull( $tmpFile,
1356 "Creation of local copy of $path succeeded ($backendName)." );
1357 $contents = file_get_contents( $tmpFile->getPath() );
1358 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1359 $this->assertEquals(
1360 current( $content ),
1362 "Local copy of $path is correct ($backendName)."
1366 $this->assertEquals(
1368 array_keys( $tmpFiles ),
1369 "Local copies in right order ($backendName)."
1371 $this->assertEquals(
1374 "Local copies array size correct ($backendName)."
1377 $tmpFile = $this->backend
->getLocalCopy( [ 'src' => $source ] );
1378 $this->assertNotNull( $tmpFile,
1379 "Creation of local copy of $source succeeded ($backendName)." );
1380 $contents = file_get_contents( $tmpFile->getPath() );
1381 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1382 $this->assertEquals(
1385 "Local copy of $source is correct ($backendName)."
1389 $obj = new stdClass();
1390 $tmpFile->bind( $obj );
1393 public static function provider_testGetLocalCopy() {
1396 $base = self
::baseStorePath();
1397 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1398 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1399 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1401 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1402 "$base/unittest-cont1/e/a/z.txt" ],
1403 [ "contents xx $", "contents xy 111", "contents xz" ]
1410 * @dataProvider provider_testGetLocalReference
1412 public function testGetLocalReference( $source, $content ) {
1413 $this->backend
= $this->singleBackend
;
1414 $this->tearDownFiles();
1415 $this->doTestGetLocalReference( $source, $content );
1416 $this->tearDownFiles();
1418 $this->backend
= $this->multiBackend
;
1419 $this->tearDownFiles();
1420 $this->doTestGetLocalReference( $source, $content );
1421 $this->tearDownFiles();
1424 private function doTestGetLocalReference( $source, $content ) {
1425 $backendName = $this->backendClass();
1427 $srcs = (array)$source;
1428 $content = (array)$content;
1429 foreach ( $srcs as $i => $src ) {
1430 $this->prepare( [ 'dir' => dirname( $src ) ] );
1431 $status = $this->backend
->doOperation(
1432 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1433 $this->assertGoodStatus( $status,
1434 "Creation of file at $src succeeded ($backendName)." );
1437 if ( is_array( $source ) ) {
1438 $tmpFiles = $this->backend
->getLocalReferenceMulti( [ 'srcs' => $source ] );
1439 foreach ( $tmpFiles as $path => $tmpFile ) {
1440 $this->assertNotNull( $tmpFile,
1441 "Creation of local copy of $path succeeded ($backendName)." );
1442 $contents = file_get_contents( $tmpFile->getPath() );
1443 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1444 $this->assertEquals(
1445 current( $content ),
1447 "Local ref of $path is correct ($backendName)."
1451 $this->assertEquals(
1453 array_keys( $tmpFiles ),
1454 "Local refs in right order ($backendName)."
1456 $this->assertEquals(
1459 "Local refs array size correct ($backendName)."
1462 $tmpFile = $this->backend
->getLocalReference( [ 'src' => $source ] );
1463 $this->assertNotNull( $tmpFile,
1464 "Creation of local copy of $source succeeded ($backendName)." );
1465 $contents = file_get_contents( $tmpFile->getPath() );
1466 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1467 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1471 public static function provider_testGetLocalReference() {
1474 $base = self
::baseStorePath();
1475 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1476 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1477 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1479 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1480 "$base/unittest-cont1/e/a/z.txt" ],
1481 [ "contents xx 1111", "contents xy %", "contents xz $" ]
1487 public function testGetLocalCopyAndReference404() {
1488 $this->backend
= $this->singleBackend
;
1489 $this->tearDownFiles();
1490 $this->doTestGetLocalCopyAndReference404();
1491 $this->tearDownFiles();
1493 $this->backend
= $this->multiBackend
;
1494 $this->tearDownFiles();
1495 $this->doTestGetLocalCopyAndReference404();
1496 $this->tearDownFiles();
1499 public function doTestGetLocalCopyAndReference404() {
1500 $backendName = $this->backendClass();
1502 $base = self
::baseStorePath();
1504 $tmpFile = $this->backend
->getLocalCopy( [
1505 'src' => "$base/unittest-cont1/not-there" ] );
1506 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1508 $tmpFile = $this->backend
->getLocalReference( [
1509 'src' => "$base/unittest-cont1/not-there" ] );
1510 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1514 * @dataProvider provider_testGetFileHttpUrl
1516 public function testGetFileHttpUrl( $source, $content ) {
1517 $this->backend
= $this->singleBackend
;
1518 $this->tearDownFiles();
1519 $this->doTestGetFileHttpUrl( $source, $content );
1520 $this->tearDownFiles();
1522 $this->backend
= $this->multiBackend
;
1523 $this->tearDownFiles();
1524 $this->doTestGetFileHttpUrl( $source, $content );
1525 $this->tearDownFiles();
1528 private function doTestGetFileHttpUrl( $source, $content ) {
1529 $backendName = $this->backendClass();
1531 $this->prepare( [ 'dir' => dirname( $source ) ] );
1532 $status = $this->backend
->doOperation(
1533 [ 'op' => 'create', 'content' => $content, 'dst' => $source ] );
1534 $this->assertGoodStatus( $status,
1535 "Creation of file at $source succeeded ($backendName)." );
1537 $url = $this->backend
->getFileHttpUrl( [ 'src' => $source ] );
1539 if ( $url !== null ) { // supported
1540 $data = MediaWikiServices
::getInstance()->getHttpRequestFactory()->
1541 get( $url, [], __METHOD__
);
1542 $this->assertEquals( $content, $data,
1543 "HTTP GET of URL has right contents ($backendName)." );
1547 public static function provider_testGetFileHttpUrl() {
1550 $base = self
::baseStorePath();
1551 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1552 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1553 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1559 * @dataProvider provider_testPrepareAndClean
1561 public function testPrepareAndClean( $path, $isOK ) {
1562 $this->backend
= $this->singleBackend
;
1563 $this->doTestPrepareAndClean( $path, $isOK );
1564 $this->tearDownFiles();
1566 $this->backend
= $this->multiBackend
;
1567 $this->doTestPrepareAndClean( $path, $isOK );
1568 $this->tearDownFiles();
1571 public static function provider_testPrepareAndClean() {
1572 $base = self
::baseStorePath();
1575 [ "$base/unittest-cont1/e/a/z/some_file1.txt", true ],
1576 [ "$base/unittest-cont2/a/z/some_file2.txt", true ],
1577 # Specific to FS backend with no basePath field set
1578 # [ "$base/unittest-cont3/a/z/some_file3.txt", false ],
1582 private function doTestPrepareAndClean( $path, $isOK ) {
1583 $backendName = $this->backendClass();
1585 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
1587 $this->assertGoodStatus( $status,
1588 "Preparing dir $path succeeded without warnings ($backendName)." );
1589 $this->assertEquals( true, $status->isOK(),
1590 "Preparing dir $path succeeded ($backendName)." );
1592 $this->assertEquals( false, $status->isOK(),
1593 "Preparing dir $path failed ($backendName)." );
1596 $status = $this->backend
->secure( [ 'dir' => dirname( $path ) ] );
1598 $this->assertGoodStatus( $status,
1599 "Securing dir $path succeeded without warnings ($backendName)." );
1600 $this->assertEquals( true, $status->isOK(),
1601 "Securing dir $path succeeded ($backendName)." );
1603 $this->assertEquals( false, $status->isOK(),
1604 "Securing dir $path failed ($backendName)." );
1607 $status = $this->backend
->publish( [ 'dir' => dirname( $path ) ] );
1609 $this->assertGoodStatus( $status,
1610 "Publishing dir $path succeeded without warnings ($backendName)." );
1611 $this->assertEquals( true, $status->isOK(),
1612 "Publishing dir $path succeeded ($backendName)." );
1614 $this->assertEquals( false, $status->isOK(),
1615 "Publishing dir $path failed ($backendName)." );
1618 $status = $this->backend
->clean( [ 'dir' => dirname( $path ) ] );
1620 $this->assertGoodStatus( $status,
1621 "Cleaning dir $path succeeded without warnings ($backendName)." );
1622 $this->assertEquals( true, $status->isOK(),
1623 "Cleaning dir $path succeeded ($backendName)." );
1625 $this->assertEquals( false, $status->isOK(),
1626 "Cleaning dir $path failed ($backendName)." );
1630 public function testRecursiveClean() {
1631 $this->backend
= $this->singleBackend
;
1632 $this->doTestRecursiveClean();
1633 $this->tearDownFiles();
1635 $this->backend
= $this->multiBackend
;
1636 $this->doTestRecursiveClean();
1637 $this->tearDownFiles();
1640 private function doTestRecursiveClean() {
1641 $backendName = $this->backendClass();
1643 $base = self
::baseStorePath();
1645 "$base/unittest-cont1",
1646 "$base/unittest-cont1/e",
1647 "$base/unittest-cont1/e/a",
1648 "$base/unittest-cont1/e/a/b",
1649 "$base/unittest-cont1/e/a/b/c",
1650 "$base/unittest-cont1/e/a/b/c/d0",
1651 "$base/unittest-cont1/e/a/b/c/d1",
1652 "$base/unittest-cont1/e/a/b/c/d2",
1653 "$base/unittest-cont1/e/a/b/c/d0/1",
1654 "$base/unittest-cont1/e/a/b/c/d0/2",
1655 "$base/unittest-cont1/e/a/b/c/d1/3",
1656 "$base/unittest-cont1/e/a/b/c/d1/4",
1657 "$base/unittest-cont1/e/a/b/c/d2/5",
1658 "$base/unittest-cont1/e/a/b/c/d2/6"
1660 foreach ( $dirs as $dir ) {
1661 $status = $this->prepare( [ 'dir' => $dir ] );
1662 $this->assertGoodStatus( $status,
1663 "Preparing dir $dir succeeded without warnings ($backendName)." );
1666 if ( $this->backend
instanceof FSFileBackend
) {
1667 foreach ( $dirs as $dir ) {
1668 $this->assertEquals( true, $this->backend
->directoryExists( [ 'dir' => $dir ] ),
1669 "Dir $dir exists ($backendName)." );
1673 $status = $this->backend
->clean(
1674 [ 'dir' => "$base/unittest-cont1", 'recursive' => 1 ] );
1675 $this->assertGoodStatus( $status,
1676 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1678 foreach ( $dirs as $dir ) {
1679 $this->assertEquals( false, $this->backend
->directoryExists( [ 'dir' => $dir ] ),
1680 "Dir $dir no longer exists ($backendName)." );
1684 public function testDoOperations() {
1685 $this->backend
= $this->singleBackend
;
1686 $this->tearDownFiles();
1687 $this->doTestDoOperations();
1688 $this->tearDownFiles();
1690 $this->backend
= $this->multiBackend
;
1691 $this->tearDownFiles();
1692 $this->doTestDoOperations();
1693 $this->tearDownFiles();
1696 private function doTestDoOperations() {
1697 $base = self
::baseStorePath();
1699 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1700 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1701 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1702 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1703 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1704 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1705 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1707 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1708 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1709 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1710 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
1711 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1712 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
1713 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1715 $status = $this->backend
->doOperations( [
1716 [ 'op' => 'describe', 'src' => $fileA,
1717 'headers' => [ 'X-Content-Length' => '91.3' ], 'disposition' => 'inline' ],
1718 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1719 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1720 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1721 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1722 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1723 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1724 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1725 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1726 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1727 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1728 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1729 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1730 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1731 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1732 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1733 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1734 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1736 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1738 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1740 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1746 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1747 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1748 $this->assertEquals( 14, count( $status->success
),
1749 "Operation batch has correct success array" );
1751 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileA ] ),
1752 "File does not exist at $fileA" );
1753 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileB ] ),
1754 "File does not exist at $fileB" );
1755 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileD ] ),
1756 "File does not exist at $fileD" );
1758 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileC ] ),
1759 "File exists at $fileC" );
1760 $this->assertEquals( $fileBContents,
1761 $this->backend
->getFileContents( [ 'src' => $fileC ] ),
1762 "Correct file contents of $fileC" );
1763 $this->assertEquals( strlen( $fileBContents ),
1764 $this->backend
->getFileSize( [ 'src' => $fileC ] ),
1765 "Correct file size of $fileC" );
1766 $this->assertEquals( Wikimedia\base_convert
( sha1( $fileBContents ), 16, 36, 31 ),
1767 $this->backend
->getFileSha1Base36( [ 'src' => $fileC ] ),
1768 "Correct file SHA-1 of $fileC" );
1771 public function testDoOperationsPipeline() {
1772 $this->backend
= $this->singleBackend
;
1773 $this->tearDownFiles();
1774 $this->doTestDoOperationsPipeline();
1775 $this->tearDownFiles();
1777 $this->backend
= $this->multiBackend
;
1778 $this->tearDownFiles();
1779 $this->doTestDoOperationsPipeline();
1780 $this->tearDownFiles();
1783 // concurrency orientated
1784 private function doTestDoOperationsPipeline() {
1785 $base = self
::baseStorePath();
1787 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1788 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1789 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1791 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1792 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1793 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1794 $this->addTmpFiles( [ $tmpNameA, $tmpNameB, $tmpNameC ] );
1795 file_put_contents( $tmpNameA, $fileAContents );
1796 file_put_contents( $tmpNameB, $fileBContents );
1797 file_put_contents( $tmpNameC, $fileCContents );
1799 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1800 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1801 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1802 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1804 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1805 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1806 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1807 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1808 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1810 $status = $this->backend
->doOperations( [
1811 [ 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ],
1812 [ 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ],
1813 [ 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ],
1814 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1815 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1816 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1817 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1818 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1819 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1820 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1821 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1822 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1823 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1824 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1825 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1826 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1827 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1828 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1829 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1830 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1832 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1834 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1836 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1842 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1843 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1844 $this->assertEquals( 16, count( $status->success
),
1845 "Operation batch has correct success array" );
1847 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileA ] ),
1848 "File does not exist at $fileA" );
1849 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileB ] ),
1850 "File does not exist at $fileB" );
1851 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileD ] ),
1852 "File does not exist at $fileD" );
1854 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileC ] ),
1855 "File exists at $fileC" );
1856 $this->assertEquals( $fileBContents,
1857 $this->backend
->getFileContents( [ 'src' => $fileC ] ),
1858 "Correct file contents of $fileC" );
1859 $this->assertEquals( strlen( $fileBContents ),
1860 $this->backend
->getFileSize( [ 'src' => $fileC ] ),
1861 "Correct file size of $fileC" );
1862 $this->assertEquals( Wikimedia\base_convert
( sha1( $fileBContents ), 16, 36, 31 ),
1863 $this->backend
->getFileSha1Base36( [ 'src' => $fileC ] ),
1864 "Correct file SHA-1 of $fileC" );
1867 public function testDoOperationsFailing() {
1868 $this->backend
= $this->singleBackend
;
1869 $this->tearDownFiles();
1870 $this->doTestDoOperationsFailing();
1871 $this->tearDownFiles();
1873 $this->backend
= $this->multiBackend
;
1874 $this->tearDownFiles();
1875 $this->doTestDoOperationsFailing();
1876 $this->tearDownFiles();
1879 private function doTestDoOperationsFailing() {
1880 $base = self
::baseStorePath();
1882 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1883 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1884 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1885 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1886 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1887 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1888 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1890 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1891 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1892 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1893 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
1894 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1895 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
1897 $status = $this->backend
->doOperations( [
1898 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1899 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1900 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1901 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1902 [ 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ],
1903 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1904 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ],
1905 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1906 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ],
1907 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1908 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ],
1909 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1910 [ 'op' => 'delete', 'src' => $fileD ],
1911 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1914 ], [ 'force' => 1 ] );
1916 $this->assertNotEquals( [], $status->getErrors(), "Operation had warnings" );
1917 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1918 $this->assertEquals( 8, count( $status->success
),
1919 "Operation batch has correct success array" );
1921 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileB ] ),
1922 "File does not exist at $fileB" );
1923 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileD ] ),
1924 "File does not exist at $fileD" );
1926 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileA ] ),
1927 "File does not exist at $fileA" );
1928 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileC ] ),
1929 "File exists at $fileC" );
1930 $this->assertEquals( $fileBContents,
1931 $this->backend
->getFileContents( [ 'src' => $fileA ] ),
1932 "Correct file contents of $fileA" );
1933 $this->assertEquals( strlen( $fileBContents ),
1934 $this->backend
->getFileSize( [ 'src' => $fileA ] ),
1935 "Correct file size of $fileA" );
1936 $this->assertEquals( Wikimedia\base_convert
( sha1( $fileBContents ), 16, 36, 31 ),
1937 $this->backend
->getFileSha1Base36( [ 'src' => $fileA ] ),
1938 "Correct file SHA-1 of $fileA" );
1941 public function testGetFileList() {
1942 $this->backend
= $this->singleBackend
;
1943 $this->tearDownFiles();
1944 $this->doTestGetFileList();
1945 $this->tearDownFiles();
1947 $this->backend
= $this->multiBackend
;
1948 $this->tearDownFiles();
1949 $this->doTestGetFileList();
1950 $this->tearDownFiles();
1953 private function doTestGetFileList() {
1954 $backendName = $this->backendClass();
1955 $base = self
::baseStorePath();
1957 // Should have no errors
1958 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont-notexists" ] );
1961 "$base/unittest-cont1/e/test1.txt",
1962 "$base/unittest-cont1/e/test2.txt",
1963 "$base/unittest-cont1/e/test3.txt",
1964 "$base/unittest-cont1/e/subdir1/test1.txt",
1965 "$base/unittest-cont1/e/subdir1/test2.txt",
1966 "$base/unittest-cont1/e/subdir2/test3.txt",
1967 "$base/unittest-cont1/e/subdir2/test4.txt",
1968 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1969 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1970 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1971 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1972 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1973 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1974 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1979 foreach ( $files as $file ) {
1980 $this->prepare( [ 'dir' => dirname( $file ) ] );
1981 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
1983 $status = $this->backend
->doQuickOperations( $ops );
1984 $this->assertGoodStatus( $status,
1985 "Creation of files succeeded ($backendName)." );
1986 $this->assertEquals( true, $status->isOK(),
1987 "Creation of files succeeded with OK status ($backendName)." );
1989 // Expected listing at root
1994 "e/subdir1/test1.txt",
1995 "e/subdir1/test2.txt",
1996 "e/subdir2/test3.txt",
1997 "e/subdir2/test4.txt",
1998 "e/subdir2/subdir/test1.txt",
1999 "e/subdir2/subdir/test2.txt",
2000 "e/subdir2/subdir/test3.txt",
2001 "e/subdir2/subdir/test4.txt",
2002 "e/subdir2/subdir/test5.txt",
2003 "e/subdir2/subdir/sub/test0.txt",
2004 "e/subdir2/subdir/sub/120-px-file.txt",
2008 // Actual listing (no trailing slash) at root
2009 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1" ] );
2010 $list = $this->listToArray( $iter );
2012 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2014 // Actual listing (no trailing slash) at root with advise
2015 $iter = $this->backend
->getFileList( [
2016 'dir' => "$base/unittest-cont1",
2019 $list = $this->listToArray( $iter );
2021 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2023 // Actual listing (with trailing slash) at root
2025 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/" ] );
2026 foreach ( $iter as $file ) {
2030 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2032 // Expected listing at subdir
2040 "sub/120-px-file.txt",
2044 // Actual listing (no trailing slash) at subdir
2045 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] );
2046 $list = $this->listToArray( $iter );
2048 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2050 // Actual listing (no trailing slash) at subdir with advise
2051 $iter = $this->backend
->getFileList( [
2052 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2055 $list = $this->listToArray( $iter );
2057 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2059 // Actual listing (with trailing slash) at subdir
2061 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ] );
2062 foreach ( $iter as $file ) {
2066 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2068 // Actual listing (using iterator second time)
2069 $list = $this->listToArray( $iter );
2071 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
2073 // Actual listing (top files only) at root
2074 $iter = $this->backend
->getTopFileList( [ 'dir' => "$base/unittest-cont1" ] );
2075 $list = $this->listToArray( $iter );
2077 $this->assertEquals( [], $list, "Correct top file listing ($backendName)." );
2079 // Expected listing (top files only) at subdir
2089 // Actual listing (top files only) at subdir
2090 $iter = $this->backend
->getTopFileList(
2091 [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ]
2093 $list = $this->listToArray( $iter );
2095 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2097 // Actual listing (top files only) at subdir with advise
2098 $iter = $this->backend
->getTopFileList( [
2099 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2102 $list = $this->listToArray( $iter );
2104 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2106 foreach ( $files as $file ) { // clean up
2107 $this->backend
->doOperation( [ 'op' => 'delete', 'src' => $file ] );
2110 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
2111 foreach ( $iter as $iter ) {
2116 public function testGetDirectoryList() {
2117 $this->backend
= $this->singleBackend
;
2118 $this->tearDownFiles();
2119 $this->doTestGetDirectoryList();
2120 $this->tearDownFiles();
2122 $this->backend
= $this->multiBackend
;
2123 $this->tearDownFiles();
2124 $this->doTestGetDirectoryList();
2125 $this->tearDownFiles();
2128 private function doTestGetDirectoryList() {
2129 $backendName = $this->backendClass();
2131 $base = self
::baseStorePath();
2133 "$base/unittest-cont1/e/test1.txt",
2134 "$base/unittest-cont1/e/test2.txt",
2135 "$base/unittest-cont1/e/test3.txt",
2136 "$base/unittest-cont1/e/subdir1/test1.txt",
2137 "$base/unittest-cont1/e/subdir1/test2.txt",
2138 "$base/unittest-cont1/e/subdir2/test3.txt",
2139 "$base/unittest-cont1/e/subdir2/test4.txt",
2140 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
2141 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
2142 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
2143 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
2144 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
2145 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
2146 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
2151 foreach ( $files as $file ) {
2152 $this->prepare( [ 'dir' => dirname( $file ) ] );
2153 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
2155 $status = $this->backend
->doQuickOperations( $ops );
2156 $this->assertGoodStatus( $status,
2157 "Creation of files succeeded ($backendName)." );
2158 $this->assertEquals( true, $status->isOK(),
2159 "Creation of files succeeded with OK status ($backendName)." );
2161 $this->assertEquals( true,
2162 $this->backend
->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] ),
2163 "Directory exists in ($backendName)." );
2164 $this->assertEquals( true,
2165 $this->backend
->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] ),
2166 "Directory exists in ($backendName)." );
2167 $this->assertEquals( false,
2168 $this->backend
->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ] ),
2169 "Directory does not exists in ($backendName)." );
2177 // Actual listing (no trailing slash)
2179 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1" ] );
2180 foreach ( $iter as $file ) {
2185 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2196 // Actual listing (no trailing slash)
2198 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e" ] );
2199 foreach ( $iter as $file ) {
2204 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2206 // Actual listing (with trailing slash)
2208 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/" ] );
2209 foreach ( $iter as $file ) {
2214 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2222 // Actual listing (no trailing slash)
2224 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir2" ] );
2225 foreach ( $iter as $file ) {
2230 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2232 // Actual listing (with trailing slash)
2234 $iter = $this->backend
->getTopDirectoryList(
2235 [ 'dir' => "$base/unittest-cont1/e/subdir2/" ]
2238 foreach ( $iter as $file ) {
2243 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2245 // Actual listing (using iterator second time)
2247 foreach ( $iter as $file ) {
2252 $this->assertEquals(
2255 "Correct top dir listing ($backendName), second iteration."
2258 // Expected listing (recursive)
2268 "e/subdir4/subdir/sub",
2272 // Actual listing (recursive)
2274 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/" ] );
2275 foreach ( $iter as $file ) {
2280 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2282 // Expected listing (recursive)
2289 // Actual listing (recursive)
2291 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir4" ] );
2292 foreach ( $iter as $file ) {
2297 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2299 // Actual listing (recursive, second time)
2301 foreach ( $iter as $file ) {
2306 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2308 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] );
2309 $items = $this->listToArray( $iter );
2310 $this->assertEquals( [], $items, "Directory listing is empty." );
2312 foreach ( $files as $file ) { // clean up
2313 $this->backend
->doOperation( [ 'op' => 'delete', 'src' => $file ] );
2316 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
2317 foreach ( $iter as $file ) {
2321 $items = $this->listToArray( $iter );
2322 $this->assertEquals( [], $items, "Directory listing is empty." );
2324 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/not/exists" ] );
2325 $items = $this->listToArray( $iter );
2326 $this->assertEquals( [], $items, "Directory listing is empty." );
2329 public function testLockCalls() {
2330 $this->backend
= $this->singleBackend
;
2331 $this->doTestLockCalls();
2334 private function doTestLockCalls() {
2335 $backendName = $this->backendClass();
2336 $base = $this->backend
->getContainerStoragePath( 'test' );
2343 "$base/subdir1", // duplicate
2344 "$base/subdir1/test1.txt",
2345 "$base/subdir1/test2.txt",
2347 "$base/subdir2", // duplicate
2348 "$base/subdir2/test3.txt",
2349 "$base/subdir2/test4.txt",
2350 "$base/subdir2/subdir",
2351 "$base/subdir2/subdir/test1.txt",
2352 "$base/subdir2/subdir/test2.txt",
2353 "$base/subdir2/subdir/test3.txt",
2354 "$base/subdir2/subdir/test4.txt",
2355 "$base/subdir2/subdir/test5.txt",
2356 "$base/subdir2/subdir/sub",
2357 "$base/subdir2/subdir/sub/test0.txt",
2358 "$base/subdir2/subdir/sub/120-px-file.txt",
2361 for ( $i = 0; $i < 25; $i++
) {
2362 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2363 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2364 "Locking of files succeeded ($backendName) ($i)." );
2365 $this->assertEquals( true, $status->isOK(),
2366 "Locking of files succeeded with OK status ($backendName) ($i)." );
2368 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2369 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2370 "Locking of files succeeded ($backendName) ($i)." );
2371 $this->assertEquals( true, $status->isOK(),
2372 "Locking of files succeeded with OK status ($backendName) ($i)." );
2374 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2375 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2376 "Locking of files succeeded ($backendName) ($i)." );
2377 $this->assertEquals( true, $status->isOK(),
2378 "Locking of files succeeded with OK status ($backendName) ($i)." );
2380 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2381 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2382 "Locking of files succeeded ($backendName). ($i)" );
2383 $this->assertEquals( true, $status->isOK(),
2384 "Locking of files succeeded with OK status ($backendName) ($i)." );
2386 # # Flip the acquire/release ordering around ##
2388 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2389 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2390 "Locking of files succeeded ($backendName) ($i)." );
2391 $this->assertEquals( true, $status->isOK(),
2392 "Locking of files succeeded with OK status ($backendName) ($i)." );
2394 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2395 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2396 "Locking of files succeeded ($backendName) ($i)." );
2397 $this->assertEquals( true, $status->isOK(),
2398 "Locking of files succeeded with OK status ($backendName) ($i)." );
2400 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2401 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2402 "Locking of files succeeded ($backendName). ($i)" );
2403 $this->assertEquals( true, $status->isOK(),
2404 "Locking of files succeeded with OK status ($backendName) ($i)." );
2406 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2407 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2408 "Locking of files succeeded ($backendName) ($i)." );
2409 $this->assertEquals( true, $status->isOK(),
2410 "Locking of files succeeded with OK status ($backendName) ($i)." );
2413 $status = Status
::newGood();
2414 $sl = $this->backend
->getScopedFileLocks( $paths, LockManager
::LOCK_EX
, $status );
2415 $this->assertInstanceOf( ScopedLock
::class, $sl,
2416 "Scoped locking of files succeeded ($backendName)." );
2417 $this->assertEquals( [], $status->getErrors(),
2418 "Scoped locking of files succeeded ($backendName)." );
2419 $this->assertEquals( true, $status->isOK(),
2420 "Scoped locking of files succeeded with OK status ($backendName)." );
2422 ScopedLock
::release( $sl );
2423 $this->assertEquals( null, $sl,
2424 "Scoped unlocking of files succeeded ($backendName)." );
2425 $this->assertEquals( [], $status->getErrors(),
2426 "Scoped unlocking of files succeeded ($backendName)." );
2427 $this->assertEquals( true, $status->isOK(),
2428 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2432 * @dataProvider provider_testGetContentType
2434 public function testGetContentType( $mimeCallback, $mimeFromString ) {
2437 $be = TestingAccessWrapper
::newFromObject( new MemoryFileBackend(
2439 'name' => 'testing',
2440 'class' => MemoryFileBackend
::class,
2442 'mimeCallback' => $mimeCallback
2446 $dst = 'mwstore://testing/container/path/to/file_no_ext';
2447 $src = "$IP/tests/phpunit/data/media/srgb.jpg";
2448 $this->assertEquals( 'image/jpeg', $be->getContentType( $dst, null, $src ) );
2449 $this->assertEquals(
2450 $mimeFromString ?
'image/jpeg' : 'unknown/unknown',
2451 $be->getContentType( $dst, file_get_contents( $src ), null ) );
2453 $src = "$IP/tests/phpunit/data/media/Png-native-test.png";
2454 $this->assertEquals( 'image/png', $be->getContentType( $dst, null, $src ) );
2455 $this->assertEquals(
2456 $mimeFromString ?
'image/png' : 'unknown/unknown',
2457 $be->getContentType( $dst, file_get_contents( $src ), null ) );
2460 public static function provider_testGetContentType() {
2463 [ [ FileBackendGroup
::singleton(), 'guessMimeInternal' ], true ]
2467 public function testReadAffinity() {
2468 $be = TestingAccessWrapper
::newFromObject(
2469 new FileBackendMultiWrite( [
2470 'name' => 'localtesting',
2471 'wikiId' => wfWikiID() . mt_rand(),
2474 'name' => 'multitesting0',
2475 'class' => MemoryFileBackend
::class,
2476 'isMultiMaster' => false,
2477 'readAffinity' => true
2480 'name' => 'multitesting1',
2481 'class' => MemoryFileBackend
::class,
2482 'isMultiMaster' => true
2488 $this->assertEquals(
2490 $be->getReadIndexFromParams( [ 'latest' => 1 ] ),
2491 'Reads with "latest" flag use backend 1'
2493 $this->assertEquals(
2495 $be->getReadIndexFromParams( [ 'latest' => 0 ] ),
2496 'Reads without "latest" flag use backend 0'
2499 $p = 'container/test-cont/file.txt';
2500 $be->backends
[0]->quickCreate( [
2501 'dst' => "mwstore://multitesting0/$p", 'content' => 'cattitude' ] );
2502 $be->backends
[1]->quickCreate( [
2503 'dst' => "mwstore://multitesting1/$p", 'content' => 'princess of power' ] );
2505 $this->assertEquals(
2507 $be->getFileContents( [ 'src' => "mwstore://localtesting/$p" ] ),
2508 "Non-latest read came from backend 0"
2510 $this->assertEquals(
2511 'princess of power',
2512 $be->getFileContents( [ 'src' => "mwstore://localtesting/$p", 'latest' => 1 ] ),
2513 "Latest read came from backend1"
2517 public function testAsyncWrites() {
2518 $be = TestingAccessWrapper
::newFromObject(
2519 new FileBackendMultiWrite( [
2520 'name' => 'localtesting',
2521 'wikiId' => wfWikiID() . mt_rand(),
2524 'name' => 'multitesting0',
2525 'class' => MemoryFileBackend
::class,
2526 'isMultiMaster' => false
2529 'name' => 'multitesting1',
2530 'class' => MemoryFileBackend
::class,
2531 'isMultiMaster' => true
2534 'replication' => 'async'
2538 $this->setMwGlobals( 'wgCommandLineMode', false );
2540 $p = 'container/test-cont/file.txt';
2542 'dst' => "mwstore://localtesting/$p", 'content' => 'cattitude' ] );
2544 $this->assertEquals(
2546 $be->backends
[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
2547 "File not yet written to backend 0"
2549 $this->assertEquals(
2551 $be->backends
[1]->getFileContents( [ 'src' => "mwstore://multitesting1/$p" ] ),
2552 "File already written to backend 1"
2555 DeferredUpdates
::doUpdates();
2557 $this->assertEquals(
2559 $be->backends
[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
2560 "File now written to backend 0"
2564 public function testSanitizeOpHeaders() {
2565 $be = TestingAccessWrapper
::newFromObject( new MemoryFileBackend( [
2566 'name' => 'localtesting',
2567 'wikiId' => wfWikiID()
2572 'content-Disposition' => FileBackend
::makeContentDisposition( 'inline', 'name' ),
2573 'Content-dUration' => 25.6,
2574 'X-LONG-VALUE' => str_pad( '0', 300 ),
2575 'CONTENT-LENGTH' => 855055,
2580 'content-disposition' => FileBackend
::makeContentDisposition( 'inline', 'name' ),
2581 'content-duration' => 25.6,
2582 'content-length' => 855055
2586 Wikimedia\
suppressWarnings();
2587 $actual = $be->sanitizeOpHeaders( $input );
2588 Wikimedia\restoreWarnings
();
2590 $this->assertEquals( $expected, $actual, "Header sanitized properly" );
2594 private function listToArray( $iter ) {
2595 return is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2598 // test helper wrapper for backend prepare() function
2599 private function prepare( array $params ) {
2600 return $this->backend
->prepare( $params );
2603 // test helper wrapper for backend prepare() function
2604 private function create( array $params ) {
2605 $params['op'] = 'create';
2607 return $this->backend
->doQuickOperations( [ $params ] );
2610 function tearDownFiles() {
2611 $containers = [ 'unittest-cont1', 'unittest-cont2', 'unittest-cont-bad' ];
2612 foreach ( $containers as $container ) {
2613 $this->deleteFiles( $container );
2617 private function deleteFiles( $container ) {
2618 $base = self
::baseStorePath();
2619 $iter = $this->backend
->getFileList( [ 'dir' => "$base/$container" ] );
2621 foreach ( $iter as $file ) {
2622 $this->backend
->quickDelete( [ 'src' => "$base/$container/$file" ] );
2624 // free the directory, to avoid Permission denied under windows on rmdir
2627 $this->backend
->clean( [ 'dir' => "$base/$container", 'recursive' => 1 ] );
2630 function assertBackendPathsConsistent( array $paths ) {
2631 if ( $this->backend
instanceof FileBackendMultiWrite
) {
2632 $status = $this->backend
->consistencyCheck( $paths );
2633 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2637 function assertGoodStatus( StatusValue
$status, $msg ) {
2638 $this->assertEquals( print_r( [], 1 ), print_r( $status->getErrors(), 1 ), $msg );