141ccd342844329e3a9b4bbe89b663286f18d7a8
[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, $alreadyExists, $okStatus, $newSize ) {
416 $this->backend = $this->singleBackend;
417 $this->tearDownFiles();
418 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
419 $this->tearDownFiles();
420
421 $this->backend = $this->multiBackend;
422 $this->tearDownFiles();
423 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
424 $this->tearDownFiles();
425 }
426
427 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
428 $backendName = $this->backendClass();
429
430 $dest = $op['dst'];
431 $this->backend->prepare( array( 'dir' => dirname( $dest ) ) );
432
433 $oldText = 'blah...blah...waahwaah';
434 if ( $alreadyExists ) {
435 $status = $this->backend->doOperation(
436 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
437 $this->assertEquals( array(), $status->errors,
438 "Creation of file at $dest succeeded ($backendName)." );
439 }
440
441 $status = $this->backend->doOperation( $op );
442 if ( $okStatus ) {
443 $this->assertEquals( array(), $status->errors,
444 "Creation of file at $dest succeeded without warnings ($backendName)." );
445 $this->assertEquals( true, $status->isOK(),
446 "Creation of file at $dest succeeded ($backendName)." );
447 $this->assertEquals( array( 0 => true ), $status->success,
448 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
449 } else {
450 $this->assertEquals( false, $status->isOK(),
451 "Creation of file at $dest failed ($backendName)." );
452 }
453
454 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
455 "Destination file $dest exists after creation ($backendName)." );
456
457 $props1 = $this->backend->getFileProps( array( 'src' => $dest ) );
458 $this->assertEquals( true, $props1['fileExists'],
459 "Destination file $dest exists according to props ($backendName)." );
460 if ( $okStatus ) { // file content is what we saved
461 $this->assertEquals( $newSize, $props1['size'],
462 "Destination file $dest has expected size according to props ($backendName)." );
463 $this->assertEquals( $newSize,
464 $this->backend->getFileSize( array( 'src' => $dest ) ),
465 "Destination file $dest has correct size ($backendName)." );
466 } else { // file content is some other previous text
467 $this->assertEquals( strlen( $oldText ), $props1['size'],
468 "Destination file $dest has original size according to props ($backendName)." );
469 $this->assertEquals( strlen( $oldText ),
470 $this->backend->getFileSize( array( 'src' => $dest ) ),
471 "Destination file $dest has original size according to props ($backendName)." );
472 }
473 }
474
475 /**
476 * @dataProvider provider_testCreate
477 */
478 public function provider_testCreate() {
479 $cases = array();
480
481 $dest = $this->baseStorePath() . '/unittest-cont2/myspacefile.txt';
482
483 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
484 $cases[] = array(
485 $op, // operation
486 false, // no dest already exists
487 true, // succeeds
488 strlen( $op['content'] )
489 );
490
491 $op2 = $op;
492 $op2['content'] = "\n";
493 $cases[] = array(
494 $op2, // operation
495 false, // no dest already exists
496 true, // succeeds
497 strlen( $op2['content'] )
498 );
499
500 $op2 = $op;
501 $op2['content'] = "fsf\n waf 3kt";
502 $cases[] = array(
503 $op2, // operation
504 true, // dest already exists
505 false, // fails
506 strlen( $op2['content'] )
507 );
508
509 $op2 = $op;
510 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
511 $op2['overwrite'] = true;
512 $cases[] = array(
513 $op2, // operation
514 true, // dest already exists
515 true, // succeeds
516 strlen( $op2['content'] )
517 );
518
519 $op2 = $op;
520 $op2['content'] = "39qjmg3-qg";
521 $op2['overwriteSame'] = true;
522 $cases[] = array(
523 $op2, // operation
524 true, // dest already exists
525 false, // succeeds
526 strlen( $op2['content'] )
527 );
528
529 return $cases;
530 }
531
532 /**
533 * @dataProvider provider_testConcatenate
534 */
535 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
536 $this->filesToPrune[] = $op['dst'];
537
538 $this->backend = $this->singleBackend;
539 $this->tearDownFiles();
540 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
541 $this->tearDownFiles();
542
543 $this->backend = $this->multiBackend;
544 $this->tearDownFiles();
545 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
546 $this->tearDownFiles();
547 }
548
549 public function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
550 $backendName = $this->backendClass();
551
552 $expContent = '';
553 // Create sources
554 $ops = array();
555 foreach ( $srcs as $i => $source ) {
556 $this->backend->prepare( array( 'dir' => dirname( $source ) ) );
557 $ops[] = array(
558 'op' => 'create', // operation
559 'dst' => $source, // source
560 'content' => $srcsContent[$i]
561 );
562 $expContent .= $srcsContent[$i];
563 }
564 $status = $this->backend->doOperations( $ops );
565
566 $this->assertEquals( array(), $status->errors,
567 "Creation of source files succeeded ($backendName)." );
568
569 $dest = $params['dst'];
570 if ( $alreadyExists ) {
571 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
572 $this->assertEquals( true, $ok,
573 "Creation of file at $dest succeeded ($backendName)." );
574 } else {
575 $ok = file_put_contents( $dest, '' ) !== false;
576 $this->assertEquals( true, $ok,
577 "Creation of 0-byte file at $dest succeeded ($backendName)." );
578 }
579
580 // Combine the files into one
581 $status = $this->backend->concatenate( $params );
582 if ( $okStatus ) {
583 $this->assertEquals( array(), $status->errors,
584 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
585 $this->assertEquals( true, $status->isOK(),
586 "Creation of concat file at $dest succeeded ($backendName)." );
587 } else {
588 $this->assertEquals( false, $status->isOK(),
589 "Creation of concat file at $dest failed ($backendName)." );
590 }
591
592 if ( $okStatus ) {
593 $this->assertEquals( true, is_file( $dest ),
594 "Dest concat file $dest exists after creation ($backendName)." );
595 } else {
596 $this->assertEquals( true, is_file( $dest ),
597 "Dest concat file $dest exists after failed creation ($backendName)." );
598 }
599
600 $contents = file_get_contents( $dest );
601 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
602
603 if ( $okStatus ) {
604 $this->assertEquals( $expContent, $contents,
605 "Concat file at $dest has correct contents ($backendName)." );
606 } else {
607 $this->assertNotEquals( $expContent, $contents,
608 "Concat file at $dest has correct contents ($backendName)." );
609 }
610 }
611
612 function provider_testConcatenate() {
613 $cases = array();
614
615 $rand = mt_rand( 0, 2000000000 ) . time();
616 $dest = wfTempDir() . "/randomfile!$rand.txt";
617 $srcs = array(
618 $this->baseStorePath() . '/unittest-cont1/file1.txt',
619 $this->baseStorePath() . '/unittest-cont1/file2.txt',
620 $this->baseStorePath() . '/unittest-cont1/file3.txt',
621 $this->baseStorePath() . '/unittest-cont1/file4.txt',
622 $this->baseStorePath() . '/unittest-cont1/file5.txt',
623 $this->baseStorePath() . '/unittest-cont1/file6.txt',
624 $this->baseStorePath() . '/unittest-cont1/file7.txt',
625 $this->baseStorePath() . '/unittest-cont1/file8.txt',
626 $this->baseStorePath() . '/unittest-cont1/file9.txt',
627 $this->baseStorePath() . '/unittest-cont1/file10.txt'
628 );
629 $content = array(
630 'egfage',
631 'ageageag',
632 'rhokohlr',
633 'shgmslkg',
634 'kenga',
635 'owagmal',
636 'kgmae',
637 'g eak;g',
638 'lkaem;a',
639 'legma'
640 );
641 $params = array( 'srcs' => $srcs, 'dst' => $dest );
642
643 $cases[] = array(
644 $params, // operation
645 $srcs, // sources
646 $content, // content for each source
647 false, // no dest already exists
648 true, // succeeds
649 );
650
651 $cases[] = array(
652 $params, // operation
653 $srcs, // sources
654 $content, // content for each source
655 true, // dest already exists
656 false, // succeeds
657 );
658
659 return $cases;
660 }
661
662 /**
663 * @dataProvider provider_testGetFileContents
664 */
665 public function testGetFileContents( $src, $content ) {
666 $this->backend = $this->singleBackend;
667 $this->tearDownFiles();
668 $this->doTestGetFileContents( $src, $content );
669 $this->tearDownFiles();
670
671 $this->backend = $this->multiBackend;
672 $this->tearDownFiles();
673 $this->doTestGetFileContents( $src, $content );
674 $this->tearDownFiles();
675 }
676
677 /**
678 * @dataProvider provider_testGetFileContents
679 */
680 public function doTestGetFileContents( $source, $content ) {
681 $backendName = $this->backendClass();
682
683 $this->backend->prepare( array( 'dir' => dirname( $source ) ) );
684
685 $status = $this->backend->doOperation(
686 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
687 $this->assertEquals( array(), $status->errors,
688 "Creation of file at $source succeeded ($backendName)." );
689 $this->assertEquals( true, $status->isOK(),
690 "Creation of file at $source succeeded with OK status ($backendName)." );
691
692 $newContents = $this->backend->getFileContents( array( 'src' => $source ) );
693 $this->assertNotEquals( false, $newContents,
694 "Read of file at $source succeeded ($backendName)." );
695
696 $this->assertEquals( $content, $newContents,
697 "Contents read match data at $source ($backendName)." );
698 }
699
700 function provider_testGetFileContents() {
701 $cases = array();
702
703 $base = $this->baseStorePath();
704 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents" );
705 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "more file contents" );
706
707 return $cases;
708 }
709
710 /**
711 * @dataProvider provider_testGetLocalCopy
712 */
713 public function testGetLocalCopy( $src, $content ) {
714 $this->backend = $this->singleBackend;
715 $this->tearDownFiles();
716 $this->doTestGetLocalCopy( $src, $content );
717 $this->tearDownFiles();
718
719 $this->backend = $this->multiBackend;
720 $this->tearDownFiles();
721 $this->doTestGetLocalCopy( $src, $content );
722 $this->tearDownFiles();
723 }
724
725 public function doTestGetLocalCopy( $source, $content ) {
726 $backendName = $this->backendClass();
727
728 $this->backend->prepare( array( 'dir' => dirname( $source ) ) );
729
730 $status = $this->backend->doOperation(
731 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
732 $this->assertEquals( array(), $status->errors,
733 "Creation of file at $source succeeded ($backendName)." );
734
735 $tmpFile = $this->backend->getLocalCopy( array( 'src' => $source ) );
736 $this->assertNotNull( $tmpFile,
737 "Creation of local copy of $source succeeded ($backendName)." );
738
739 $contents = file_get_contents( $tmpFile->getPath() );
740 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
741 }
742
743 function provider_testGetLocalCopy() {
744 $cases = array();
745
746 $base = $this->baseStorePath();
747 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
748 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
749
750 return $cases;
751 }
752
753 /**
754 * @dataProvider provider_testGetLocalReference
755 */
756 public function testGetLocalReference( $src, $content ) {
757 $this->backend = $this->singleBackend;
758 $this->tearDownFiles();
759 $this->doTestGetLocalReference( $src, $content );
760 $this->tearDownFiles();
761
762 $this->backend = $this->multiBackend;
763 $this->tearDownFiles();
764 $this->doTestGetLocalReference( $src, $content );
765 $this->tearDownFiles();
766 }
767
768 private function doTestGetLocalReference( $source, $content ) {
769 $backendName = $this->backendClass();
770
771 $this->backend->prepare( array( 'dir' => dirname( $source ) ) );
772
773 $status = $this->backend->doOperation(
774 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
775 $this->assertEquals( array(), $status->errors,
776 "Creation of file at $source succeeded ($backendName)." );
777
778 $tmpFile = $this->backend->getLocalReference( array( 'src' => $source ) );
779 $this->assertNotNull( $tmpFile,
780 "Creation of local copy of $source succeeded ($backendName)." );
781
782 $contents = file_get_contents( $tmpFile->getPath() );
783 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
784 }
785
786 function provider_testGetLocalReference() {
787 $cases = array();
788
789 $base = $this->baseStorePath();
790 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
791 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
792
793 return $cases;
794 }
795
796 /**
797 * @dataProvider provider_testPrepareAndClean
798 */
799 public function testPrepareAndClean( $path, $isOK ) {
800 $this->backend = $this->singleBackend;
801 $this->doTestPrepareAndClean( $path, $isOK );
802
803 $this->backend = $this->multiBackend;
804 $this->doTestPrepareAndClean( $path, $isOK );
805 }
806
807 function provider_testPrepareAndClean() {
808 $base = $this->baseStorePath();
809 return array(
810 array( "$base/unittest-cont1/a/z/some_file1.txt", true ),
811 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
812 # Specific to FS backend with no basePath field set
813 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
814 );
815 }
816
817 function doTestPrepareAndClean( $path, $isOK ) {
818 $backendName = $this->backendClass();
819
820 $status = $this->backend->prepare( array( 'dir' => $path ) );
821 if ( $isOK ) {
822 $this->assertEquals( array(), $status->errors,
823 "Preparing dir $path succeeded without warnings ($backendName)." );
824 $this->assertEquals( true, $status->isOK(),
825 "Preparing dir $path succeeded ($backendName)." );
826 } else {
827 $this->assertEquals( false, $status->isOK(),
828 "Preparing dir $path failed ($backendName)." );
829 }
830
831 $status = $this->backend->clean( array( 'dir' => $path ) );
832 if ( $isOK ) {
833 $this->assertEquals( array(), $status->errors,
834 "Cleaning dir $path succeeded without warnings ($backendName)." );
835 $this->assertEquals( true, $status->isOK(),
836 "Cleaning dir $path succeeded ($backendName)." );
837 } else {
838 $this->assertEquals( false, $status->isOK(),
839 "Cleaning dir $path failed ($backendName)." );
840 }
841 }
842
843 // @TODO: testSecure
844
845 public function testDoOperations() {
846 $this->backend = $this->singleBackend;
847 $this->doTestDoOperations();
848
849 $this->backend = $this->multiBackend;
850 $this->doTestDoOperations();
851 }
852
853 function doTestDoOperations() {
854 $base = $this->baseStorePath();
855
856 $fileA = "$base/unittest-cont1/a/b/fileA.txt";
857 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
858 $fileB = "$base/unittest-cont1/a/b/fileB.txt";
859 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
860 $fileC = "$base/unittest-cont1/a/b/fileC.txt";
861 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
862 $fileD = "$base/unittest-cont1/a/b/fileD.txt";
863
864 $this->backend->prepare( array( 'dir' => dirname( $fileA ) ) );
865 $this->backend->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
866 $this->backend->prepare( array( 'dir' => dirname( $fileB ) ) );
867 $this->backend->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
868 $this->backend->prepare( array( 'dir' => dirname( $fileC ) ) );
869 $this->backend->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
870
871 $status = $this->backend->doOperations( array(
872 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
873 // Now: A:<A>, B:<B>, C:<A>, D:<D> (file:<orginal contents>)
874 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
875 // Now: A:<A>, B:<B>, C:<A>, D:<D>
876 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
877 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
878 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
879 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
880 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
881 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
882 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
883 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
884 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
885 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
886 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
887 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
888 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
889 // Does nothing
890 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
891 // Does nothing
892 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
893 // Does nothing
894 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
895 // Does nothing
896 array( 'op' => 'null' ),
897 // Does nothing
898 ) );
899
900 $this->assertEquals( array(), $status->errors, "Operation batch succeeded" );
901 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
902 $this->assertEquals( 13, count( $status->success ),
903 "Operation batch has correct success array" );
904
905 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
906 "File does not exist at $fileA" );
907 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
908 "File does not exist at $fileB" );
909 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
910 "File does not exist at $fileD" );
911
912 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
913 "File exists at $fileC" );
914 $this->assertEquals( $fileBContents,
915 $this->backend->getFileContents( array( 'src' => $fileC ) ),
916 "Correct file contents of $fileC" );
917 $this->assertEquals( strlen( $fileBContents ),
918 $this->backend->getFileSize( array( 'src' => $fileC ) ),
919 "Correct file size of $fileC" );
920 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
921 $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
922 "Correct file SHA-1 of $fileC" );
923
924 // @TODO: test some cases where the ops should fail
925 }
926
927 public function testGetFileList() {
928 $this->backend = $this->singleBackend;
929 $this->tearDownFiles();
930 $this->doTestGetFileList();
931 $this->tearDownFiles();
932
933 $this->backend = $this->multiBackend;
934 $this->tearDownFiles();
935 $this->doTestGetFileList();
936 $this->tearDownFiles();
937 }
938
939 private function doTestGetFileList() {
940 $backendName = $this->backendClass();
941
942 $base = $this->baseStorePath();
943 $files = array(
944 "$base/unittest-cont1/test1.txt",
945 "$base/unittest-cont1/test2.txt",
946 "$base/unittest-cont1/test3.txt",
947 "$base/unittest-cont1/subdir1/test1.txt",
948 "$base/unittest-cont1/subdir1/test2.txt",
949 "$base/unittest-cont1/subdir2/test3.txt",
950 "$base/unittest-cont1/subdir2/test4.txt",
951 "$base/unittest-cont1/subdir2/subdir/test1.txt",
952 "$base/unittest-cont1/subdir2/subdir/test2.txt",
953 "$base/unittest-cont1/subdir2/subdir/test3.txt",
954 "$base/unittest-cont1/subdir2/subdir/test4.txt",
955 "$base/unittest-cont1/subdir2/subdir/test5.txt",
956 "$base/unittest-cont1/subdir2/subdir/sub/test0.txt",
957 "$base/unittest-cont1/subdir2/subdir/sub/120-px-file.txt",
958 );
959
960 // Add the files
961 $ops = array();
962 foreach ( $files as $file ) {
963 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
964 $this->backend->prepare( array( 'dir' => dirname( $file ) ) );
965 }
966 $status = $this->backend->doOperations( $ops );
967 $this->assertEquals( array(), $status->errors,
968 "Creation of files succeeded ($backendName)." );
969 $this->assertEquals( true, $status->isOK(),
970 "Creation of files succeeded with OK status ($backendName)." );
971
972 // Expected listing
973 $expected = array(
974 "test1.txt",
975 "test2.txt",
976 "test3.txt",
977 "subdir1/test1.txt",
978 "subdir1/test2.txt",
979 "subdir2/test3.txt",
980 "subdir2/test4.txt",
981 "subdir2/subdir/test1.txt",
982 "subdir2/subdir/test2.txt",
983 "subdir2/subdir/test3.txt",
984 "subdir2/subdir/test4.txt",
985 "subdir2/subdir/test5.txt",
986 "subdir2/subdir/sub/test0.txt",
987 "subdir2/subdir/sub/120-px-file.txt",
988 );
989 sort( $expected );
990
991 // Actual listing (no trailing slash)
992 $list = array();
993 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
994 foreach ( $iter as $file ) {
995 $list[] = $file;
996 }
997 sort( $list );
998
999 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1000
1001 // Actual listing (with trailing slash)
1002 $list = array();
1003 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1004 foreach ( $iter as $file ) {
1005 $list[] = $file;
1006 }
1007 sort( $list );
1008
1009 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1010
1011 // Expected listing
1012 $expected = array(
1013 "test1.txt",
1014 "test2.txt",
1015 "test3.txt",
1016 "test4.txt",
1017 "test5.txt",
1018 "sub/test0.txt",
1019 "sub/120-px-file.txt",
1020 );
1021 sort( $expected );
1022
1023 // Actual listing (no trailing slash)
1024 $list = array();
1025 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) );
1026 foreach ( $iter as $file ) {
1027 $list[] = $file;
1028 }
1029 sort( $list );
1030
1031 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1032
1033 // Actual listing (with trailing slash)
1034 $list = array();
1035 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir/" ) );
1036 foreach ( $iter as $file ) {
1037 $list[] = $file;
1038 }
1039 sort( $list );
1040
1041 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1042
1043 foreach ( $files as $file ) {
1044 $this->backend->doOperation( array( 'op' => 'delete', 'src' => "$base/$file" ) );
1045 }
1046
1047 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1048 foreach ( $iter as $iter ) {} // no errors
1049 }
1050
1051 function tearDownFiles() {
1052 foreach ( $this->filesToPrune as $file ) {
1053 @unlink( $file );
1054 }
1055 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1056 foreach ( $containers as $container ) {
1057 $this->deleteFiles( $this->backend, $container );
1058 }
1059 }
1060
1061 private function deleteFiles( $backend, $container ) {
1062 $base = $this->baseStorePath();
1063 $iter = $backend->getFileList( array( 'dir' => "$base/$container" ) );
1064 if ( $iter ) {
1065 foreach ( $iter as $file ) { // delete files
1066 $backend->delete( array( 'src' => "$base/$container/$file" ), array( 'force' => 1 ) );
1067 }
1068 foreach ( $iter as $file ) { // delete dirs
1069 $tmp = $file;
1070 while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
1071 $backend->clean( array( 'dir' => $tmp ) );
1072 }
1073 }
1074 }
1075 }
1076
1077 function tearDown() {
1078 parent::tearDown();
1079 }
1080 }