Merge "Fix regression in API log events (bug 35635)"
[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 } else {
886 $size = $this->backend->getFileSize( array( 'src' => $path ) );
887 $time = $this->backend->getFileTimestamp( array( 'src' => $path ) );
888 $stat = $this->backend->getFileStat( array( 'src' => $path ) );
889
890 $this->assertFalse( $size, "Correct file size of '$path'" );
891 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
892 $this->assertFalse( $stat, "Correct file stat of '$path'" );
893 }
894 }
895
896 function provider_testGetFileStat() {
897 $cases = array();
898
899 $base = $this->baseStorePath();
900 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
901 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
902 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
903
904 return $cases;
905 }
906
907 /**
908 * @dataProvider provider_testGetFileContents
909 */
910 public function testGetFileContents( $source, $content ) {
911 $this->backend = $this->singleBackend;
912 $this->tearDownFiles();
913 $this->doTestGetFileContents( $source, $content );
914 $this->tearDownFiles();
915
916 $this->backend = $this->multiBackend;
917 $this->tearDownFiles();
918 $this->doTestGetFileContents( $source, $content );
919 $this->tearDownFiles();
920 }
921
922 private function doTestGetFileContents( $source, $content ) {
923 $backendName = $this->backendClass();
924
925 $this->prepare( array( 'dir' => dirname( $source ) ) );
926
927 $status = $this->backend->doOperation(
928 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
929 $this->assertGoodStatus( $status,
930 "Creation of file at $source succeeded ($backendName)." );
931 $this->assertEquals( true, $status->isOK(),
932 "Creation of file at $source succeeded with OK status ($backendName)." );
933
934 $newContents = $this->backend->getFileContents( array( 'src' => $source, 'latest' => 1 ) );
935 $this->assertNotEquals( false, $newContents,
936 "Read of file at $source succeeded ($backendName)." );
937
938 $this->assertEquals( $content, $newContents,
939 "Contents read match data at $source ($backendName)." );
940 }
941
942 function provider_testGetFileContents() {
943 $cases = array();
944
945 $base = $this->baseStorePath();
946 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
947 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
948
949 return $cases;
950 }
951
952 /**
953 * @dataProvider provider_testGetLocalCopy
954 */
955 public function testGetLocalCopy( $source, $content ) {
956 $this->backend = $this->singleBackend;
957 $this->tearDownFiles();
958 $this->doTestGetLocalCopy( $source, $content );
959 $this->tearDownFiles();
960
961 $this->backend = $this->multiBackend;
962 $this->tearDownFiles();
963 $this->doTestGetLocalCopy( $source, $content );
964 $this->tearDownFiles();
965 }
966
967 private function doTestGetLocalCopy( $source, $content ) {
968 $backendName = $this->backendClass();
969
970 $this->prepare( array( 'dir' => dirname( $source ) ) );
971
972 $status = $this->backend->doOperation(
973 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
974 $this->assertGoodStatus( $status,
975 "Creation of file at $source succeeded ($backendName)." );
976
977 $tmpFile = $this->backend->getLocalCopy( array( 'src' => $source ) );
978 $this->assertNotNull( $tmpFile,
979 "Creation of local copy of $source succeeded ($backendName)." );
980
981 $contents = file_get_contents( $tmpFile->getPath() );
982 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
983 }
984
985 function provider_testGetLocalCopy() {
986 $cases = array();
987
988 $base = $this->baseStorePath();
989 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
990 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
991
992 return $cases;
993 }
994
995 /**
996 * @dataProvider provider_testGetLocalReference
997 */
998 public function testGetLocalReference( $source, $content ) {
999 $this->backend = $this->singleBackend;
1000 $this->tearDownFiles();
1001 $this->doTestGetLocalReference( $source, $content );
1002 $this->tearDownFiles();
1003
1004 $this->backend = $this->multiBackend;
1005 $this->tearDownFiles();
1006 $this->doTestGetLocalReference( $source, $content );
1007 $this->tearDownFiles();
1008 }
1009
1010 private function doTestGetLocalReference( $source, $content ) {
1011 $backendName = $this->backendClass();
1012
1013 $this->prepare( array( 'dir' => dirname( $source ) ) );
1014
1015 $status = $this->create( array( 'content' => $content, 'dst' => $source ) );
1016 $this->assertGoodStatus( $status,
1017 "Creation of file at $source succeeded ($backendName)." );
1018
1019 $tmpFile = $this->backend->getLocalReference( array( 'src' => $source ) );
1020 $this->assertNotNull( $tmpFile,
1021 "Creation of local copy of $source succeeded ($backendName)." );
1022
1023 $contents = file_get_contents( $tmpFile->getPath() );
1024 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1025 }
1026
1027 function provider_testGetLocalReference() {
1028 $cases = array();
1029
1030 $base = $this->baseStorePath();
1031 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1032 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1033
1034 return $cases;
1035 }
1036
1037 /**
1038 * @dataProvider provider_testPrepareAndClean
1039 */
1040 public function testPrepareAndClean( $path, $isOK ) {
1041 $this->backend = $this->singleBackend;
1042 $this->doTestPrepareAndClean( $path, $isOK );
1043 $this->tearDownFiles();
1044
1045 $this->backend = $this->multiBackend;
1046 $this->doTestPrepareAndClean( $path, $isOK );
1047 $this->tearDownFiles();
1048 }
1049
1050 function provider_testPrepareAndClean() {
1051 $base = $this->baseStorePath();
1052 return array(
1053 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1054 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1055 # Specific to FS backend with no basePath field set
1056 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1057 );
1058 }
1059
1060 private function doTestPrepareAndClean( $path, $isOK ) {
1061 $backendName = $this->backendClass();
1062
1063 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1064 if ( $isOK ) {
1065 $this->assertGoodStatus( $status,
1066 "Preparing dir $path succeeded without warnings ($backendName)." );
1067 $this->assertEquals( true, $status->isOK(),
1068 "Preparing dir $path succeeded ($backendName)." );
1069 } else {
1070 $this->assertEquals( false, $status->isOK(),
1071 "Preparing dir $path failed ($backendName)." );
1072 }
1073
1074 $status = $this->backend->clean( array( 'dir' => dirname( $path ) ) );
1075 if ( $isOK ) {
1076 $this->assertGoodStatus( $status,
1077 "Cleaning dir $path succeeded without warnings ($backendName)." );
1078 $this->assertEquals( true, $status->isOK(),
1079 "Cleaning dir $path succeeded ($backendName)." );
1080 } else {
1081 $this->assertEquals( false, $status->isOK(),
1082 "Cleaning dir $path failed ($backendName)." );
1083 }
1084 }
1085
1086 public function testRecursiveClean() {
1087 $this->backend = $this->singleBackend;
1088 $this->doTestRecursiveClean();
1089 $this->tearDownFiles();
1090
1091 $this->backend = $this->multiBackend;
1092 $this->doTestRecursiveClean();
1093 $this->tearDownFiles();
1094 }
1095
1096 private function doTestRecursiveClean() {
1097 $backendName = $this->backendClass();
1098
1099 $base = $this->baseStorePath();
1100 $dirs = array(
1101 "$base/unittest-cont1/e/a",
1102 "$base/unittest-cont1/e/a/b",
1103 "$base/unittest-cont1/e/a/b/c",
1104 "$base/unittest-cont1/e/a/b/c/d0",
1105 "$base/unittest-cont1/e/a/b/c/d1",
1106 "$base/unittest-cont1/e/a/b/c/d2",
1107 "$base/unittest-cont1/e/a/b/c/d0/1",
1108 "$base/unittest-cont1/e/a/b/c/d0/2",
1109 "$base/unittest-cont1/e/a/b/c/d1/3",
1110 "$base/unittest-cont1/e/a/b/c/d1/4",
1111 "$base/unittest-cont1/e/a/b/c/d2/5",
1112 "$base/unittest-cont1/e/a/b/c/d2/6"
1113 );
1114 foreach ( $dirs as $dir ) {
1115 $status = $this->prepare( array( 'dir' => $dir ) );
1116 $this->assertGoodStatus( $status,
1117 "Preparing dir $dir succeeded without warnings ($backendName)." );
1118 }
1119
1120 if ( $this->backend instanceof FSFileBackend ) {
1121 foreach ( $dirs as $dir ) {
1122 $this->assertEquals( true, $this->backend->directoryExists( array( 'dir' => $dir ) ),
1123 "Dir $dir exists ($backendName)." );
1124 }
1125 }
1126
1127 $status = $this->backend->clean(
1128 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1129 $this->assertGoodStatus( $status,
1130 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1131
1132 foreach ( $dirs as $dir ) {
1133 $this->assertEquals( false, $this->backend->directoryExists( array( 'dir' => $dir ) ),
1134 "Dir $dir no longer exists ($backendName)." );
1135 }
1136 }
1137
1138 // @TODO: testSecure
1139
1140 public function testDoOperations() {
1141 $this->backend = $this->singleBackend;
1142 $this->tearDownFiles();
1143 $this->doTestDoOperations();
1144 $this->tearDownFiles();
1145
1146 $this->backend = $this->multiBackend;
1147 $this->tearDownFiles();
1148 $this->doTestDoOperations();
1149 $this->tearDownFiles();
1150
1151 $this->backend = $this->singleBackend;
1152 $this->tearDownFiles();
1153 $this->doTestDoOperations2();
1154 $this->tearDownFiles();
1155
1156 $this->backend = $this->multiBackend;
1157 $this->tearDownFiles();
1158 $this->doTestDoOperations2();
1159 $this->tearDownFiles();
1160
1161 $this->backend = $this->singleBackend;
1162 $this->tearDownFiles();
1163 $this->doTestDoOperationsFailing();
1164 $this->tearDownFiles();
1165
1166 $this->backend = $this->multiBackend;
1167 $this->tearDownFiles();
1168 $this->doTestDoOperationsFailing();
1169 $this->tearDownFiles();
1170 }
1171
1172 private function doTestDoOperations() {
1173 $base = $this->baseStorePath();
1174
1175 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1176 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1177 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1178 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1179 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1180 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1181 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1182
1183 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1184 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1185 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1186 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1187 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1188 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1189 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1190
1191 $status = $this->backend->doOperations( array(
1192 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1193 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1194 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1195 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1196 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1197 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1198 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1199 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1200 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1201 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1202 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1203 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1204 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1205 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1206 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1207 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1208 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1209 // Does nothing
1210 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1211 // Does nothing
1212 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1213 // Does nothing
1214 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1215 // Does nothing
1216 array( 'op' => 'null' ),
1217 // Does nothing
1218 ) );
1219
1220 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1221 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1222 $this->assertEquals( 13, count( $status->success ),
1223 "Operation batch has correct success array" );
1224
1225 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
1226 "File does not exist at $fileA" );
1227 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1228 "File does not exist at $fileB" );
1229 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1230 "File does not exist at $fileD" );
1231
1232 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1233 "File exists at $fileC" );
1234 $this->assertEquals( $fileBContents,
1235 $this->backend->getFileContents( array( 'src' => $fileC ) ),
1236 "Correct file contents of $fileC" );
1237 $this->assertEquals( strlen( $fileBContents ),
1238 $this->backend->getFileSize( array( 'src' => $fileC ) ),
1239 "Correct file size of $fileC" );
1240 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1241 $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
1242 "Correct file SHA-1 of $fileC" );
1243 }
1244
1245 // concurrency orientated
1246 private function doTestDoOperations2() {
1247 $base = $this->baseStorePath();
1248
1249 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1250 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1251 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1252
1253 $tmpNameA = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1254 file_put_contents( $tmpNameA, $fileAContents );
1255 $tmpNameB = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1256 file_put_contents( $tmpNameB, $fileBContents );
1257 $tmpNameC = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1258 file_put_contents( $tmpNameC, $fileCContents );
1259
1260 $this->filesToPrune[] = $tmpNameA; # avoid file leaking
1261 $this->filesToPrune[] = $tmpNameB; # avoid file leaking
1262 $this->filesToPrune[] = $tmpNameC; # avoid file leaking
1263
1264 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1265 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1266 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1267 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1268
1269 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1270 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1271 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1272 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1273 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1274
1275 $status = $this->backend->doOperations( array(
1276 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1277 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1278 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1279 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1280 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1281 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1282 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1283 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1284 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1285 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1286 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1287 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1288 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1289 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1290 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1291 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1292 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1293 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1294 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1295 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1296 // Does nothing
1297 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1298 // Does nothing
1299 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1300 // Does nothing
1301 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1302 // Does nothing
1303 array( 'op' => 'null' ),
1304 // Does nothing
1305 ) );
1306
1307 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1308 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1309 $this->assertEquals( 16, count( $status->success ),
1310 "Operation batch has correct success array" );
1311
1312 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
1313 "File does not exist at $fileA" );
1314 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1315 "File does not exist at $fileB" );
1316 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1317 "File does not exist at $fileD" );
1318
1319 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1320 "File exists at $fileC" );
1321 $this->assertEquals( $fileBContents,
1322 $this->backend->getFileContents( array( 'src' => $fileC ) ),
1323 "Correct file contents of $fileC" );
1324 $this->assertEquals( strlen( $fileBContents ),
1325 $this->backend->getFileSize( array( 'src' => $fileC ) ),
1326 "Correct file size of $fileC" );
1327 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1328 $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
1329 "Correct file SHA-1 of $fileC" );
1330 }
1331
1332 private function doTestDoOperationsFailing() {
1333 $base = $this->baseStorePath();
1334
1335 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1336 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1337 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1338 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1339 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1340 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1341 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1342
1343 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1344 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1345 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1346 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1347 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1348 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1349
1350 $status = $this->backend->doOperations( array(
1351 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1352 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1353 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1354 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1355 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1356 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1357 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1358 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1359 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1360 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1361 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1362 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1363 array( 'op' => 'delete', 'src' => $fileD ),
1364 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1365 array( 'op' => 'null' ),
1366 // Does nothing
1367 ), array( 'force' => 1 ) );
1368
1369 $this->assertNotEquals( array(), $status->errors, "Operation had warnings" );
1370 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1371 $this->assertEquals( 8, count( $status->success ),
1372 "Operation batch has correct success array" );
1373
1374 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1375 "File does not exist at $fileB" );
1376 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1377 "File does not exist at $fileD" );
1378
1379 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileA ) ),
1380 "File does not exist at $fileA" );
1381 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1382 "File exists at $fileC" );
1383 $this->assertEquals( $fileBContents,
1384 $this->backend->getFileContents( array( 'src' => $fileA ) ),
1385 "Correct file contents of $fileA" );
1386 $this->assertEquals( strlen( $fileBContents ),
1387 $this->backend->getFileSize( array( 'src' => $fileA ) ),
1388 "Correct file size of $fileA" );
1389 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1390 $this->backend->getFileSha1Base36( array( 'src' => $fileA ) ),
1391 "Correct file SHA-1 of $fileA" );
1392 }
1393
1394 public function testGetFileList() {
1395 $this->backend = $this->singleBackend;
1396 $this->tearDownFiles();
1397 $this->doTestGetFileList();
1398 $this->tearDownFiles();
1399
1400 $this->backend = $this->multiBackend;
1401 $this->tearDownFiles();
1402 $this->doTestGetFileList();
1403 $this->tearDownFiles();
1404 }
1405
1406 private function doTestGetFileList() {
1407 $backendName = $this->backendClass();
1408 $base = $this->baseStorePath();
1409
1410 // Should have no errors
1411 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1412
1413 $files = array(
1414 "$base/unittest-cont1/e/test1.txt",
1415 "$base/unittest-cont1/e/test2.txt",
1416 "$base/unittest-cont1/e/test3.txt",
1417 "$base/unittest-cont1/e/subdir1/test1.txt",
1418 "$base/unittest-cont1/e/subdir1/test2.txt",
1419 "$base/unittest-cont1/e/subdir2/test3.txt",
1420 "$base/unittest-cont1/e/subdir2/test4.txt",
1421 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1422 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1423 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1424 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1425 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1426 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1427 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1428 );
1429
1430 // Add the files
1431 $ops = array();
1432 foreach ( $files as $file ) {
1433 $this->prepare( array( 'dir' => dirname( $file ) ) );
1434 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1435 }
1436 $status = $this->backend->doQuickOperations( $ops );
1437 $this->assertGoodStatus( $status,
1438 "Creation of files succeeded ($backendName)." );
1439 $this->assertEquals( true, $status->isOK(),
1440 "Creation of files succeeded with OK status ($backendName)." );
1441
1442 // Expected listing
1443 $expected = array(
1444 "e/test1.txt",
1445 "e/test2.txt",
1446 "e/test3.txt",
1447 "e/subdir1/test1.txt",
1448 "e/subdir1/test2.txt",
1449 "e/subdir2/test3.txt",
1450 "e/subdir2/test4.txt",
1451 "e/subdir2/subdir/test1.txt",
1452 "e/subdir2/subdir/test2.txt",
1453 "e/subdir2/subdir/test3.txt",
1454 "e/subdir2/subdir/test4.txt",
1455 "e/subdir2/subdir/test5.txt",
1456 "e/subdir2/subdir/sub/test0.txt",
1457 "e/subdir2/subdir/sub/120-px-file.txt",
1458 );
1459 sort( $expected );
1460
1461 // Actual listing (no trailing slash)
1462 $list = array();
1463 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1464 foreach ( $iter as $file ) {
1465 $list[] = $file;
1466 }
1467 sort( $list );
1468
1469 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1470
1471 // Actual listing (with trailing slash)
1472 $list = array();
1473 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1474 foreach ( $iter as $file ) {
1475 $list[] = $file;
1476 }
1477 sort( $list );
1478
1479 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1480
1481 // Expected listing
1482 $expected = array(
1483 "test1.txt",
1484 "test2.txt",
1485 "test3.txt",
1486 "test4.txt",
1487 "test5.txt",
1488 "sub/test0.txt",
1489 "sub/120-px-file.txt",
1490 );
1491 sort( $expected );
1492
1493 // Actual listing (no trailing slash)
1494 $list = array();
1495 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1496 foreach ( $iter as $file ) {
1497 $list[] = $file;
1498 }
1499 sort( $list );
1500
1501 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1502
1503 // Actual listing (with trailing slash)
1504 $list = array();
1505 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1506 foreach ( $iter as $file ) {
1507 $list[] = $file;
1508 }
1509 sort( $list );
1510
1511 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1512
1513 // Actual listing (using iterator second time)
1514 $list = array();
1515 foreach ( $iter as $file ) {
1516 $list[] = $file;
1517 }
1518 sort( $list );
1519
1520 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1521
1522 // Expected listing (top files only)
1523 $expected = array(
1524 "test1.txt",
1525 "test2.txt",
1526 "test3.txt",
1527 "test4.txt",
1528 "test5.txt"
1529 );
1530 sort( $expected );
1531
1532 // Actual listing (top files only)
1533 $list = array();
1534 $iter = $this->backend->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1535 foreach ( $iter as $file ) {
1536 $list[] = $file;
1537 }
1538 sort( $list );
1539
1540 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1541
1542 foreach ( $files as $file ) { // clean up
1543 $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1544 }
1545
1546 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1547 foreach ( $iter as $iter ) {} // no errors
1548 }
1549
1550 public function testGetDirectoryList() {
1551 $this->backend = $this->singleBackend;
1552 $this->tearDownFiles();
1553 $this->doTestGetDirectoryList();
1554 $this->tearDownFiles();
1555
1556 $this->backend = $this->multiBackend;
1557 $this->tearDownFiles();
1558 $this->doTestGetDirectoryList();
1559 $this->tearDownFiles();
1560 }
1561
1562 private function doTestGetDirectoryList() {
1563 $backendName = $this->backendClass();
1564
1565 $base = $this->baseStorePath();
1566 $files = array(
1567 "$base/unittest-cont1/e/test1.txt",
1568 "$base/unittest-cont1/e/test2.txt",
1569 "$base/unittest-cont1/e/test3.txt",
1570 "$base/unittest-cont1/e/subdir1/test1.txt",
1571 "$base/unittest-cont1/e/subdir1/test2.txt",
1572 "$base/unittest-cont1/e/subdir2/test3.txt",
1573 "$base/unittest-cont1/e/subdir2/test4.txt",
1574 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1575 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1576 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1577 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1578 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1579 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1580 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1581 );
1582
1583 // Add the files
1584 $ops = array();
1585 foreach ( $files as $file ) {
1586 $this->prepare( array( 'dir' => dirname( $file ) ) );
1587 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1588 }
1589 $status = $this->backend->doQuickOperations( $ops );
1590 $this->assertGoodStatus( $status,
1591 "Creation of files succeeded ($backendName)." );
1592 $this->assertEquals( true, $status->isOK(),
1593 "Creation of files succeeded with OK status ($backendName)." );
1594
1595 $this->assertEquals( true,
1596 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1597 "Directory exists in ($backendName)." );
1598 $this->assertEquals( true,
1599 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1600 "Directory exists in ($backendName)." );
1601 $this->assertEquals( false,
1602 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1603 "Directory does not exists in ($backendName)." );
1604
1605 // Expected listing
1606 $expected = array(
1607 "e",
1608 );
1609 sort( $expected );
1610
1611 // Actual listing (no trailing slash)
1612 $list = array();
1613 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1614 foreach ( $iter as $file ) {
1615 $list[] = $file;
1616 }
1617 sort( $list );
1618
1619 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1620
1621 // Expected listing
1622 $expected = array(
1623 "subdir1",
1624 "subdir2",
1625 "subdir3",
1626 "subdir4",
1627 );
1628 sort( $expected );
1629
1630 // Actual listing (no trailing slash)
1631 $list = array();
1632 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
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 // Actual listing (with trailing slash)
1641 $list = array();
1642 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1643 foreach ( $iter as $file ) {
1644 $list[] = $file;
1645 }
1646 sort( $list );
1647
1648 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1649
1650 // Expected listing
1651 $expected = array(
1652 "subdir",
1653 );
1654 sort( $expected );
1655
1656 // Actual listing (no trailing slash)
1657 $list = array();
1658 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1659 foreach ( $iter as $file ) {
1660 $list[] = $file;
1661 }
1662 sort( $list );
1663
1664 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1665
1666 // Actual listing (with trailing slash)
1667 $list = array();
1668 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1669 foreach ( $iter as $file ) {
1670 $list[] = $file;
1671 }
1672 sort( $list );
1673
1674 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1675
1676 // Actual listing (using iterator second time)
1677 $list = array();
1678 foreach ( $iter as $file ) {
1679 $list[] = $file;
1680 }
1681 sort( $list );
1682
1683 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1684
1685 // Expected listing (recursive)
1686 $expected = array(
1687 "e",
1688 "e/subdir1",
1689 "e/subdir2",
1690 "e/subdir3",
1691 "e/subdir4",
1692 "e/subdir2/subdir",
1693 "e/subdir3/subdir",
1694 "e/subdir4/subdir",
1695 "e/subdir4/subdir/sub",
1696 );
1697 sort( $expected );
1698
1699 // Actual listing (recursive)
1700 $list = array();
1701 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1702 foreach ( $iter as $file ) {
1703 $list[] = $file;
1704 }
1705 sort( $list );
1706
1707 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1708
1709 // Expected listing (recursive)
1710 $expected = array(
1711 "subdir",
1712 "subdir/sub",
1713 );
1714 sort( $expected );
1715
1716 // Actual listing (recursive)
1717 $list = array();
1718 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
1719 foreach ( $iter as $file ) {
1720 $list[] = $file;
1721 }
1722 sort( $list );
1723
1724 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1725
1726 // Actual listing (recursive, second time)
1727 $list = array();
1728 foreach ( $iter as $file ) {
1729 $list[] = $file;
1730 }
1731 sort( $list );
1732
1733 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1734
1735 foreach ( $files as $file ) { // clean up
1736 $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1737 }
1738
1739 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1740 foreach ( $iter as $iter ) {} // no errors
1741 }
1742
1743 public function testLockCalls() {
1744 $this->backend = $this->singleBackend;
1745 $this->doTestLockCalls();
1746 }
1747
1748 private function doTestLockCalls() {
1749 $backendName = $this->backendClass();
1750
1751 for ( $i=0; $i<50; $i++ ) {
1752 $paths = array(
1753 "test1.txt",
1754 "test2.txt",
1755 "test3.txt",
1756 "subdir1",
1757 "subdir1", // duplicate
1758 "subdir1/test1.txt",
1759 "subdir1/test2.txt",
1760 "subdir2",
1761 "subdir2", // duplicate
1762 "subdir2/test3.txt",
1763 "subdir2/test4.txt",
1764 "subdir2/subdir",
1765 "subdir2/subdir/test1.txt",
1766 "subdir2/subdir/test2.txt",
1767 "subdir2/subdir/test3.txt",
1768 "subdir2/subdir/test4.txt",
1769 "subdir2/subdir/test5.txt",
1770 "subdir2/subdir/sub",
1771 "subdir2/subdir/sub/test0.txt",
1772 "subdir2/subdir/sub/120-px-file.txt",
1773 );
1774
1775 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
1776 $this->assertEquals( array(), $status->errors,
1777 "Locking of files succeeded ($backendName)." );
1778 $this->assertEquals( true, $status->isOK(),
1779 "Locking of files succeeded with OK status ($backendName)." );
1780
1781 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
1782 $this->assertEquals( array(), $status->errors,
1783 "Locking of files succeeded ($backendName)." );
1784 $this->assertEquals( true, $status->isOK(),
1785 "Locking of files succeeded with OK status ($backendName)." );
1786
1787 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
1788 $this->assertEquals( array(), $status->errors,
1789 "Locking of files succeeded ($backendName)." );
1790 $this->assertEquals( true, $status->isOK(),
1791 "Locking of files succeeded with OK status ($backendName)." );
1792
1793 $status = $this->backend->unlockFiles( $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 }
1800
1801 // test helper wrapper for backend prepare() function
1802 private function prepare( array $params ) {
1803 return $this->backend->prepare( $params );
1804 }
1805
1806 // test helper wrapper for backend prepare() function
1807 private function create( array $params ) {
1808 $params['op'] = 'create';
1809 return $this->backend->doQuickOperations( array( $params ) );
1810 }
1811
1812 function tearDownFiles() {
1813 foreach ( $this->filesToPrune as $file ) {
1814 @unlink( $file );
1815 }
1816 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1817 foreach ( $containers as $container ) {
1818 $this->deleteFiles( $container );
1819 }
1820 $this->filesToPrune = array();
1821 }
1822
1823 private function deleteFiles( $container ) {
1824 $base = $this->baseStorePath();
1825 $iter = $this->backend->getFileList( array( 'dir' => "$base/$container" ) );
1826 if ( $iter ) {
1827 foreach ( $iter as $file ) {
1828 $this->backend->delete( array( 'src' => "$base/$container/$file" ),
1829 array( 'force' => 1, 'nonLocking' => 1 ) );
1830 }
1831 }
1832 $this->backend->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
1833 }
1834
1835 function assertBackendPathsConsistent( array $paths ) {
1836 if ( $this->backend instanceof FileBackendMultiWrite ) {
1837 $status = $this->backend->consistencyCheck( $paths );
1838 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
1839 }
1840 }
1841
1842 function assertGoodStatus( $status, $msg ) {
1843 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors, 1 ), $msg );
1844 }
1845
1846 function tearDown() {
1847 parent::tearDown();
1848 }
1849 }