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