f9d78e5f4aa1350f66e4d5e7092ed086abf93854
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 $this->backend
->clearCache( array( $path ) );
888 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
890 $this->assertEquals( strlen( $content ), $size,
891 "Correct file size of '$path'" );
893 $this->backend
->preloadCache( array( $path ) );
895 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
897 $this->assertEquals( strlen( $content ), $size,
898 "Correct file size of '$path'" );
900 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
901 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
902 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
904 $this->assertFalse( $size, "Correct file size of '$path'" );
905 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
906 $this->assertFalse( $stat, "Correct file stat of '$path'" );
910 function provider_testGetFileStat() {
913 $base = $this->baseStorePath();
914 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
915 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
916 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
922 * @dataProvider provider_testGetFileContents
924 public function testGetFileContents( $source, $content ) {
925 $this->backend
= $this->singleBackend
;
926 $this->tearDownFiles();
927 $this->doTestGetFileContents( $source, $content );
928 $this->tearDownFiles();
930 $this->backend
= $this->multiBackend
;
931 $this->tearDownFiles();
932 $this->doTestGetFileContents( $source, $content );
933 $this->tearDownFiles();
936 private function doTestGetFileContents( $source, $content ) {
937 $backendName = $this->backendClass();
939 $this->prepare( array( 'dir' => dirname( $source ) ) );
941 $status = $this->backend
->doOperation(
942 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
943 $this->assertGoodStatus( $status,
944 "Creation of file at $source succeeded ($backendName)." );
945 $this->assertEquals( true, $status->isOK(),
946 "Creation of file at $source succeeded with OK status ($backendName)." );
948 $newContents = $this->backend
->getFileContents( array( 'src' => $source, 'latest' => 1 ) );
949 $this->assertNotEquals( false, $newContents,
950 "Read of file at $source succeeded ($backendName)." );
952 $this->assertEquals( $content, $newContents,
953 "Contents read match data at $source ($backendName)." );
956 function provider_testGetFileContents() {
959 $base = $this->baseStorePath();
960 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
961 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
967 * @dataProvider provider_testGetLocalCopy
969 public function testGetLocalCopy( $source, $content ) {
970 $this->backend
= $this->singleBackend
;
971 $this->tearDownFiles();
972 $this->doTestGetLocalCopy( $source, $content );
973 $this->tearDownFiles();
975 $this->backend
= $this->multiBackend
;
976 $this->tearDownFiles();
977 $this->doTestGetLocalCopy( $source, $content );
978 $this->tearDownFiles();
981 private function doTestGetLocalCopy( $source, $content ) {
982 $backendName = $this->backendClass();
984 $this->prepare( array( 'dir' => dirname( $source ) ) );
986 $status = $this->backend
->doOperation(
987 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
988 $this->assertGoodStatus( $status,
989 "Creation of file at $source succeeded ($backendName)." );
991 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
992 $this->assertNotNull( $tmpFile,
993 "Creation of local copy of $source succeeded ($backendName)." );
995 $contents = file_get_contents( $tmpFile->getPath() );
996 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
999 function provider_testGetLocalCopy() {
1002 $base = $this->baseStorePath();
1003 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1004 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1010 * @dataProvider provider_testGetLocalReference
1012 public function testGetLocalReference( $source, $content ) {
1013 $this->backend
= $this->singleBackend
;
1014 $this->tearDownFiles();
1015 $this->doTestGetLocalReference( $source, $content );
1016 $this->tearDownFiles();
1018 $this->backend
= $this->multiBackend
;
1019 $this->tearDownFiles();
1020 $this->doTestGetLocalReference( $source, $content );
1021 $this->tearDownFiles();
1024 private function doTestGetLocalReference( $source, $content ) {
1025 $backendName = $this->backendClass();
1027 $this->prepare( array( 'dir' => dirname( $source ) ) );
1029 $status = $this->create( array( 'content' => $content, 'dst' => $source ) );
1030 $this->assertGoodStatus( $status,
1031 "Creation of file at $source succeeded ($backendName)." );
1033 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
1034 $this->assertNotNull( $tmpFile,
1035 "Creation of local copy of $source succeeded ($backendName)." );
1037 $contents = file_get_contents( $tmpFile->getPath() );
1038 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1041 function provider_testGetLocalReference() {
1044 $base = $this->baseStorePath();
1045 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1046 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1052 * @dataProvider provider_testPrepareAndClean
1054 public function testPrepareAndClean( $path, $isOK ) {
1055 $this->backend
= $this->singleBackend
;
1056 $this->doTestPrepareAndClean( $path, $isOK );
1057 $this->tearDownFiles();
1059 $this->backend
= $this->multiBackend
;
1060 $this->doTestPrepareAndClean( $path, $isOK );
1061 $this->tearDownFiles();
1064 function provider_testPrepareAndClean() {
1065 $base = $this->baseStorePath();
1067 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1068 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1069 # Specific to FS backend with no basePath field set
1070 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1074 private function doTestPrepareAndClean( $path, $isOK ) {
1075 $backendName = $this->backendClass();
1077 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1079 $this->assertGoodStatus( $status,
1080 "Preparing dir $path succeeded without warnings ($backendName)." );
1081 $this->assertEquals( true, $status->isOK(),
1082 "Preparing dir $path succeeded ($backendName)." );
1084 $this->assertEquals( false, $status->isOK(),
1085 "Preparing dir $path failed ($backendName)." );
1088 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1090 $this->assertGoodStatus( $status,
1091 "Cleaning dir $path succeeded without warnings ($backendName)." );
1092 $this->assertEquals( true, $status->isOK(),
1093 "Cleaning dir $path succeeded ($backendName)." );
1095 $this->assertEquals( false, $status->isOK(),
1096 "Cleaning dir $path failed ($backendName)." );
1100 public function testRecursiveClean() {
1101 $this->backend
= $this->singleBackend
;
1102 $this->doTestRecursiveClean();
1103 $this->tearDownFiles();
1105 $this->backend
= $this->multiBackend
;
1106 $this->doTestRecursiveClean();
1107 $this->tearDownFiles();
1110 private function doTestRecursiveClean() {
1111 $backendName = $this->backendClass();
1113 $base = $this->baseStorePath();
1115 "$base/unittest-cont1/e/a",
1116 "$base/unittest-cont1/e/a/b",
1117 "$base/unittest-cont1/e/a/b/c",
1118 "$base/unittest-cont1/e/a/b/c/d0",
1119 "$base/unittest-cont1/e/a/b/c/d1",
1120 "$base/unittest-cont1/e/a/b/c/d2",
1121 "$base/unittest-cont1/e/a/b/c/d0/1",
1122 "$base/unittest-cont1/e/a/b/c/d0/2",
1123 "$base/unittest-cont1/e/a/b/c/d1/3",
1124 "$base/unittest-cont1/e/a/b/c/d1/4",
1125 "$base/unittest-cont1/e/a/b/c/d2/5",
1126 "$base/unittest-cont1/e/a/b/c/d2/6"
1128 foreach ( $dirs as $dir ) {
1129 $status = $this->prepare( array( 'dir' => $dir ) );
1130 $this->assertGoodStatus( $status,
1131 "Preparing dir $dir succeeded without warnings ($backendName)." );
1134 if ( $this->backend
instanceof FSFileBackend
) {
1135 foreach ( $dirs as $dir ) {
1136 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1137 "Dir $dir exists ($backendName)." );
1141 $status = $this->backend
->clean(
1142 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1143 $this->assertGoodStatus( $status,
1144 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1146 foreach ( $dirs as $dir ) {
1147 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1148 "Dir $dir no longer exists ($backendName)." );
1152 // @TODO: testSecure
1154 public function testDoOperations() {
1155 $this->backend
= $this->singleBackend
;
1156 $this->tearDownFiles();
1157 $this->doTestDoOperations();
1158 $this->tearDownFiles();
1160 $this->backend
= $this->multiBackend
;
1161 $this->tearDownFiles();
1162 $this->doTestDoOperations();
1163 $this->tearDownFiles();
1166 private function doTestDoOperations() {
1167 $base = $this->baseStorePath();
1169 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1170 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1171 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1172 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1173 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1174 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1175 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1177 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1178 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1179 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1180 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1181 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1182 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1183 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1185 $status = $this->backend
->doOperations( array(
1186 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1187 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1188 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1189 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1190 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1191 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1192 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1193 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1194 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1195 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1196 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1197 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1198 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1199 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1200 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1201 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1202 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1204 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1206 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1208 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1210 array( 'op' => 'null' ),
1214 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1215 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1216 $this->assertEquals( 13, count( $status->success
),
1217 "Operation batch has correct success array" );
1219 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1220 "File does not exist at $fileA" );
1221 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1222 "File does not exist at $fileB" );
1223 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1224 "File does not exist at $fileD" );
1226 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1227 "File exists at $fileC" );
1228 $this->assertEquals( $fileBContents,
1229 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1230 "Correct file contents of $fileC" );
1231 $this->assertEquals( strlen( $fileBContents ),
1232 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1233 "Correct file size of $fileC" );
1234 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1235 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1236 "Correct file SHA-1 of $fileC" );
1239 public function testDoOperationsPipeline() {
1240 $this->backend
= $this->singleBackend
;
1241 $this->tearDownFiles();
1242 $this->doTestDoOperationsPipeline();
1243 $this->tearDownFiles();
1245 $this->backend
= $this->multiBackend
;
1246 $this->tearDownFiles();
1247 $this->doTestDoOperationsPipeline();
1248 $this->tearDownFiles();
1251 // concurrency orientated
1252 private function doTestDoOperationsPipeline() {
1253 $base = $this->baseStorePath();
1255 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1256 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1257 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1259 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1260 file_put_contents( $tmpNameA, $fileAContents );
1261 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1262 file_put_contents( $tmpNameB, $fileBContents );
1263 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1264 file_put_contents( $tmpNameC, $fileCContents );
1266 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1267 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1268 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1270 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1271 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1272 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1273 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1275 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1276 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1277 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1278 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1279 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1281 $status = $this->backend
->doOperations( array(
1282 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1283 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1284 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1285 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1286 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1287 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1288 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1289 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1290 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1291 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1292 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1293 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1294 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1295 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1296 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1297 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1298 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1299 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1300 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1301 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1303 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1305 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1307 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1309 array( 'op' => 'null' ),
1313 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1314 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1315 $this->assertEquals( 16, count( $status->success
),
1316 "Operation batch has correct success array" );
1318 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1319 "File does not exist at $fileA" );
1320 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1321 "File does not exist at $fileB" );
1322 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1323 "File does not exist at $fileD" );
1325 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1326 "File exists at $fileC" );
1327 $this->assertEquals( $fileBContents,
1328 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1329 "Correct file contents of $fileC" );
1330 $this->assertEquals( strlen( $fileBContents ),
1331 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1332 "Correct file size of $fileC" );
1333 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1334 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1335 "Correct file SHA-1 of $fileC" );
1338 public function testDoOperationsFailing() {
1339 $this->backend
= $this->singleBackend
;
1340 $this->tearDownFiles();
1341 $this->doTestDoOperationsFailing();
1342 $this->tearDownFiles();
1344 $this->backend
= $this->multiBackend
;
1345 $this->tearDownFiles();
1346 $this->doTestDoOperationsFailing();
1347 $this->tearDownFiles();
1350 private function doTestDoOperationsFailing() {
1351 $base = $this->baseStorePath();
1353 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1354 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1355 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1356 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1357 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1358 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1359 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1361 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1362 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1363 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1364 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1365 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1366 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1368 $status = $this->backend
->doOperations( array(
1369 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1370 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1371 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1372 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1373 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1374 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1375 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1376 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1377 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1378 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1379 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1380 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1381 array( 'op' => 'delete', 'src' => $fileD ),
1382 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1383 array( 'op' => 'null' ),
1385 ), array( 'force' => 1 ) );
1387 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1388 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1389 $this->assertEquals( 8, count( $status->success
),
1390 "Operation batch has correct success array" );
1392 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1393 "File does not exist at $fileB" );
1394 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1395 "File does not exist at $fileD" );
1397 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1398 "File does not exist at $fileA" );
1399 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1400 "File exists at $fileC" );
1401 $this->assertEquals( $fileBContents,
1402 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1403 "Correct file contents of $fileA" );
1404 $this->assertEquals( strlen( $fileBContents ),
1405 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1406 "Correct file size of $fileA" );
1407 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1408 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1409 "Correct file SHA-1 of $fileA" );
1412 public function testGetFileList() {
1413 $this->backend
= $this->singleBackend
;
1414 $this->tearDownFiles();
1415 $this->doTestGetFileList();
1416 $this->tearDownFiles();
1418 $this->backend
= $this->multiBackend
;
1419 $this->tearDownFiles();
1420 $this->doTestGetFileList();
1421 $this->tearDownFiles();
1424 private function doTestGetFileList() {
1425 $backendName = $this->backendClass();
1426 $base = $this->baseStorePath();
1428 // Should have no errors
1429 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1432 "$base/unittest-cont1/e/test1.txt",
1433 "$base/unittest-cont1/e/test2.txt",
1434 "$base/unittest-cont1/e/test3.txt",
1435 "$base/unittest-cont1/e/subdir1/test1.txt",
1436 "$base/unittest-cont1/e/subdir1/test2.txt",
1437 "$base/unittest-cont1/e/subdir2/test3.txt",
1438 "$base/unittest-cont1/e/subdir2/test4.txt",
1439 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1440 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1441 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1442 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1443 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1444 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1445 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1450 foreach ( $files as $file ) {
1451 $this->prepare( array( 'dir' => dirname( $file ) ) );
1452 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1454 $status = $this->backend
->doQuickOperations( $ops );
1455 $this->assertGoodStatus( $status,
1456 "Creation of files succeeded ($backendName)." );
1457 $this->assertEquals( true, $status->isOK(),
1458 "Creation of files succeeded with OK status ($backendName)." );
1465 "e/subdir1/test1.txt",
1466 "e/subdir1/test2.txt",
1467 "e/subdir2/test3.txt",
1468 "e/subdir2/test4.txt",
1469 "e/subdir2/subdir/test1.txt",
1470 "e/subdir2/subdir/test2.txt",
1471 "e/subdir2/subdir/test3.txt",
1472 "e/subdir2/subdir/test4.txt",
1473 "e/subdir2/subdir/test5.txt",
1474 "e/subdir2/subdir/sub/test0.txt",
1475 "e/subdir2/subdir/sub/120-px-file.txt",
1479 // Actual listing (no trailing slash)
1481 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1482 foreach ( $iter as $file ) {
1487 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1489 // Actual listing (with trailing slash)
1491 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1492 foreach ( $iter as $file ) {
1497 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1507 "sub/120-px-file.txt",
1511 // Actual listing (no trailing slash)
1513 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1514 foreach ( $iter as $file ) {
1519 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1521 // Actual listing (with trailing slash)
1523 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1524 foreach ( $iter as $file ) {
1529 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1531 // Actual listing (using iterator second time)
1533 foreach ( $iter as $file ) {
1538 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1540 // Expected listing (top files only)
1550 // Actual listing (top files only)
1552 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1553 foreach ( $iter as $file ) {
1558 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1560 foreach ( $files as $file ) { // clean up
1561 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1564 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1565 foreach ( $iter as $iter ) {} // no errors
1568 public function testGetDirectoryList() {
1569 $this->backend
= $this->singleBackend
;
1570 $this->tearDownFiles();
1571 $this->doTestGetDirectoryList();
1572 $this->tearDownFiles();
1574 $this->backend
= $this->multiBackend
;
1575 $this->tearDownFiles();
1576 $this->doTestGetDirectoryList();
1577 $this->tearDownFiles();
1580 private function doTestGetDirectoryList() {
1581 $backendName = $this->backendClass();
1583 $base = $this->baseStorePath();
1585 "$base/unittest-cont1/e/test1.txt",
1586 "$base/unittest-cont1/e/test2.txt",
1587 "$base/unittest-cont1/e/test3.txt",
1588 "$base/unittest-cont1/e/subdir1/test1.txt",
1589 "$base/unittest-cont1/e/subdir1/test2.txt",
1590 "$base/unittest-cont1/e/subdir2/test3.txt",
1591 "$base/unittest-cont1/e/subdir2/test4.txt",
1592 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1593 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1594 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1595 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1596 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1597 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1598 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1603 foreach ( $files as $file ) {
1604 $this->prepare( array( 'dir' => dirname( $file ) ) );
1605 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1607 $status = $this->backend
->doQuickOperations( $ops );
1608 $this->assertGoodStatus( $status,
1609 "Creation of files succeeded ($backendName)." );
1610 $this->assertEquals( true, $status->isOK(),
1611 "Creation of files succeeded with OK status ($backendName)." );
1613 $this->assertEquals( true,
1614 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1615 "Directory exists in ($backendName)." );
1616 $this->assertEquals( true,
1617 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1618 "Directory exists in ($backendName)." );
1619 $this->assertEquals( false,
1620 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1621 "Directory does not exists in ($backendName)." );
1629 // Actual listing (no trailing slash)
1631 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1632 foreach ( $iter as $file ) {
1637 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1648 // Actual listing (no trailing slash)
1650 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1651 foreach ( $iter as $file ) {
1656 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1658 // Actual listing (with trailing slash)
1660 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1661 foreach ( $iter as $file ) {
1666 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1674 // Actual listing (no trailing slash)
1676 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1677 foreach ( $iter as $file ) {
1682 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1684 // Actual listing (with trailing slash)
1686 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1687 foreach ( $iter as $file ) {
1692 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1694 // Actual listing (using iterator second time)
1696 foreach ( $iter as $file ) {
1701 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1703 // Expected listing (recursive)
1713 "e/subdir4/subdir/sub",
1717 // Actual listing (recursive)
1719 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1720 foreach ( $iter as $file ) {
1725 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1727 // Expected listing (recursive)
1734 // Actual listing (recursive)
1736 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
1737 foreach ( $iter as $file ) {
1742 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1744 // Actual listing (recursive, second time)
1746 foreach ( $iter as $file ) {
1751 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1753 foreach ( $files as $file ) { // clean up
1754 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1757 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1758 foreach ( $iter as $iter ) {} // no errors
1761 public function testLockCalls() {
1762 $this->backend
= $this->singleBackend
;
1763 $this->doTestLockCalls();
1766 private function doTestLockCalls() {
1767 $backendName = $this->backendClass();
1769 for ( $i=0; $i<50; $i++
) {
1775 "subdir1", // duplicate
1776 "subdir1/test1.txt",
1777 "subdir1/test2.txt",
1779 "subdir2", // duplicate
1780 "subdir2/test3.txt",
1781 "subdir2/test4.txt",
1783 "subdir2/subdir/test1.txt",
1784 "subdir2/subdir/test2.txt",
1785 "subdir2/subdir/test3.txt",
1786 "subdir2/subdir/test4.txt",
1787 "subdir2/subdir/test5.txt",
1788 "subdir2/subdir/sub",
1789 "subdir2/subdir/sub/test0.txt",
1790 "subdir2/subdir/sub/120-px-file.txt",
1793 $status = $this->backend
->lockFiles( $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)." );
1799 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
1800 $this->assertEquals( array(), $status->errors
,
1801 "Locking of files succeeded ($backendName)." );
1802 $this->assertEquals( true, $status->isOK(),
1803 "Locking of files succeeded with OK status ($backendName)." );
1805 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
1806 $this->assertEquals( array(), $status->errors
,
1807 "Locking of files succeeded ($backendName)." );
1808 $this->assertEquals( true, $status->isOK(),
1809 "Locking of files succeeded with OK status ($backendName)." );
1811 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
1812 $this->assertEquals( array(), $status->errors
,
1813 "Locking of files succeeded ($backendName)." );
1814 $this->assertEquals( true, $status->isOK(),
1815 "Locking of files succeeded with OK status ($backendName)." );
1819 // test helper wrapper for backend prepare() function
1820 private function prepare( array $params ) {
1821 return $this->backend
->prepare( $params );
1824 // test helper wrapper for backend prepare() function
1825 private function create( array $params ) {
1826 $params['op'] = 'create';
1827 return $this->backend
->doQuickOperations( array( $params ) );
1830 function tearDownFiles() {
1831 foreach ( $this->filesToPrune
as $file ) {
1834 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1835 foreach ( $containers as $container ) {
1836 $this->deleteFiles( $container );
1838 $this->filesToPrune
= array();
1841 private function deleteFiles( $container ) {
1842 $base = $this->baseStorePath();
1843 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
1845 foreach ( $iter as $file ) {
1846 $this->backend
->delete( array( 'src' => "$base/$container/$file" ),
1847 array( 'force' => 1, 'nonLocking' => 1 ) );
1850 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
1853 function assertBackendPathsConsistent( array $paths ) {
1854 if ( $this->backend
instanceof FileBackendMultiWrite
) {
1855 $status = $this->backend
->consistencyCheck( $paths );
1856 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
1860 function assertGoodStatus( $status, $msg ) {
1861 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );
1864 function tearDown() {