8 class FileBackendTest
extends MediaWikiTestCase
{
9 private $backend, $multiBackend;
10 private $filesToPrune = array();
11 private static $backendToUse;
13 protected function setUp() {
14 global $wgFileBackends;
16 $tmpPrefix = wfTempDir() . '/filebackend-unittest-' . time() . '-' . mt_rand();
17 if ( $this->getCliArg( 'use-filebackend=' ) ) {
18 if ( self
::$backendToUse ) {
19 $this->singleBackend
= self
::$backendToUse;
21 $name = $this->getCliArg( 'use-filebackend=' );
23 foreach ( $wgFileBackends as $conf ) {
24 if ( $conf['name'] == $name ) {
29 $useConfig['name'] = 'localtesting'; // swap name
30 $useConfig['shardViaHashLevels'] = array( // test sharding
31 'unittest-cont1' => array( 'levels' => 1, 'base' => 16, 'repeat' => 1 )
33 $class = $useConfig['class'];
34 self
::$backendToUse = new $class( $useConfig );
35 $this->singleBackend
= self
::$backendToUse;
38 $this->singleBackend
= new FSFileBackend( array(
39 'name' => 'localtesting',
40 'lockManager' => 'fsLockManager',
41 #'parallelize' => 'implicit',
42 'containerPaths' => array(
43 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
44 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
47 $this->multiBackend
= new FileBackendMultiWrite( array(
48 'name' => 'localtesting',
49 'lockManager' => 'fsLockManager',
50 'parallelize' => 'implicit',
53 'name' => 'localmultitesting1',
54 'class' => 'FSFileBackend',
55 'lockManager' => 'nullLockManager',
56 'containerPaths' => array(
57 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
58 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
59 'isMultiMaster' => false
62 'name' => 'localmultitesting2',
63 'class' => 'FSFileBackend',
64 'lockManager' => 'nullLockManager',
65 'containerPaths' => array(
66 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
67 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
68 'isMultiMaster' => true
72 $this->filesToPrune
= array();
75 private static function baseStorePath() {
76 return 'mwstore://localtesting';
79 private function backendClass() {
80 return get_class( $this->backend
);
84 * @dataProvider provider_testIsStoragePath
86 public function testIsStoragePath( $path, $isStorePath ) {
87 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
88 "FileBackend::isStoragePath on path '$path'" );
91 function provider_testIsStoragePath() {
93 array( 'mwstore://', true ),
94 array( 'mwstore://backend', true ),
95 array( 'mwstore://backend/container', true ),
96 array( 'mwstore://backend/container/', true ),
97 array( 'mwstore://backend/container/path', true ),
98 array( 'mwstore://backend//container/', true ),
99 array( 'mwstore://backend//container//', true ),
100 array( 'mwstore://backend//container//path', true ),
101 array( 'mwstore:///', true ),
102 array( 'mwstore:/', false ),
103 array( 'mwstore:', false ),
108 * @dataProvider provider_testSplitStoragePath
110 public function testSplitStoragePath( $path, $res ) {
111 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
112 "FileBackend::splitStoragePath on path '$path'" );
115 function provider_testSplitStoragePath() {
117 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
118 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
119 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
120 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
121 array( 'mwstore://backend//container/path', array( null, null, null ) ),
122 array( 'mwstore://backend//container//path', array( null, null, null ) ),
123 array( 'mwstore://', array( null, null, null ) ),
124 array( 'mwstore://backend', array( null, null, null ) ),
125 array( 'mwstore:///', array( null, null, null ) ),
126 array( 'mwstore:/', array( null, null, null ) ),
127 array( 'mwstore:', array( null, null, null ) )
132 * @dataProvider provider_normalizeStoragePath
134 public function testNormalizeStoragePath( $path, $res ) {
135 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
136 "FileBackend::normalizeStoragePath on path '$path'" );
139 function provider_normalizeStoragePath() {
141 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
142 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
143 array( 'mwstore://backend/container/path', 'mwstore://backend/container/path' ),
144 array( 'mwstore://backend/container//path', 'mwstore://backend/container/path' ),
145 array( 'mwstore://backend/container///path', 'mwstore://backend/container/path' ),
146 array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj',
147 array( 'mwstore://', null ),
148 array( 'mwstore://backend', null ),
149 array( 'mwstore://backend//container/path', null ),
150 array( 'mwstore://backend//container//path', null ),
151 array( 'mwstore:///', null ),
152 array( 'mwstore:/', null ),
153 array( 'mwstore:', null ), )
158 * @dataProvider provider_testParentStoragePath
160 public function testParentStoragePath( $path, $res ) {
161 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
162 "FileBackend::parentStoragePath on path '$path'" );
165 function provider_testParentStoragePath() {
167 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
168 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
169 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
170 array( 'mwstore://backend/container', null ),
171 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
172 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
173 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
174 array( 'mwstore://backend/container/', null ),
179 * @dataProvider provider_testExtensionFromPath
181 public function testExtensionFromPath( $path, $res ) {
182 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
183 "FileBackend::extensionFromPath on path '$path'" );
186 public static function provider_testExtensionFromPath() {
188 array( 'mwstore://backend/container/path.txt', 'txt' ),
189 array( 'mwstore://backend/container/path.svg.png', 'png' ),
190 array( 'mwstore://backend/container/path', '' ),
191 array( 'mwstore://backend/container/path.', '' ),
196 * @dataProvider provider_testStore
198 public function testStore( $op ) {
199 $this->filesToPrune
[] = $op['src'];
201 $this->backend
= $this->singleBackend
;
202 $this->tearDownFiles();
203 $this->doTestStore( $op );
204 $this->tearDownFiles();
206 $this->backend
= $this->multiBackend
;
207 $this->tearDownFiles();
208 $this->doTestStore( $op );
209 $this->filesToPrune
[] = $op['src']; # avoid file leaking
210 $this->tearDownFiles();
213 private function doTestStore( $op ) {
214 $backendName = $this->backendClass();
216 $source = $op['src'];
218 $this->prepare( array( 'dir' => dirname( $dest ) ) );
220 file_put_contents( $source, "Unit test file" );
222 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
223 $this->backend
->store( $op );
226 $status = $this->backend
->doOperation( $op );
228 $this->assertGoodStatus( $status,
229 "Store from $source to $dest succeeded without warnings ($backendName)." );
230 $this->assertEquals( true, $status->isOK(),
231 "Store from $source to $dest succeeded ($backendName)." );
232 $this->assertEquals( array( 0 => true ), $status->success
,
233 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
234 $this->assertEquals( true, file_exists( $source ),
235 "Source file $source still exists ($backendName)." );
236 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
237 "Destination file $dest exists ($backendName)." );
239 $this->assertEquals( filesize( $source ),
240 $this->backend
->getFileSize( array( 'src' => $dest ) ),
241 "Destination file $dest has correct size ($backendName)." );
243 $props1 = FSFile
::getPropsFromPath( $source );
244 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
245 $this->assertEquals( $props1, $props2,
246 "Source and destination have the same props ($backendName)." );
248 $this->assertBackendPathsConsistent( array( $dest ) );
251 public static function provider_testStore() {
254 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
255 $toPath = self
::baseStorePath() . '/unittest-cont1/e/fun/obj1.txt';
256 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
264 $op2['overwrite'] = true;
272 $op2['overwriteSame'] = true;
283 * @dataProvider provider_testCopy
285 public function testCopy( $op ) {
286 $this->backend
= $this->singleBackend
;
287 $this->tearDownFiles();
288 $this->doTestCopy( $op );
289 $this->tearDownFiles();
291 $this->backend
= $this->multiBackend
;
292 $this->tearDownFiles();
293 $this->doTestCopy( $op );
294 $this->tearDownFiles();
297 private function doTestCopy( $op ) {
298 $backendName = $this->backendClass();
300 $source = $op['src'];
302 $this->prepare( array( 'dir' => dirname( $source ) ) );
303 $this->prepare( array( 'dir' => dirname( $dest ) ) );
305 if ( isset( $op['ignoreMissingSource'] ) ) {
306 $status = $this->backend
->doOperation( $op );
307 $this->assertGoodStatus( $status,
308 "Move from $source to $dest succeeded without warnings ($backendName)." );
309 $this->assertEquals( array( 0 => true ), $status->success
,
310 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
311 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
312 "Source file $source does not exist ($backendName)." );
313 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $dest ) ),
314 "Destination file $dest does not exist ($backendName)." );
318 $status = $this->backend
->doOperation(
319 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
320 $this->assertGoodStatus( $status,
321 "Creation of file at $source succeeded ($backendName)." );
323 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
324 $this->backend
->copy( $op );
327 $status = $this->backend
->doOperation( $op );
329 $this->assertGoodStatus( $status,
330 "Copy from $source to $dest succeeded without warnings ($backendName)." );
331 $this->assertEquals( true, $status->isOK(),
332 "Copy from $source to $dest succeeded ($backendName)." );
333 $this->assertEquals( array( 0 => true ), $status->success
,
334 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
335 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
336 "Source file $source still exists ($backendName)." );
337 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
338 "Destination file $dest exists after copy ($backendName)." );
341 $this->backend
->getFileSize( array( 'src' => $source ) ),
342 $this->backend
->getFileSize( array( 'src' => $dest ) ),
343 "Destination file $dest has correct size ($backendName)." );
345 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
346 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
347 $this->assertEquals( $props1, $props2,
348 "Source and destination have the same props ($backendName)." );
350 $this->assertBackendPathsConsistent( array( $source, $dest ) );
353 public static function provider_testCopy() {
356 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
357 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
359 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
367 $op2['overwrite'] = true;
375 $op2['overwriteSame'] = true;
383 $op2['ignoreMissingSource'] = true;
394 * @dataProvider provider_testMove
396 public function testMove( $op ) {
397 $this->backend
= $this->singleBackend
;
398 $this->tearDownFiles();
399 $this->doTestMove( $op );
400 $this->tearDownFiles();
402 $this->backend
= $this->multiBackend
;
403 $this->tearDownFiles();
404 $this->doTestMove( $op );
405 $this->tearDownFiles();
408 private function doTestMove( $op ) {
409 $backendName = $this->backendClass();
411 $source = $op['src'];
413 $this->prepare( array( 'dir' => dirname( $source ) ) );
414 $this->prepare( array( 'dir' => dirname( $dest ) ) );
416 if ( isset( $op['ignoreMissingSource'] ) ) {
417 $status = $this->backend
->doOperation( $op );
418 $this->assertGoodStatus( $status,
419 "Move from $source to $dest succeeded without warnings ($backendName)." );
420 $this->assertEquals( array( 0 => true ), $status->success
,
421 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
422 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
423 "Source file $source does not exist ($backendName)." );
424 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $dest ) ),
425 "Destination file $dest does not exist ($backendName)." );
429 $status = $this->backend
->doOperation(
430 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
431 $this->assertGoodStatus( $status,
432 "Creation of file at $source succeeded ($backendName)." );
434 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
435 $this->backend
->copy( $op );
438 $status = $this->backend
->doOperation( $op );
439 $this->assertGoodStatus( $status,
440 "Move from $source to $dest succeeded without warnings ($backendName)." );
441 $this->assertEquals( true, $status->isOK(),
442 "Move from $source to $dest succeeded ($backendName)." );
443 $this->assertEquals( array( 0 => true ), $status->success
,
444 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
445 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
446 "Source file $source does not still exists ($backendName)." );
447 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
448 "Destination file $dest exists after move ($backendName)." );
450 $this->assertNotEquals(
451 $this->backend
->getFileSize( array( 'src' => $source ) ),
452 $this->backend
->getFileSize( array( 'src' => $dest ) ),
453 "Destination file $dest has correct size ($backendName)." );
455 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
456 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
457 $this->assertEquals( false, $props1['fileExists'],
458 "Source file does not exist accourding to props ($backendName)." );
459 $this->assertEquals( true, $props2['fileExists'],
460 "Destination file exists accourding to props ($backendName)." );
462 $this->assertBackendPathsConsistent( array( $source, $dest ) );
465 public static function provider_testMove() {
468 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
469 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
471 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
479 $op2['overwrite'] = true;
487 $op2['overwriteSame'] = true;
495 $op2['ignoreMissingSource'] = true;
506 * @dataProvider provider_testDelete
508 public function testDelete( $op, $withSource, $okStatus ) {
509 $this->backend
= $this->singleBackend
;
510 $this->tearDownFiles();
511 $this->doTestDelete( $op, $withSource, $okStatus );
512 $this->tearDownFiles();
514 $this->backend
= $this->multiBackend
;
515 $this->tearDownFiles();
516 $this->doTestDelete( $op, $withSource, $okStatus );
517 $this->tearDownFiles();
520 private function doTestDelete( $op, $withSource, $okStatus ) {
521 $backendName = $this->backendClass();
523 $source = $op['src'];
524 $this->prepare( array( 'dir' => dirname( $source ) ) );
527 $status = $this->backend
->doOperation(
528 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
529 $this->assertGoodStatus( $status,
530 "Creation of file at $source succeeded ($backendName)." );
533 $status = $this->backend
->doOperation( $op );
535 $this->assertGoodStatus( $status,
536 "Deletion of file at $source succeeded without warnings ($backendName)." );
537 $this->assertEquals( true, $status->isOK(),
538 "Deletion of file at $source succeeded ($backendName)." );
539 $this->assertEquals( array( 0 => true ), $status->success
,
540 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
542 $this->assertEquals( false, $status->isOK(),
543 "Deletion of file at $source failed ($backendName)." );
546 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
547 "Source file $source does not exist after move ($backendName)." );
550 $this->backend
->getFileSize( array( 'src' => $source ) ),
551 "Source file $source has correct size (false) ($backendName)." );
553 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
554 $this->assertFalse( $props1['fileExists'],
555 "Source file $source does not exist according to props ($backendName)." );
557 $this->assertBackendPathsConsistent( array( $source ) );
560 public static function provider_testDelete() {
563 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
565 $op = array( 'op' => 'delete', 'src' => $source );
574 false, // without source
578 $op['ignoreMissingSource'] = true;
581 false, // without source
589 * @dataProvider provider_testCreate
591 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
592 $this->backend
= $this->singleBackend
;
593 $this->tearDownFiles();
594 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
595 $this->tearDownFiles();
597 $this->backend
= $this->multiBackend
;
598 $this->tearDownFiles();
599 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
600 $this->tearDownFiles();
603 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
604 $backendName = $this->backendClass();
607 $this->prepare( array( 'dir' => dirname( $dest ) ) );
609 $oldText = 'blah...blah...waahwaah';
610 if ( $alreadyExists ) {
611 $status = $this->backend
->doOperation(
612 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
613 $this->assertGoodStatus( $status,
614 "Creation of file at $dest succeeded ($backendName)." );
617 $status = $this->backend
->doOperation( $op );
619 $this->assertGoodStatus( $status,
620 "Creation of file at $dest succeeded without warnings ($backendName)." );
621 $this->assertEquals( true, $status->isOK(),
622 "Creation of file at $dest succeeded ($backendName)." );
623 $this->assertEquals( array( 0 => true ), $status->success
,
624 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
626 $this->assertEquals( false, $status->isOK(),
627 "Creation of file at $dest failed ($backendName)." );
630 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
631 "Destination file $dest exists after creation ($backendName)." );
633 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
634 $this->assertEquals( true, $props1['fileExists'],
635 "Destination file $dest exists according to props ($backendName)." );
636 if ( $okStatus ) { // file content is what we saved
637 $this->assertEquals( $newSize, $props1['size'],
638 "Destination file $dest has expected size according to props ($backendName)." );
639 $this->assertEquals( $newSize,
640 $this->backend
->getFileSize( array( 'src' => $dest ) ),
641 "Destination file $dest has correct size ($backendName)." );
642 } else { // file content is some other previous text
643 $this->assertEquals( strlen( $oldText ), $props1['size'],
644 "Destination file $dest has original size according to props ($backendName)." );
645 $this->assertEquals( strlen( $oldText ),
646 $this->backend
->getFileSize( array( 'src' => $dest ) ),
647 "Destination file $dest has original size according to props ($backendName)." );
650 $this->assertBackendPathsConsistent( array( $dest ) );
654 * @dataProvider provider_testCreate
656 public static function provider_testCreate() {
659 $dest = self
::baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
661 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
664 false, // no dest already exists
666 strlen( $op['content'] )
670 $op2['content'] = "\n";
673 false, // no dest already exists
675 strlen( $op2['content'] )
679 $op2['content'] = "fsf\n waf 3kt";
682 true, // dest already exists
684 strlen( $op2['content'] )
688 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
689 $op2['overwrite'] = true;
692 true, // dest already exists
694 strlen( $op2['content'] )
698 $op2['content'] = "39qjmg3-qg";
699 $op2['overwriteSame'] = true;
702 true, // dest already exists
704 strlen( $op2['content'] )
710 public function testDoQuickOperations() {
711 $this->backend
= $this->singleBackend
;
712 $this->doTestDoQuickOperations();
713 $this->tearDownFiles();
715 $this->backend
= $this->multiBackend
;
716 $this->doTestDoQuickOperations();
717 $this->tearDownFiles();
720 private function doTestDoQuickOperations() {
721 $backendName = $this->backendClass();
723 $base = self
::baseStorePath();
725 "$base/unittest-cont1/e/fileA.a",
726 "$base/unittest-cont1/e/fileB.a",
727 "$base/unittest-cont1/e/fileC.a"
731 foreach ( $files as $path ) {
732 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
733 $this->assertGoodStatus( $status,
734 "Preparing $path succeeded without warnings ($backendName)." );
735 $ops[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand(0,50000) );
736 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
738 $purgeOps[] = array( 'op' => 'null' );
739 $status = $this->backend
->doQuickOperations( $ops );
740 $this->assertGoodStatus( $status,
741 "Creation of source files succeeded ($backendName)." );
743 foreach ( $files as $file ) {
744 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $file ) ),
745 "File $file exists." );
748 $status = $this->backend
->doQuickOperations( $purgeOps );
749 $this->assertGoodStatus( $status,
750 "Quick deletion of source files succeeded ($backendName)." );
752 foreach ( $files as $file ) {
753 $this->assertFalse( $this->backend
->fileExists( array( 'src' => $file ) ),
754 "File $file purged." );
759 * @dataProvider provider_testConcatenate
761 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
762 $this->filesToPrune
[] = $op['dst'];
764 $this->backend
= $this->singleBackend
;
765 $this->tearDownFiles();
766 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
767 $this->tearDownFiles();
769 $this->backend
= $this->multiBackend
;
770 $this->tearDownFiles();
771 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
772 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
773 $this->tearDownFiles();
776 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
777 $backendName = $this->backendClass();
782 foreach ( $srcs as $i => $source ) {
783 $this->prepare( array( 'dir' => dirname( $source ) ) );
785 'op' => 'create', // operation
786 'dst' => $source, // source
787 'content' => $srcsContent[$i]
789 $expContent .= $srcsContent[$i];
791 $status = $this->backend
->doOperations( $ops );
793 $this->assertGoodStatus( $status,
794 "Creation of source files succeeded ($backendName)." );
796 $dest = $params['dst'];
797 if ( $alreadyExists ) {
798 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
799 $this->assertEquals( true, $ok,
800 "Creation of file at $dest succeeded ($backendName)." );
802 $ok = file_put_contents( $dest, '' ) !== false;
803 $this->assertEquals( true, $ok,
804 "Creation of 0-byte file at $dest succeeded ($backendName)." );
807 // Combine the files into one
808 $status = $this->backend
->concatenate( $params );
810 $this->assertGoodStatus( $status,
811 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
812 $this->assertEquals( true, $status->isOK(),
813 "Creation of concat file at $dest succeeded ($backendName)." );
815 $this->assertEquals( false, $status->isOK(),
816 "Creation of concat file at $dest failed ($backendName)." );
820 $this->assertEquals( true, is_file( $dest ),
821 "Dest concat file $dest exists after creation ($backendName)." );
823 $this->assertEquals( true, is_file( $dest ),
824 "Dest concat file $dest exists after failed creation ($backendName)." );
827 $contents = file_get_contents( $dest );
828 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
831 $this->assertEquals( $expContent, $contents,
832 "Concat file at $dest has correct contents ($backendName)." );
834 $this->assertNotEquals( $expContent, $contents,
835 "Concat file at $dest has correct contents ($backendName)." );
839 function provider_testConcatenate() {
842 $rand = mt_rand( 0, 2000000000 ) . time();
843 $dest = wfTempDir() . "/randomfile!$rand.txt";
845 self
::baseStorePath() . '/unittest-cont1/e/file1.txt',
846 self
::baseStorePath() . '/unittest-cont1/e/file2.txt',
847 self
::baseStorePath() . '/unittest-cont1/e/file3.txt',
848 self
::baseStorePath() . '/unittest-cont1/e/file4.txt',
849 self
::baseStorePath() . '/unittest-cont1/e/file5.txt',
850 self
::baseStorePath() . '/unittest-cont1/e/file6.txt',
851 self
::baseStorePath() . '/unittest-cont1/e/file7.txt',
852 self
::baseStorePath() . '/unittest-cont1/e/file8.txt',
853 self
::baseStorePath() . '/unittest-cont1/e/file9.txt',
854 self
::baseStorePath() . '/unittest-cont1/e/file10.txt'
868 $params = array( 'srcs' => $srcs, 'dst' => $dest );
871 $params, // operation
873 $content, // content for each source
874 false, // no dest already exists
879 $params, // operation
881 $content, // content for each source
882 true, // dest already exists
890 * @dataProvider provider_testGetFileStat
892 public function testGetFileStat( $path, $content, $alreadyExists ) {
893 $this->backend
= $this->singleBackend
;
894 $this->tearDownFiles();
895 $this->doTestGetFileStat( $path, $content, $alreadyExists );
896 $this->tearDownFiles();
898 $this->backend
= $this->multiBackend
;
899 $this->tearDownFiles();
900 $this->doTestGetFileStat( $path, $content, $alreadyExists );
901 $this->tearDownFiles();
904 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
905 $backendName = $this->backendClass();
907 if ( $alreadyExists ) {
908 $this->prepare( array( 'dir' => dirname( $path ) ) );
909 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
910 $this->assertGoodStatus( $status,
911 "Creation of file at $path succeeded ($backendName)." );
913 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
914 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
915 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
917 $this->assertEquals( strlen( $content ), $size,
918 "Correct file size of '$path'" );
919 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
920 "Correct file timestamp of '$path'" );
922 $size = $stat['size'];
923 $time = $stat['mtime'];
924 $this->assertEquals( strlen( $content ), $size,
925 "Correct file size of '$path'" );
926 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
927 "Correct file timestamp of '$path'" );
929 $this->backend
->clearCache( array( $path ) );
931 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
933 $this->assertEquals( strlen( $content ), $size,
934 "Correct file size of '$path'" );
936 $this->backend
->preloadCache( array( $path ) );
938 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
940 $this->assertEquals( strlen( $content ), $size,
941 "Correct file size of '$path'" );
943 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
944 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
945 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
947 $this->assertFalse( $size, "Correct file size of '$path'" );
948 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
949 $this->assertFalse( $stat, "Correct file stat of '$path'" );
953 function provider_testGetFileStat() {
956 $base = self
::baseStorePath();
957 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
958 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
959 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
965 * @dataProvider provider_testGetFileContents
967 public function testGetFileContents( $source, $content ) {
968 $this->backend
= $this->singleBackend
;
969 $this->tearDownFiles();
970 $this->doTestGetFileContents( $source, $content );
971 $this->tearDownFiles();
973 $this->backend
= $this->multiBackend
;
974 $this->tearDownFiles();
975 $this->doTestGetFileContents( $source, $content );
976 $this->tearDownFiles();
979 private function doTestGetFileContents( $source, $content ) {
980 $backendName = $this->backendClass();
982 $srcs = (array)$source;
983 $content = (array)$content;
984 foreach ( $srcs as $i => $src ) {
985 $this->prepare( array( 'dir' => dirname( $src ) ) );
986 $status = $this->backend
->doOperation(
987 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
988 $this->assertGoodStatus( $status,
989 "Creation of file at $src succeeded ($backendName)." );
992 if ( is_array( $source ) ) {
993 $contents = $this->backend
->getFileContentsMulti( array( 'srcs' => $source ) );
994 foreach ( $contents as $path => $data ) {
995 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
996 $this->assertEquals( current( $content ), $data, "Contents of $path is correct ($backendName)." );
999 $this->assertEquals( $source, array_keys( $contents ), "Contents in right order ($backendName)." );
1000 $this->assertEquals( count( $source ), count( $contents ), "Contents array size correct ($backendName)." );
1002 $data = $this->backend
->getFileContents( array( 'src' => $source ) );
1003 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1004 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1008 function provider_testGetFileContents() {
1011 $base = self
::baseStorePath();
1012 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
1013 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
1015 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1016 "$base/unittest-cont1/e/a/z.txt" ),
1017 array( "contents xx", "contents xy", "contents xz" )
1024 * @dataProvider provider_testGetLocalCopy
1026 public function testGetLocalCopy( $source, $content ) {
1027 $this->backend
= $this->singleBackend
;
1028 $this->tearDownFiles();
1029 $this->doTestGetLocalCopy( $source, $content );
1030 $this->tearDownFiles();
1032 $this->backend
= $this->multiBackend
;
1033 $this->tearDownFiles();
1034 $this->doTestGetLocalCopy( $source, $content );
1035 $this->tearDownFiles();
1038 private function doTestGetLocalCopy( $source, $content ) {
1039 $backendName = $this->backendClass();
1041 $srcs = (array)$source;
1042 $content = (array)$content;
1043 foreach ( $srcs as $i => $src ) {
1044 $this->prepare( array( 'dir' => dirname( $src ) ) );
1045 $status = $this->backend
->doOperation(
1046 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1047 $this->assertGoodStatus( $status,
1048 "Creation of file at $src succeeded ($backendName)." );
1051 if ( is_array( $source ) ) {
1052 $tmpFiles = $this->backend
->getLocalCopyMulti( array( 'srcs' => $source ) );
1053 foreach ( $tmpFiles as $path => $tmpFile ) {
1054 $this->assertNotNull( $tmpFile,
1055 "Creation of local copy of $path succeeded ($backendName)." );
1056 $contents = file_get_contents( $tmpFile->getPath() );
1057 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1058 $this->assertEquals( current( $content ), $contents, "Local copy of $path is correct ($backendName)." );
1061 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local copies in right order ($backendName)." );
1062 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local copies array size correct ($backendName)." );
1064 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
1065 $this->assertNotNull( $tmpFile,
1066 "Creation of local copy of $source succeeded ($backendName)." );
1067 $contents = file_get_contents( $tmpFile->getPath() );
1068 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1069 $this->assertEquals( $content[0], $contents, "Local copy of $source is correct ($backendName)." );
1072 $obj = new stdClass();
1073 $tmpFile->bind( $obj );
1076 function provider_testGetLocalCopy() {
1079 $base = self
::baseStorePath();
1080 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1081 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1082 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1084 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1085 "$base/unittest-cont1/e/a/z.txt" ),
1086 array( "contents xx", "contents xy", "contents xz" )
1093 * @dataProvider provider_testGetLocalReference
1095 public function testGetLocalReference( $source, $content ) {
1096 $this->backend
= $this->singleBackend
;
1097 $this->tearDownFiles();
1098 $this->doTestGetLocalReference( $source, $content );
1099 $this->tearDownFiles();
1101 $this->backend
= $this->multiBackend
;
1102 $this->tearDownFiles();
1103 $this->doTestGetLocalReference( $source, $content );
1104 $this->tearDownFiles();
1107 private function doTestGetLocalReference( $source, $content ) {
1108 $backendName = $this->backendClass();
1110 $srcs = (array)$source;
1111 $content = (array)$content;
1112 foreach ( $srcs as $i => $src ) {
1113 $this->prepare( array( 'dir' => dirname( $src ) ) );
1114 $status = $this->backend
->doOperation(
1115 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1116 $this->assertGoodStatus( $status,
1117 "Creation of file at $src succeeded ($backendName)." );
1120 if ( is_array( $source ) ) {
1121 $tmpFiles = $this->backend
->getLocalReferenceMulti( array( 'srcs' => $source ) );
1122 foreach ( $tmpFiles as $path => $tmpFile ) {
1123 $this->assertNotNull( $tmpFile,
1124 "Creation of local copy of $path succeeded ($backendName)." );
1125 $contents = file_get_contents( $tmpFile->getPath() );
1126 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1127 $this->assertEquals( current( $content ), $contents, "Local ref of $path is correct ($backendName)." );
1130 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local refs in right order ($backendName)." );
1131 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local refs array size correct ($backendName)." );
1133 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
1134 $this->assertNotNull( $tmpFile,
1135 "Creation of local copy of $source succeeded ($backendName)." );
1136 $contents = file_get_contents( $tmpFile->getPath() );
1137 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1138 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1142 function provider_testGetLocalReference() {
1145 $base = self
::baseStorePath();
1146 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1147 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1148 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1150 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1151 "$base/unittest-cont1/e/a/z.txt" ),
1152 array( "contents xx", "contents xy", "contents xz" )
1158 public function testGetLocalCopyAndReference404() {
1159 $this->backend
= $this->singleBackend
;
1160 $this->tearDownFiles();
1161 $this->doTestGetLocalCopyAndReference404();
1162 $this->tearDownFiles();
1164 $this->backend
= $this->multiBackend
;
1165 $this->tearDownFiles();
1166 $this->doTestGetLocalCopyAndReference404();
1167 $this->tearDownFiles();
1170 public function doTestGetLocalCopyAndReference404() {
1171 $backendName = $this->backendClass();
1173 $base = self
::baseStorePath();
1175 $tmpFile = $this->backend
->getLocalCopy( array(
1176 'src' => "$base/unittest-cont1/not-there" ) );
1177 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1179 $tmpFile = $this->backend
->getLocalReference( array(
1180 'src' => "$base/unittest-cont1/not-there" ) );
1181 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1185 * @dataProvider provider_testPrepareAndClean
1187 public function testPrepareAndClean( $path, $isOK ) {
1188 $this->backend
= $this->singleBackend
;
1189 $this->doTestPrepareAndClean( $path, $isOK );
1190 $this->tearDownFiles();
1192 $this->backend
= $this->multiBackend
;
1193 $this->doTestPrepareAndClean( $path, $isOK );
1194 $this->tearDownFiles();
1197 function provider_testPrepareAndClean() {
1198 $base = self
::baseStorePath();
1200 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1201 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1202 # Specific to FS backend with no basePath field set
1203 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1207 private function doTestPrepareAndClean( $path, $isOK ) {
1208 $backendName = $this->backendClass();
1210 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1212 $this->assertGoodStatus( $status,
1213 "Preparing dir $path succeeded without warnings ($backendName)." );
1214 $this->assertEquals( true, $status->isOK(),
1215 "Preparing dir $path succeeded ($backendName)." );
1217 $this->assertEquals( false, $status->isOK(),
1218 "Preparing dir $path failed ($backendName)." );
1221 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1223 $this->assertGoodStatus( $status,
1224 "Cleaning dir $path succeeded without warnings ($backendName)." );
1225 $this->assertEquals( true, $status->isOK(),
1226 "Cleaning dir $path succeeded ($backendName)." );
1228 $this->assertEquals( false, $status->isOK(),
1229 "Cleaning dir $path failed ($backendName)." );
1233 public function testRecursiveClean() {
1234 $this->backend
= $this->singleBackend
;
1235 $this->doTestRecursiveClean();
1236 $this->tearDownFiles();
1238 $this->backend
= $this->multiBackend
;
1239 $this->doTestRecursiveClean();
1240 $this->tearDownFiles();
1243 private function doTestRecursiveClean() {
1244 $backendName = $this->backendClass();
1246 $base = self
::baseStorePath();
1248 "$base/unittest-cont1/e/a",
1249 "$base/unittest-cont1/e/a/b",
1250 "$base/unittest-cont1/e/a/b/c",
1251 "$base/unittest-cont1/e/a/b/c/d0",
1252 "$base/unittest-cont1/e/a/b/c/d1",
1253 "$base/unittest-cont1/e/a/b/c/d2",
1254 "$base/unittest-cont1/e/a/b/c/d0/1",
1255 "$base/unittest-cont1/e/a/b/c/d0/2",
1256 "$base/unittest-cont1/e/a/b/c/d1/3",
1257 "$base/unittest-cont1/e/a/b/c/d1/4",
1258 "$base/unittest-cont1/e/a/b/c/d2/5",
1259 "$base/unittest-cont1/e/a/b/c/d2/6"
1261 foreach ( $dirs as $dir ) {
1262 $status = $this->prepare( array( 'dir' => $dir ) );
1263 $this->assertGoodStatus( $status,
1264 "Preparing dir $dir succeeded without warnings ($backendName)." );
1267 if ( $this->backend
instanceof FSFileBackend
) {
1268 foreach ( $dirs as $dir ) {
1269 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1270 "Dir $dir exists ($backendName)." );
1274 $status = $this->backend
->clean(
1275 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1276 $this->assertGoodStatus( $status,
1277 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1279 foreach ( $dirs as $dir ) {
1280 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1281 "Dir $dir no longer exists ($backendName)." );
1285 // @TODO: testSecure
1287 public function testDoOperations() {
1288 $this->backend
= $this->singleBackend
;
1289 $this->tearDownFiles();
1290 $this->doTestDoOperations();
1291 $this->tearDownFiles();
1293 $this->backend
= $this->multiBackend
;
1294 $this->tearDownFiles();
1295 $this->doTestDoOperations();
1296 $this->tearDownFiles();
1299 private function doTestDoOperations() {
1300 $base = self
::baseStorePath();
1302 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1303 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1304 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1305 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1306 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1307 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1308 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1310 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1311 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1312 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1313 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1314 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1315 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1316 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1318 $status = $this->backend
->doOperations( array(
1319 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1320 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1321 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1322 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1323 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1324 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1325 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1326 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1327 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1328 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1329 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1330 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1331 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1332 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1333 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1334 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1335 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1337 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1339 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1341 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1343 array( 'op' => 'null' ),
1347 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1348 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1349 $this->assertEquals( 13, count( $status->success
),
1350 "Operation batch has correct success array" );
1352 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1353 "File does not exist at $fileA" );
1354 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1355 "File does not exist at $fileB" );
1356 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1357 "File does not exist at $fileD" );
1359 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1360 "File exists at $fileC" );
1361 $this->assertEquals( $fileBContents,
1362 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1363 "Correct file contents of $fileC" );
1364 $this->assertEquals( strlen( $fileBContents ),
1365 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1366 "Correct file size of $fileC" );
1367 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1368 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1369 "Correct file SHA-1 of $fileC" );
1372 public function testDoOperationsPipeline() {
1373 $this->backend
= $this->singleBackend
;
1374 $this->tearDownFiles();
1375 $this->doTestDoOperationsPipeline();
1376 $this->tearDownFiles();
1378 $this->backend
= $this->multiBackend
;
1379 $this->tearDownFiles();
1380 $this->doTestDoOperationsPipeline();
1381 $this->tearDownFiles();
1384 // concurrency orientated
1385 private function doTestDoOperationsPipeline() {
1386 $base = self
::baseStorePath();
1388 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1389 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1390 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1392 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1393 file_put_contents( $tmpNameA, $fileAContents );
1394 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1395 file_put_contents( $tmpNameB, $fileBContents );
1396 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1397 file_put_contents( $tmpNameC, $fileCContents );
1399 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1400 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1401 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1403 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1404 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1405 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1406 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1408 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1409 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1410 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1411 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1412 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1414 $status = $this->backend
->doOperations( array(
1415 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1416 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1417 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1418 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1419 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1420 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1421 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1422 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1423 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1424 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1425 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1426 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1427 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1428 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1429 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1430 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1431 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1432 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1433 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1434 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1436 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1438 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1440 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1442 array( 'op' => 'null' ),
1446 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1447 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1448 $this->assertEquals( 16, count( $status->success
),
1449 "Operation batch has correct success array" );
1451 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1452 "File does not exist at $fileA" );
1453 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1454 "File does not exist at $fileB" );
1455 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1456 "File does not exist at $fileD" );
1458 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1459 "File exists at $fileC" );
1460 $this->assertEquals( $fileBContents,
1461 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1462 "Correct file contents of $fileC" );
1463 $this->assertEquals( strlen( $fileBContents ),
1464 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1465 "Correct file size of $fileC" );
1466 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1467 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1468 "Correct file SHA-1 of $fileC" );
1471 public function testDoOperationsFailing() {
1472 $this->backend
= $this->singleBackend
;
1473 $this->tearDownFiles();
1474 $this->doTestDoOperationsFailing();
1475 $this->tearDownFiles();
1477 $this->backend
= $this->multiBackend
;
1478 $this->tearDownFiles();
1479 $this->doTestDoOperationsFailing();
1480 $this->tearDownFiles();
1483 private function doTestDoOperationsFailing() {
1484 $base = self
::baseStorePath();
1486 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1487 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1488 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1489 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1490 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1491 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1492 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1494 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1495 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1496 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1497 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1498 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1499 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1501 $status = $this->backend
->doOperations( array(
1502 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1503 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1504 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1505 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1506 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1507 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1508 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1509 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1510 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1511 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1512 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1513 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1514 array( 'op' => 'delete', 'src' => $fileD ),
1515 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1516 array( 'op' => 'null' ),
1518 ), array( 'force' => 1 ) );
1520 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1521 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1522 $this->assertEquals( 8, count( $status->success
),
1523 "Operation batch has correct success array" );
1525 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1526 "File does not exist at $fileB" );
1527 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1528 "File does not exist at $fileD" );
1530 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1531 "File does not exist at $fileA" );
1532 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1533 "File exists at $fileC" );
1534 $this->assertEquals( $fileBContents,
1535 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1536 "Correct file contents of $fileA" );
1537 $this->assertEquals( strlen( $fileBContents ),
1538 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1539 "Correct file size of $fileA" );
1540 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1541 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1542 "Correct file SHA-1 of $fileA" );
1545 public function testGetFileList() {
1546 $this->backend
= $this->singleBackend
;
1547 $this->tearDownFiles();
1548 $this->doTestGetFileList();
1549 $this->tearDownFiles();
1551 $this->backend
= $this->multiBackend
;
1552 $this->tearDownFiles();
1553 $this->doTestGetFileList();
1554 $this->tearDownFiles();
1557 private function doTestGetFileList() {
1558 $backendName = $this->backendClass();
1559 $base = self
::baseStorePath();
1561 // Should have no errors
1562 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1565 "$base/unittest-cont1/e/test1.txt",
1566 "$base/unittest-cont1/e/test2.txt",
1567 "$base/unittest-cont1/e/test3.txt",
1568 "$base/unittest-cont1/e/subdir1/test1.txt",
1569 "$base/unittest-cont1/e/subdir1/test2.txt",
1570 "$base/unittest-cont1/e/subdir2/test3.txt",
1571 "$base/unittest-cont1/e/subdir2/test4.txt",
1572 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1573 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1574 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1575 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1576 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1577 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1578 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1583 foreach ( $files as $file ) {
1584 $this->prepare( array( 'dir' => dirname( $file ) ) );
1585 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1587 $status = $this->backend
->doQuickOperations( $ops );
1588 $this->assertGoodStatus( $status,
1589 "Creation of files succeeded ($backendName)." );
1590 $this->assertEquals( true, $status->isOK(),
1591 "Creation of files succeeded with OK status ($backendName)." );
1598 "e/subdir1/test1.txt",
1599 "e/subdir1/test2.txt",
1600 "e/subdir2/test3.txt",
1601 "e/subdir2/test4.txt",
1602 "e/subdir2/subdir/test1.txt",
1603 "e/subdir2/subdir/test2.txt",
1604 "e/subdir2/subdir/test3.txt",
1605 "e/subdir2/subdir/test4.txt",
1606 "e/subdir2/subdir/test5.txt",
1607 "e/subdir2/subdir/sub/test0.txt",
1608 "e/subdir2/subdir/sub/120-px-file.txt",
1612 // Actual listing (no trailing slash)
1614 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1615 foreach ( $iter as $file ) {
1620 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1622 // Actual listing (with trailing slash)
1624 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1625 foreach ( $iter as $file ) {
1630 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1640 "sub/120-px-file.txt",
1644 // Actual listing (no trailing slash)
1646 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1647 foreach ( $iter as $file ) {
1652 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1654 // Actual listing (with trailing slash)
1656 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1657 foreach ( $iter as $file ) {
1662 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1664 // Actual listing (using iterator second time)
1666 foreach ( $iter as $file ) {
1671 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1673 // Expected listing (top files only)
1683 // Actual listing (top files only)
1685 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1686 foreach ( $iter as $file ) {
1691 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1693 foreach ( $files as $file ) { // clean up
1694 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1697 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1698 foreach ( $iter as $iter ) {} // no errors
1701 public function testGetDirectoryList() {
1702 $this->backend
= $this->singleBackend
;
1703 $this->tearDownFiles();
1704 $this->doTestGetDirectoryList();
1705 $this->tearDownFiles();
1707 $this->backend
= $this->multiBackend
;
1708 $this->tearDownFiles();
1709 $this->doTestGetDirectoryList();
1710 $this->tearDownFiles();
1713 private function doTestGetDirectoryList() {
1714 $backendName = $this->backendClass();
1716 $base = self
::baseStorePath();
1718 "$base/unittest-cont1/e/test1.txt",
1719 "$base/unittest-cont1/e/test2.txt",
1720 "$base/unittest-cont1/e/test3.txt",
1721 "$base/unittest-cont1/e/subdir1/test1.txt",
1722 "$base/unittest-cont1/e/subdir1/test2.txt",
1723 "$base/unittest-cont1/e/subdir2/test3.txt",
1724 "$base/unittest-cont1/e/subdir2/test4.txt",
1725 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1726 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1727 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1728 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1729 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1730 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1731 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1736 foreach ( $files as $file ) {
1737 $this->prepare( array( 'dir' => dirname( $file ) ) );
1738 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1740 $status = $this->backend
->doQuickOperations( $ops );
1741 $this->assertGoodStatus( $status,
1742 "Creation of files succeeded ($backendName)." );
1743 $this->assertEquals( true, $status->isOK(),
1744 "Creation of files succeeded with OK status ($backendName)." );
1746 $this->assertEquals( true,
1747 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1748 "Directory exists in ($backendName)." );
1749 $this->assertEquals( true,
1750 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1751 "Directory exists in ($backendName)." );
1752 $this->assertEquals( false,
1753 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1754 "Directory does not exists in ($backendName)." );
1762 // Actual listing (no trailing slash)
1764 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1765 foreach ( $iter as $file ) {
1770 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1781 // Actual listing (no trailing slash)
1783 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1784 foreach ( $iter as $file ) {
1789 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1791 // Actual listing (with trailing slash)
1793 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1794 foreach ( $iter as $file ) {
1799 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1807 // Actual listing (no trailing slash)
1809 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1810 foreach ( $iter as $file ) {
1815 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1817 // Actual listing (with trailing slash)
1819 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1820 foreach ( $iter as $file ) {
1825 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1827 // Actual listing (using iterator second time)
1829 foreach ( $iter as $file ) {
1834 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1836 // Expected listing (recursive)
1846 "e/subdir4/subdir/sub",
1850 // Actual listing (recursive)
1852 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1853 foreach ( $iter as $file ) {
1858 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1860 // Expected listing (recursive)
1867 // Actual listing (recursive)
1869 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
1870 foreach ( $iter as $file ) {
1875 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1877 // Actual listing (recursive, second time)
1879 foreach ( $iter as $file ) {
1884 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1886 foreach ( $files as $file ) { // clean up
1887 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1890 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1891 foreach ( $iter as $iter ) {} // no errors
1894 public function testLockCalls() {
1895 $this->backend
= $this->singleBackend
;
1896 $this->doTestLockCalls();
1899 private function doTestLockCalls() {
1900 $backendName = $this->backendClass();
1902 for ( $i=0; $i<50; $i++
) {
1908 "subdir1", // duplicate
1909 "subdir1/test1.txt",
1910 "subdir1/test2.txt",
1912 "subdir2", // duplicate
1913 "subdir2/test3.txt",
1914 "subdir2/test4.txt",
1916 "subdir2/subdir/test1.txt",
1917 "subdir2/subdir/test2.txt",
1918 "subdir2/subdir/test3.txt",
1919 "subdir2/subdir/test4.txt",
1920 "subdir2/subdir/test5.txt",
1921 "subdir2/subdir/sub",
1922 "subdir2/subdir/sub/test0.txt",
1923 "subdir2/subdir/sub/120-px-file.txt",
1926 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
1927 $this->assertEquals( array(), $status->errors
,
1928 "Locking of files succeeded ($backendName)." );
1929 $this->assertEquals( true, $status->isOK(),
1930 "Locking of files succeeded with OK status ($backendName)." );
1932 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
1933 $this->assertEquals( array(), $status->errors
,
1934 "Locking of files succeeded ($backendName)." );
1935 $this->assertEquals( true, $status->isOK(),
1936 "Locking of files succeeded with OK status ($backendName)." );
1938 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
1939 $this->assertEquals( array(), $status->errors
,
1940 "Locking of files succeeded ($backendName)." );
1941 $this->assertEquals( true, $status->isOK(),
1942 "Locking of files succeeded with OK status ($backendName)." );
1944 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
1945 $this->assertEquals( array(), $status->errors
,
1946 "Locking of files succeeded ($backendName)." );
1947 $this->assertEquals( true, $status->isOK(),
1948 "Locking of files succeeded with OK status ($backendName)." );
1952 // test helper wrapper for backend prepare() function
1953 private function prepare( array $params ) {
1954 return $this->backend
->prepare( $params );
1957 // test helper wrapper for backend prepare() function
1958 private function create( array $params ) {
1959 $params['op'] = 'create';
1960 return $this->backend
->doQuickOperations( array( $params ) );
1963 function tearDownFiles() {
1964 foreach ( $this->filesToPrune
as $file ) {
1967 $containers = array( 'unittest-cont1', 'unittest-cont2' );
1968 foreach ( $containers as $container ) {
1969 $this->deleteFiles( $container );
1971 $this->filesToPrune
= array();
1974 private function deleteFiles( $container ) {
1975 $base = self
::baseStorePath();
1976 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
1978 foreach ( $iter as $file ) {
1979 $this->backend
->quickDelete( array( 'src' => "$base/$container/$file" ) );
1982 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
1985 function assertBackendPathsConsistent( array $paths ) {
1986 if ( $this->backend
instanceof FileBackendMultiWrite
) {
1987 $status = $this->backend
->consistencyCheck( $paths );
1988 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
1992 function assertGoodStatus( $status, $msg ) {
1993 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );