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