Cleaned up test cleanup for FileBackend and avoid use of @.
[lhc/web/wiklou.git] / tests / phpunit / includes / filebackend / FileBackendTest.php
1 <?php
2
3 /**
4 * @group FileRepo
5 * @group FileBackend
6 * @group medium
7 */
8 class FileBackendTest extends MediaWikiTestCase {
9 private $backend, $multiBackend;
10 private $filesToPrune = array();
11 private static $backendToUse;
12
13 protected function setUp() {
14 global $wgFileBackends;
15 parent::setUp();
16 $uniqueId = time() . '-' . mt_rand();
17 $tmpPrefix = wfTempDir() . '/filebackend-unittest-' . $uniqueId;
18 if ( $this->getCliArg( 'use-filebackend=' ) ) {
19 if ( self::$backendToUse ) {
20 $this->singleBackend = self::$backendToUse;
21 } else {
22 $name = $this->getCliArg( 'use-filebackend=' );
23 $useConfig = array();
24 foreach ( $wgFileBackends as $conf ) {
25 if ( $conf['name'] == $name ) {
26 $useConfig = $conf;
27 break;
28 }
29 }
30 $useConfig['name'] = 'localtesting'; // swap name
31 $useConfig['shardViaHashLevels'] = array( // test sharding
32 'unittest-cont1' => array( 'levels' => 1, 'base' => 16, 'repeat' => 1 )
33 );
34 $class = $useConfig['class'];
35 self::$backendToUse = new $class( $useConfig );
36 $this->singleBackend = self::$backendToUse;
37 }
38 } else {
39 $this->singleBackend = new FSFileBackend( array(
40 'name' => 'localtesting',
41 'lockManager' => 'fsLockManager',
42 #'parallelize' => 'implicit',
43 'wikiId' => wfWikiID() . $uniqueId,
44 'containerPaths' => array(
45 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
46 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
47 ) );
48 }
49 $this->multiBackend = new FileBackendMultiWrite( array(
50 'name' => 'localtesting',
51 'lockManager' => 'fsLockManager',
52 'parallelize' => 'implicit',
53 'wikiId' => wfWikiId() . $uniqueId,
54 'backends' => array(
55 array(
56 'name' => 'localmultitesting1',
57 'class' => 'FSFileBackend',
58 'lockManager' => 'nullLockManager',
59 'containerPaths' => array(
60 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
61 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
62 'isMultiMaster' => false
63 ),
64 array(
65 'name' => 'localmultitesting2',
66 'class' => 'FSFileBackend',
67 'lockManager' => 'nullLockManager',
68 'containerPaths' => array(
69 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
70 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
71 'isMultiMaster' => true
72 )
73 )
74 ) );
75 $this->filesToPrune = array();
76 }
77
78 private static function baseStorePath() {
79 return 'mwstore://localtesting';
80 }
81
82 private function backendClass() {
83 return get_class( $this->backend );
84 }
85
86 /**
87 * @dataProvider provider_testIsStoragePath
88 */
89 public function testIsStoragePath( $path, $isStorePath ) {
90 $this->assertEquals( $isStorePath, FileBackend::isStoragePath( $path ),
91 "FileBackend::isStoragePath on path '$path'" );
92 }
93
94 public static function provider_testIsStoragePath() {
95 return array(
96 array( 'mwstore://', true ),
97 array( 'mwstore://backend', true ),
98 array( 'mwstore://backend/container', true ),
99 array( 'mwstore://backend/container/', true ),
100 array( 'mwstore://backend/container/path', true ),
101 array( 'mwstore://backend//container/', true ),
102 array( 'mwstore://backend//container//', true ),
103 array( 'mwstore://backend//container//path', true ),
104 array( 'mwstore:///', true ),
105 array( 'mwstore:/', false ),
106 array( 'mwstore:', false ),
107 );
108 }
109
110 /**
111 * @dataProvider provider_testSplitStoragePath
112 */
113 public function testSplitStoragePath( $path, $res ) {
114 $this->assertEquals( $res, FileBackend::splitStoragePath( $path ),
115 "FileBackend::splitStoragePath on path '$path'" );
116 }
117
118 public static function provider_testSplitStoragePath() {
119 return array(
120 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
121 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
122 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
123 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
124 array( 'mwstore://backend//container/path', array( null, null, null ) ),
125 array( 'mwstore://backend//container//path', array( null, null, null ) ),
126 array( 'mwstore://', array( null, null, null ) ),
127 array( 'mwstore://backend', array( null, null, null ) ),
128 array( 'mwstore:///', array( null, null, null ) ),
129 array( 'mwstore:/', array( null, null, null ) ),
130 array( 'mwstore:', array( null, null, null ) )
131 );
132 }
133
134 /**
135 * @dataProvider provider_normalizeStoragePath
136 */
137 public function testNormalizeStoragePath( $path, $res ) {
138 $this->assertEquals( $res, FileBackend::normalizeStoragePath( $path ),
139 "FileBackend::normalizeStoragePath on path '$path'" );
140 }
141
142 public static function provider_normalizeStoragePath() {
143 return array(
144 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
145 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
146 array( 'mwstore://backend/container/path', 'mwstore://backend/container/path' ),
147 array( 'mwstore://backend/container//path', 'mwstore://backend/container/path' ),
148 array( 'mwstore://backend/container///path', 'mwstore://backend/container/path' ),
149 array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj' ),
150 array( 'mwstore://', null ),
151 array( 'mwstore://backend', null ),
152 array( 'mwstore://backend//container/path', null ),
153 array( 'mwstore://backend//container//path', null ),
154 array( 'mwstore:///', null ),
155 array( 'mwstore:/', null ),
156 array( 'mwstore:', null ),
157 );
158 }
159
160 /**
161 * @dataProvider provider_testParentStoragePath
162 */
163 public function testParentStoragePath( $path, $res ) {
164 $this->assertEquals( $res, FileBackend::parentStoragePath( $path ),
165 "FileBackend::parentStoragePath on path '$path'" );
166 }
167
168 public static function provider_testParentStoragePath() {
169 return array(
170 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
171 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
172 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
173 array( 'mwstore://backend/container', null ),
174 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
175 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
176 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
177 array( 'mwstore://backend/container/', null ),
178 );
179 }
180
181 /**
182 * @dataProvider provider_testExtensionFromPath
183 */
184 public function testExtensionFromPath( $path, $res ) {
185 $this->assertEquals( $res, FileBackend::extensionFromPath( $path ),
186 "FileBackend::extensionFromPath on path '$path'" );
187 }
188
189 public static function provider_testExtensionFromPath() {
190 return array(
191 array( 'mwstore://backend/container/path.txt', 'txt' ),
192 array( 'mwstore://backend/container/path.svg.png', 'png' ),
193 array( 'mwstore://backend/container/path', '' ),
194 array( 'mwstore://backend/container/path.', '' ),
195 );
196 }
197
198 /**
199 * @dataProvider provider_testStore
200 */
201 public function testStore( $op ) {
202 $this->filesToPrune[] = $op['src'];
203
204 $this->backend = $this->singleBackend;
205 $this->tearDownFiles();
206 $this->doTestStore( $op );
207 $this->tearDownFiles();
208
209 $this->backend = $this->multiBackend;
210 $this->tearDownFiles();
211 $this->doTestStore( $op );
212 $this->filesToPrune[] = $op['src']; # avoid file leaking
213 $this->tearDownFiles();
214 }
215
216 private function doTestStore( $op ) {
217 $backendName = $this->backendClass();
218
219 $source = $op['src'];
220 $dest = $op['dst'];
221 $this->prepare( array( 'dir' => dirname( $dest ) ) );
222
223 file_put_contents( $source, "Unit test file" );
224
225 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
226 $this->backend->store( $op );
227 }
228
229 $status = $this->backend->doOperation( $op );
230
231 $this->assertGoodStatus( $status,
232 "Store from $source to $dest succeeded without warnings ($backendName)." );
233 $this->assertEquals( true, $status->isOK(),
234 "Store from $source to $dest succeeded ($backendName)." );
235 $this->assertEquals( array( 0 => true ), $status->success,
236 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
237 $this->assertEquals( true, file_exists( $source ),
238 "Source file $source still exists ($backendName)." );
239 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
240 "Destination file $dest exists ($backendName)." );
241
242 $this->assertEquals( filesize( $source ),
243 $this->backend->getFileSize( array( 'src' => $dest ) ),
244 "Destination file $dest has correct size ($backendName)." );
245
246 $props1 = FSFile::getPropsFromPath( $source );
247 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
248 $this->assertEquals( $props1, $props2,
249 "Source and destination have the same props ($backendName)." );
250
251 $this->assertBackendPathsConsistent( array( $dest ) );
252 }
253
254 public static function provider_testStore() {
255 $cases = array();
256
257 $tmpName = TempFSFile::factory( "unittests_", 'txt' )->getPath();
258 $toPath = self::baseStorePath() . '/unittest-cont1/e/fun/obj1.txt';
259 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
260 $cases[] = array(
261 $op, // operation
262 $tmpName, // source
263 $toPath, // dest
264 );
265
266 $op2 = $op;
267 $op2['overwrite'] = true;
268 $cases[] = array(
269 $op2, // operation
270 $tmpName, // source
271 $toPath, // dest
272 );
273
274 $op2 = $op;
275 $op2['overwriteSame'] = true;
276 $cases[] = array(
277 $op2, // operation
278 $tmpName, // source
279 $toPath, // dest
280 );
281
282 return $cases;
283 }
284
285 /**
286 * @dataProvider provider_testCopy
287 */
288 public function testCopy( $op ) {
289 $this->backend = $this->singleBackend;
290 $this->tearDownFiles();
291 $this->doTestCopy( $op );
292 $this->tearDownFiles();
293
294 $this->backend = $this->multiBackend;
295 $this->tearDownFiles();
296 $this->doTestCopy( $op );
297 $this->tearDownFiles();
298 }
299
300 private function doTestCopy( $op ) {
301 $backendName = $this->backendClass();
302
303 $source = $op['src'];
304 $dest = $op['dst'];
305 $this->prepare( array( 'dir' => dirname( $source ) ) );
306 $this->prepare( array( 'dir' => dirname( $dest ) ) );
307
308 if ( isset( $op['ignoreMissingSource'] ) ) {
309 $status = $this->backend->doOperation( $op );
310 $this->assertGoodStatus( $status,
311 "Move from $source to $dest succeeded without warnings ($backendName)." );
312 $this->assertEquals( array( 0 => true ), $status->success,
313 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
314 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
315 "Source file $source does not exist ($backendName)." );
316 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $dest ) ),
317 "Destination file $dest does not exist ($backendName)." );
318 return; // done
319 }
320
321 $status = $this->backend->doOperation(
322 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
323 $this->assertGoodStatus( $status,
324 "Creation of file at $source succeeded ($backendName)." );
325
326 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
327 $this->backend->copy( $op );
328 }
329
330 $status = $this->backend->doOperation( $op );
331
332 $this->assertGoodStatus( $status,
333 "Copy from $source to $dest succeeded without warnings ($backendName)." );
334 $this->assertEquals( true, $status->isOK(),
335 "Copy from $source to $dest succeeded ($backendName)." );
336 $this->assertEquals( array( 0 => true ), $status->success,
337 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
338 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $source ) ),
339 "Source file $source still exists ($backendName)." );
340 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
341 "Destination file $dest exists after copy ($backendName)." );
342
343 $this->assertEquals(
344 $this->backend->getFileSize( array( 'src' => $source ) ),
345 $this->backend->getFileSize( array( 'src' => $dest ) ),
346 "Destination file $dest has correct size ($backendName)." );
347
348 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
349 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
350 $this->assertEquals( $props1, $props2,
351 "Source and destination have the same props ($backendName)." );
352
353 $this->assertBackendPathsConsistent( array( $source, $dest ) );
354 }
355
356 public static function provider_testCopy() {
357 $cases = array();
358
359 $source = self::baseStorePath() . '/unittest-cont1/e/file.txt';
360 $dest = self::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
361
362 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
363 $cases[] = array(
364 $op, // operation
365 $source, // source
366 $dest, // dest
367 );
368
369 $op2 = $op;
370 $op2['overwrite'] = true;
371 $cases[] = array(
372 $op2, // operation
373 $source, // source
374 $dest, // dest
375 );
376
377 $op2 = $op;
378 $op2['overwriteSame'] = true;
379 $cases[] = array(
380 $op2, // operation
381 $source, // source
382 $dest, // dest
383 );
384
385 $op2 = $op;
386 $op2['ignoreMissingSource'] = true;
387 $cases[] = array(
388 $op2, // operation
389 $source, // source
390 $dest, // dest
391 );
392
393 $op2 = $op;
394 $op2['ignoreMissingSource'] = true;
395 $cases[] = array(
396 $op2, // operation
397 self::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
398 $dest, // dest
399 );
400
401 return $cases;
402 }
403
404 /**
405 * @dataProvider provider_testMove
406 */
407 public function testMove( $op ) {
408 $this->backend = $this->singleBackend;
409 $this->tearDownFiles();
410 $this->doTestMove( $op );
411 $this->tearDownFiles();
412
413 $this->backend = $this->multiBackend;
414 $this->tearDownFiles();
415 $this->doTestMove( $op );
416 $this->tearDownFiles();
417 }
418
419 private function doTestMove( $op ) {
420 $backendName = $this->backendClass();
421
422 $source = $op['src'];
423 $dest = $op['dst'];
424 $this->prepare( array( 'dir' => dirname( $source ) ) );
425 $this->prepare( array( 'dir' => dirname( $dest ) ) );
426
427 if ( isset( $op['ignoreMissingSource'] ) ) {
428 $status = $this->backend->doOperation( $op );
429 $this->assertGoodStatus( $status,
430 "Move from $source to $dest succeeded without warnings ($backendName)." );
431 $this->assertEquals( array( 0 => true ), $status->success,
432 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
433 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
434 "Source file $source does not exist ($backendName)." );
435 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $dest ) ),
436 "Destination file $dest does not exist ($backendName)." );
437 return; // done
438 }
439
440 $status = $this->backend->doOperation(
441 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
442 $this->assertGoodStatus( $status,
443 "Creation of file at $source succeeded ($backendName)." );
444
445 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
446 $this->backend->copy( $op );
447 }
448
449 $status = $this->backend->doOperation( $op );
450 $this->assertGoodStatus( $status,
451 "Move from $source to $dest succeeded without warnings ($backendName)." );
452 $this->assertEquals( true, $status->isOK(),
453 "Move from $source to $dest succeeded ($backendName)." );
454 $this->assertEquals( array( 0 => true ), $status->success,
455 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
456 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
457 "Source file $source does not still exists ($backendName)." );
458 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
459 "Destination file $dest exists after move ($backendName)." );
460
461 $this->assertNotEquals(
462 $this->backend->getFileSize( array( 'src' => $source ) ),
463 $this->backend->getFileSize( array( 'src' => $dest ) ),
464 "Destination file $dest has correct size ($backendName)." );
465
466 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
467 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
468 $this->assertEquals( false, $props1['fileExists'],
469 "Source file does not exist accourding to props ($backendName)." );
470 $this->assertEquals( true, $props2['fileExists'],
471 "Destination file exists accourding to props ($backendName)." );
472
473 $this->assertBackendPathsConsistent( array( $source, $dest ) );
474 }
475
476 public static function provider_testMove() {
477 $cases = array();
478
479 $source = self::baseStorePath() . '/unittest-cont1/e/file.txt';
480 $dest = self::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
481
482 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
483 $cases[] = array(
484 $op, // operation
485 $source, // source
486 $dest, // dest
487 );
488
489 $op2 = $op;
490 $op2['overwrite'] = true;
491 $cases[] = array(
492 $op2, // operation
493 $source, // source
494 $dest, // dest
495 );
496
497 $op2 = $op;
498 $op2['overwriteSame'] = true;
499 $cases[] = array(
500 $op2, // operation
501 $source, // source
502 $dest, // dest
503 );
504
505 $op2 = $op;
506 $op2['ignoreMissingSource'] = true;
507 $cases[] = array(
508 $op2, // operation
509 $source, // source
510 $dest, // dest
511 );
512
513 $op2 = $op;
514 $op2['ignoreMissingSource'] = true;
515 $cases[] = array(
516 $op2, // operation
517 self::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
518 $dest, // dest
519 );
520
521 return $cases;
522 }
523
524 /**
525 * @dataProvider provider_testDelete
526 */
527 public function testDelete( $op, $withSource, $okStatus ) {
528 $this->backend = $this->singleBackend;
529 $this->tearDownFiles();
530 $this->doTestDelete( $op, $withSource, $okStatus );
531 $this->tearDownFiles();
532
533 $this->backend = $this->multiBackend;
534 $this->tearDownFiles();
535 $this->doTestDelete( $op, $withSource, $okStatus );
536 $this->tearDownFiles();
537 }
538
539 private function doTestDelete( $op, $withSource, $okStatus ) {
540 $backendName = $this->backendClass();
541
542 $source = $op['src'];
543 $this->prepare( array( 'dir' => dirname( $source ) ) );
544
545 if ( $withSource ) {
546 $status = $this->backend->doOperation(
547 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
548 $this->assertGoodStatus( $status,
549 "Creation of file at $source succeeded ($backendName)." );
550 }
551
552 $status = $this->backend->doOperation( $op );
553 if ( $okStatus ) {
554 $this->assertGoodStatus( $status,
555 "Deletion of file at $source succeeded without warnings ($backendName)." );
556 $this->assertEquals( true, $status->isOK(),
557 "Deletion of file at $source succeeded ($backendName)." );
558 $this->assertEquals( array( 0 => true ), $status->success,
559 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
560 } else {
561 $this->assertEquals( false, $status->isOK(),
562 "Deletion of file at $source failed ($backendName)." );
563 }
564
565 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
566 "Source file $source does not exist after move ($backendName)." );
567
568 $this->assertFalse(
569 $this->backend->getFileSize( array( 'src' => $source ) ),
570 "Source file $source has correct size (false) ($backendName)." );
571
572 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
573 $this->assertFalse( $props1['fileExists'],
574 "Source file $source does not exist according to props ($backendName)." );
575
576 $this->assertBackendPathsConsistent( array( $source ) );
577 }
578
579 public static function provider_testDelete() {
580 $cases = array();
581
582 $source = self::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
583
584 $op = array( 'op' => 'delete', 'src' => $source );
585 $cases[] = array(
586 $op, // operation
587 true, // with source
588 true // succeeds
589 );
590
591 $cases[] = array(
592 $op, // operation
593 false, // without source
594 false // fails
595 );
596
597 $op['ignoreMissingSource'] = true;
598 $cases[] = array(
599 $op, // operation
600 false, // without source
601 true // succeeds
602 );
603
604 $op['ignoreMissingSource'] = true;
605 $op['src'] = self::baseStorePath() . '/unittest-cont-bad/e/file.txt';
606 $cases[] = array(
607 $op, // operation
608 false, // without source
609 true // succeeds
610 );
611
612 return $cases;
613 }
614
615 /**
616 * @dataProvider provider_testDescribe
617 */
618 public function testDescribe( $op, $withSource, $okStatus ) {
619 $this->backend = $this->singleBackend;
620 $this->tearDownFiles();
621 $this->doTestDescribe( $op, $withSource, $okStatus );
622 $this->tearDownFiles();
623
624 $this->backend = $this->multiBackend;
625 $this->tearDownFiles();
626 $this->doTestDescribe( $op, $withSource, $okStatus );
627 $this->tearDownFiles();
628 }
629
630 private function doTestDescribe( $op, $withSource, $okStatus ) {
631 $backendName = $this->backendClass();
632
633 $source = $op['src'];
634 $this->prepare( array( 'dir' => dirname( $source ) ) );
635
636 if ( $withSource ) {
637 $status = $this->backend->doOperation(
638 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
639 $this->assertGoodStatus( $status,
640 "Creation of file at $source succeeded ($backendName)." );
641 }
642
643 $status = $this->backend->doOperation( $op );
644 if ( $okStatus ) {
645 $this->assertGoodStatus( $status,
646 "Describe of file at $source succeeded without warnings ($backendName)." );
647 $this->assertEquals( true, $status->isOK(),
648 "Describe of file at $source succeeded ($backendName)." );
649 $this->assertEquals( array( 0 => true ), $status->success,
650 "Describe of file at $source has proper 'success' field in Status ($backendName)." );
651 } else {
652 $this->assertEquals( false, $status->isOK(),
653 "Describe of file at $source failed ($backendName)." );
654 }
655
656 $this->assertBackendPathsConsistent( array( $source ) );
657 }
658
659 public static function provider_testDescribe() {
660 $cases = array();
661
662 $source = self::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
663
664 $op = array( 'op' => 'describe', 'src' => $source,
665 'headers' => array( 'X-Content-Length' => '91.3', 'Content-Old-Header' => '' ),
666 'disposition' => 'inline' );
667 $cases[] = array(
668 $op, // operation
669 true, // with source
670 true // succeeds
671 );
672
673 $cases[] = array(
674 $op, // operation
675 false, // without source
676 false // fails
677 );
678
679 return $cases;
680 }
681
682 /**
683 * @dataProvider provider_testCreate
684 */
685 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
686 $this->backend = $this->singleBackend;
687 $this->tearDownFiles();
688 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
689 $this->tearDownFiles();
690
691 $this->backend = $this->multiBackend;
692 $this->tearDownFiles();
693 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
694 $this->tearDownFiles();
695 }
696
697 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
698 $backendName = $this->backendClass();
699
700 $dest = $op['dst'];
701 $this->prepare( array( 'dir' => dirname( $dest ) ) );
702
703 $oldText = 'blah...blah...waahwaah';
704 if ( $alreadyExists ) {
705 $status = $this->backend->doOperation(
706 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
707 $this->assertGoodStatus( $status,
708 "Creation of file at $dest succeeded ($backendName)." );
709 }
710
711 $status = $this->backend->doOperation( $op );
712 if ( $okStatus ) {
713 $this->assertGoodStatus( $status,
714 "Creation of file at $dest succeeded without warnings ($backendName)." );
715 $this->assertEquals( true, $status->isOK(),
716 "Creation of file at $dest succeeded ($backendName)." );
717 $this->assertEquals( array( 0 => true ), $status->success,
718 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
719 } else {
720 $this->assertEquals( false, $status->isOK(),
721 "Creation of file at $dest failed ($backendName)." );
722 }
723
724 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
725 "Destination file $dest exists after creation ($backendName)." );
726
727 $props1 = $this->backend->getFileProps( array( 'src' => $dest ) );
728 $this->assertEquals( true, $props1['fileExists'],
729 "Destination file $dest exists according to props ($backendName)." );
730 if ( $okStatus ) { // file content is what we saved
731 $this->assertEquals( $newSize, $props1['size'],
732 "Destination file $dest has expected size according to props ($backendName)." );
733 $this->assertEquals( $newSize,
734 $this->backend->getFileSize( array( 'src' => $dest ) ),
735 "Destination file $dest has correct size ($backendName)." );
736 } else { // file content is some other previous text
737 $this->assertEquals( strlen( $oldText ), $props1['size'],
738 "Destination file $dest has original size according to props ($backendName)." );
739 $this->assertEquals( strlen( $oldText ),
740 $this->backend->getFileSize( array( 'src' => $dest ) ),
741 "Destination file $dest has original size according to props ($backendName)." );
742 }
743
744 $this->assertBackendPathsConsistent( array( $dest ) );
745 }
746
747 /**
748 * @dataProvider provider_testCreate
749 */
750 public static function provider_testCreate() {
751 $cases = array();
752
753 $dest = self::baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
754
755 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
756 $cases[] = array(
757 $op, // operation
758 false, // no dest already exists
759 true, // succeeds
760 strlen( $op['content'] )
761 );
762
763 $op2 = $op;
764 $op2['content'] = "\n";
765 $cases[] = array(
766 $op2, // operation
767 false, // no dest already exists
768 true, // succeeds
769 strlen( $op2['content'] )
770 );
771
772 $op2 = $op;
773 $op2['content'] = "fsf\n waf 3kt";
774 $cases[] = array(
775 $op2, // operation
776 true, // dest already exists
777 false, // fails
778 strlen( $op2['content'] )
779 );
780
781 $op2 = $op;
782 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
783 $op2['overwrite'] = true;
784 $cases[] = array(
785 $op2, // operation
786 true, // dest already exists
787 true, // succeeds
788 strlen( $op2['content'] )
789 );
790
791 $op2 = $op;
792 $op2['content'] = "39qjmg3-qg";
793 $op2['overwriteSame'] = true;
794 $cases[] = array(
795 $op2, // operation
796 true, // dest already exists
797 false, // succeeds
798 strlen( $op2['content'] )
799 );
800
801 return $cases;
802 }
803
804 public function testDoQuickOperations() {
805 $this->backend = $this->singleBackend;
806 $this->doTestDoQuickOperations();
807 $this->tearDownFiles();
808
809 $this->backend = $this->multiBackend;
810 $this->doTestDoQuickOperations();
811 $this->tearDownFiles();
812 }
813
814 private function doTestDoQuickOperations() {
815 $backendName = $this->backendClass();
816
817 $base = self::baseStorePath();
818 $files = array(
819 "$base/unittest-cont1/e/fileA.a",
820 "$base/unittest-cont1/e/fileB.a",
821 "$base/unittest-cont1/e/fileC.a"
822 );
823 $createOps = array();
824 $purgeOps = array();
825 foreach ( $files as $path ) {
826 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
827 $this->assertGoodStatus( $status,
828 "Preparing $path succeeded without warnings ($backendName)." );
829 $createOps[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand( 0, 50000 ) );
830 $copyOps[] = array( 'op' => 'copy', 'src' => $path, 'dst' => "$path-2" );
831 $moveOps[] = array( 'op' => 'move', 'src' => "$path-2", 'dst' => "$path-3" );
832 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
833 $purgeOps[] = array( 'op' => 'delete', 'src' => "$path-3" );
834 }
835 $purgeOps[] = array( 'op' => 'null' );
836
837 $this->assertGoodStatus(
838 $this->backend->doQuickOperations( $createOps ),
839 "Creation of source files succeeded ($backendName)." );
840 foreach ( $files as $file ) {
841 $this->assertTrue( $this->backend->fileExists( array( 'src' => $file ) ),
842 "File $file exists." );
843 }
844
845 $this->assertGoodStatus(
846 $this->backend->doQuickOperations( $copyOps ),
847 "Quick copy of source files succeeded ($backendName)." );
848 foreach ( $files as $file ) {
849 $this->assertTrue( $this->backend->fileExists( array( 'src' => "$file-2" ) ),
850 "File $file-2 exists." );
851 }
852
853 $this->assertGoodStatus(
854 $this->backend->doQuickOperations( $moveOps ),
855 "Quick move of source files succeeded ($backendName)." );
856 foreach ( $files as $file ) {
857 $this->assertTrue( $this->backend->fileExists( array( 'src' => "$file-3" ) ),
858 "File $file-3 move in." );
859 $this->assertFalse( $this->backend->fileExists( array( 'src' => "$file-2" ) ),
860 "File $file-2 moved away." );
861 }
862
863 $this->assertGoodStatus(
864 $this->backend->quickCopy( array( 'src' => $files[0], 'dst' => $files[0] ) ),
865 "Copy of file {$files[0]} over itself succeeded ($backendName)." );
866 $this->assertTrue( $this->backend->fileExists( array( 'src' => $files[0] ) ),
867 "File {$files[0]} still exists." );
868
869 $this->assertGoodStatus(
870 $this->backend->quickMove( array( 'src' => $files[0], 'dst' => $files[0] ) ),
871 "Move of file {$files[0]} over itself succeeded ($backendName)." );
872 $this->assertTrue( $this->backend->fileExists( array( 'src' => $files[0] ) ),
873 "File {$files[0]} still exists." );
874
875 $this->assertGoodStatus(
876 $this->backend->doQuickOperations( $purgeOps ),
877 "Quick deletion of source files succeeded ($backendName)." );
878 foreach ( $files as $file ) {
879 $this->assertFalse( $this->backend->fileExists( array( 'src' => $file ) ),
880 "File $file purged." );
881 $this->assertFalse( $this->backend->fileExists( array( 'src' => "$file-3" ) ),
882 "File $file-3 purged." );
883 }
884 }
885
886 /**
887 * @dataProvider provider_testConcatenate
888 */
889 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
890 $this->filesToPrune[] = $op['dst'];
891
892 $this->backend = $this->singleBackend;
893 $this->tearDownFiles();
894 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
895 $this->filesToPrune[] = $op['dst']; # avoid file leaking
896 $this->tearDownFiles();
897
898 $this->backend = $this->multiBackend;
899 $this->tearDownFiles();
900 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
901 $this->filesToPrune[] = $op['dst']; # avoid file leaking
902 $this->tearDownFiles();
903 }
904
905 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
906 $backendName = $this->backendClass();
907
908 $expContent = '';
909 // Create sources
910 $ops = array();
911 foreach ( $srcs as $i => $source ) {
912 $this->prepare( array( 'dir' => dirname( $source ) ) );
913 $ops[] = array(
914 'op' => 'create', // operation
915 'dst' => $source, // source
916 'content' => $srcsContent[$i]
917 );
918 $expContent .= $srcsContent[$i];
919 }
920 $status = $this->backend->doOperations( $ops );
921
922 $this->assertGoodStatus( $status,
923 "Creation of source files succeeded ($backendName)." );
924
925 $dest = $params['dst'];
926 if ( $alreadyExists ) {
927 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
928 $this->assertEquals( true, $ok,
929 "Creation of file at $dest succeeded ($backendName)." );
930 } else {
931 $ok = file_put_contents( $dest, '' ) !== false;
932 $this->assertEquals( true, $ok,
933 "Creation of 0-byte file at $dest succeeded ($backendName)." );
934 }
935
936 // Combine the files into one
937 $status = $this->backend->concatenate( $params );
938 if ( $okStatus ) {
939 $this->assertGoodStatus( $status,
940 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
941 $this->assertEquals( true, $status->isOK(),
942 "Creation of concat file at $dest succeeded ($backendName)." );
943 } else {
944 $this->assertEquals( false, $status->isOK(),
945 "Creation of concat file at $dest failed ($backendName)." );
946 }
947
948 if ( $okStatus ) {
949 $this->assertEquals( true, is_file( $dest ),
950 "Dest concat file $dest exists after creation ($backendName)." );
951 } else {
952 $this->assertEquals( true, is_file( $dest ),
953 "Dest concat file $dest exists after failed creation ($backendName)." );
954 }
955
956 $contents = file_get_contents( $dest );
957 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
958
959 if ( $okStatus ) {
960 $this->assertEquals( $expContent, $contents,
961 "Concat file at $dest has correct contents ($backendName)." );
962 } else {
963 $this->assertNotEquals( $expContent, $contents,
964 "Concat file at $dest has correct contents ($backendName)." );
965 }
966 }
967
968 public static function provider_testConcatenate() {
969 $cases = array();
970
971 $rand = mt_rand( 0, 2000000000 ) . time();
972 $dest = wfTempDir() . "/randomfile!$rand.txt";
973 $srcs = array(
974 self::baseStorePath() . '/unittest-cont1/e/file1.txt',
975 self::baseStorePath() . '/unittest-cont1/e/file2.txt',
976 self::baseStorePath() . '/unittest-cont1/e/file3.txt',
977 self::baseStorePath() . '/unittest-cont1/e/file4.txt',
978 self::baseStorePath() . '/unittest-cont1/e/file5.txt',
979 self::baseStorePath() . '/unittest-cont1/e/file6.txt',
980 self::baseStorePath() . '/unittest-cont1/e/file7.txt',
981 self::baseStorePath() . '/unittest-cont1/e/file8.txt',
982 self::baseStorePath() . '/unittest-cont1/e/file9.txt',
983 self::baseStorePath() . '/unittest-cont1/e/file10.txt'
984 );
985 $content = array(
986 'egfage',
987 'ageageag',
988 'rhokohlr',
989 'shgmslkg',
990 'kenga',
991 'owagmal',
992 'kgmae',
993 'g eak;g',
994 'lkaem;a',
995 'legma'
996 );
997 $params = array( 'srcs' => $srcs, 'dst' => $dest );
998
999 $cases[] = array(
1000 $params, // operation
1001 $srcs, // sources
1002 $content, // content for each source
1003 false, // no dest already exists
1004 true, // succeeds
1005 );
1006
1007 $cases[] = array(
1008 $params, // operation
1009 $srcs, // sources
1010 $content, // content for each source
1011 true, // dest already exists
1012 false, // succeeds
1013 );
1014
1015 return $cases;
1016 }
1017
1018 /**
1019 * @dataProvider provider_testGetFileStat
1020 */
1021 public function testGetFileStat( $path, $content, $alreadyExists ) {
1022 $this->backend = $this->singleBackend;
1023 $this->tearDownFiles();
1024 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1025 $this->tearDownFiles();
1026
1027 $this->backend = $this->multiBackend;
1028 $this->tearDownFiles();
1029 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1030 $this->tearDownFiles();
1031 }
1032
1033 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
1034 $backendName = $this->backendClass();
1035
1036 if ( $alreadyExists ) {
1037 $this->prepare( array( 'dir' => dirname( $path ) ) );
1038 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
1039 $this->assertGoodStatus( $status,
1040 "Creation of file at $path succeeded ($backendName)." );
1041
1042 $size = $this->backend->getFileSize( array( 'src' => $path ) );
1043 $time = $this->backend->getFileTimestamp( array( 'src' => $path ) );
1044 $stat = $this->backend->getFileStat( array( 'src' => $path ) );
1045
1046 $this->assertEquals( strlen( $content ), $size,
1047 "Correct file size of '$path'" );
1048 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
1049 "Correct file timestamp of '$path'" );
1050
1051 $size = $stat['size'];
1052 $time = $stat['mtime'];
1053 $this->assertEquals( strlen( $content ), $size,
1054 "Correct file size of '$path'" );
1055 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
1056 "Correct file timestamp of '$path'" );
1057
1058 $this->backend->clearCache( array( $path ) );
1059
1060 $size = $this->backend->getFileSize( array( 'src' => $path ) );
1061
1062 $this->assertEquals( strlen( $content ), $size,
1063 "Correct file size of '$path'" );
1064
1065 $this->backend->preloadCache( array( $path ) );
1066
1067 $size = $this->backend->getFileSize( array( 'src' => $path ) );
1068
1069 $this->assertEquals( strlen( $content ), $size,
1070 "Correct file size of '$path'" );
1071 } else {
1072 $size = $this->backend->getFileSize( array( 'src' => $path ) );
1073 $time = $this->backend->getFileTimestamp( array( 'src' => $path ) );
1074 $stat = $this->backend->getFileStat( array( 'src' => $path ) );
1075
1076 $this->assertFalse( $size, "Correct file size of '$path'" );
1077 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1078 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1079 }
1080 }
1081
1082 public static function provider_testGetFileStat() {
1083 $cases = array();
1084
1085 $base = self::baseStorePath();
1086 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
1087 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
1088 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
1089
1090 return $cases;
1091 }
1092
1093 /**
1094 * @dataProvider provider_testGetFileContents
1095 */
1096 public function testGetFileContents( $source, $content ) {
1097 $this->backend = $this->singleBackend;
1098 $this->tearDownFiles();
1099 $this->doTestGetFileContents( $source, $content );
1100 $this->tearDownFiles();
1101
1102 $this->backend = $this->multiBackend;
1103 $this->tearDownFiles();
1104 $this->doTestGetFileContents( $source, $content );
1105 $this->tearDownFiles();
1106 }
1107
1108 private function doTestGetFileContents( $source, $content ) {
1109 $backendName = $this->backendClass();
1110
1111 $srcs = (array)$source;
1112 $content = (array)$content;
1113 foreach ( $srcs as $i => $src ) {
1114 $this->prepare( array( 'dir' => dirname( $src ) ) );
1115 $status = $this->backend->doOperation(
1116 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1117 $this->assertGoodStatus( $status,
1118 "Creation of file at $src succeeded ($backendName)." );
1119 }
1120
1121 if ( is_array( $source ) ) {
1122 $contents = $this->backend->getFileContentsMulti( array( 'srcs' => $source ) );
1123 foreach ( $contents as $path => $data ) {
1124 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1125 $this->assertEquals( current( $content ), $data, "Contents of $path is correct ($backendName)." );
1126 next( $content );
1127 }
1128 $this->assertEquals( $source, array_keys( $contents ), "Contents in right order ($backendName)." );
1129 $this->assertEquals( count( $source ), count( $contents ), "Contents array size correct ($backendName)." );
1130 } else {
1131 $data = $this->backend->getFileContents( array( 'src' => $source ) );
1132 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1133 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1134 }
1135 }
1136
1137 public static function provider_testGetFileContents() {
1138 $cases = array();
1139
1140 $base = self::baseStorePath();
1141 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
1142 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
1143 $cases[] = array(
1144 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1145 "$base/unittest-cont1/e/a/z.txt" ),
1146 array( "contents xx", "contents xy", "contents xz" )
1147 );
1148
1149 return $cases;
1150 }
1151
1152 /**
1153 * @dataProvider provider_testGetLocalCopy
1154 */
1155 public function testGetLocalCopy( $source, $content ) {
1156 $this->backend = $this->singleBackend;
1157 $this->tearDownFiles();
1158 $this->doTestGetLocalCopy( $source, $content );
1159 $this->tearDownFiles();
1160
1161 $this->backend = $this->multiBackend;
1162 $this->tearDownFiles();
1163 $this->doTestGetLocalCopy( $source, $content );
1164 $this->tearDownFiles();
1165 }
1166
1167 private function doTestGetLocalCopy( $source, $content ) {
1168 $backendName = $this->backendClass();
1169
1170 $srcs = (array)$source;
1171 $content = (array)$content;
1172 foreach ( $srcs as $i => $src ) {
1173 $this->prepare( array( 'dir' => dirname( $src ) ) );
1174 $status = $this->backend->doOperation(
1175 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1176 $this->assertGoodStatus( $status,
1177 "Creation of file at $src succeeded ($backendName)." );
1178 }
1179
1180 if ( is_array( $source ) ) {
1181 $tmpFiles = $this->backend->getLocalCopyMulti( array( 'srcs' => $source ) );
1182 foreach ( $tmpFiles as $path => $tmpFile ) {
1183 $this->assertNotNull( $tmpFile,
1184 "Creation of local copy of $path succeeded ($backendName)." );
1185 $contents = file_get_contents( $tmpFile->getPath() );
1186 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1187 $this->assertEquals( current( $content ), $contents, "Local copy of $path is correct ($backendName)." );
1188 next( $content );
1189 }
1190 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local copies in right order ($backendName)." );
1191 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local copies array size correct ($backendName)." );
1192 } else {
1193 $tmpFile = $this->backend->getLocalCopy( array( 'src' => $source ) );
1194 $this->assertNotNull( $tmpFile,
1195 "Creation of local copy of $source succeeded ($backendName)." );
1196 $contents = file_get_contents( $tmpFile->getPath() );
1197 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1198 $this->assertEquals( $content[0], $contents, "Local copy of $source is correct ($backendName)." );
1199 }
1200
1201 $obj = new stdClass();
1202 $tmpFile->bind( $obj );
1203 }
1204
1205 public static function provider_testGetLocalCopy() {
1206 $cases = array();
1207
1208 $base = self::baseStorePath();
1209 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1210 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1211 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1212 $cases[] = array(
1213 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1214 "$base/unittest-cont1/e/a/z.txt" ),
1215 array( "contents xx", "contents xy", "contents xz" )
1216 );
1217
1218 return $cases;
1219 }
1220
1221 /**
1222 * @dataProvider provider_testGetLocalReference
1223 */
1224 public function testGetLocalReference( $source, $content ) {
1225 $this->backend = $this->singleBackend;
1226 $this->tearDownFiles();
1227 $this->doTestGetLocalReference( $source, $content );
1228 $this->tearDownFiles();
1229
1230 $this->backend = $this->multiBackend;
1231 $this->tearDownFiles();
1232 $this->doTestGetLocalReference( $source, $content );
1233 $this->tearDownFiles();
1234 }
1235
1236 private function doTestGetLocalReference( $source, $content ) {
1237 $backendName = $this->backendClass();
1238
1239 $srcs = (array)$source;
1240 $content = (array)$content;
1241 foreach ( $srcs as $i => $src ) {
1242 $this->prepare( array( 'dir' => dirname( $src ) ) );
1243 $status = $this->backend->doOperation(
1244 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1245 $this->assertGoodStatus( $status,
1246 "Creation of file at $src succeeded ($backendName)." );
1247 }
1248
1249 if ( is_array( $source ) ) {
1250 $tmpFiles = $this->backend->getLocalReferenceMulti( array( 'srcs' => $source ) );
1251 foreach ( $tmpFiles as $path => $tmpFile ) {
1252 $this->assertNotNull( $tmpFile,
1253 "Creation of local copy of $path succeeded ($backendName)." );
1254 $contents = file_get_contents( $tmpFile->getPath() );
1255 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1256 $this->assertEquals( current( $content ), $contents, "Local ref of $path is correct ($backendName)." );
1257 next( $content );
1258 }
1259 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local refs in right order ($backendName)." );
1260 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local refs array size correct ($backendName)." );
1261 } else {
1262 $tmpFile = $this->backend->getLocalReference( array( 'src' => $source ) );
1263 $this->assertNotNull( $tmpFile,
1264 "Creation of local copy of $source succeeded ($backendName)." );
1265 $contents = file_get_contents( $tmpFile->getPath() );
1266 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1267 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1268 }
1269 }
1270
1271 public static function provider_testGetLocalReference() {
1272 $cases = array();
1273
1274 $base = self::baseStorePath();
1275 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1276 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1277 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1278 $cases[] = array(
1279 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1280 "$base/unittest-cont1/e/a/z.txt" ),
1281 array( "contents xx", "contents xy", "contents xz" )
1282 );
1283
1284 return $cases;
1285 }
1286
1287 public function testGetLocalCopyAndReference404() {
1288 $this->backend = $this->singleBackend;
1289 $this->tearDownFiles();
1290 $this->doTestGetLocalCopyAndReference404();
1291 $this->tearDownFiles();
1292
1293 $this->backend = $this->multiBackend;
1294 $this->tearDownFiles();
1295 $this->doTestGetLocalCopyAndReference404();
1296 $this->tearDownFiles();
1297 }
1298
1299 public function doTestGetLocalCopyAndReference404() {
1300 $backendName = $this->backendClass();
1301
1302 $base = self::baseStorePath();
1303
1304 $tmpFile = $this->backend->getLocalCopy( array(
1305 'src' => "$base/unittest-cont1/not-there" ) );
1306 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1307
1308 $tmpFile = $this->backend->getLocalReference( array(
1309 'src' => "$base/unittest-cont1/not-there" ) );
1310 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1311 }
1312
1313 /**
1314 * @dataProvider provider_testGetFileHttpUrl
1315 */
1316 public function testGetFileHttpUrl( $source, $content ) {
1317 $this->backend = $this->singleBackend;
1318 $this->tearDownFiles();
1319 $this->doTestGetFileHttpUrl( $source, $content );
1320 $this->tearDownFiles();
1321
1322 $this->backend = $this->multiBackend;
1323 $this->tearDownFiles();
1324 $this->doTestGetFileHttpUrl( $source, $content );
1325 $this->tearDownFiles();
1326 }
1327
1328 private function doTestGetFileHttpUrl( $source, $content ) {
1329 $backendName = $this->backendClass();
1330
1331 $this->prepare( array( 'dir' => dirname( $source ) ) );
1332 $status = $this->backend->doOperation(
1333 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
1334 $this->assertGoodStatus( $status,
1335 "Creation of file at $source succeeded ($backendName)." );
1336
1337 $url = $this->backend->getFileHttpUrl( array( 'src' => $source ) );
1338
1339 if ( $url !== null ) { // supported
1340 $data = Http::request( "GET", $url );
1341 $this->assertEquals( $content, $data,
1342 "HTTP GET of URL has right contents ($backendName)." );
1343 }
1344 }
1345
1346 public static function provider_testGetFileHttpUrl() {
1347 $cases = array();
1348
1349 $base = self::baseStorePath();
1350 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1351 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1352 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1353
1354 return $cases;
1355 }
1356
1357 /**
1358 * @dataProvider provider_testPrepareAndClean
1359 */
1360 public function testPrepareAndClean( $path, $isOK ) {
1361 $this->backend = $this->singleBackend;
1362 $this->doTestPrepareAndClean( $path, $isOK );
1363 $this->tearDownFiles();
1364
1365 $this->backend = $this->multiBackend;
1366 $this->doTestPrepareAndClean( $path, $isOK );
1367 $this->tearDownFiles();
1368 }
1369
1370 public static function provider_testPrepareAndClean() {
1371 $base = self::baseStorePath();
1372 return array(
1373 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1374 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1375 # Specific to FS backend with no basePath field set
1376 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1377 );
1378 }
1379
1380 private function doTestPrepareAndClean( $path, $isOK ) {
1381 $backendName = $this->backendClass();
1382
1383 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1384 if ( $isOK ) {
1385 $this->assertGoodStatus( $status,
1386 "Preparing dir $path succeeded without warnings ($backendName)." );
1387 $this->assertEquals( true, $status->isOK(),
1388 "Preparing dir $path succeeded ($backendName)." );
1389 } else {
1390 $this->assertEquals( false, $status->isOK(),
1391 "Preparing dir $path failed ($backendName)." );
1392 }
1393
1394 $status = $this->backend->clean( array( 'dir' => dirname( $path ) ) );
1395 if ( $isOK ) {
1396 $this->assertGoodStatus( $status,
1397 "Cleaning dir $path succeeded without warnings ($backendName)." );
1398 $this->assertEquals( true, $status->isOK(),
1399 "Cleaning dir $path succeeded ($backendName)." );
1400 } else {
1401 $this->assertEquals( false, $status->isOK(),
1402 "Cleaning dir $path failed ($backendName)." );
1403 }
1404 }
1405
1406 public function testRecursiveClean() {
1407 $this->backend = $this->singleBackend;
1408 $this->doTestRecursiveClean();
1409 $this->tearDownFiles();
1410
1411 $this->backend = $this->multiBackend;
1412 $this->doTestRecursiveClean();
1413 $this->tearDownFiles();
1414 }
1415
1416 private function doTestRecursiveClean() {
1417 $backendName = $this->backendClass();
1418
1419 $base = self::baseStorePath();
1420 $dirs = array(
1421 "$base/unittest-cont1",
1422 "$base/unittest-cont1/e",
1423 "$base/unittest-cont1/e/a",
1424 "$base/unittest-cont1/e/a/b",
1425 "$base/unittest-cont1/e/a/b/c",
1426 "$base/unittest-cont1/e/a/b/c/d0",
1427 "$base/unittest-cont1/e/a/b/c/d1",
1428 "$base/unittest-cont1/e/a/b/c/d2",
1429 "$base/unittest-cont1/e/a/b/c/d0/1",
1430 "$base/unittest-cont1/e/a/b/c/d0/2",
1431 "$base/unittest-cont1/e/a/b/c/d1/3",
1432 "$base/unittest-cont1/e/a/b/c/d1/4",
1433 "$base/unittest-cont1/e/a/b/c/d2/5",
1434 "$base/unittest-cont1/e/a/b/c/d2/6"
1435 );
1436 foreach ( $dirs as $dir ) {
1437 $status = $this->prepare( array( 'dir' => $dir ) );
1438 $this->assertGoodStatus( $status,
1439 "Preparing dir $dir succeeded without warnings ($backendName)." );
1440 }
1441
1442 if ( $this->backend instanceof FSFileBackend ) {
1443 foreach ( $dirs as $dir ) {
1444 $this->assertEquals( true, $this->backend->directoryExists( array( 'dir' => $dir ) ),
1445 "Dir $dir exists ($backendName)." );
1446 }
1447 }
1448
1449 $status = $this->backend->clean(
1450 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1451 $this->assertGoodStatus( $status,
1452 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1453
1454 foreach ( $dirs as $dir ) {
1455 $this->assertEquals( false, $this->backend->directoryExists( array( 'dir' => $dir ) ),
1456 "Dir $dir no longer exists ($backendName)." );
1457 }
1458 }
1459
1460 // @TODO: testSecure
1461
1462 public function testDoOperations() {
1463 $this->backend = $this->singleBackend;
1464 $this->tearDownFiles();
1465 $this->doTestDoOperations();
1466 $this->tearDownFiles();
1467
1468 $this->backend = $this->multiBackend;
1469 $this->tearDownFiles();
1470 $this->doTestDoOperations();
1471 $this->tearDownFiles();
1472 }
1473
1474 private function doTestDoOperations() {
1475 $base = self::baseStorePath();
1476
1477 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1478 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1479 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1480 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1481 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1482 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1483 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1484
1485 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1486 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1487 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1488 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1489 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1490 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1491 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1492
1493 $status = $this->backend->doOperations( array(
1494 array( 'op' => 'describe', 'src' => $fileA,
1495 'headers' => array( 'X-Content-Length' => '91.3' ), 'disposition' => 'inline' ),
1496 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1497 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1498 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1499 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1500 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1501 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1502 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1503 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1504 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1505 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1506 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1507 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1508 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1509 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1510 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1511 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1512 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1513 // Does nothing
1514 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1515 // Does nothing
1516 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1517 // Does nothing
1518 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1519 // Does nothing
1520 array( 'op' => 'null' ),
1521 // Does nothing
1522 ) );
1523
1524 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1525 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1526 $this->assertEquals( 14, count( $status->success ),
1527 "Operation batch has correct success array" );
1528
1529 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
1530 "File does not exist at $fileA" );
1531 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1532 "File does not exist at $fileB" );
1533 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1534 "File does not exist at $fileD" );
1535
1536 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1537 "File exists at $fileC" );
1538 $this->assertEquals( $fileBContents,
1539 $this->backend->getFileContents( array( 'src' => $fileC ) ),
1540 "Correct file contents of $fileC" );
1541 $this->assertEquals( strlen( $fileBContents ),
1542 $this->backend->getFileSize( array( 'src' => $fileC ) ),
1543 "Correct file size of $fileC" );
1544 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1545 $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
1546 "Correct file SHA-1 of $fileC" );
1547 }
1548
1549 public function testDoOperationsPipeline() {
1550 $this->backend = $this->singleBackend;
1551 $this->tearDownFiles();
1552 $this->doTestDoOperationsPipeline();
1553 $this->tearDownFiles();
1554
1555 $this->backend = $this->multiBackend;
1556 $this->tearDownFiles();
1557 $this->doTestDoOperationsPipeline();
1558 $this->tearDownFiles();
1559 }
1560
1561 // concurrency orientated
1562 private function doTestDoOperationsPipeline() {
1563 $base = self::baseStorePath();
1564
1565 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1566 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1567 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1568
1569 $tmpNameA = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1570 file_put_contents( $tmpNameA, $fileAContents );
1571 $tmpNameB = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1572 file_put_contents( $tmpNameB, $fileBContents );
1573 $tmpNameC = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1574 file_put_contents( $tmpNameC, $fileCContents );
1575
1576 $this->filesToPrune[] = $tmpNameA; # avoid file leaking
1577 $this->filesToPrune[] = $tmpNameB; # avoid file leaking
1578 $this->filesToPrune[] = $tmpNameC; # avoid file leaking
1579
1580 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1581 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1582 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1583 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1584
1585 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1586 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1587 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1588 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1589 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1590
1591 $status = $this->backend->doOperations( array(
1592 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1593 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1594 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1595 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1596 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1597 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1598 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1599 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1600 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1601 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1602 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1603 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1604 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1605 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1606 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1607 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1608 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1609 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1610 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1611 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1612 // Does nothing
1613 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1614 // Does nothing
1615 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1616 // Does nothing
1617 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1618 // Does nothing
1619 array( 'op' => 'null' ),
1620 // Does nothing
1621 ) );
1622
1623 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1624 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1625 $this->assertEquals( 16, count( $status->success ),
1626 "Operation batch has correct success array" );
1627
1628 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
1629 "File does not exist at $fileA" );
1630 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1631 "File does not exist at $fileB" );
1632 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1633 "File does not exist at $fileD" );
1634
1635 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1636 "File exists at $fileC" );
1637 $this->assertEquals( $fileBContents,
1638 $this->backend->getFileContents( array( 'src' => $fileC ) ),
1639 "Correct file contents of $fileC" );
1640 $this->assertEquals( strlen( $fileBContents ),
1641 $this->backend->getFileSize( array( 'src' => $fileC ) ),
1642 "Correct file size of $fileC" );
1643 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1644 $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
1645 "Correct file SHA-1 of $fileC" );
1646 }
1647
1648 public function testDoOperationsFailing() {
1649 $this->backend = $this->singleBackend;
1650 $this->tearDownFiles();
1651 $this->doTestDoOperationsFailing();
1652 $this->tearDownFiles();
1653
1654 $this->backend = $this->multiBackend;
1655 $this->tearDownFiles();
1656 $this->doTestDoOperationsFailing();
1657 $this->tearDownFiles();
1658 }
1659
1660 private function doTestDoOperationsFailing() {
1661 $base = self::baseStorePath();
1662
1663 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1664 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1665 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1666 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1667 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1668 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1669 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1670
1671 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1672 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1673 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1674 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1675 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1676 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1677
1678 $status = $this->backend->doOperations( array(
1679 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1680 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1681 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1682 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1683 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1684 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1685 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1686 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1687 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1688 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1689 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1690 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1691 array( 'op' => 'delete', 'src' => $fileD ),
1692 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1693 array( 'op' => 'null' ),
1694 // Does nothing
1695 ), array( 'force' => 1 ) );
1696
1697 $this->assertNotEquals( array(), $status->errors, "Operation had warnings" );
1698 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1699 $this->assertEquals( 8, count( $status->success ),
1700 "Operation batch has correct success array" );
1701
1702 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1703 "File does not exist at $fileB" );
1704 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1705 "File does not exist at $fileD" );
1706
1707 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileA ) ),
1708 "File does not exist at $fileA" );
1709 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1710 "File exists at $fileC" );
1711 $this->assertEquals( $fileBContents,
1712 $this->backend->getFileContents( array( 'src' => $fileA ) ),
1713 "Correct file contents of $fileA" );
1714 $this->assertEquals( strlen( $fileBContents ),
1715 $this->backend->getFileSize( array( 'src' => $fileA ) ),
1716 "Correct file size of $fileA" );
1717 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1718 $this->backend->getFileSha1Base36( array( 'src' => $fileA ) ),
1719 "Correct file SHA-1 of $fileA" );
1720 }
1721
1722 public function testGetFileList() {
1723 $this->backend = $this->singleBackend;
1724 $this->tearDownFiles();
1725 $this->doTestGetFileList();
1726 $this->tearDownFiles();
1727
1728 $this->backend = $this->multiBackend;
1729 $this->tearDownFiles();
1730 $this->doTestGetFileList();
1731 $this->tearDownFiles();
1732 }
1733
1734 private function doTestGetFileList() {
1735 $backendName = $this->backendClass();
1736 $base = self::baseStorePath();
1737
1738 // Should have no errors
1739 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1740
1741 $files = array(
1742 "$base/unittest-cont1/e/test1.txt",
1743 "$base/unittest-cont1/e/test2.txt",
1744 "$base/unittest-cont1/e/test3.txt",
1745 "$base/unittest-cont1/e/subdir1/test1.txt",
1746 "$base/unittest-cont1/e/subdir1/test2.txt",
1747 "$base/unittest-cont1/e/subdir2/test3.txt",
1748 "$base/unittest-cont1/e/subdir2/test4.txt",
1749 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1750 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1751 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1752 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1753 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1754 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1755 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1756 );
1757
1758 // Add the files
1759 $ops = array();
1760 foreach ( $files as $file ) {
1761 $this->prepare( array( 'dir' => dirname( $file ) ) );
1762 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1763 }
1764 $status = $this->backend->doQuickOperations( $ops );
1765 $this->assertGoodStatus( $status,
1766 "Creation of files succeeded ($backendName)." );
1767 $this->assertEquals( true, $status->isOK(),
1768 "Creation of files succeeded with OK status ($backendName)." );
1769
1770 // Expected listing
1771 $expected = array(
1772 "e/test1.txt",
1773 "e/test2.txt",
1774 "e/test3.txt",
1775 "e/subdir1/test1.txt",
1776 "e/subdir1/test2.txt",
1777 "e/subdir2/test3.txt",
1778 "e/subdir2/test4.txt",
1779 "e/subdir2/subdir/test1.txt",
1780 "e/subdir2/subdir/test2.txt",
1781 "e/subdir2/subdir/test3.txt",
1782 "e/subdir2/subdir/test4.txt",
1783 "e/subdir2/subdir/test5.txt",
1784 "e/subdir2/subdir/sub/test0.txt",
1785 "e/subdir2/subdir/sub/120-px-file.txt",
1786 );
1787 sort( $expected );
1788
1789 // Actual listing (no trailing slash)
1790 $list = array();
1791 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1792 foreach ( $iter as $file ) {
1793 $list[] = $file;
1794 }
1795 sort( $list );
1796
1797 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1798
1799 // Actual listing (with trailing slash)
1800 $list = array();
1801 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1802 foreach ( $iter as $file ) {
1803 $list[] = $file;
1804 }
1805 sort( $list );
1806
1807 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1808
1809 // Expected listing
1810 $expected = array(
1811 "test1.txt",
1812 "test2.txt",
1813 "test3.txt",
1814 "test4.txt",
1815 "test5.txt",
1816 "sub/test0.txt",
1817 "sub/120-px-file.txt",
1818 );
1819 sort( $expected );
1820
1821 // Actual listing (no trailing slash)
1822 $list = array();
1823 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1824 foreach ( $iter as $file ) {
1825 $list[] = $file;
1826 }
1827 sort( $list );
1828
1829 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1830
1831 // Actual listing (with trailing slash)
1832 $list = array();
1833 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1834 foreach ( $iter as $file ) {
1835 $list[] = $file;
1836 }
1837 sort( $list );
1838
1839 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1840
1841 // Actual listing (using iterator second time)
1842 $list = array();
1843 foreach ( $iter as $file ) {
1844 $list[] = $file;
1845 }
1846 sort( $list );
1847
1848 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1849
1850 // Expected listing (top files only)
1851 $expected = array(
1852 "test1.txt",
1853 "test2.txt",
1854 "test3.txt",
1855 "test4.txt",
1856 "test5.txt"
1857 );
1858 sort( $expected );
1859
1860 // Actual listing (top files only)
1861 $list = array();
1862 $iter = $this->backend->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1863 foreach ( $iter as $file ) {
1864 $list[] = $file;
1865 }
1866 sort( $list );
1867
1868 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1869
1870 foreach ( $files as $file ) { // clean up
1871 $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1872 }
1873
1874 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1875 foreach ( $iter as $iter ) {
1876 // no errors
1877 }
1878 }
1879
1880 public function testGetDirectoryList() {
1881 $this->backend = $this->singleBackend;
1882 $this->tearDownFiles();
1883 $this->doTestGetDirectoryList();
1884 $this->tearDownFiles();
1885
1886 $this->backend = $this->multiBackend;
1887 $this->tearDownFiles();
1888 $this->doTestGetDirectoryList();
1889 $this->tearDownFiles();
1890 }
1891
1892 private function doTestGetDirectoryList() {
1893 $backendName = $this->backendClass();
1894
1895 $base = self::baseStorePath();
1896 $files = array(
1897 "$base/unittest-cont1/e/test1.txt",
1898 "$base/unittest-cont1/e/test2.txt",
1899 "$base/unittest-cont1/e/test3.txt",
1900 "$base/unittest-cont1/e/subdir1/test1.txt",
1901 "$base/unittest-cont1/e/subdir1/test2.txt",
1902 "$base/unittest-cont1/e/subdir2/test3.txt",
1903 "$base/unittest-cont1/e/subdir2/test4.txt",
1904 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1905 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1906 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1907 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1908 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1909 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1910 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1911 );
1912
1913 // Add the files
1914 $ops = array();
1915 foreach ( $files as $file ) {
1916 $this->prepare( array( 'dir' => dirname( $file ) ) );
1917 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1918 }
1919 $status = $this->backend->doQuickOperations( $ops );
1920 $this->assertGoodStatus( $status,
1921 "Creation of files succeeded ($backendName)." );
1922 $this->assertEquals( true, $status->isOK(),
1923 "Creation of files succeeded with OK status ($backendName)." );
1924
1925 $this->assertEquals( true,
1926 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1927 "Directory exists in ($backendName)." );
1928 $this->assertEquals( true,
1929 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1930 "Directory exists in ($backendName)." );
1931 $this->assertEquals( false,
1932 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1933 "Directory does not exists in ($backendName)." );
1934
1935 // Expected listing
1936 $expected = array(
1937 "e",
1938 );
1939 sort( $expected );
1940
1941 // Actual listing (no trailing slash)
1942 $list = array();
1943 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1944 foreach ( $iter as $file ) {
1945 $list[] = $file;
1946 }
1947 sort( $list );
1948
1949 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1950
1951 // Expected listing
1952 $expected = array(
1953 "subdir1",
1954 "subdir2",
1955 "subdir3",
1956 "subdir4",
1957 );
1958 sort( $expected );
1959
1960 // Actual listing (no trailing slash)
1961 $list = array();
1962 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1963 foreach ( $iter as $file ) {
1964 $list[] = $file;
1965 }
1966 sort( $list );
1967
1968 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1969
1970 // Actual listing (with trailing slash)
1971 $list = array();
1972 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1973 foreach ( $iter as $file ) {
1974 $list[] = $file;
1975 }
1976 sort( $list );
1977
1978 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1979
1980 // Expected listing
1981 $expected = array(
1982 "subdir",
1983 );
1984 sort( $expected );
1985
1986 // Actual listing (no trailing slash)
1987 $list = array();
1988 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1989 foreach ( $iter as $file ) {
1990 $list[] = $file;
1991 }
1992 sort( $list );
1993
1994 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1995
1996 // Actual listing (with trailing slash)
1997 $list = array();
1998 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1999 foreach ( $iter as $file ) {
2000 $list[] = $file;
2001 }
2002 sort( $list );
2003
2004 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2005
2006 // Actual listing (using iterator second time)
2007 $list = array();
2008 foreach ( $iter as $file ) {
2009 $list[] = $file;
2010 }
2011 sort( $list );
2012
2013 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
2014
2015 // Expected listing (recursive)
2016 $expected = array(
2017 "e",
2018 "e/subdir1",
2019 "e/subdir2",
2020 "e/subdir3",
2021 "e/subdir4",
2022 "e/subdir2/subdir",
2023 "e/subdir3/subdir",
2024 "e/subdir4/subdir",
2025 "e/subdir4/subdir/sub",
2026 );
2027 sort( $expected );
2028
2029 // Actual listing (recursive)
2030 $list = array();
2031 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
2032 foreach ( $iter as $file ) {
2033 $list[] = $file;
2034 }
2035 sort( $list );
2036
2037 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2038
2039 // Expected listing (recursive)
2040 $expected = array(
2041 "subdir",
2042 "subdir/sub",
2043 );
2044 sort( $expected );
2045
2046 // Actual listing (recursive)
2047 $list = array();
2048 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
2049 foreach ( $iter as $file ) {
2050 $list[] = $file;
2051 }
2052 sort( $list );
2053
2054 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2055
2056 // Actual listing (recursive, second time)
2057 $list = array();
2058 foreach ( $iter as $file ) {
2059 $list[] = $file;
2060 }
2061 sort( $list );
2062
2063 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2064
2065 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) );
2066 $items = is_array( $iter ) ? $iter : iterator_to_array( $iter );
2067 $this->assertEquals( array(), $items, "Directory listing is empty." );
2068
2069 foreach ( $files as $file ) { // clean up
2070 $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
2071 }
2072
2073 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
2074 foreach ( $iter as $file ) {
2075 // no errors
2076 }
2077
2078 $items = is_array( $iter ) ? $iter : iterator_to_array( $iter );
2079 $this->assertEquals( array(), $items, "Directory listing is empty." );
2080
2081 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/not/exists" ) );
2082 $items = is_array( $iter ) ? $iter : iterator_to_array( $iter );
2083 $this->assertEquals( array(), $items, "Directory listing is empty." );
2084 }
2085
2086 public function testLockCalls() {
2087 $this->backend = $this->singleBackend;
2088 $this->doTestLockCalls();
2089 }
2090
2091 private function doTestLockCalls() {
2092 $backendName = $this->backendClass();
2093
2094 $paths = array(
2095 "test1.txt",
2096 "test2.txt",
2097 "test3.txt",
2098 "subdir1",
2099 "subdir1", // duplicate
2100 "subdir1/test1.txt",
2101 "subdir1/test2.txt",
2102 "subdir2",
2103 "subdir2", // duplicate
2104 "subdir2/test3.txt",
2105 "subdir2/test4.txt",
2106 "subdir2/subdir",
2107 "subdir2/subdir/test1.txt",
2108 "subdir2/subdir/test2.txt",
2109 "subdir2/subdir/test3.txt",
2110 "subdir2/subdir/test4.txt",
2111 "subdir2/subdir/test5.txt",
2112 "subdir2/subdir/sub",
2113 "subdir2/subdir/sub/test0.txt",
2114 "subdir2/subdir/sub/120-px-file.txt",
2115 );
2116
2117 for ( $i = 0; $i < 25; $i++ ) {
2118 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
2119 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2120 "Locking of files succeeded ($backendName) ($i)." );
2121 $this->assertEquals( true, $status->isOK(),
2122 "Locking of files succeeded with OK status ($backendName) ($i)." );
2123
2124 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
2125 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2126 "Locking of files succeeded ($backendName) ($i)." );
2127 $this->assertEquals( true, $status->isOK(),
2128 "Locking of files succeeded with OK status ($backendName) ($i)." );
2129
2130 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
2131 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2132 "Locking of files succeeded ($backendName) ($i)." );
2133 $this->assertEquals( true, $status->isOK(),
2134 "Locking of files succeeded with OK status ($backendName) ($i)." );
2135
2136 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_EX );
2137 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2138 "Locking of files succeeded ($backendName). ($i)" );
2139 $this->assertEquals( true, $status->isOK(),
2140 "Locking of files succeeded with OK status ($backendName) ($i)." );
2141
2142 ## Flip the acquire/release ordering around ##
2143
2144 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
2145 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2146 "Locking of files succeeded ($backendName) ($i)." );
2147 $this->assertEquals( true, $status->isOK(),
2148 "Locking of files succeeded with OK status ($backendName) ($i)." );
2149
2150 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
2151 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2152 "Locking of files succeeded ($backendName) ($i)." );
2153 $this->assertEquals( true, $status->isOK(),
2154 "Locking of files succeeded with OK status ($backendName) ($i)." );
2155
2156 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_EX );
2157 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2158 "Locking of files succeeded ($backendName). ($i)" );
2159 $this->assertEquals( true, $status->isOK(),
2160 "Locking of files succeeded with OK status ($backendName) ($i)." );
2161
2162 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
2163 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2164 "Locking of files succeeded ($backendName) ($i)." );
2165 $this->assertEquals( true, $status->isOK(),
2166 "Locking of files succeeded with OK status ($backendName) ($i)." );
2167 }
2168
2169 $status = Status::newGood();
2170 $sl = $this->backend->getScopedFileLocks( $paths, LockManager::LOCK_EX, $status );
2171 $this->assertType( 'ScopedLock', $sl,
2172 "Scoped locking of files succeeded ($backendName)." );
2173 $this->assertEquals( array(), $status->errors,
2174 "Scoped locking of files succeeded ($backendName)." );
2175 $this->assertEquals( true, $status->isOK(),
2176 "Scoped locking of files succeeded with OK status ($backendName)." );
2177
2178 ScopedLock::release( $sl );
2179 $this->assertEquals( null, $sl,
2180 "Scoped unlocking of files succeeded ($backendName)." );
2181 $this->assertEquals( array(), $status->errors,
2182 "Scoped unlocking of files succeeded ($backendName)." );
2183 $this->assertEquals( true, $status->isOK(),
2184 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2185 }
2186
2187 // test helper wrapper for backend prepare() function
2188 private function prepare( array $params ) {
2189 return $this->backend->prepare( $params );
2190 }
2191
2192 // test helper wrapper for backend prepare() function
2193 private function create( array $params ) {
2194 $params['op'] = 'create';
2195 return $this->backend->doQuickOperations( array( $params ) );
2196 }
2197
2198 function tearDownFiles() {
2199 foreach ( $this->filesToPrune as $file ) {
2200 if ( is_file( $file ) ) {
2201 unlink( $file );
2202 }
2203 }
2204 $containers = array( 'unittest-cont1', 'unittest-cont2' );
2205 foreach ( $containers as $container ) {
2206 $this->deleteFiles( $container );
2207 }
2208 $this->filesToPrune = array();
2209 }
2210
2211 private function deleteFiles( $container ) {
2212 $base = self::baseStorePath();
2213 $iter = $this->backend->getFileList( array( 'dir' => "$base/$container" ) );
2214 if ( $iter ) {
2215 foreach ( $iter as $file ) {
2216 $this->backend->quickDelete( array( 'src' => "$base/$container/$file" ) );
2217 }
2218 // free the directory, to avoid Permission denied under windows on rmdir
2219 unset( $iter );
2220 }
2221 $this->backend->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
2222 }
2223
2224 function assertBackendPathsConsistent( array $paths ) {
2225 if ( $this->backend instanceof FileBackendMultiWrite ) {
2226 $status = $this->backend->consistencyCheck( $paths );
2227 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2228 }
2229 }
2230
2231 function assertGoodStatus( $status, $msg ) {
2232 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors, 1 ), $msg );
2233 }
2234 }