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 ) {
29 $useConfig['name'] = 'localtesting'; // swap name
30 $class = $useConfig['class'];
31 self
::$backendToUse = new $class( $useConfig );
32 $this->singleBackend
= self
::$backendToUse;
35 $this->singleBackend
= new FSFileBackend( array(
36 'name' => 'localtesting',
37 'lockManager' => 'fsLockManager',
38 'containerPaths' => array(
39 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
40 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
43 $this->multiBackend
= new FileBackendMultiWrite( array(
44 'name' => 'localtesting',
45 'lockManager' => 'fsLockManager',
48 'name' => 'localmutlitesting1',
49 'class' => 'FSFileBackend',
50 'lockManager' => 'nullLockManager',
51 'containerPaths' => array(
52 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
53 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
54 'isMultiMaster' => false
57 'name' => 'localmutlitesting2',
58 'class' => 'FSFileBackend',
59 'lockManager' => 'nullLockManager',
60 'containerPaths' => array(
61 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
62 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
63 'isMultiMaster' => true
67 $this->filesToPrune
= array();
70 private function baseStorePath() {
71 return 'mwstore://localtesting';
74 private function backendClass() {
75 return get_class( $this->backend
);
79 * @dataProvider provider_testIsStoragePath
81 public function testIsStoragePath( $path, $isStorePath ) {
82 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
83 "FileBackend::isStoragePath on path '$path'" );
86 function provider_testIsStoragePath() {
88 array( 'mwstore://', true ),
89 array( 'mwstore://backend', true ),
90 array( 'mwstore://backend/container', true ),
91 array( 'mwstore://backend/container/', true ),
92 array( 'mwstore://backend/container/path', true ),
93 array( 'mwstore://backend//container/', true ),
94 array( 'mwstore://backend//container//', true ),
95 array( 'mwstore://backend//container//path', true ),
96 array( 'mwstore:///', true ),
97 array( 'mwstore:/', false ),
98 array( 'mwstore:', false ),
103 * @dataProvider provider_testSplitStoragePath
105 public function testSplitStoragePath( $path, $res ) {
106 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
107 "FileBackend::splitStoragePath on path '$path'" );
110 function provider_testSplitStoragePath() {
112 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
113 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
114 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
115 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
116 array( 'mwstore://backend//container/path', array( null, null, null ) ),
117 array( 'mwstore://backend//container//path', array( null, null, null ) ),
118 array( 'mwstore://', array( null, null, null ) ),
119 array( 'mwstore://backend', array( null, null, null ) ),
120 array( 'mwstore:///', array( null, null, null ) ),
121 array( 'mwstore:/', array( null, null, null ) ),
122 array( 'mwstore:', array( null, null, null ) )
127 * @dataProvider provider_normalizeStoragePath
129 public function testNormalizeStoragePath( $path, $res ) {
130 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
131 "FileBackend::normalizeStoragePath on path '$path'" );
134 function provider_normalizeStoragePath() {
136 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
137 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
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', 'mwstore://backend/container/path' ),
141 array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj',
142 array( 'mwstore://', null ),
143 array( 'mwstore://backend', null ),
144 array( 'mwstore://backend//container/path', null ),
145 array( 'mwstore://backend//container//path', null ),
146 array( 'mwstore:///', null ),
147 array( 'mwstore:/', null ),
148 array( 'mwstore:', null ), )
153 * @dataProvider provider_testParentStoragePath
155 public function testParentStoragePath( $path, $res ) {
156 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
157 "FileBackend::parentStoragePath on path '$path'" );
160 function provider_testParentStoragePath() {
162 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
163 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
164 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
165 array( 'mwstore://backend/container', null ),
166 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
167 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
168 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
169 array( 'mwstore://backend/container/', null ),
174 * @dataProvider provider_testExtensionFromPath
176 public function testExtensionFromPath( $path, $res ) {
177 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
178 "FileBackend::extensionFromPath on path '$path'" );
181 function provider_testExtensionFromPath() {
183 array( 'mwstore://backend/container/path.txt', 'txt' ),
184 array( 'mwstore://backend/container/path.svg.png', 'png' ),
185 array( 'mwstore://backend/container/path', '' ),
186 array( 'mwstore://backend/container/path.', '' ),
191 * @dataProvider provider_testStore
193 public function testStore( $op ) {
194 $this->filesToPrune
[] = $op['src'];
196 $this->backend
= $this->singleBackend
;
197 $this->tearDownFiles();
198 $this->doTestStore( $op );
199 $this->tearDownFiles();
201 $this->backend
= $this->multiBackend
;
202 $this->tearDownFiles();
203 $this->doTestStore( $op );
204 $this->filesToPrune
[] = $op['src']; # avoid file leaking
205 $this->tearDownFiles();
208 private function doTestStore( $op ) {
209 $backendName = $this->backendClass();
211 $source = $op['src'];
213 $this->prepare( array( 'dir' => dirname( $dest ) ) );
215 file_put_contents( $source, "Unit test file" );
217 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
218 $this->backend
->store( $op );
221 $status = $this->backend
->doOperation( $op );
223 $this->assertEquals( array(), $status->errors
,
224 "Store from $source to $dest succeeded without warnings ($backendName)." );
225 $this->assertEquals( array(), $status->errors
,
226 "Store from $source to $dest succeeded ($backendName)." );
227 $this->assertEquals( array( 0 => true ), $status->success
,
228 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
229 $this->assertEquals( true, file_exists( $source ),
230 "Source file $source still exists ($backendName)." );
231 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
232 "Destination file $dest exists ($backendName)." );
234 $this->assertEquals( filesize( $source ),
235 $this->backend
->getFileSize( array( 'src' => $dest ) ),
236 "Destination file $dest has correct size ($backendName)." );
238 $props1 = FSFile
::getPropsFromPath( $source );
239 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
240 $this->assertEquals( $props1, $props2,
241 "Source and destination have the same props ($backendName)." );
244 public function provider_testStore() {
247 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
248 $toPath = $this->baseStorePath() . '/unittest-cont1/fun/obj1.txt';
249 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
257 $op2['overwrite'] = true;
265 $op2['overwriteSame'] = true;
276 * @dataProvider provider_testCopy
278 public function testCopy( $op ) {
279 $this->backend
= $this->singleBackend
;
280 $this->tearDownFiles();
281 $this->doTestCopy( $op );
282 $this->tearDownFiles();
284 $this->backend
= $this->multiBackend
;
285 $this->tearDownFiles();
286 $this->doTestCopy( $op );
287 $this->tearDownFiles();
290 private function doTestCopy( $op ) {
291 $backendName = $this->backendClass();
293 $source = $op['src'];
295 $this->prepare( array( 'dir' => dirname( $source ) ) );
296 $this->prepare( array( 'dir' => dirname( $dest ) ) );
298 $status = $this->backend
->doOperation(
299 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
300 $this->assertEquals( array(), $status->errors
,
301 "Creation of file at $source succeeded ($backendName)." );
303 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
304 $this->backend
->copy( $op );
307 $status = $this->backend
->doOperation( $op );
309 $this->assertEquals( array(), $status->errors
,
310 "Copy from $source to $dest succeeded without warnings ($backendName)." );
311 $this->assertEquals( true, $status->isOK(),
312 "Copy from $source to $dest succeeded ($backendName)." );
313 $this->assertEquals( array( 0 => true ), $status->success
,
314 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
315 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
316 "Source file $source still exists ($backendName)." );
317 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
318 "Destination file $dest exists after copy ($backendName)." );
321 $this->backend
->getFileSize( array( 'src' => $source ) ),
322 $this->backend
->getFileSize( array( 'src' => $dest ) ),
323 "Destination file $dest has correct size ($backendName)." );
325 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
326 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
327 $this->assertEquals( $props1, $props2,
328 "Source and destination have the same props ($backendName)." );
331 public function provider_testCopy() {
334 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
335 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
337 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
345 $op2['overwrite'] = true;
353 $op2['overwriteSame'] = true;
364 * @dataProvider provider_testMove
366 public function testMove( $op ) {
367 $this->backend
= $this->singleBackend
;
368 $this->tearDownFiles();
369 $this->doTestMove( $op );
370 $this->tearDownFiles();
372 $this->backend
= $this->multiBackend
;
373 $this->tearDownFiles();
374 $this->doTestMove( $op );
375 $this->tearDownFiles();
378 private function doTestMove( $op ) {
379 $backendName = $this->backendClass();
381 $source = $op['src'];
383 $this->prepare( array( 'dir' => dirname( $source ) ) );
384 $this->prepare( array( 'dir' => dirname( $dest ) ) );
386 $status = $this->backend
->doOperation(
387 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
388 $this->assertEquals( array(), $status->errors
,
389 "Creation of file at $source succeeded ($backendName)." );
391 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
392 $this->backend
->copy( $op );
395 $status = $this->backend
->doOperation( $op );
396 $this->assertEquals( array(), $status->errors
,
397 "Move from $source to $dest succeeded without warnings ($backendName)." );
398 $this->assertEquals( true, $status->isOK(),
399 "Move from $source to $dest succeeded ($backendName)." );
400 $this->assertEquals( array( 0 => true ), $status->success
,
401 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
402 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
403 "Source file $source does not still exists ($backendName)." );
404 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
405 "Destination file $dest exists after move ($backendName)." );
407 $this->assertNotEquals(
408 $this->backend
->getFileSize( array( 'src' => $source ) ),
409 $this->backend
->getFileSize( array( 'src' => $dest ) ),
410 "Destination file $dest has correct size ($backendName)." );
412 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
413 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
414 $this->assertEquals( false, $props1['fileExists'],
415 "Source file does not exist accourding to props ($backendName)." );
416 $this->assertEquals( true, $props2['fileExists'],
417 "Destination file exists accourding to props ($backendName)." );
420 public function provider_testMove() {
423 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
424 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
426 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
434 $op2['overwrite'] = true;
442 $op2['overwriteSame'] = true;
453 * @dataProvider provider_testDelete
455 public function testDelete( $op, $withSource, $okStatus ) {
456 $this->backend
= $this->singleBackend
;
457 $this->tearDownFiles();
458 $this->doTestDelete( $op, $withSource, $okStatus );
459 $this->tearDownFiles();
461 $this->backend
= $this->multiBackend
;
462 $this->tearDownFiles();
463 $this->doTestDelete( $op, $withSource, $okStatus );
464 $this->tearDownFiles();
467 private function doTestDelete( $op, $withSource, $okStatus ) {
468 $backendName = $this->backendClass();
470 $source = $op['src'];
471 $this->prepare( array( 'dir' => dirname( $source ) ) );
474 $status = $this->backend
->doOperation(
475 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
476 $this->assertEquals( array(), $status->errors
,
477 "Creation of file at $source succeeded ($backendName)." );
480 $status = $this->backend
->doOperation( $op );
482 $this->assertEquals( array(), $status->errors
,
483 "Deletion of file at $source succeeded without warnings ($backendName)." );
484 $this->assertEquals( true, $status->isOK(),
485 "Deletion of file at $source succeeded ($backendName)." );
486 $this->assertEquals( array( 0 => true ), $status->success
,
487 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
489 $this->assertEquals( false, $status->isOK(),
490 "Deletion of file at $source failed ($backendName)." );
493 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
494 "Source file $source does not exist after move ($backendName)." );
497 $this->backend
->getFileSize( array( 'src' => $source ) ),
498 "Source file $source has correct size (false) ($backendName)." );
500 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
501 $this->assertFalse( $props1['fileExists'],
502 "Source file $source does not exist according to props ($backendName)." );
505 public function provider_testDelete() {
508 $source = $this->baseStorePath() . '/unittest-cont1/myfacefile.txt';
510 $op = array( 'op' => 'delete', 'src' => $source );
519 false, // without source
523 $op['ignoreMissingSource'] = true;
526 false, // without source
534 * @dataProvider provider_testCreate
536 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
537 $this->backend
= $this->singleBackend
;
538 $this->tearDownFiles();
539 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
540 $this->tearDownFiles();
542 $this->backend
= $this->multiBackend
;
543 $this->tearDownFiles();
544 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
545 $this->tearDownFiles();
548 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
549 $backendName = $this->backendClass();
552 $this->prepare( array( 'dir' => dirname( $dest ) ) );
554 $oldText = 'blah...blah...waahwaah';
555 if ( $alreadyExists ) {
556 $status = $this->backend
->doOperation(
557 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
558 $this->assertEquals( array(), $status->errors
,
559 "Creation of file at $dest succeeded ($backendName)." );
562 $status = $this->backend
->doOperation( $op );
564 $this->assertEquals( array(), $status->errors
,
565 "Creation of file at $dest succeeded without warnings ($backendName)." );
566 $this->assertEquals( true, $status->isOK(),
567 "Creation of file at $dest succeeded ($backendName)." );
568 $this->assertEquals( array( 0 => true ), $status->success
,
569 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
571 $this->assertEquals( false, $status->isOK(),
572 "Creation of file at $dest failed ($backendName)." );
575 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
576 "Destination file $dest exists after creation ($backendName)." );
578 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
579 $this->assertEquals( true, $props1['fileExists'],
580 "Destination file $dest exists according to props ($backendName)." );
581 if ( $okStatus ) { // file content is what we saved
582 $this->assertEquals( $newSize, $props1['size'],
583 "Destination file $dest has expected size according to props ($backendName)." );
584 $this->assertEquals( $newSize,
585 $this->backend
->getFileSize( array( 'src' => $dest ) ),
586 "Destination file $dest has correct size ($backendName)." );
587 } else { // file content is some other previous text
588 $this->assertEquals( strlen( $oldText ), $props1['size'],
589 "Destination file $dest has original size according to props ($backendName)." );
590 $this->assertEquals( strlen( $oldText ),
591 $this->backend
->getFileSize( array( 'src' => $dest ) ),
592 "Destination file $dest has original size according to props ($backendName)." );
597 * @dataProvider provider_testCreate
599 public function provider_testCreate() {
602 $dest = $this->baseStorePath() . '/unittest-cont2/myspacefile.txt';
604 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
607 false, // no dest already exists
609 strlen( $op['content'] )
613 $op2['content'] = "\n";
616 false, // no dest already exists
618 strlen( $op2['content'] )
622 $op2['content'] = "fsf\n waf 3kt";
625 true, // dest already exists
627 strlen( $op2['content'] )
631 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
632 $op2['overwrite'] = true;
635 true, // dest already exists
637 strlen( $op2['content'] )
641 $op2['content'] = "39qjmg3-qg";
642 $op2['overwriteSame'] = true;
645 true, // dest already exists
647 strlen( $op2['content'] )
654 * @dataProvider provider_testConcatenate
656 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
657 $this->filesToPrune
[] = $op['dst'];
659 $this->backend
= $this->singleBackend
;
660 $this->tearDownFiles();
661 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
662 $this->tearDownFiles();
664 $this->backend
= $this->multiBackend
;
665 $this->tearDownFiles();
666 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
667 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
668 $this->tearDownFiles();
671 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
672 $backendName = $this->backendClass();
677 foreach ( $srcs as $i => $source ) {
678 $this->prepare( array( 'dir' => dirname( $source ) ) );
680 'op' => 'create', // operation
681 'dst' => $source, // source
682 'content' => $srcsContent[$i]
684 $expContent .= $srcsContent[$i];
686 $status = $this->backend
->doOperations( $ops );
688 $this->assertEquals( array(), $status->errors
,
689 "Creation of source files succeeded ($backendName)." );
691 $dest = $params['dst'];
692 if ( $alreadyExists ) {
693 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
694 $this->assertEquals( true, $ok,
695 "Creation of file at $dest succeeded ($backendName)." );
697 $ok = file_put_contents( $dest, '' ) !== false;
698 $this->assertEquals( true, $ok,
699 "Creation of 0-byte file at $dest succeeded ($backendName)." );
702 // Combine the files into one
703 $status = $this->backend
->concatenate( $params );
705 $this->assertEquals( array(), $status->errors
,
706 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
707 $this->assertEquals( true, $status->isOK(),
708 "Creation of concat file at $dest succeeded ($backendName)." );
710 $this->assertEquals( false, $status->isOK(),
711 "Creation of concat file at $dest failed ($backendName)." );
715 $this->assertEquals( true, is_file( $dest ),
716 "Dest concat file $dest exists after creation ($backendName)." );
718 $this->assertEquals( true, is_file( $dest ),
719 "Dest concat file $dest exists after failed creation ($backendName)." );
722 $contents = file_get_contents( $dest );
723 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
726 $this->assertEquals( $expContent, $contents,
727 "Concat file at $dest has correct contents ($backendName)." );
729 $this->assertNotEquals( $expContent, $contents,
730 "Concat file at $dest has correct contents ($backendName)." );
734 function provider_testConcatenate() {
737 $rand = mt_rand( 0, 2000000000 ) . time();
738 $dest = wfTempDir() . "/randomfile!$rand.txt";
740 $this->baseStorePath() . '/unittest-cont1/file1.txt',
741 $this->baseStorePath() . '/unittest-cont1/file2.txt',
742 $this->baseStorePath() . '/unittest-cont1/file3.txt',
743 $this->baseStorePath() . '/unittest-cont1/file4.txt',
744 $this->baseStorePath() . '/unittest-cont1/file5.txt',
745 $this->baseStorePath() . '/unittest-cont1/file6.txt',
746 $this->baseStorePath() . '/unittest-cont1/file7.txt',
747 $this->baseStorePath() . '/unittest-cont1/file8.txt',
748 $this->baseStorePath() . '/unittest-cont1/file9.txt',
749 $this->baseStorePath() . '/unittest-cont1/file10.txt'
763 $params = array( 'srcs' => $srcs, 'dst' => $dest );
766 $params, // operation
768 $content, // content for each source
769 false, // no dest already exists
774 $params, // operation
776 $content, // content for each source
777 true, // dest already exists
785 * @dataProvider provider_testGetFileStat
787 public function testGetFileStat( $path, $content, $alreadyExists ) {
788 $this->backend
= $this->singleBackend
;
789 $this->tearDownFiles();
790 $this->doTestGetFileStat( $path, $content, $alreadyExists );
791 $this->tearDownFiles();
793 $this->backend
= $this->multiBackend
;
794 $this->tearDownFiles();
795 $this->doTestGetFileStat( $path, $content, $alreadyExists );
796 $this->tearDownFiles();
799 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
800 $backendName = $this->backendClass();
802 if ( $alreadyExists ) {
803 $this->prepare( array( 'dir' => dirname( $path ) ) );
804 $status = $this->backend
->create( array( 'dst' => $path, 'content' => $content ) );
805 $this->assertEquals( array(), $status->errors
,
806 "Creation of file at $path succeeded ($backendName)." );
808 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
809 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
810 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
812 $this->assertEquals( strlen( $content ), $size,
813 "Correct file size of '$path'" );
814 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 5,
815 "Correct file timestamp of '$path'" );
817 $size = $stat['size'];
818 $time = $stat['mtime'];
819 $this->assertEquals( strlen( $content ), $size,
820 "Correct file size of '$path'" );
821 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 5,
822 "Correct file timestamp of '$path'" );
824 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
825 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
826 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
828 $this->assertFalse( $size, "Correct file size of '$path'" );
829 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
830 $this->assertFalse( $stat, "Correct file stat of '$path'" );
834 function provider_testGetFileStat() {
837 $base = $this->baseStorePath();
838 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents", true );
839 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "", true );
840 $cases[] = array( "$base/unittest-cont1/b/some-diff_file.txt", null, false );
846 * @dataProvider provider_testGetFileContents
848 public function testGetFileContents( $source, $content ) {
849 $this->backend
= $this->singleBackend
;
850 $this->tearDownFiles();
851 $this->doTestGetFileContents( $source, $content );
852 $this->tearDownFiles();
854 $this->backend
= $this->multiBackend
;
855 $this->tearDownFiles();
856 $this->doTestGetFileContents( $source, $content );
857 $this->tearDownFiles();
860 private function doTestGetFileContents( $source, $content ) {
861 $backendName = $this->backendClass();
863 $this->prepare( array( 'dir' => dirname( $source ) ) );
865 $status = $this->backend
->doOperation(
866 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
867 $this->assertEquals( array(), $status->errors
,
868 "Creation of file at $source succeeded ($backendName)." );
869 $this->assertEquals( true, $status->isOK(),
870 "Creation of file at $source succeeded with OK status ($backendName)." );
872 $newContents = $this->backend
->getFileContents( array( 'src' => $source, 'latest' => 1 ) );
873 $this->assertNotEquals( false, $newContents,
874 "Read of file at $source succeeded ($backendName)." );
876 $this->assertEquals( $content, $newContents,
877 "Contents read match data at $source ($backendName)." );
880 function provider_testGetFileContents() {
883 $base = $this->baseStorePath();
884 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents" );
885 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "more file contents" );
891 * @dataProvider provider_testGetLocalCopy
893 public function testGetLocalCopy( $source, $content ) {
894 $this->backend
= $this->singleBackend
;
895 $this->tearDownFiles();
896 $this->doTestGetLocalCopy( $source, $content );
897 $this->tearDownFiles();
899 $this->backend
= $this->multiBackend
;
900 $this->tearDownFiles();
901 $this->doTestGetLocalCopy( $source, $content );
902 $this->tearDownFiles();
905 private function doTestGetLocalCopy( $source, $content ) {
906 $backendName = $this->backendClass();
908 $this->prepare( array( 'dir' => dirname( $source ) ) );
910 $status = $this->backend
->doOperation(
911 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
912 $this->assertEquals( array(), $status->errors
,
913 "Creation of file at $source succeeded ($backendName)." );
915 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
916 $this->assertNotNull( $tmpFile,
917 "Creation of local copy of $source succeeded ($backendName)." );
919 $contents = file_get_contents( $tmpFile->getPath() );
920 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
923 function provider_testGetLocalCopy() {
926 $base = $this->baseStorePath();
927 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
928 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
934 * @dataProvider provider_testGetLocalReference
936 public function testGetLocalReference( $source, $content ) {
937 $this->backend
= $this->singleBackend
;
938 $this->tearDownFiles();
939 $this->doTestGetLocalReference( $source, $content );
940 $this->tearDownFiles();
942 $this->backend
= $this->multiBackend
;
943 $this->tearDownFiles();
944 $this->doTestGetLocalReference( $source, $content );
945 $this->tearDownFiles();
948 private function doTestGetLocalReference( $source, $content ) {
949 $backendName = $this->backendClass();
951 $this->prepare( array( 'dir' => dirname( $source ) ) );
953 $status = $this->backend
->doOperation(
954 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
955 $this->assertEquals( array(), $status->errors
,
956 "Creation of file at $source succeeded ($backendName)." );
958 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
959 $this->assertNotNull( $tmpFile,
960 "Creation of local copy of $source succeeded ($backendName)." );
962 $contents = file_get_contents( $tmpFile->getPath() );
963 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
966 function provider_testGetLocalReference() {
969 $base = $this->baseStorePath();
970 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
971 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
977 * @dataProvider provider_testPrepareAndClean
979 public function testPrepareAndClean( $path, $isOK ) {
980 $this->backend
= $this->singleBackend
;
981 $this->doTestPrepareAndClean( $path, $isOK );
982 $this->tearDownFiles();
984 $this->backend
= $this->multiBackend
;
985 $this->doTestPrepareAndClean( $path, $isOK );
986 $this->tearDownFiles();
989 function provider_testPrepareAndClean() {
990 $base = $this->baseStorePath();
992 array( "$base/unittest-cont1/a/z/some_file1.txt", true ),
993 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
994 # Specific to FS backend with no basePath field set
995 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
999 private function doTestPrepareAndClean( $path, $isOK ) {
1000 $backendName = $this->backendClass();
1002 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1004 $this->assertEquals( array(), $status->errors
,
1005 "Preparing dir $path succeeded without warnings ($backendName)." );
1006 $this->assertEquals( true, $status->isOK(),
1007 "Preparing dir $path succeeded ($backendName)." );
1009 $this->assertEquals( false, $status->isOK(),
1010 "Preparing dir $path failed ($backendName)." );
1013 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1015 $this->assertEquals( array(), $status->errors
,
1016 "Cleaning dir $path succeeded without warnings ($backendName)." );
1017 $this->assertEquals( true, $status->isOK(),
1018 "Cleaning dir $path succeeded ($backendName)." );
1020 $this->assertEquals( false, $status->isOK(),
1021 "Cleaning dir $path failed ($backendName)." );
1025 public function testRecursiveClean() {
1026 $this->backend
= $this->singleBackend
;
1027 $this->doTestRecursiveClean();
1028 $this->tearDownFiles();
1030 $this->backend
= $this->multiBackend
;
1031 $this->doTestRecursiveClean();
1032 $this->tearDownFiles();
1035 function doTestRecursiveClean() {
1036 $backendName = $this->backendClass();
1038 $base = $this->baseStorePath();
1040 "$base/unittest-cont1/a",
1041 "$base/unittest-cont1/a/b",
1042 "$base/unittest-cont1/a/b/c",
1043 "$base/unittest-cont1/a/b/c/d0",
1044 "$base/unittest-cont1/a/b/c/d1",
1045 "$base/unittest-cont1/a/b/c/d2",
1046 "$base/unittest-cont1/a/b/c/d0/1",
1047 "$base/unittest-cont1/a/b/c/d0/2",
1048 "$base/unittest-cont1/a/b/c/d1/3",
1049 "$base/unittest-cont1/a/b/c/d1/4",
1050 "$base/unittest-cont1/a/b/c/d2/5",
1051 "$base/unittest-cont1/a/b/c/d2/6"
1053 foreach ( $dirs as $dir ) {
1054 $status = $this->prepare( array( 'dir' => $dir ) );
1055 $this->assertEquals( array(), $status->errors
,
1056 "Preparing dir $dir succeeded without warnings ($backendName)." );
1059 if ( $this->backend
instanceof FSFileBackend
) {
1060 foreach ( $dirs as $dir ) {
1061 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1062 "Dir $dir exists ($backendName)." );
1066 $status = $this->backend
->clean(
1067 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1068 $this->assertEquals( array(), $status->errors
,
1069 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1071 foreach ( $dirs as $dir ) {
1072 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1073 "Dir $dir no longer exists ($backendName)." );
1077 // @TODO: testSecure
1079 public function testDoOperations() {
1080 $this->backend
= $this->singleBackend
;
1081 $this->tearDownFiles();
1082 $this->doTestDoOperations();
1083 $this->tearDownFiles();
1085 $this->backend
= $this->multiBackend
;
1086 $this->tearDownFiles();
1087 $this->doTestDoOperations();
1088 $this->tearDownFiles();
1090 $this->backend
= $this->singleBackend
;
1091 $this->tearDownFiles();
1092 $this->doTestDoOperationsFailing();
1093 $this->tearDownFiles();
1095 $this->backend
= $this->multiBackend
;
1096 $this->tearDownFiles();
1097 $this->doTestDoOperationsFailing();
1098 $this->tearDownFiles();
1100 // @TODO: test some cases where the ops should fail
1103 private function doTestDoOperations() {
1104 $base = $this->baseStorePath();
1106 $fileA = "$base/unittest-cont1/a/b/fileA.txt";
1107 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1108 $fileB = "$base/unittest-cont1/a/b/fileB.txt";
1109 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1110 $fileC = "$base/unittest-cont1/a/b/fileC.txt";
1111 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1112 $fileD = "$base/unittest-cont1/a/b/fileD.txt";
1114 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1115 $this->backend
->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1116 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1117 $this->backend
->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1118 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1119 $this->backend
->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1120 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1122 $status = $this->backend
->doOperations( array(
1123 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1124 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1125 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1126 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1127 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1128 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1129 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1130 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1131 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1132 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1133 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1134 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1135 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1136 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1137 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1138 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1139 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1141 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1143 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1145 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1147 array( 'op' => 'null' ),
1151 $this->assertEquals( array(), $status->errors
, "Operation batch succeeded" );
1152 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1153 $this->assertEquals( 13, count( $status->success
),
1154 "Operation batch has correct success array" );
1156 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1157 "File does not exist at $fileA" );
1158 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1159 "File does not exist at $fileB" );
1160 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1161 "File does not exist at $fileD" );
1163 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1164 "File exists at $fileC" );
1165 $this->assertEquals( $fileBContents,
1166 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1167 "Correct file contents of $fileC" );
1168 $this->assertEquals( strlen( $fileBContents ),
1169 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1170 "Correct file size of $fileC" );
1171 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1172 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1173 "Correct file SHA-1 of $fileC" );
1176 private function doTestDoOperationsFailing() {
1177 $base = $this->baseStorePath();
1179 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1180 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1181 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1182 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1183 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1184 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1185 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1187 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1188 $this->backend
->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1189 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1190 $this->backend
->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1191 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1192 $this->backend
->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1194 $status = $this->backend
->doOperations( array(
1195 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1196 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1197 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1198 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1199 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1200 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1201 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1202 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1203 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1204 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1205 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1206 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1207 array( 'op' => 'delete', 'src' => $fileD ),
1208 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1209 array( 'op' => 'null' ),
1211 ), array( 'force' => 1 ) );
1213 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1214 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1215 $this->assertEquals( 8, count( $status->success
),
1216 "Operation batch has correct success array" );
1218 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1219 "File does not exist at $fileB" );
1220 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1221 "File does not exist at $fileD" );
1223 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1224 "File does not exist at $fileA" );
1225 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1226 "File exists at $fileC" );
1227 $this->assertEquals( $fileBContents,
1228 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1229 "Correct file contents of $fileA" );
1230 $this->assertEquals( strlen( $fileBContents ),
1231 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1232 "Correct file size of $fileA" );
1233 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1234 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1235 "Correct file SHA-1 of $fileA" );
1238 public function testGetFileList() {
1239 $this->backend
= $this->singleBackend
;
1240 $this->tearDownFiles();
1241 $this->doTestGetFileList();
1242 $this->tearDownFiles();
1244 $this->backend
= $this->multiBackend
;
1245 $this->tearDownFiles();
1246 $this->doTestGetFileList();
1247 $this->tearDownFiles();
1250 private function doTestGetFileList() {
1251 $backendName = $this->backendClass();
1253 $base = $this->baseStorePath();
1255 "$base/unittest-cont1/test1.txt",
1256 "$base/unittest-cont1/test2.txt",
1257 "$base/unittest-cont1/test3.txt",
1258 "$base/unittest-cont1/subdir1/test1.txt",
1259 "$base/unittest-cont1/subdir1/test2.txt",
1260 "$base/unittest-cont1/subdir2/test3.txt",
1261 "$base/unittest-cont1/subdir2/test4.txt",
1262 "$base/unittest-cont1/subdir2/subdir/test1.txt",
1263 "$base/unittest-cont1/subdir2/subdir/test2.txt",
1264 "$base/unittest-cont1/subdir2/subdir/test3.txt",
1265 "$base/unittest-cont1/subdir2/subdir/test4.txt",
1266 "$base/unittest-cont1/subdir2/subdir/test5.txt",
1267 "$base/unittest-cont1/subdir2/subdir/sub/test0.txt",
1268 "$base/unittest-cont1/subdir2/subdir/sub/120-px-file.txt",
1273 foreach ( $files as $file ) {
1274 $this->prepare( array( 'dir' => dirname( $file ) ) );
1275 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1277 $status = $this->backend
->doOperations( $ops );
1278 $this->assertEquals( array(), $status->errors
,
1279 "Creation of files succeeded ($backendName)." );
1280 $this->assertEquals( true, $status->isOK(),
1281 "Creation of files succeeded with OK status ($backendName)." );
1288 "subdir1/test1.txt",
1289 "subdir1/test2.txt",
1290 "subdir2/test3.txt",
1291 "subdir2/test4.txt",
1292 "subdir2/subdir/test1.txt",
1293 "subdir2/subdir/test2.txt",
1294 "subdir2/subdir/test3.txt",
1295 "subdir2/subdir/test4.txt",
1296 "subdir2/subdir/test5.txt",
1297 "subdir2/subdir/sub/test0.txt",
1298 "subdir2/subdir/sub/120-px-file.txt",
1302 // Actual listing (no trailing slash)
1304 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1305 foreach ( $iter as $file ) {
1310 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1312 // Actual listing (with trailing slash)
1314 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1315 foreach ( $iter as $file ) {
1320 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1330 "sub/120-px-file.txt",
1334 // Actual listing (no trailing slash)
1336 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) );
1337 foreach ( $iter as $file ) {
1342 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1344 // Actual listing (with trailing slash)
1346 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir/" ) );
1347 foreach ( $iter as $file ) {
1352 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1354 // Actual listing (using iterator second time)
1356 foreach ( $iter as $file ) {
1361 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1363 // Expected listing (top files only)
1373 // Actual listing (top files only)
1375 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) );
1376 foreach ( $iter as $file ) {
1381 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1383 foreach ( $files as $file ) { // clean up
1384 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1387 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1388 foreach ( $iter as $iter ) {} // no errors
1391 public function testGetDirectoryList() {
1392 $this->backend
= $this->singleBackend
;
1393 $this->tearDownFiles();
1394 $this->doTestGetDirectoryList();
1395 $this->tearDownFiles();
1397 $this->backend
= $this->multiBackend
;
1398 $this->tearDownFiles();
1399 $this->doTestGetDirectoryList();
1400 $this->tearDownFiles();
1403 private function doTestGetDirectoryList() {
1404 $backendName = $this->backendClass();
1406 $base = $this->baseStorePath();
1408 "$base/unittest-cont1/test1.txt",
1409 "$base/unittest-cont1/test2.txt",
1410 "$base/unittest-cont1/test3.txt",
1411 "$base/unittest-cont1/subdir1/test1.txt",
1412 "$base/unittest-cont1/subdir1/test2.txt",
1413 "$base/unittest-cont1/subdir2/test3.txt",
1414 "$base/unittest-cont1/subdir2/test4.txt",
1415 "$base/unittest-cont1/subdir2/subdir/test1.txt",
1416 "$base/unittest-cont1/subdir3/subdir/test2.txt",
1417 "$base/unittest-cont1/subdir4/subdir/test3.txt",
1418 "$base/unittest-cont1/subdir4/subdir/test4.txt",
1419 "$base/unittest-cont1/subdir4/subdir/test5.txt",
1420 "$base/unittest-cont1/subdir4/subdir/sub/test0.txt",
1421 "$base/unittest-cont1/subdir4/subdir/sub/120-px-file.txt",
1426 foreach ( $files as $file ) {
1427 $this->prepare( array( 'dir' => dirname( $file ) ) );
1428 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1430 $status = $this->backend
->doOperations( $ops );
1431 $this->assertEquals( array(), $status->errors
,
1432 "Creation of files succeeded ($backendName)." );
1433 $this->assertEquals( true, $status->isOK(),
1434 "Creation of files succeeded with OK status ($backendName)." );
1445 $this->assertEquals( true,
1446 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/subdir1" ) ),
1447 "Directory exists in ($backendName)." );
1448 $this->assertEquals( true,
1449 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) ),
1450 "Directory exists in ($backendName)." );
1451 $this->assertEquals( false,
1452 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/subdir2/test1.txt" ) ),
1453 "Directory does not exists in ($backendName)." );
1455 // Actual listing (no trailing slash)
1457 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1458 foreach ( $iter as $file ) {
1463 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1465 // Actual listing (with trailing slash)
1467 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1468 foreach ( $iter as $file ) {
1473 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1481 // Actual listing (no trailing slash)
1483 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/subdir2" ) );
1484 foreach ( $iter as $file ) {
1489 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1491 // Actual listing (with trailing slash)
1493 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/subdir2/" ) );
1494 foreach ( $iter as $file ) {
1499 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1501 // Actual listing (using iterator second time)
1503 foreach ( $iter as $file ) {
1508 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1510 // Expected listing (recursive)
1519 "subdir4/subdir/sub",
1523 // Actual listing (recursive)
1525 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1526 foreach ( $iter as $file ) {
1531 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1533 // Expected listing (recursive)
1540 // Actual listing (recursive)
1542 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/subdir4" ) );
1543 foreach ( $iter as $file ) {
1548 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1550 // Actual listing (recursive, second time)
1552 foreach ( $iter as $file ) {
1557 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1559 foreach ( $files as $file ) { // clean up
1560 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1563 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1564 foreach ( $iter as $iter ) {} // no errors
1567 public function testLockCalls() {
1568 $this->backend
= $this->singleBackend
;
1569 $this->doTestLockCalls();
1572 private function doTestLockCalls() {
1573 $backendName = $this->backendClass();
1575 for ( $i=0; $i<50; $i++
) {
1581 "subdir1", // duplicate
1582 "subdir1/test1.txt",
1583 "subdir1/test2.txt",
1585 "subdir2", // duplicate
1586 "subdir2/test3.txt",
1587 "subdir2/test4.txt",
1589 "subdir2/subdir/test1.txt",
1590 "subdir2/subdir/test2.txt",
1591 "subdir2/subdir/test3.txt",
1592 "subdir2/subdir/test4.txt",
1593 "subdir2/subdir/test5.txt",
1594 "subdir2/subdir/sub",
1595 "subdir2/subdir/sub/test0.txt",
1596 "subdir2/subdir/sub/120-px-file.txt",
1599 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
1600 $this->assertEquals( array(), $status->errors
,
1601 "Locking of files succeeded ($backendName)." );
1602 $this->assertEquals( true, $status->isOK(),
1603 "Locking of files succeeded with OK status ($backendName)." );
1605 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
1606 $this->assertEquals( array(), $status->errors
,
1607 "Locking of files succeeded ($backendName)." );
1608 $this->assertEquals( true, $status->isOK(),
1609 "Locking of files succeeded with OK status ($backendName)." );
1611 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
1612 $this->assertEquals( array(), $status->errors
,
1613 "Locking of files succeeded ($backendName)." );
1614 $this->assertEquals( true, $status->isOK(),
1615 "Locking of files succeeded with OK status ($backendName)." );
1617 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
1618 $this->assertEquals( array(), $status->errors
,
1619 "Locking of files succeeded ($backendName)." );
1620 $this->assertEquals( true, $status->isOK(),
1621 "Locking of files succeeded with OK status ($backendName)." );
1625 // test helper wrapper for backend prepare() function
1626 private function prepare( array $params ) {
1627 $this->dirsToPrune
[] = $params['dir'];
1628 return $this->backend
->prepare( $params );
1631 function tearDownFiles() {
1632 foreach ( $this->filesToPrune
as $file ) {
1635 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1636 foreach ( $containers as $container ) {
1637 $this->deleteFiles( $container );
1639 foreach ( $this->dirsToPrune
as $dir ) {
1640 $this->recursiveClean( $dir );
1642 $this->filesToPrune
= $this->dirsToPrune
= array();
1645 private function deleteFiles( $container ) {
1646 $base = $this->baseStorePath();
1647 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
1649 foreach ( $iter as $file ) {
1650 $this->backend
->delete( array( 'src' => "$base/$container/$file" ),
1651 array( 'force' => 1, 'nonLocking' => 1 ) );
1656 private function recursiveClean( $dir ) {
1658 if ( !$this->backend
->clean( array( 'dir' => $dir ) )->isOK() ) {
1661 } while ( $dir = FileBackend
::parentStoragePath( $dir ) );
1664 function tearDown() {