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