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