da36e90060ba133265c180ace17ba466dd38838a
[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 protected 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' => 'localmultitesting1',
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' => 'localmultitesting2',
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 static 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 public static 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 static function provider_testStore() {
252 $cases = array();
253
254 $tmpName = TempFSFile::factory( "unittests_", 'txt' )->getPath();
255 $toPath = self::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 if ( isset( $op['ignoreMissingSource'] ) ) {
306 $status = $this->backend->doOperation( $op );
307 $this->assertGoodStatus( $status,
308 "Move from $source to $dest succeeded without warnings ($backendName)." );
309 $this->assertEquals( array( 0 => true ), $status->success,
310 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
311 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
312 "Source file $source does not exist ($backendName)." );
313 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $dest ) ),
314 "Destination file $dest does not exist ($backendName)." );
315 return; // done
316 }
317
318 $status = $this->backend->doOperation(
319 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
320 $this->assertGoodStatus( $status,
321 "Creation of file at $source succeeded ($backendName)." );
322
323 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
324 $this->backend->copy( $op );
325 }
326
327 $status = $this->backend->doOperation( $op );
328
329 $this->assertGoodStatus( $status,
330 "Copy from $source to $dest succeeded without warnings ($backendName)." );
331 $this->assertEquals( true, $status->isOK(),
332 "Copy from $source to $dest succeeded ($backendName)." );
333 $this->assertEquals( array( 0 => true ), $status->success,
334 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
335 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $source ) ),
336 "Source file $source still exists ($backendName)." );
337 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
338 "Destination file $dest exists after copy ($backendName)." );
339
340 $this->assertEquals(
341 $this->backend->getFileSize( array( 'src' => $source ) ),
342 $this->backend->getFileSize( array( 'src' => $dest ) ),
343 "Destination file $dest has correct size ($backendName)." );
344
345 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
346 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
347 $this->assertEquals( $props1, $props2,
348 "Source and destination have the same props ($backendName)." );
349
350 $this->assertBackendPathsConsistent( array( $source, $dest ) );
351 }
352
353 public static function provider_testCopy() {
354 $cases = array();
355
356 $source = self::baseStorePath() . '/unittest-cont1/e/file.txt';
357 $dest = self::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
358
359 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
360 $cases[] = array(
361 $op, // operation
362 $source, // source
363 $dest, // dest
364 );
365
366 $op2 = $op;
367 $op2['overwrite'] = true;
368 $cases[] = array(
369 $op2, // operation
370 $source, // source
371 $dest, // dest
372 );
373
374 $op2 = $op;
375 $op2['overwriteSame'] = true;
376 $cases[] = array(
377 $op2, // operation
378 $source, // source
379 $dest, // dest
380 );
381
382 $op2 = $op;
383 $op2['ignoreMissingSource'] = true;
384 $cases[] = array(
385 $op2, // operation
386 $source, // source
387 $dest, // dest
388 );
389
390 return $cases;
391 }
392
393 /**
394 * @dataProvider provider_testMove
395 */
396 public function testMove( $op ) {
397 $this->backend = $this->singleBackend;
398 $this->tearDownFiles();
399 $this->doTestMove( $op );
400 $this->tearDownFiles();
401
402 $this->backend = $this->multiBackend;
403 $this->tearDownFiles();
404 $this->doTestMove( $op );
405 $this->tearDownFiles();
406 }
407
408 private function doTestMove( $op ) {
409 $backendName = $this->backendClass();
410
411 $source = $op['src'];
412 $dest = $op['dst'];
413 $this->prepare( array( 'dir' => dirname( $source ) ) );
414 $this->prepare( array( 'dir' => dirname( $dest ) ) );
415
416 if ( isset( $op['ignoreMissingSource'] ) ) {
417 $status = $this->backend->doOperation( $op );
418 $this->assertGoodStatus( $status,
419 "Move from $source to $dest succeeded without warnings ($backendName)." );
420 $this->assertEquals( array( 0 => true ), $status->success,
421 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
422 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
423 "Source file $source does not exist ($backendName)." );
424 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $dest ) ),
425 "Destination file $dest does not exist ($backendName)." );
426 return; // done
427 }
428
429 $status = $this->backend->doOperation(
430 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
431 $this->assertGoodStatus( $status,
432 "Creation of file at $source succeeded ($backendName)." );
433
434 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
435 $this->backend->copy( $op );
436 }
437
438 $status = $this->backend->doOperation( $op );
439 $this->assertGoodStatus( $status,
440 "Move from $source to $dest succeeded without warnings ($backendName)." );
441 $this->assertEquals( true, $status->isOK(),
442 "Move from $source to $dest succeeded ($backendName)." );
443 $this->assertEquals( array( 0 => true ), $status->success,
444 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
445 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
446 "Source file $source does not still exists ($backendName)." );
447 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
448 "Destination file $dest exists after move ($backendName)." );
449
450 $this->assertNotEquals(
451 $this->backend->getFileSize( array( 'src' => $source ) ),
452 $this->backend->getFileSize( array( 'src' => $dest ) ),
453 "Destination file $dest has correct size ($backendName)." );
454
455 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
456 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
457 $this->assertEquals( false, $props1['fileExists'],
458 "Source file does not exist accourding to props ($backendName)." );
459 $this->assertEquals( true, $props2['fileExists'],
460 "Destination file exists accourding to props ($backendName)." );
461
462 $this->assertBackendPathsConsistent( array( $source, $dest ) );
463 }
464
465 public static function provider_testMove() {
466 $cases = array();
467
468 $source = self::baseStorePath() . '/unittest-cont1/e/file.txt';
469 $dest = self::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
470
471 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
472 $cases[] = array(
473 $op, // operation
474 $source, // source
475 $dest, // dest
476 );
477
478 $op2 = $op;
479 $op2['overwrite'] = true;
480 $cases[] = array(
481 $op2, // operation
482 $source, // source
483 $dest, // dest
484 );
485
486 $op2 = $op;
487 $op2['overwriteSame'] = true;
488 $cases[] = array(
489 $op2, // operation
490 $source, // source
491 $dest, // dest
492 );
493
494 $op2 = $op;
495 $op2['ignoreMissingSource'] = true;
496 $cases[] = array(
497 $op2, // operation
498 $source, // source
499 $dest, // dest
500 );
501
502 return $cases;
503 }
504
505 /**
506 * @dataProvider provider_testDelete
507 */
508 public function testDelete( $op, $withSource, $okStatus ) {
509 $this->backend = $this->singleBackend;
510 $this->tearDownFiles();
511 $this->doTestDelete( $op, $withSource, $okStatus );
512 $this->tearDownFiles();
513
514 $this->backend = $this->multiBackend;
515 $this->tearDownFiles();
516 $this->doTestDelete( $op, $withSource, $okStatus );
517 $this->tearDownFiles();
518 }
519
520 private function doTestDelete( $op, $withSource, $okStatus ) {
521 $backendName = $this->backendClass();
522
523 $source = $op['src'];
524 $this->prepare( array( 'dir' => dirname( $source ) ) );
525
526 if ( $withSource ) {
527 $status = $this->backend->doOperation(
528 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
529 $this->assertGoodStatus( $status,
530 "Creation of file at $source succeeded ($backendName)." );
531 }
532
533 $status = $this->backend->doOperation( $op );
534 if ( $okStatus ) {
535 $this->assertGoodStatus( $status,
536 "Deletion of file at $source succeeded without warnings ($backendName)." );
537 $this->assertEquals( true, $status->isOK(),
538 "Deletion of file at $source succeeded ($backendName)." );
539 $this->assertEquals( array( 0 => true ), $status->success,
540 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
541 } else {
542 $this->assertEquals( false, $status->isOK(),
543 "Deletion of file at $source failed ($backendName)." );
544 }
545
546 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
547 "Source file $source does not exist after move ($backendName)." );
548
549 $this->assertFalse(
550 $this->backend->getFileSize( array( 'src' => $source ) ),
551 "Source file $source has correct size (false) ($backendName)." );
552
553 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
554 $this->assertFalse( $props1['fileExists'],
555 "Source file $source does not exist according to props ($backendName)." );
556
557 $this->assertBackendPathsConsistent( array( $source ) );
558 }
559
560 public static function provider_testDelete() {
561 $cases = array();
562
563 $source = self::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
564
565 $op = array( 'op' => 'delete', 'src' => $source );
566 $cases[] = array(
567 $op, // operation
568 true, // with source
569 true // succeeds
570 );
571
572 $cases[] = array(
573 $op, // operation
574 false, // without source
575 false // fails
576 );
577
578 $op['ignoreMissingSource'] = true;
579 $cases[] = array(
580 $op, // operation
581 false, // without source
582 true // succeeds
583 );
584
585 return $cases;
586 }
587
588 /**
589 * @dataProvider provider_testCreate
590 */
591 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
592 $this->backend = $this->singleBackend;
593 $this->tearDownFiles();
594 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
595 $this->tearDownFiles();
596
597 $this->backend = $this->multiBackend;
598 $this->tearDownFiles();
599 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
600 $this->tearDownFiles();
601 }
602
603 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
604 $backendName = $this->backendClass();
605
606 $dest = $op['dst'];
607 $this->prepare( array( 'dir' => dirname( $dest ) ) );
608
609 $oldText = 'blah...blah...waahwaah';
610 if ( $alreadyExists ) {
611 $status = $this->backend->doOperation(
612 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
613 $this->assertGoodStatus( $status,
614 "Creation of file at $dest succeeded ($backendName)." );
615 }
616
617 $status = $this->backend->doOperation( $op );
618 if ( $okStatus ) {
619 $this->assertGoodStatus( $status,
620 "Creation of file at $dest succeeded without warnings ($backendName)." );
621 $this->assertEquals( true, $status->isOK(),
622 "Creation of file at $dest succeeded ($backendName)." );
623 $this->assertEquals( array( 0 => true ), $status->success,
624 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
625 } else {
626 $this->assertEquals( false, $status->isOK(),
627 "Creation of file at $dest failed ($backendName)." );
628 }
629
630 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
631 "Destination file $dest exists after creation ($backendName)." );
632
633 $props1 = $this->backend->getFileProps( array( 'src' => $dest ) );
634 $this->assertEquals( true, $props1['fileExists'],
635 "Destination file $dest exists according to props ($backendName)." );
636 if ( $okStatus ) { // file content is what we saved
637 $this->assertEquals( $newSize, $props1['size'],
638 "Destination file $dest has expected size according to props ($backendName)." );
639 $this->assertEquals( $newSize,
640 $this->backend->getFileSize( array( 'src' => $dest ) ),
641 "Destination file $dest has correct size ($backendName)." );
642 } else { // file content is some other previous text
643 $this->assertEquals( strlen( $oldText ), $props1['size'],
644 "Destination file $dest has original size according to props ($backendName)." );
645 $this->assertEquals( strlen( $oldText ),
646 $this->backend->getFileSize( array( 'src' => $dest ) ),
647 "Destination file $dest has original size according to props ($backendName)." );
648 }
649
650 $this->assertBackendPathsConsistent( array( $dest ) );
651 }
652
653 /**
654 * @dataProvider provider_testCreate
655 */
656 public static function provider_testCreate() {
657 $cases = array();
658
659 $dest = self::baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
660
661 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
662 $cases[] = array(
663 $op, // operation
664 false, // no dest already exists
665 true, // succeeds
666 strlen( $op['content'] )
667 );
668
669 $op2 = $op;
670 $op2['content'] = "\n";
671 $cases[] = array(
672 $op2, // operation
673 false, // no dest already exists
674 true, // succeeds
675 strlen( $op2['content'] )
676 );
677
678 $op2 = $op;
679 $op2['content'] = "fsf\n waf 3kt";
680 $cases[] = array(
681 $op2, // operation
682 true, // dest already exists
683 false, // fails
684 strlen( $op2['content'] )
685 );
686
687 $op2 = $op;
688 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
689 $op2['overwrite'] = true;
690 $cases[] = array(
691 $op2, // operation
692 true, // dest already exists
693 true, // succeeds
694 strlen( $op2['content'] )
695 );
696
697 $op2 = $op;
698 $op2['content'] = "39qjmg3-qg";
699 $op2['overwriteSame'] = true;
700 $cases[] = array(
701 $op2, // operation
702 true, // dest already exists
703 false, // succeeds
704 strlen( $op2['content'] )
705 );
706
707 return $cases;
708 }
709
710 public function testDoQuickOperations() {
711 $this->backend = $this->singleBackend;
712 $this->doTestDoQuickOperations();
713 $this->tearDownFiles();
714
715 $this->backend = $this->multiBackend;
716 $this->doTestDoQuickOperations();
717 $this->tearDownFiles();
718 }
719
720 private function doTestDoQuickOperations() {
721 $backendName = $this->backendClass();
722
723 $base = self::baseStorePath();
724 $files = array(
725 "$base/unittest-cont1/e/fileA.a",
726 "$base/unittest-cont1/e/fileB.a",
727 "$base/unittest-cont1/e/fileC.a"
728 );
729 $ops = array();
730 $purgeOps = array();
731 foreach ( $files as $path ) {
732 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
733 $this->assertGoodStatus( $status,
734 "Preparing $path succeeded without warnings ($backendName)." );
735 $ops[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand(0,50000) );
736 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
737 }
738 $purgeOps[] = array( 'op' => 'null' );
739 $status = $this->backend->doQuickOperations( $ops );
740 $this->assertGoodStatus( $status,
741 "Creation of source files succeeded ($backendName)." );
742
743 foreach ( $files as $file ) {
744 $this->assertTrue( $this->backend->fileExists( array( 'src' => $file ) ),
745 "File $file exists." );
746 }
747
748 $status = $this->backend->doQuickOperations( $purgeOps );
749 $this->assertGoodStatus( $status,
750 "Quick deletion of source files succeeded ($backendName)." );
751
752 foreach ( $files as $file ) {
753 $this->assertFalse( $this->backend->fileExists( array( 'src' => $file ) ),
754 "File $file purged." );
755 }
756 }
757
758 /**
759 * @dataProvider provider_testConcatenate
760 */
761 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
762 $this->filesToPrune[] = $op['dst'];
763
764 $this->backend = $this->singleBackend;
765 $this->tearDownFiles();
766 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
767 $this->tearDownFiles();
768
769 $this->backend = $this->multiBackend;
770 $this->tearDownFiles();
771 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
772 $this->filesToPrune[] = $op['dst']; # avoid file leaking
773 $this->tearDownFiles();
774 }
775
776 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
777 $backendName = $this->backendClass();
778
779 $expContent = '';
780 // Create sources
781 $ops = array();
782 foreach ( $srcs as $i => $source ) {
783 $this->prepare( array( 'dir' => dirname( $source ) ) );
784 $ops[] = array(
785 'op' => 'create', // operation
786 'dst' => $source, // source
787 'content' => $srcsContent[$i]
788 );
789 $expContent .= $srcsContent[$i];
790 }
791 $status = $this->backend->doOperations( $ops );
792
793 $this->assertGoodStatus( $status,
794 "Creation of source files succeeded ($backendName)." );
795
796 $dest = $params['dst'];
797 if ( $alreadyExists ) {
798 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
799 $this->assertEquals( true, $ok,
800 "Creation of file at $dest succeeded ($backendName)." );
801 } else {
802 $ok = file_put_contents( $dest, '' ) !== false;
803 $this->assertEquals( true, $ok,
804 "Creation of 0-byte file at $dest succeeded ($backendName)." );
805 }
806
807 // Combine the files into one
808 $status = $this->backend->concatenate( $params );
809 if ( $okStatus ) {
810 $this->assertGoodStatus( $status,
811 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
812 $this->assertEquals( true, $status->isOK(),
813 "Creation of concat file at $dest succeeded ($backendName)." );
814 } else {
815 $this->assertEquals( false, $status->isOK(),
816 "Creation of concat file at $dest failed ($backendName)." );
817 }
818
819 if ( $okStatus ) {
820 $this->assertEquals( true, is_file( $dest ),
821 "Dest concat file $dest exists after creation ($backendName)." );
822 } else {
823 $this->assertEquals( true, is_file( $dest ),
824 "Dest concat file $dest exists after failed creation ($backendName)." );
825 }
826
827 $contents = file_get_contents( $dest );
828 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
829
830 if ( $okStatus ) {
831 $this->assertEquals( $expContent, $contents,
832 "Concat file at $dest has correct contents ($backendName)." );
833 } else {
834 $this->assertNotEquals( $expContent, $contents,
835 "Concat file at $dest has correct contents ($backendName)." );
836 }
837 }
838
839 function provider_testConcatenate() {
840 $cases = array();
841
842 $rand = mt_rand( 0, 2000000000 ) . time();
843 $dest = wfTempDir() . "/randomfile!$rand.txt";
844 $srcs = array(
845 self::baseStorePath() . '/unittest-cont1/e/file1.txt',
846 self::baseStorePath() . '/unittest-cont1/e/file2.txt',
847 self::baseStorePath() . '/unittest-cont1/e/file3.txt',
848 self::baseStorePath() . '/unittest-cont1/e/file4.txt',
849 self::baseStorePath() . '/unittest-cont1/e/file5.txt',
850 self::baseStorePath() . '/unittest-cont1/e/file6.txt',
851 self::baseStorePath() . '/unittest-cont1/e/file7.txt',
852 self::baseStorePath() . '/unittest-cont1/e/file8.txt',
853 self::baseStorePath() . '/unittest-cont1/e/file9.txt',
854 self::baseStorePath() . '/unittest-cont1/e/file10.txt'
855 );
856 $content = array(
857 'egfage',
858 'ageageag',
859 'rhokohlr',
860 'shgmslkg',
861 'kenga',
862 'owagmal',
863 'kgmae',
864 'g eak;g',
865 'lkaem;a',
866 'legma'
867 );
868 $params = array( 'srcs' => $srcs, 'dst' => $dest );
869
870 $cases[] = array(
871 $params, // operation
872 $srcs, // sources
873 $content, // content for each source
874 false, // no dest already exists
875 true, // succeeds
876 );
877
878 $cases[] = array(
879 $params, // operation
880 $srcs, // sources
881 $content, // content for each source
882 true, // dest already exists
883 false, // succeeds
884 );
885
886 return $cases;
887 }
888
889 /**
890 * @dataProvider provider_testGetFileStat
891 */
892 public function testGetFileStat( $path, $content, $alreadyExists ) {
893 $this->backend = $this->singleBackend;
894 $this->tearDownFiles();
895 $this->doTestGetFileStat( $path, $content, $alreadyExists );
896 $this->tearDownFiles();
897
898 $this->backend = $this->multiBackend;
899 $this->tearDownFiles();
900 $this->doTestGetFileStat( $path, $content, $alreadyExists );
901 $this->tearDownFiles();
902 }
903
904 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
905 $backendName = $this->backendClass();
906
907 if ( $alreadyExists ) {
908 $this->prepare( array( 'dir' => dirname( $path ) ) );
909 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
910 $this->assertGoodStatus( $status,
911 "Creation of file at $path succeeded ($backendName)." );
912
913 $size = $this->backend->getFileSize( array( 'src' => $path ) );
914 $time = $this->backend->getFileTimestamp( array( 'src' => $path ) );
915 $stat = $this->backend->getFileStat( array( 'src' => $path ) );
916
917 $this->assertEquals( strlen( $content ), $size,
918 "Correct file size of '$path'" );
919 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
920 "Correct file timestamp of '$path'" );
921
922 $size = $stat['size'];
923 $time = $stat['mtime'];
924 $this->assertEquals( strlen( $content ), $size,
925 "Correct file size of '$path'" );
926 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
927 "Correct file timestamp of '$path'" );
928
929 $this->backend->clearCache( array( $path ) );
930
931 $size = $this->backend->getFileSize( array( 'src' => $path ) );
932
933 $this->assertEquals( strlen( $content ), $size,
934 "Correct file size of '$path'" );
935
936 $this->backend->preloadCache( array( $path ) );
937
938 $size = $this->backend->getFileSize( array( 'src' => $path ) );
939
940 $this->assertEquals( strlen( $content ), $size,
941 "Correct file size of '$path'" );
942 } else {
943 $size = $this->backend->getFileSize( array( 'src' => $path ) );
944 $time = $this->backend->getFileTimestamp( array( 'src' => $path ) );
945 $stat = $this->backend->getFileStat( array( 'src' => $path ) );
946
947 $this->assertFalse( $size, "Correct file size of '$path'" );
948 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
949 $this->assertFalse( $stat, "Correct file stat of '$path'" );
950 }
951 }
952
953 function provider_testGetFileStat() {
954 $cases = array();
955
956 $base = self::baseStorePath();
957 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
958 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
959 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
960
961 return $cases;
962 }
963
964 /**
965 * @dataProvider provider_testGetFileContents
966 */
967 public function testGetFileContents( $source, $content ) {
968 $this->backend = $this->singleBackend;
969 $this->tearDownFiles();
970 $this->doTestGetFileContents( $source, $content );
971 $this->tearDownFiles();
972
973 $this->backend = $this->multiBackend;
974 $this->tearDownFiles();
975 $this->doTestGetFileContents( $source, $content );
976 $this->tearDownFiles();
977 }
978
979 private function doTestGetFileContents( $source, $content ) {
980 $backendName = $this->backendClass();
981
982 $srcs = (array)$source;
983 $content = (array)$content;
984 foreach ( $srcs as $i => $src ) {
985 $this->prepare( array( 'dir' => dirname( $src ) ) );
986 $status = $this->backend->doOperation(
987 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
988 $this->assertGoodStatus( $status,
989 "Creation of file at $src succeeded ($backendName)." );
990 }
991
992 if ( is_array( $source ) ) {
993 $contents = $this->backend->getFileContentsMulti( array( 'srcs' => $source ) );
994 foreach ( $contents as $path => $data ) {
995 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
996 $this->assertEquals( current( $content ), $data, "Contents of $path is correct ($backendName)." );
997 next( $content );
998 }
999 $this->assertEquals( $source, array_keys( $contents ), "Contents in right order ($backendName)." );
1000 $this->assertEquals( count( $source ), count( $contents ), "Contents array size correct ($backendName)." );
1001 } else {
1002 $data = $this->backend->getFileContents( array( 'src' => $source ) );
1003 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1004 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1005 }
1006 }
1007
1008 function provider_testGetFileContents() {
1009 $cases = array();
1010
1011 $base = self::baseStorePath();
1012 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
1013 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
1014 $cases[] = array(
1015 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1016 "$base/unittest-cont1/e/a/z.txt" ),
1017 array( "contents xx", "contents xy", "contents xz" )
1018 );
1019
1020 return $cases;
1021 }
1022
1023 /**
1024 * @dataProvider provider_testGetLocalCopy
1025 */
1026 public function testGetLocalCopy( $source, $content ) {
1027 $this->backend = $this->singleBackend;
1028 $this->tearDownFiles();
1029 $this->doTestGetLocalCopy( $source, $content );
1030 $this->tearDownFiles();
1031
1032 $this->backend = $this->multiBackend;
1033 $this->tearDownFiles();
1034 $this->doTestGetLocalCopy( $source, $content );
1035 $this->tearDownFiles();
1036 }
1037
1038 private function doTestGetLocalCopy( $source, $content ) {
1039 $backendName = $this->backendClass();
1040
1041 $srcs = (array)$source;
1042 $content = (array)$content;
1043 foreach ( $srcs as $i => $src ) {
1044 $this->prepare( array( 'dir' => dirname( $src ) ) );
1045 $status = $this->backend->doOperation(
1046 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1047 $this->assertGoodStatus( $status,
1048 "Creation of file at $src succeeded ($backendName)." );
1049 }
1050
1051 if ( is_array( $source ) ) {
1052 $tmpFiles = $this->backend->getLocalCopyMulti( array( 'srcs' => $source ) );
1053 foreach ( $tmpFiles as $path => $tmpFile ) {
1054 $this->assertNotNull( $tmpFile,
1055 "Creation of local copy of $path succeeded ($backendName)." );
1056 $contents = file_get_contents( $tmpFile->getPath() );
1057 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1058 $this->assertEquals( current( $content ), $contents, "Local copy of $path is correct ($backendName)." );
1059 next( $content );
1060 }
1061 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local copies in right order ($backendName)." );
1062 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local copies array size correct ($backendName)." );
1063 } else {
1064 $tmpFile = $this->backend->getLocalCopy( array( 'src' => $source ) );
1065 $this->assertNotNull( $tmpFile,
1066 "Creation of local copy of $source succeeded ($backendName)." );
1067 $contents = file_get_contents( $tmpFile->getPath() );
1068 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1069 $this->assertEquals( $content[0], $contents, "Local copy of $source is correct ($backendName)." );
1070 }
1071
1072 $obj = new stdClass();
1073 $tmpFile->bind( $obj );
1074 }
1075
1076 function provider_testGetLocalCopy() {
1077 $cases = array();
1078
1079 $base = self::baseStorePath();
1080 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1081 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1082 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1083 $cases[] = array(
1084 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1085 "$base/unittest-cont1/e/a/z.txt" ),
1086 array( "contents xx", "contents xy", "contents xz" )
1087 );
1088
1089 return $cases;
1090 }
1091
1092 /**
1093 * @dataProvider provider_testGetLocalReference
1094 */
1095 public function testGetLocalReference( $source, $content ) {
1096 $this->backend = $this->singleBackend;
1097 $this->tearDownFiles();
1098 $this->doTestGetLocalReference( $source, $content );
1099 $this->tearDownFiles();
1100
1101 $this->backend = $this->multiBackend;
1102 $this->tearDownFiles();
1103 $this->doTestGetLocalReference( $source, $content );
1104 $this->tearDownFiles();
1105 }
1106
1107 private function doTestGetLocalReference( $source, $content ) {
1108 $backendName = $this->backendClass();
1109
1110 $srcs = (array)$source;
1111 $content = (array)$content;
1112 foreach ( $srcs as $i => $src ) {
1113 $this->prepare( array( 'dir' => dirname( $src ) ) );
1114 $status = $this->backend->doOperation(
1115 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1116 $this->assertGoodStatus( $status,
1117 "Creation of file at $src succeeded ($backendName)." );
1118 }
1119
1120 if ( is_array( $source ) ) {
1121 $tmpFiles = $this->backend->getLocalReferenceMulti( array( 'srcs' => $source ) );
1122 foreach ( $tmpFiles as $path => $tmpFile ) {
1123 $this->assertNotNull( $tmpFile,
1124 "Creation of local copy of $path succeeded ($backendName)." );
1125 $contents = file_get_contents( $tmpFile->getPath() );
1126 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1127 $this->assertEquals( current( $content ), $contents, "Local ref of $path is correct ($backendName)." );
1128 next( $content );
1129 }
1130 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local refs in right order ($backendName)." );
1131 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local refs array size correct ($backendName)." );
1132 } else {
1133 $tmpFile = $this->backend->getLocalReference( array( 'src' => $source ) );
1134 $this->assertNotNull( $tmpFile,
1135 "Creation of local copy of $source succeeded ($backendName)." );
1136 $contents = file_get_contents( $tmpFile->getPath() );
1137 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1138 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1139 }
1140 }
1141
1142 function provider_testGetLocalReference() {
1143 $cases = array();
1144
1145 $base = self::baseStorePath();
1146 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1147 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1148 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1149 $cases[] = array(
1150 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1151 "$base/unittest-cont1/e/a/z.txt" ),
1152 array( "contents xx", "contents xy", "contents xz" )
1153 );
1154
1155 return $cases;
1156 }
1157
1158 public function testGetLocalCopyAndReference404() {
1159 $this->backend = $this->singleBackend;
1160 $this->tearDownFiles();
1161 $this->doTestGetLocalCopyAndReference404();
1162 $this->tearDownFiles();
1163
1164 $this->backend = $this->multiBackend;
1165 $this->tearDownFiles();
1166 $this->doTestGetLocalCopyAndReference404();
1167 $this->tearDownFiles();
1168 }
1169
1170 public function doTestGetLocalCopyAndReference404() {
1171 $backendName = $this->backendClass();
1172
1173 $base = self::baseStorePath();
1174
1175 $tmpFile = $this->backend->getLocalCopy( array(
1176 'src' => "$base/unittest-cont1/not-there" ) );
1177 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1178
1179 $tmpFile = $this->backend->getLocalReference( array(
1180 'src' => "$base/unittest-cont1/not-there" ) );
1181 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1182 }
1183
1184 /**
1185 * @dataProvider provider_testPrepareAndClean
1186 */
1187 public function testPrepareAndClean( $path, $isOK ) {
1188 $this->backend = $this->singleBackend;
1189 $this->doTestPrepareAndClean( $path, $isOK );
1190 $this->tearDownFiles();
1191
1192 $this->backend = $this->multiBackend;
1193 $this->doTestPrepareAndClean( $path, $isOK );
1194 $this->tearDownFiles();
1195 }
1196
1197 function provider_testPrepareAndClean() {
1198 $base = self::baseStorePath();
1199 return array(
1200 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1201 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1202 # Specific to FS backend with no basePath field set
1203 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1204 );
1205 }
1206
1207 private function doTestPrepareAndClean( $path, $isOK ) {
1208 $backendName = $this->backendClass();
1209
1210 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1211 if ( $isOK ) {
1212 $this->assertGoodStatus( $status,
1213 "Preparing dir $path succeeded without warnings ($backendName)." );
1214 $this->assertEquals( true, $status->isOK(),
1215 "Preparing dir $path succeeded ($backendName)." );
1216 } else {
1217 $this->assertEquals( false, $status->isOK(),
1218 "Preparing dir $path failed ($backendName)." );
1219 }
1220
1221 $status = $this->backend->clean( array( 'dir' => dirname( $path ) ) );
1222 if ( $isOK ) {
1223 $this->assertGoodStatus( $status,
1224 "Cleaning dir $path succeeded without warnings ($backendName)." );
1225 $this->assertEquals( true, $status->isOK(),
1226 "Cleaning dir $path succeeded ($backendName)." );
1227 } else {
1228 $this->assertEquals( false, $status->isOK(),
1229 "Cleaning dir $path failed ($backendName)." );
1230 }
1231 }
1232
1233 public function testRecursiveClean() {
1234 $this->backend = $this->singleBackend;
1235 $this->doTestRecursiveClean();
1236 $this->tearDownFiles();
1237
1238 $this->backend = $this->multiBackend;
1239 $this->doTestRecursiveClean();
1240 $this->tearDownFiles();
1241 }
1242
1243 private function doTestRecursiveClean() {
1244 $backendName = $this->backendClass();
1245
1246 $base = self::baseStorePath();
1247 $dirs = array(
1248 "$base/unittest-cont1/e/a",
1249 "$base/unittest-cont1/e/a/b",
1250 "$base/unittest-cont1/e/a/b/c",
1251 "$base/unittest-cont1/e/a/b/c/d0",
1252 "$base/unittest-cont1/e/a/b/c/d1",
1253 "$base/unittest-cont1/e/a/b/c/d2",
1254 "$base/unittest-cont1/e/a/b/c/d0/1",
1255 "$base/unittest-cont1/e/a/b/c/d0/2",
1256 "$base/unittest-cont1/e/a/b/c/d1/3",
1257 "$base/unittest-cont1/e/a/b/c/d1/4",
1258 "$base/unittest-cont1/e/a/b/c/d2/5",
1259 "$base/unittest-cont1/e/a/b/c/d2/6"
1260 );
1261 foreach ( $dirs as $dir ) {
1262 $status = $this->prepare( array( 'dir' => $dir ) );
1263 $this->assertGoodStatus( $status,
1264 "Preparing dir $dir succeeded without warnings ($backendName)." );
1265 }
1266
1267 if ( $this->backend instanceof FSFileBackend ) {
1268 foreach ( $dirs as $dir ) {
1269 $this->assertEquals( true, $this->backend->directoryExists( array( 'dir' => $dir ) ),
1270 "Dir $dir exists ($backendName)." );
1271 }
1272 }
1273
1274 $status = $this->backend->clean(
1275 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1276 $this->assertGoodStatus( $status,
1277 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1278
1279 foreach ( $dirs as $dir ) {
1280 $this->assertEquals( false, $this->backend->directoryExists( array( 'dir' => $dir ) ),
1281 "Dir $dir no longer exists ($backendName)." );
1282 }
1283 }
1284
1285 // @TODO: testSecure
1286
1287 public function testDoOperations() {
1288 $this->backend = $this->singleBackend;
1289 $this->tearDownFiles();
1290 $this->doTestDoOperations();
1291 $this->tearDownFiles();
1292
1293 $this->backend = $this->multiBackend;
1294 $this->tearDownFiles();
1295 $this->doTestDoOperations();
1296 $this->tearDownFiles();
1297 }
1298
1299 private function doTestDoOperations() {
1300 $base = self::baseStorePath();
1301
1302 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1303 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1304 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1305 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1306 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1307 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1308 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1309
1310 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1311 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1312 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1313 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1314 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1315 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1316 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1317
1318 $status = $this->backend->doOperations( array(
1319 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1320 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1321 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1322 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1323 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1324 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1325 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1326 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1327 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1328 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1329 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1330 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1331 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1332 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1333 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1334 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1335 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1336 // Does nothing
1337 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1338 // Does nothing
1339 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1340 // Does nothing
1341 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1342 // Does nothing
1343 array( 'op' => 'null' ),
1344 // Does nothing
1345 ) );
1346
1347 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1348 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1349 $this->assertEquals( 13, count( $status->success ),
1350 "Operation batch has correct success array" );
1351
1352 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
1353 "File does not exist at $fileA" );
1354 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1355 "File does not exist at $fileB" );
1356 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1357 "File does not exist at $fileD" );
1358
1359 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1360 "File exists at $fileC" );
1361 $this->assertEquals( $fileBContents,
1362 $this->backend->getFileContents( array( 'src' => $fileC ) ),
1363 "Correct file contents of $fileC" );
1364 $this->assertEquals( strlen( $fileBContents ),
1365 $this->backend->getFileSize( array( 'src' => $fileC ) ),
1366 "Correct file size of $fileC" );
1367 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1368 $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
1369 "Correct file SHA-1 of $fileC" );
1370 }
1371
1372 public function testDoOperationsPipeline() {
1373 $this->backend = $this->singleBackend;
1374 $this->tearDownFiles();
1375 $this->doTestDoOperationsPipeline();
1376 $this->tearDownFiles();
1377
1378 $this->backend = $this->multiBackend;
1379 $this->tearDownFiles();
1380 $this->doTestDoOperationsPipeline();
1381 $this->tearDownFiles();
1382 }
1383
1384 // concurrency orientated
1385 private function doTestDoOperationsPipeline() {
1386 $base = self::baseStorePath();
1387
1388 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1389 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1390 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1391
1392 $tmpNameA = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1393 file_put_contents( $tmpNameA, $fileAContents );
1394 $tmpNameB = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1395 file_put_contents( $tmpNameB, $fileBContents );
1396 $tmpNameC = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1397 file_put_contents( $tmpNameC, $fileCContents );
1398
1399 $this->filesToPrune[] = $tmpNameA; # avoid file leaking
1400 $this->filesToPrune[] = $tmpNameB; # avoid file leaking
1401 $this->filesToPrune[] = $tmpNameC; # avoid file leaking
1402
1403 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1404 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1405 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1406 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1407
1408 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1409 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1410 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1411 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1412 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1413
1414 $status = $this->backend->doOperations( array(
1415 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1416 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1417 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1418 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1419 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1420 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1421 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1422 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1423 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1424 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1425 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1426 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1427 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1428 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1429 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1430 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1431 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1432 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1433 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1434 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1435 // Does nothing
1436 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1437 // Does nothing
1438 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1439 // Does nothing
1440 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1441 // Does nothing
1442 array( 'op' => 'null' ),
1443 // Does nothing
1444 ) );
1445
1446 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1447 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1448 $this->assertEquals( 16, count( $status->success ),
1449 "Operation batch has correct success array" );
1450
1451 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
1452 "File does not exist at $fileA" );
1453 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1454 "File does not exist at $fileB" );
1455 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1456 "File does not exist at $fileD" );
1457
1458 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1459 "File exists at $fileC" );
1460 $this->assertEquals( $fileBContents,
1461 $this->backend->getFileContents( array( 'src' => $fileC ) ),
1462 "Correct file contents of $fileC" );
1463 $this->assertEquals( strlen( $fileBContents ),
1464 $this->backend->getFileSize( array( 'src' => $fileC ) ),
1465 "Correct file size of $fileC" );
1466 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1467 $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
1468 "Correct file SHA-1 of $fileC" );
1469 }
1470
1471 public function testDoOperationsFailing() {
1472 $this->backend = $this->singleBackend;
1473 $this->tearDownFiles();
1474 $this->doTestDoOperationsFailing();
1475 $this->tearDownFiles();
1476
1477 $this->backend = $this->multiBackend;
1478 $this->tearDownFiles();
1479 $this->doTestDoOperationsFailing();
1480 $this->tearDownFiles();
1481 }
1482
1483 private function doTestDoOperationsFailing() {
1484 $base = self::baseStorePath();
1485
1486 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1487 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1488 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1489 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1490 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1491 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1492 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1493
1494 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1495 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1496 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1497 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1498 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1499 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1500
1501 $status = $this->backend->doOperations( array(
1502 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1503 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1504 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1505 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1506 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1507 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1508 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1509 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1510 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1511 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1512 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1513 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1514 array( 'op' => 'delete', 'src' => $fileD ),
1515 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1516 array( 'op' => 'null' ),
1517 // Does nothing
1518 ), array( 'force' => 1 ) );
1519
1520 $this->assertNotEquals( array(), $status->errors, "Operation had warnings" );
1521 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1522 $this->assertEquals( 8, count( $status->success ),
1523 "Operation batch has correct success array" );
1524
1525 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1526 "File does not exist at $fileB" );
1527 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1528 "File does not exist at $fileD" );
1529
1530 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileA ) ),
1531 "File does not exist at $fileA" );
1532 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1533 "File exists at $fileC" );
1534 $this->assertEquals( $fileBContents,
1535 $this->backend->getFileContents( array( 'src' => $fileA ) ),
1536 "Correct file contents of $fileA" );
1537 $this->assertEquals( strlen( $fileBContents ),
1538 $this->backend->getFileSize( array( 'src' => $fileA ) ),
1539 "Correct file size of $fileA" );
1540 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1541 $this->backend->getFileSha1Base36( array( 'src' => $fileA ) ),
1542 "Correct file SHA-1 of $fileA" );
1543 }
1544
1545 public function testGetFileList() {
1546 $this->backend = $this->singleBackend;
1547 $this->tearDownFiles();
1548 $this->doTestGetFileList();
1549 $this->tearDownFiles();
1550
1551 $this->backend = $this->multiBackend;
1552 $this->tearDownFiles();
1553 $this->doTestGetFileList();
1554 $this->tearDownFiles();
1555 }
1556
1557 private function doTestGetFileList() {
1558 $backendName = $this->backendClass();
1559 $base = self::baseStorePath();
1560
1561 // Should have no errors
1562 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1563
1564 $files = array(
1565 "$base/unittest-cont1/e/test1.txt",
1566 "$base/unittest-cont1/e/test2.txt",
1567 "$base/unittest-cont1/e/test3.txt",
1568 "$base/unittest-cont1/e/subdir1/test1.txt",
1569 "$base/unittest-cont1/e/subdir1/test2.txt",
1570 "$base/unittest-cont1/e/subdir2/test3.txt",
1571 "$base/unittest-cont1/e/subdir2/test4.txt",
1572 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1573 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1574 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1575 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1576 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1577 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1578 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1579 );
1580
1581 // Add the files
1582 $ops = array();
1583 foreach ( $files as $file ) {
1584 $this->prepare( array( 'dir' => dirname( $file ) ) );
1585 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1586 }
1587 $status = $this->backend->doQuickOperations( $ops );
1588 $this->assertGoodStatus( $status,
1589 "Creation of files succeeded ($backendName)." );
1590 $this->assertEquals( true, $status->isOK(),
1591 "Creation of files succeeded with OK status ($backendName)." );
1592
1593 // Expected listing
1594 $expected = array(
1595 "e/test1.txt",
1596 "e/test2.txt",
1597 "e/test3.txt",
1598 "e/subdir1/test1.txt",
1599 "e/subdir1/test2.txt",
1600 "e/subdir2/test3.txt",
1601 "e/subdir2/test4.txt",
1602 "e/subdir2/subdir/test1.txt",
1603 "e/subdir2/subdir/test2.txt",
1604 "e/subdir2/subdir/test3.txt",
1605 "e/subdir2/subdir/test4.txt",
1606 "e/subdir2/subdir/test5.txt",
1607 "e/subdir2/subdir/sub/test0.txt",
1608 "e/subdir2/subdir/sub/120-px-file.txt",
1609 );
1610 sort( $expected );
1611
1612 // Actual listing (no trailing slash)
1613 $list = array();
1614 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1615 foreach ( $iter as $file ) {
1616 $list[] = $file;
1617 }
1618 sort( $list );
1619
1620 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1621
1622 // Actual listing (with trailing slash)
1623 $list = array();
1624 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1625 foreach ( $iter as $file ) {
1626 $list[] = $file;
1627 }
1628 sort( $list );
1629
1630 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1631
1632 // Expected listing
1633 $expected = array(
1634 "test1.txt",
1635 "test2.txt",
1636 "test3.txt",
1637 "test4.txt",
1638 "test5.txt",
1639 "sub/test0.txt",
1640 "sub/120-px-file.txt",
1641 );
1642 sort( $expected );
1643
1644 // Actual listing (no trailing slash)
1645 $list = array();
1646 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1647 foreach ( $iter as $file ) {
1648 $list[] = $file;
1649 }
1650 sort( $list );
1651
1652 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1653
1654 // Actual listing (with trailing slash)
1655 $list = array();
1656 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1657 foreach ( $iter as $file ) {
1658 $list[] = $file;
1659 }
1660 sort( $list );
1661
1662 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1663
1664 // Actual listing (using iterator second time)
1665 $list = array();
1666 foreach ( $iter as $file ) {
1667 $list[] = $file;
1668 }
1669 sort( $list );
1670
1671 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1672
1673 // Expected listing (top files only)
1674 $expected = array(
1675 "test1.txt",
1676 "test2.txt",
1677 "test3.txt",
1678 "test4.txt",
1679 "test5.txt"
1680 );
1681 sort( $expected );
1682
1683 // Actual listing (top files only)
1684 $list = array();
1685 $iter = $this->backend->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1686 foreach ( $iter as $file ) {
1687 $list[] = $file;
1688 }
1689 sort( $list );
1690
1691 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1692
1693 foreach ( $files as $file ) { // clean up
1694 $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1695 }
1696
1697 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1698 foreach ( $iter as $iter ) {} // no errors
1699 }
1700
1701 public function testGetDirectoryList() {
1702 $this->backend = $this->singleBackend;
1703 $this->tearDownFiles();
1704 $this->doTestGetDirectoryList();
1705 $this->tearDownFiles();
1706
1707 $this->backend = $this->multiBackend;
1708 $this->tearDownFiles();
1709 $this->doTestGetDirectoryList();
1710 $this->tearDownFiles();
1711 }
1712
1713 private function doTestGetDirectoryList() {
1714 $backendName = $this->backendClass();
1715
1716 $base = self::baseStorePath();
1717 $files = array(
1718 "$base/unittest-cont1/e/test1.txt",
1719 "$base/unittest-cont1/e/test2.txt",
1720 "$base/unittest-cont1/e/test3.txt",
1721 "$base/unittest-cont1/e/subdir1/test1.txt",
1722 "$base/unittest-cont1/e/subdir1/test2.txt",
1723 "$base/unittest-cont1/e/subdir2/test3.txt",
1724 "$base/unittest-cont1/e/subdir2/test4.txt",
1725 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1726 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1727 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1728 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1729 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1730 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1731 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1732 );
1733
1734 // Add the files
1735 $ops = array();
1736 foreach ( $files as $file ) {
1737 $this->prepare( array( 'dir' => dirname( $file ) ) );
1738 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1739 }
1740 $status = $this->backend->doQuickOperations( $ops );
1741 $this->assertGoodStatus( $status,
1742 "Creation of files succeeded ($backendName)." );
1743 $this->assertEquals( true, $status->isOK(),
1744 "Creation of files succeeded with OK status ($backendName)." );
1745
1746 $this->assertEquals( true,
1747 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1748 "Directory exists in ($backendName)." );
1749 $this->assertEquals( true,
1750 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1751 "Directory exists in ($backendName)." );
1752 $this->assertEquals( false,
1753 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1754 "Directory does not exists in ($backendName)." );
1755
1756 // Expected listing
1757 $expected = array(
1758 "e",
1759 );
1760 sort( $expected );
1761
1762 // Actual listing (no trailing slash)
1763 $list = array();
1764 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1765 foreach ( $iter as $file ) {
1766 $list[] = $file;
1767 }
1768 sort( $list );
1769
1770 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1771
1772 // Expected listing
1773 $expected = array(
1774 "subdir1",
1775 "subdir2",
1776 "subdir3",
1777 "subdir4",
1778 );
1779 sort( $expected );
1780
1781 // Actual listing (no trailing slash)
1782 $list = array();
1783 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1784 foreach ( $iter as $file ) {
1785 $list[] = $file;
1786 }
1787 sort( $list );
1788
1789 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1790
1791 // Actual listing (with trailing slash)
1792 $list = array();
1793 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1794 foreach ( $iter as $file ) {
1795 $list[] = $file;
1796 }
1797 sort( $list );
1798
1799 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1800
1801 // Expected listing
1802 $expected = array(
1803 "subdir",
1804 );
1805 sort( $expected );
1806
1807 // Actual listing (no trailing slash)
1808 $list = array();
1809 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1810 foreach ( $iter as $file ) {
1811 $list[] = $file;
1812 }
1813 sort( $list );
1814
1815 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1816
1817 // Actual listing (with trailing slash)
1818 $list = array();
1819 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1820 foreach ( $iter as $file ) {
1821 $list[] = $file;
1822 }
1823 sort( $list );
1824
1825 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1826
1827 // Actual listing (using iterator second time)
1828 $list = array();
1829 foreach ( $iter as $file ) {
1830 $list[] = $file;
1831 }
1832 sort( $list );
1833
1834 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1835
1836 // Expected listing (recursive)
1837 $expected = array(
1838 "e",
1839 "e/subdir1",
1840 "e/subdir2",
1841 "e/subdir3",
1842 "e/subdir4",
1843 "e/subdir2/subdir",
1844 "e/subdir3/subdir",
1845 "e/subdir4/subdir",
1846 "e/subdir4/subdir/sub",
1847 );
1848 sort( $expected );
1849
1850 // Actual listing (recursive)
1851 $list = array();
1852 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1853 foreach ( $iter as $file ) {
1854 $list[] = $file;
1855 }
1856 sort( $list );
1857
1858 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1859
1860 // Expected listing (recursive)
1861 $expected = array(
1862 "subdir",
1863 "subdir/sub",
1864 );
1865 sort( $expected );
1866
1867 // Actual listing (recursive)
1868 $list = array();
1869 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
1870 foreach ( $iter as $file ) {
1871 $list[] = $file;
1872 }
1873 sort( $list );
1874
1875 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1876
1877 // Actual listing (recursive, second time)
1878 $list = array();
1879 foreach ( $iter as $file ) {
1880 $list[] = $file;
1881 }
1882 sort( $list );
1883
1884 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1885
1886 foreach ( $files as $file ) { // clean up
1887 $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1888 }
1889
1890 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1891 foreach ( $iter as $iter ) {} // no errors
1892 }
1893
1894 public function testLockCalls() {
1895 $this->backend = $this->singleBackend;
1896 $this->doTestLockCalls();
1897 }
1898
1899 private function doTestLockCalls() {
1900 $backendName = $this->backendClass();
1901
1902 for ( $i=0; $i<50; $i++ ) {
1903 $paths = array(
1904 "test1.txt",
1905 "test2.txt",
1906 "test3.txt",
1907 "subdir1",
1908 "subdir1", // duplicate
1909 "subdir1/test1.txt",
1910 "subdir1/test2.txt",
1911 "subdir2",
1912 "subdir2", // duplicate
1913 "subdir2/test3.txt",
1914 "subdir2/test4.txt",
1915 "subdir2/subdir",
1916 "subdir2/subdir/test1.txt",
1917 "subdir2/subdir/test2.txt",
1918 "subdir2/subdir/test3.txt",
1919 "subdir2/subdir/test4.txt",
1920 "subdir2/subdir/test5.txt",
1921 "subdir2/subdir/sub",
1922 "subdir2/subdir/sub/test0.txt",
1923 "subdir2/subdir/sub/120-px-file.txt",
1924 );
1925
1926 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
1927 $this->assertEquals( array(), $status->errors,
1928 "Locking of files succeeded ($backendName)." );
1929 $this->assertEquals( true, $status->isOK(),
1930 "Locking of files succeeded with OK status ($backendName)." );
1931
1932 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
1933 $this->assertEquals( array(), $status->errors,
1934 "Locking of files succeeded ($backendName)." );
1935 $this->assertEquals( true, $status->isOK(),
1936 "Locking of files succeeded with OK status ($backendName)." );
1937
1938 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
1939 $this->assertEquals( array(), $status->errors,
1940 "Locking of files succeeded ($backendName)." );
1941 $this->assertEquals( true, $status->isOK(),
1942 "Locking of files succeeded with OK status ($backendName)." );
1943
1944 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_EX );
1945 $this->assertEquals( array(), $status->errors,
1946 "Locking of files succeeded ($backendName)." );
1947 $this->assertEquals( true, $status->isOK(),
1948 "Locking of files succeeded with OK status ($backendName)." );
1949 }
1950 }
1951
1952 // test helper wrapper for backend prepare() function
1953 private function prepare( array $params ) {
1954 return $this->backend->prepare( $params );
1955 }
1956
1957 // test helper wrapper for backend prepare() function
1958 private function create( array $params ) {
1959 $params['op'] = 'create';
1960 return $this->backend->doQuickOperations( array( $params ) );
1961 }
1962
1963 function tearDownFiles() {
1964 foreach ( $this->filesToPrune as $file ) {
1965 @unlink( $file );
1966 }
1967 $containers = array( 'unittest-cont1', 'unittest-cont2' );
1968 foreach ( $containers as $container ) {
1969 $this->deleteFiles( $container );
1970 }
1971 $this->filesToPrune = array();
1972 }
1973
1974 private function deleteFiles( $container ) {
1975 $base = self::baseStorePath();
1976 $iter = $this->backend->getFileList( array( 'dir' => "$base/$container" ) );
1977 if ( $iter ) {
1978 foreach ( $iter as $file ) {
1979 $this->backend->quickDelete( array( 'src' => "$base/$container/$file" ) );
1980 }
1981 }
1982 $this->backend->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
1983 }
1984
1985 function assertBackendPathsConsistent( array $paths ) {
1986 if ( $this->backend instanceof FileBackendMultiWrite ) {
1987 $status = $this->backend->consistencyCheck( $paths );
1988 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
1989 }
1990 }
1991
1992 function assertGoodStatus( $status, $msg ) {
1993 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors, 1 ), $msg );
1994 }
1995 }