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 $status = $this->backend
->doOperation(
306 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
307 $this->assertGoodStatus( $status,
308 "Creation of file at $source succeeded ($backendName)." );
310 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
311 $this->backend
->copy( $op );
314 $status = $this->backend
->doOperation( $op );
316 $this->assertGoodStatus( $status,
317 "Copy from $source to $dest succeeded without warnings ($backendName)." );
318 $this->assertEquals( true, $status->isOK(),
319 "Copy from $source to $dest succeeded ($backendName)." );
320 $this->assertEquals( array( 0 => true ), $status->success
,
321 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
322 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
323 "Source file $source still exists ($backendName)." );
324 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
325 "Destination file $dest exists after copy ($backendName)." );
328 $this->backend
->getFileSize( array( 'src' => $source ) ),
329 $this->backend
->getFileSize( array( 'src' => $dest ) ),
330 "Destination file $dest has correct size ($backendName)." );
332 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
333 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
334 $this->assertEquals( $props1, $props2,
335 "Source and destination have the same props ($backendName)." );
337 $this->assertBackendPathsConsistent( array( $source, $dest ) );
340 public static function provider_testCopy() {
343 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
344 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
346 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
354 $op2['overwrite'] = true;
362 $op2['overwriteSame'] = true;
373 * @dataProvider provider_testMove
375 public function testMove( $op ) {
376 $this->backend
= $this->singleBackend
;
377 $this->tearDownFiles();
378 $this->doTestMove( $op );
379 $this->tearDownFiles();
381 $this->backend
= $this->multiBackend
;
382 $this->tearDownFiles();
383 $this->doTestMove( $op );
384 $this->tearDownFiles();
387 private function doTestMove( $op ) {
388 $backendName = $this->backendClass();
390 $source = $op['src'];
392 $this->prepare( array( 'dir' => dirname( $source ) ) );
393 $this->prepare( array( 'dir' => dirname( $dest ) ) );
395 $status = $this->backend
->doOperation(
396 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
397 $this->assertGoodStatus( $status,
398 "Creation of file at $source succeeded ($backendName)." );
400 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
401 $this->backend
->copy( $op );
404 $status = $this->backend
->doOperation( $op );
405 $this->assertGoodStatus( $status,
406 "Move from $source to $dest succeeded without warnings ($backendName)." );
407 $this->assertEquals( true, $status->isOK(),
408 "Move from $source to $dest succeeded ($backendName)." );
409 $this->assertEquals( array( 0 => true ), $status->success
,
410 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
411 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
412 "Source file $source does not still exists ($backendName)." );
413 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
414 "Destination file $dest exists after move ($backendName)." );
416 $this->assertNotEquals(
417 $this->backend
->getFileSize( array( 'src' => $source ) ),
418 $this->backend
->getFileSize( array( 'src' => $dest ) ),
419 "Destination file $dest has correct size ($backendName)." );
421 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
422 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
423 $this->assertEquals( false, $props1['fileExists'],
424 "Source file does not exist accourding to props ($backendName)." );
425 $this->assertEquals( true, $props2['fileExists'],
426 "Destination file exists accourding to props ($backendName)." );
428 $this->assertBackendPathsConsistent( array( $source, $dest ) );
431 public static function provider_testMove() {
434 $source = self
::baseStorePath() . '/unittest-cont1/e/file.txt';
435 $dest = self
::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
437 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
445 $op2['overwrite'] = true;
453 $op2['overwriteSame'] = true;
464 * @dataProvider provider_testDelete
466 public function testDelete( $op, $withSource, $okStatus ) {
467 $this->backend
= $this->singleBackend
;
468 $this->tearDownFiles();
469 $this->doTestDelete( $op, $withSource, $okStatus );
470 $this->tearDownFiles();
472 $this->backend
= $this->multiBackend
;
473 $this->tearDownFiles();
474 $this->doTestDelete( $op, $withSource, $okStatus );
475 $this->tearDownFiles();
478 private function doTestDelete( $op, $withSource, $okStatus ) {
479 $backendName = $this->backendClass();
481 $source = $op['src'];
482 $this->prepare( array( 'dir' => dirname( $source ) ) );
485 $status = $this->backend
->doOperation(
486 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
487 $this->assertGoodStatus( $status,
488 "Creation of file at $source succeeded ($backendName)." );
491 $status = $this->backend
->doOperation( $op );
493 $this->assertGoodStatus( $status,
494 "Deletion of file at $source succeeded without warnings ($backendName)." );
495 $this->assertEquals( true, $status->isOK(),
496 "Deletion of file at $source succeeded ($backendName)." );
497 $this->assertEquals( array( 0 => true ), $status->success
,
498 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
500 $this->assertEquals( false, $status->isOK(),
501 "Deletion of file at $source failed ($backendName)." );
504 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
505 "Source file $source does not exist after move ($backendName)." );
508 $this->backend
->getFileSize( array( 'src' => $source ) ),
509 "Source file $source has correct size (false) ($backendName)." );
511 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
512 $this->assertFalse( $props1['fileExists'],
513 "Source file $source does not exist according to props ($backendName)." );
515 $this->assertBackendPathsConsistent( array( $source ) );
518 public static function provider_testDelete() {
521 $source = self
::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
523 $op = array( 'op' => 'delete', 'src' => $source );
532 false, // without source
536 $op['ignoreMissingSource'] = true;
539 false, // without source
547 * @dataProvider provider_testCreate
549 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
550 $this->backend
= $this->singleBackend
;
551 $this->tearDownFiles();
552 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
553 $this->tearDownFiles();
555 $this->backend
= $this->multiBackend
;
556 $this->tearDownFiles();
557 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
558 $this->tearDownFiles();
561 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
562 $backendName = $this->backendClass();
565 $this->prepare( array( 'dir' => dirname( $dest ) ) );
567 $oldText = 'blah...blah...waahwaah';
568 if ( $alreadyExists ) {
569 $status = $this->backend
->doOperation(
570 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
571 $this->assertGoodStatus( $status,
572 "Creation of file at $dest succeeded ($backendName)." );
575 $status = $this->backend
->doOperation( $op );
577 $this->assertGoodStatus( $status,
578 "Creation of file at $dest succeeded without warnings ($backendName)." );
579 $this->assertEquals( true, $status->isOK(),
580 "Creation of file at $dest succeeded ($backendName)." );
581 $this->assertEquals( array( 0 => true ), $status->success
,
582 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
584 $this->assertEquals( false, $status->isOK(),
585 "Creation of file at $dest failed ($backendName)." );
588 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
589 "Destination file $dest exists after creation ($backendName)." );
591 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
592 $this->assertEquals( true, $props1['fileExists'],
593 "Destination file $dest exists according to props ($backendName)." );
594 if ( $okStatus ) { // file content is what we saved
595 $this->assertEquals( $newSize, $props1['size'],
596 "Destination file $dest has expected size according to props ($backendName)." );
597 $this->assertEquals( $newSize,
598 $this->backend
->getFileSize( array( 'src' => $dest ) ),
599 "Destination file $dest has correct size ($backendName)." );
600 } else { // file content is some other previous text
601 $this->assertEquals( strlen( $oldText ), $props1['size'],
602 "Destination file $dest has original size according to props ($backendName)." );
603 $this->assertEquals( strlen( $oldText ),
604 $this->backend
->getFileSize( array( 'src' => $dest ) ),
605 "Destination file $dest has original size according to props ($backendName)." );
608 $this->assertBackendPathsConsistent( array( $dest ) );
612 * @dataProvider provider_testCreate
614 public static function provider_testCreate() {
617 $dest = self
::baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
619 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
622 false, // no dest already exists
624 strlen( $op['content'] )
628 $op2['content'] = "\n";
631 false, // no dest already exists
633 strlen( $op2['content'] )
637 $op2['content'] = "fsf\n waf 3kt";
640 true, // dest already exists
642 strlen( $op2['content'] )
646 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
647 $op2['overwrite'] = true;
650 true, // dest already exists
652 strlen( $op2['content'] )
656 $op2['content'] = "39qjmg3-qg";
657 $op2['overwriteSame'] = true;
660 true, // dest already exists
662 strlen( $op2['content'] )
668 public function testDoQuickOperations() {
669 $this->backend
= $this->singleBackend
;
670 $this->doTestDoQuickOperations();
671 $this->tearDownFiles();
673 $this->backend
= $this->multiBackend
;
674 $this->doTestDoQuickOperations();
675 $this->tearDownFiles();
678 private function doTestDoQuickOperations() {
679 $backendName = $this->backendClass();
681 $base = self
::baseStorePath();
683 "$base/unittest-cont1/e/fileA.a",
684 "$base/unittest-cont1/e/fileB.a",
685 "$base/unittest-cont1/e/fileC.a"
689 foreach ( $files as $path ) {
690 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
691 $this->assertGoodStatus( $status,
692 "Preparing $path succeeded without warnings ($backendName)." );
693 $ops[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand(0,50000) );
694 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
696 $purgeOps[] = array( 'op' => 'null' );
697 $status = $this->backend
->doQuickOperations( $ops );
698 $this->assertGoodStatus( $status,
699 "Creation of source files succeeded ($backendName)." );
701 foreach ( $files as $file ) {
702 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $file ) ),
703 "File $file exists." );
706 $status = $this->backend
->doQuickOperations( $purgeOps );
707 $this->assertGoodStatus( $status,
708 "Quick deletion of source files succeeded ($backendName)." );
710 foreach ( $files as $file ) {
711 $this->assertFalse( $this->backend
->fileExists( array( 'src' => $file ) ),
712 "File $file purged." );
717 * @dataProvider provider_testConcatenate
719 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
720 $this->filesToPrune
[] = $op['dst'];
722 $this->backend
= $this->singleBackend
;
723 $this->tearDownFiles();
724 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
725 $this->tearDownFiles();
727 $this->backend
= $this->multiBackend
;
728 $this->tearDownFiles();
729 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
730 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
731 $this->tearDownFiles();
734 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
735 $backendName = $this->backendClass();
740 foreach ( $srcs as $i => $source ) {
741 $this->prepare( array( 'dir' => dirname( $source ) ) );
743 'op' => 'create', // operation
744 'dst' => $source, // source
745 'content' => $srcsContent[$i]
747 $expContent .= $srcsContent[$i];
749 $status = $this->backend
->doOperations( $ops );
751 $this->assertGoodStatus( $status,
752 "Creation of source files succeeded ($backendName)." );
754 $dest = $params['dst'];
755 if ( $alreadyExists ) {
756 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
757 $this->assertEquals( true, $ok,
758 "Creation of file at $dest succeeded ($backendName)." );
760 $ok = file_put_contents( $dest, '' ) !== false;
761 $this->assertEquals( true, $ok,
762 "Creation of 0-byte file at $dest succeeded ($backendName)." );
765 // Combine the files into one
766 $status = $this->backend
->concatenate( $params );
768 $this->assertGoodStatus( $status,
769 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
770 $this->assertEquals( true, $status->isOK(),
771 "Creation of concat file at $dest succeeded ($backendName)." );
773 $this->assertEquals( false, $status->isOK(),
774 "Creation of concat file at $dest failed ($backendName)." );
778 $this->assertEquals( true, is_file( $dest ),
779 "Dest concat file $dest exists after creation ($backendName)." );
781 $this->assertEquals( true, is_file( $dest ),
782 "Dest concat file $dest exists after failed creation ($backendName)." );
785 $contents = file_get_contents( $dest );
786 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
789 $this->assertEquals( $expContent, $contents,
790 "Concat file at $dest has correct contents ($backendName)." );
792 $this->assertNotEquals( $expContent, $contents,
793 "Concat file at $dest has correct contents ($backendName)." );
797 function provider_testConcatenate() {
800 $rand = mt_rand( 0, 2000000000 ) . time();
801 $dest = wfTempDir() . "/randomfile!$rand.txt";
803 self
::baseStorePath() . '/unittest-cont1/e/file1.txt',
804 self
::baseStorePath() . '/unittest-cont1/e/file2.txt',
805 self
::baseStorePath() . '/unittest-cont1/e/file3.txt',
806 self
::baseStorePath() . '/unittest-cont1/e/file4.txt',
807 self
::baseStorePath() . '/unittest-cont1/e/file5.txt',
808 self
::baseStorePath() . '/unittest-cont1/e/file6.txt',
809 self
::baseStorePath() . '/unittest-cont1/e/file7.txt',
810 self
::baseStorePath() . '/unittest-cont1/e/file8.txt',
811 self
::baseStorePath() . '/unittest-cont1/e/file9.txt',
812 self
::baseStorePath() . '/unittest-cont1/e/file10.txt'
826 $params = array( 'srcs' => $srcs, 'dst' => $dest );
829 $params, // operation
831 $content, // content for each source
832 false, // no dest already exists
837 $params, // operation
839 $content, // content for each source
840 true, // dest already exists
848 * @dataProvider provider_testGetFileStat
850 public function testGetFileStat( $path, $content, $alreadyExists ) {
851 $this->backend
= $this->singleBackend
;
852 $this->tearDownFiles();
853 $this->doTestGetFileStat( $path, $content, $alreadyExists );
854 $this->tearDownFiles();
856 $this->backend
= $this->multiBackend
;
857 $this->tearDownFiles();
858 $this->doTestGetFileStat( $path, $content, $alreadyExists );
859 $this->tearDownFiles();
862 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
863 $backendName = $this->backendClass();
865 if ( $alreadyExists ) {
866 $this->prepare( array( 'dir' => dirname( $path ) ) );
867 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
868 $this->assertGoodStatus( $status,
869 "Creation of file at $path succeeded ($backendName)." );
871 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
872 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
873 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
875 $this->assertEquals( strlen( $content ), $size,
876 "Correct file size of '$path'" );
877 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
878 "Correct file timestamp of '$path'" );
880 $size = $stat['size'];
881 $time = $stat['mtime'];
882 $this->assertEquals( strlen( $content ), $size,
883 "Correct file size of '$path'" );
884 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
885 "Correct file timestamp of '$path'" );
887 $this->backend
->clearCache( array( $path ) );
889 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
891 $this->assertEquals( strlen( $content ), $size,
892 "Correct file size of '$path'" );
894 $this->backend
->preloadCache( array( $path ) );
896 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
898 $this->assertEquals( strlen( $content ), $size,
899 "Correct file size of '$path'" );
901 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
902 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
903 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
905 $this->assertFalse( $size, "Correct file size of '$path'" );
906 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
907 $this->assertFalse( $stat, "Correct file stat of '$path'" );
911 function provider_testGetFileStat() {
914 $base = self
::baseStorePath();
915 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
916 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
917 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
923 * @dataProvider provider_testGetFileContents
925 public function testGetFileContents( $source, $content ) {
926 $this->backend
= $this->singleBackend
;
927 $this->tearDownFiles();
928 $this->doTestGetFileContents( $source, $content );
929 $this->tearDownFiles();
931 $this->backend
= $this->multiBackend
;
932 $this->tearDownFiles();
933 $this->doTestGetFileContents( $source, $content );
934 $this->tearDownFiles();
937 private function doTestGetFileContents( $source, $content ) {
938 $backendName = $this->backendClass();
940 $srcs = (array)$source;
941 $content = (array)$content;
942 foreach ( $srcs as $i => $src ) {
943 $this->prepare( array( 'dir' => dirname( $src ) ) );
944 $status = $this->backend
->doOperation(
945 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
946 $this->assertGoodStatus( $status,
947 "Creation of file at $src succeeded ($backendName)." );
950 if ( is_array( $source ) ) {
951 $contents = $this->backend
->getFileContentsMulti( array( 'srcs' => $source ) );
952 foreach ( $contents as $path => $data ) {
953 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
954 $this->assertEquals( current( $content ), $data, "Contents of $path is correct ($backendName)." );
957 $this->assertEquals( $source, array_keys( $contents ), "Contents in right order ($backendName)." );
958 $this->assertEquals( count( $source ), count( $contents ), "Contents array size correct ($backendName)." );
960 $data = $this->backend
->getFileContents( array( 'src' => $source ) );
961 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
962 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
966 function provider_testGetFileContents() {
969 $base = self
::baseStorePath();
970 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
971 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
973 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
974 "$base/unittest-cont1/e/a/z.txt" ),
975 array( "contents xx", "contents xy", "contents xz" )
982 * @dataProvider provider_testGetLocalCopy
984 public function testGetLocalCopy( $source, $content ) {
985 $this->backend
= $this->singleBackend
;
986 $this->tearDownFiles();
987 $this->doTestGetLocalCopy( $source, $content );
988 $this->tearDownFiles();
990 $this->backend
= $this->multiBackend
;
991 $this->tearDownFiles();
992 $this->doTestGetLocalCopy( $source, $content );
993 $this->tearDownFiles();
996 private function doTestGetLocalCopy( $source, $content ) {
997 $backendName = $this->backendClass();
999 $srcs = (array)$source;
1000 $content = (array)$content;
1001 foreach ( $srcs as $i => $src ) {
1002 $this->prepare( array( 'dir' => dirname( $src ) ) );
1003 $status = $this->backend
->doOperation(
1004 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1005 $this->assertGoodStatus( $status,
1006 "Creation of file at $src succeeded ($backendName)." );
1009 if ( is_array( $source ) ) {
1010 $tmpFiles = $this->backend
->getLocalCopyMulti( array( 'srcs' => $source ) );
1011 foreach ( $tmpFiles as $path => $tmpFile ) {
1012 $this->assertNotNull( $tmpFile,
1013 "Creation of local copy of $path succeeded ($backendName)." );
1014 $contents = file_get_contents( $tmpFile->getPath() );
1015 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1016 $this->assertEquals( current( $content ), $contents, "Local copy of $path is correct ($backendName)." );
1019 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local copies in right order ($backendName)." );
1020 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local copies array size correct ($backendName)." );
1022 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
1023 $this->assertNotNull( $tmpFile,
1024 "Creation of local copy of $source succeeded ($backendName)." );
1025 $contents = file_get_contents( $tmpFile->getPath() );
1026 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1027 $this->assertEquals( $content[0], $contents, "Local copy of $source is correct ($backendName)." );
1030 $obj = new stdClass();
1031 $tmpFile->bind( $obj );
1034 function provider_testGetLocalCopy() {
1037 $base = self
::baseStorePath();
1038 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1039 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1040 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1042 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1043 "$base/unittest-cont1/e/a/z.txt" ),
1044 array( "contents xx", "contents xy", "contents xz" )
1051 * @dataProvider provider_testGetLocalReference
1053 public function testGetLocalReference( $source, $content ) {
1054 $this->backend
= $this->singleBackend
;
1055 $this->tearDownFiles();
1056 $this->doTestGetLocalReference( $source, $content );
1057 $this->tearDownFiles();
1059 $this->backend
= $this->multiBackend
;
1060 $this->tearDownFiles();
1061 $this->doTestGetLocalReference( $source, $content );
1062 $this->tearDownFiles();
1065 private function doTestGetLocalReference( $source, $content ) {
1066 $backendName = $this->backendClass();
1068 $srcs = (array)$source;
1069 $content = (array)$content;
1070 foreach ( $srcs as $i => $src ) {
1071 $this->prepare( array( 'dir' => dirname( $src ) ) );
1072 $status = $this->backend
->doOperation(
1073 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1074 $this->assertGoodStatus( $status,
1075 "Creation of file at $src succeeded ($backendName)." );
1078 if ( is_array( $source ) ) {
1079 $tmpFiles = $this->backend
->getLocalReferenceMulti( array( 'srcs' => $source ) );
1080 foreach ( $tmpFiles as $path => $tmpFile ) {
1081 $this->assertNotNull( $tmpFile,
1082 "Creation of local copy of $path succeeded ($backendName)." );
1083 $contents = file_get_contents( $tmpFile->getPath() );
1084 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1085 $this->assertEquals( current( $content ), $contents, "Local ref of $path is correct ($backendName)." );
1088 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local refs in right order ($backendName)." );
1089 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local refs array size correct ($backendName)." );
1091 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
1092 $this->assertNotNull( $tmpFile,
1093 "Creation of local copy of $source succeeded ($backendName)." );
1094 $contents = file_get_contents( $tmpFile->getPath() );
1095 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1096 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1100 function provider_testGetLocalReference() {
1103 $base = self
::baseStorePath();
1104 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1105 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1106 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1108 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1109 "$base/unittest-cont1/e/a/z.txt" ),
1110 array( "contents xx", "contents xy", "contents xz" )
1116 public function testGetLocalCopyAndReference404() {
1117 $this->backend
= $this->singleBackend
;
1118 $this->tearDownFiles();
1119 $this->doTestGetLocalCopyAndReference404();
1120 $this->tearDownFiles();
1122 $this->backend
= $this->multiBackend
;
1123 $this->tearDownFiles();
1124 $this->doTestGetLocalCopyAndReference404();
1125 $this->tearDownFiles();
1128 public function doTestGetLocalCopyAndReference404() {
1129 $backendName = $this->backendClass();
1131 $base = self
::baseStorePath();
1133 $tmpFile = $this->backend
->getLocalCopy( array(
1134 'src' => "$base/unittest-cont1/not-there" ) );
1135 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1137 $tmpFile = $this->backend
->getLocalReference( array(
1138 'src' => "$base/unittest-cont1/not-there" ) );
1139 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1143 * @dataProvider provider_testPrepareAndClean
1145 public function testPrepareAndClean( $path, $isOK ) {
1146 $this->backend
= $this->singleBackend
;
1147 $this->doTestPrepareAndClean( $path, $isOK );
1148 $this->tearDownFiles();
1150 $this->backend
= $this->multiBackend
;
1151 $this->doTestPrepareAndClean( $path, $isOK );
1152 $this->tearDownFiles();
1155 function provider_testPrepareAndClean() {
1156 $base = self
::baseStorePath();
1158 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1159 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1160 # Specific to FS backend with no basePath field set
1161 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1165 private function doTestPrepareAndClean( $path, $isOK ) {
1166 $backendName = $this->backendClass();
1168 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1170 $this->assertGoodStatus( $status,
1171 "Preparing dir $path succeeded without warnings ($backendName)." );
1172 $this->assertEquals( true, $status->isOK(),
1173 "Preparing dir $path succeeded ($backendName)." );
1175 $this->assertEquals( false, $status->isOK(),
1176 "Preparing dir $path failed ($backendName)." );
1179 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1181 $this->assertGoodStatus( $status,
1182 "Cleaning dir $path succeeded without warnings ($backendName)." );
1183 $this->assertEquals( true, $status->isOK(),
1184 "Cleaning dir $path succeeded ($backendName)." );
1186 $this->assertEquals( false, $status->isOK(),
1187 "Cleaning dir $path failed ($backendName)." );
1191 public function testRecursiveClean() {
1192 $this->backend
= $this->singleBackend
;
1193 $this->doTestRecursiveClean();
1194 $this->tearDownFiles();
1196 $this->backend
= $this->multiBackend
;
1197 $this->doTestRecursiveClean();
1198 $this->tearDownFiles();
1201 private function doTestRecursiveClean() {
1202 $backendName = $this->backendClass();
1204 $base = self
::baseStorePath();
1206 "$base/unittest-cont1/e/a",
1207 "$base/unittest-cont1/e/a/b",
1208 "$base/unittest-cont1/e/a/b/c",
1209 "$base/unittest-cont1/e/a/b/c/d0",
1210 "$base/unittest-cont1/e/a/b/c/d1",
1211 "$base/unittest-cont1/e/a/b/c/d2",
1212 "$base/unittest-cont1/e/a/b/c/d0/1",
1213 "$base/unittest-cont1/e/a/b/c/d0/2",
1214 "$base/unittest-cont1/e/a/b/c/d1/3",
1215 "$base/unittest-cont1/e/a/b/c/d1/4",
1216 "$base/unittest-cont1/e/a/b/c/d2/5",
1217 "$base/unittest-cont1/e/a/b/c/d2/6"
1219 foreach ( $dirs as $dir ) {
1220 $status = $this->prepare( array( 'dir' => $dir ) );
1221 $this->assertGoodStatus( $status,
1222 "Preparing dir $dir succeeded without warnings ($backendName)." );
1225 if ( $this->backend
instanceof FSFileBackend
) {
1226 foreach ( $dirs as $dir ) {
1227 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1228 "Dir $dir exists ($backendName)." );
1232 $status = $this->backend
->clean(
1233 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1234 $this->assertGoodStatus( $status,
1235 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1237 foreach ( $dirs as $dir ) {
1238 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1239 "Dir $dir no longer exists ($backendName)." );
1243 // @TODO: testSecure
1245 public function testDoOperations() {
1246 $this->backend
= $this->singleBackend
;
1247 $this->tearDownFiles();
1248 $this->doTestDoOperations();
1249 $this->tearDownFiles();
1251 $this->backend
= $this->multiBackend
;
1252 $this->tearDownFiles();
1253 $this->doTestDoOperations();
1254 $this->tearDownFiles();
1257 private function doTestDoOperations() {
1258 $base = self
::baseStorePath();
1260 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1261 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1262 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1263 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1264 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1265 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1266 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1268 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1269 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1270 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1271 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1272 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1273 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1274 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1276 $status = $this->backend
->doOperations( array(
1277 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1278 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1279 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1280 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1281 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1282 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1283 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1284 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1285 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1286 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1287 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1288 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1289 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1290 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1291 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1292 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1293 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1295 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1297 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1299 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1301 array( 'op' => 'null' ),
1305 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1306 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1307 $this->assertEquals( 13, count( $status->success
),
1308 "Operation batch has correct success array" );
1310 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1311 "File does not exist at $fileA" );
1312 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1313 "File does not exist at $fileB" );
1314 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1315 "File does not exist at $fileD" );
1317 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1318 "File exists at $fileC" );
1319 $this->assertEquals( $fileBContents,
1320 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1321 "Correct file contents of $fileC" );
1322 $this->assertEquals( strlen( $fileBContents ),
1323 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1324 "Correct file size of $fileC" );
1325 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1326 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1327 "Correct file SHA-1 of $fileC" );
1330 public function testDoOperationsPipeline() {
1331 $this->backend
= $this->singleBackend
;
1332 $this->tearDownFiles();
1333 $this->doTestDoOperationsPipeline();
1334 $this->tearDownFiles();
1336 $this->backend
= $this->multiBackend
;
1337 $this->tearDownFiles();
1338 $this->doTestDoOperationsPipeline();
1339 $this->tearDownFiles();
1342 // concurrency orientated
1343 private function doTestDoOperationsPipeline() {
1344 $base = self
::baseStorePath();
1346 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1347 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1348 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1350 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1351 file_put_contents( $tmpNameA, $fileAContents );
1352 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1353 file_put_contents( $tmpNameB, $fileBContents );
1354 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1355 file_put_contents( $tmpNameC, $fileCContents );
1357 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1358 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1359 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1361 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1362 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1363 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1364 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1366 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1367 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1368 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1369 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1370 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1372 $status = $this->backend
->doOperations( array(
1373 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1374 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1375 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1376 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1377 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1378 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1379 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1380 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1381 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1382 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1383 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1384 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1385 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1386 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1387 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1388 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1389 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1390 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1391 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1392 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1394 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1396 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1398 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1400 array( 'op' => 'null' ),
1404 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1405 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1406 $this->assertEquals( 16, count( $status->success
),
1407 "Operation batch has correct success array" );
1409 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1410 "File does not exist at $fileA" );
1411 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1412 "File does not exist at $fileB" );
1413 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1414 "File does not exist at $fileD" );
1416 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1417 "File exists at $fileC" );
1418 $this->assertEquals( $fileBContents,
1419 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1420 "Correct file contents of $fileC" );
1421 $this->assertEquals( strlen( $fileBContents ),
1422 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1423 "Correct file size of $fileC" );
1424 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1425 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1426 "Correct file SHA-1 of $fileC" );
1429 public function testDoOperationsFailing() {
1430 $this->backend
= $this->singleBackend
;
1431 $this->tearDownFiles();
1432 $this->doTestDoOperationsFailing();
1433 $this->tearDownFiles();
1435 $this->backend
= $this->multiBackend
;
1436 $this->tearDownFiles();
1437 $this->doTestDoOperationsFailing();
1438 $this->tearDownFiles();
1441 private function doTestDoOperationsFailing() {
1442 $base = self
::baseStorePath();
1444 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1445 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1446 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1447 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1448 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1449 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1450 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1452 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1453 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1454 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1455 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1456 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1457 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1459 $status = $this->backend
->doOperations( array(
1460 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1461 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1462 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1463 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1464 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1465 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1466 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1467 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1468 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1469 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1470 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1471 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1472 array( 'op' => 'delete', 'src' => $fileD ),
1473 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1474 array( 'op' => 'null' ),
1476 ), array( 'force' => 1 ) );
1478 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1479 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1480 $this->assertEquals( 8, count( $status->success
),
1481 "Operation batch has correct success array" );
1483 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1484 "File does not exist at $fileB" );
1485 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1486 "File does not exist at $fileD" );
1488 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1489 "File does not exist at $fileA" );
1490 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1491 "File exists at $fileC" );
1492 $this->assertEquals( $fileBContents,
1493 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1494 "Correct file contents of $fileA" );
1495 $this->assertEquals( strlen( $fileBContents ),
1496 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1497 "Correct file size of $fileA" );
1498 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1499 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1500 "Correct file SHA-1 of $fileA" );
1503 public function testGetFileList() {
1504 $this->backend
= $this->singleBackend
;
1505 $this->tearDownFiles();
1506 $this->doTestGetFileList();
1507 $this->tearDownFiles();
1509 $this->backend
= $this->multiBackend
;
1510 $this->tearDownFiles();
1511 $this->doTestGetFileList();
1512 $this->tearDownFiles();
1515 private function doTestGetFileList() {
1516 $backendName = $this->backendClass();
1517 $base = self
::baseStorePath();
1519 // Should have no errors
1520 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1523 "$base/unittest-cont1/e/test1.txt",
1524 "$base/unittest-cont1/e/test2.txt",
1525 "$base/unittest-cont1/e/test3.txt",
1526 "$base/unittest-cont1/e/subdir1/test1.txt",
1527 "$base/unittest-cont1/e/subdir1/test2.txt",
1528 "$base/unittest-cont1/e/subdir2/test3.txt",
1529 "$base/unittest-cont1/e/subdir2/test4.txt",
1530 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1531 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1532 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1533 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1534 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1535 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1536 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1541 foreach ( $files as $file ) {
1542 $this->prepare( array( 'dir' => dirname( $file ) ) );
1543 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1545 $status = $this->backend
->doQuickOperations( $ops );
1546 $this->assertGoodStatus( $status,
1547 "Creation of files succeeded ($backendName)." );
1548 $this->assertEquals( true, $status->isOK(),
1549 "Creation of files succeeded with OK status ($backendName)." );
1556 "e/subdir1/test1.txt",
1557 "e/subdir1/test2.txt",
1558 "e/subdir2/test3.txt",
1559 "e/subdir2/test4.txt",
1560 "e/subdir2/subdir/test1.txt",
1561 "e/subdir2/subdir/test2.txt",
1562 "e/subdir2/subdir/test3.txt",
1563 "e/subdir2/subdir/test4.txt",
1564 "e/subdir2/subdir/test5.txt",
1565 "e/subdir2/subdir/sub/test0.txt",
1566 "e/subdir2/subdir/sub/120-px-file.txt",
1570 // Actual listing (no trailing slash)
1572 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1573 foreach ( $iter as $file ) {
1578 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1580 // Actual listing (with trailing slash)
1582 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1583 foreach ( $iter as $file ) {
1588 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1598 "sub/120-px-file.txt",
1602 // Actual listing (no trailing slash)
1604 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1605 foreach ( $iter as $file ) {
1610 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1612 // Actual listing (with trailing slash)
1614 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1615 foreach ( $iter as $file ) {
1620 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1622 // Actual listing (using iterator second time)
1624 foreach ( $iter as $file ) {
1629 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1631 // Expected listing (top files only)
1641 // Actual listing (top files only)
1643 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1644 foreach ( $iter as $file ) {
1649 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1651 foreach ( $files as $file ) { // clean up
1652 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1655 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1656 foreach ( $iter as $iter ) {} // no errors
1659 public function testGetDirectoryList() {
1660 $this->backend
= $this->singleBackend
;
1661 $this->tearDownFiles();
1662 $this->doTestGetDirectoryList();
1663 $this->tearDownFiles();
1665 $this->backend
= $this->multiBackend
;
1666 $this->tearDownFiles();
1667 $this->doTestGetDirectoryList();
1668 $this->tearDownFiles();
1671 private function doTestGetDirectoryList() {
1672 $backendName = $this->backendClass();
1674 $base = self
::baseStorePath();
1676 "$base/unittest-cont1/e/test1.txt",
1677 "$base/unittest-cont1/e/test2.txt",
1678 "$base/unittest-cont1/e/test3.txt",
1679 "$base/unittest-cont1/e/subdir1/test1.txt",
1680 "$base/unittest-cont1/e/subdir1/test2.txt",
1681 "$base/unittest-cont1/e/subdir2/test3.txt",
1682 "$base/unittest-cont1/e/subdir2/test4.txt",
1683 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1684 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1685 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1686 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1687 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1688 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1689 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1694 foreach ( $files as $file ) {
1695 $this->prepare( array( 'dir' => dirname( $file ) ) );
1696 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1698 $status = $this->backend
->doQuickOperations( $ops );
1699 $this->assertGoodStatus( $status,
1700 "Creation of files succeeded ($backendName)." );
1701 $this->assertEquals( true, $status->isOK(),
1702 "Creation of files succeeded with OK status ($backendName)." );
1704 $this->assertEquals( true,
1705 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1706 "Directory exists in ($backendName)." );
1707 $this->assertEquals( true,
1708 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1709 "Directory exists in ($backendName)." );
1710 $this->assertEquals( false,
1711 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1712 "Directory does not exists in ($backendName)." );
1720 // Actual listing (no trailing slash)
1722 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1723 foreach ( $iter as $file ) {
1728 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1739 // Actual listing (no trailing slash)
1741 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1742 foreach ( $iter as $file ) {
1747 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1749 // Actual listing (with trailing slash)
1751 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1752 foreach ( $iter as $file ) {
1757 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1765 // Actual listing (no trailing slash)
1767 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1768 foreach ( $iter as $file ) {
1773 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1775 // Actual listing (with trailing slash)
1777 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1778 foreach ( $iter as $file ) {
1783 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1785 // Actual listing (using iterator second time)
1787 foreach ( $iter as $file ) {
1792 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1794 // Expected listing (recursive)
1804 "e/subdir4/subdir/sub",
1808 // Actual listing (recursive)
1810 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1811 foreach ( $iter as $file ) {
1816 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1818 // Expected listing (recursive)
1825 // Actual listing (recursive)
1827 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
1828 foreach ( $iter as $file ) {
1833 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1835 // Actual listing (recursive, second time)
1837 foreach ( $iter as $file ) {
1842 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1844 foreach ( $files as $file ) { // clean up
1845 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1848 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1849 foreach ( $iter as $iter ) {} // no errors
1852 public function testLockCalls() {
1853 $this->backend
= $this->singleBackend
;
1854 $this->doTestLockCalls();
1857 private function doTestLockCalls() {
1858 $backendName = $this->backendClass();
1860 for ( $i=0; $i<50; $i++
) {
1866 "subdir1", // duplicate
1867 "subdir1/test1.txt",
1868 "subdir1/test2.txt",
1870 "subdir2", // duplicate
1871 "subdir2/test3.txt",
1872 "subdir2/test4.txt",
1874 "subdir2/subdir/test1.txt",
1875 "subdir2/subdir/test2.txt",
1876 "subdir2/subdir/test3.txt",
1877 "subdir2/subdir/test4.txt",
1878 "subdir2/subdir/test5.txt",
1879 "subdir2/subdir/sub",
1880 "subdir2/subdir/sub/test0.txt",
1881 "subdir2/subdir/sub/120-px-file.txt",
1884 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
1885 $this->assertEquals( array(), $status->errors
,
1886 "Locking of files succeeded ($backendName)." );
1887 $this->assertEquals( true, $status->isOK(),
1888 "Locking of files succeeded with OK status ($backendName)." );
1890 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
1891 $this->assertEquals( array(), $status->errors
,
1892 "Locking of files succeeded ($backendName)." );
1893 $this->assertEquals( true, $status->isOK(),
1894 "Locking of files succeeded with OK status ($backendName)." );
1896 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
1897 $this->assertEquals( array(), $status->errors
,
1898 "Locking of files succeeded ($backendName)." );
1899 $this->assertEquals( true, $status->isOK(),
1900 "Locking of files succeeded with OK status ($backendName)." );
1902 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
1903 $this->assertEquals( array(), $status->errors
,
1904 "Locking of files succeeded ($backendName)." );
1905 $this->assertEquals( true, $status->isOK(),
1906 "Locking of files succeeded with OK status ($backendName)." );
1910 // test helper wrapper for backend prepare() function
1911 private function prepare( array $params ) {
1912 return $this->backend
->prepare( $params );
1915 // test helper wrapper for backend prepare() function
1916 private function create( array $params ) {
1917 $params['op'] = 'create';
1918 return $this->backend
->doQuickOperations( array( $params ) );
1921 function tearDownFiles() {
1922 foreach ( $this->filesToPrune
as $file ) {
1925 $containers = array( 'unittest-cont1', 'unittest-cont2' );
1926 foreach ( $containers as $container ) {
1927 $this->deleteFiles( $container );
1929 $this->filesToPrune
= array();
1932 private function deleteFiles( $container ) {
1933 $base = self
::baseStorePath();
1934 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
1936 foreach ( $iter as $file ) {
1937 $this->backend
->quickDelete( array( 'src' => "$base/$container/$file" ) );
1940 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
1943 function assertBackendPathsConsistent( array $paths ) {
1944 if ( $this->backend
instanceof FileBackendMultiWrite
) {
1945 $status = $this->backend
->consistencyCheck( $paths );
1946 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
1950 function assertGoodStatus( $status, $msg ) {
1951 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );