7b04a40752849e591432f996974f42a3a2715b2f
7 class FileBackendTest
extends MediaWikiTestCase
{
8 private $backend, $multiBackend;
9 private $filesToPrune = array();
10 private $dirsToPrune = array();
11 private static $backendToUse;
14 global $wgFileBackends;
16 $tmpPrefix = wfTempDir() . '/filebackend-unittest-' . time() . '-' . mt_rand();
17 if ( $this->getCliArg( 'use-filebackend=' ) ) {
18 if ( self
::$backendToUse ) {
19 $this->singleBackend
= self
::$backendToUse;
21 $name = $this->getCliArg( 'use-filebackend=' );
23 foreach ( $wgFileBackends as $conf ) {
24 if ( $conf['name'] == $name ) {
28 $useConfig['name'] = 'localtesting'; // swap name
29 $class = $conf['class'];
30 self
::$backendToUse = new $class( $useConfig );
31 $this->singleBackend
= self
::$backendToUse;
34 $this->singleBackend
= new FSFileBackend( array(
35 'name' => 'localtesting',
36 'lockManager' => 'fsLockManager',
37 'containerPaths' => array(
38 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
39 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
42 $this->multiBackend
= new FileBackendMultiWrite( array(
43 'name' => 'localtesting',
44 'lockManager' => 'fsLockManager',
47 'name' => 'localmutlitesting1',
48 'class' => 'FSFileBackend',
49 'lockManager' => 'nullLockManager',
50 'containerPaths' => array(
51 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
52 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
53 'isMultiMaster' => false
56 'name' => 'localmutlitesting2',
57 'class' => 'FSFileBackend',
58 'lockManager' => 'nullLockManager',
59 'containerPaths' => array(
60 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
61 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
62 'isMultiMaster' => true
66 $this->filesToPrune
= array();
69 private function baseStorePath() {
70 return 'mwstore://localtesting';
73 private function backendClass() {
74 return get_class( $this->backend
);
78 * @dataProvider provider_testIsStoragePath
80 public function testIsStoragePath( $path, $isStorePath ) {
81 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
82 "FileBackend::isStoragePath on path '$path'" );
85 function provider_testIsStoragePath() {
87 array( 'mwstore://', true ),
88 array( 'mwstore://backend', true ),
89 array( 'mwstore://backend/container', true ),
90 array( 'mwstore://backend/container/', true ),
91 array( 'mwstore://backend/container/path', true ),
92 array( 'mwstore://backend//container/', true ),
93 array( 'mwstore://backend//container//', true ),
94 array( 'mwstore://backend//container//path', true ),
95 array( 'mwstore:///', true ),
96 array( 'mwstore:/', false ),
97 array( 'mwstore:', false ),
102 * @dataProvider provider_testSplitStoragePath
104 public function testSplitStoragePath( $path, $res ) {
105 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
106 "FileBackend::splitStoragePath on path '$path'" );
109 function provider_testSplitStoragePath() {
111 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
112 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
113 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
114 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
115 array( 'mwstore://backend//container/path', array( null, null, null ) ),
116 array( 'mwstore://backend//container//path', array( null, null, null ) ),
117 array( 'mwstore://', array( null, null, null ) ),
118 array( 'mwstore://backend', array( null, null, null ) ),
119 array( 'mwstore:///', array( null, null, null ) ),
120 array( 'mwstore:/', array( null, null, null ) ),
121 array( 'mwstore:', array( null, null, null ) )
126 * @dataProvider provider_normalizeStoragePath
128 public function testNormalizeStoragePath( $path, $res ) {
129 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
130 "FileBackend::normalizeStoragePath on path '$path'" );
133 function provider_normalizeStoragePath() {
135 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
136 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
137 array( 'mwstore://backend/container/path', 'mwstore://backend/container/path' ),
138 array( 'mwstore://backend/container//path', 'mwstore://backend/container/path' ),
139 array( 'mwstore://backend/container///path', 'mwstore://backend/container/path' ),
140 array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj',
141 array( 'mwstore://', null ),
142 array( 'mwstore://backend', null ),
143 array( 'mwstore://backend//container/path', null ),
144 array( 'mwstore://backend//container//path', null ),
145 array( 'mwstore:///', null ),
146 array( 'mwstore:/', null ),
147 array( 'mwstore:', null ), )
152 * @dataProvider provider_testParentStoragePath
154 public function testParentStoragePath( $path, $res ) {
155 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
156 "FileBackend::parentStoragePath on path '$path'" );
159 function provider_testParentStoragePath() {
161 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
162 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
163 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
164 array( 'mwstore://backend/container', null ),
165 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
166 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
167 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
168 array( 'mwstore://backend/container/', null ),
173 * @dataProvider provider_testExtensionFromPath
175 public function testExtensionFromPath( $path, $res ) {
176 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
177 "FileBackend::extensionFromPath on path '$path'" );
180 function provider_testExtensionFromPath() {
182 array( 'mwstore://backend/container/path.txt', 'txt' ),
183 array( 'mwstore://backend/container/path.svg.png', 'png' ),
184 array( 'mwstore://backend/container/path', '' ),
185 array( 'mwstore://backend/container/path.', '' ),
190 * @dataProvider provider_testStore
192 public function testStore( $op ) {
193 $this->filesToPrune
[] = $op['src'];
195 $this->backend
= $this->singleBackend
;
196 $this->tearDownFiles();
197 $this->doTestStore( $op );
198 $this->tearDownFiles();
200 $this->backend
= $this->multiBackend
;
201 $this->tearDownFiles();
202 $this->doTestStore( $op );
203 $this->tearDownFiles();
206 function doTestStore( $op ) {
207 $backendName = $this->backendClass();
209 $source = $op['src'];
211 $this->prepare( array( 'dir' => dirname( $dest ) ) );
213 file_put_contents( $source, "Unit test file" );
215 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
216 $this->backend
->store( $op );
219 $status = $this->backend
->doOperation( $op );
221 $this->assertEquals( array(), $status->errors
,
222 "Store from $source to $dest succeeded without warnings ($backendName)." );
223 $this->assertEquals( array(), $status->errors
,
224 "Store from $source to $dest succeeded ($backendName)." );
225 $this->assertEquals( array( 0 => true ), $status->success
,
226 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
227 $this->assertEquals( true, file_exists( $source ),
228 "Source file $source still exists ($backendName)." );
229 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
230 "Destination file $dest exists ($backendName)." );
232 $this->assertEquals( filesize( $source ),
233 $this->backend
->getFileSize( array( 'src' => $dest ) ),
234 "Destination file $dest has correct size ($backendName)." );
236 $props1 = FSFile
::getPropsFromPath( $source );
237 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
238 $this->assertEquals( $props1, $props2,
239 "Source and destination have the same props ($backendName)." );
242 public function provider_testStore() {
245 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
246 $toPath = $this->baseStorePath() . '/unittest-cont1/fun/obj1.txt';
247 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
255 $op2['overwrite'] = true;
263 $op2['overwriteSame'] = true;
274 * @dataProvider provider_testCopy
276 public function testCopy( $op ) {
277 $this->backend
= $this->singleBackend
;
278 $this->tearDownFiles();
279 $this->doTestCopy( $op );
280 $this->tearDownFiles();
282 $this->backend
= $this->multiBackend
;
283 $this->tearDownFiles();
284 $this->doTestCopy( $op );
285 $this->tearDownFiles();
288 function doTestCopy( $op ) {
289 $backendName = $this->backendClass();
291 $source = $op['src'];
293 $this->prepare( array( 'dir' => dirname( $source ) ) );
294 $this->prepare( array( 'dir' => dirname( $dest ) ) );
296 $status = $this->backend
->doOperation(
297 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
298 $this->assertEquals( array(), $status->errors
,
299 "Creation of file at $source succeeded ($backendName)." );
301 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
302 $this->backend
->copy( $op );
305 $status = $this->backend
->doOperation( $op );
307 $this->assertEquals( array(), $status->errors
,
308 "Copy from $source to $dest succeeded without warnings ($backendName)." );
309 $this->assertEquals( true, $status->isOK(),
310 "Copy from $source to $dest succeeded ($backendName)." );
311 $this->assertEquals( array( 0 => true ), $status->success
,
312 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
313 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
314 "Source file $source still exists ($backendName)." );
315 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
316 "Destination file $dest exists after copy ($backendName)." );
319 $this->backend
->getFileSize( array( 'src' => $source ) ),
320 $this->backend
->getFileSize( array( 'src' => $dest ) ),
321 "Destination file $dest has correct size ($backendName)." );
323 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
324 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
325 $this->assertEquals( $props1, $props2,
326 "Source and destination have the same props ($backendName)." );
329 public function provider_testCopy() {
332 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
333 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
335 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
343 $op2['overwrite'] = true;
351 $op2['overwriteSame'] = true;
362 * @dataProvider provider_testMove
364 public function testMove( $op ) {
365 $this->backend
= $this->singleBackend
;
366 $this->tearDownFiles();
367 $this->doTestMove( $op );
368 $this->tearDownFiles();
370 $this->backend
= $this->multiBackend
;
371 $this->tearDownFiles();
372 $this->doTestMove( $op );
373 $this->tearDownFiles();
376 private function doTestMove( $op ) {
377 $backendName = $this->backendClass();
379 $source = $op['src'];
381 $this->prepare( array( 'dir' => dirname( $source ) ) );
382 $this->prepare( array( 'dir' => dirname( $dest ) ) );
384 $status = $this->backend
->doOperation(
385 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
386 $this->assertEquals( array(), $status->errors
,
387 "Creation of file at $source succeeded ($backendName)." );
389 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
390 $this->backend
->copy( $op );
393 $status = $this->backend
->doOperation( $op );
394 $this->assertEquals( array(), $status->errors
,
395 "Move from $source to $dest succeeded without warnings ($backendName)." );
396 $this->assertEquals( true, $status->isOK(),
397 "Move from $source to $dest succeeded ($backendName)." );
398 $this->assertEquals( array( 0 => true ), $status->success
,
399 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
400 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
401 "Source file $source does not still exists ($backendName)." );
402 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
403 "Destination file $dest exists after move ($backendName)." );
405 $this->assertNotEquals(
406 $this->backend
->getFileSize( array( 'src' => $source ) ),
407 $this->backend
->getFileSize( array( 'src' => $dest ) ),
408 "Destination file $dest has correct size ($backendName)." );
410 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
411 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
412 $this->assertEquals( false, $props1['fileExists'],
413 "Source file does not exist accourding to props ($backendName)." );
414 $this->assertEquals( true, $props2['fileExists'],
415 "Destination file exists accourding to props ($backendName)." );
418 public function provider_testMove() {
421 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
422 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
424 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
432 $op2['overwrite'] = true;
440 $op2['overwriteSame'] = true;
451 * @dataProvider provider_testDelete
453 public function testDelete( $op, $withSource, $okStatus ) {
454 $this->backend
= $this->singleBackend
;
455 $this->tearDownFiles();
456 $this->doTestDelete( $op, $withSource, $okStatus );
457 $this->tearDownFiles();
459 $this->backend
= $this->multiBackend
;
460 $this->tearDownFiles();
461 $this->doTestDelete( $op, $withSource, $okStatus );
462 $this->tearDownFiles();
465 private function doTestDelete( $op, $withSource, $okStatus ) {
466 $backendName = $this->backendClass();
468 $source = $op['src'];
469 $this->prepare( array( 'dir' => dirname( $source ) ) );
472 $status = $this->backend
->doOperation(
473 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
474 $this->assertEquals( array(), $status->errors
,
475 "Creation of file at $source succeeded ($backendName)." );
478 $status = $this->backend
->doOperation( $op );
480 $this->assertEquals( array(), $status->errors
,
481 "Deletion of file at $source succeeded without warnings ($backendName)." );
482 $this->assertEquals( true, $status->isOK(),
483 "Deletion of file at $source succeeded ($backendName)." );
484 $this->assertEquals( array( 0 => true ), $status->success
,
485 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
487 $this->assertEquals( false, $status->isOK(),
488 "Deletion of file at $source failed ($backendName)." );
491 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
492 "Source file $source does not exist after move ($backendName)." );
495 $this->backend
->getFileSize( array( 'src' => $source ) ),
496 "Source file $source has correct size (false) ($backendName)." );
498 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
499 $this->assertFalse( $props1['fileExists'],
500 "Source file $source does not exist according to props ($backendName)." );
503 public function provider_testDelete() {
506 $source = $this->baseStorePath() . '/unittest-cont1/myfacefile.txt';
508 $op = array( 'op' => 'delete', 'src' => $source );
517 false, // without source
521 $op['ignoreMissingSource'] = true;
524 false, // without source
532 * @dataProvider provider_testCreate
534 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
535 $this->backend
= $this->singleBackend
;
536 $this->tearDownFiles();
537 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
538 $this->tearDownFiles();
540 $this->backend
= $this->multiBackend
;
541 $this->tearDownFiles();
542 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
543 $this->tearDownFiles();
546 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
547 $backendName = $this->backendClass();
550 $this->prepare( array( 'dir' => dirname( $dest ) ) );
552 $oldText = 'blah...blah...waahwaah';
553 if ( $alreadyExists ) {
554 $status = $this->backend
->doOperation(
555 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
556 $this->assertEquals( array(), $status->errors
,
557 "Creation of file at $dest succeeded ($backendName)." );
560 $status = $this->backend
->doOperation( $op );
562 $this->assertEquals( array(), $status->errors
,
563 "Creation of file at $dest succeeded without warnings ($backendName)." );
564 $this->assertEquals( true, $status->isOK(),
565 "Creation of file at $dest succeeded ($backendName)." );
566 $this->assertEquals( array( 0 => true ), $status->success
,
567 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
569 $this->assertEquals( false, $status->isOK(),
570 "Creation of file at $dest failed ($backendName)." );
573 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
574 "Destination file $dest exists after creation ($backendName)." );
576 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
577 $this->assertEquals( true, $props1['fileExists'],
578 "Destination file $dest exists according to props ($backendName)." );
579 if ( $okStatus ) { // file content is what we saved
580 $this->assertEquals( $newSize, $props1['size'],
581 "Destination file $dest has expected size according to props ($backendName)." );
582 $this->assertEquals( $newSize,
583 $this->backend
->getFileSize( array( 'src' => $dest ) ),
584 "Destination file $dest has correct size ($backendName)." );
585 } else { // file content is some other previous text
586 $this->assertEquals( strlen( $oldText ), $props1['size'],
587 "Destination file $dest has original size according to props ($backendName)." );
588 $this->assertEquals( strlen( $oldText ),
589 $this->backend
->getFileSize( array( 'src' => $dest ) ),
590 "Destination file $dest has original size according to props ($backendName)." );
595 * @dataProvider provider_testCreate
597 public function provider_testCreate() {
600 $dest = $this->baseStorePath() . '/unittest-cont2/myspacefile.txt';
602 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
605 false, // no dest already exists
607 strlen( $op['content'] )
611 $op2['content'] = "\n";
614 false, // no dest already exists
616 strlen( $op2['content'] )
620 $op2['content'] = "fsf\n waf 3kt";
623 true, // dest already exists
625 strlen( $op2['content'] )
629 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
630 $op2['overwrite'] = true;
633 true, // dest already exists
635 strlen( $op2['content'] )
639 $op2['content'] = "39qjmg3-qg";
640 $op2['overwriteSame'] = true;
643 true, // dest already exists
645 strlen( $op2['content'] )
652 * @dataProvider provider_testConcatenate
654 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
655 $this->filesToPrune
[] = $op['dst'];
657 $this->backend
= $this->singleBackend
;
658 $this->tearDownFiles();
659 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
660 $this->tearDownFiles();
662 $this->backend
= $this->multiBackend
;
663 $this->tearDownFiles();
664 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
665 $this->tearDownFiles();
668 public function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
669 $backendName = $this->backendClass();
674 foreach ( $srcs as $i => $source ) {
675 $this->prepare( array( 'dir' => dirname( $source ) ) );
677 'op' => 'create', // operation
678 'dst' => $source, // source
679 'content' => $srcsContent[$i]
681 $expContent .= $srcsContent[$i];
683 $status = $this->backend
->doOperations( $ops );
685 $this->assertEquals( array(), $status->errors
,
686 "Creation of source files succeeded ($backendName)." );
688 $dest = $params['dst'];
689 if ( $alreadyExists ) {
690 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
691 $this->assertEquals( true, $ok,
692 "Creation of file at $dest succeeded ($backendName)." );
694 $ok = file_put_contents( $dest, '' ) !== false;
695 $this->assertEquals( true, $ok,
696 "Creation of 0-byte file at $dest succeeded ($backendName)." );
699 // Combine the files into one
700 $status = $this->backend
->concatenate( $params );
702 $this->assertEquals( array(), $status->errors
,
703 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
704 $this->assertEquals( true, $status->isOK(),
705 "Creation of concat file at $dest succeeded ($backendName)." );
707 $this->assertEquals( false, $status->isOK(),
708 "Creation of concat file at $dest failed ($backendName)." );
712 $this->assertEquals( true, is_file( $dest ),
713 "Dest concat file $dest exists after creation ($backendName)." );
715 $this->assertEquals( true, is_file( $dest ),
716 "Dest concat file $dest exists after failed creation ($backendName)." );
719 $contents = file_get_contents( $dest );
720 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
723 $this->assertEquals( $expContent, $contents,
724 "Concat file at $dest has correct contents ($backendName)." );
726 $this->assertNotEquals( $expContent, $contents,
727 "Concat file at $dest has correct contents ($backendName)." );
731 function provider_testConcatenate() {
734 $rand = mt_rand( 0, 2000000000 ) . time();
735 $dest = wfTempDir() . "/randomfile!$rand.txt";
737 $this->baseStorePath() . '/unittest-cont1/file1.txt',
738 $this->baseStorePath() . '/unittest-cont1/file2.txt',
739 $this->baseStorePath() . '/unittest-cont1/file3.txt',
740 $this->baseStorePath() . '/unittest-cont1/file4.txt',
741 $this->baseStorePath() . '/unittest-cont1/file5.txt',
742 $this->baseStorePath() . '/unittest-cont1/file6.txt',
743 $this->baseStorePath() . '/unittest-cont1/file7.txt',
744 $this->baseStorePath() . '/unittest-cont1/file8.txt',
745 $this->baseStorePath() . '/unittest-cont1/file9.txt',
746 $this->baseStorePath() . '/unittest-cont1/file10.txt'
760 $params = array( 'srcs' => $srcs, 'dst' => $dest );
763 $params, // operation
765 $content, // content for each source
766 false, // no dest already exists
771 $params, // operation
773 $content, // content for each source
774 true, // dest already exists
782 * @dataProvider provider_testGetFileStat
784 public function testGetFileStat( $path, $content, $alreadyExists ) {
785 $this->backend
= $this->singleBackend
;
786 $this->tearDownFiles();
787 $this->doTestGetFileStat( $path, $content, $alreadyExists );
788 $this->tearDownFiles();
790 $this->backend
= $this->multiBackend
;
791 $this->tearDownFiles();
792 $this->doTestGetFileStat( $path, $content, $alreadyExists );
793 $this->tearDownFiles();
796 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
797 $backendName = $this->backendClass();
799 if ( $alreadyExists ) {
800 $this->prepare( array( 'dir' => dirname( $path ) ) );
801 $status = $this->backend
->create( array( 'dst' => $path, 'content' => $content ) );
802 $this->assertEquals( array(), $status->errors
,
803 "Creation of file at $path succeeded ($backendName)." );
805 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
806 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
807 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
809 $this->assertEquals( strlen( $content ), $size,
810 "Correct file size of '$path'" );
811 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 5,
812 "Correct file timestamp of '$path'" );
814 $size = $stat['size'];
815 $time = $stat['mtime'];
816 $this->assertEquals( strlen( $content ), $size,
817 "Correct file size of '$path'" );
818 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 5,
819 "Correct file timestamp of '$path'" );
821 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
822 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
823 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
825 $this->assertFalse( $size, "Correct file size of '$path'" );
826 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
827 $this->assertFalse( $stat, "Correct file stat of '$path'" );
831 function provider_testGetFileStat() {
834 $base = $this->baseStorePath();
835 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents", true );
836 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "", true );
837 $cases[] = array( "$base/unittest-cont1/b/some-diff_file.txt", null, false );
843 * @dataProvider provider_testGetFileContents
845 public function testGetFileContents( $source, $content ) {
846 $this->backend
= $this->singleBackend
;
847 $this->tearDownFiles();
848 $this->doTestGetFileContents( $source, $content );
849 $this->tearDownFiles();
851 $this->backend
= $this->multiBackend
;
852 $this->tearDownFiles();
853 $this->doTestGetFileContents( $source, $content );
854 $this->tearDownFiles();
857 public function doTestGetFileContents( $source, $content ) {
858 $backendName = $this->backendClass();
860 $this->prepare( array( 'dir' => dirname( $source ) ) );
862 $status = $this->backend
->doOperation(
863 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
864 $this->assertEquals( array(), $status->errors
,
865 "Creation of file at $source succeeded ($backendName)." );
866 $this->assertEquals( true, $status->isOK(),
867 "Creation of file at $source succeeded with OK status ($backendName)." );
869 $newContents = $this->backend
->getFileContents( array( 'src' => $source, 'latest' => 1 ) );
870 $this->assertNotEquals( false, $newContents,
871 "Read of file at $source succeeded ($backendName)." );
873 $this->assertEquals( $content, $newContents,
874 "Contents read match data at $source ($backendName)." );
877 function provider_testGetFileContents() {
880 $base = $this->baseStorePath();
881 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents" );
882 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "more file contents" );
888 * @dataProvider provider_testGetLocalCopy
890 public function testGetLocalCopy( $source, $content ) {
891 $this->backend
= $this->singleBackend
;
892 $this->tearDownFiles();
893 $this->doTestGetLocalCopy( $source, $content );
894 $this->tearDownFiles();
896 $this->backend
= $this->multiBackend
;
897 $this->tearDownFiles();
898 $this->doTestGetLocalCopy( $source, $content );
899 $this->tearDownFiles();
902 public function doTestGetLocalCopy( $source, $content ) {
903 $backendName = $this->backendClass();
905 $this->prepare( array( 'dir' => dirname( $source ) ) );
907 $status = $this->backend
->doOperation(
908 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
909 $this->assertEquals( array(), $status->errors
,
910 "Creation of file at $source succeeded ($backendName)." );
912 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
913 $this->assertNotNull( $tmpFile,
914 "Creation of local copy of $source succeeded ($backendName)." );
916 $contents = file_get_contents( $tmpFile->getPath() );
917 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
920 function provider_testGetLocalCopy() {
923 $base = $this->baseStorePath();
924 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
925 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
931 * @dataProvider provider_testGetLocalReference
933 public function testGetLocalReference( $source, $content ) {
934 $this->backend
= $this->singleBackend
;
935 $this->tearDownFiles();
936 $this->doTestGetLocalReference( $source, $content );
937 $this->tearDownFiles();
939 $this->backend
= $this->multiBackend
;
940 $this->tearDownFiles();
941 $this->doTestGetLocalReference( $source, $content );
942 $this->tearDownFiles();
945 private function doTestGetLocalReference( $source, $content ) {
946 $backendName = $this->backendClass();
948 $this->prepare( array( 'dir' => dirname( $source ) ) );
950 $status = $this->backend
->doOperation(
951 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
952 $this->assertEquals( array(), $status->errors
,
953 "Creation of file at $source succeeded ($backendName)." );
955 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
956 $this->assertNotNull( $tmpFile,
957 "Creation of local copy of $source succeeded ($backendName)." );
959 $contents = file_get_contents( $tmpFile->getPath() );
960 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
963 function provider_testGetLocalReference() {
966 $base = $this->baseStorePath();
967 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
968 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
974 * @dataProvider provider_testPrepareAndClean
976 public function testPrepareAndClean( $path, $isOK ) {
977 $this->backend
= $this->singleBackend
;
978 $this->doTestPrepareAndClean( $path, $isOK );
979 $this->tearDownFiles();
981 $this->backend
= $this->multiBackend
;
982 $this->doTestPrepareAndClean( $path, $isOK );
983 $this->tearDownFiles();
986 function provider_testPrepareAndClean() {
987 $base = $this->baseStorePath();
989 array( "$base/unittest-cont1/a/z/some_file1.txt", true ),
990 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
991 # Specific to FS backend with no basePath field set
992 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
996 function doTestPrepareAndClean( $path, $isOK ) {
997 $backendName = $this->backendClass();
999 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1001 $this->assertEquals( array(), $status->errors
,
1002 "Preparing dir $path succeeded without warnings ($backendName)." );
1003 $this->assertEquals( true, $status->isOK(),
1004 "Preparing dir $path succeeded ($backendName)." );
1006 $this->assertEquals( false, $status->isOK(),
1007 "Preparing dir $path failed ($backendName)." );
1010 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1012 $this->assertEquals( array(), $status->errors
,
1013 "Cleaning dir $path succeeded without warnings ($backendName)." );
1014 $this->assertEquals( true, $status->isOK(),
1015 "Cleaning dir $path succeeded ($backendName)." );
1017 $this->assertEquals( false, $status->isOK(),
1018 "Cleaning dir $path failed ($backendName)." );
1022 // @TODO: testSecure
1024 public function testDoOperations() {
1025 $this->backend
= $this->singleBackend
;
1026 $this->tearDownFiles();
1027 $this->doTestDoOperations();
1028 $this->tearDownFiles();
1030 $this->backend
= $this->multiBackend
;
1031 $this->tearDownFiles();
1032 $this->doTestDoOperations();
1033 $this->tearDownFiles();
1035 // @TODO: test some cases where the ops should fail
1038 function doTestDoOperations() {
1039 $base = $this->baseStorePath();
1041 $fileA = "$base/unittest-cont1/a/b/fileA.txt";
1042 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1043 $fileB = "$base/unittest-cont1/a/b/fileB.txt";
1044 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1045 $fileC = "$base/unittest-cont1/a/b/fileC.txt";
1046 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1047 $fileD = "$base/unittest-cont1/a/b/fileD.txt";
1049 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1050 $this->backend
->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1051 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1052 $this->backend
->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1053 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1054 $this->backend
->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1056 $status = $this->backend
->doOperations( array(
1057 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1058 // Now: A:<A>, B:<B>, C:<A>, D:<D> (file:<orginal contents>)
1059 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1060 // Now: A:<A>, B:<B>, C:<A>, D:<D>
1061 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1062 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1063 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1064 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1065 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1066 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1067 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1068 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1069 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1070 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1071 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1072 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1073 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1075 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1077 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1079 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1081 array( 'op' => 'null' ),
1085 $this->assertEquals( array(), $status->errors
, "Operation batch succeeded" );
1086 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1087 $this->assertEquals( 13, count( $status->success
),
1088 "Operation batch has correct success array" );
1090 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1091 "File does not exist at $fileA" );
1092 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1093 "File does not exist at $fileB" );
1094 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1095 "File does not exist at $fileD" );
1097 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1098 "File exists at $fileC" );
1099 $this->assertEquals( $fileBContents,
1100 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1101 "Correct file contents of $fileC" );
1102 $this->assertEquals( strlen( $fileBContents ),
1103 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1104 "Correct file size of $fileC" );
1105 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1106 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1107 "Correct file SHA-1 of $fileC" );
1110 public function testGetFileList() {
1111 $this->backend
= $this->singleBackend
;
1112 $this->tearDownFiles();
1113 $this->doTestGetFileList();
1114 $this->tearDownFiles();
1116 $this->backend
= $this->multiBackend
;
1117 $this->tearDownFiles();
1118 $this->doTestGetFileList();
1119 $this->tearDownFiles();
1122 private function doTestGetFileList() {
1123 $backendName = $this->backendClass();
1125 $base = $this->baseStorePath();
1127 "$base/unittest-cont1/test1.txt",
1128 "$base/unittest-cont1/test2.txt",
1129 "$base/unittest-cont1/test3.txt",
1130 "$base/unittest-cont1/subdir1/test1.txt",
1131 "$base/unittest-cont1/subdir1/test2.txt",
1132 "$base/unittest-cont1/subdir2/test3.txt",
1133 "$base/unittest-cont1/subdir2/test4.txt",
1134 "$base/unittest-cont1/subdir2/subdir/test1.txt",
1135 "$base/unittest-cont1/subdir2/subdir/test2.txt",
1136 "$base/unittest-cont1/subdir2/subdir/test3.txt",
1137 "$base/unittest-cont1/subdir2/subdir/test4.txt",
1138 "$base/unittest-cont1/subdir2/subdir/test5.txt",
1139 "$base/unittest-cont1/subdir2/subdir/sub/test0.txt",
1140 "$base/unittest-cont1/subdir2/subdir/sub/120-px-file.txt",
1145 foreach ( $files as $file ) {
1146 $this->prepare( array( 'dir' => dirname( $file ) ) );
1147 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1149 $status = $this->backend
->doOperations( $ops );
1150 $this->assertEquals( array(), $status->errors
,
1151 "Creation of files succeeded ($backendName)." );
1152 $this->assertEquals( true, $status->isOK(),
1153 "Creation of files succeeded with OK status ($backendName)." );
1160 "subdir1/test1.txt",
1161 "subdir1/test2.txt",
1162 "subdir2/test3.txt",
1163 "subdir2/test4.txt",
1164 "subdir2/subdir/test1.txt",
1165 "subdir2/subdir/test2.txt",
1166 "subdir2/subdir/test3.txt",
1167 "subdir2/subdir/test4.txt",
1168 "subdir2/subdir/test5.txt",
1169 "subdir2/subdir/sub/test0.txt",
1170 "subdir2/subdir/sub/120-px-file.txt",
1174 // Actual listing (no trailing slash)
1176 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1177 foreach ( $iter as $file ) {
1182 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1184 // Actual listing (with trailing slash)
1186 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1187 foreach ( $iter as $file ) {
1192 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1202 "sub/120-px-file.txt",
1206 // Actual listing (no trailing slash)
1208 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) );
1209 foreach ( $iter as $file ) {
1214 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1216 // Actual listing (with trailing slash)
1218 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir/" ) );
1219 foreach ( $iter as $file ) {
1224 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1226 // Actual listing (using iterator second time)
1228 foreach ( $iter as $file ) {
1233 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1235 foreach ( $files as $file ) { // clean up
1236 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1239 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1240 foreach ( $iter as $iter ) {} // no errors
1243 // test helper wrapper for backend prepare() function
1244 private function prepare( array $params ) {
1245 $this->dirsToPrune
[] = $params['dir'];
1246 return $this->backend
->prepare( $params );
1249 function tearDownFiles() {
1250 foreach ( $this->filesToPrune
as $file ) {
1253 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1254 foreach ( $containers as $container ) {
1255 $this->deleteFiles( $container );
1257 foreach ( $this->dirsToPrune
as $dir ) {
1258 $this->recursiveClean( $dir );
1260 $this->filesToPrune
= $this->dirsToPrune
= array();
1263 private function deleteFiles( $container ) {
1264 $base = $this->baseStorePath();
1265 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
1267 foreach ( $iter as $file ) {
1268 $this->backend
->delete( array( 'src' => "$base/$container/$file" ), array( 'force' => 1 ) );
1273 private function recursiveClean( $dir ) {
1275 if ( !$this->backend
->clean( array( 'dir' => $dir ) )->isOK() ) {
1278 } while ( $dir = FileBackend
::parentStoragePath( $dir ) );
1281 function tearDown() {