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