5 * ^---- causes phpunit to use a higher timeout threshold
10 class FileBackendTest
extends MediaWikiTestCase
{
11 private $backend, $multiBackend;
12 private $filesToPrune = array();
13 private $dirsToPrune = array();
14 private static $backendToUse;
17 global $wgFileBackends;
19 $tmpPrefix = wfTempDir() . '/filebackend-unittest-' . time() . '-' . mt_rand();
20 if ( $this->getCliArg( 'use-filebackend=' ) ) {
21 if ( self
::$backendToUse ) {
22 $this->singleBackend
= self
::$backendToUse;
24 $name = $this->getCliArg( 'use-filebackend=' );
26 foreach ( $wgFileBackends as $conf ) {
27 if ( $conf['name'] == $name ) {
32 $useConfig['name'] = 'localtesting'; // swap name
33 $class = $useConfig['class'];
34 self
::$backendToUse = new $class( $useConfig );
35 $this->singleBackend
= self
::$backendToUse;
38 $this->singleBackend
= new FSFileBackend( array(
39 'name' => 'localtesting',
40 'lockManager' => 'fsLockManager',
41 #'parallelize' => 'implicit',
42 'containerPaths' => array(
43 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
44 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
47 $this->multiBackend
= new FileBackendMultiWrite( array(
48 'name' => 'localtesting',
49 'lockManager' => 'fsLockManager',
50 'parallelize' => 'implicit',
53 'name' => 'localmutlitesting1',
54 'class' => 'FSFileBackend',
55 'lockManager' => 'nullLockManager',
56 'containerPaths' => array(
57 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
58 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
59 'isMultiMaster' => false
62 'name' => 'localmutlitesting2',
63 'class' => 'FSFileBackend',
64 'lockManager' => 'nullLockManager',
65 'containerPaths' => array(
66 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
67 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
68 'isMultiMaster' => true
72 $this->filesToPrune
= array();
75 private function baseStorePath() {
76 return 'mwstore://localtesting';
79 private function backendClass() {
80 return get_class( $this->backend
);
84 * @dataProvider provider_testIsStoragePath
86 public function testIsStoragePath( $path, $isStorePath ) {
87 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
88 "FileBackend::isStoragePath on path '$path'" );
91 function provider_testIsStoragePath() {
93 array( 'mwstore://', true ),
94 array( 'mwstore://backend', true ),
95 array( 'mwstore://backend/container', true ),
96 array( 'mwstore://backend/container/', true ),
97 array( 'mwstore://backend/container/path', true ),
98 array( 'mwstore://backend//container/', true ),
99 array( 'mwstore://backend//container//', true ),
100 array( 'mwstore://backend//container//path', true ),
101 array( 'mwstore:///', true ),
102 array( 'mwstore:/', false ),
103 array( 'mwstore:', false ),
108 * @dataProvider provider_testSplitStoragePath
110 public function testSplitStoragePath( $path, $res ) {
111 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
112 "FileBackend::splitStoragePath on path '$path'" );
115 function provider_testSplitStoragePath() {
117 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
118 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
119 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
120 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
121 array( 'mwstore://backend//container/path', array( null, null, null ) ),
122 array( 'mwstore://backend//container//path', array( null, null, null ) ),
123 array( 'mwstore://', array( null, null, null ) ),
124 array( 'mwstore://backend', array( null, null, null ) ),
125 array( 'mwstore:///', array( null, null, null ) ),
126 array( 'mwstore:/', array( null, null, null ) ),
127 array( 'mwstore:', array( null, null, null ) )
132 * @dataProvider provider_normalizeStoragePath
134 public function testNormalizeStoragePath( $path, $res ) {
135 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
136 "FileBackend::normalizeStoragePath on path '$path'" );
139 function provider_normalizeStoragePath() {
141 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
142 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
143 array( 'mwstore://backend/container/path', 'mwstore://backend/container/path' ),
144 array( 'mwstore://backend/container//path', 'mwstore://backend/container/path' ),
145 array( 'mwstore://backend/container///path', 'mwstore://backend/container/path' ),
146 array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj',
147 array( 'mwstore://', null ),
148 array( 'mwstore://backend', null ),
149 array( 'mwstore://backend//container/path', null ),
150 array( 'mwstore://backend//container//path', null ),
151 array( 'mwstore:///', null ),
152 array( 'mwstore:/', null ),
153 array( 'mwstore:', null ), )
158 * @dataProvider provider_testParentStoragePath
160 public function testParentStoragePath( $path, $res ) {
161 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
162 "FileBackend::parentStoragePath on path '$path'" );
165 function provider_testParentStoragePath() {
167 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
168 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
169 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
170 array( 'mwstore://backend/container', null ),
171 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
172 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
173 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
174 array( 'mwstore://backend/container/', null ),
179 * @dataProvider provider_testExtensionFromPath
181 public function testExtensionFromPath( $path, $res ) {
182 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
183 "FileBackend::extensionFromPath on path '$path'" );
186 function provider_testExtensionFromPath() {
188 array( 'mwstore://backend/container/path.txt', 'txt' ),
189 array( 'mwstore://backend/container/path.svg.png', 'png' ),
190 array( 'mwstore://backend/container/path', '' ),
191 array( 'mwstore://backend/container/path.', '' ),
196 * @dataProvider provider_testStore
198 public function testStore( $op ) {
199 $this->filesToPrune
[] = $op['src'];
201 $this->backend
= $this->singleBackend
;
202 $this->tearDownFiles();
203 $this->doTestStore( $op );
204 $this->tearDownFiles();
206 $this->backend
= $this->multiBackend
;
207 $this->tearDownFiles();
208 $this->doTestStore( $op );
209 $this->filesToPrune
[] = $op['src']; # avoid file leaking
210 $this->tearDownFiles();
213 private function doTestStore( $op ) {
214 $backendName = $this->backendClass();
216 $source = $op['src'];
218 $this->prepare( array( 'dir' => dirname( $dest ) ) );
220 file_put_contents( $source, "Unit test file" );
222 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
223 $this->backend
->store( $op );
226 $status = $this->backend
->doOperation( $op );
228 $this->assertGoodStatus( $status,
229 "Store from $source to $dest succeeded without warnings ($backendName)." );
230 $this->assertEquals( true, $status->isOK(),
231 "Store from $source to $dest succeeded ($backendName)." );
232 $this->assertEquals( array( 0 => true ), $status->success
,
233 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
234 $this->assertEquals( true, file_exists( $source ),
235 "Source file $source still exists ($backendName)." );
236 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
237 "Destination file $dest exists ($backendName)." );
239 $this->assertEquals( filesize( $source ),
240 $this->backend
->getFileSize( array( 'src' => $dest ) ),
241 "Destination file $dest has correct size ($backendName)." );
243 $props1 = FSFile
::getPropsFromPath( $source );
244 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
245 $this->assertEquals( $props1, $props2,
246 "Source and destination have the same props ($backendName)." );
249 public function provider_testStore() {
252 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
253 $toPath = $this->baseStorePath() . '/unittest-cont1/fun/obj1.txt';
254 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
262 $op2['overwrite'] = true;
270 $op2['overwriteSame'] = true;
281 * @dataProvider provider_testCopy
283 public function testCopy( $op ) {
284 $this->backend
= $this->singleBackend
;
285 $this->tearDownFiles();
286 $this->doTestCopy( $op );
287 $this->tearDownFiles();
289 $this->backend
= $this->multiBackend
;
290 $this->tearDownFiles();
291 $this->doTestCopy( $op );
292 $this->tearDownFiles();
295 private function doTestCopy( $op ) {
296 $backendName = $this->backendClass();
298 $source = $op['src'];
300 $this->prepare( array( 'dir' => dirname( $source ) ) );
301 $this->prepare( array( 'dir' => dirname( $dest ) ) );
303 $status = $this->backend
->doOperation(
304 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
305 $this->assertGoodStatus( $status,
306 "Creation of file at $source succeeded ($backendName)." );
308 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
309 $this->backend
->copy( $op );
312 $status = $this->backend
->doOperation( $op );
314 $this->assertGoodStatus( $status,
315 "Copy from $source to $dest succeeded without warnings ($backendName)." );
316 $this->assertEquals( true, $status->isOK(),
317 "Copy from $source to $dest succeeded ($backendName)." );
318 $this->assertEquals( array( 0 => true ), $status->success
,
319 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
320 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
321 "Source file $source still exists ($backendName)." );
322 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
323 "Destination file $dest exists after copy ($backendName)." );
326 $this->backend
->getFileSize( array( 'src' => $source ) ),
327 $this->backend
->getFileSize( array( 'src' => $dest ) ),
328 "Destination file $dest has correct size ($backendName)." );
330 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
331 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
332 $this->assertEquals( $props1, $props2,
333 "Source and destination have the same props ($backendName)." );
336 public function provider_testCopy() {
339 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
340 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
342 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
350 $op2['overwrite'] = true;
358 $op2['overwriteSame'] = true;
369 * @dataProvider provider_testMove
371 public function testMove( $op ) {
372 $this->backend
= $this->singleBackend
;
373 $this->tearDownFiles();
374 $this->doTestMove( $op );
375 $this->tearDownFiles();
377 $this->backend
= $this->multiBackend
;
378 $this->tearDownFiles();
379 $this->doTestMove( $op );
380 $this->tearDownFiles();
383 private function doTestMove( $op ) {
384 $backendName = $this->backendClass();
386 $source = $op['src'];
388 $this->prepare( array( 'dir' => dirname( $source ) ) );
389 $this->prepare( array( 'dir' => dirname( $dest ) ) );
391 $status = $this->backend
->doOperation(
392 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
393 $this->assertGoodStatus( $status,
394 "Creation of file at $source succeeded ($backendName)." );
396 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
397 $this->backend
->copy( $op );
400 $status = $this->backend
->doOperation( $op );
401 $this->assertGoodStatus( $status,
402 "Move from $source to $dest succeeded without warnings ($backendName)." );
403 $this->assertEquals( true, $status->isOK(),
404 "Move from $source to $dest succeeded ($backendName)." );
405 $this->assertEquals( array( 0 => true ), $status->success
,
406 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
407 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
408 "Source file $source does not still exists ($backendName)." );
409 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
410 "Destination file $dest exists after move ($backendName)." );
412 $this->assertNotEquals(
413 $this->backend
->getFileSize( array( 'src' => $source ) ),
414 $this->backend
->getFileSize( array( 'src' => $dest ) ),
415 "Destination file $dest has correct size ($backendName)." );
417 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
418 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
419 $this->assertEquals( false, $props1['fileExists'],
420 "Source file does not exist accourding to props ($backendName)." );
421 $this->assertEquals( true, $props2['fileExists'],
422 "Destination file exists accourding to props ($backendName)." );
425 public function provider_testMove() {
428 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
429 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
431 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
439 $op2['overwrite'] = true;
447 $op2['overwriteSame'] = true;
458 * @dataProvider provider_testDelete
460 public function testDelete( $op, $withSource, $okStatus ) {
461 $this->backend
= $this->singleBackend
;
462 $this->tearDownFiles();
463 $this->doTestDelete( $op, $withSource, $okStatus );
464 $this->tearDownFiles();
466 $this->backend
= $this->multiBackend
;
467 $this->tearDownFiles();
468 $this->doTestDelete( $op, $withSource, $okStatus );
469 $this->tearDownFiles();
472 private function doTestDelete( $op, $withSource, $okStatus ) {
473 $backendName = $this->backendClass();
475 $source = $op['src'];
476 $this->prepare( array( 'dir' => dirname( $source ) ) );
479 $status = $this->backend
->doOperation(
480 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
481 $this->assertGoodStatus( $status,
482 "Creation of file at $source succeeded ($backendName)." );
485 $status = $this->backend
->doOperation( $op );
487 $this->assertGoodStatus( $status,
488 "Deletion of file at $source succeeded without warnings ($backendName)." );
489 $this->assertEquals( true, $status->isOK(),
490 "Deletion of file at $source succeeded ($backendName)." );
491 $this->assertEquals( array( 0 => true ), $status->success
,
492 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
494 $this->assertEquals( false, $status->isOK(),
495 "Deletion of file at $source failed ($backendName)." );
498 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
499 "Source file $source does not exist after move ($backendName)." );
502 $this->backend
->getFileSize( array( 'src' => $source ) ),
503 "Source file $source has correct size (false) ($backendName)." );
505 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
506 $this->assertFalse( $props1['fileExists'],
507 "Source file $source does not exist according to props ($backendName)." );
510 public function provider_testDelete() {
513 $source = $this->baseStorePath() . '/unittest-cont1/myfacefile.txt';
515 $op = array( 'op' => 'delete', 'src' => $source );
524 false, // without source
528 $op['ignoreMissingSource'] = true;
531 false, // without source
539 * @dataProvider provider_testCreate
541 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
542 $this->backend
= $this->singleBackend
;
543 $this->tearDownFiles();
544 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
545 $this->tearDownFiles();
547 $this->backend
= $this->multiBackend
;
548 $this->tearDownFiles();
549 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
550 $this->tearDownFiles();
553 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
554 $backendName = $this->backendClass();
557 $this->prepare( array( 'dir' => dirname( $dest ) ) );
559 $oldText = 'blah...blah...waahwaah';
560 if ( $alreadyExists ) {
561 $status = $this->backend
->doOperation(
562 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
563 $this->assertGoodStatus( $status,
564 "Creation of file at $dest succeeded ($backendName)." );
567 $status = $this->backend
->doOperation( $op );
569 $this->assertGoodStatus( $status,
570 "Creation of file at $dest succeeded without warnings ($backendName)." );
571 $this->assertEquals( true, $status->isOK(),
572 "Creation of file at $dest succeeded ($backendName)." );
573 $this->assertEquals( array( 0 => true ), $status->success
,
574 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
576 $this->assertEquals( false, $status->isOK(),
577 "Creation of file at $dest failed ($backendName)." );
580 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
581 "Destination file $dest exists after creation ($backendName)." );
583 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
584 $this->assertEquals( true, $props1['fileExists'],
585 "Destination file $dest exists according to props ($backendName)." );
586 if ( $okStatus ) { // file content is what we saved
587 $this->assertEquals( $newSize, $props1['size'],
588 "Destination file $dest has expected size according to props ($backendName)." );
589 $this->assertEquals( $newSize,
590 $this->backend
->getFileSize( array( 'src' => $dest ) ),
591 "Destination file $dest has correct size ($backendName)." );
592 } else { // file content is some other previous text
593 $this->assertEquals( strlen( $oldText ), $props1['size'],
594 "Destination file $dest has original size according to props ($backendName)." );
595 $this->assertEquals( strlen( $oldText ),
596 $this->backend
->getFileSize( array( 'src' => $dest ) ),
597 "Destination file $dest has original size according to props ($backendName)." );
602 * @dataProvider provider_testCreate
604 public function provider_testCreate() {
607 $dest = $this->baseStorePath() . '/unittest-cont2/myspacefile.txt';
609 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
612 false, // no dest already exists
614 strlen( $op['content'] )
618 $op2['content'] = "\n";
621 false, // no dest already exists
623 strlen( $op2['content'] )
627 $op2['content'] = "fsf\n waf 3kt";
630 true, // dest already exists
632 strlen( $op2['content'] )
636 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
637 $op2['overwrite'] = true;
640 true, // dest already exists
642 strlen( $op2['content'] )
646 $op2['content'] = "39qjmg3-qg";
647 $op2['overwriteSame'] = true;
650 true, // dest already exists
652 strlen( $op2['content'] )
659 * @dataProvider provider_testConcatenate
661 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
662 $this->filesToPrune
[] = $op['dst'];
664 $this->backend
= $this->singleBackend
;
665 $this->tearDownFiles();
666 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
667 $this->tearDownFiles();
669 $this->backend
= $this->multiBackend
;
670 $this->tearDownFiles();
671 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
672 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
673 $this->tearDownFiles();
676 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
677 $backendName = $this->backendClass();
682 foreach ( $srcs as $i => $source ) {
683 $this->prepare( array( 'dir' => dirname( $source ) ) );
685 'op' => 'create', // operation
686 'dst' => $source, // source
687 'content' => $srcsContent[$i]
689 $expContent .= $srcsContent[$i];
691 $status = $this->backend
->doOperations( $ops );
693 $this->assertGoodStatus( $status,
694 "Creation of source files succeeded ($backendName)." );
696 $dest = $params['dst'];
697 if ( $alreadyExists ) {
698 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
699 $this->assertEquals( true, $ok,
700 "Creation of file at $dest succeeded ($backendName)." );
702 $ok = file_put_contents( $dest, '' ) !== false;
703 $this->assertEquals( true, $ok,
704 "Creation of 0-byte file at $dest succeeded ($backendName)." );
707 // Combine the files into one
708 $status = $this->backend
->concatenate( $params );
710 $this->assertGoodStatus( $status,
711 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
712 $this->assertEquals( true, $status->isOK(),
713 "Creation of concat file at $dest succeeded ($backendName)." );
715 $this->assertEquals( false, $status->isOK(),
716 "Creation of concat file at $dest failed ($backendName)." );
720 $this->assertEquals( true, is_file( $dest ),
721 "Dest concat file $dest exists after creation ($backendName)." );
723 $this->assertEquals( true, is_file( $dest ),
724 "Dest concat file $dest exists after failed creation ($backendName)." );
727 $contents = file_get_contents( $dest );
728 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
731 $this->assertEquals( $expContent, $contents,
732 "Concat file at $dest has correct contents ($backendName)." );
734 $this->assertNotEquals( $expContent, $contents,
735 "Concat file at $dest has correct contents ($backendName)." );
739 function provider_testConcatenate() {
742 $rand = mt_rand( 0, 2000000000 ) . time();
743 $dest = wfTempDir() . "/randomfile!$rand.txt";
745 $this->baseStorePath() . '/unittest-cont1/file1.txt',
746 $this->baseStorePath() . '/unittest-cont1/file2.txt',
747 $this->baseStorePath() . '/unittest-cont1/file3.txt',
748 $this->baseStorePath() . '/unittest-cont1/file4.txt',
749 $this->baseStorePath() . '/unittest-cont1/file5.txt',
750 $this->baseStorePath() . '/unittest-cont1/file6.txt',
751 $this->baseStorePath() . '/unittest-cont1/file7.txt',
752 $this->baseStorePath() . '/unittest-cont1/file8.txt',
753 $this->baseStorePath() . '/unittest-cont1/file9.txt',
754 $this->baseStorePath() . '/unittest-cont1/file10.txt'
768 $params = array( 'srcs' => $srcs, 'dst' => $dest );
771 $params, // operation
773 $content, // content for each source
774 false, // no dest already exists
779 $params, // operation
781 $content, // content for each source
782 true, // dest already exists
790 * @dataProvider provider_testGetFileStat
792 public function testGetFileStat( $path, $content, $alreadyExists ) {
793 $this->backend
= $this->singleBackend
;
794 $this->tearDownFiles();
795 $this->doTestGetFileStat( $path, $content, $alreadyExists );
796 $this->tearDownFiles();
798 $this->backend
= $this->multiBackend
;
799 $this->tearDownFiles();
800 $this->doTestGetFileStat( $path, $content, $alreadyExists );
801 $this->tearDownFiles();
804 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
805 $backendName = $this->backendClass();
807 if ( $alreadyExists ) {
808 $this->prepare( array( 'dir' => dirname( $path ) ) );
809 $status = $this->backend
->create( array( 'dst' => $path, 'content' => $content ) );
810 $this->assertGoodStatus( $status,
811 "Creation of file at $path succeeded ($backendName)." );
813 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
814 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
815 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
817 $this->assertEquals( strlen( $content ), $size,
818 "Correct file size of '$path'" );
819 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 5,
820 "Correct file timestamp of '$path'" );
822 $size = $stat['size'];
823 $time = $stat['mtime'];
824 $this->assertEquals( strlen( $content ), $size,
825 "Correct file size of '$path'" );
826 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 5,
827 "Correct file timestamp of '$path'" );
829 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
830 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
831 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
833 $this->assertFalse( $size, "Correct file size of '$path'" );
834 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
835 $this->assertFalse( $stat, "Correct file stat of '$path'" );
839 function provider_testGetFileStat() {
842 $base = $this->baseStorePath();
843 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents", true );
844 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "", true );
845 $cases[] = array( "$base/unittest-cont1/b/some-diff_file.txt", null, false );
851 * @dataProvider provider_testGetFileContents
853 public function testGetFileContents( $source, $content ) {
854 $this->backend
= $this->singleBackend
;
855 $this->tearDownFiles();
856 $this->doTestGetFileContents( $source, $content );
857 $this->tearDownFiles();
859 $this->backend
= $this->multiBackend
;
860 $this->tearDownFiles();
861 $this->doTestGetFileContents( $source, $content );
862 $this->tearDownFiles();
865 private function doTestGetFileContents( $source, $content ) {
866 $backendName = $this->backendClass();
868 $this->prepare( array( 'dir' => dirname( $source ) ) );
870 $status = $this->backend
->doOperation(
871 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
872 $this->assertGoodStatus( $status,
873 "Creation of file at $source succeeded ($backendName)." );
874 $this->assertEquals( true, $status->isOK(),
875 "Creation of file at $source succeeded with OK status ($backendName)." );
877 $newContents = $this->backend
->getFileContents( array( 'src' => $source, 'latest' => 1 ) );
878 $this->assertNotEquals( false, $newContents,
879 "Read of file at $source succeeded ($backendName)." );
881 $this->assertEquals( $content, $newContents,
882 "Contents read match data at $source ($backendName)." );
885 function provider_testGetFileContents() {
888 $base = $this->baseStorePath();
889 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents" );
890 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "more file contents" );
896 * @dataProvider provider_testGetLocalCopy
898 public function testGetLocalCopy( $source, $content ) {
899 $this->backend
= $this->singleBackend
;
900 $this->tearDownFiles();
901 $this->doTestGetLocalCopy( $source, $content );
902 $this->tearDownFiles();
904 $this->backend
= $this->multiBackend
;
905 $this->tearDownFiles();
906 $this->doTestGetLocalCopy( $source, $content );
907 $this->tearDownFiles();
910 private function doTestGetLocalCopy( $source, $content ) {
911 $backendName = $this->backendClass();
913 $this->prepare( array( 'dir' => dirname( $source ) ) );
915 $status = $this->backend
->doOperation(
916 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
917 $this->assertGoodStatus( $status,
918 "Creation of file at $source succeeded ($backendName)." );
920 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
921 $this->assertNotNull( $tmpFile,
922 "Creation of local copy of $source succeeded ($backendName)." );
924 $contents = file_get_contents( $tmpFile->getPath() );
925 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
928 function provider_testGetLocalCopy() {
931 $base = $this->baseStorePath();
932 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
933 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
939 * @dataProvider provider_testGetLocalReference
941 public function testGetLocalReference( $source, $content ) {
942 $this->backend
= $this->singleBackend
;
943 $this->tearDownFiles();
944 $this->doTestGetLocalReference( $source, $content );
945 $this->tearDownFiles();
947 $this->backend
= $this->multiBackend
;
948 $this->tearDownFiles();
949 $this->doTestGetLocalReference( $source, $content );
950 $this->tearDownFiles();
953 private function doTestGetLocalReference( $source, $content ) {
954 $backendName = $this->backendClass();
956 $this->prepare( array( 'dir' => dirname( $source ) ) );
958 $status = $this->backend
->doOperation(
959 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
960 $this->assertGoodStatus( $status,
961 "Creation of file at $source succeeded ($backendName)." );
963 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
964 $this->assertNotNull( $tmpFile,
965 "Creation of local copy of $source succeeded ($backendName)." );
967 $contents = file_get_contents( $tmpFile->getPath() );
968 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
971 function provider_testGetLocalReference() {
974 $base = $this->baseStorePath();
975 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
976 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
982 * @dataProvider provider_testPrepareAndClean
984 public function testPrepareAndClean( $path, $isOK ) {
985 $this->backend
= $this->singleBackend
;
986 $this->doTestPrepareAndClean( $path, $isOK );
987 $this->tearDownFiles();
989 $this->backend
= $this->multiBackend
;
990 $this->doTestPrepareAndClean( $path, $isOK );
991 $this->tearDownFiles();
994 function provider_testPrepareAndClean() {
995 $base = $this->baseStorePath();
997 array( "$base/unittest-cont1/a/z/some_file1.txt", true ),
998 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
999 # Specific to FS backend with no basePath field set
1000 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1004 private function doTestPrepareAndClean( $path, $isOK ) {
1005 $backendName = $this->backendClass();
1007 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1009 $this->assertGoodStatus( $status,
1010 "Preparing dir $path succeeded without warnings ($backendName)." );
1011 $this->assertEquals( true, $status->isOK(),
1012 "Preparing dir $path succeeded ($backendName)." );
1014 $this->assertEquals( false, $status->isOK(),
1015 "Preparing dir $path failed ($backendName)." );
1018 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1020 $this->assertGoodStatus( $status,
1021 "Cleaning dir $path succeeded without warnings ($backendName)." );
1022 $this->assertEquals( true, $status->isOK(),
1023 "Cleaning dir $path succeeded ($backendName)." );
1025 $this->assertEquals( false, $status->isOK(),
1026 "Cleaning dir $path failed ($backendName)." );
1030 public function testRecursiveClean() {
1031 $this->backend
= $this->singleBackend
;
1032 $this->doTestRecursiveClean();
1033 $this->tearDownFiles();
1035 $this->backend
= $this->multiBackend
;
1036 $this->doTestRecursiveClean();
1037 $this->tearDownFiles();
1040 function doTestRecursiveClean() {
1041 $backendName = $this->backendClass();
1043 $base = $this->baseStorePath();
1045 "$base/unittest-cont1/a",
1046 "$base/unittest-cont1/a/b",
1047 "$base/unittest-cont1/a/b/c",
1048 "$base/unittest-cont1/a/b/c/d0",
1049 "$base/unittest-cont1/a/b/c/d1",
1050 "$base/unittest-cont1/a/b/c/d2",
1051 "$base/unittest-cont1/a/b/c/d0/1",
1052 "$base/unittest-cont1/a/b/c/d0/2",
1053 "$base/unittest-cont1/a/b/c/d1/3",
1054 "$base/unittest-cont1/a/b/c/d1/4",
1055 "$base/unittest-cont1/a/b/c/d2/5",
1056 "$base/unittest-cont1/a/b/c/d2/6"
1058 foreach ( $dirs as $dir ) {
1059 $status = $this->prepare( array( 'dir' => $dir ) );
1060 $this->assertGoodStatus( $status,
1061 "Preparing dir $dir succeeded without warnings ($backendName)." );
1064 if ( $this->backend
instanceof FSFileBackend
) {
1065 foreach ( $dirs as $dir ) {
1066 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1067 "Dir $dir exists ($backendName)." );
1071 $status = $this->backend
->clean(
1072 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1073 $this->assertGoodStatus( $status,
1074 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1076 foreach ( $dirs as $dir ) {
1077 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1078 "Dir $dir no longer exists ($backendName)." );
1082 // @TODO: testSecure
1084 public function testDoOperations() {
1085 $this->backend
= $this->singleBackend
;
1086 $this->tearDownFiles();
1087 $this->doTestDoOperations();
1088 $this->tearDownFiles();
1090 $this->backend
= $this->multiBackend
;
1091 $this->tearDownFiles();
1092 $this->doTestDoOperations();
1093 $this->tearDownFiles();
1095 $this->backend
= $this->singleBackend
;
1096 $this->tearDownFiles();
1097 $this->doTestDoOperations2();
1098 $this->tearDownFiles();
1100 $this->backend
= $this->multiBackend
;
1101 $this->tearDownFiles();
1102 $this->doTestDoOperations2();
1103 $this->tearDownFiles();
1105 $this->backend
= $this->singleBackend
;
1106 $this->tearDownFiles();
1107 $this->doTestDoOperationsFailing();
1108 $this->tearDownFiles();
1110 $this->backend
= $this->multiBackend
;
1111 $this->tearDownFiles();
1112 $this->doTestDoOperationsFailing();
1113 $this->tearDownFiles();
1116 private function doTestDoOperations() {
1117 $base = $this->baseStorePath();
1119 $fileA = "$base/unittest-cont1/a/b/fileA.txt";
1120 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1121 $fileB = "$base/unittest-cont1/a/b/fileB.txt";
1122 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1123 $fileC = "$base/unittest-cont1/a/b/fileC.txt";
1124 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1125 $fileD = "$base/unittest-cont1/a/b/fileD.txt";
1127 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1128 $this->backend
->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1129 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1130 $this->backend
->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1131 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1132 $this->backend
->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1133 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1135 $status = $this->backend
->doOperations( array(
1136 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1137 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1138 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1139 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1140 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1141 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1142 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1143 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1144 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1145 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1146 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1147 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1148 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1149 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1150 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1151 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1152 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1154 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1156 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1158 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1160 array( 'op' => 'null' ),
1164 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1165 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1166 $this->assertEquals( 13, count( $status->success
),
1167 "Operation batch has correct success array" );
1169 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1170 "File does not exist at $fileA" );
1171 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1172 "File does not exist at $fileB" );
1173 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1174 "File does not exist at $fileD" );
1176 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1177 "File exists at $fileC" );
1178 $this->assertEquals( $fileBContents,
1179 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1180 "Correct file contents of $fileC" );
1181 $this->assertEquals( strlen( $fileBContents ),
1182 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1183 "Correct file size of $fileC" );
1184 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1185 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1186 "Correct file SHA-1 of $fileC" );
1189 // concurrency orientated
1190 function doTestDoOperations2() {
1191 $base = $this->baseStorePath();
1193 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1194 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1195 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1197 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1198 file_put_contents( $tmpNameA, $fileAContents );
1199 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1200 file_put_contents( $tmpNameB, $fileBContents );
1201 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1202 file_put_contents( $tmpNameC, $fileCContents );
1204 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1205 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1206 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1208 $fileA = "$base/unittest-cont1/a/b/fileA.txt";
1209 $fileB = "$base/unittest-cont1/a/b/fileB.txt";
1210 $fileC = "$base/unittest-cont1/a/b/fileC.txt";
1211 $fileD = "$base/unittest-cont1/a/b/fileD.txt";
1213 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1214 $this->backend
->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1215 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1216 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1217 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1219 $status = $this->backend
->doOperations( array(
1220 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1221 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1222 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1223 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1224 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1225 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1226 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1227 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1228 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1229 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1230 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1231 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1232 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1233 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1234 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1235 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1236 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1237 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1238 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1239 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1241 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1243 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1245 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1247 array( 'op' => 'null' ),
1251 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1252 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1253 $this->assertEquals( 16, count( $status->success
),
1254 "Operation batch has correct success array" );
1256 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1257 "File does not exist at $fileA" );
1258 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1259 "File does not exist at $fileB" );
1260 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1261 "File does not exist at $fileD" );
1263 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1264 "File exists at $fileC" );
1265 $this->assertEquals( $fileBContents,
1266 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1267 "Correct file contents of $fileC" );
1268 $this->assertEquals( strlen( $fileBContents ),
1269 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1270 "Correct file size of $fileC" );
1271 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1272 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1273 "Correct file SHA-1 of $fileC" );
1276 function doTestDoOperationsFailing() {
1277 $base = $this->baseStorePath();
1279 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1280 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1281 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1282 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1283 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1284 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1285 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1287 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1288 $this->backend
->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1289 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1290 $this->backend
->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1291 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1292 $this->backend
->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1294 $status = $this->backend
->doOperations( array(
1295 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1296 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1297 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1298 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1299 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1300 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1301 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1302 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1303 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1304 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1305 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1306 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1307 array( 'op' => 'delete', 'src' => $fileD ),
1308 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1309 array( 'op' => 'null' ),
1311 ), array( 'force' => 1 ) );
1313 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1314 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1315 $this->assertEquals( 8, count( $status->success
),
1316 "Operation batch has correct success array" );
1318 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1319 "File does not exist at $fileB" );
1320 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1321 "File does not exist at $fileD" );
1323 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1324 "File does not exist at $fileA" );
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' => $fileA ) ),
1329 "Correct file contents of $fileA" );
1330 $this->assertEquals( strlen( $fileBContents ),
1331 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1332 "Correct file size of $fileA" );
1333 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1334 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1335 "Correct file SHA-1 of $fileA" );
1338 public function testGetFileList() {
1339 $this->backend
= $this->singleBackend
;
1340 $this->tearDownFiles();
1341 $this->doTestGetFileList();
1342 $this->tearDownFiles();
1344 $this->backend
= $this->multiBackend
;
1345 $this->tearDownFiles();
1346 $this->doTestGetFileList();
1347 $this->tearDownFiles();
1350 private function doTestGetFileList() {
1351 $backendName = $this->backendClass();
1353 $base = $this->baseStorePath();
1355 "$base/unittest-cont1/test1.txt",
1356 "$base/unittest-cont1/test2.txt",
1357 "$base/unittest-cont1/test3.txt",
1358 "$base/unittest-cont1/subdir1/test1.txt",
1359 "$base/unittest-cont1/subdir1/test2.txt",
1360 "$base/unittest-cont1/subdir2/test3.txt",
1361 "$base/unittest-cont1/subdir2/test4.txt",
1362 "$base/unittest-cont1/subdir2/subdir/test1.txt",
1363 "$base/unittest-cont1/subdir2/subdir/test2.txt",
1364 "$base/unittest-cont1/subdir2/subdir/test3.txt",
1365 "$base/unittest-cont1/subdir2/subdir/test4.txt",
1366 "$base/unittest-cont1/subdir2/subdir/test5.txt",
1367 "$base/unittest-cont1/subdir2/subdir/sub/test0.txt",
1368 "$base/unittest-cont1/subdir2/subdir/sub/120-px-file.txt",
1373 foreach ( $files as $file ) {
1374 $this->prepare( array( 'dir' => dirname( $file ) ) );
1375 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1377 $status = $this->backend
->doOperations( $ops );
1378 $this->assertGoodStatus( $status,
1379 "Creation of files succeeded ($backendName)." );
1380 $this->assertEquals( true, $status->isOK(),
1381 "Creation of files succeeded with OK status ($backendName)." );
1388 "subdir1/test1.txt",
1389 "subdir1/test2.txt",
1390 "subdir2/test3.txt",
1391 "subdir2/test4.txt",
1392 "subdir2/subdir/test1.txt",
1393 "subdir2/subdir/test2.txt",
1394 "subdir2/subdir/test3.txt",
1395 "subdir2/subdir/test4.txt",
1396 "subdir2/subdir/test5.txt",
1397 "subdir2/subdir/sub/test0.txt",
1398 "subdir2/subdir/sub/120-px-file.txt",
1402 // Actual listing (no trailing slash)
1404 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1405 foreach ( $iter as $file ) {
1410 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1412 // Actual listing (with trailing slash)
1414 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1415 foreach ( $iter as $file ) {
1420 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1430 "sub/120-px-file.txt",
1434 // Actual listing (no trailing slash)
1436 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) );
1437 foreach ( $iter as $file ) {
1442 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1444 // Actual listing (with trailing slash)
1446 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir/" ) );
1447 foreach ( $iter as $file ) {
1452 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1454 // Actual listing (using iterator second time)
1456 foreach ( $iter as $file ) {
1461 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1463 // Expected listing (top files only)
1473 // Actual listing (top files only)
1475 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) );
1476 foreach ( $iter as $file ) {
1481 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1483 foreach ( $files as $file ) { // clean up
1484 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1487 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1488 foreach ( $iter as $iter ) {} // no errors
1491 public function testGetDirectoryList() {
1492 $this->backend
= $this->singleBackend
;
1493 $this->tearDownFiles();
1494 $this->doTestGetDirectoryList();
1495 $this->tearDownFiles();
1497 $this->backend
= $this->multiBackend
;
1498 $this->tearDownFiles();
1499 $this->doTestGetDirectoryList();
1500 $this->tearDownFiles();
1503 private function doTestGetDirectoryList() {
1504 $backendName = $this->backendClass();
1506 $base = $this->baseStorePath();
1508 "$base/unittest-cont1/test1.txt",
1509 "$base/unittest-cont1/test2.txt",
1510 "$base/unittest-cont1/test3.txt",
1511 "$base/unittest-cont1/subdir1/test1.txt",
1512 "$base/unittest-cont1/subdir1/test2.txt",
1513 "$base/unittest-cont1/subdir2/test3.txt",
1514 "$base/unittest-cont1/subdir2/test4.txt",
1515 "$base/unittest-cont1/subdir2/subdir/test1.txt",
1516 "$base/unittest-cont1/subdir3/subdir/test2.txt",
1517 "$base/unittest-cont1/subdir4/subdir/test3.txt",
1518 "$base/unittest-cont1/subdir4/subdir/test4.txt",
1519 "$base/unittest-cont1/subdir4/subdir/test5.txt",
1520 "$base/unittest-cont1/subdir4/subdir/sub/test0.txt",
1521 "$base/unittest-cont1/subdir4/subdir/sub/120-px-file.txt",
1526 foreach ( $files as $file ) {
1527 $this->prepare( array( 'dir' => dirname( $file ) ) );
1528 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1530 $status = $this->backend
->doOperations( $ops );
1531 $this->assertGoodStatus( $status,
1532 "Creation of files succeeded ($backendName)." );
1533 $this->assertEquals( true, $status->isOK(),
1534 "Creation of files succeeded with OK status ($backendName)." );
1545 $this->assertEquals( true,
1546 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/subdir1" ) ),
1547 "Directory exists in ($backendName)." );
1548 $this->assertEquals( true,
1549 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) ),
1550 "Directory exists in ($backendName)." );
1551 $this->assertEquals( false,
1552 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/subdir2/test1.txt" ) ),
1553 "Directory does not exists in ($backendName)." );
1555 // Actual listing (no trailing slash)
1557 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1558 foreach ( $iter as $file ) {
1563 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1565 // Actual listing (with trailing slash)
1567 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1568 foreach ( $iter as $file ) {
1573 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1581 // Actual listing (no trailing slash)
1583 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/subdir2" ) );
1584 foreach ( $iter as $file ) {
1589 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1591 // Actual listing (with trailing slash)
1593 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/subdir2/" ) );
1594 foreach ( $iter as $file ) {
1599 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1601 // Actual listing (using iterator second time)
1603 foreach ( $iter as $file ) {
1608 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1610 // Expected listing (recursive)
1619 "subdir4/subdir/sub",
1623 // Actual listing (recursive)
1625 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1626 foreach ( $iter as $file ) {
1631 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1633 // Expected listing (recursive)
1640 // Actual listing (recursive)
1642 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/subdir4" ) );
1643 foreach ( $iter as $file ) {
1648 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1650 // Actual listing (recursive, second time)
1652 foreach ( $iter as $file ) {
1657 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1659 foreach ( $files as $file ) { // clean up
1660 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1663 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1664 foreach ( $iter as $iter ) {} // no errors
1667 public function testLockCalls() {
1668 $this->backend
= $this->singleBackend
;
1669 $this->doTestLockCalls();
1672 private function doTestLockCalls() {
1673 $backendName = $this->backendClass();
1675 for ( $i=0; $i<50; $i++
) {
1681 "subdir1", // duplicate
1682 "subdir1/test1.txt",
1683 "subdir1/test2.txt",
1685 "subdir2", // duplicate
1686 "subdir2/test3.txt",
1687 "subdir2/test4.txt",
1689 "subdir2/subdir/test1.txt",
1690 "subdir2/subdir/test2.txt",
1691 "subdir2/subdir/test3.txt",
1692 "subdir2/subdir/test4.txt",
1693 "subdir2/subdir/test5.txt",
1694 "subdir2/subdir/sub",
1695 "subdir2/subdir/sub/test0.txt",
1696 "subdir2/subdir/sub/120-px-file.txt",
1699 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
1700 $this->assertEquals( array(), $status->errors
,
1701 "Locking of files succeeded ($backendName)." );
1702 $this->assertEquals( true, $status->isOK(),
1703 "Locking of files succeeded with OK status ($backendName)." );
1705 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
1706 $this->assertEquals( array(), $status->errors
,
1707 "Locking of files succeeded ($backendName)." );
1708 $this->assertEquals( true, $status->isOK(),
1709 "Locking of files succeeded with OK status ($backendName)." );
1711 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
1712 $this->assertEquals( array(), $status->errors
,
1713 "Locking of files succeeded ($backendName)." );
1714 $this->assertEquals( true, $status->isOK(),
1715 "Locking of files succeeded with OK status ($backendName)." );
1717 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
1718 $this->assertEquals( array(), $status->errors
,
1719 "Locking of files succeeded ($backendName)." );
1720 $this->assertEquals( true, $status->isOK(),
1721 "Locking of files succeeded with OK status ($backendName)." );
1725 // test helper wrapper for backend prepare() function
1726 private function prepare( array $params ) {
1727 $this->dirsToPrune
[] = $params['dir'];
1728 return $this->backend
->prepare( $params );
1731 function tearDownFiles() {
1732 foreach ( $this->filesToPrune
as $file ) {
1735 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1736 foreach ( $containers as $container ) {
1737 $this->deleteFiles( $container );
1739 foreach ( $this->dirsToPrune
as $dir ) {
1740 $this->recursiveClean( $dir );
1742 $this->filesToPrune
= $this->dirsToPrune
= array();
1745 private function deleteFiles( $container ) {
1746 $base = $this->baseStorePath();
1747 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
1749 foreach ( $iter as $file ) {
1750 $this->backend
->delete( array( 'src' => "$base/$container/$file" ),
1751 array( 'force' => 1, 'nonLocking' => 1 ) );
1756 private function recursiveClean( $dir ) {
1757 $this->backend
->clean( array( 'dir' => $dir, 'recursive' => 1 ) );
1760 function assertGoodStatus( $status, $msg ) {
1761 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );
1764 function tearDown() {