9fbf7bb0a47bf36c0b1677ff1f706d804dae5409
[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 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 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 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 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 $ops = 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 $ops[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand(0, 50000) );
827 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
828 }
829 $purgeOps[] = array( 'op' => 'null' );
830 $status = $this->backend->doQuickOperations( $ops );
831 $this->assertGoodStatus( $status,
832 "Creation of source files succeeded ($backendName)." );
833
834 foreach ( $files as $file ) {
835 $this->assertTrue( $this->backend->fileExists( array( 'src' => $file ) ),
836 "File $file exists." );
837 }
838
839 $status = $this->backend->doQuickOperations( $purgeOps );
840 $this->assertGoodStatus( $status,
841 "Quick deletion of source files succeeded ($backendName)." );
842
843 foreach ( $files as $file ) {
844 $this->assertFalse( $this->backend->fileExists( array( 'src' => $file ) ),
845 "File $file purged." );
846 }
847 }
848
849 /**
850 * @dataProvider provider_testConcatenate
851 */
852 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
853 $this->filesToPrune[] = $op['dst'];
854
855 $this->backend = $this->singleBackend;
856 $this->tearDownFiles();
857 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
858 $this->tearDownFiles();
859
860 $this->backend = $this->multiBackend;
861 $this->tearDownFiles();
862 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
863 $this->filesToPrune[] = $op['dst']; # avoid file leaking
864 $this->tearDownFiles();
865 }
866
867 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
868 $backendName = $this->backendClass();
869
870 $expContent = '';
871 // Create sources
872 $ops = array();
873 foreach ( $srcs as $i => $source ) {
874 $this->prepare( array( 'dir' => dirname( $source ) ) );
875 $ops[] = array(
876 'op' => 'create', // operation
877 'dst' => $source, // source
878 'content' => $srcsContent[$i]
879 );
880 $expContent .= $srcsContent[$i];
881 }
882 $status = $this->backend->doOperations( $ops );
883
884 $this->assertGoodStatus( $status,
885 "Creation of source files succeeded ($backendName)." );
886
887 $dest = $params['dst'];
888 if ( $alreadyExists ) {
889 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
890 $this->assertEquals( true, $ok,
891 "Creation of file at $dest succeeded ($backendName)." );
892 } else {
893 $ok = file_put_contents( $dest, '' ) !== false;
894 $this->assertEquals( true, $ok,
895 "Creation of 0-byte file at $dest succeeded ($backendName)." );
896 }
897
898 // Combine the files into one
899 $status = $this->backend->concatenate( $params );
900 if ( $okStatus ) {
901 $this->assertGoodStatus( $status,
902 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
903 $this->assertEquals( true, $status->isOK(),
904 "Creation of concat file at $dest succeeded ($backendName)." );
905 } else {
906 $this->assertEquals( false, $status->isOK(),
907 "Creation of concat file at $dest failed ($backendName)." );
908 }
909
910 if ( $okStatus ) {
911 $this->assertEquals( true, is_file( $dest ),
912 "Dest concat file $dest exists after creation ($backendName)." );
913 } else {
914 $this->assertEquals( true, is_file( $dest ),
915 "Dest concat file $dest exists after failed creation ($backendName)." );
916 }
917
918 $contents = file_get_contents( $dest );
919 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
920
921 if ( $okStatus ) {
922 $this->assertEquals( $expContent, $contents,
923 "Concat file at $dest has correct contents ($backendName)." );
924 } else {
925 $this->assertNotEquals( $expContent, $contents,
926 "Concat file at $dest has correct contents ($backendName)." );
927 }
928 }
929
930 function provider_testConcatenate() {
931 $cases = array();
932
933 $rand = mt_rand( 0, 2000000000 ) . time();
934 $dest = wfTempDir() . "/randomfile!$rand.txt";
935 $srcs = array(
936 self::baseStorePath() . '/unittest-cont1/e/file1.txt',
937 self::baseStorePath() . '/unittest-cont1/e/file2.txt',
938 self::baseStorePath() . '/unittest-cont1/e/file3.txt',
939 self::baseStorePath() . '/unittest-cont1/e/file4.txt',
940 self::baseStorePath() . '/unittest-cont1/e/file5.txt',
941 self::baseStorePath() . '/unittest-cont1/e/file6.txt',
942 self::baseStorePath() . '/unittest-cont1/e/file7.txt',
943 self::baseStorePath() . '/unittest-cont1/e/file8.txt',
944 self::baseStorePath() . '/unittest-cont1/e/file9.txt',
945 self::baseStorePath() . '/unittest-cont1/e/file10.txt'
946 );
947 $content = array(
948 'egfage',
949 'ageageag',
950 'rhokohlr',
951 'shgmslkg',
952 'kenga',
953 'owagmal',
954 'kgmae',
955 'g eak;g',
956 'lkaem;a',
957 'legma'
958 );
959 $params = array( 'srcs' => $srcs, 'dst' => $dest );
960
961 $cases[] = array(
962 $params, // operation
963 $srcs, // sources
964 $content, // content for each source
965 false, // no dest already exists
966 true, // succeeds
967 );
968
969 $cases[] = array(
970 $params, // operation
971 $srcs, // sources
972 $content, // content for each source
973 true, // dest already exists
974 false, // succeeds
975 );
976
977 return $cases;
978 }
979
980 /**
981 * @dataProvider provider_testGetFileStat
982 */
983 public function testGetFileStat( $path, $content, $alreadyExists ) {
984 $this->backend = $this->singleBackend;
985 $this->tearDownFiles();
986 $this->doTestGetFileStat( $path, $content, $alreadyExists );
987 $this->tearDownFiles();
988
989 $this->backend = $this->multiBackend;
990 $this->tearDownFiles();
991 $this->doTestGetFileStat( $path, $content, $alreadyExists );
992 $this->tearDownFiles();
993 }
994
995 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
996 $backendName = $this->backendClass();
997
998 if ( $alreadyExists ) {
999 $this->prepare( array( 'dir' => dirname( $path ) ) );
1000 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
1001 $this->assertGoodStatus( $status,
1002 "Creation of file at $path succeeded ($backendName)." );
1003
1004 $size = $this->backend->getFileSize( array( 'src' => $path ) );
1005 $time = $this->backend->getFileTimestamp( array( 'src' => $path ) );
1006 $stat = $this->backend->getFileStat( array( 'src' => $path ) );
1007
1008 $this->assertEquals( strlen( $content ), $size,
1009 "Correct file size of '$path'" );
1010 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
1011 "Correct file timestamp of '$path'" );
1012
1013 $size = $stat['size'];
1014 $time = $stat['mtime'];
1015 $this->assertEquals( strlen( $content ), $size,
1016 "Correct file size of '$path'" );
1017 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
1018 "Correct file timestamp of '$path'" );
1019
1020 $this->backend->clearCache( array( $path ) );
1021
1022 $size = $this->backend->getFileSize( array( 'src' => $path ) );
1023
1024 $this->assertEquals( strlen( $content ), $size,
1025 "Correct file size of '$path'" );
1026
1027 $this->backend->preloadCache( array( $path ) );
1028
1029 $size = $this->backend->getFileSize( array( 'src' => $path ) );
1030
1031 $this->assertEquals( strlen( $content ), $size,
1032 "Correct file size of '$path'" );
1033 } else {
1034 $size = $this->backend->getFileSize( array( 'src' => $path ) );
1035 $time = $this->backend->getFileTimestamp( array( 'src' => $path ) );
1036 $stat = $this->backend->getFileStat( array( 'src' => $path ) );
1037
1038 $this->assertFalse( $size, "Correct file size of '$path'" );
1039 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1040 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1041 }
1042 }
1043
1044 function provider_testGetFileStat() {
1045 $cases = array();
1046
1047 $base = self::baseStorePath();
1048 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
1049 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
1050 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
1051
1052 return $cases;
1053 }
1054
1055 /**
1056 * @dataProvider provider_testGetFileContents
1057 */
1058 public function testGetFileContents( $source, $content ) {
1059 $this->backend = $this->singleBackend;
1060 $this->tearDownFiles();
1061 $this->doTestGetFileContents( $source, $content );
1062 $this->tearDownFiles();
1063
1064 $this->backend = $this->multiBackend;
1065 $this->tearDownFiles();
1066 $this->doTestGetFileContents( $source, $content );
1067 $this->tearDownFiles();
1068 }
1069
1070 private function doTestGetFileContents( $source, $content ) {
1071 $backendName = $this->backendClass();
1072
1073 $srcs = (array)$source;
1074 $content = (array)$content;
1075 foreach ( $srcs as $i => $src ) {
1076 $this->prepare( array( 'dir' => dirname( $src ) ) );
1077 $status = $this->backend->doOperation(
1078 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1079 $this->assertGoodStatus( $status,
1080 "Creation of file at $src succeeded ($backendName)." );
1081 }
1082
1083 if ( is_array( $source ) ) {
1084 $contents = $this->backend->getFileContentsMulti( array( 'srcs' => $source ) );
1085 foreach ( $contents as $path => $data ) {
1086 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1087 $this->assertEquals( current( $content ), $data, "Contents of $path is correct ($backendName)." );
1088 next( $content );
1089 }
1090 $this->assertEquals( $source, array_keys( $contents ), "Contents in right order ($backendName)." );
1091 $this->assertEquals( count( $source ), count( $contents ), "Contents array size correct ($backendName)." );
1092 } else {
1093 $data = $this->backend->getFileContents( array( 'src' => $source ) );
1094 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1095 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1096 }
1097 }
1098
1099 function provider_testGetFileContents() {
1100 $cases = array();
1101
1102 $base = self::baseStorePath();
1103 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
1104 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
1105 $cases[] = array(
1106 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1107 "$base/unittest-cont1/e/a/z.txt" ),
1108 array( "contents xx", "contents xy", "contents xz" )
1109 );
1110
1111 return $cases;
1112 }
1113
1114 /**
1115 * @dataProvider provider_testGetLocalCopy
1116 */
1117 public function testGetLocalCopy( $source, $content ) {
1118 $this->backend = $this->singleBackend;
1119 $this->tearDownFiles();
1120 $this->doTestGetLocalCopy( $source, $content );
1121 $this->tearDownFiles();
1122
1123 $this->backend = $this->multiBackend;
1124 $this->tearDownFiles();
1125 $this->doTestGetLocalCopy( $source, $content );
1126 $this->tearDownFiles();
1127 }
1128
1129 private function doTestGetLocalCopy( $source, $content ) {
1130 $backendName = $this->backendClass();
1131
1132 $srcs = (array)$source;
1133 $content = (array)$content;
1134 foreach ( $srcs as $i => $src ) {
1135 $this->prepare( array( 'dir' => dirname( $src ) ) );
1136 $status = $this->backend->doOperation(
1137 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1138 $this->assertGoodStatus( $status,
1139 "Creation of file at $src succeeded ($backendName)." );
1140 }
1141
1142 if ( is_array( $source ) ) {
1143 $tmpFiles = $this->backend->getLocalCopyMulti( array( 'srcs' => $source ) );
1144 foreach ( $tmpFiles as $path => $tmpFile ) {
1145 $this->assertNotNull( $tmpFile,
1146 "Creation of local copy of $path succeeded ($backendName)." );
1147 $contents = file_get_contents( $tmpFile->getPath() );
1148 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1149 $this->assertEquals( current( $content ), $contents, "Local copy of $path is correct ($backendName)." );
1150 next( $content );
1151 }
1152 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local copies in right order ($backendName)." );
1153 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local copies array size correct ($backendName)." );
1154 } else {
1155 $tmpFile = $this->backend->getLocalCopy( array( 'src' => $source ) );
1156 $this->assertNotNull( $tmpFile,
1157 "Creation of local copy of $source succeeded ($backendName)." );
1158 $contents = file_get_contents( $tmpFile->getPath() );
1159 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1160 $this->assertEquals( $content[0], $contents, "Local copy of $source is correct ($backendName)." );
1161 }
1162
1163 $obj = new stdClass();
1164 $tmpFile->bind( $obj );
1165 }
1166
1167 function provider_testGetLocalCopy() {
1168 $cases = array();
1169
1170 $base = self::baseStorePath();
1171 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1172 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1173 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1174 $cases[] = array(
1175 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1176 "$base/unittest-cont1/e/a/z.txt" ),
1177 array( "contents xx", "contents xy", "contents xz" )
1178 );
1179
1180 return $cases;
1181 }
1182
1183 /**
1184 * @dataProvider provider_testGetLocalReference
1185 */
1186 public function testGetLocalReference( $source, $content ) {
1187 $this->backend = $this->singleBackend;
1188 $this->tearDownFiles();
1189 $this->doTestGetLocalReference( $source, $content );
1190 $this->tearDownFiles();
1191
1192 $this->backend = $this->multiBackend;
1193 $this->tearDownFiles();
1194 $this->doTestGetLocalReference( $source, $content );
1195 $this->tearDownFiles();
1196 }
1197
1198 private function doTestGetLocalReference( $source, $content ) {
1199 $backendName = $this->backendClass();
1200
1201 $srcs = (array)$source;
1202 $content = (array)$content;
1203 foreach ( $srcs as $i => $src ) {
1204 $this->prepare( array( 'dir' => dirname( $src ) ) );
1205 $status = $this->backend->doOperation(
1206 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1207 $this->assertGoodStatus( $status,
1208 "Creation of file at $src succeeded ($backendName)." );
1209 }
1210
1211 if ( is_array( $source ) ) {
1212 $tmpFiles = $this->backend->getLocalReferenceMulti( array( 'srcs' => $source ) );
1213 foreach ( $tmpFiles as $path => $tmpFile ) {
1214 $this->assertNotNull( $tmpFile,
1215 "Creation of local copy of $path succeeded ($backendName)." );
1216 $contents = file_get_contents( $tmpFile->getPath() );
1217 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1218 $this->assertEquals( current( $content ), $contents, "Local ref of $path is correct ($backendName)." );
1219 next( $content );
1220 }
1221 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local refs in right order ($backendName)." );
1222 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local refs array size correct ($backendName)." );
1223 } else {
1224 $tmpFile = $this->backend->getLocalReference( array( 'src' => $source ) );
1225 $this->assertNotNull( $tmpFile,
1226 "Creation of local copy of $source succeeded ($backendName)." );
1227 $contents = file_get_contents( $tmpFile->getPath() );
1228 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1229 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1230 }
1231 }
1232
1233 function provider_testGetLocalReference() {
1234 $cases = array();
1235
1236 $base = self::baseStorePath();
1237 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1238 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1239 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1240 $cases[] = array(
1241 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1242 "$base/unittest-cont1/e/a/z.txt" ),
1243 array( "contents xx", "contents xy", "contents xz" )
1244 );
1245
1246 return $cases;
1247 }
1248
1249 public function testGetLocalCopyAndReference404() {
1250 $this->backend = $this->singleBackend;
1251 $this->tearDownFiles();
1252 $this->doTestGetLocalCopyAndReference404();
1253 $this->tearDownFiles();
1254
1255 $this->backend = $this->multiBackend;
1256 $this->tearDownFiles();
1257 $this->doTestGetLocalCopyAndReference404();
1258 $this->tearDownFiles();
1259 }
1260
1261 public function doTestGetLocalCopyAndReference404() {
1262 $backendName = $this->backendClass();
1263
1264 $base = self::baseStorePath();
1265
1266 $tmpFile = $this->backend->getLocalCopy( array(
1267 'src' => "$base/unittest-cont1/not-there" ) );
1268 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1269
1270 $tmpFile = $this->backend->getLocalReference( array(
1271 'src' => "$base/unittest-cont1/not-there" ) );
1272 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1273 }
1274
1275 /**
1276 * @dataProvider provider_testGetFileHttpUrl
1277 */
1278 public function testGetFileHttpUrl( $source, $content ) {
1279 $this->backend = $this->singleBackend;
1280 $this->tearDownFiles();
1281 $this->doTestGetFileHttpUrl( $source, $content );
1282 $this->tearDownFiles();
1283
1284 $this->backend = $this->multiBackend;
1285 $this->tearDownFiles();
1286 $this->doTestGetFileHttpUrl( $source, $content );
1287 $this->tearDownFiles();
1288 }
1289
1290 private function doTestGetFileHttpUrl( $source, $content ) {
1291 $backendName = $this->backendClass();
1292
1293 $this->prepare( array( 'dir' => dirname( $source ) ) );
1294 $status = $this->backend->doOperation(
1295 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
1296 $this->assertGoodStatus( $status,
1297 "Creation of file at $source succeeded ($backendName)." );
1298
1299 $url = $this->backend->getFileHttpUrl( array( 'src' => $source ) );
1300
1301 if ( $url !== null ) { // supported
1302 $data = Http::request( "GET", $url );
1303 $this->assertEquals( $content, $data,
1304 "HTTP GET of URL has right contents ($backendName)." );
1305 }
1306 }
1307
1308 function provider_testGetFileHttpUrl() {
1309 $cases = array();
1310
1311 $base = self::baseStorePath();
1312 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1313 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1314 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1315
1316 return $cases;
1317 }
1318
1319 /**
1320 * @dataProvider provider_testPrepareAndClean
1321 */
1322 public function testPrepareAndClean( $path, $isOK ) {
1323 $this->backend = $this->singleBackend;
1324 $this->doTestPrepareAndClean( $path, $isOK );
1325 $this->tearDownFiles();
1326
1327 $this->backend = $this->multiBackend;
1328 $this->doTestPrepareAndClean( $path, $isOK );
1329 $this->tearDownFiles();
1330 }
1331
1332 function provider_testPrepareAndClean() {
1333 $base = self::baseStorePath();
1334 return array(
1335 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1336 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1337 # Specific to FS backend with no basePath field set
1338 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1339 );
1340 }
1341
1342 private function doTestPrepareAndClean( $path, $isOK ) {
1343 $backendName = $this->backendClass();
1344
1345 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1346 if ( $isOK ) {
1347 $this->assertGoodStatus( $status,
1348 "Preparing dir $path succeeded without warnings ($backendName)." );
1349 $this->assertEquals( true, $status->isOK(),
1350 "Preparing dir $path succeeded ($backendName)." );
1351 } else {
1352 $this->assertEquals( false, $status->isOK(),
1353 "Preparing dir $path failed ($backendName)." );
1354 }
1355
1356 $status = $this->backend->clean( array( 'dir' => dirname( $path ) ) );
1357 if ( $isOK ) {
1358 $this->assertGoodStatus( $status,
1359 "Cleaning dir $path succeeded without warnings ($backendName)." );
1360 $this->assertEquals( true, $status->isOK(),
1361 "Cleaning dir $path succeeded ($backendName)." );
1362 } else {
1363 $this->assertEquals( false, $status->isOK(),
1364 "Cleaning dir $path failed ($backendName)." );
1365 }
1366 }
1367
1368 public function testRecursiveClean() {
1369 $this->backend = $this->singleBackend;
1370 $this->doTestRecursiveClean();
1371 $this->tearDownFiles();
1372
1373 $this->backend = $this->multiBackend;
1374 $this->doTestRecursiveClean();
1375 $this->tearDownFiles();
1376 }
1377
1378 private function doTestRecursiveClean() {
1379 $backendName = $this->backendClass();
1380
1381 $base = self::baseStorePath();
1382 $dirs = array(
1383 "$base/unittest-cont1",
1384 "$base/unittest-cont1/e",
1385 "$base/unittest-cont1/e/a",
1386 "$base/unittest-cont1/e/a/b",
1387 "$base/unittest-cont1/e/a/b/c",
1388 "$base/unittest-cont1/e/a/b/c/d0",
1389 "$base/unittest-cont1/e/a/b/c/d1",
1390 "$base/unittest-cont1/e/a/b/c/d2",
1391 "$base/unittest-cont1/e/a/b/c/d0/1",
1392 "$base/unittest-cont1/e/a/b/c/d0/2",
1393 "$base/unittest-cont1/e/a/b/c/d1/3",
1394 "$base/unittest-cont1/e/a/b/c/d1/4",
1395 "$base/unittest-cont1/e/a/b/c/d2/5",
1396 "$base/unittest-cont1/e/a/b/c/d2/6"
1397 );
1398 foreach ( $dirs as $dir ) {
1399 $status = $this->prepare( array( 'dir' => $dir ) );
1400 $this->assertGoodStatus( $status,
1401 "Preparing dir $dir succeeded without warnings ($backendName)." );
1402 }
1403
1404 if ( $this->backend instanceof FSFileBackend ) {
1405 foreach ( $dirs as $dir ) {
1406 $this->assertEquals( true, $this->backend->directoryExists( array( 'dir' => $dir ) ),
1407 "Dir $dir exists ($backendName)." );
1408 }
1409 }
1410
1411 $status = $this->backend->clean(
1412 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1413 $this->assertGoodStatus( $status,
1414 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1415
1416 foreach ( $dirs as $dir ) {
1417 $this->assertEquals( false, $this->backend->directoryExists( array( 'dir' => $dir ) ),
1418 "Dir $dir no longer exists ($backendName)." );
1419 }
1420 }
1421
1422 // @TODO: testSecure
1423
1424 public function testDoOperations() {
1425 $this->backend = $this->singleBackend;
1426 $this->tearDownFiles();
1427 $this->doTestDoOperations();
1428 $this->tearDownFiles();
1429
1430 $this->backend = $this->multiBackend;
1431 $this->tearDownFiles();
1432 $this->doTestDoOperations();
1433 $this->tearDownFiles();
1434 }
1435
1436 private function doTestDoOperations() {
1437 $base = self::baseStorePath();
1438
1439 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1440 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1441 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1442 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1443 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1444 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1445 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1446
1447 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1448 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1449 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1450 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1451 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1452 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1453 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1454
1455 $status = $this->backend->doOperations( array(
1456 array( 'op' => 'describe', 'src' => $fileA,
1457 'headers' => array( 'X-Content-Length' => '91.3' ), 'disposition' => 'inline' ),
1458 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1459 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1460 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1461 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1462 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1463 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1464 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1465 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1466 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1467 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1468 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1469 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1470 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1471 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1472 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1473 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1474 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1475 // Does nothing
1476 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1477 // Does nothing
1478 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1479 // Does nothing
1480 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1481 // Does nothing
1482 array( 'op' => 'null' ),
1483 // Does nothing
1484 ) );
1485
1486 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1487 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1488 $this->assertEquals( 14, count( $status->success ),
1489 "Operation batch has correct success array" );
1490
1491 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
1492 "File does not exist at $fileA" );
1493 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1494 "File does not exist at $fileB" );
1495 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1496 "File does not exist at $fileD" );
1497
1498 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1499 "File exists at $fileC" );
1500 $this->assertEquals( $fileBContents,
1501 $this->backend->getFileContents( array( 'src' => $fileC ) ),
1502 "Correct file contents of $fileC" );
1503 $this->assertEquals( strlen( $fileBContents ),
1504 $this->backend->getFileSize( array( 'src' => $fileC ) ),
1505 "Correct file size of $fileC" );
1506 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1507 $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
1508 "Correct file SHA-1 of $fileC" );
1509 }
1510
1511 public function testDoOperationsPipeline() {
1512 $this->backend = $this->singleBackend;
1513 $this->tearDownFiles();
1514 $this->doTestDoOperationsPipeline();
1515 $this->tearDownFiles();
1516
1517 $this->backend = $this->multiBackend;
1518 $this->tearDownFiles();
1519 $this->doTestDoOperationsPipeline();
1520 $this->tearDownFiles();
1521 }
1522
1523 // concurrency orientated
1524 private function doTestDoOperationsPipeline() {
1525 $base = self::baseStorePath();
1526
1527 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1528 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1529 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1530
1531 $tmpNameA = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1532 file_put_contents( $tmpNameA, $fileAContents );
1533 $tmpNameB = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1534 file_put_contents( $tmpNameB, $fileBContents );
1535 $tmpNameC = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1536 file_put_contents( $tmpNameC, $fileCContents );
1537
1538 $this->filesToPrune[] = $tmpNameA; # avoid file leaking
1539 $this->filesToPrune[] = $tmpNameB; # avoid file leaking
1540 $this->filesToPrune[] = $tmpNameC; # avoid file leaking
1541
1542 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1543 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1544 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1545 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1546
1547 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1548 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1549 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1550 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1551 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1552
1553 $status = $this->backend->doOperations( array(
1554 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1555 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1556 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1557 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1558 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1559 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1560 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1561 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1562 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1563 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1564 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1565 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1566 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1567 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1568 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1569 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1570 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1571 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1572 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1573 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1574 // Does nothing
1575 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1576 // Does nothing
1577 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1578 // Does nothing
1579 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1580 // Does nothing
1581 array( 'op' => 'null' ),
1582 // Does nothing
1583 ) );
1584
1585 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1586 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1587 $this->assertEquals( 16, count( $status->success ),
1588 "Operation batch has correct success array" );
1589
1590 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
1591 "File does not exist at $fileA" );
1592 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1593 "File does not exist at $fileB" );
1594 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1595 "File does not exist at $fileD" );
1596
1597 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1598 "File exists at $fileC" );
1599 $this->assertEquals( $fileBContents,
1600 $this->backend->getFileContents( array( 'src' => $fileC ) ),
1601 "Correct file contents of $fileC" );
1602 $this->assertEquals( strlen( $fileBContents ),
1603 $this->backend->getFileSize( array( 'src' => $fileC ) ),
1604 "Correct file size of $fileC" );
1605 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1606 $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
1607 "Correct file SHA-1 of $fileC" );
1608 }
1609
1610 public function testDoOperationsFailing() {
1611 $this->backend = $this->singleBackend;
1612 $this->tearDownFiles();
1613 $this->doTestDoOperationsFailing();
1614 $this->tearDownFiles();
1615
1616 $this->backend = $this->multiBackend;
1617 $this->tearDownFiles();
1618 $this->doTestDoOperationsFailing();
1619 $this->tearDownFiles();
1620 }
1621
1622 private function doTestDoOperationsFailing() {
1623 $base = self::baseStorePath();
1624
1625 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1626 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1627 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1628 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1629 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1630 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1631 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1632
1633 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1634 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1635 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1636 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1637 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1638 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1639
1640 $status = $this->backend->doOperations( array(
1641 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1642 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1643 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1644 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1645 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1646 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1647 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1648 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1649 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1650 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1651 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1652 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1653 array( 'op' => 'delete', 'src' => $fileD ),
1654 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1655 array( 'op' => 'null' ),
1656 // Does nothing
1657 ), array( 'force' => 1 ) );
1658
1659 $this->assertNotEquals( array(), $status->errors, "Operation had warnings" );
1660 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1661 $this->assertEquals( 8, count( $status->success ),
1662 "Operation batch has correct success array" );
1663
1664 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1665 "File does not exist at $fileB" );
1666 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1667 "File does not exist at $fileD" );
1668
1669 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileA ) ),
1670 "File does not exist at $fileA" );
1671 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1672 "File exists at $fileC" );
1673 $this->assertEquals( $fileBContents,
1674 $this->backend->getFileContents( array( 'src' => $fileA ) ),
1675 "Correct file contents of $fileA" );
1676 $this->assertEquals( strlen( $fileBContents ),
1677 $this->backend->getFileSize( array( 'src' => $fileA ) ),
1678 "Correct file size of $fileA" );
1679 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1680 $this->backend->getFileSha1Base36( array( 'src' => $fileA ) ),
1681 "Correct file SHA-1 of $fileA" );
1682 }
1683
1684 public function testGetFileList() {
1685 $this->backend = $this->singleBackend;
1686 $this->tearDownFiles();
1687 $this->doTestGetFileList();
1688 $this->tearDownFiles();
1689
1690 $this->backend = $this->multiBackend;
1691 $this->tearDownFiles();
1692 $this->doTestGetFileList();
1693 $this->tearDownFiles();
1694 }
1695
1696 private function doTestGetFileList() {
1697 $backendName = $this->backendClass();
1698 $base = self::baseStorePath();
1699
1700 // Should have no errors
1701 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1702
1703 $files = array(
1704 "$base/unittest-cont1/e/test1.txt",
1705 "$base/unittest-cont1/e/test2.txt",
1706 "$base/unittest-cont1/e/test3.txt",
1707 "$base/unittest-cont1/e/subdir1/test1.txt",
1708 "$base/unittest-cont1/e/subdir1/test2.txt",
1709 "$base/unittest-cont1/e/subdir2/test3.txt",
1710 "$base/unittest-cont1/e/subdir2/test4.txt",
1711 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1712 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1713 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1714 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1715 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1716 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1717 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1718 );
1719
1720 // Add the files
1721 $ops = array();
1722 foreach ( $files as $file ) {
1723 $this->prepare( array( 'dir' => dirname( $file ) ) );
1724 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1725 }
1726 $status = $this->backend->doQuickOperations( $ops );
1727 $this->assertGoodStatus( $status,
1728 "Creation of files succeeded ($backendName)." );
1729 $this->assertEquals( true, $status->isOK(),
1730 "Creation of files succeeded with OK status ($backendName)." );
1731
1732 // Expected listing
1733 $expected = array(
1734 "e/test1.txt",
1735 "e/test2.txt",
1736 "e/test3.txt",
1737 "e/subdir1/test1.txt",
1738 "e/subdir1/test2.txt",
1739 "e/subdir2/test3.txt",
1740 "e/subdir2/test4.txt",
1741 "e/subdir2/subdir/test1.txt",
1742 "e/subdir2/subdir/test2.txt",
1743 "e/subdir2/subdir/test3.txt",
1744 "e/subdir2/subdir/test4.txt",
1745 "e/subdir2/subdir/test5.txt",
1746 "e/subdir2/subdir/sub/test0.txt",
1747 "e/subdir2/subdir/sub/120-px-file.txt",
1748 );
1749 sort( $expected );
1750
1751 // Actual listing (no trailing slash)
1752 $list = array();
1753 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1754 foreach ( $iter as $file ) {
1755 $list[] = $file;
1756 }
1757 sort( $list );
1758
1759 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1760
1761 // Actual listing (with trailing slash)
1762 $list = array();
1763 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1764 foreach ( $iter as $file ) {
1765 $list[] = $file;
1766 }
1767 sort( $list );
1768
1769 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1770
1771 // Expected listing
1772 $expected = array(
1773 "test1.txt",
1774 "test2.txt",
1775 "test3.txt",
1776 "test4.txt",
1777 "test5.txt",
1778 "sub/test0.txt",
1779 "sub/120-px-file.txt",
1780 );
1781 sort( $expected );
1782
1783 // Actual listing (no trailing slash)
1784 $list = array();
1785 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1786 foreach ( $iter as $file ) {
1787 $list[] = $file;
1788 }
1789 sort( $list );
1790
1791 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1792
1793 // Actual listing (with trailing slash)
1794 $list = array();
1795 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1796 foreach ( $iter as $file ) {
1797 $list[] = $file;
1798 }
1799 sort( $list );
1800
1801 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1802
1803 // Actual listing (using iterator second time)
1804 $list = array();
1805 foreach ( $iter as $file ) {
1806 $list[] = $file;
1807 }
1808 sort( $list );
1809
1810 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1811
1812 // Expected listing (top files only)
1813 $expected = array(
1814 "test1.txt",
1815 "test2.txt",
1816 "test3.txt",
1817 "test4.txt",
1818 "test5.txt"
1819 );
1820 sort( $expected );
1821
1822 // Actual listing (top files only)
1823 $list = array();
1824 $iter = $this->backend->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1825 foreach ( $iter as $file ) {
1826 $list[] = $file;
1827 }
1828 sort( $list );
1829
1830 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1831
1832 foreach ( $files as $file ) { // clean up
1833 $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1834 }
1835
1836 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1837 foreach ( $iter as $iter ) {} // no errors
1838 }
1839
1840 public function testGetDirectoryList() {
1841 $this->backend = $this->singleBackend;
1842 $this->tearDownFiles();
1843 $this->doTestGetDirectoryList();
1844 $this->tearDownFiles();
1845
1846 $this->backend = $this->multiBackend;
1847 $this->tearDownFiles();
1848 $this->doTestGetDirectoryList();
1849 $this->tearDownFiles();
1850 }
1851
1852 private function doTestGetDirectoryList() {
1853 $backendName = $this->backendClass();
1854
1855 $base = self::baseStorePath();
1856 $files = array(
1857 "$base/unittest-cont1/e/test1.txt",
1858 "$base/unittest-cont1/e/test2.txt",
1859 "$base/unittest-cont1/e/test3.txt",
1860 "$base/unittest-cont1/e/subdir1/test1.txt",
1861 "$base/unittest-cont1/e/subdir1/test2.txt",
1862 "$base/unittest-cont1/e/subdir2/test3.txt",
1863 "$base/unittest-cont1/e/subdir2/test4.txt",
1864 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1865 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1866 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1867 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1868 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1869 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1870 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1871 );
1872
1873 // Add the files
1874 $ops = array();
1875 foreach ( $files as $file ) {
1876 $this->prepare( array( 'dir' => dirname( $file ) ) );
1877 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1878 }
1879 $status = $this->backend->doQuickOperations( $ops );
1880 $this->assertGoodStatus( $status,
1881 "Creation of files succeeded ($backendName)." );
1882 $this->assertEquals( true, $status->isOK(),
1883 "Creation of files succeeded with OK status ($backendName)." );
1884
1885 $this->assertEquals( true,
1886 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1887 "Directory exists in ($backendName)." );
1888 $this->assertEquals( true,
1889 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1890 "Directory exists in ($backendName)." );
1891 $this->assertEquals( false,
1892 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1893 "Directory does not exists in ($backendName)." );
1894
1895 // Expected listing
1896 $expected = array(
1897 "e",
1898 );
1899 sort( $expected );
1900
1901 // Actual listing (no trailing slash)
1902 $list = array();
1903 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1904 foreach ( $iter as $file ) {
1905 $list[] = $file;
1906 }
1907 sort( $list );
1908
1909 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1910
1911 // Expected listing
1912 $expected = array(
1913 "subdir1",
1914 "subdir2",
1915 "subdir3",
1916 "subdir4",
1917 );
1918 sort( $expected );
1919
1920 // Actual listing (no trailing slash)
1921 $list = array();
1922 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1923 foreach ( $iter as $file ) {
1924 $list[] = $file;
1925 }
1926 sort( $list );
1927
1928 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1929
1930 // Actual listing (with trailing slash)
1931 $list = array();
1932 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1933 foreach ( $iter as $file ) {
1934 $list[] = $file;
1935 }
1936 sort( $list );
1937
1938 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1939
1940 // Expected listing
1941 $expected = array(
1942 "subdir",
1943 );
1944 sort( $expected );
1945
1946 // Actual listing (no trailing slash)
1947 $list = array();
1948 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1949 foreach ( $iter as $file ) {
1950 $list[] = $file;
1951 }
1952 sort( $list );
1953
1954 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1955
1956 // Actual listing (with trailing slash)
1957 $list = array();
1958 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
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 (using iterator second time)
1967 $list = array();
1968 foreach ( $iter as $file ) {
1969 $list[] = $file;
1970 }
1971 sort( $list );
1972
1973 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1974
1975 // Expected listing (recursive)
1976 $expected = array(
1977 "e",
1978 "e/subdir1",
1979 "e/subdir2",
1980 "e/subdir3",
1981 "e/subdir4",
1982 "e/subdir2/subdir",
1983 "e/subdir3/subdir",
1984 "e/subdir4/subdir",
1985 "e/subdir4/subdir/sub",
1986 );
1987 sort( $expected );
1988
1989 // Actual listing (recursive)
1990 $list = array();
1991 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1992 foreach ( $iter as $file ) {
1993 $list[] = $file;
1994 }
1995 sort( $list );
1996
1997 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1998
1999 // Expected listing (recursive)
2000 $expected = array(
2001 "subdir",
2002 "subdir/sub",
2003 );
2004 sort( $expected );
2005
2006 // Actual listing (recursive)
2007 $list = array();
2008 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
2009 foreach ( $iter as $file ) {
2010 $list[] = $file;
2011 }
2012 sort( $list );
2013
2014 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2015
2016 // Actual listing (recursive, second time)
2017 $list = array();
2018 foreach ( $iter as $file ) {
2019 $list[] = $file;
2020 }
2021 sort( $list );
2022
2023 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2024
2025 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) );
2026 $items = is_array( $iter ) ? $iter : iterator_to_array( $iter );
2027 $this->assertEquals( array(), $items, "Directory listing is empty." );
2028
2029 foreach ( $files as $file ) { // clean up
2030 $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
2031 }
2032
2033 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
2034 foreach ( $iter as $file ) {} // no errors
2035 $items = is_array( $iter ) ? $iter : iterator_to_array( $iter );
2036 $this->assertEquals( array(), $items, "Directory listing is empty." );
2037
2038 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/not/exists" ) );
2039 $items = is_array( $iter ) ? $iter : iterator_to_array( $iter );
2040 $this->assertEquals( array(), $items, "Directory listing is empty." );
2041 }
2042
2043 public function testLockCalls() {
2044 $this->backend = $this->singleBackend;
2045 $this->doTestLockCalls();
2046 }
2047
2048 private function doTestLockCalls() {
2049 $backendName = $this->backendClass();
2050
2051 $paths = array(
2052 "test1.txt",
2053 "test2.txt",
2054 "test3.txt",
2055 "subdir1",
2056 "subdir1", // duplicate
2057 "subdir1/test1.txt",
2058 "subdir1/test2.txt",
2059 "subdir2",
2060 "subdir2", // duplicate
2061 "subdir2/test3.txt",
2062 "subdir2/test4.txt",
2063 "subdir2/subdir",
2064 "subdir2/subdir/test1.txt",
2065 "subdir2/subdir/test2.txt",
2066 "subdir2/subdir/test3.txt",
2067 "subdir2/subdir/test4.txt",
2068 "subdir2/subdir/test5.txt",
2069 "subdir2/subdir/sub",
2070 "subdir2/subdir/sub/test0.txt",
2071 "subdir2/subdir/sub/120-px-file.txt",
2072 );
2073
2074 for ( $i=0; $i<25; $i++ ) {
2075 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
2076 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2077 "Locking of files succeeded ($backendName) ($i)." );
2078 $this->assertEquals( true, $status->isOK(),
2079 "Locking of files succeeded with OK status ($backendName) ($i)." );
2080
2081 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
2082 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2083 "Locking of files succeeded ($backendName) ($i)." );
2084 $this->assertEquals( true, $status->isOK(),
2085 "Locking of files succeeded with OK status ($backendName) ($i)." );
2086
2087 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
2088 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2089 "Locking of files succeeded ($backendName) ($i)." );
2090 $this->assertEquals( true, $status->isOK(),
2091 "Locking of files succeeded with OK status ($backendName) ($i)." );
2092
2093 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_EX );
2094 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2095 "Locking of files succeeded ($backendName). ($i)" );
2096 $this->assertEquals( true, $status->isOK(),
2097 "Locking of files succeeded with OK status ($backendName) ($i)." );
2098
2099 ## Flip the acquire/release ordering around ##
2100
2101 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
2102 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2103 "Locking of files succeeded ($backendName) ($i)." );
2104 $this->assertEquals( true, $status->isOK(),
2105 "Locking of files succeeded with OK status ($backendName) ($i)." );
2106
2107 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
2108 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2109 "Locking of files succeeded ($backendName) ($i)." );
2110 $this->assertEquals( true, $status->isOK(),
2111 "Locking of files succeeded with OK status ($backendName) ($i)." );
2112
2113 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_EX );
2114 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2115 "Locking of files succeeded ($backendName). ($i)" );
2116 $this->assertEquals( true, $status->isOK(),
2117 "Locking of files succeeded with OK status ($backendName) ($i)." );
2118
2119 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
2120 $this->assertEquals( print_r( array(), true ), print_r( $status->errors, true ),
2121 "Locking of files succeeded ($backendName) ($i)." );
2122 $this->assertEquals( true, $status->isOK(),
2123 "Locking of files succeeded with OK status ($backendName) ($i)." );
2124 }
2125
2126 $status = Status::newGood();
2127 $sl = $this->backend->getScopedFileLocks( $paths, LockManager::LOCK_EX, $status );
2128 $this->assertType( 'ScopedLock', $sl,
2129 "Scoped locking of files succeeded ($backendName)." );
2130 $this->assertEquals( array(), $status->errors,
2131 "Scoped locking of files succeeded ($backendName)." );
2132 $this->assertEquals( true, $status->isOK(),
2133 "Scoped locking of files succeeded with OK status ($backendName)." );
2134
2135 ScopedLock::release( $sl );
2136 $this->assertEquals( null, $sl,
2137 "Scoped unlocking of files succeeded ($backendName)." );
2138 $this->assertEquals( array(), $status->errors,
2139 "Scoped unlocking of files succeeded ($backendName)." );
2140 $this->assertEquals( true, $status->isOK(),
2141 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2142 }
2143
2144 // test helper wrapper for backend prepare() function
2145 private function prepare( array $params ) {
2146 return $this->backend->prepare( $params );
2147 }
2148
2149 // test helper wrapper for backend prepare() function
2150 private function create( array $params ) {
2151 $params['op'] = 'create';
2152 return $this->backend->doQuickOperations( array( $params ) );
2153 }
2154
2155 function tearDownFiles() {
2156 foreach ( $this->filesToPrune as $file ) {
2157 @unlink( $file );
2158 }
2159 $containers = array( 'unittest-cont1', 'unittest-cont2' );
2160 foreach ( $containers as $container ) {
2161 $this->deleteFiles( $container );
2162 }
2163 $this->filesToPrune = array();
2164 }
2165
2166 private function deleteFiles( $container ) {
2167 $base = self::baseStorePath();
2168 $iter = $this->backend->getFileList( array( 'dir' => "$base/$container" ) );
2169 if ( $iter ) {
2170 foreach ( $iter as $file ) {
2171 $this->backend->quickDelete( array( 'src' => "$base/$container/$file" ) );
2172 }
2173 // free the directory, to avoid Permission denied under windows on rmdir
2174 unset( $iter );
2175 }
2176 $this->backend->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
2177 }
2178
2179 function assertBackendPathsConsistent( array $paths ) {
2180 if ( $this->backend instanceof FileBackendMultiWrite ) {
2181 $status = $this->backend->consistencyCheck( $paths );
2182 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2183 }
2184 }
2185
2186 function assertGoodStatus( $status, $msg ) {
2187 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors, 1 ), $msg );
2188 }
2189 }