11 * @covers CreateFileOp
12 * @covers DeleteFileOp
13 * @covers DescribeFileOp
15 * @covers FSFileBackend
16 * @covers FSFileBackendDirList
17 * @covers FSFileBackendFileList
18 * @covers FSFileBackendList
19 * @covers FSFileOpHandle
20 * @covers FileBackendDBRepoWrapper
21 * @covers FileBackendError
22 * @covers FileBackendGroup
23 * @covers FileBackendMultiWrite
24 * @covers FileBackendStore
25 * @covers FileBackendStoreOpHandle
26 * @covers FileBackendStoreShardDirIterator
27 * @covers FileBackendStoreShardFileIterator
28 * @covers FileBackendStoreShardListIterator
32 * @covers HTTPFileStreamer
33 * @covers LockManagerGroup
34 * @covers MemoryFileBackend
36 * @covers MySqlLockManager
37 * @covers NullFileJournal
42 * @covers FSLockManager
44 * @covers NullLockManager
46 class FileBackendTest
extends MediaWikiTestCase
{
48 /** @var FileBackend */
50 /** @var FileBackendMultiWrite */
51 private $multiBackend;
52 /** @var FSFileBackend */
53 public $singleBackend;
54 private static $backendToUse;
56 protected function setUp() {
57 global $wgFileBackends;
59 $tmpDir = $this->getNewTempDirectory();
60 if ( $this->getCliArg( 'use-filebackend' ) ) {
61 if ( self
::$backendToUse ) {
62 $this->singleBackend
= self
::$backendToUse;
64 $name = $this->getCliArg( 'use-filebackend' );
66 foreach ( $wgFileBackends as $conf ) {
67 if ( $conf['name'] == $name ) {
72 $useConfig['name'] = 'localtesting'; // swap name
73 $useConfig['shardViaHashLevels'] = [ // test sharding
74 'unittest-cont1' => [ 'levels' => 1, 'base' => 16, 'repeat' => 1 ]
76 if ( isset( $useConfig['fileJournal'] ) ) {
77 $useConfig['fileJournal'] = FileJournal
::factory( $useConfig['fileJournal'], $name );
79 $useConfig['lockManager'] = LockManagerGroup
::singleton()->get( $useConfig['lockManager'] );
80 $class = $useConfig['class'];
81 self
::$backendToUse = new $class( $useConfig );
82 $this->singleBackend
= self
::$backendToUse;
85 $this->singleBackend
= new FSFileBackend( [
86 'name' => 'localtesting',
87 'lockManager' => LockManagerGroup
::singleton()->get( 'fsLockManager' ),
88 'wikiId' => wfWikiID(),
90 'unittest-cont1' => "{$tmpDir}/localtesting-cont1",
91 'unittest-cont2' => "{$tmpDir}/localtesting-cont2" ]
94 $this->multiBackend
= new FileBackendMultiWrite( [
95 'name' => 'localtesting',
96 'lockManager' => LockManagerGroup
::singleton()->get( 'fsLockManager' ),
97 'parallelize' => 'implicit',
98 'wikiId' => wfWikiID() . wfRandomString(),
101 'name' => 'localmultitesting1',
102 'class' => 'FSFileBackend',
103 'containerPaths' => [
104 'unittest-cont1' => "{$tmpDir}/localtestingmulti1-cont1",
105 'unittest-cont2' => "{$tmpDir}/localtestingmulti1-cont2" ],
106 'isMultiMaster' => false
109 'name' => 'localmultitesting2',
110 'class' => 'FSFileBackend',
111 'containerPaths' => [
112 'unittest-cont1' => "{$tmpDir}/localtestingmulti2-cont1",
113 'unittest-cont2' => "{$tmpDir}/localtestingmulti2-cont2" ],
114 'isMultiMaster' => true
120 private static function baseStorePath() {
121 return 'mwstore://localtesting';
124 private function backendClass() {
125 return get_class( $this->backend
);
129 * @dataProvider provider_testIsStoragePath
131 public function testIsStoragePath( $path, $isStorePath ) {
132 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
133 "FileBackend::isStoragePath on path '$path'" );
136 public static function provider_testIsStoragePath() {
138 [ 'mwstore://', true ],
139 [ 'mwstore://backend', true ],
140 [ 'mwstore://backend/container', true ],
141 [ 'mwstore://backend/container/', true ],
142 [ 'mwstore://backend/container/path', true ],
143 [ 'mwstore://backend//container/', true ],
144 [ 'mwstore://backend//container//', true ],
145 [ 'mwstore://backend//container//path', true ],
146 [ 'mwstore:///', true ],
147 [ 'mwstore:/', false ],
148 [ 'mwstore:', false ],
153 * @dataProvider provider_testSplitStoragePath
155 public function testSplitStoragePath( $path, $res ) {
156 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
157 "FileBackend::splitStoragePath on path '$path'" );
160 public static function provider_testSplitStoragePath() {
162 [ 'mwstore://backend/container', [ 'backend', 'container', '' ] ],
163 [ 'mwstore://backend/container/', [ 'backend', 'container', '' ] ],
164 [ 'mwstore://backend/container/path', [ 'backend', 'container', 'path' ] ],
165 [ 'mwstore://backend/container//path', [ 'backend', 'container', '/path' ] ],
166 [ 'mwstore://backend//container/path', [ null, null, null ] ],
167 [ 'mwstore://backend//container//path', [ null, null, null ] ],
168 [ 'mwstore://', [ null, null, null ] ],
169 [ 'mwstore://backend', [ null, null, null ] ],
170 [ 'mwstore:///', [ null, null, null ] ],
171 [ 'mwstore:/', [ null, null, null ] ],
172 [ 'mwstore:', [ null, null, null ] ]
177 * @dataProvider provider_normalizeStoragePath
179 public function testNormalizeStoragePath( $path, $res ) {
180 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
181 "FileBackend::normalizeStoragePath on path '$path'" );
184 public static function provider_normalizeStoragePath() {
186 [ 'mwstore://backend/container', 'mwstore://backend/container' ],
187 [ 'mwstore://backend/container/', 'mwstore://backend/container' ],
188 [ 'mwstore://backend/container/path', 'mwstore://backend/container/path' ],
189 [ 'mwstore://backend/container//path', 'mwstore://backend/container/path' ],
190 [ 'mwstore://backend/container///path', 'mwstore://backend/container/path' ],
192 'mwstore://backend/container///path//to///obj',
193 'mwstore://backend/container/path/to/obj'
195 [ 'mwstore://', null ],
196 [ 'mwstore://backend', null ],
197 [ 'mwstore://backend//container/path', null ],
198 [ 'mwstore://backend//container//path', null ],
199 [ 'mwstore:///', null ],
200 [ 'mwstore:/', null ],
201 [ 'mwstore:', null ],
206 * @dataProvider provider_testParentStoragePath
208 public function testParentStoragePath( $path, $res ) {
209 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
210 "FileBackend::parentStoragePath on path '$path'" );
213 public static function provider_testParentStoragePath() {
215 [ 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ],
216 [ 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ],
217 [ 'mwstore://backend/container/path', 'mwstore://backend/container' ],
218 [ 'mwstore://backend/container', null ],
219 [ 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ],
220 [ 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ],
221 [ 'mwstore://backend/container/path/', 'mwstore://backend/container' ],
222 [ 'mwstore://backend/container/', null ],
227 * @dataProvider provider_testExtensionFromPath
229 public function testExtensionFromPath( $path, $res ) {
230 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
231 "FileBackend::extensionFromPath on path '$path'" );
234 public static function provider_testExtensionFromPath() {
236 [ 'mwstore://backend/container/path.txt', 'txt' ],
237 [ 'mwstore://backend/container/path.svg.png', 'png' ],
238 [ 'mwstore://backend/container/path', '' ],
239 [ 'mwstore://backend/container/path.', '' ],
244 * @dataProvider provider_testStore
246 public function testStore( $op ) {
247 $this->addTmpFiles( $op['src'] );
249 $this->backend
= $this->singleBackend
;
250 $this->tearDownFiles();
251 $this->doTestStore( $op );
252 $this->tearDownFiles();
254 $this->backend
= $this->multiBackend
;
255 $this->tearDownFiles();
256 $this->doTestStore( $op );
257 $this->tearDownFiles();
260 private function doTestStore( $op ) {
261 $backendName = $this->backendClass();
263 $source = $op['src'];
265 $this->prepare( [ 'dir' => dirname( $dest ) ] );
267 file_put_contents( $source, "Unit test file" );
269 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
270 $this->backend
->store( $op );
273 $status = $this->backend
->doOperation( $op );
275 $this->assertGoodStatus( $status,
276 "Store from $source to $dest succeeded without warnings ($backendName)." );
277 $this->assertEquals( true, $status->isOK(),
278 "Store from $source to $dest succeeded ($backendName)." );
279 $this->assertEquals( [ 0 => true ], $status->success
,
280 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
281 $this->assertEquals( true, file_exists( $source ),
282 "Source file $source still exists ($backendName)." );
283 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $dest ] ),
284 "Destination file $dest exists ($backendName)." );
286 $this->assertEquals( filesize( $source ),
287 $this->backend
->getFileSize( [ 'src' => $dest ] ),
288 "Destination file $dest has correct size ($backendName)." );
290 $props1 = FSFile
::getPropsFromPath( $source );
291 $props2 = $this->backend
->getFileProps( [ 'src' => $dest ] );
292 $this->assertEquals( $props1, $props2,
293 "Source and destination have the same props ($backendName)." );
295 $this->assertBackendPathsConsistent( [ $dest ] );
298 public static function provider_testStore() {
301 $tmpName = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
302 $toPath = self
::baseStorePath() . '/unittest-cont1/e/fun/obj1.txt';
303 $op = [ 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath ];
307 $op2['overwrite'] = true;
311 $op3['overwriteSame'] = true;
318 * @dataProvider provider_testCopy
320 public function testCopy( $op ) {
321 $this->backend
= $this->singleBackend
;
322 $this->tearDownFiles();
323 $this->doTestCopy( $op );
324 $this->tearDownFiles();
326 $this->backend
= $this->multiBackend
;
327 $this->tearDownFiles();
328 $this->doTestCopy( $op );
329 $this->tearDownFiles();
332 private function doTestCopy( $op ) {
333 $backendName = $this->backendClass();
335 $source = $op['src'];
337 $this->prepare( [ 'dir' => dirname( $source ) ] );
338 $this->prepare( [ 'dir' => dirname( $dest ) ] );
340 if ( isset( $op['ignoreMissingSource'] ) ) {
341 $status = $this->backend
->doOperation( $op );
342 $this->assertGoodStatus( $status,
343 "Move from $source to $dest succeeded without warnings ($backendName)." );
344 $this->assertEquals( [ 0 => true ], $status->success
,
345 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
346 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $source ] ),
347 "Source file $source does not exist ($backendName)." );
348 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $dest ] ),
349 "Destination file $dest does not exist ($backendName)." );
354 $status = $this->backend
->doOperation(
355 [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ] );
356 $this->assertGoodStatus( $status,
357 "Creation of file at $source succeeded ($backendName)." );
359 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
360 $this->backend
->copy( $op );
363 $status = $this->backend
->doOperation( $op );
365 $this->assertGoodStatus( $status,
366 "Copy from $source to $dest succeeded without warnings ($backendName)." );
367 $this->assertEquals( true, $status->isOK(),
368 "Copy from $source to $dest succeeded ($backendName)." );
369 $this->assertEquals( [ 0 => true ], $status->success
,
370 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
371 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $source ] ),
372 "Source file $source still exists ($backendName)." );
373 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $dest ] ),
374 "Destination file $dest exists after copy ($backendName)." );
377 $this->backend
->getFileSize( [ 'src' => $source ] ),
378 $this->backend
->getFileSize( [ 'src' => $dest ] ),
379 "Destination file $dest has correct size ($backendName)." );
381 $props1 = $this->backend
->getFileProps( [ 'src' => $source ] );
382 $props2 = $this->backend
->getFileProps( [ 'src' => $dest ] );
383 $this->assertEquals( $props1, $props2,
384 "Source and destination have the same props ($backendName)." );
386 $this->assertBackendPathsConsistent( [ $source, $dest ] );
389 public static function provider_testCopy() {
392 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
393 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
395 $op = [ 'op' => 'copy', 'src' => $source, 'dst' => $dest ];
403 $op2['overwrite'] = true;
411 $op2['overwriteSame'] = true;
419 $op2['ignoreMissingSource'] = true;
427 $op2['ignoreMissingSource'] = true;
430 self
::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
438 * @dataProvider provider_testMove
440 public function testMove( $op ) {
441 $this->backend
= $this->singleBackend
;
442 $this->tearDownFiles();
443 $this->doTestMove( $op );
444 $this->tearDownFiles();
446 $this->backend
= $this->multiBackend
;
447 $this->tearDownFiles();
448 $this->doTestMove( $op );
449 $this->tearDownFiles();
452 private function doTestMove( $op ) {
453 $backendName = $this->backendClass();
455 $source = $op['src'];
457 $this->prepare( [ 'dir' => dirname( $source ) ] );
458 $this->prepare( [ 'dir' => dirname( $dest ) ] );
460 if ( isset( $op['ignoreMissingSource'] ) ) {
461 $status = $this->backend
->doOperation( $op );
462 $this->assertGoodStatus( $status,
463 "Move from $source to $dest succeeded without warnings ($backendName)." );
464 $this->assertEquals( [ 0 => true ], $status->success
,
465 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
466 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $source ] ),
467 "Source file $source does not exist ($backendName)." );
468 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $dest ] ),
469 "Destination file $dest does not exist ($backendName)." );
474 $status = $this->backend
->doOperation(
475 [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ] );
476 $this->assertGoodStatus( $status,
477 "Creation of file at $source succeeded ($backendName)." );
479 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
480 $this->backend
->copy( $op );
483 $status = $this->backend
->doOperation( $op );
484 $this->assertGoodStatus( $status,
485 "Move from $source to $dest succeeded without warnings ($backendName)." );
486 $this->assertEquals( true, $status->isOK(),
487 "Move from $source to $dest succeeded ($backendName)." );
488 $this->assertEquals( [ 0 => true ], $status->success
,
489 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
490 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $source ] ),
491 "Source file $source does not still exists ($backendName)." );
492 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $dest ] ),
493 "Destination file $dest exists after move ($backendName)." );
495 $this->assertNotEquals(
496 $this->backend
->getFileSize( [ 'src' => $source ] ),
497 $this->backend
->getFileSize( [ 'src' => $dest ] ),
498 "Destination file $dest has correct size ($backendName)." );
500 $props1 = $this->backend
->getFileProps( [ 'src' => $source ] );
501 $props2 = $this->backend
->getFileProps( [ 'src' => $dest ] );
502 $this->assertEquals( false, $props1['fileExists'],
503 "Source file does not exist accourding to props ($backendName)." );
504 $this->assertEquals( true, $props2['fileExists'],
505 "Destination file exists accourding to props ($backendName)." );
507 $this->assertBackendPathsConsistent( [ $source, $dest ] );
510 public static function provider_testMove() {
513 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
514 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
516 $op = [ 'op' => 'move', 'src' => $source, 'dst' => $dest ];
524 $op2['overwrite'] = true;
532 $op2['overwriteSame'] = true;
540 $op2['ignoreMissingSource'] = true;
548 $op2['ignoreMissingSource'] = true;
551 self
::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
559 * @dataProvider provider_testDelete
561 public function testDelete( $op, $withSource, $okStatus ) {
562 $this->backend
= $this->singleBackend
;
563 $this->tearDownFiles();
564 $this->doTestDelete( $op, $withSource, $okStatus );
565 $this->tearDownFiles();
567 $this->backend
= $this->multiBackend
;
568 $this->tearDownFiles();
569 $this->doTestDelete( $op, $withSource, $okStatus );
570 $this->tearDownFiles();
573 private function doTestDelete( $op, $withSource, $okStatus ) {
574 $backendName = $this->backendClass();
576 $source = $op['src'];
577 $this->prepare( [ 'dir' => dirname( $source ) ] );
580 $status = $this->backend
->doOperation(
581 [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ] );
582 $this->assertGoodStatus( $status,
583 "Creation of file at $source succeeded ($backendName)." );
586 $status = $this->backend
->doOperation( $op );
588 $this->assertGoodStatus( $status,
589 "Deletion of file at $source succeeded without warnings ($backendName)." );
590 $this->assertEquals( true, $status->isOK(),
591 "Deletion of file at $source succeeded ($backendName)." );
592 $this->assertEquals( [ 0 => true ], $status->success
,
593 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
595 $this->assertEquals( false, $status->isOK(),
596 "Deletion of file at $source failed ($backendName)." );
599 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $source ] ),
600 "Source file $source does not exist after move ($backendName)." );
603 $this->backend
->getFileSize( [ 'src' => $source ] ),
604 "Source file $source has correct size (false) ($backendName)." );
606 $props1 = $this->backend
->getFileProps( [ 'src' => $source ] );
607 $this->assertFalse( $props1['fileExists'],
608 "Source file $source does not exist according to props ($backendName)." );
610 $this->assertBackendPathsConsistent( [ $source ] );
613 public static function provider_testDelete() {
616 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
618 $op = [ 'op' => 'delete', 'src' => $source ];
627 false, // without source
631 $op['ignoreMissingSource'] = true;
634 false, // without source
638 $op['ignoreMissingSource'] = true;
639 $op['src'] = self
::baseStorePath() . '/unittest-cont-bad/e/file.txt';
642 false, // without source
650 * @dataProvider provider_testDescribe
652 public function testDescribe( $op, $withSource, $okStatus ) {
653 $this->backend
= $this->singleBackend
;
654 $this->tearDownFiles();
655 $this->doTestDescribe( $op, $withSource, $okStatus );
656 $this->tearDownFiles();
658 $this->backend
= $this->multiBackend
;
659 $this->tearDownFiles();
660 $this->doTestDescribe( $op, $withSource, $okStatus );
661 $this->tearDownFiles();
664 private function doTestDescribe( $op, $withSource, $okStatus ) {
665 $backendName = $this->backendClass();
667 $source = $op['src'];
668 $this->prepare( [ 'dir' => dirname( $source ) ] );
671 $status = $this->backend
->doOperation(
672 [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source,
673 'headers' => [ 'Content-Disposition' => 'xxx' ] ] );
674 $this->assertGoodStatus( $status,
675 "Creation of file at $source succeeded ($backendName)." );
676 if ( $this->backend
->hasFeatures( FileBackend
::ATTR_HEADERS
) ) {
677 $attr = $this->backend
->getFileXAttributes( [ 'src' => $source ] );
678 $this->assertHasHeaders( [ 'Content-Disposition' => 'xxx' ], $attr );
681 $status = $this->backend
->describe( [ 'src' => $source,
682 'headers' => [ 'Content-Disposition' => '' ] ] ); // remove
683 $this->assertGoodStatus( $status,
684 "Removal of header for $source succeeded ($backendName)." );
686 if ( $this->backend
->hasFeatures( FileBackend
::ATTR_HEADERS
) ) {
687 $attr = $this->backend
->getFileXAttributes( [ 'src' => $source ] );
688 $this->assertFalse( isset( $attr['headers']['content-disposition'] ),
689 "File 'Content-Disposition' header removed." );
693 $status = $this->backend
->doOperation( $op );
695 $this->assertGoodStatus( $status,
696 "Describe of file at $source succeeded without warnings ($backendName)." );
697 $this->assertEquals( true, $status->isOK(),
698 "Describe of file at $source succeeded ($backendName)." );
699 $this->assertEquals( [ 0 => true ], $status->success
,
700 "Describe of file at $source has proper 'success' field in Status ($backendName)." );
701 if ( $this->backend
->hasFeatures( FileBackend
::ATTR_HEADERS
) ) {
702 $attr = $this->backend
->getFileXAttributes( [ 'src' => $source ] );
703 $this->assertHasHeaders( $op['headers'], $attr );
706 $this->assertEquals( false, $status->isOK(),
707 "Describe of file at $source failed ($backendName)." );
710 $this->assertBackendPathsConsistent( [ $source ] );
713 private function assertHasHeaders( array $headers, array $attr ) {
714 foreach ( $headers as $n => $v ) {
716 $this->assertTrue( isset( $attr['headers'][strtolower( $n )] ),
717 "File has '$n' header." );
718 $this->assertEquals( $v, $attr['headers'][strtolower( $n )],
719 "File has '$n' header value." );
721 $this->assertFalse( isset( $attr['headers'][strtolower( $n )] ),
722 "File does not have '$n' header." );
727 public static function provider_testDescribe() {
730 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
732 $op = [ 'op' => 'describe', 'src' => $source,
733 'headers' => [ 'Content-Disposition' => 'inline' ], ];
742 false, // without source
750 * @dataProvider provider_testCreate
752 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
753 $this->backend
= $this->singleBackend
;
754 $this->tearDownFiles();
755 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
756 $this->tearDownFiles();
758 $this->backend
= $this->multiBackend
;
759 $this->tearDownFiles();
760 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
761 $this->tearDownFiles();
764 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
765 $backendName = $this->backendClass();
768 $this->prepare( [ 'dir' => dirname( $dest ) ] );
770 $oldText = 'blah...blah...waahwaah';
771 if ( $alreadyExists ) {
772 $status = $this->backend
->doOperation(
773 [ 'op' => 'create', 'content' => $oldText, 'dst' => $dest ] );
774 $this->assertGoodStatus( $status,
775 "Creation of file at $dest succeeded ($backendName)." );
778 $status = $this->backend
->doOperation( $op );
780 $this->assertGoodStatus( $status,
781 "Creation of file at $dest succeeded without warnings ($backendName)." );
782 $this->assertEquals( true, $status->isOK(),
783 "Creation of file at $dest succeeded ($backendName)." );
784 $this->assertEquals( [ 0 => true ], $status->success
,
785 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
787 $this->assertEquals( false, $status->isOK(),
788 "Creation of file at $dest failed ($backendName)." );
791 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $dest ] ),
792 "Destination file $dest exists after creation ($backendName)." );
794 $props1 = $this->backend
->getFileProps( [ 'src' => $dest ] );
795 $this->assertEquals( true, $props1['fileExists'],
796 "Destination file $dest exists according to props ($backendName)." );
797 if ( $okStatus ) { // file content is what we saved
798 $this->assertEquals( $newSize, $props1['size'],
799 "Destination file $dest has expected size according to props ($backendName)." );
800 $this->assertEquals( $newSize,
801 $this->backend
->getFileSize( [ 'src' => $dest ] ),
802 "Destination file $dest has correct size ($backendName)." );
803 } else { // file content is some other previous text
804 $this->assertEquals( strlen( $oldText ), $props1['size'],
805 "Destination file $dest has original size according to props ($backendName)." );
806 $this->assertEquals( strlen( $oldText ),
807 $this->backend
->getFileSize( [ 'src' => $dest ] ),
808 "Destination file $dest has original size according to props ($backendName)." );
811 $this->assertBackendPathsConsistent( [ $dest ] );
815 * @dataProvider provider_testCreate
817 public static function provider_testCreate() {
820 $dest = self
::baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
822 $op = [ 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest ];
825 false, // no dest already exists
827 strlen( $op['content'] )
831 $op2['content'] = "\n";
834 false, // no dest already exists
836 strlen( $op2['content'] )
840 $op2['content'] = "fsf\n waf 3kt";
843 true, // dest already exists
845 strlen( $op2['content'] )
849 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
850 $op2['overwrite'] = true;
853 true, // dest already exists
855 strlen( $op2['content'] )
859 $op2['content'] = "39qjmg3-qg";
860 $op2['overwriteSame'] = true;
863 true, // dest already exists
865 strlen( $op2['content'] )
871 public function testDoQuickOperations() {
872 $this->backend
= $this->singleBackend
;
873 $this->doTestDoQuickOperations();
874 $this->tearDownFiles();
876 $this->backend
= $this->multiBackend
;
877 $this->doTestDoQuickOperations();
878 $this->tearDownFiles();
881 private function doTestDoQuickOperations() {
882 $backendName = $this->backendClass();
884 $base = self
::baseStorePath();
886 "$base/unittest-cont1/e/fileA.a",
887 "$base/unittest-cont1/e/fileB.a",
888 "$base/unittest-cont1/e/fileC.a"
892 foreach ( $files as $path ) {
893 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
894 $this->assertGoodStatus( $status,
895 "Preparing $path succeeded without warnings ($backendName)." );
896 $createOps[] = [ 'op' => 'create', 'dst' => $path, 'content' => mt_rand( 0, 50000 ) ];
897 $copyOps[] = [ 'op' => 'copy', 'src' => $path, 'dst' => "$path-2" ];
898 $moveOps[] = [ 'op' => 'move', 'src' => "$path-2", 'dst' => "$path-3" ];
899 $purgeOps[] = [ 'op' => 'delete', 'src' => $path ];
900 $purgeOps[] = [ 'op' => 'delete', 'src' => "$path-3" ];
902 $purgeOps[] = [ 'op' => 'null' ];
904 $this->assertGoodStatus(
905 $this->backend
->doQuickOperations( $createOps ),
906 "Creation of source files succeeded ($backendName)." );
907 foreach ( $files as $file ) {
908 $this->assertTrue( $this->backend
->fileExists( [ 'src' => $file ] ),
909 "File $file exists." );
912 $this->assertGoodStatus(
913 $this->backend
->doQuickOperations( $copyOps ),
914 "Quick copy of source files succeeded ($backendName)." );
915 foreach ( $files as $file ) {
916 $this->assertTrue( $this->backend
->fileExists( [ 'src' => "$file-2" ] ),
917 "File $file-2 exists." );
920 $this->assertGoodStatus(
921 $this->backend
->doQuickOperations( $moveOps ),
922 "Quick move of source files succeeded ($backendName)." );
923 foreach ( $files as $file ) {
924 $this->assertTrue( $this->backend
->fileExists( [ 'src' => "$file-3" ] ),
925 "File $file-3 move in." );
926 $this->assertFalse( $this->backend
->fileExists( [ 'src' => "$file-2" ] ),
927 "File $file-2 moved away." );
930 $this->assertGoodStatus(
931 $this->backend
->quickCopy( [ 'src' => $files[0], 'dst' => $files[0] ] ),
932 "Copy of file {$files[0]} over itself succeeded ($backendName)." );
933 $this->assertTrue( $this->backend
->fileExists( [ 'src' => $files[0] ] ),
934 "File {$files[0]} still exists." );
936 $this->assertGoodStatus(
937 $this->backend
->quickMove( [ 'src' => $files[0], 'dst' => $files[0] ] ),
938 "Move of file {$files[0]} over itself succeeded ($backendName)." );
939 $this->assertTrue( $this->backend
->fileExists( [ 'src' => $files[0] ] ),
940 "File {$files[0]} still exists." );
942 $this->assertGoodStatus(
943 $this->backend
->doQuickOperations( $purgeOps ),
944 "Quick deletion of source files succeeded ($backendName)." );
945 foreach ( $files as $file ) {
946 $this->assertFalse( $this->backend
->fileExists( [ 'src' => $file ] ),
947 "File $file purged." );
948 $this->assertFalse( $this->backend
->fileExists( [ 'src' => "$file-3" ] ),
949 "File $file-3 purged." );
954 * @dataProvider provider_testConcatenate
956 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
957 $this->backend
= $this->singleBackend
;
958 $this->tearDownFiles();
959 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
960 $this->tearDownFiles();
962 $this->backend
= $this->multiBackend
;
963 $this->tearDownFiles();
964 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
965 $this->tearDownFiles();
968 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
969 $backendName = $this->backendClass();
974 foreach ( $srcs as $i => $source ) {
975 $this->prepare( [ 'dir' => dirname( $source ) ] );
977 'op' => 'create', // operation
978 'dst' => $source, // source
979 'content' => $srcsContent[$i]
981 $expContent .= $srcsContent[$i];
983 $status = $this->backend
->doOperations( $ops );
985 $this->assertGoodStatus( $status,
986 "Creation of source files succeeded ($backendName)." );
988 $dest = $params['dst'] = $this->getNewTempFile();
989 if ( $alreadyExists ) {
990 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
991 $this->assertEquals( true, $ok,
992 "Creation of file at $dest succeeded ($backendName)." );
994 $ok = file_put_contents( $dest, '' ) !== false;
995 $this->assertEquals( true, $ok,
996 "Creation of 0-byte file at $dest succeeded ($backendName)." );
999 // Combine the files into one
1000 $status = $this->backend
->concatenate( $params );
1002 $this->assertGoodStatus( $status,
1003 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
1004 $this->assertEquals( true, $status->isOK(),
1005 "Creation of concat file at $dest succeeded ($backendName)." );
1007 $this->assertEquals( false, $status->isOK(),
1008 "Creation of concat file at $dest failed ($backendName)." );
1012 $this->assertEquals( true, is_file( $dest ),
1013 "Dest concat file $dest exists after creation ($backendName)." );
1015 $this->assertEquals( true, is_file( $dest ),
1016 "Dest concat file $dest exists after failed creation ($backendName)." );
1019 $contents = file_get_contents( $dest );
1020 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
1023 $this->assertEquals( $expContent, $contents,
1024 "Concat file at $dest has correct contents ($backendName)." );
1026 $this->assertNotEquals( $expContent, $contents,
1027 "Concat file at $dest has correct contents ($backendName)." );
1031 public static function provider_testConcatenate() {
1035 self
::baseStorePath() . '/unittest-cont1/e/file1.txt',
1036 self
::baseStorePath() . '/unittest-cont1/e/file2.txt',
1037 self
::baseStorePath() . '/unittest-cont1/e/file3.txt',
1038 self
::baseStorePath() . '/unittest-cont1/e/file4.txt',
1039 self
::baseStorePath() . '/unittest-cont1/e/file5.txt',
1040 self
::baseStorePath() . '/unittest-cont1/e/file6.txt',
1041 self
::baseStorePath() . '/unittest-cont1/e/file7.txt',
1042 self
::baseStorePath() . '/unittest-cont1/e/file8.txt',
1043 self
::baseStorePath() . '/unittest-cont1/e/file9.txt',
1044 self
::baseStorePath() . '/unittest-cont1/e/file10.txt'
1058 $params = [ 'srcs' => $srcs ];
1061 $params, // operation
1063 $content, // content for each source
1064 false, // no dest already exists
1069 $params, // operation
1071 $content, // content for each source
1072 true, // dest already exists
1080 * @dataProvider provider_testGetFileStat
1082 public function testGetFileStat( $path, $content, $alreadyExists ) {
1083 $this->backend
= $this->singleBackend
;
1084 $this->tearDownFiles();
1085 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1086 $this->tearDownFiles();
1088 $this->backend
= $this->multiBackend
;
1089 $this->tearDownFiles();
1090 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1091 $this->tearDownFiles();
1094 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
1095 $backendName = $this->backendClass();
1097 if ( $alreadyExists ) {
1098 $this->prepare( [ 'dir' => dirname( $path ) ] );
1099 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1100 $this->assertGoodStatus( $status,
1101 "Creation of file at $path succeeded ($backendName)." );
1103 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1104 $time = $this->backend
->getFileTimestamp( [ 'src' => $path ] );
1105 $stat = $this->backend
->getFileStat( [ 'src' => $path ] );
1107 $this->assertEquals( strlen( $content ), $size,
1108 "Correct file size of '$path'" );
1109 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1110 "Correct file timestamp of '$path'" );
1112 $size = $stat['size'];
1113 $time = $stat['mtime'];
1114 $this->assertEquals( strlen( $content ), $size,
1115 "Correct file size of '$path'" );
1116 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
1117 "Correct file timestamp of '$path'" );
1119 $this->backend
->clearCache( [ $path ] );
1121 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1123 $this->assertEquals( strlen( $content ), $size,
1124 "Correct file size of '$path'" );
1126 $this->backend
->preloadCache( [ $path ] );
1128 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1130 $this->assertEquals( strlen( $content ), $size,
1131 "Correct file size of '$path'" );
1133 $size = $this->backend
->getFileSize( [ 'src' => $path ] );
1134 $time = $this->backend
->getFileTimestamp( [ 'src' => $path ] );
1135 $stat = $this->backend
->getFileStat( [ 'src' => $path ] );
1137 $this->assertFalse( $size, "Correct file size of '$path'" );
1138 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1139 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1143 public static function provider_testGetFileStat() {
1146 $base = self
::baseStorePath();
1147 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true ];
1148 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "", true ];
1149 $cases[] = [ "$base/unittest-cont1/e/b/some-diff_file.txt", null, false ];
1155 * @dataProvider provider_testGetFileStat
1157 public function testStreamFile( $path, $content, $alreadyExists ) {
1158 $this->backend
= $this->singleBackend
;
1159 $this->tearDownFiles();
1160 $this->doTestStreamFile( $path, $content, $alreadyExists );
1161 $this->tearDownFiles();
1163 $this->backend
= $this->multiBackend
;
1164 $this->tearDownFiles();
1165 $this->doTestStreamFile( $path, $content, $alreadyExists );
1166 $this->tearDownFiles();
1169 private function doTestStreamFile( $path, $content ) {
1170 $backendName = $this->backendClass();
1172 if ( $content !== null ) {
1173 $this->prepare( [ 'dir' => dirname( $path ) ] );
1174 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1175 $this->assertGoodStatus( $status,
1176 "Creation of file at $path succeeded ($backendName)." );
1179 $this->backend
->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
1180 $data = ob_get_contents();
1183 $this->assertEquals( $content, $data, "Correct content streamed from '$path'" );
1184 } else { // 404 case
1186 $this->backend
->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
1187 $data = ob_get_contents();
1190 $this->assertRegExp( '#<h1>File not found</h1>#', $data,
1191 "Correct content streamed from '$path' ($backendName)" );
1195 public static function provider_testStreamFile() {
1198 $base = self
::baseStorePath();
1199 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
1200 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", null ];
1205 public function testStreamFileRange() {
1206 $this->backend
= $this->singleBackend
;
1207 $this->tearDownFiles();
1208 $this->doTestStreamFileRange();
1209 $this->tearDownFiles();
1211 $this->backend
= $this->multiBackend
;
1212 $this->tearDownFiles();
1213 $this->doTestStreamFileRange();
1214 $this->tearDownFiles();
1217 private function doTestStreamFileRange() {
1218 $backendName = $this->backendClass();
1220 $base = self
::baseStorePath();
1221 $path = "$base/unittest-cont1/e/b/z/range_file.txt";
1222 $content = "0123456789ABCDEF";
1224 $this->prepare( [ 'dir' => dirname( $path ) ] );
1225 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1226 $this->assertGoodStatus( $status,
1227 "Creation of file at $path succeeded ($backendName)." );
1231 'bytes=0-3' => '0123',
1232 'bytes=4-8' => '45678',
1233 'bytes=15-15' => 'F',
1234 'bytes=14-15' => 'EF',
1235 'bytes=-5' => 'BCDEF',
1237 'bytes=10-16' => 'ABCDEF',
1238 'bytes=10-99' => 'ABCDEF',
1241 foreach ( $ranges as $range => $chunk ) {
1243 $this->backend
->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1,
1244 'options' => [ 'range' => $range ] ] );
1245 $data = ob_get_contents();
1248 $this->assertEquals( $chunk, $data, "Correct chunk streamed from '$path' for '$range'" );
1253 * @dataProvider provider_testGetFileContents
1255 public function testGetFileContents( $source, $content ) {
1256 $this->backend
= $this->singleBackend
;
1257 $this->tearDownFiles();
1258 $this->doTestGetFileContents( $source, $content );
1259 $this->tearDownFiles();
1261 $this->backend
= $this->multiBackend
;
1262 $this->tearDownFiles();
1263 $this->doTestGetFileContents( $source, $content );
1264 $this->tearDownFiles();
1267 private function doTestGetFileContents( $source, $content ) {
1268 $backendName = $this->backendClass();
1270 $srcs = (array)$source;
1271 $content = (array)$content;
1272 foreach ( $srcs as $i => $src ) {
1273 $this->prepare( [ 'dir' => dirname( $src ) ] );
1274 $status = $this->backend
->doOperation(
1275 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1276 $this->assertGoodStatus( $status,
1277 "Creation of file at $src succeeded ($backendName)." );
1280 if ( is_array( $source ) ) {
1281 $contents = $this->backend
->getFileContentsMulti( [ 'srcs' => $source ] );
1282 foreach ( $contents as $path => $data ) {
1283 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1284 $this->assertEquals(
1285 current( $content ),
1287 "Contents of $path is correct ($backendName)."
1291 $this->assertEquals(
1293 array_keys( $contents ),
1294 "Contents in right order ($backendName)."
1296 $this->assertEquals(
1299 "Contents array size correct ($backendName)."
1302 $data = $this->backend
->getFileContents( [ 'src' => $source ] );
1303 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1304 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1308 public static function provider_testGetFileContents() {
1311 $base = self
::baseStorePath();
1312 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
1313 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" ];
1315 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1316 "$base/unittest-cont1/e/a/z.txt" ],
1317 [ "contents xx", "contents xy", "contents xz" ]
1324 * @dataProvider provider_testGetLocalCopy
1326 public function testGetLocalCopy( $source, $content ) {
1327 $this->backend
= $this->singleBackend
;
1328 $this->tearDownFiles();
1329 $this->doTestGetLocalCopy( $source, $content );
1330 $this->tearDownFiles();
1332 $this->backend
= $this->multiBackend
;
1333 $this->tearDownFiles();
1334 $this->doTestGetLocalCopy( $source, $content );
1335 $this->tearDownFiles();
1338 private function doTestGetLocalCopy( $source, $content ) {
1339 $backendName = $this->backendClass();
1341 $srcs = (array)$source;
1342 $content = (array)$content;
1343 foreach ( $srcs as $i => $src ) {
1344 $this->prepare( [ 'dir' => dirname( $src ) ] );
1345 $status = $this->backend
->doOperation(
1346 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1347 $this->assertGoodStatus( $status,
1348 "Creation of file at $src succeeded ($backendName)." );
1351 if ( is_array( $source ) ) {
1352 $tmpFiles = $this->backend
->getLocalCopyMulti( [ 'srcs' => $source ] );
1353 foreach ( $tmpFiles as $path => $tmpFile ) {
1354 $this->assertNotNull( $tmpFile,
1355 "Creation of local copy of $path succeeded ($backendName)." );
1356 $contents = file_get_contents( $tmpFile->getPath() );
1357 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1358 $this->assertEquals(
1359 current( $content ),
1361 "Local copy of $path is correct ($backendName)."
1365 $this->assertEquals(
1367 array_keys( $tmpFiles ),
1368 "Local copies in right order ($backendName)."
1370 $this->assertEquals(
1373 "Local copies array size correct ($backendName)."
1376 $tmpFile = $this->backend
->getLocalCopy( [ 'src' => $source ] );
1377 $this->assertNotNull( $tmpFile,
1378 "Creation of local copy of $source succeeded ($backendName)." );
1379 $contents = file_get_contents( $tmpFile->getPath() );
1380 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1381 $this->assertEquals(
1384 "Local copy of $source is correct ($backendName)."
1388 $obj = new stdClass();
1389 $tmpFile->bind( $obj );
1392 public static function provider_testGetLocalCopy() {
1395 $base = self
::baseStorePath();
1396 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1397 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1398 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1400 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1401 "$base/unittest-cont1/e/a/z.txt" ],
1402 [ "contents xx $", "contents xy 111", "contents xz" ]
1409 * @dataProvider provider_testGetLocalReference
1411 public function testGetLocalReference( $source, $content ) {
1412 $this->backend
= $this->singleBackend
;
1413 $this->tearDownFiles();
1414 $this->doTestGetLocalReference( $source, $content );
1415 $this->tearDownFiles();
1417 $this->backend
= $this->multiBackend
;
1418 $this->tearDownFiles();
1419 $this->doTestGetLocalReference( $source, $content );
1420 $this->tearDownFiles();
1423 private function doTestGetLocalReference( $source, $content ) {
1424 $backendName = $this->backendClass();
1426 $srcs = (array)$source;
1427 $content = (array)$content;
1428 foreach ( $srcs as $i => $src ) {
1429 $this->prepare( [ 'dir' => dirname( $src ) ] );
1430 $status = $this->backend
->doOperation(
1431 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1432 $this->assertGoodStatus( $status,
1433 "Creation of file at $src succeeded ($backendName)." );
1436 if ( is_array( $source ) ) {
1437 $tmpFiles = $this->backend
->getLocalReferenceMulti( [ 'srcs' => $source ] );
1438 foreach ( $tmpFiles as $path => $tmpFile ) {
1439 $this->assertNotNull( $tmpFile,
1440 "Creation of local copy of $path succeeded ($backendName)." );
1441 $contents = file_get_contents( $tmpFile->getPath() );
1442 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1443 $this->assertEquals(
1444 current( $content ),
1446 "Local ref of $path is correct ($backendName)."
1450 $this->assertEquals(
1452 array_keys( $tmpFiles ),
1453 "Local refs in right order ($backendName)."
1455 $this->assertEquals(
1458 "Local refs array size correct ($backendName)."
1461 $tmpFile = $this->backend
->getLocalReference( [ 'src' => $source ] );
1462 $this->assertNotNull( $tmpFile,
1463 "Creation of local copy of $source succeeded ($backendName)." );
1464 $contents = file_get_contents( $tmpFile->getPath() );
1465 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1466 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1470 public static function provider_testGetLocalReference() {
1473 $base = self
::baseStorePath();
1474 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1475 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1476 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1478 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1479 "$base/unittest-cont1/e/a/z.txt" ],
1480 [ "contents xx 1111", "contents xy %", "contents xz $" ]
1486 public function testGetLocalCopyAndReference404() {
1487 $this->backend
= $this->singleBackend
;
1488 $this->tearDownFiles();
1489 $this->doTestGetLocalCopyAndReference404();
1490 $this->tearDownFiles();
1492 $this->backend
= $this->multiBackend
;
1493 $this->tearDownFiles();
1494 $this->doTestGetLocalCopyAndReference404();
1495 $this->tearDownFiles();
1498 public function doTestGetLocalCopyAndReference404() {
1499 $backendName = $this->backendClass();
1501 $base = self
::baseStorePath();
1503 $tmpFile = $this->backend
->getLocalCopy( [
1504 'src' => "$base/unittest-cont1/not-there" ] );
1505 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1507 $tmpFile = $this->backend
->getLocalReference( [
1508 'src' => "$base/unittest-cont1/not-there" ] );
1509 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1513 * @dataProvider provider_testGetFileHttpUrl
1515 public function testGetFileHttpUrl( $source, $content ) {
1516 $this->backend
= $this->singleBackend
;
1517 $this->tearDownFiles();
1518 $this->doTestGetFileHttpUrl( $source, $content );
1519 $this->tearDownFiles();
1521 $this->backend
= $this->multiBackend
;
1522 $this->tearDownFiles();
1523 $this->doTestGetFileHttpUrl( $source, $content );
1524 $this->tearDownFiles();
1527 private function doTestGetFileHttpUrl( $source, $content ) {
1528 $backendName = $this->backendClass();
1530 $this->prepare( [ 'dir' => dirname( $source ) ] );
1531 $status = $this->backend
->doOperation(
1532 [ 'op' => 'create', 'content' => $content, 'dst' => $source ] );
1533 $this->assertGoodStatus( $status,
1534 "Creation of file at $source succeeded ($backendName)." );
1536 $url = $this->backend
->getFileHttpUrl( [ 'src' => $source ] );
1538 if ( $url !== null ) { // supported
1539 $data = Http
::request( "GET", $url, [], __METHOD__
);
1540 $this->assertEquals( $content, $data,
1541 "HTTP GET of URL has right contents ($backendName)." );
1545 public static function provider_testGetFileHttpUrl() {
1548 $base = self
::baseStorePath();
1549 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1550 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1551 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1557 * @dataProvider provider_testPrepareAndClean
1559 public function testPrepareAndClean( $path, $isOK ) {
1560 $this->backend
= $this->singleBackend
;
1561 $this->doTestPrepareAndClean( $path, $isOK );
1562 $this->tearDownFiles();
1564 $this->backend
= $this->multiBackend
;
1565 $this->doTestPrepareAndClean( $path, $isOK );
1566 $this->tearDownFiles();
1569 public static function provider_testPrepareAndClean() {
1570 $base = self
::baseStorePath();
1573 [ "$base/unittest-cont1/e/a/z/some_file1.txt", true ],
1574 [ "$base/unittest-cont2/a/z/some_file2.txt", true ],
1575 # Specific to FS backend with no basePath field set
1576 # [ "$base/unittest-cont3/a/z/some_file3.txt", false ],
1580 private function doTestPrepareAndClean( $path, $isOK ) {
1581 $backendName = $this->backendClass();
1583 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
1585 $this->assertGoodStatus( $status,
1586 "Preparing dir $path succeeded without warnings ($backendName)." );
1587 $this->assertEquals( true, $status->isOK(),
1588 "Preparing dir $path succeeded ($backendName)." );
1590 $this->assertEquals( false, $status->isOK(),
1591 "Preparing dir $path failed ($backendName)." );
1594 $status = $this->backend
->secure( [ 'dir' => dirname( $path ) ] );
1596 $this->assertGoodStatus( $status,
1597 "Securing dir $path succeeded without warnings ($backendName)." );
1598 $this->assertEquals( true, $status->isOK(),
1599 "Securing dir $path succeeded ($backendName)." );
1601 $this->assertEquals( false, $status->isOK(),
1602 "Securing dir $path failed ($backendName)." );
1605 $status = $this->backend
->publish( [ 'dir' => dirname( $path ) ] );
1607 $this->assertGoodStatus( $status,
1608 "Publishing dir $path succeeded without warnings ($backendName)." );
1609 $this->assertEquals( true, $status->isOK(),
1610 "Publishing dir $path succeeded ($backendName)." );
1612 $this->assertEquals( false, $status->isOK(),
1613 "Publishing dir $path failed ($backendName)." );
1616 $status = $this->backend
->clean( [ 'dir' => dirname( $path ) ] );
1618 $this->assertGoodStatus( $status,
1619 "Cleaning dir $path succeeded without warnings ($backendName)." );
1620 $this->assertEquals( true, $status->isOK(),
1621 "Cleaning dir $path succeeded ($backendName)." );
1623 $this->assertEquals( false, $status->isOK(),
1624 "Cleaning dir $path failed ($backendName)." );
1628 public function testRecursiveClean() {
1629 $this->backend
= $this->singleBackend
;
1630 $this->doTestRecursiveClean();
1631 $this->tearDownFiles();
1633 $this->backend
= $this->multiBackend
;
1634 $this->doTestRecursiveClean();
1635 $this->tearDownFiles();
1638 private function doTestRecursiveClean() {
1639 $backendName = $this->backendClass();
1641 $base = self
::baseStorePath();
1643 "$base/unittest-cont1",
1644 "$base/unittest-cont1/e",
1645 "$base/unittest-cont1/e/a",
1646 "$base/unittest-cont1/e/a/b",
1647 "$base/unittest-cont1/e/a/b/c",
1648 "$base/unittest-cont1/e/a/b/c/d0",
1649 "$base/unittest-cont1/e/a/b/c/d1",
1650 "$base/unittest-cont1/e/a/b/c/d2",
1651 "$base/unittest-cont1/e/a/b/c/d0/1",
1652 "$base/unittest-cont1/e/a/b/c/d0/2",
1653 "$base/unittest-cont1/e/a/b/c/d1/3",
1654 "$base/unittest-cont1/e/a/b/c/d1/4",
1655 "$base/unittest-cont1/e/a/b/c/d2/5",
1656 "$base/unittest-cont1/e/a/b/c/d2/6"
1658 foreach ( $dirs as $dir ) {
1659 $status = $this->prepare( [ 'dir' => $dir ] );
1660 $this->assertGoodStatus( $status,
1661 "Preparing dir $dir succeeded without warnings ($backendName)." );
1664 if ( $this->backend
instanceof FSFileBackend
) {
1665 foreach ( $dirs as $dir ) {
1666 $this->assertEquals( true, $this->backend
->directoryExists( [ 'dir' => $dir ] ),
1667 "Dir $dir exists ($backendName)." );
1671 $status = $this->backend
->clean(
1672 [ 'dir' => "$base/unittest-cont1", 'recursive' => 1 ] );
1673 $this->assertGoodStatus( $status,
1674 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1676 foreach ( $dirs as $dir ) {
1677 $this->assertEquals( false, $this->backend
->directoryExists( [ 'dir' => $dir ] ),
1678 "Dir $dir no longer exists ($backendName)." );
1682 public function testDoOperations() {
1683 $this->backend
= $this->singleBackend
;
1684 $this->tearDownFiles();
1685 $this->doTestDoOperations();
1686 $this->tearDownFiles();
1688 $this->backend
= $this->multiBackend
;
1689 $this->tearDownFiles();
1690 $this->doTestDoOperations();
1691 $this->tearDownFiles();
1694 private function doTestDoOperations() {
1695 $base = self
::baseStorePath();
1697 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1698 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1699 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1700 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1701 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1702 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1703 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1705 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1706 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1707 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1708 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
1709 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1710 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
1711 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1713 $status = $this->backend
->doOperations( [
1714 [ 'op' => 'describe', 'src' => $fileA,
1715 'headers' => [ 'X-Content-Length' => '91.3' ], 'disposition' => 'inline' ],
1716 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1717 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1718 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1719 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1720 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1721 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1722 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1723 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1724 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1725 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1726 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1727 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1728 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1729 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1730 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1731 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1732 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1734 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1736 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1738 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1744 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1745 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1746 $this->assertEquals( 14, count( $status->success
),
1747 "Operation batch has correct success array" );
1749 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileA ] ),
1750 "File does not exist at $fileA" );
1751 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileB ] ),
1752 "File does not exist at $fileB" );
1753 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileD ] ),
1754 "File does not exist at $fileD" );
1756 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileC ] ),
1757 "File exists at $fileC" );
1758 $this->assertEquals( $fileBContents,
1759 $this->backend
->getFileContents( [ 'src' => $fileC ] ),
1760 "Correct file contents of $fileC" );
1761 $this->assertEquals( strlen( $fileBContents ),
1762 $this->backend
->getFileSize( [ 'src' => $fileC ] ),
1763 "Correct file size of $fileC" );
1764 $this->assertEquals( Wikimedia\base_convert
( sha1( $fileBContents ), 16, 36, 31 ),
1765 $this->backend
->getFileSha1Base36( [ 'src' => $fileC ] ),
1766 "Correct file SHA-1 of $fileC" );
1769 public function testDoOperationsPipeline() {
1770 $this->backend
= $this->singleBackend
;
1771 $this->tearDownFiles();
1772 $this->doTestDoOperationsPipeline();
1773 $this->tearDownFiles();
1775 $this->backend
= $this->multiBackend
;
1776 $this->tearDownFiles();
1777 $this->doTestDoOperationsPipeline();
1778 $this->tearDownFiles();
1781 // concurrency orientated
1782 private function doTestDoOperationsPipeline() {
1783 $base = self
::baseStorePath();
1785 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1786 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1787 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1789 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1790 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1791 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1792 $this->addTmpFiles( [ $tmpNameA, $tmpNameB, $tmpNameC ] );
1793 file_put_contents( $tmpNameA, $fileAContents );
1794 file_put_contents( $tmpNameB, $fileBContents );
1795 file_put_contents( $tmpNameC, $fileCContents );
1797 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1798 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1799 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1800 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1802 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1803 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1804 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1805 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1806 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1808 $status = $this->backend
->doOperations( [
1809 [ 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ],
1810 [ 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ],
1811 [ 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ],
1812 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1813 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1814 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1815 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1816 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1817 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1818 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1819 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1820 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1821 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1822 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1823 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1824 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1825 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1826 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1827 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1828 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1830 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1832 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1834 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1840 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1841 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1842 $this->assertEquals( 16, count( $status->success
),
1843 "Operation batch has correct success array" );
1845 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileA ] ),
1846 "File does not exist at $fileA" );
1847 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileB ] ),
1848 "File does not exist at $fileB" );
1849 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileD ] ),
1850 "File does not exist at $fileD" );
1852 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileC ] ),
1853 "File exists at $fileC" );
1854 $this->assertEquals( $fileBContents,
1855 $this->backend
->getFileContents( [ 'src' => $fileC ] ),
1856 "Correct file contents of $fileC" );
1857 $this->assertEquals( strlen( $fileBContents ),
1858 $this->backend
->getFileSize( [ 'src' => $fileC ] ),
1859 "Correct file size of $fileC" );
1860 $this->assertEquals( Wikimedia\base_convert
( sha1( $fileBContents ), 16, 36, 31 ),
1861 $this->backend
->getFileSha1Base36( [ 'src' => $fileC ] ),
1862 "Correct file SHA-1 of $fileC" );
1865 public function testDoOperationsFailing() {
1866 $this->backend
= $this->singleBackend
;
1867 $this->tearDownFiles();
1868 $this->doTestDoOperationsFailing();
1869 $this->tearDownFiles();
1871 $this->backend
= $this->multiBackend
;
1872 $this->tearDownFiles();
1873 $this->doTestDoOperationsFailing();
1874 $this->tearDownFiles();
1877 private function doTestDoOperationsFailing() {
1878 $base = self
::baseStorePath();
1880 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1881 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1882 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1883 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1884 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1885 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1886 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1888 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1889 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1890 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1891 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
1892 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1893 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
1895 $status = $this->backend
->doOperations( [
1896 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1897 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1898 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1899 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1900 [ 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ],
1901 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1902 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ],
1903 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1904 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ],
1905 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1906 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ],
1907 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1908 [ 'op' => 'delete', 'src' => $fileD ],
1909 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1912 ], [ 'force' => 1 ] );
1914 $this->assertNotEquals( [], $status->getErrors(), "Operation had warnings" );
1915 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1916 $this->assertEquals( 8, count( $status->success
),
1917 "Operation batch has correct success array" );
1919 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileB ] ),
1920 "File does not exist at $fileB" );
1921 $this->assertEquals( false, $this->backend
->fileExists( [ 'src' => $fileD ] ),
1922 "File does not exist at $fileD" );
1924 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileA ] ),
1925 "File does not exist at $fileA" );
1926 $this->assertEquals( true, $this->backend
->fileExists( [ 'src' => $fileC ] ),
1927 "File exists at $fileC" );
1928 $this->assertEquals( $fileBContents,
1929 $this->backend
->getFileContents( [ 'src' => $fileA ] ),
1930 "Correct file contents of $fileA" );
1931 $this->assertEquals( strlen( $fileBContents ),
1932 $this->backend
->getFileSize( [ 'src' => $fileA ] ),
1933 "Correct file size of $fileA" );
1934 $this->assertEquals( Wikimedia\base_convert
( sha1( $fileBContents ), 16, 36, 31 ),
1935 $this->backend
->getFileSha1Base36( [ 'src' => $fileA ] ),
1936 "Correct file SHA-1 of $fileA" );
1939 public function testGetFileList() {
1940 $this->backend
= $this->singleBackend
;
1941 $this->tearDownFiles();
1942 $this->doTestGetFileList();
1943 $this->tearDownFiles();
1945 $this->backend
= $this->multiBackend
;
1946 $this->tearDownFiles();
1947 $this->doTestGetFileList();
1948 $this->tearDownFiles();
1951 private function doTestGetFileList() {
1952 $backendName = $this->backendClass();
1953 $base = self
::baseStorePath();
1955 // Should have no errors
1956 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont-notexists" ] );
1959 "$base/unittest-cont1/e/test1.txt",
1960 "$base/unittest-cont1/e/test2.txt",
1961 "$base/unittest-cont1/e/test3.txt",
1962 "$base/unittest-cont1/e/subdir1/test1.txt",
1963 "$base/unittest-cont1/e/subdir1/test2.txt",
1964 "$base/unittest-cont1/e/subdir2/test3.txt",
1965 "$base/unittest-cont1/e/subdir2/test4.txt",
1966 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1967 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1968 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1969 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1970 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1971 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1972 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1977 foreach ( $files as $file ) {
1978 $this->prepare( [ 'dir' => dirname( $file ) ] );
1979 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
1981 $status = $this->backend
->doQuickOperations( $ops );
1982 $this->assertGoodStatus( $status,
1983 "Creation of files succeeded ($backendName)." );
1984 $this->assertEquals( true, $status->isOK(),
1985 "Creation of files succeeded with OK status ($backendName)." );
1987 // Expected listing at root
1992 "e/subdir1/test1.txt",
1993 "e/subdir1/test2.txt",
1994 "e/subdir2/test3.txt",
1995 "e/subdir2/test4.txt",
1996 "e/subdir2/subdir/test1.txt",
1997 "e/subdir2/subdir/test2.txt",
1998 "e/subdir2/subdir/test3.txt",
1999 "e/subdir2/subdir/test4.txt",
2000 "e/subdir2/subdir/test5.txt",
2001 "e/subdir2/subdir/sub/test0.txt",
2002 "e/subdir2/subdir/sub/120-px-file.txt",
2006 // Actual listing (no trailing slash) at root
2007 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1" ] );
2008 $list = $this->listToArray( $iter );
2010 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2012 // Actual listing (no trailing slash) at root with advise
2013 $iter = $this->backend
->getFileList( [
2014 'dir' => "$base/unittest-cont1",
2017 $list = $this->listToArray( $iter );
2019 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2021 // Actual listing (with trailing slash) at root
2023 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/" ] );
2024 foreach ( $iter as $file ) {
2028 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2030 // Expected listing at subdir
2038 "sub/120-px-file.txt",
2042 // Actual listing (no trailing slash) at subdir
2043 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] );
2044 $list = $this->listToArray( $iter );
2046 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2048 // Actual listing (no trailing slash) at subdir with advise
2049 $iter = $this->backend
->getFileList( [
2050 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2053 $list = $this->listToArray( $iter );
2055 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2057 // Actual listing (with trailing slash) at subdir
2059 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ] );
2060 foreach ( $iter as $file ) {
2064 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2066 // Actual listing (using iterator second time)
2067 $list = $this->listToArray( $iter );
2069 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
2071 // Actual listing (top files only) at root
2072 $iter = $this->backend
->getTopFileList( [ 'dir' => "$base/unittest-cont1" ] );
2073 $list = $this->listToArray( $iter );
2075 $this->assertEquals( [], $list, "Correct top file listing ($backendName)." );
2077 // Expected listing (top files only) at subdir
2087 // Actual listing (top files only) at subdir
2088 $iter = $this->backend
->getTopFileList(
2089 [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ]
2091 $list = $this->listToArray( $iter );
2093 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2095 // Actual listing (top files only) at subdir with advise
2096 $iter = $this->backend
->getTopFileList( [
2097 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2100 $list = $this->listToArray( $iter );
2102 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2104 foreach ( $files as $file ) { // clean up
2105 $this->backend
->doOperation( [ 'op' => 'delete', 'src' => $file ] );
2108 $iter = $this->backend
->getFileList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
2109 foreach ( $iter as $iter ) {
2114 public function testGetDirectoryList() {
2115 $this->backend
= $this->singleBackend
;
2116 $this->tearDownFiles();
2117 $this->doTestGetDirectoryList();
2118 $this->tearDownFiles();
2120 $this->backend
= $this->multiBackend
;
2121 $this->tearDownFiles();
2122 $this->doTestGetDirectoryList();
2123 $this->tearDownFiles();
2126 private function doTestGetDirectoryList() {
2127 $backendName = $this->backendClass();
2129 $base = self
::baseStorePath();
2131 "$base/unittest-cont1/e/test1.txt",
2132 "$base/unittest-cont1/e/test2.txt",
2133 "$base/unittest-cont1/e/test3.txt",
2134 "$base/unittest-cont1/e/subdir1/test1.txt",
2135 "$base/unittest-cont1/e/subdir1/test2.txt",
2136 "$base/unittest-cont1/e/subdir2/test3.txt",
2137 "$base/unittest-cont1/e/subdir2/test4.txt",
2138 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
2139 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
2140 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
2141 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
2142 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
2143 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
2144 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
2149 foreach ( $files as $file ) {
2150 $this->prepare( [ 'dir' => dirname( $file ) ] );
2151 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
2153 $status = $this->backend
->doQuickOperations( $ops );
2154 $this->assertGoodStatus( $status,
2155 "Creation of files succeeded ($backendName)." );
2156 $this->assertEquals( true, $status->isOK(),
2157 "Creation of files succeeded with OK status ($backendName)." );
2159 $this->assertEquals( true,
2160 $this->backend
->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] ),
2161 "Directory exists in ($backendName)." );
2162 $this->assertEquals( true,
2163 $this->backend
->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] ),
2164 "Directory exists in ($backendName)." );
2165 $this->assertEquals( false,
2166 $this->backend
->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ] ),
2167 "Directory does not exists in ($backendName)." );
2175 // Actual listing (no trailing slash)
2177 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1" ] );
2178 foreach ( $iter as $file ) {
2183 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2194 // Actual listing (no trailing slash)
2196 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e" ] );
2197 foreach ( $iter as $file ) {
2202 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2204 // Actual listing (with trailing slash)
2206 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/" ] );
2207 foreach ( $iter as $file ) {
2212 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2220 // Actual listing (no trailing slash)
2222 $iter = $this->backend
->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir2" ] );
2223 foreach ( $iter as $file ) {
2228 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2230 // Actual listing (with trailing slash)
2232 $iter = $this->backend
->getTopDirectoryList(
2233 [ 'dir' => "$base/unittest-cont1/e/subdir2/" ]
2236 foreach ( $iter as $file ) {
2241 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2243 // Actual listing (using iterator second time)
2245 foreach ( $iter as $file ) {
2250 $this->assertEquals(
2253 "Correct top dir listing ($backendName), second iteration."
2256 // Expected listing (recursive)
2266 "e/subdir4/subdir/sub",
2270 // Actual listing (recursive)
2272 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/" ] );
2273 foreach ( $iter as $file ) {
2278 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2280 // Expected listing (recursive)
2287 // Actual listing (recursive)
2289 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir4" ] );
2290 foreach ( $iter as $file ) {
2295 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2297 // Actual listing (recursive, second time)
2299 foreach ( $iter as $file ) {
2304 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2306 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] );
2307 $items = $this->listToArray( $iter );
2308 $this->assertEquals( [], $items, "Directory listing is empty." );
2310 foreach ( $files as $file ) { // clean up
2311 $this->backend
->doOperation( [ 'op' => 'delete', 'src' => $file ] );
2314 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
2315 foreach ( $iter as $file ) {
2319 $items = $this->listToArray( $iter );
2320 $this->assertEquals( [], $items, "Directory listing is empty." );
2322 $iter = $this->backend
->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/not/exists" ] );
2323 $items = $this->listToArray( $iter );
2324 $this->assertEquals( [], $items, "Directory listing is empty." );
2327 public function testLockCalls() {
2328 $this->backend
= $this->singleBackend
;
2329 $this->doTestLockCalls();
2332 private function doTestLockCalls() {
2333 $backendName = $this->backendClass();
2340 "subdir1", // duplicate
2341 "subdir1/test1.txt",
2342 "subdir1/test2.txt",
2344 "subdir2", // duplicate
2345 "subdir2/test3.txt",
2346 "subdir2/test4.txt",
2348 "subdir2/subdir/test1.txt",
2349 "subdir2/subdir/test2.txt",
2350 "subdir2/subdir/test3.txt",
2351 "subdir2/subdir/test4.txt",
2352 "subdir2/subdir/test5.txt",
2353 "subdir2/subdir/sub",
2354 "subdir2/subdir/sub/test0.txt",
2355 "subdir2/subdir/sub/120-px-file.txt",
2358 for ( $i = 0; $i < 25; $i++
) {
2359 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2360 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2361 "Locking of files succeeded ($backendName) ($i)." );
2362 $this->assertEquals( true, $status->isOK(),
2363 "Locking of files succeeded with OK status ($backendName) ($i)." );
2365 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2366 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2367 "Locking of files succeeded ($backendName) ($i)." );
2368 $this->assertEquals( true, $status->isOK(),
2369 "Locking of files succeeded with OK status ($backendName) ($i)." );
2371 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2372 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2373 "Locking of files succeeded ($backendName) ($i)." );
2374 $this->assertEquals( true, $status->isOK(),
2375 "Locking of files succeeded with OK status ($backendName) ($i)." );
2377 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2378 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2379 "Locking of files succeeded ($backendName). ($i)" );
2380 $this->assertEquals( true, $status->isOK(),
2381 "Locking of files succeeded with OK status ($backendName) ($i)." );
2383 # # Flip the acquire/release ordering around ##
2385 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
2386 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2387 "Locking of files succeeded ($backendName) ($i)." );
2388 $this->assertEquals( true, $status->isOK(),
2389 "Locking of files succeeded with OK status ($backendName) ($i)." );
2391 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
2392 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2393 "Locking of files succeeded ($backendName) ($i)." );
2394 $this->assertEquals( true, $status->isOK(),
2395 "Locking of files succeeded with OK status ($backendName) ($i)." );
2397 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
2398 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2399 "Locking of files succeeded ($backendName). ($i)" );
2400 $this->assertEquals( true, $status->isOK(),
2401 "Locking of files succeeded with OK status ($backendName) ($i)." );
2403 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
2404 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2405 "Locking of files succeeded ($backendName) ($i)." );
2406 $this->assertEquals( true, $status->isOK(),
2407 "Locking of files succeeded with OK status ($backendName) ($i)." );
2410 $status = Status
::newGood();
2411 $sl = $this->backend
->getScopedFileLocks( $paths, LockManager
::LOCK_EX
, $status );
2412 $this->assertInstanceOf( 'ScopedLock', $sl,
2413 "Scoped locking of files succeeded ($backendName)." );
2414 $this->assertEquals( [], $status->getErrors(),
2415 "Scoped locking of files succeeded ($backendName)." );
2416 $this->assertEquals( true, $status->isOK(),
2417 "Scoped locking of files succeeded with OK status ($backendName)." );
2419 ScopedLock
::release( $sl );
2420 $this->assertEquals( null, $sl,
2421 "Scoped unlocking of files succeeded ($backendName)." );
2422 $this->assertEquals( [], $status->getErrors(),
2423 "Scoped unlocking of files succeeded ($backendName)." );
2424 $this->assertEquals( true, $status->isOK(),
2425 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2429 * @dataProvider provider_testGetContentType
2431 public function testGetContentType( $mimeCallback, $mimeFromString ) {
2434 $be = TestingAccessWrapper
::newFromObject( new MemoryFileBackend(
2436 'name' => 'testing',
2437 'class' => 'MemoryFileBackend',
2439 'mimeCallback' => $mimeCallback
2443 $dst = 'mwstore://testing/container/path/to/file_no_ext';
2444 $src = "$IP/tests/phpunit/data/media/srgb.jpg";
2445 $this->assertEquals( 'image/jpeg', $be->getContentType( $dst, null, $src ) );
2446 $this->assertEquals(
2447 $mimeFromString ?
'image/jpeg' : 'unknown/unknown',
2448 $be->getContentType( $dst, file_get_contents( $src ), null ) );
2450 $src = "$IP/tests/phpunit/data/media/Png-native-test.png";
2451 $this->assertEquals( 'image/png', $be->getContentType( $dst, null, $src ) );
2452 $this->assertEquals(
2453 $mimeFromString ?
'image/png' : 'unknown/unknown',
2454 $be->getContentType( $dst, file_get_contents( $src ), null ) );
2457 public static function provider_testGetContentType() {
2460 [ [ FileBackendGroup
::singleton(), 'guessMimeInternal' ], true ]
2464 public function testReadAffinity() {
2465 $be = TestingAccessWrapper
::newFromObject(
2466 new FileBackendMultiWrite( [
2467 'name' => 'localtesting',
2468 'wikiId' => wfWikiID() . mt_rand(),
2471 'name' => 'multitesting0',
2472 'class' => 'MemoryFileBackend',
2473 'isMultiMaster' => false,
2474 'readAffinity' => true
2477 'name' => 'multitesting1',
2478 'class' => 'MemoryFileBackend',
2479 'isMultiMaster' => true
2485 $this->assertEquals(
2487 $be->getReadIndexFromParams( [ 'latest' => 1 ] ),
2488 'Reads with "latest" flag use backend 1'
2490 $this->assertEquals(
2492 $be->getReadIndexFromParams( [ 'latest' => 0 ] ),
2493 'Reads without "latest" flag use backend 0'
2496 $p = 'container/test-cont/file.txt';
2497 $be->backends
[0]->quickCreate( [
2498 'dst' => "mwstore://multitesting0/$p", 'content' => 'cattitude' ] );
2499 $be->backends
[1]->quickCreate( [
2500 'dst' => "mwstore://multitesting1/$p", 'content' => 'princess of power' ] );
2502 $this->assertEquals(
2504 $be->getFileContents( [ 'src' => "mwstore://localtesting/$p" ] ),
2505 "Non-latest read came from backend 0"
2507 $this->assertEquals(
2508 'princess of power',
2509 $be->getFileContents( [ 'src' => "mwstore://localtesting/$p", 'latest' => 1 ] ),
2510 "Latest read came from backend1"
2514 public function testAsyncWrites() {
2515 $be = TestingAccessWrapper
::newFromObject(
2516 new FileBackendMultiWrite( [
2517 'name' => 'localtesting',
2518 'wikiId' => wfWikiID() . mt_rand(),
2521 'name' => 'multitesting0',
2522 'class' => 'MemoryFileBackend',
2523 'isMultiMaster' => false
2526 'name' => 'multitesting1',
2527 'class' => 'MemoryFileBackend',
2528 'isMultiMaster' => true
2531 'replication' => 'async'
2535 $this->setMwGlobals( 'wgCommandLineMode', false );
2537 $p = 'container/test-cont/file.txt';
2539 'dst' => "mwstore://localtesting/$p", 'content' => 'cattitude' ] );
2541 $this->assertEquals(
2543 $be->backends
[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
2544 "File not yet written to backend 0"
2546 $this->assertEquals(
2548 $be->backends
[1]->getFileContents( [ 'src' => "mwstore://multitesting1/$p" ] ),
2549 "File already written to backend 1"
2552 DeferredUpdates
::doUpdates();
2554 $this->assertEquals(
2556 $be->backends
[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
2557 "File now written to backend 0"
2561 public function testSanitizeOpHeaders() {
2562 $be = TestingAccessWrapper
::newFromObject( new MemoryFileBackend( [
2563 'name' => 'localtesting',
2564 'wikiId' => wfWikiID()
2567 $name = wfRandomString( 300 );
2571 'content-Disposition' => FileBackend
::makeContentDisposition( 'inline', $name ),
2572 'Content-dUration' => 25.6,
2573 'X-LONG-VALUE' => str_pad( '0', 300 ),
2574 'CONTENT-LENGTH' => 855055,
2579 'content-disposition' => FileBackend
::makeContentDisposition( 'inline', $name ),
2580 'content-duration' => 25.6,
2581 'content-length' => 855055
2585 MediaWiki\
suppressWarnings();
2586 $actual = $be->sanitizeOpHeaders( $input );
2587 MediaWiki\restoreWarnings
();
2589 $this->assertEquals( $expected, $actual, "Header sanitized properly" );
2593 private function listToArray( $iter ) {
2594 return is_array( $iter ) ?
$iter : iterator_to_array( $iter );
2597 // test helper wrapper for backend prepare() function
2598 private function prepare( array $params ) {
2599 return $this->backend
->prepare( $params );
2602 // test helper wrapper for backend prepare() function
2603 private function create( array $params ) {
2604 $params['op'] = 'create';
2606 return $this->backend
->doQuickOperations( [ $params ] );
2609 function tearDownFiles() {
2610 $containers = [ 'unittest-cont1', 'unittest-cont2', 'unittest-cont-bad' ];
2611 foreach ( $containers as $container ) {
2612 $this->deleteFiles( $container );
2616 private function deleteFiles( $container ) {
2617 $base = self
::baseStorePath();
2618 $iter = $this->backend
->getFileList( [ 'dir' => "$base/$container" ] );
2620 foreach ( $iter as $file ) {
2621 $this->backend
->quickDelete( [ 'src' => "$base/$container/$file" ] );
2623 // free the directory, to avoid Permission denied under windows on rmdir
2626 $this->backend
->clean( [ 'dir' => "$base/$container", 'recursive' => 1 ] );
2629 function assertBackendPathsConsistent( array $paths ) {
2630 if ( $this->backend
instanceof FileBackendMultiWrite
) {
2631 $status = $this->backend
->consistencyCheck( $paths );
2632 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2636 function assertGoodStatus( StatusValue
$status, $msg ) {
2637 $this->assertEquals( print_r( [], 1 ), print_r( $status->getErrors(), 1 ), $msg );