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