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