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