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