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