Merge "Make sure that SQLite uses no prefix"
[lhc/web/wiklou.git] / tests / phpunit / includes / filebackend / 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_testGetFileHttpUrl
1186 */
1187 public function testGetFileHttpUrl( $source, $content ) {
1188 $this->backend = $this->singleBackend;
1189 $this->tearDownFiles();
1190 $this->doTestGetFileHttpUrl( $source, $content );
1191 $this->tearDownFiles();
1192
1193 $this->backend = $this->multiBackend;
1194 $this->tearDownFiles();
1195 $this->doTestGetFileHttpUrl( $source, $content );
1196 $this->tearDownFiles();
1197 }
1198
1199 private function doTestGetFileHttpUrl( $source, $content ) {
1200 $backendName = $this->backendClass();
1201
1202 $this->prepare( array( 'dir' => dirname( $source ) ) );
1203 $status = $this->backend->doOperation(
1204 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
1205 $this->assertGoodStatus( $status,
1206 "Creation of file at $source succeeded ($backendName)." );
1207
1208 $url = $this->backend->getFileHttpUrl( array( 'src' => $source ) );
1209
1210 if ( $url !== null ) { // supported
1211 $data = Http::request( "GET", $url );
1212 $this->assertEquals( $content, $data,
1213 "HTTP GET of URL has right contents ($backendName)." );
1214 }
1215 }
1216
1217 function provider_testGetFileHttpUrl() {
1218 $cases = array();
1219
1220 $base = self::baseStorePath();
1221 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1222 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1223 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1224
1225 return $cases;
1226 }
1227
1228 /**
1229 * @dataProvider provider_testPrepareAndClean
1230 */
1231 public function testPrepareAndClean( $path, $isOK ) {
1232 $this->backend = $this->singleBackend;
1233 $this->doTestPrepareAndClean( $path, $isOK );
1234 $this->tearDownFiles();
1235
1236 $this->backend = $this->multiBackend;
1237 $this->doTestPrepareAndClean( $path, $isOK );
1238 $this->tearDownFiles();
1239 }
1240
1241 function provider_testPrepareAndClean() {
1242 $base = self::baseStorePath();
1243 return array(
1244 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1245 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1246 # Specific to FS backend with no basePath field set
1247 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1248 );
1249 }
1250
1251 private function doTestPrepareAndClean( $path, $isOK ) {
1252 $backendName = $this->backendClass();
1253
1254 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1255 if ( $isOK ) {
1256 $this->assertGoodStatus( $status,
1257 "Preparing dir $path succeeded without warnings ($backendName)." );
1258 $this->assertEquals( true, $status->isOK(),
1259 "Preparing dir $path succeeded ($backendName)." );
1260 } else {
1261 $this->assertEquals( false, $status->isOK(),
1262 "Preparing dir $path failed ($backendName)." );
1263 }
1264
1265 $status = $this->backend->clean( array( 'dir' => dirname( $path ) ) );
1266 if ( $isOK ) {
1267 $this->assertGoodStatus( $status,
1268 "Cleaning dir $path succeeded without warnings ($backendName)." );
1269 $this->assertEquals( true, $status->isOK(),
1270 "Cleaning dir $path succeeded ($backendName)." );
1271 } else {
1272 $this->assertEquals( false, $status->isOK(),
1273 "Cleaning dir $path failed ($backendName)." );
1274 }
1275 }
1276
1277 public function testRecursiveClean() {
1278 $this->backend = $this->singleBackend;
1279 $this->doTestRecursiveClean();
1280 $this->tearDownFiles();
1281
1282 $this->backend = $this->multiBackend;
1283 $this->doTestRecursiveClean();
1284 $this->tearDownFiles();
1285 }
1286
1287 private function doTestRecursiveClean() {
1288 $backendName = $this->backendClass();
1289
1290 $base = self::baseStorePath();
1291 $dirs = array(
1292 "$base/unittest-cont1/e/a",
1293 "$base/unittest-cont1/e/a/b",
1294 "$base/unittest-cont1/e/a/b/c",
1295 "$base/unittest-cont1/e/a/b/c/d0",
1296 "$base/unittest-cont1/e/a/b/c/d1",
1297 "$base/unittest-cont1/e/a/b/c/d2",
1298 "$base/unittest-cont1/e/a/b/c/d0/1",
1299 "$base/unittest-cont1/e/a/b/c/d0/2",
1300 "$base/unittest-cont1/e/a/b/c/d1/3",
1301 "$base/unittest-cont1/e/a/b/c/d1/4",
1302 "$base/unittest-cont1/e/a/b/c/d2/5",
1303 "$base/unittest-cont1/e/a/b/c/d2/6"
1304 );
1305 foreach ( $dirs as $dir ) {
1306 $status = $this->prepare( array( 'dir' => $dir ) );
1307 $this->assertGoodStatus( $status,
1308 "Preparing dir $dir succeeded without warnings ($backendName)." );
1309 }
1310
1311 if ( $this->backend instanceof FSFileBackend ) {
1312 foreach ( $dirs as $dir ) {
1313 $this->assertEquals( true, $this->backend->directoryExists( array( 'dir' => $dir ) ),
1314 "Dir $dir exists ($backendName)." );
1315 }
1316 }
1317
1318 $status = $this->backend->clean(
1319 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1320 $this->assertGoodStatus( $status,
1321 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1322
1323 foreach ( $dirs as $dir ) {
1324 $this->assertEquals( false, $this->backend->directoryExists( array( 'dir' => $dir ) ),
1325 "Dir $dir no longer exists ($backendName)." );
1326 }
1327 }
1328
1329 // @TODO: testSecure
1330
1331 public function testDoOperations() {
1332 $this->backend = $this->singleBackend;
1333 $this->tearDownFiles();
1334 $this->doTestDoOperations();
1335 $this->tearDownFiles();
1336
1337 $this->backend = $this->multiBackend;
1338 $this->tearDownFiles();
1339 $this->doTestDoOperations();
1340 $this->tearDownFiles();
1341 }
1342
1343 private function doTestDoOperations() {
1344 $base = self::baseStorePath();
1345
1346 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1347 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1348 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1349 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1350 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1351 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1352 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1353
1354 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1355 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1356 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1357 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1358 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1359 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1360 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1361
1362 $status = $this->backend->doOperations( array(
1363 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1364 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1365 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1366 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1367 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1368 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1369 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1370 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1371 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1372 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1373 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1374 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1375 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1376 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1377 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1378 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1379 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1380 // Does nothing
1381 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1382 // Does nothing
1383 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1384 // Does nothing
1385 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1386 // Does nothing
1387 array( 'op' => 'null' ),
1388 // Does nothing
1389 ) );
1390
1391 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1392 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1393 $this->assertEquals( 13, count( $status->success ),
1394 "Operation batch has correct success array" );
1395
1396 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
1397 "File does not exist at $fileA" );
1398 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1399 "File does not exist at $fileB" );
1400 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1401 "File does not exist at $fileD" );
1402
1403 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1404 "File exists at $fileC" );
1405 $this->assertEquals( $fileBContents,
1406 $this->backend->getFileContents( array( 'src' => $fileC ) ),
1407 "Correct file contents of $fileC" );
1408 $this->assertEquals( strlen( $fileBContents ),
1409 $this->backend->getFileSize( array( 'src' => $fileC ) ),
1410 "Correct file size of $fileC" );
1411 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1412 $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
1413 "Correct file SHA-1 of $fileC" );
1414 }
1415
1416 public function testDoOperationsPipeline() {
1417 $this->backend = $this->singleBackend;
1418 $this->tearDownFiles();
1419 $this->doTestDoOperationsPipeline();
1420 $this->tearDownFiles();
1421
1422 $this->backend = $this->multiBackend;
1423 $this->tearDownFiles();
1424 $this->doTestDoOperationsPipeline();
1425 $this->tearDownFiles();
1426 }
1427
1428 // concurrency orientated
1429 private function doTestDoOperationsPipeline() {
1430 $base = self::baseStorePath();
1431
1432 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1433 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1434 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1435
1436 $tmpNameA = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1437 file_put_contents( $tmpNameA, $fileAContents );
1438 $tmpNameB = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1439 file_put_contents( $tmpNameB, $fileBContents );
1440 $tmpNameC = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1441 file_put_contents( $tmpNameC, $fileCContents );
1442
1443 $this->filesToPrune[] = $tmpNameA; # avoid file leaking
1444 $this->filesToPrune[] = $tmpNameB; # avoid file leaking
1445 $this->filesToPrune[] = $tmpNameC; # avoid file leaking
1446
1447 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1448 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1449 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1450 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1451
1452 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1453 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1454 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1455 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1456 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1457
1458 $status = $this->backend->doOperations( array(
1459 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1460 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1461 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1462 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1463 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1464 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1465 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1466 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1467 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1468 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1469 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1470 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1471 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1472 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1473 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1474 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1475 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1476 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1477 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1478 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1479 // Does nothing
1480 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1481 // Does nothing
1482 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1483 // Does nothing
1484 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1485 // Does nothing
1486 array( 'op' => 'null' ),
1487 // Does nothing
1488 ) );
1489
1490 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1491 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1492 $this->assertEquals( 16, count( $status->success ),
1493 "Operation batch has correct success array" );
1494
1495 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
1496 "File does not exist at $fileA" );
1497 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1498 "File does not exist at $fileB" );
1499 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1500 "File does not exist at $fileD" );
1501
1502 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1503 "File exists at $fileC" );
1504 $this->assertEquals( $fileBContents,
1505 $this->backend->getFileContents( array( 'src' => $fileC ) ),
1506 "Correct file contents of $fileC" );
1507 $this->assertEquals( strlen( $fileBContents ),
1508 $this->backend->getFileSize( array( 'src' => $fileC ) ),
1509 "Correct file size of $fileC" );
1510 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1511 $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
1512 "Correct file SHA-1 of $fileC" );
1513 }
1514
1515 public function testDoOperationsFailing() {
1516 $this->backend = $this->singleBackend;
1517 $this->tearDownFiles();
1518 $this->doTestDoOperationsFailing();
1519 $this->tearDownFiles();
1520
1521 $this->backend = $this->multiBackend;
1522 $this->tearDownFiles();
1523 $this->doTestDoOperationsFailing();
1524 $this->tearDownFiles();
1525 }
1526
1527 private function doTestDoOperationsFailing() {
1528 $base = self::baseStorePath();
1529
1530 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1531 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1532 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1533 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1534 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1535 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1536 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1537
1538 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1539 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1540 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1541 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1542 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1543 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1544
1545 $status = $this->backend->doOperations( array(
1546 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1547 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1548 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1549 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1550 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1551 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1552 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1553 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1554 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1555 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1556 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1557 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1558 array( 'op' => 'delete', 'src' => $fileD ),
1559 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1560 array( 'op' => 'null' ),
1561 // Does nothing
1562 ), array( 'force' => 1 ) );
1563
1564 $this->assertNotEquals( array(), $status->errors, "Operation had warnings" );
1565 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1566 $this->assertEquals( 8, count( $status->success ),
1567 "Operation batch has correct success array" );
1568
1569 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1570 "File does not exist at $fileB" );
1571 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1572 "File does not exist at $fileD" );
1573
1574 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileA ) ),
1575 "File does not exist at $fileA" );
1576 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1577 "File exists at $fileC" );
1578 $this->assertEquals( $fileBContents,
1579 $this->backend->getFileContents( array( 'src' => $fileA ) ),
1580 "Correct file contents of $fileA" );
1581 $this->assertEquals( strlen( $fileBContents ),
1582 $this->backend->getFileSize( array( 'src' => $fileA ) ),
1583 "Correct file size of $fileA" );
1584 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1585 $this->backend->getFileSha1Base36( array( 'src' => $fileA ) ),
1586 "Correct file SHA-1 of $fileA" );
1587 }
1588
1589 public function testGetFileList() {
1590 $this->backend = $this->singleBackend;
1591 $this->tearDownFiles();
1592 $this->doTestGetFileList();
1593 $this->tearDownFiles();
1594
1595 $this->backend = $this->multiBackend;
1596 $this->tearDownFiles();
1597 $this->doTestGetFileList();
1598 $this->tearDownFiles();
1599 }
1600
1601 private function doTestGetFileList() {
1602 $backendName = $this->backendClass();
1603 $base = self::baseStorePath();
1604
1605 // Should have no errors
1606 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1607
1608 $files = array(
1609 "$base/unittest-cont1/e/test1.txt",
1610 "$base/unittest-cont1/e/test2.txt",
1611 "$base/unittest-cont1/e/test3.txt",
1612 "$base/unittest-cont1/e/subdir1/test1.txt",
1613 "$base/unittest-cont1/e/subdir1/test2.txt",
1614 "$base/unittest-cont1/e/subdir2/test3.txt",
1615 "$base/unittest-cont1/e/subdir2/test4.txt",
1616 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1617 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1618 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1619 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1620 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1621 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1622 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1623 );
1624
1625 // Add the files
1626 $ops = array();
1627 foreach ( $files as $file ) {
1628 $this->prepare( array( 'dir' => dirname( $file ) ) );
1629 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1630 }
1631 $status = $this->backend->doQuickOperations( $ops );
1632 $this->assertGoodStatus( $status,
1633 "Creation of files succeeded ($backendName)." );
1634 $this->assertEquals( true, $status->isOK(),
1635 "Creation of files succeeded with OK status ($backendName)." );
1636
1637 // Expected listing
1638 $expected = array(
1639 "e/test1.txt",
1640 "e/test2.txt",
1641 "e/test3.txt",
1642 "e/subdir1/test1.txt",
1643 "e/subdir1/test2.txt",
1644 "e/subdir2/test3.txt",
1645 "e/subdir2/test4.txt",
1646 "e/subdir2/subdir/test1.txt",
1647 "e/subdir2/subdir/test2.txt",
1648 "e/subdir2/subdir/test3.txt",
1649 "e/subdir2/subdir/test4.txt",
1650 "e/subdir2/subdir/test5.txt",
1651 "e/subdir2/subdir/sub/test0.txt",
1652 "e/subdir2/subdir/sub/120-px-file.txt",
1653 );
1654 sort( $expected );
1655
1656 // Actual listing (no trailing slash)
1657 $list = array();
1658 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1659 foreach ( $iter as $file ) {
1660 $list[] = $file;
1661 }
1662 sort( $list );
1663
1664 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1665
1666 // Actual listing (with trailing slash)
1667 $list = array();
1668 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1669 foreach ( $iter as $file ) {
1670 $list[] = $file;
1671 }
1672 sort( $list );
1673
1674 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1675
1676 // Expected listing
1677 $expected = array(
1678 "test1.txt",
1679 "test2.txt",
1680 "test3.txt",
1681 "test4.txt",
1682 "test5.txt",
1683 "sub/test0.txt",
1684 "sub/120-px-file.txt",
1685 );
1686 sort( $expected );
1687
1688 // Actual listing (no trailing slash)
1689 $list = array();
1690 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1691 foreach ( $iter as $file ) {
1692 $list[] = $file;
1693 }
1694 sort( $list );
1695
1696 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1697
1698 // Actual listing (with trailing slash)
1699 $list = array();
1700 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1701 foreach ( $iter as $file ) {
1702 $list[] = $file;
1703 }
1704 sort( $list );
1705
1706 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1707
1708 // Actual listing (using iterator second time)
1709 $list = array();
1710 foreach ( $iter as $file ) {
1711 $list[] = $file;
1712 }
1713 sort( $list );
1714
1715 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1716
1717 // Expected listing (top files only)
1718 $expected = array(
1719 "test1.txt",
1720 "test2.txt",
1721 "test3.txt",
1722 "test4.txt",
1723 "test5.txt"
1724 );
1725 sort( $expected );
1726
1727 // Actual listing (top files only)
1728 $list = array();
1729 $iter = $this->backend->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1730 foreach ( $iter as $file ) {
1731 $list[] = $file;
1732 }
1733 sort( $list );
1734
1735 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1736
1737 foreach ( $files as $file ) { // clean up
1738 $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1739 }
1740
1741 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1742 foreach ( $iter as $iter ) {} // no errors
1743 }
1744
1745 public function testGetDirectoryList() {
1746 $this->backend = $this->singleBackend;
1747 $this->tearDownFiles();
1748 $this->doTestGetDirectoryList();
1749 $this->tearDownFiles();
1750
1751 $this->backend = $this->multiBackend;
1752 $this->tearDownFiles();
1753 $this->doTestGetDirectoryList();
1754 $this->tearDownFiles();
1755 }
1756
1757 private function doTestGetDirectoryList() {
1758 $backendName = $this->backendClass();
1759
1760 $base = self::baseStorePath();
1761 $files = array(
1762 "$base/unittest-cont1/e/test1.txt",
1763 "$base/unittest-cont1/e/test2.txt",
1764 "$base/unittest-cont1/e/test3.txt",
1765 "$base/unittest-cont1/e/subdir1/test1.txt",
1766 "$base/unittest-cont1/e/subdir1/test2.txt",
1767 "$base/unittest-cont1/e/subdir2/test3.txt",
1768 "$base/unittest-cont1/e/subdir2/test4.txt",
1769 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1770 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1771 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1772 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1773 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1774 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1775 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1776 );
1777
1778 // Add the files
1779 $ops = array();
1780 foreach ( $files as $file ) {
1781 $this->prepare( array( 'dir' => dirname( $file ) ) );
1782 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1783 }
1784 $status = $this->backend->doQuickOperations( $ops );
1785 $this->assertGoodStatus( $status,
1786 "Creation of files succeeded ($backendName)." );
1787 $this->assertEquals( true, $status->isOK(),
1788 "Creation of files succeeded with OK status ($backendName)." );
1789
1790 $this->assertEquals( true,
1791 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1792 "Directory exists in ($backendName)." );
1793 $this->assertEquals( true,
1794 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1795 "Directory exists in ($backendName)." );
1796 $this->assertEquals( false,
1797 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1798 "Directory does not exists in ($backendName)." );
1799
1800 // Expected listing
1801 $expected = array(
1802 "e",
1803 );
1804 sort( $expected );
1805
1806 // Actual listing (no trailing slash)
1807 $list = array();
1808 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1809 foreach ( $iter as $file ) {
1810 $list[] = $file;
1811 }
1812 sort( $list );
1813
1814 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1815
1816 // Expected listing
1817 $expected = array(
1818 "subdir1",
1819 "subdir2",
1820 "subdir3",
1821 "subdir4",
1822 );
1823 sort( $expected );
1824
1825 // Actual listing (no trailing slash)
1826 $list = array();
1827 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1828 foreach ( $iter as $file ) {
1829 $list[] = $file;
1830 }
1831 sort( $list );
1832
1833 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1834
1835 // Actual listing (with trailing slash)
1836 $list = array();
1837 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1838 foreach ( $iter as $file ) {
1839 $list[] = $file;
1840 }
1841 sort( $list );
1842
1843 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1844
1845 // Expected listing
1846 $expected = array(
1847 "subdir",
1848 );
1849 sort( $expected );
1850
1851 // Actual listing (no trailing slash)
1852 $list = array();
1853 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1854 foreach ( $iter as $file ) {
1855 $list[] = $file;
1856 }
1857 sort( $list );
1858
1859 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1860
1861 // Actual listing (with trailing slash)
1862 $list = array();
1863 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1864 foreach ( $iter as $file ) {
1865 $list[] = $file;
1866 }
1867 sort( $list );
1868
1869 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1870
1871 // Actual listing (using iterator second time)
1872 $list = array();
1873 foreach ( $iter as $file ) {
1874 $list[] = $file;
1875 }
1876 sort( $list );
1877
1878 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1879
1880 // Expected listing (recursive)
1881 $expected = array(
1882 "e",
1883 "e/subdir1",
1884 "e/subdir2",
1885 "e/subdir3",
1886 "e/subdir4",
1887 "e/subdir2/subdir",
1888 "e/subdir3/subdir",
1889 "e/subdir4/subdir",
1890 "e/subdir4/subdir/sub",
1891 );
1892 sort( $expected );
1893
1894 // Actual listing (recursive)
1895 $list = array();
1896 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1897 foreach ( $iter as $file ) {
1898 $list[] = $file;
1899 }
1900 sort( $list );
1901
1902 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1903
1904 // Expected listing (recursive)
1905 $expected = array(
1906 "subdir",
1907 "subdir/sub",
1908 );
1909 sort( $expected );
1910
1911 // Actual listing (recursive)
1912 $list = array();
1913 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
1914 foreach ( $iter as $file ) {
1915 $list[] = $file;
1916 }
1917 sort( $list );
1918
1919 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1920
1921 // Actual listing (recursive, second time)
1922 $list = array();
1923 foreach ( $iter as $file ) {
1924 $list[] = $file;
1925 }
1926 sort( $list );
1927
1928 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1929
1930 foreach ( $files as $file ) { // clean up
1931 $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1932 }
1933
1934 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1935 foreach ( $iter as $iter ) {} // no errors
1936 }
1937
1938 public function testLockCalls() {
1939 $this->backend = $this->singleBackend;
1940 $this->doTestLockCalls();
1941 }
1942
1943 private function doTestLockCalls() {
1944 $backendName = $this->backendClass();
1945
1946 for ( $i=0; $i<50; $i++ ) {
1947 $paths = array(
1948 "test1.txt",
1949 "test2.txt",
1950 "test3.txt",
1951 "subdir1",
1952 "subdir1", // duplicate
1953 "subdir1/test1.txt",
1954 "subdir1/test2.txt",
1955 "subdir2",
1956 "subdir2", // duplicate
1957 "subdir2/test3.txt",
1958 "subdir2/test4.txt",
1959 "subdir2/subdir",
1960 "subdir2/subdir/test1.txt",
1961 "subdir2/subdir/test2.txt",
1962 "subdir2/subdir/test3.txt",
1963 "subdir2/subdir/test4.txt",
1964 "subdir2/subdir/test5.txt",
1965 "subdir2/subdir/sub",
1966 "subdir2/subdir/sub/test0.txt",
1967 "subdir2/subdir/sub/120-px-file.txt",
1968 );
1969
1970 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
1971 $this->assertEquals( array(), $status->errors,
1972 "Locking of files succeeded ($backendName)." );
1973 $this->assertEquals( true, $status->isOK(),
1974 "Locking of files succeeded with OK status ($backendName)." );
1975
1976 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
1977 $this->assertEquals( array(), $status->errors,
1978 "Locking of files succeeded ($backendName)." );
1979 $this->assertEquals( true, $status->isOK(),
1980 "Locking of files succeeded with OK status ($backendName)." );
1981
1982 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
1983 $this->assertEquals( array(), $status->errors,
1984 "Locking of files succeeded ($backendName)." );
1985 $this->assertEquals( true, $status->isOK(),
1986 "Locking of files succeeded with OK status ($backendName)." );
1987
1988 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_EX );
1989 $this->assertEquals( array(), $status->errors,
1990 "Locking of files succeeded ($backendName)." );
1991 $this->assertEquals( true, $status->isOK(),
1992 "Locking of files succeeded with OK status ($backendName)." );
1993 }
1994 }
1995
1996 // test helper wrapper for backend prepare() function
1997 private function prepare( array $params ) {
1998 return $this->backend->prepare( $params );
1999 }
2000
2001 // test helper wrapper for backend prepare() function
2002 private function create( array $params ) {
2003 $params['op'] = 'create';
2004 return $this->backend->doQuickOperations( array( $params ) );
2005 }
2006
2007 function tearDownFiles() {
2008 foreach ( $this->filesToPrune as $file ) {
2009 @unlink( $file );
2010 }
2011 $containers = array( 'unittest-cont1', 'unittest-cont2' );
2012 foreach ( $containers as $container ) {
2013 $this->deleteFiles( $container );
2014 }
2015 $this->filesToPrune = array();
2016 }
2017
2018 private function deleteFiles( $container ) {
2019 $base = self::baseStorePath();
2020 $iter = $this->backend->getFileList( array( 'dir' => "$base/$container" ) );
2021 if ( $iter ) {
2022 foreach ( $iter as $file ) {
2023 $this->backend->quickDelete( array( 'src' => "$base/$container/$file" ) );
2024 }
2025 }
2026 $this->backend->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
2027 }
2028
2029 function assertBackendPathsConsistent( array $paths ) {
2030 if ( $this->backend instanceof FileBackendMultiWrite ) {
2031 $status = $this->backend->consistencyCheck( $paths );
2032 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2033 }
2034 }
2035
2036 function assertGoodStatus( $status, $msg ) {
2037 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors, 1 ), $msg );
2038 }
2039 }