Made FileOp classes enforce required params. Also reverts r109823.
[lhc/web/wiklou.git] / tests / phpunit / includes / filerepo / FileBackendTest.php
1 <?php
2
3 /**
4 * @group FileRepo
5 */
6 class FileBackendTest extends MediaWikiTestCase {
7 private $backend, $multiBackend;
8 private $filesToPrune;
9 private static $backendToUse;
10
11 function setUp() {
12 global $wgFileBackends;
13 parent::setUp();
14 $tmpDir = wfTempDir() . '/file-backend-test-' . time() . '-' . mt_rand();
15 if ( $this->getCliArg( 'use-filebackend=' ) ) {
16 if ( self::$backendToUse ) {
17 $this->singleBackend = self::$backendToUse;
18 } else {
19 $name = $this->getCliArg( 'use-filebackend=' );
20 $useConfig = array();
21 foreach ( $wgFileBackends as $conf ) {
22 if ( $conf['name'] == $name ) {
23 $useConfig = $conf;
24 }
25 }
26 $useConfig['name'] = 'localtesting'; // swap name
27 self::$backendToUse = new $conf['class']( $useConfig );
28 $this->singleBackend = self::$backendToUse;
29 }
30 } else {
31 $this->singleBackend = new FSFileBackend( array(
32 'name' => 'localtesting',
33 'lockManager' => 'fsLockManager',
34 'containerPaths' => array(
35 'unittest-cont1' => "$tmpDir/localtesting/unittest-cont1",
36 'unittest-cont2' => "$tmpDir/localtesting/unittest-cont2" )
37 ) );
38 }
39 $this->multiBackend = new FileBackendMultiWrite( array(
40 'name' => 'localtesting',
41 'lockManager' => 'fsLockManager',
42 'backends' => array(
43 array(
44 'name' => 'localmutlitesting1',
45 'class' => 'FSFileBackend',
46 'lockManager' => 'nullLockManager',
47 'containerPaths' => array(
48 'unittest-cont1' => "$tmpDir/localtestingmulti1/cont1",
49 'unittest-cont2' => "$tmpDir/localtestingmulti1/unittest-cont2" ),
50 'isMultiMaster' => false
51 ),
52 array(
53 'name' => 'localmutlitesting2',
54 'class' => 'FSFileBackend',
55 'lockManager' => 'nullLockManager',
56 'containerPaths' => array(
57 'unittest-cont1' => "$tmpDir/localtestingmulti2/cont1",
58 'unittest-cont2' => "$tmpDir/localtestingmulti2/unittest-cont2" ),
59 'isMultiMaster' => true
60 )
61 )
62 ) );
63 $this->filesToPrune = array();
64 }
65
66 private function baseStorePath() {
67 return 'mwstore://localtesting';
68 }
69
70 private function backendClass() {
71 return get_class( $this->backend );
72 }
73
74 /**
75 * @dataProvider provider_testStore
76 */
77 public function testStore( $op, $source, $dest ) {
78 $this->filesToPrune[] = $source;
79
80 $this->backend = $this->singleBackend;
81 $this->tearDownFiles();
82 $this->doTestStore( $op, $source, $dest );
83 $this->tearDownFiles();
84
85 $this->backend = $this->multiBackend;
86 $this->tearDownFiles();
87 $this->doTestStore( $op, $source, $dest );
88 $this->tearDownFiles();
89 }
90
91 function doTestStore( $op, $source, $dest ) {
92 $backendName = $this->backendClass();
93
94 $this->backend->prepare( array( 'dir' => dirname( $dest ) ) );
95
96 file_put_contents( $source, "Unit test file" );
97
98 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
99 $this->backend->store( $op );
100 }
101
102 $status = $this->backend->doOperation( $op );
103
104 $this->assertEquals( array(), $status->errors,
105 "Store from $source to $dest succeeded without warnings ($backendName)." );
106 $this->assertEquals( array(), $status->errors,
107 "Store from $source to $dest succeeded ($backendName)." );
108 $this->assertEquals( array( 0 => true ), $status->success,
109 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
110 $this->assertEquals( true, file_exists( $source ),
111 "Source file $source still exists ($backendName)." );
112 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
113 "Destination file $dest exists ($backendName)." );
114
115 $this->assertEquals( filesize( $source ),
116 $this->backend->getFileSize( array( 'src' => $dest ) ),
117 "Destination file $dest has correct size ($backendName)." );
118
119 $props1 = FSFile::getPropsFromPath( $source );
120 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
121 $this->assertEquals( $props1, $props2,
122 "Source and destination have the same props ($backendName)." );
123 }
124
125 public function provider_testStore() {
126 $cases = array();
127
128 $tmpName = TempFSFile::factory( "unittests_", 'txt' )->getPath();
129 $toPath = $this->baseStorePath() . '/unittest-cont1/fun/obj1.txt';
130 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
131 $cases[] = array(
132 $op, // operation
133 $tmpName, // source
134 $toPath, // dest
135 );
136
137 $op2 = $op;
138 $op2['overwrite'] = true;
139 $cases[] = array(
140 $op2, // operation
141 $tmpName, // source
142 $toPath, // dest
143 );
144
145 $op2 = $op;
146 $op2['overwriteSame'] = true;
147 $cases[] = array(
148 $op2, // operation
149 $tmpName, // source
150 $toPath, // dest
151 );
152
153 return $cases;
154 }
155
156 /**
157 * @dataProvider provider_testCopy
158 */
159 public function testCopy( $op, $source, $dest ) {
160 $this->backend = $this->singleBackend;
161 $this->tearDownFiles();
162 $this->doTestCopy( $op, $source, $dest );
163 $this->tearDownFiles();
164
165 $this->backend = $this->multiBackend;
166 $this->tearDownFiles();
167 $this->doTestCopy( $op, $source, $dest );
168 $this->tearDownFiles();
169 }
170
171 function doTestCopy( $op, $source, $dest ) {
172 $backendName = $this->backendClass();
173
174 $this->backend->prepare( array( 'dir' => dirname( $source ) ) );
175 $this->backend->prepare( array( 'dir' => dirname( $dest ) ) );
176
177 $status = $this->backend->doOperation(
178 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
179 $this->assertEquals( array(), $status->errors,
180 "Creation of file at $source succeeded ($backendName)." );
181
182 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
183 $this->backend->copy( $op );
184 }
185
186 $status = $this->backend->doOperation( $op );
187
188 $this->assertEquals( array(), $status->errors,
189 "Copy from $source to $dest succeeded without warnings ($backendName)." );
190 $this->assertEquals( true, $status->isOK(),
191 "Copy from $source to $dest succeeded ($backendName)." );
192 $this->assertEquals( array( 0 => true ), $status->success,
193 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
194 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $source ) ),
195 "Source file $source still exists ($backendName)." );
196 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
197 "Destination file $dest exists after copy ($backendName)." );
198
199 $this->assertEquals(
200 $this->backend->getFileSize( array( 'src' => $source ) ),
201 $this->backend->getFileSize( array( 'src' => $dest ) ),
202 "Destination file $dest has correct size ($backendName)." );
203
204 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
205 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
206 $this->assertEquals( $props1, $props2,
207 "Source and destination have the same props ($backendName)." );
208 }
209
210 public function provider_testCopy() {
211 $cases = array();
212
213 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
214 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
215
216 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
217 $cases[] = array(
218 $op, // operation
219 $source, // source
220 $dest, // dest
221 );
222
223 $op2 = $op;
224 $op2['overwrite'] = true;
225 $cases[] = array(
226 $op2, // operation
227 $source, // source
228 $dest, // dest
229 );
230
231 $op2 = $op;
232 $op2['overwriteSame'] = true;
233 $cases[] = array(
234 $op2, // operation
235 $source, // source
236 $dest, // dest
237 );
238
239 return $cases;
240 }
241
242 /**
243 * @dataProvider provider_testMove
244 */
245 public function testMove( $op, $source, $dest ) {
246 $this->backend = $this->singleBackend;
247 $this->tearDownFiles();
248 $this->doTestMove( $op, $source, $dest );
249 $this->tearDownFiles();
250
251 $this->backend = $this->multiBackend;
252 $this->tearDownFiles();
253 $this->doTestMove( $op, $source, $dest );
254 $this->tearDownFiles();
255 }
256
257 private function doTestMove( $op, $source, $dest ) {
258 $backendName = $this->backendClass();
259
260 $this->backend->prepare( array( 'dir' => dirname( $source ) ) );
261 $this->backend->prepare( array( 'dir' => dirname( $dest ) ) );
262
263 $status = $this->backend->doOperation(
264 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
265 $this->assertEquals( array(), $status->errors,
266 "Creation of file at $source succeeded ($backendName)." );
267
268 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
269 $this->backend->copy( $op );
270 }
271
272 $status = $this->backend->doOperation( $op );
273 $this->assertEquals( array(), $status->errors,
274 "Move from $source to $dest succeeded without warnings ($backendName)." );
275 $this->assertEquals( true, $status->isOK(),
276 "Move from $source to $dest succeeded ($backendName)." );
277 $this->assertEquals( array( 0 => true ), $status->success,
278 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
279 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
280 "Source file $source does not still exists ($backendName)." );
281 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
282 "Destination file $dest exists after move ($backendName)." );
283
284 $this->assertNotEquals(
285 $this->backend->getFileSize( array( 'src' => $source ) ),
286 $this->backend->getFileSize( array( 'src' => $dest ) ),
287 "Destination file $dest has correct size ($backendName)." );
288
289 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
290 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
291 $this->assertEquals( false, $props1['fileExists'],
292 "Source file does not exist accourding to props ($backendName)." );
293 $this->assertEquals( true, $props2['fileExists'],
294 "Destination file exists accourding to props ($backendName)." );
295 }
296
297 public function provider_testMove() {
298 $cases = array();
299
300 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
301 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
302
303 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
304 $cases[] = array(
305 $op, // operation
306 $source, // source
307 $dest, // dest
308 );
309
310 $op2 = $op;
311 $op2['overwrite'] = true;
312 $cases[] = array(
313 $op2, // operation
314 $source, // source
315 $dest, // dest
316 );
317
318 $op2 = $op;
319 $op2['overwriteSame'] = true;
320 $cases[] = array(
321 $op2, // operation
322 $source, // source
323 $dest, // dest
324 );
325
326 return $cases;
327 }
328
329 /**
330 * @dataProvider provider_testDelete
331 */
332 public function testDelete( $op, $source, $withSource, $okStatus ) {
333 $this->backend = $this->singleBackend;
334 $this->tearDownFiles();
335 $this->doTestDelete( $op, $source, $withSource, $okStatus );
336 $this->tearDownFiles();
337
338 $this->backend = $this->multiBackend;
339 $this->tearDownFiles();
340 $this->doTestDelete( $op, $source, $withSource, $okStatus );
341 $this->tearDownFiles();
342 }
343
344 private function doTestDelete( $op, $source, $withSource, $okStatus ) {
345 $backendName = $this->backendClass();
346
347 $this->backend->prepare( array( 'dir' => dirname( $source ) ) );
348
349 if ( $withSource ) {
350 $status = $this->backend->doOperation(
351 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
352 $this->assertEquals( array(), $status->errors,
353 "Creation of file at $source succeeded ($backendName)." );
354 }
355
356 $status = $this->backend->doOperation( $op );
357 if ( $okStatus ) {
358 $this->assertEquals( array(), $status->errors,
359 "Deletion of file at $source succeeded without warnings ($backendName)." );
360 $this->assertEquals( true, $status->isOK(),
361 "Deletion of file at $source succeeded ($backendName)." );
362 $this->assertEquals( array( 0 => true ), $status->success,
363 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
364 } else {
365 $this->assertEquals( false, $status->isOK(),
366 "Deletion of file at $source failed ($backendName)." );
367 }
368
369 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
370 "Source file $source does not exist after move ($backendName)." );
371
372 $this->assertFalse(
373 $this->backend->getFileSize( array( 'src' => $source ) ),
374 "Source file $source has correct size (false) ($backendName)." );
375
376 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
377 $this->assertFalse( $props1['fileExists'],
378 "Source file $source does not exist according to props ($backendName)." );
379 }
380
381 public function provider_testDelete() {
382 $cases = array();
383
384 $source = $this->baseStorePath() . '/unittest-cont1/myfacefile.txt';
385
386 $op = array( 'op' => 'delete', 'src' => $source );
387 $cases[] = array(
388 $op, // operation
389 $source, // source
390 true, // with source
391 true // succeeds
392 );
393
394 $cases[] = array(
395 $op, // operation
396 $source, // source
397 false, // without source
398 false // fails
399 );
400
401 $op['ignoreMissingSource'] = true;
402 $cases[] = array(
403 $op, // operation
404 $source, // source
405 false, // without source
406 true // succeeds
407 );
408
409 return $cases;
410 }
411
412 /**
413 * @dataProvider provider_testCreate
414 */
415 public function testCreate( $op, $dest, $alreadyExists, $okStatus, $newSize ) {
416 $this->backend = $this->singleBackend;
417 $this->tearDownFiles();
418 $this->doTestCreate( $op, $dest, $alreadyExists, $okStatus, $newSize );
419 $this->tearDownFiles();
420
421 $this->backend = $this->multiBackend;
422 $this->tearDownFiles();
423 $this->doTestCreate( $op, $dest, $alreadyExists, $okStatus, $newSize );
424 $this->tearDownFiles();
425 }
426
427 private function doTestCreate( $op, $dest, $alreadyExists, $okStatus, $newSize ) {
428 $backendName = $this->backendClass();
429
430 $this->backend->prepare( array( 'dir' => dirname( $dest ) ) );
431
432 $oldText = 'blah...blah...waahwaah';
433 if ( $alreadyExists ) {
434 $status = $this->backend->doOperation(
435 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
436 $this->assertEquals( array(), $status->errors,
437 "Creation of file at $dest succeeded ($backendName)." );
438 }
439
440 $status = $this->backend->doOperation( $op );
441 if ( $okStatus ) {
442 $this->assertEquals( array(), $status->errors,
443 "Creation of file at $dest succeeded without warnings ($backendName)." );
444 $this->assertEquals( true, $status->isOK(),
445 "Creation of file at $dest succeeded ($backendName)." );
446 $this->assertEquals( array( 0 => true ), $status->success,
447 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
448 } else {
449 $this->assertEquals( false, $status->isOK(),
450 "Creation of file at $dest failed ($backendName)." );
451 }
452
453 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
454 "Destination file $dest exists after creation ($backendName)." );
455
456 $props1 = $this->backend->getFileProps( array( 'src' => $dest ) );
457 $this->assertEquals( true, $props1['fileExists'],
458 "Destination file $dest exists according to props ($backendName)." );
459 if ( $okStatus ) { // file content is what we saved
460 $this->assertEquals( $newSize, $props1['size'],
461 "Destination file $dest has expected size according to props ($backendName)." );
462 $this->assertEquals( $newSize,
463 $this->backend->getFileSize( array( 'src' => $dest ) ),
464 "Destination file $dest has correct size ($backendName)." );
465 } else { // file content is some other previous text
466 $this->assertEquals( strlen( $oldText ), $props1['size'],
467 "Destination file $dest has original size according to props ($backendName)." );
468 $this->assertEquals( strlen( $oldText ),
469 $this->backend->getFileSize( array( 'src' => $dest ) ),
470 "Destination file $dest has original size according to props ($backendName)." );
471 }
472 }
473
474 /**
475 * @dataProvider provider_testCreate
476 */
477 public function provider_testCreate() {
478 $cases = array();
479
480 $source = $this->baseStorePath() . '/unittest-cont2/myspacefile.txt';
481
482 $dummyText = 'hey hey';
483 $op = array( 'op' => 'create', 'content' => $dummyText, 'dst' => $source );
484 $cases[] = array(
485 $op, // operation
486 $source, // source
487 false, // no dest already exists
488 true, // succeeds
489 strlen( $dummyText )
490 );
491
492 $cases[] = array(
493 $op, // operation
494 $source, // source
495 true, // dest already exists
496 false, // fails
497 strlen( $dummyText )
498 );
499
500 $op2 = $op;
501 $op2['overwrite'] = true;
502 $cases[] = array(
503 $op2, // operation
504 $source, // source
505 true, // dest already exists
506 true, // succeeds
507 strlen( $dummyText )
508 );
509
510 $op2 = $op;
511 $op2['overwriteSame'] = true;
512 $cases[] = array(
513 $op2, // operation
514 $source, // source
515 true, // dest already exists
516 false, // succeeds
517 strlen( $dummyText )
518 );
519
520 return $cases;
521 }
522
523 /**
524 * @dataProvider provider_testConcatenate
525 */
526 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
527 $this->filesToPrune[] = $op['dst'];
528
529 $this->backend = $this->singleBackend;
530 $this->tearDownFiles();
531 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
532 $this->tearDownFiles();
533
534 $this->backend = $this->multiBackend;
535 $this->tearDownFiles();
536 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
537 $this->tearDownFiles();
538 }
539
540 public function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
541 $backendName = $this->backendClass();
542
543 $expContent = '';
544 // Create sources
545 $ops = array();
546 foreach ( $srcs as $i => $source ) {
547 $this->backend->prepare( array( 'dir' => dirname( $source ) ) );
548 $ops[] = array(
549 'op' => 'create', // operation
550 'dst' => $source, // source
551 'content' => $srcsContent[$i]
552 );
553 $expContent .= $srcsContent[$i];
554 }
555 $status = $this->backend->doOperations( $ops );
556
557 $this->assertEquals( array(), $status->errors,
558 "Creation of source files succeeded ($backendName)." );
559
560 $dest = $params['dst'];
561 if ( $alreadyExists ) {
562 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
563 $this->assertEquals( true, $ok,
564 "Creation of file at $dest succeeded ($backendName)." );
565 } else {
566 $ok = file_put_contents( $dest, '' ) !== false;
567 $this->assertEquals( true, $ok,
568 "Creation of 0-byte file at $dest succeeded ($backendName)." );
569 }
570
571 // Combine the files into one
572 $status = $this->backend->concatenate( $params );
573 if ( $okStatus ) {
574 $this->assertEquals( array(), $status->errors,
575 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
576 $this->assertEquals( true, $status->isOK(),
577 "Creation of concat file at $dest succeeded ($backendName)." );
578 } else {
579 $this->assertEquals( false, $status->isOK(),
580 "Creation of concat file at $dest failed ($backendName)." );
581 }
582
583 if ( $okStatus ) {
584 $this->assertEquals( true, is_file( $dest ),
585 "Dest concat file $dest exists after creation ($backendName)." );
586 } else {
587 $this->assertEquals( true, is_file( $dest ),
588 "Dest concat file $dest exists after failed creation ($backendName)." );
589 }
590
591 $contents = file_get_contents( $dest );
592 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
593
594 if ( $okStatus ) {
595 $this->assertEquals( $expContent, $contents,
596 "Concat file at $dest has correct contents ($backendName)." );
597 } else {
598 $this->assertNotEquals( $expContent, $contents,
599 "Concat file at $dest has correct contents ($backendName)." );
600 }
601 }
602
603 function provider_testConcatenate() {
604 $cases = array();
605
606 $rand = mt_rand( 0, 2000000000 ) . time();
607 $dest = wfTempDir() . "/randomfile!$rand.txt";
608 $srcs = array(
609 $this->baseStorePath() . '/unittest-cont1/file1.txt',
610 $this->baseStorePath() . '/unittest-cont1/file2.txt',
611 $this->baseStorePath() . '/unittest-cont1/file3.txt',
612 $this->baseStorePath() . '/unittest-cont1/file4.txt',
613 $this->baseStorePath() . '/unittest-cont1/file5.txt',
614 $this->baseStorePath() . '/unittest-cont1/file6.txt',
615 $this->baseStorePath() . '/unittest-cont1/file7.txt',
616 $this->baseStorePath() . '/unittest-cont1/file8.txt',
617 $this->baseStorePath() . '/unittest-cont1/file9.txt',
618 $this->baseStorePath() . '/unittest-cont1/file10.txt'
619 );
620 $content = array(
621 'egfage',
622 'ageageag',
623 'rhokohlr',
624 'shgmslkg',
625 'kenga',
626 'owagmal',
627 'kgmae',
628 'g eak;g',
629 'lkaem;a',
630 'legma'
631 );
632 $params = array( 'srcs' => $srcs, 'dst' => $dest );
633
634 $cases[] = array(
635 $params, // operation
636 $srcs, // sources
637 $content, // content for each source
638 false, // no dest already exists
639 true, // succeeds
640 );
641
642 $cases[] = array(
643 $params, // operation
644 $srcs, // sources
645 $content, // content for each source
646 true, // dest already exists
647 false, // succeeds
648 );
649
650 return $cases;
651 }
652
653 /**
654 * @dataProvider provider_testGetFileContents
655 */
656 public function testGetFileContents( $src, $content ) {
657 $this->backend = $this->singleBackend;
658 $this->tearDownFiles();
659 $this->doTestGetFileContents( $src, $content );
660 $this->tearDownFiles();
661
662 $this->backend = $this->multiBackend;
663 $this->tearDownFiles();
664 $this->doTestGetFileContents( $src, $content );
665 $this->tearDownFiles();
666 }
667
668 /**
669 * @dataProvider provider_testGetFileContents
670 */
671 public function doTestGetFileContents( $source, $content ) {
672 $backendName = $this->backendClass();
673
674 $this->backend->prepare( array( 'dir' => dirname( $source ) ) );
675
676 $status = $this->backend->doOperation(
677 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
678 $this->assertEquals( array(), $status->errors,
679 "Creation of file at $source succeeded ($backendName)." );
680 $this->assertEquals( true, $status->isOK(),
681 "Creation of file at $source succeeded with OK status ($backendName)." );
682
683 $newContents = $this->backend->getFileContents( array( 'src' => $source ) );
684 $this->assertNotEquals( false, $newContents,
685 "Read of file at $source succeeded ($backendName)." );
686
687 $this->assertEquals( $content, $newContents,
688 "Contents read match data at $source ($backendName)." );
689 }
690
691 function provider_testGetFileContents() {
692 $cases = array();
693
694 $base = $this->baseStorePath();
695 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents" );
696 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "more file contents" );
697
698 return $cases;
699 }
700
701 /**
702 * @dataProvider provider_testGetLocalCopy
703 */
704 public function testGetLocalCopy( $src, $content ) {
705 $this->backend = $this->singleBackend;
706 $this->tearDownFiles();
707 $this->doTestGetLocalCopy( $src, $content );
708 $this->tearDownFiles();
709
710 $this->backend = $this->multiBackend;
711 $this->tearDownFiles();
712 $this->doTestGetLocalCopy( $src, $content );
713 $this->tearDownFiles();
714 }
715
716 public function doTestGetLocalCopy( $source, $content ) {
717 $backendName = $this->backendClass();
718
719 $this->backend->prepare( array( 'dir' => dirname( $source ) ) );
720
721 $status = $this->backend->doOperation(
722 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
723 $this->assertEquals( array(), $status->errors,
724 "Creation of file at $source succeeded ($backendName)." );
725
726 $tmpFile = $this->backend->getLocalCopy( array( 'src' => $source ) );
727 $this->assertNotNull( $tmpFile,
728 "Creation of local copy of $source succeeded ($backendName)." );
729
730 $contents = file_get_contents( $tmpFile->getPath() );
731 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
732 }
733
734 function provider_testGetLocalCopy() {
735 $cases = array();
736
737 $base = $this->baseStorePath();
738 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
739 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
740
741 return $cases;
742 }
743
744 /**
745 * @dataProvider provider_testGetLocalReference
746 */
747 public function testGetLocalReference( $src, $content ) {
748 $this->backend = $this->singleBackend;
749 $this->tearDownFiles();
750 $this->doTestGetLocalReference( $src, $content );
751 $this->tearDownFiles();
752
753 $this->backend = $this->multiBackend;
754 $this->tearDownFiles();
755 $this->doTestGetLocalReference( $src, $content );
756 $this->tearDownFiles();
757 }
758
759 private function doTestGetLocalReference( $source, $content ) {
760 $backendName = $this->backendClass();
761
762 $this->backend->prepare( array( 'dir' => dirname( $source ) ) );
763
764 $status = $this->backend->doOperation(
765 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
766 $this->assertEquals( array(), $status->errors,
767 "Creation of file at $source succeeded ($backendName)." );
768
769 $tmpFile = $this->backend->getLocalReference( array( 'src' => $source ) );
770 $this->assertNotNull( $tmpFile,
771 "Creation of local copy of $source succeeded ($backendName)." );
772
773 $contents = file_get_contents( $tmpFile->getPath() );
774 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
775 }
776
777 function provider_testGetLocalReference() {
778 $cases = array();
779
780 $base = $this->baseStorePath();
781 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
782 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
783
784 return $cases;
785 }
786
787 /**
788 * @dataProvider provider_testPrepareAndClean
789 */
790 public function testPrepareAndClean( $path, $isOK ) {
791 $this->backend = $this->singleBackend;
792 $this->doTestPrepareAndClean( $path, $isOK );
793
794 $this->backend = $this->multiBackend;
795 $this->doTestPrepareAndClean( $path, $isOK );
796 }
797
798 function provider_testPrepareAndClean() {
799 $base = $this->baseStorePath();
800 return array(
801 array( "$base/unittest-cont1/a/z/some_file1.txt", true ),
802 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
803 # Specific to FS backend with no basePath field set
804 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
805 );
806 }
807
808 function doTestPrepareAndClean( $path, $isOK ) {
809 $backendName = $this->backendClass();
810
811 $status = $this->backend->prepare( array( 'dir' => $path ) );
812 if ( $isOK ) {
813 $this->assertEquals( array(), $status->errors,
814 "Preparing dir $path succeeded without warnings ($backendName)." );
815 $this->assertEquals( true, $status->isOK(),
816 "Preparing dir $path succeeded ($backendName)." );
817 } else {
818 $this->assertEquals( false, $status->isOK(),
819 "Preparing dir $path failed ($backendName)." );
820 }
821
822 $status = $this->backend->clean( array( 'dir' => $path ) );
823 if ( $isOK ) {
824 $this->assertEquals( array(), $status->errors,
825 "Cleaning dir $path succeeded without warnings ($backendName)." );
826 $this->assertEquals( true, $status->isOK(),
827 "Cleaning dir $path succeeded ($backendName)." );
828 } else {
829 $this->assertEquals( false, $status->isOK(),
830 "Cleaning dir $path failed ($backendName)." );
831 }
832 }
833
834 // @TODO: testSecure
835
836 public function testDoOperations() {
837 $this->backend = $this->singleBackend;
838 $this->doTestDoOperations();
839
840 $this->backend = $this->multiBackend;
841 $this->doTestDoOperations();
842 }
843
844 function doTestDoOperations() {
845 $base = $this->baseStorePath();
846
847 $fileA = "$base/unittest-cont1/a/b/fileA.txt";
848 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
849 $fileB = "$base/unittest-cont1/a/b/fileB.txt";
850 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
851 $fileC = "$base/unittest-cont1/a/b/fileC.txt";
852 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
853 $fileD = "$base/unittest-cont1/a/b/fileD.txt";
854
855 $this->backend->prepare( array( 'dir' => dirname( $fileA ) ) );
856 $this->backend->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
857 $this->backend->prepare( array( 'dir' => dirname( $fileB ) ) );
858 $this->backend->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
859 $this->backend->prepare( array( 'dir' => dirname( $fileC ) ) );
860 $this->backend->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
861
862 $status = $this->backend->doOperations( array(
863 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
864 // Now: A:<A>, B:<B>, C:<A>, D:<D> (file:<orginal contents>)
865 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
866 // Now: A:<A>, B:<B>, C:<A>, D:<D>
867 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
868 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
869 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
870 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
871 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
872 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
873 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
874 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
875 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
876 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
877 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
878 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
879 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
880 // Does nothing
881 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
882 // Does nothing
883 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
884 // Does nothing
885 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
886 // Does nothing
887 array( 'op' => 'null' ),
888 // Does nothing
889 ) );
890
891 $this->assertEquals( array(), $status->errors, "Operation batch succeeded" );
892 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
893 $this->assertEquals( 13, count( $status->success ),
894 "Operation batch has correct success array" );
895
896 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
897 "File does not exist at $fileA" );
898 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
899 "File does not exist at $fileB" );
900 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
901 "File does not exist at $fileD" );
902
903 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
904 "File exists at $fileC" );
905 $this->assertEquals( $fileBContents,
906 $this->backend->getFileContents( array( 'src' => $fileC ) ),
907 "Correct file contents of $fileC" );
908 $this->assertEquals( strlen( $fileBContents ),
909 $this->backend->getFileSize( array( 'src' => $fileC ) ),
910 "Correct file size of $fileC" );
911 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
912 $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
913 "Correct file SHA-1 of $fileC" );
914
915 // @TODO: test some cases where the ops should fail
916 }
917
918 public function testGetFileList() {
919 $this->backend = $this->singleBackend;
920 $this->tearDownFiles();
921 $this->doTestGetFileList();
922 $this->tearDownFiles();
923
924 $this->backend = $this->multiBackend;
925 $this->tearDownFiles();
926 $this->doTestGetFileList();
927 $this->tearDownFiles();
928 }
929
930 private function doTestGetFileList() {
931 $backendName = $this->backendClass();
932
933 $base = $this->baseStorePath();
934 $files = array(
935 "$base/unittest-cont1/test1.txt",
936 "$base/unittest-cont1/test2.txt",
937 "$base/unittest-cont1/test3.txt",
938 "$base/unittest-cont1/subdir1/test1.txt",
939 "$base/unittest-cont1/subdir1/test2.txt",
940 "$base/unittest-cont1/subdir2/test3.txt",
941 "$base/unittest-cont1/subdir2/test4.txt",
942 "$base/unittest-cont1/subdir2/subdir/test1.txt",
943 "$base/unittest-cont1/subdir2/subdir/test2.txt",
944 "$base/unittest-cont1/subdir2/subdir/test3.txt",
945 "$base/unittest-cont1/subdir2/subdir/test4.txt",
946 "$base/unittest-cont1/subdir2/subdir/test5.txt",
947 "$base/unittest-cont1/subdir2/subdir/sub/test0.txt",
948 "$base/unittest-cont1/subdir2/subdir/sub/120-px-file.txt",
949 );
950
951 // Add the files
952 $ops = array();
953 foreach ( $files as $file ) {
954 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
955 $this->backend->prepare( array( 'dir' => dirname( $file ) ) );
956 }
957 $status = $this->backend->doOperations( $ops );
958 $this->assertEquals( array(), $status->errors,
959 "Creation of files succeeded ($backendName)." );
960 $this->assertEquals( true, $status->isOK(),
961 "Creation of files succeeded with OK status ($backendName)." );
962
963 // Expected listing
964 $expected = array(
965 "test1.txt",
966 "test2.txt",
967 "test3.txt",
968 "subdir1/test1.txt",
969 "subdir1/test2.txt",
970 "subdir2/test3.txt",
971 "subdir2/test4.txt",
972 "subdir2/subdir/test1.txt",
973 "subdir2/subdir/test2.txt",
974 "subdir2/subdir/test3.txt",
975 "subdir2/subdir/test4.txt",
976 "subdir2/subdir/test5.txt",
977 "subdir2/subdir/sub/test0.txt",
978 "subdir2/subdir/sub/120-px-file.txt",
979 );
980 sort( $expected );
981
982 // Actual listing (no trailing slash)
983 $list = array();
984 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
985 foreach ( $iter as $file ) {
986 $list[] = $file;
987 }
988 sort( $list );
989
990 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
991
992 // Actual listing (with trailing slash)
993 $list = array();
994 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
995 foreach ( $iter as $file ) {
996 $list[] = $file;
997 }
998 sort( $list );
999
1000 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1001
1002 // Expected listing
1003 $expected = array(
1004 "test1.txt",
1005 "test2.txt",
1006 "test3.txt",
1007 "test4.txt",
1008 "test5.txt",
1009 "sub/test0.txt",
1010 "sub/120-px-file.txt",
1011 );
1012 sort( $expected );
1013
1014 // Actual listing (no trailing slash)
1015 $list = array();
1016 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) );
1017 foreach ( $iter as $file ) {
1018 $list[] = $file;
1019 }
1020 sort( $list );
1021
1022 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1023
1024 // Actual listing (with trailing slash)
1025 $list = array();
1026 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir/" ) );
1027 foreach ( $iter as $file ) {
1028 $list[] = $file;
1029 }
1030 sort( $list );
1031
1032 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1033
1034 foreach ( $files as $file ) {
1035 $this->backend->doOperation( array( 'op' => 'delete', 'src' => "$base/$file" ) );
1036 }
1037
1038 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1039 foreach ( $iter as $iter ) {} // no errors
1040 }
1041
1042 function tearDownFiles() {
1043 foreach ( $this->filesToPrune as $file ) {
1044 @unlink( $file );
1045 }
1046 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1047 foreach ( $containers as $container ) {
1048 $this->deleteFiles( $this->backend, $container );
1049 }
1050 }
1051
1052 private function deleteFiles( $backend, $container ) {
1053 $base = $this->baseStorePath();
1054 $iter = $backend->getFileList( array( 'dir' => "$base/$container" ) );
1055 if ( $iter ) {
1056 foreach ( $iter as $file ) {
1057 $backend->delete( array( 'src' => "$base/$container/$file", 'ignoreMissingSource' => 1 ) );
1058 $tmp = $file;
1059 while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
1060 $backend->clean( array( 'dir' => $tmp ) );
1061 }
1062 }
1063 }
1064 }
1065
1066 function tearDown() {
1067 parent::tearDown();
1068 }
1069 }