7 class FileBackendTest
extends MediaWikiTestCase
{
8 private $backend, $multiBackend;
9 private $filesToPrune = array();
10 private static $backendToUse;
13 global $wgFileBackends;
15 $tmpPrefix = wfTempDir() . '/filebackend-unittest-' . time() . '-' . mt_rand();
16 if ( $this->getCliArg( 'use-filebackend=' ) ) {
17 if ( self
::$backendToUse ) {
18 $this->singleBackend
= self
::$backendToUse;
20 $name = $this->getCliArg( 'use-filebackend=' );
22 foreach ( $wgFileBackends as $conf ) {
23 if ( $conf['name'] == $name ) {
28 $useConfig['name'] = 'localtesting'; // swap name
29 $useConfig['shardViaHashLevels'] = array( // test sharding
30 'unittest-cont1' => array( 'levels' => 1, 'base' => 16, 'repeat' => 1 )
32 $class = $useConfig['class'];
33 self
::$backendToUse = new $class( $useConfig );
34 $this->singleBackend
= self
::$backendToUse;
37 $this->singleBackend
= new FSFileBackend( array(
38 'name' => 'localtesting',
39 'lockManager' => 'fsLockManager',
40 #'parallelize' => 'implicit',
41 'containerPaths' => array(
42 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
43 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
46 $this->multiBackend
= new FileBackendMultiWrite( array(
47 'name' => 'localtesting',
48 'lockManager' => 'fsLockManager',
49 'parallelize' => 'implicit',
52 'name' => 'localmutlitesting1',
53 'class' => 'FSFileBackend',
54 'lockManager' => 'nullLockManager',
55 'containerPaths' => array(
56 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
57 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
58 'isMultiMaster' => false
61 'name' => 'localmutlitesting2',
62 'class' => 'FSFileBackend',
63 'lockManager' => 'nullLockManager',
64 'containerPaths' => array(
65 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
66 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
67 'isMultiMaster' => true
71 $this->filesToPrune
= array();
74 private function baseStorePath() {
75 return 'mwstore://localtesting';
78 private function backendClass() {
79 return get_class( $this->backend
);
83 * @dataProvider provider_testIsStoragePath
85 public function testIsStoragePath( $path, $isStorePath ) {
86 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
87 "FileBackend::isStoragePath on path '$path'" );
90 function provider_testIsStoragePath() {
92 array( 'mwstore://', true ),
93 array( 'mwstore://backend', true ),
94 array( 'mwstore://backend/container', true ),
95 array( 'mwstore://backend/container/', true ),
96 array( 'mwstore://backend/container/path', true ),
97 array( 'mwstore://backend//container/', true ),
98 array( 'mwstore://backend//container//', true ),
99 array( 'mwstore://backend//container//path', true ),
100 array( 'mwstore:///', true ),
101 array( 'mwstore:/', false ),
102 array( 'mwstore:', false ),
107 * @dataProvider provider_testSplitStoragePath
109 public function testSplitStoragePath( $path, $res ) {
110 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
111 "FileBackend::splitStoragePath on path '$path'" );
114 function provider_testSplitStoragePath() {
116 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
117 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
118 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
119 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
120 array( 'mwstore://backend//container/path', array( null, null, null ) ),
121 array( 'mwstore://backend//container//path', array( null, null, null ) ),
122 array( 'mwstore://', array( null, null, null ) ),
123 array( 'mwstore://backend', array( null, null, null ) ),
124 array( 'mwstore:///', array( null, null, null ) ),
125 array( 'mwstore:/', array( null, null, null ) ),
126 array( 'mwstore:', array( null, null, null ) )
131 * @dataProvider provider_normalizeStoragePath
133 public function testNormalizeStoragePath( $path, $res ) {
134 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
135 "FileBackend::normalizeStoragePath on path '$path'" );
138 function provider_normalizeStoragePath() {
140 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
141 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
142 array( 'mwstore://backend/container/path', 'mwstore://backend/container/path' ),
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//to///obj', 'mwstore://backend/container/path/to/obj',
146 array( 'mwstore://', null ),
147 array( 'mwstore://backend', null ),
148 array( 'mwstore://backend//container/path', null ),
149 array( 'mwstore://backend//container//path', null ),
150 array( 'mwstore:///', null ),
151 array( 'mwstore:/', null ),
152 array( 'mwstore:', null ), )
157 * @dataProvider provider_testParentStoragePath
159 public function testParentStoragePath( $path, $res ) {
160 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
161 "FileBackend::parentStoragePath on path '$path'" );
164 function provider_testParentStoragePath() {
166 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
167 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
168 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
169 array( 'mwstore://backend/container', null ),
170 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
171 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
172 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
173 array( 'mwstore://backend/container/', null ),
178 * @dataProvider provider_testExtensionFromPath
180 public function testExtensionFromPath( $path, $res ) {
181 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
182 "FileBackend::extensionFromPath on path '$path'" );
185 function provider_testExtensionFromPath() {
187 array( 'mwstore://backend/container/path.txt', 'txt' ),
188 array( 'mwstore://backend/container/path.svg.png', 'png' ),
189 array( 'mwstore://backend/container/path', '' ),
190 array( 'mwstore://backend/container/path.', '' ),
195 * @dataProvider provider_testStore
197 public function testStore( $op ) {
198 $this->filesToPrune
[] = $op['src'];
200 $this->backend
= $this->singleBackend
;
201 $this->tearDownFiles();
202 $this->doTestStore( $op );
203 $this->tearDownFiles();
205 $this->backend
= $this->multiBackend
;
206 $this->tearDownFiles();
207 $this->doTestStore( $op );
208 $this->filesToPrune
[] = $op['src']; # avoid file leaking
209 $this->tearDownFiles();
212 private function doTestStore( $op ) {
213 $backendName = $this->backendClass();
215 $source = $op['src'];
217 $this->prepare( array( 'dir' => dirname( $dest ) ) );
219 file_put_contents( $source, "Unit test file" );
221 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
222 $this->backend
->store( $op );
225 $status = $this->backend
->doOperation( $op );
227 $this->assertGoodStatus( $status,
228 "Store from $source to $dest succeeded without warnings ($backendName)." );
229 $this->assertEquals( true, $status->isOK(),
230 "Store from $source to $dest succeeded ($backendName)." );
231 $this->assertEquals( array( 0 => true ), $status->success
,
232 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
233 $this->assertEquals( true, file_exists( $source ),
234 "Source file $source still exists ($backendName)." );
235 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
236 "Destination file $dest exists ($backendName)." );
238 $this->assertEquals( filesize( $source ),
239 $this->backend
->getFileSize( array( 'src' => $dest ) ),
240 "Destination file $dest has correct size ($backendName)." );
242 $props1 = FSFile
::getPropsFromPath( $source );
243 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
244 $this->assertEquals( $props1, $props2,
245 "Source and destination have the same props ($backendName)." );
247 $this->assertBackendPathsConsistent( array( $dest ) );
250 public function provider_testStore() {
253 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
254 $toPath = $this->baseStorePath() . '/unittest-cont1/e/fun/obj1.txt';
255 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
263 $op2['overwrite'] = true;
271 $op2['overwriteSame'] = true;
282 * @dataProvider provider_testCopy
284 public function testCopy( $op ) {
285 $this->backend
= $this->singleBackend
;
286 $this->tearDownFiles();
287 $this->doTestCopy( $op );
288 $this->tearDownFiles();
290 $this->backend
= $this->multiBackend
;
291 $this->tearDownFiles();
292 $this->doTestCopy( $op );
293 $this->tearDownFiles();
296 private function doTestCopy( $op ) {
297 $backendName = $this->backendClass();
299 $source = $op['src'];
301 $this->prepare( array( 'dir' => dirname( $source ) ) );
302 $this->prepare( array( 'dir' => dirname( $dest ) ) );
304 $status = $this->backend
->doOperation(
305 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
306 $this->assertGoodStatus( $status,
307 "Creation of file at $source succeeded ($backendName)." );
309 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
310 $this->backend
->copy( $op );
313 $status = $this->backend
->doOperation( $op );
315 $this->assertGoodStatus( $status,
316 "Copy from $source to $dest succeeded without warnings ($backendName)." );
317 $this->assertEquals( true, $status->isOK(),
318 "Copy from $source to $dest succeeded ($backendName)." );
319 $this->assertEquals( array( 0 => true ), $status->success
,
320 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
321 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
322 "Source file $source still exists ($backendName)." );
323 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
324 "Destination file $dest exists after copy ($backendName)." );
327 $this->backend
->getFileSize( array( 'src' => $source ) ),
328 $this->backend
->getFileSize( array( 'src' => $dest ) ),
329 "Destination file $dest has correct size ($backendName)." );
331 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
332 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
333 $this->assertEquals( $props1, $props2,
334 "Source and destination have the same props ($backendName)." );
336 $this->assertBackendPathsConsistent( array( $source, $dest ) );
339 public function provider_testCopy() {
342 $source = $this->baseStorePath() . '/unittest-cont1/e/file.txt';
343 $dest = $this->baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
345 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
353 $op2['overwrite'] = true;
361 $op2['overwriteSame'] = true;
372 * @dataProvider provider_testMove
374 public function testMove( $op ) {
375 $this->backend
= $this->singleBackend
;
376 $this->tearDownFiles();
377 $this->doTestMove( $op );
378 $this->tearDownFiles();
380 $this->backend
= $this->multiBackend
;
381 $this->tearDownFiles();
382 $this->doTestMove( $op );
383 $this->tearDownFiles();
386 private function doTestMove( $op ) {
387 $backendName = $this->backendClass();
389 $source = $op['src'];
391 $this->prepare( array( 'dir' => dirname( $source ) ) );
392 $this->prepare( array( 'dir' => dirname( $dest ) ) );
394 $status = $this->backend
->doOperation(
395 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
396 $this->assertGoodStatus( $status,
397 "Creation of file at $source succeeded ($backendName)." );
399 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
400 $this->backend
->copy( $op );
403 $status = $this->backend
->doOperation( $op );
404 $this->assertGoodStatus( $status,
405 "Move from $source to $dest succeeded without warnings ($backendName)." );
406 $this->assertEquals( true, $status->isOK(),
407 "Move from $source to $dest succeeded ($backendName)." );
408 $this->assertEquals( array( 0 => true ), $status->success
,
409 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
410 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
411 "Source file $source does not still exists ($backendName)." );
412 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
413 "Destination file $dest exists after move ($backendName)." );
415 $this->assertNotEquals(
416 $this->backend
->getFileSize( array( 'src' => $source ) ),
417 $this->backend
->getFileSize( array( 'src' => $dest ) ),
418 "Destination file $dest has correct size ($backendName)." );
420 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
421 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
422 $this->assertEquals( false, $props1['fileExists'],
423 "Source file does not exist accourding to props ($backendName)." );
424 $this->assertEquals( true, $props2['fileExists'],
425 "Destination file exists accourding to props ($backendName)." );
427 $this->assertBackendPathsConsistent( array( $source, $dest ) );
430 public function provider_testMove() {
433 $source = $this->baseStorePath() . '/unittest-cont1/e/file.txt';
434 $dest = $this->baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
436 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
444 $op2['overwrite'] = true;
452 $op2['overwriteSame'] = true;
463 * @dataProvider provider_testDelete
465 public function testDelete( $op, $withSource, $okStatus ) {
466 $this->backend
= $this->singleBackend
;
467 $this->tearDownFiles();
468 $this->doTestDelete( $op, $withSource, $okStatus );
469 $this->tearDownFiles();
471 $this->backend
= $this->multiBackend
;
472 $this->tearDownFiles();
473 $this->doTestDelete( $op, $withSource, $okStatus );
474 $this->tearDownFiles();
477 private function doTestDelete( $op, $withSource, $okStatus ) {
478 $backendName = $this->backendClass();
480 $source = $op['src'];
481 $this->prepare( array( 'dir' => dirname( $source ) ) );
484 $status = $this->backend
->doOperation(
485 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
486 $this->assertGoodStatus( $status,
487 "Creation of file at $source succeeded ($backendName)." );
490 $status = $this->backend
->doOperation( $op );
492 $this->assertGoodStatus( $status,
493 "Deletion of file at $source succeeded without warnings ($backendName)." );
494 $this->assertEquals( true, $status->isOK(),
495 "Deletion of file at $source succeeded ($backendName)." );
496 $this->assertEquals( array( 0 => true ), $status->success
,
497 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
499 $this->assertEquals( false, $status->isOK(),
500 "Deletion of file at $source failed ($backendName)." );
503 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
504 "Source file $source does not exist after move ($backendName)." );
507 $this->backend
->getFileSize( array( 'src' => $source ) ),
508 "Source file $source has correct size (false) ($backendName)." );
510 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
511 $this->assertFalse( $props1['fileExists'],
512 "Source file $source does not exist according to props ($backendName)." );
514 $this->assertBackendPathsConsistent( array( $source ) );
517 public function provider_testDelete() {
520 $source = $this->baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
522 $op = array( 'op' => 'delete', 'src' => $source );
531 false, // without source
535 $op['ignoreMissingSource'] = true;
538 false, // without source
546 * @dataProvider provider_testCreate
548 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
549 $this->backend
= $this->singleBackend
;
550 $this->tearDownFiles();
551 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
552 $this->tearDownFiles();
554 $this->backend
= $this->multiBackend
;
555 $this->tearDownFiles();
556 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
557 $this->tearDownFiles();
560 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
561 $backendName = $this->backendClass();
564 $this->prepare( array( 'dir' => dirname( $dest ) ) );
566 $oldText = 'blah...blah...waahwaah';
567 if ( $alreadyExists ) {
568 $status = $this->backend
->doOperation(
569 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
570 $this->assertGoodStatus( $status,
571 "Creation of file at $dest succeeded ($backendName)." );
574 $status = $this->backend
->doOperation( $op );
576 $this->assertGoodStatus( $status,
577 "Creation of file at $dest succeeded without warnings ($backendName)." );
578 $this->assertEquals( true, $status->isOK(),
579 "Creation of file at $dest succeeded ($backendName)." );
580 $this->assertEquals( array( 0 => true ), $status->success
,
581 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
583 $this->assertEquals( false, $status->isOK(),
584 "Creation of file at $dest failed ($backendName)." );
587 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
588 "Destination file $dest exists after creation ($backendName)." );
590 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
591 $this->assertEquals( true, $props1['fileExists'],
592 "Destination file $dest exists according to props ($backendName)." );
593 if ( $okStatus ) { // file content is what we saved
594 $this->assertEquals( $newSize, $props1['size'],
595 "Destination file $dest has expected size according to props ($backendName)." );
596 $this->assertEquals( $newSize,
597 $this->backend
->getFileSize( array( 'src' => $dest ) ),
598 "Destination file $dest has correct size ($backendName)." );
599 } else { // file content is some other previous text
600 $this->assertEquals( strlen( $oldText ), $props1['size'],
601 "Destination file $dest has original size according to props ($backendName)." );
602 $this->assertEquals( strlen( $oldText ),
603 $this->backend
->getFileSize( array( 'src' => $dest ) ),
604 "Destination file $dest has original size according to props ($backendName)." );
607 $this->assertBackendPathsConsistent( array( $dest ) );
611 * @dataProvider provider_testCreate
613 public function provider_testCreate() {
616 $dest = $this->baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
618 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
621 false, // no dest already exists
623 strlen( $op['content'] )
627 $op2['content'] = "\n";
630 false, // no dest already exists
632 strlen( $op2['content'] )
636 $op2['content'] = "fsf\n waf 3kt";
639 true, // dest already exists
641 strlen( $op2['content'] )
645 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
646 $op2['overwrite'] = true;
649 true, // dest already exists
651 strlen( $op2['content'] )
655 $op2['content'] = "39qjmg3-qg";
656 $op2['overwriteSame'] = true;
659 true, // dest already exists
661 strlen( $op2['content'] )
667 public function testDoQuickOperations() {
668 $this->backend
= $this->singleBackend
;
669 $this->doTestDoQuickOperations();
670 $this->tearDownFiles();
672 $this->backend
= $this->multiBackend
;
673 $this->doTestDoQuickOperations();
674 $this->tearDownFiles();
677 private function doTestDoQuickOperations() {
678 $backendName = $this->backendClass();
680 $base = $this->baseStorePath();
682 "$base/unittest-cont1/e/fileA.a",
683 "$base/unittest-cont1/e/fileB.a",
684 "$base/unittest-cont1/e/fileC.a"
688 foreach ( $files as $path ) {
689 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
690 $this->assertGoodStatus( $status,
691 "Preparing $path succeeded without warnings ($backendName)." );
692 $ops[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand(0,50000) );
693 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
695 $purgeOps[] = array( 'op' => 'null' );
696 $status = $this->backend
->doQuickOperations( $ops );
697 $this->assertGoodStatus( $status,
698 "Creation of source files succeeded ($backendName)." );
700 foreach ( $files as $file ) {
701 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $file ) ),
702 "File $file exists." );
705 $status = $this->backend
->doQuickOperations( $purgeOps );
706 $this->assertGoodStatus( $status,
707 "Quick deletion of source files succeeded ($backendName)." );
709 foreach ( $files as $file ) {
710 $this->assertFalse( $this->backend
->fileExists( array( 'src' => $file ) ),
711 "File $file purged." );
716 * @dataProvider provider_testConcatenate
718 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
719 $this->filesToPrune
[] = $op['dst'];
721 $this->backend
= $this->singleBackend
;
722 $this->tearDownFiles();
723 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
724 $this->tearDownFiles();
726 $this->backend
= $this->multiBackend
;
727 $this->tearDownFiles();
728 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
729 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
730 $this->tearDownFiles();
733 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
734 $backendName = $this->backendClass();
739 foreach ( $srcs as $i => $source ) {
740 $this->prepare( array( 'dir' => dirname( $source ) ) );
742 'op' => 'create', // operation
743 'dst' => $source, // source
744 'content' => $srcsContent[$i]
746 $expContent .= $srcsContent[$i];
748 $status = $this->backend
->doOperations( $ops );
750 $this->assertGoodStatus( $status,
751 "Creation of source files succeeded ($backendName)." );
753 $dest = $params['dst'];
754 if ( $alreadyExists ) {
755 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
756 $this->assertEquals( true, $ok,
757 "Creation of file at $dest succeeded ($backendName)." );
759 $ok = file_put_contents( $dest, '' ) !== false;
760 $this->assertEquals( true, $ok,
761 "Creation of 0-byte file at $dest succeeded ($backendName)." );
764 // Combine the files into one
765 $status = $this->backend
->concatenate( $params );
767 $this->assertGoodStatus( $status,
768 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
769 $this->assertEquals( true, $status->isOK(),
770 "Creation of concat file at $dest succeeded ($backendName)." );
772 $this->assertEquals( false, $status->isOK(),
773 "Creation of concat file at $dest failed ($backendName)." );
777 $this->assertEquals( true, is_file( $dest ),
778 "Dest concat file $dest exists after creation ($backendName)." );
780 $this->assertEquals( true, is_file( $dest ),
781 "Dest concat file $dest exists after failed creation ($backendName)." );
784 $contents = file_get_contents( $dest );
785 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
788 $this->assertEquals( $expContent, $contents,
789 "Concat file at $dest has correct contents ($backendName)." );
791 $this->assertNotEquals( $expContent, $contents,
792 "Concat file at $dest has correct contents ($backendName)." );
796 function provider_testConcatenate() {
799 $rand = mt_rand( 0, 2000000000 ) . time();
800 $dest = wfTempDir() . "/randomfile!$rand.txt";
802 $this->baseStorePath() . '/unittest-cont1/e/file1.txt',
803 $this->baseStorePath() . '/unittest-cont1/e/file2.txt',
804 $this->baseStorePath() . '/unittest-cont1/e/file3.txt',
805 $this->baseStorePath() . '/unittest-cont1/e/file4.txt',
806 $this->baseStorePath() . '/unittest-cont1/e/file5.txt',
807 $this->baseStorePath() . '/unittest-cont1/e/file6.txt',
808 $this->baseStorePath() . '/unittest-cont1/e/file7.txt',
809 $this->baseStorePath() . '/unittest-cont1/e/file8.txt',
810 $this->baseStorePath() . '/unittest-cont1/e/file9.txt',
811 $this->baseStorePath() . '/unittest-cont1/e/file10.txt'
825 $params = array( 'srcs' => $srcs, 'dst' => $dest );
828 $params, // operation
830 $content, // content for each source
831 false, // no dest already exists
836 $params, // operation
838 $content, // content for each source
839 true, // dest already exists
847 * @dataProvider provider_testGetFileStat
849 public function testGetFileStat( $path, $content, $alreadyExists ) {
850 $this->backend
= $this->singleBackend
;
851 $this->tearDownFiles();
852 $this->doTestGetFileStat( $path, $content, $alreadyExists );
853 $this->tearDownFiles();
855 $this->backend
= $this->multiBackend
;
856 $this->tearDownFiles();
857 $this->doTestGetFileStat( $path, $content, $alreadyExists );
858 $this->tearDownFiles();
861 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
862 $backendName = $this->backendClass();
864 if ( $alreadyExists ) {
865 $this->prepare( array( 'dir' => dirname( $path ) ) );
866 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
867 $this->assertGoodStatus( $status,
868 "Creation of file at $path succeeded ($backendName)." );
870 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
871 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
872 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
874 $this->assertEquals( strlen( $content ), $size,
875 "Correct file size of '$path'" );
876 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
877 "Correct file timestamp of '$path'" );
879 $size = $stat['size'];
880 $time = $stat['mtime'];
881 $this->assertEquals( strlen( $content ), $size,
882 "Correct file size of '$path'" );
883 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
884 "Correct file timestamp of '$path'" );
886 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
887 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
888 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
890 $this->assertFalse( $size, "Correct file size of '$path'" );
891 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
892 $this->assertFalse( $stat, "Correct file stat of '$path'" );
896 function provider_testGetFileStat() {
899 $base = $this->baseStorePath();
900 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
901 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
902 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
908 * @dataProvider provider_testGetFileContents
910 public function testGetFileContents( $source, $content ) {
911 $this->backend
= $this->singleBackend
;
912 $this->tearDownFiles();
913 $this->doTestGetFileContents( $source, $content );
914 $this->tearDownFiles();
916 $this->backend
= $this->multiBackend
;
917 $this->tearDownFiles();
918 $this->doTestGetFileContents( $source, $content );
919 $this->tearDownFiles();
922 private function doTestGetFileContents( $source, $content ) {
923 $backendName = $this->backendClass();
925 $this->prepare( array( 'dir' => dirname( $source ) ) );
927 $status = $this->backend
->doOperation(
928 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
929 $this->assertGoodStatus( $status,
930 "Creation of file at $source succeeded ($backendName)." );
931 $this->assertEquals( true, $status->isOK(),
932 "Creation of file at $source succeeded with OK status ($backendName)." );
934 $newContents = $this->backend
->getFileContents( array( 'src' => $source, 'latest' => 1 ) );
935 $this->assertNotEquals( false, $newContents,
936 "Read of file at $source succeeded ($backendName)." );
938 $this->assertEquals( $content, $newContents,
939 "Contents read match data at $source ($backendName)." );
942 function provider_testGetFileContents() {
945 $base = $this->baseStorePath();
946 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
947 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
953 * @dataProvider provider_testGetLocalCopy
955 public function testGetLocalCopy( $source, $content ) {
956 $this->backend
= $this->singleBackend
;
957 $this->tearDownFiles();
958 $this->doTestGetLocalCopy( $source, $content );
959 $this->tearDownFiles();
961 $this->backend
= $this->multiBackend
;
962 $this->tearDownFiles();
963 $this->doTestGetLocalCopy( $source, $content );
964 $this->tearDownFiles();
967 private function doTestGetLocalCopy( $source, $content ) {
968 $backendName = $this->backendClass();
970 $this->prepare( array( 'dir' => dirname( $source ) ) );
972 $status = $this->backend
->doOperation(
973 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
974 $this->assertGoodStatus( $status,
975 "Creation of file at $source succeeded ($backendName)." );
977 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
978 $this->assertNotNull( $tmpFile,
979 "Creation of local copy of $source succeeded ($backendName)." );
981 $contents = file_get_contents( $tmpFile->getPath() );
982 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
985 function provider_testGetLocalCopy() {
988 $base = $this->baseStorePath();
989 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
990 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
996 * @dataProvider provider_testGetLocalReference
998 public function testGetLocalReference( $source, $content ) {
999 $this->backend
= $this->singleBackend
;
1000 $this->tearDownFiles();
1001 $this->doTestGetLocalReference( $source, $content );
1002 $this->tearDownFiles();
1004 $this->backend
= $this->multiBackend
;
1005 $this->tearDownFiles();
1006 $this->doTestGetLocalReference( $source, $content );
1007 $this->tearDownFiles();
1010 private function doTestGetLocalReference( $source, $content ) {
1011 $backendName = $this->backendClass();
1013 $this->prepare( array( 'dir' => dirname( $source ) ) );
1015 $status = $this->create( array( 'content' => $content, 'dst' => $source ) );
1016 $this->assertGoodStatus( $status,
1017 "Creation of file at $source succeeded ($backendName)." );
1019 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
1020 $this->assertNotNull( $tmpFile,
1021 "Creation of local copy of $source succeeded ($backendName)." );
1023 $contents = file_get_contents( $tmpFile->getPath() );
1024 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1027 function provider_testGetLocalReference() {
1030 $base = $this->baseStorePath();
1031 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1032 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1038 * @dataProvider provider_testPrepareAndClean
1040 public function testPrepareAndClean( $path, $isOK ) {
1041 $this->backend
= $this->singleBackend
;
1042 $this->doTestPrepareAndClean( $path, $isOK );
1043 $this->tearDownFiles();
1045 $this->backend
= $this->multiBackend
;
1046 $this->doTestPrepareAndClean( $path, $isOK );
1047 $this->tearDownFiles();
1050 function provider_testPrepareAndClean() {
1051 $base = $this->baseStorePath();
1053 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1054 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1055 # Specific to FS backend with no basePath field set
1056 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1060 private function doTestPrepareAndClean( $path, $isOK ) {
1061 $backendName = $this->backendClass();
1063 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1065 $this->assertGoodStatus( $status,
1066 "Preparing dir $path succeeded without warnings ($backendName)." );
1067 $this->assertEquals( true, $status->isOK(),
1068 "Preparing dir $path succeeded ($backendName)." );
1070 $this->assertEquals( false, $status->isOK(),
1071 "Preparing dir $path failed ($backendName)." );
1074 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1076 $this->assertGoodStatus( $status,
1077 "Cleaning dir $path succeeded without warnings ($backendName)." );
1078 $this->assertEquals( true, $status->isOK(),
1079 "Cleaning dir $path succeeded ($backendName)." );
1081 $this->assertEquals( false, $status->isOK(),
1082 "Cleaning dir $path failed ($backendName)." );
1086 public function testRecursiveClean() {
1087 $this->backend
= $this->singleBackend
;
1088 $this->doTestRecursiveClean();
1089 $this->tearDownFiles();
1091 $this->backend
= $this->multiBackend
;
1092 $this->doTestRecursiveClean();
1093 $this->tearDownFiles();
1096 private function doTestRecursiveClean() {
1097 $backendName = $this->backendClass();
1099 $base = $this->baseStorePath();
1101 "$base/unittest-cont1/e/a",
1102 "$base/unittest-cont1/e/a/b",
1103 "$base/unittest-cont1/e/a/b/c",
1104 "$base/unittest-cont1/e/a/b/c/d0",
1105 "$base/unittest-cont1/e/a/b/c/d1",
1106 "$base/unittest-cont1/e/a/b/c/d2",
1107 "$base/unittest-cont1/e/a/b/c/d0/1",
1108 "$base/unittest-cont1/e/a/b/c/d0/2",
1109 "$base/unittest-cont1/e/a/b/c/d1/3",
1110 "$base/unittest-cont1/e/a/b/c/d1/4",
1111 "$base/unittest-cont1/e/a/b/c/d2/5",
1112 "$base/unittest-cont1/e/a/b/c/d2/6"
1114 foreach ( $dirs as $dir ) {
1115 $status = $this->prepare( array( 'dir' => $dir ) );
1116 $this->assertGoodStatus( $status,
1117 "Preparing dir $dir succeeded without warnings ($backendName)." );
1120 if ( $this->backend
instanceof FSFileBackend
) {
1121 foreach ( $dirs as $dir ) {
1122 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1123 "Dir $dir exists ($backendName)." );
1127 $status = $this->backend
->clean(
1128 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1129 $this->assertGoodStatus( $status,
1130 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1132 foreach ( $dirs as $dir ) {
1133 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1134 "Dir $dir no longer exists ($backendName)." );
1138 // @TODO: testSecure
1140 public function testDoOperations() {
1141 $this->backend
= $this->singleBackend
;
1142 $this->tearDownFiles();
1143 $this->doTestDoOperations();
1144 $this->tearDownFiles();
1146 $this->backend
= $this->multiBackend
;
1147 $this->tearDownFiles();
1148 $this->doTestDoOperations();
1149 $this->tearDownFiles();
1151 $this->backend
= $this->singleBackend
;
1152 $this->tearDownFiles();
1153 $this->doTestDoOperations2();
1154 $this->tearDownFiles();
1156 $this->backend
= $this->multiBackend
;
1157 $this->tearDownFiles();
1158 $this->doTestDoOperations2();
1159 $this->tearDownFiles();
1161 $this->backend
= $this->singleBackend
;
1162 $this->tearDownFiles();
1163 $this->doTestDoOperationsFailing();
1164 $this->tearDownFiles();
1166 $this->backend
= $this->multiBackend
;
1167 $this->tearDownFiles();
1168 $this->doTestDoOperationsFailing();
1169 $this->tearDownFiles();
1172 private function doTestDoOperations() {
1173 $base = $this->baseStorePath();
1175 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1176 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1177 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1178 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1179 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1180 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1181 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1183 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1184 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1185 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1186 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1187 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1188 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1189 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1191 $status = $this->backend
->doOperations( array(
1192 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1193 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1194 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1195 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1196 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1197 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1198 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1199 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1200 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1201 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1202 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1203 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1204 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1205 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1206 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1207 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1208 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1210 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1212 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1214 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1216 array( 'op' => 'null' ),
1220 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1221 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1222 $this->assertEquals( 13, count( $status->success
),
1223 "Operation batch has correct success array" );
1225 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1226 "File does not exist at $fileA" );
1227 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1228 "File does not exist at $fileB" );
1229 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1230 "File does not exist at $fileD" );
1232 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1233 "File exists at $fileC" );
1234 $this->assertEquals( $fileBContents,
1235 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1236 "Correct file contents of $fileC" );
1237 $this->assertEquals( strlen( $fileBContents ),
1238 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1239 "Correct file size of $fileC" );
1240 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1241 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1242 "Correct file SHA-1 of $fileC" );
1245 // concurrency orientated
1246 private function doTestDoOperations2() {
1247 $base = $this->baseStorePath();
1249 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1250 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1251 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1253 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1254 file_put_contents( $tmpNameA, $fileAContents );
1255 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1256 file_put_contents( $tmpNameB, $fileBContents );
1257 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1258 file_put_contents( $tmpNameC, $fileCContents );
1260 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1261 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1262 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1264 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1265 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1266 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1267 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1269 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1270 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1271 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1272 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1273 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1275 $status = $this->backend
->doOperations( array(
1276 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1277 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1278 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1279 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1280 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1281 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1282 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1283 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1284 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1285 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1286 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1287 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1288 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1289 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1290 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1291 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1292 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1293 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1294 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1295 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1297 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1299 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1301 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1303 array( 'op' => 'null' ),
1307 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1308 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1309 $this->assertEquals( 16, count( $status->success
),
1310 "Operation batch has correct success array" );
1312 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1313 "File does not exist at $fileA" );
1314 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1315 "File does not exist at $fileB" );
1316 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1317 "File does not exist at $fileD" );
1319 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1320 "File exists at $fileC" );
1321 $this->assertEquals( $fileBContents,
1322 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1323 "Correct file contents of $fileC" );
1324 $this->assertEquals( strlen( $fileBContents ),
1325 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1326 "Correct file size of $fileC" );
1327 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1328 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1329 "Correct file SHA-1 of $fileC" );
1332 private function doTestDoOperationsFailing() {
1333 $base = $this->baseStorePath();
1335 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1336 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1337 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1338 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1339 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1340 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1341 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1343 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1344 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1345 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1346 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1347 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1348 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1350 $status = $this->backend
->doOperations( array(
1351 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1352 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1353 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1354 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1355 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1356 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1357 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1358 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1359 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1360 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1361 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1362 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1363 array( 'op' => 'delete', 'src' => $fileD ),
1364 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1365 array( 'op' => 'null' ),
1367 ), array( 'force' => 1 ) );
1369 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1370 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1371 $this->assertEquals( 8, count( $status->success
),
1372 "Operation batch has correct success array" );
1374 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1375 "File does not exist at $fileB" );
1376 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1377 "File does not exist at $fileD" );
1379 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1380 "File does not exist at $fileA" );
1381 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1382 "File exists at $fileC" );
1383 $this->assertEquals( $fileBContents,
1384 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1385 "Correct file contents of $fileA" );
1386 $this->assertEquals( strlen( $fileBContents ),
1387 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1388 "Correct file size of $fileA" );
1389 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1390 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1391 "Correct file SHA-1 of $fileA" );
1394 public function testGetFileList() {
1395 $this->backend
= $this->singleBackend
;
1396 $this->tearDownFiles();
1397 $this->doTestGetFileList();
1398 $this->tearDownFiles();
1400 $this->backend
= $this->multiBackend
;
1401 $this->tearDownFiles();
1402 $this->doTestGetFileList();
1403 $this->tearDownFiles();
1406 private function doTestGetFileList() {
1407 $backendName = $this->backendClass();
1408 $base = $this->baseStorePath();
1410 // Should have no errors
1411 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1414 "$base/unittest-cont1/e/test1.txt",
1415 "$base/unittest-cont1/e/test2.txt",
1416 "$base/unittest-cont1/e/test3.txt",
1417 "$base/unittest-cont1/e/subdir1/test1.txt",
1418 "$base/unittest-cont1/e/subdir1/test2.txt",
1419 "$base/unittest-cont1/e/subdir2/test3.txt",
1420 "$base/unittest-cont1/e/subdir2/test4.txt",
1421 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1422 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1423 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1424 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1425 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1426 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1427 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1432 foreach ( $files as $file ) {
1433 $this->prepare( array( 'dir' => dirname( $file ) ) );
1434 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1436 $status = $this->backend
->doQuickOperations( $ops );
1437 $this->assertGoodStatus( $status,
1438 "Creation of files succeeded ($backendName)." );
1439 $this->assertEquals( true, $status->isOK(),
1440 "Creation of files succeeded with OK status ($backendName)." );
1447 "e/subdir1/test1.txt",
1448 "e/subdir1/test2.txt",
1449 "e/subdir2/test3.txt",
1450 "e/subdir2/test4.txt",
1451 "e/subdir2/subdir/test1.txt",
1452 "e/subdir2/subdir/test2.txt",
1453 "e/subdir2/subdir/test3.txt",
1454 "e/subdir2/subdir/test4.txt",
1455 "e/subdir2/subdir/test5.txt",
1456 "e/subdir2/subdir/sub/test0.txt",
1457 "e/subdir2/subdir/sub/120-px-file.txt",
1461 // Actual listing (no trailing slash)
1463 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1464 foreach ( $iter as $file ) {
1469 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1471 // Actual listing (with trailing slash)
1473 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1474 foreach ( $iter as $file ) {
1479 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1489 "sub/120-px-file.txt",
1493 // Actual listing (no trailing slash)
1495 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1496 foreach ( $iter as $file ) {
1501 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1503 // Actual listing (with trailing slash)
1505 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1506 foreach ( $iter as $file ) {
1511 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1513 // Actual listing (using iterator second time)
1515 foreach ( $iter as $file ) {
1520 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1522 // Expected listing (top files only)
1532 // Actual listing (top files only)
1534 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1535 foreach ( $iter as $file ) {
1540 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1542 foreach ( $files as $file ) { // clean up
1543 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1546 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1547 foreach ( $iter as $iter ) {} // no errors
1550 public function testGetDirectoryList() {
1551 $this->backend
= $this->singleBackend
;
1552 $this->tearDownFiles();
1553 $this->doTestGetDirectoryList();
1554 $this->tearDownFiles();
1556 $this->backend
= $this->multiBackend
;
1557 $this->tearDownFiles();
1558 $this->doTestGetDirectoryList();
1559 $this->tearDownFiles();
1562 private function doTestGetDirectoryList() {
1563 $backendName = $this->backendClass();
1565 $base = $this->baseStorePath();
1567 "$base/unittest-cont1/e/test1.txt",
1568 "$base/unittest-cont1/e/test2.txt",
1569 "$base/unittest-cont1/e/test3.txt",
1570 "$base/unittest-cont1/e/subdir1/test1.txt",
1571 "$base/unittest-cont1/e/subdir1/test2.txt",
1572 "$base/unittest-cont1/e/subdir2/test3.txt",
1573 "$base/unittest-cont1/e/subdir2/test4.txt",
1574 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1575 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1576 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1577 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1578 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1579 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1580 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1585 foreach ( $files as $file ) {
1586 $this->prepare( array( 'dir' => dirname( $file ) ) );
1587 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1589 $status = $this->backend
->doQuickOperations( $ops );
1590 $this->assertGoodStatus( $status,
1591 "Creation of files succeeded ($backendName)." );
1592 $this->assertEquals( true, $status->isOK(),
1593 "Creation of files succeeded with OK status ($backendName)." );
1595 $this->assertEquals( true,
1596 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1597 "Directory exists in ($backendName)." );
1598 $this->assertEquals( true,
1599 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1600 "Directory exists in ($backendName)." );
1601 $this->assertEquals( false,
1602 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1603 "Directory does not exists in ($backendName)." );
1611 // Actual listing (no trailing slash)
1613 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1614 foreach ( $iter as $file ) {
1619 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1630 // Actual listing (no trailing slash)
1632 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1633 foreach ( $iter as $file ) {
1638 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1640 // Actual listing (with trailing slash)
1642 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1643 foreach ( $iter as $file ) {
1648 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1656 // Actual listing (no trailing slash)
1658 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1659 foreach ( $iter as $file ) {
1664 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1666 // Actual listing (with trailing slash)
1668 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1669 foreach ( $iter as $file ) {
1674 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1676 // Actual listing (using iterator second time)
1678 foreach ( $iter as $file ) {
1683 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1685 // Expected listing (recursive)
1695 "e/subdir4/subdir/sub",
1699 // Actual listing (recursive)
1701 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1702 foreach ( $iter as $file ) {
1707 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1709 // Expected listing (recursive)
1716 // Actual listing (recursive)
1718 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
1719 foreach ( $iter as $file ) {
1724 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1726 // Actual listing (recursive, second time)
1728 foreach ( $iter as $file ) {
1733 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1735 foreach ( $files as $file ) { // clean up
1736 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1739 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1740 foreach ( $iter as $iter ) {} // no errors
1743 public function testLockCalls() {
1744 $this->backend
= $this->singleBackend
;
1745 $this->doTestLockCalls();
1748 private function doTestLockCalls() {
1749 $backendName = $this->backendClass();
1751 for ( $i=0; $i<50; $i++
) {
1757 "subdir1", // duplicate
1758 "subdir1/test1.txt",
1759 "subdir1/test2.txt",
1761 "subdir2", // duplicate
1762 "subdir2/test3.txt",
1763 "subdir2/test4.txt",
1765 "subdir2/subdir/test1.txt",
1766 "subdir2/subdir/test2.txt",
1767 "subdir2/subdir/test3.txt",
1768 "subdir2/subdir/test4.txt",
1769 "subdir2/subdir/test5.txt",
1770 "subdir2/subdir/sub",
1771 "subdir2/subdir/sub/test0.txt",
1772 "subdir2/subdir/sub/120-px-file.txt",
1775 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
1776 $this->assertEquals( array(), $status->errors
,
1777 "Locking of files succeeded ($backendName)." );
1778 $this->assertEquals( true, $status->isOK(),
1779 "Locking of files succeeded with OK status ($backendName)." );
1781 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
1782 $this->assertEquals( array(), $status->errors
,
1783 "Locking of files succeeded ($backendName)." );
1784 $this->assertEquals( true, $status->isOK(),
1785 "Locking of files succeeded with OK status ($backendName)." );
1787 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
1788 $this->assertEquals( array(), $status->errors
,
1789 "Locking of files succeeded ($backendName)." );
1790 $this->assertEquals( true, $status->isOK(),
1791 "Locking of files succeeded with OK status ($backendName)." );
1793 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
1794 $this->assertEquals( array(), $status->errors
,
1795 "Locking of files succeeded ($backendName)." );
1796 $this->assertEquals( true, $status->isOK(),
1797 "Locking of files succeeded with OK status ($backendName)." );
1801 // test helper wrapper for backend prepare() function
1802 private function prepare( array $params ) {
1803 return $this->backend
->prepare( $params );
1806 // test helper wrapper for backend prepare() function
1807 private function create( array $params ) {
1808 $params['op'] = 'create';
1809 return $this->backend
->doQuickOperations( array( $params ) );
1812 function tearDownFiles() {
1813 foreach ( $this->filesToPrune
as $file ) {
1816 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1817 foreach ( $containers as $container ) {
1818 $this->deleteFiles( $container );
1820 $this->filesToPrune
= array();
1823 private function deleteFiles( $container ) {
1824 $base = $this->baseStorePath();
1825 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
1827 foreach ( $iter as $file ) {
1828 $this->backend
->delete( array( 'src' => "$base/$container/$file" ),
1829 array( 'force' => 1, 'nonLocking' => 1 ) );
1832 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
1835 function assertBackendPathsConsistent( array $paths ) {
1836 if ( $this->backend
instanceof FileBackendMultiWrite
) {
1837 $status = $this->backend
->consistencyCheck( $paths );
1838 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
1842 function assertGoodStatus( $status, $msg ) {
1843 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );
1846 function tearDown() {