3 // @TODO: fix empty dir leakage
4 class FileBackendTest
extends MediaWikiTestCase
{
5 private $backend, $multiBackend;
6 private $filesToPrune, $pathsToPrune;
10 $this->backend
= new FSFileBackend( array(
11 'name' => 'localtesting',
12 'lockManager' => 'fsLockManager',
13 'containerPaths' => array(
14 'cont1' => wfTempDir() . '/localtesting/cont1',
15 'cont2' => wfTempDir() . '/localtesting/cont2' )
17 $this->multiBackend
= new FileBackendMultiWrite( array(
18 'name' => 'localtestingmulti',
19 'lockManager' => 'fsLockManager',
22 'name' => 'localmutlitesting1',
23 'class' => 'FSFileBackend',
24 'lockManager' => 'nullLockManager',
25 'containerPaths' => array(
26 'cont1' => wfTempDir() . '/localtestingmulti1/cont1',
27 'cont2' => wfTempDir() . '/localtestingmulti1/cont2' ),
28 'isMultiMaster' => false
31 'name' => 'localmutlitesting2',
32 'class' => 'FSFileBackend',
33 'lockManager' => 'nullLockManager',
34 'containerPaths' => array(
35 'cont1' => wfTempDir() . '/localtestingmulti2/cont1',
36 'cont2' => wfTempDir() . '/localtestingmulti2/cont2' ),
37 'isMultiMaster' => true
41 $this->filesToPrune
= $this->pathsToPrune
= array();
44 private function singleBasePath() {
45 return 'mwstore://localtesting';
49 * @dataProvider provider_testStore
51 public function testStore( $op, $source, $dest ) {
52 $this->filesToPrune
[] = $source;
53 $this->pathsToPrune
[] = $dest;
55 file_put_contents( $source, "Unit test file" );
56 $status = $this->backend
->doOperation( $op );
58 $this->assertEquals( true, $status->isOK(),
59 "Store from $source to $dest succeeded." );
60 $this->assertEquals( true, $status->isGood(),
61 "Store from $source to $dest succeeded without warnings." );
62 $this->assertEquals( true, file_exists( $source ),
63 "Source file $source still exists." );
64 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
65 "Destination file $dest exists." );
67 $props1 = FSFile
::getPropsFromPath( $source );
68 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
69 $this->assertEquals( $props1, $props2,
70 "Source and destination have the same props." );
73 public function provider_testStore() {
76 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
77 $toPath = $this->singleBasePath() . '/cont1/fun/obj1.txt';
78 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
85 $op['overwriteDest'] = true;
96 * @dataProvider provider_testCopy
98 public function testCopy( $op, $source, $dest ) {
99 $this->pathsToPrune
[] = $source;
100 $this->pathsToPrune
[] = $dest;
102 $status = $this->backend
->doOperation(
103 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
104 $this->assertEquals( true, $status->isOK(), "Creation of file at $source succeeded." );
106 $status = $this->backend
->doOperation( $op );
107 $this->assertEquals( true, $status->isOK(),
108 "Copy from $source to $dest succeeded." );
109 $this->assertEquals( true, $status->isGood(),
110 "Copy from $source to $dest succeeded without warnings." );
111 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
112 "Source file $source still exists." );
113 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
114 "Destination file $dest exists after copy." );
116 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
117 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
118 $this->assertEquals( $props1, $props2,
119 "Source and destination have the same props." );
122 public function provider_testCopy() {
125 $source = $this->singleBasePath() . '/cont1/file.txt';
126 $dest = $this->singleBasePath() . '/cont2/fileMoved.txt';
128 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
135 $op['overwriteDest'] = true;
146 * @dataProvider provider_testMove
148 public function testMove( $op, $source, $dest ) {
149 $this->pathsToPrune
[] = $source;
150 $this->pathsToPrune
[] = $dest;
152 $status = $this->backend
->doOperation(
153 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
154 $this->assertEquals( true, $status->isOK(), "Creation of file at $source succeeded." );
156 $status = $this->backend
->doOperation( $op );
157 $this->assertEquals( true, $status->isOK(),
158 "Move from $source to $dest succeeded." );
159 $this->assertEquals( true, $status->isGood(),
160 "Move from $source to $dest succeeded without warnings." );
161 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
162 "Source file $source does not still exists." );
163 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
164 "Destination file $dest exists after move." );
166 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
167 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
168 $this->assertEquals( false, $props1['fileExists'],
169 "Source file does not exist accourding to props." );
170 $this->assertEquals( true, $props2['fileExists'],
171 "Destination file exists accourding to props." );
174 public function provider_testMove() {
177 $source = $this->singleBasePath() . '/cont1/file.txt';
178 $dest = $this->singleBasePath() . '/cont2/fileMoved.txt';
180 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
187 $op['overwriteDest'] = true;
198 * @dataProvider provider_testDelete
200 public function testDelete( $op, $source, $withSource, $okStatus ) {
201 $this->pathsToPrune
[] = $source;
204 $status = $this->backend
->doOperation(
205 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
206 $this->assertEquals( true, $status->isOK(), "Creation of file at $source succeeded." );
209 $status = $this->backend
->doOperation( $op );
211 $this->assertEquals( true, $status->isOK(), "Deletion of file at $source succeeded." );
213 $this->assertEquals( false, $status->isOK(), "Deletion of file at $source failed." );
216 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
217 "Source file $source does not exist after move." );
219 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
220 $this->assertEquals( false, $props1['fileExists'],
221 "Source file $source does not exist according to props." );
224 public function provider_testDelete() {
227 $source = $this->singleBasePath() . '/cont1/myfacefile.txt';
229 $op = array( 'op' => 'delete', 'src' => $source );
240 false, // without source
244 $op['ignoreMissingSource'] = true;
248 false, // without source
256 * @dataProvider provider_testCreate
258 public function testCreate( $op, $dest, $alreadyExists, $okStatus, $newSize ) {
259 $this->pathsToPrune
[] = $dest;
261 $oldText = 'blah...blah...waahwaah';
262 if ( $alreadyExists ) {
263 $status = $this->backend
->doOperation(
264 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
265 $this->assertEquals( true, $status->isOK(), "Creation of file at $dest succeeded." );
268 $status = $this->backend
->doOperation( $op );
270 $this->assertEquals( true, $status->isOK(), "Creation of file at $dest succeeded." );
272 $this->assertEquals( false, $status->isOK(), "Creation of file at $dest failed." );
275 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
276 "Dest file $dest exists after creation." );
278 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
279 $this->assertEquals( true, $props1['fileExists'],
280 "Dest file $dest exists according to props." );
281 if ( $okStatus ) { // file content is what we saved
282 $this->assertEquals( $newSize, $props1['size'],
283 "Dest file $dest has expected size according to props." );
284 } else { // file content is some other previous text
285 $this->assertEquals( strlen( $oldText ), $props1['size'],
286 "Dest file $dest has different size that given text according to props." );
291 * @dataProvider provider_testCreate
293 public function provider_testCreate() {
296 $source = $this->singleBasePath() . '/cont2/myspacefile.txt';
298 $dummyText = 'hey hey';
299 $op = array( 'op' => 'create', 'content' => $dummyText, 'dst' => $source );
303 false, // no dest already exists
311 true, // dest already exists
316 $op['overwriteDest'] = true;
320 true, // dest already exists
329 * @dataProvider provider_testConcatenate
331 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
332 $this->pathsToPrune
= array_merge( $this->pathsToPrune
, $srcs );
333 $this->pathsToPrune
[] = $op['dst'];
338 foreach ( $srcs as $i => $source ) {
340 'op' => 'create', // operation
341 'dst' => $source, // source
342 'content' => $srcsContent[$i]
344 $expContent .= $srcsContent[$i];
346 $status = $this->backend
->doOperations( $ops );
348 $this->assertEquals( true, $status->isOK(), "Creation of source files succeeded." );
351 if ( $alreadyExists ) {
352 $oldText = 'blah...blah...waahwaah';
353 $status = $this->backend
->doOperation(
354 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
355 $this->assertEquals( true, $status->isOK(), "Creation of file at $dest succeeded." );
359 $status = $this->backend
->doOperation( $op );
361 $this->assertEquals( true, $status->isOK(), "Creation of concat file at $dest succeeded." );
363 $this->assertEquals( false, $status->isOK(), "Creation of concat file at $dest failed." );
367 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
368 "Dest concat file $dest exists after creation." );
370 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
371 "Dest concat file $dest exists after failed creation." );
374 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $dest ) );
375 $this->assertNotNull( $tmpFile, "Creation of local copy of $dest succeeded." );
377 $contents = file_get_contents( $tmpFile->getPath() );
378 $this->assertNotEquals( false, $contents, "Local copy of $dest exists." );
381 $this->assertEquals( $expContent, $contents, "Concat file at $dest has correct contents." );
383 $this->assertNotEquals( $expContent, $contents, "Concat file at $dest has correct contents." );
387 function provider_testConcatenate() {
390 $dest = $this->singleBasePath() . '/cont1/full_file.txt';
392 $this->singleBasePath() . '/cont1/file1.txt',
393 $this->singleBasePath() . '/cont1/file2.txt',
394 $this->singleBasePath() . '/cont1/file3.txt',
395 $this->singleBasePath() . '/cont1/file4.txt',
396 $this->singleBasePath() . '/cont1/file5.txt',
397 $this->singleBasePath() . '/cont1/file6.txt',
398 $this->singleBasePath() . '/cont1/file7.txt',
399 $this->singleBasePath() . '/cont1/file8.txt',
400 $this->singleBasePath() . '/cont1/file9.txt',
401 $this->singleBasePath() . '/cont1/file10.txt'
415 $op = array( 'op' => 'concatenate', 'srcs' => $srcs, 'dst' => $dest );
420 $content, // content for each source
421 false, // no dest already exists
428 $content, // content for each source
429 true, // no dest already exists
433 $op['overwriteDest'] = true;
437 $content, // content for each source
438 true, // no dest already exists
446 * @dataProvider provider_testGetLocalCopy
448 public function testGetLocalCopy( $src, $content ) {
449 $this->pathsToPrune
[] = $src;
451 $status = $this->backend
->doOperation(
452 array( 'op' => 'create', 'content' => $content, 'dst' => $src ) );
453 $this->assertEquals( true, $status->isOK(), "Creation of file at $src succeeded." );
455 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $src ) );
456 $this->assertNotNull( $tmpFile, "Creation of local copy of $src succeeded." );
458 $contents = file_get_contents( $tmpFile->getPath() );
459 $this->assertNotEquals( false, $contents, "Local copy of $src exists." );
462 function provider_testGetLocalCopy() {
465 $base = $this->singleBasePath();
466 $cases[] = array( "$base/cont1/a/z/some_file.txt", "some file contents" );
467 $cases[] = array( "$base/cont1/a/some-other_file.txt", "more file contents" );
473 * @dataProvider provider_testGetReference
475 public function testGetLocalReference( $src, $content ) {
476 $this->pathsToPrune
[] = $src;
478 $status = $this->backend
->doOperation(
479 array( 'op' => 'create', 'content' => $content, 'dst' => $src ) );
480 $this->assertEquals( true, $status->isOK(), "Creation of file at $src succeeded." );
482 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $src ) );
483 $this->assertNotNull( $tmpFile, "Creation of local copy of $src succeeded." );
485 $contents = file_get_contents( $tmpFile->getPath() );
486 $this->assertNotEquals( false, $contents, "Local copy of $src exists." );
489 function provider_testGetReference() {
492 $base = $this->singleBasePath();
493 $cases[] = array( "$base/cont1/a/z/some_file.txt", "some file contents" );
494 $cases[] = array( "$base/cont1/a/some-other_file.txt", "more file contents" );
499 // @TODO: testPrepare
505 // @TODO: testDoOperations
507 public function testGetFileList() {
508 $base = $this->singleBasePath();
510 "$base/cont1/test1.txt",
511 "$base/cont1/test2.txt",
512 "$base/cont1/test3.txt",
513 "$base/cont1/subdir1/test1.txt",
514 "$base/cont1/subdir1/test2.txt",
515 "$base/cont1/subdir2/test3.txt",
516 "$base/cont1/subdir2/test4.txt",
517 "$base/cont1/subdir2/subdir/test1.txt",
518 "$base/cont1/subdir2/subdir/test2.txt",
519 "$base/cont1/subdir2/subdir/test3.txt",
520 "$base/cont1/subdir2/subdir/test4.txt",
521 "$base/cont1/subdir2/subdir/test5.txt",
522 "$base/cont1/subdir2/subdir/sub/test0.txt",
523 "$base/cont1/subdir2/subdir/sub/120-px-file.txt",
525 $this->pathsToPrune
= array_merge( $this->pathsToPrune
, $files );
529 foreach ( $files as $file ) {
530 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
532 $status = $this->backend
->doOperations( $ops );
533 $this->assertEquals( true, $status->isOK(), "Creation of files succeeded." );
544 "subdir2/subdir/test1.txt",
545 "subdir2/subdir/test2.txt",
546 "subdir2/subdir/test3.txt",
547 "subdir2/subdir/test4.txt",
548 "subdir2/subdir/test5.txt",
549 "subdir2/subdir/sub/test0.txt",
550 "subdir2/subdir/sub/120-px-file.txt",
552 $expected = sort( $expected );
554 // Actual listing (no trailing slash)
556 $iter = $this->backend
->getFileList( array( 'dir' => "$base/cont1" ) );
557 foreach ( $iter as $file ) {
561 $this->assertEquals( $expected, sort( $list ), "Correct file listing." );
563 // Actual listing (with trailing slash)
565 $iter = $this->backend
->getFileList( array( 'dir' => "$base/cont1/" ) );
566 foreach ( $iter as $file ) {
570 $this->assertEquals( $expected, sort( $list ), "Correct file listing." );
572 foreach ( $files as $file ) {
573 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => "$base/$file" ) );
576 $iter = $this->backend
->getFileList( array( 'dir' => "$base/cont1/not/exists" ) );
577 foreach ( $iter as $iter ) {} // no errors
580 function tearDown() {
582 foreach ( $this->filesToPrune
as $file ) {
585 foreach ( $this->pathsToPrune
as $file ) {
586 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
587 $this->multiBackend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
589 $this->backend
= $this->multiBackend
= null;
590 $this->filesToPrune
= $this->pathsToPrune
= array();