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