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