Merge "resourceloader: Compile documentElement.className server-side"
[lhc/web/wiklou.git] / tests / phpunit / includes / filebackend / FileBackendTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use Wikimedia\TestingAccessWrapper;
5
6 /**
7 * @group FileRepo
8 * @group FileBackend
9 * @group medium
10 *
11 * @covers FileBackend
12 *
13 * @covers CopyFileOp
14 * @covers CreateFileOp
15 * @covers DeleteFileOp
16 * @covers DescribeFileOp
17 * @covers FSFile
18 * @covers FSFileBackend
19 * @covers FSFileBackendDirList
20 * @covers FSFileBackendFileList
21 * @covers FSFileBackendList
22 * @covers FSFileOpHandle
23 * @covers FileBackendDBRepoWrapper
24 * @covers FileBackendError
25 * @covers FileBackendGroup
26 * @covers FileBackendMultiWrite
27 * @covers FileBackendStore
28 * @covers FileBackendStoreOpHandle
29 * @covers FileBackendStoreShardDirIterator
30 * @covers FileBackendStoreShardFileIterator
31 * @covers FileBackendStoreShardListIterator
32 * @covers FileOp
33 * @covers FileOpBatch
34 * @covers HTTPFileStreamer
35 * @covers LockManagerGroup
36 * @covers MemoryFileBackend
37 * @covers MoveFileOp
38 * @covers MySqlLockManager
39 * @covers NullFileOp
40 * @covers StoreFileOp
41 * @covers TempFSFile
42 *
43 * @covers FSLockManager
44 * @covers LockManager
45 * @covers NullLockManager
46 */
47 class FileBackendTest extends MediaWikiTestCase {
48
49 /** @var FileBackend */
50 private $backend;
51 /** @var FileBackendMultiWrite */
52 private $multiBackend;
53 /** @var FSFileBackend */
54 public $singleBackend;
55 private static $backendToUse;
56
57 protected function setUp() {
58 global $wgFileBackends;
59 parent::setUp();
60 $tmpDir = $this->getNewTempDirectory();
61 if ( $this->getCliArg( 'use-filebackend' ) ) {
62 if ( self::$backendToUse ) {
63 $this->singleBackend = self::$backendToUse;
64 } else {
65 $name = $this->getCliArg( 'use-filebackend' );
66 $useConfig = [];
67 foreach ( $wgFileBackends as $conf ) {
68 if ( $conf['name'] == $name ) {
69 $useConfig = $conf;
70 break;
71 }
72 }
73 $useConfig['name'] = 'localtesting'; // swap name
74 $useConfig['shardViaHashLevels'] = [ // test sharding
75 'unittest-cont1' => [ 'levels' => 1, 'base' => 16, 'repeat' => 1 ]
76 ];
77 if ( isset( $useConfig['fileJournal'] ) ) {
78 $useConfig['fileJournal'] = FileJournal::factory( $useConfig['fileJournal'], $name );
79 }
80 $useConfig['lockManager'] = LockManagerGroup::singleton()->get( $useConfig['lockManager'] );
81 $class = $useConfig['class'];
82 self::$backendToUse = new $class( $useConfig );
83 $this->singleBackend = self::$backendToUse;
84 }
85 } else {
86 $this->singleBackend = new FSFileBackend( [
87 'name' => 'localtesting',
88 'lockManager' => LockManagerGroup::singleton()->get( 'fsLockManager' ),
89 'wikiId' => wfWikiID(),
90 'containerPaths' => [
91 'unittest-cont1' => "{$tmpDir}/localtesting-cont1",
92 'unittest-cont2' => "{$tmpDir}/localtesting-cont2" ]
93 ] );
94 }
95 $this->multiBackend = new FileBackendMultiWrite( [
96 'name' => 'localtesting',
97 'lockManager' => LockManagerGroup::singleton()->get( 'fsLockManager' ),
98 'parallelize' => 'implicit',
99 'wikiId' => 'testdb',
100 'backends' => [
101 [
102 'name' => 'localmultitesting1',
103 'class' => FSFileBackend::class,
104 'containerPaths' => [
105 'unittest-cont1' => "{$tmpDir}/localtestingmulti1-cont1",
106 'unittest-cont2' => "{$tmpDir}/localtestingmulti1-cont2" ],
107 'isMultiMaster' => false
108 ],
109 [
110 'name' => 'localmultitesting2',
111 'class' => FSFileBackend::class,
112 'containerPaths' => [
113 'unittest-cont1' => "{$tmpDir}/localtestingmulti2-cont1",
114 'unittest-cont2' => "{$tmpDir}/localtestingmulti2-cont2" ],
115 'isMultiMaster' => true
116 ]
117 ]
118 ] );
119 }
120
121 private static function baseStorePath() {
122 return 'mwstore://localtesting';
123 }
124
125 private function backendClass() {
126 return get_class( $this->backend );
127 }
128
129 /**
130 * @dataProvider provider_testIsStoragePath
131 */
132 public function testIsStoragePath( $path, $isStorePath ) {
133 $this->assertEquals( $isStorePath, FileBackend::isStoragePath( $path ),
134 "FileBackend::isStoragePath on path '$path'" );
135 }
136
137 public static function provider_testIsStoragePath() {
138 return [
139 [ 'mwstore://', true ],
140 [ 'mwstore://backend', true ],
141 [ 'mwstore://backend/container', true ],
142 [ 'mwstore://backend/container/', true ],
143 [ 'mwstore://backend/container/path', true ],
144 [ 'mwstore://backend//container/', true ],
145 [ 'mwstore://backend//container//', true ],
146 [ 'mwstore://backend//container//path', true ],
147 [ 'mwstore:///', true ],
148 [ 'mwstore:/', false ],
149 [ 'mwstore:', false ],
150 ];
151 }
152
153 /**
154 * @dataProvider provider_testSplitStoragePath
155 */
156 public function testSplitStoragePath( $path, $res ) {
157 $this->assertEquals( $res, FileBackend::splitStoragePath( $path ),
158 "FileBackend::splitStoragePath on path '$path'" );
159 }
160
161 public static function provider_testSplitStoragePath() {
162 return [
163 [ 'mwstore://backend/container', [ 'backend', 'container', '' ] ],
164 [ 'mwstore://backend/container/', [ 'backend', 'container', '' ] ],
165 [ 'mwstore://backend/container/path', [ 'backend', 'container', 'path' ] ],
166 [ 'mwstore://backend/container//path', [ 'backend', 'container', '/path' ] ],
167 [ 'mwstore://backend//container/path', [ null, null, null ] ],
168 [ 'mwstore://backend//container//path', [ null, null, null ] ],
169 [ 'mwstore://', [ null, null, null ] ],
170 [ 'mwstore://backend', [ null, null, null ] ],
171 [ 'mwstore:///', [ null, null, null ] ],
172 [ 'mwstore:/', [ null, null, null ] ],
173 [ 'mwstore:', [ null, null, null ] ]
174 ];
175 }
176
177 /**
178 * @dataProvider provider_normalizeStoragePath
179 */
180 public function testNormalizeStoragePath( $path, $res ) {
181 $this->assertEquals( $res, FileBackend::normalizeStoragePath( $path ),
182 "FileBackend::normalizeStoragePath on path '$path'" );
183 }
184
185 public static function provider_normalizeStoragePath() {
186 return [
187 [ 'mwstore://backend/container', 'mwstore://backend/container' ],
188 [ 'mwstore://backend/container/', 'mwstore://backend/container' ],
189 [ 'mwstore://backend/container/path', 'mwstore://backend/container/path' ],
190 [ 'mwstore://backend/container//path', 'mwstore://backend/container/path' ],
191 [ 'mwstore://backend/container///path', 'mwstore://backend/container/path' ],
192 [
193 'mwstore://backend/container///path//to///obj',
194 'mwstore://backend/container/path/to/obj'
195 ],
196 [ 'mwstore://', null ],
197 [ 'mwstore://backend', null ],
198 [ 'mwstore://backend//container/path', null ],
199 [ 'mwstore://backend//container//path', null ],
200 [ 'mwstore:///', null ],
201 [ 'mwstore:/', null ],
202 [ 'mwstore:', null ],
203 ];
204 }
205
206 /**
207 * @dataProvider provider_testParentStoragePath
208 */
209 public function testParentStoragePath( $path, $res ) {
210 $this->assertEquals( $res, FileBackend::parentStoragePath( $path ),
211 "FileBackend::parentStoragePath on path '$path'" );
212 }
213
214 public static function provider_testParentStoragePath() {
215 return [
216 [ 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ],
217 [ 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ],
218 [ 'mwstore://backend/container/path', 'mwstore://backend/container' ],
219 [ 'mwstore://backend/container', null ],
220 [ 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ],
221 [ 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ],
222 [ 'mwstore://backend/container/path/', 'mwstore://backend/container' ],
223 [ 'mwstore://backend/container/', null ],
224 ];
225 }
226
227 /**
228 * @dataProvider provider_testExtensionFromPath
229 */
230 public function testExtensionFromPath( $path, $res ) {
231 $this->assertEquals( $res, FileBackend::extensionFromPath( $path ),
232 "FileBackend::extensionFromPath on path '$path'" );
233 }
234
235 public static function provider_testExtensionFromPath() {
236 return [
237 [ 'mwstore://backend/container/path.txt', 'txt' ],
238 [ 'mwstore://backend/container/path.svg.png', 'png' ],
239 [ 'mwstore://backend/container/path', '' ],
240 [ 'mwstore://backend/container/path.', '' ],
241 ];
242 }
243
244 /**
245 * @dataProvider provider_testStore
246 */
247 public function testStore( $op ) {
248 $this->addTmpFiles( $op['src'] );
249
250 $this->backend = $this->singleBackend;
251 $this->tearDownFiles();
252 $this->doTestStore( $op );
253 $this->tearDownFiles();
254
255 $this->backend = $this->multiBackend;
256 $this->tearDownFiles();
257 $this->doTestStore( $op );
258 $this->tearDownFiles();
259 }
260
261 private function doTestStore( $op ) {
262 $backendName = $this->backendClass();
263
264 $source = $op['src'];
265 $dest = $op['dst'];
266 $this->prepare( [ 'dir' => dirname( $dest ) ] );
267
268 file_put_contents( $source, "Unit test file" );
269
270 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
271 $this->backend->store( $op );
272 }
273
274 $status = $this->backend->doOperation( $op );
275
276 $this->assertGoodStatus( $status,
277 "Store from $source to $dest succeeded without warnings ($backendName)." );
278 $this->assertEquals( true, $status->isOK(),
279 "Store from $source to $dest succeeded ($backendName)." );
280 $this->assertEquals( [ 0 => true ], $status->success,
281 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
282 $this->assertEquals( true, file_exists( $source ),
283 "Source file $source still exists ($backendName)." );
284 $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $dest ] ),
285 "Destination file $dest exists ($backendName)." );
286
287 $this->assertEquals( filesize( $source ),
288 $this->backend->getFileSize( [ 'src' => $dest ] ),
289 "Destination file $dest has correct size ($backendName)." );
290
291 $props1 = FSFile::getPropsFromPath( $source );
292 $props2 = $this->backend->getFileProps( [ 'src' => $dest ] );
293 $this->assertEquals( $props1, $props2,
294 "Source and destination have the same props ($backendName)." );
295
296 $this->assertBackendPathsConsistent( [ $dest ] );
297 }
298
299 public static function provider_testStore() {
300 $cases = [];
301
302 $tmpName = TempFSFile::factory( "unittests_", 'txt', wfTempDir() )->getPath();
303 $toPath = self::baseStorePath() . '/unittest-cont1/e/fun/obj1.txt';
304 $op = [ 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath ];
305 $cases[] = [ $op ];
306
307 $op2 = $op;
308 $op2['overwrite'] = true;
309 $cases[] = [ $op2 ];
310
311 $op3 = $op;
312 $op3['overwriteSame'] = true;
313 $cases[] = [ $op3 ];
314
315 return $cases;
316 }
317
318 /**
319 * @dataProvider provider_testCopy
320 */
321 public function testCopy( $op ) {
322 $this->backend = $this->singleBackend;
323 $this->tearDownFiles();
324 $this->doTestCopy( $op );
325 $this->tearDownFiles();
326
327 $this->backend = $this->multiBackend;
328 $this->tearDownFiles();
329 $this->doTestCopy( $op );
330 $this->tearDownFiles();
331 }
332
333 private function doTestCopy( $op ) {
334 $backendName = $this->backendClass();
335
336 $source = $op['src'];
337 $dest = $op['dst'];
338 $this->prepare( [ 'dir' => dirname( $source ) ] );
339 $this->prepare( [ 'dir' => dirname( $dest ) ] );
340
341 if ( isset( $op['ignoreMissingSource'] ) ) {
342 $status = $this->backend->doOperation( $op );
343 $this->assertGoodStatus( $status,
344 "Move from $source to $dest succeeded without warnings ($backendName)." );
345 $this->assertEquals( [ 0 => true ], $status->success,
346 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
347 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $source ] ),
348 "Source file $source does not exist ($backendName)." );
349 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $dest ] ),
350 "Destination file $dest does not exist ($backendName)." );
351
352 return;
353 }
354
355 $status = $this->backend->doOperation(
356 [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ] );
357 $this->assertGoodStatus( $status,
358 "Creation of file at $source succeeded ($backendName)." );
359
360 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
361 $this->backend->copy( $op );
362 }
363
364 $status = $this->backend->doOperation( $op );
365
366 $this->assertGoodStatus( $status,
367 "Copy from $source to $dest succeeded without warnings ($backendName)." );
368 $this->assertEquals( true, $status->isOK(),
369 "Copy from $source to $dest succeeded ($backendName)." );
370 $this->assertEquals( [ 0 => true ], $status->success,
371 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
372 $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $source ] ),
373 "Source file $source still exists ($backendName)." );
374 $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $dest ] ),
375 "Destination file $dest exists after copy ($backendName)." );
376
377 $this->assertEquals(
378 $this->backend->getFileSize( [ 'src' => $source ] ),
379 $this->backend->getFileSize( [ 'src' => $dest ] ),
380 "Destination file $dest has correct size ($backendName)." );
381
382 $props1 = $this->backend->getFileProps( [ 'src' => $source ] );
383 $props2 = $this->backend->getFileProps( [ 'src' => $dest ] );
384 $this->assertEquals( $props1, $props2,
385 "Source and destination have the same props ($backendName)." );
386
387 $this->assertBackendPathsConsistent( [ $source, $dest ] );
388 }
389
390 public static function provider_testCopy() {
391 $cases = [];
392
393 $source = self::baseStorePath() . '/unittest-cont1/e/file.txt';
394 $dest = self::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
395
396 $op = [ 'op' => 'copy', 'src' => $source, 'dst' => $dest ];
397 $cases[] = [
398 $op, // operation
399 $source, // source
400 $dest, // dest
401 ];
402
403 $op2 = $op;
404 $op2['overwrite'] = true;
405 $cases[] = [
406 $op2, // operation
407 $source, // source
408 $dest, // dest
409 ];
410
411 $op2 = $op;
412 $op2['overwriteSame'] = true;
413 $cases[] = [
414 $op2, // operation
415 $source, // source
416 $dest, // dest
417 ];
418
419 $op2 = $op;
420 $op2['ignoreMissingSource'] = true;
421 $cases[] = [
422 $op2, // operation
423 $source, // source
424 $dest, // dest
425 ];
426
427 $op2 = $op;
428 $op2['ignoreMissingSource'] = true;
429 $cases[] = [
430 $op2, // operation
431 self::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
432 $dest, // dest
433 ];
434
435 return $cases;
436 }
437
438 /**
439 * @dataProvider provider_testMove
440 */
441 public function testMove( $op ) {
442 $this->backend = $this->singleBackend;
443 $this->tearDownFiles();
444 $this->doTestMove( $op );
445 $this->tearDownFiles();
446
447 $this->backend = $this->multiBackend;
448 $this->tearDownFiles();
449 $this->doTestMove( $op );
450 $this->tearDownFiles();
451 }
452
453 private function doTestMove( $op ) {
454 $backendName = $this->backendClass();
455
456 $source = $op['src'];
457 $dest = $op['dst'];
458 $this->prepare( [ 'dir' => dirname( $source ) ] );
459 $this->prepare( [ 'dir' => dirname( $dest ) ] );
460
461 if ( isset( $op['ignoreMissingSource'] ) ) {
462 $status = $this->backend->doOperation( $op );
463 $this->assertGoodStatus( $status,
464 "Move from $source to $dest succeeded without warnings ($backendName)." );
465 $this->assertEquals( [ 0 => true ], $status->success,
466 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
467 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $source ] ),
468 "Source file $source does not exist ($backendName)." );
469 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $dest ] ),
470 "Destination file $dest does not exist ($backendName)." );
471
472 return;
473 }
474
475 $status = $this->backend->doOperation(
476 [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ] );
477 $this->assertGoodStatus( $status,
478 "Creation of file at $source succeeded ($backendName)." );
479
480 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
481 $this->backend->copy( $op );
482 }
483
484 $status = $this->backend->doOperation( $op );
485 $this->assertGoodStatus( $status,
486 "Move from $source to $dest succeeded without warnings ($backendName)." );
487 $this->assertEquals( true, $status->isOK(),
488 "Move from $source to $dest succeeded ($backendName)." );
489 $this->assertEquals( [ 0 => true ], $status->success,
490 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
491 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $source ] ),
492 "Source file $source does not still exists ($backendName)." );
493 $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $dest ] ),
494 "Destination file $dest exists after move ($backendName)." );
495
496 $this->assertNotEquals(
497 $this->backend->getFileSize( [ 'src' => $source ] ),
498 $this->backend->getFileSize( [ 'src' => $dest ] ),
499 "Destination file $dest has correct size ($backendName)." );
500
501 $props1 = $this->backend->getFileProps( [ 'src' => $source ] );
502 $props2 = $this->backend->getFileProps( [ 'src' => $dest ] );
503 $this->assertEquals( false, $props1['fileExists'],
504 "Source file does not exist accourding to props ($backendName)." );
505 $this->assertEquals( true, $props2['fileExists'],
506 "Destination file exists accourding to props ($backendName)." );
507
508 $this->assertBackendPathsConsistent( [ $source, $dest ] );
509 }
510
511 public static function provider_testMove() {
512 $cases = [];
513
514 $source = self::baseStorePath() . '/unittest-cont1/e/file.txt';
515 $dest = self::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
516
517 $op = [ 'op' => 'move', 'src' => $source, 'dst' => $dest ];
518 $cases[] = [
519 $op, // operation
520 $source, // source
521 $dest, // dest
522 ];
523
524 $op2 = $op;
525 $op2['overwrite'] = true;
526 $cases[] = [
527 $op2, // operation
528 $source, // source
529 $dest, // dest
530 ];
531
532 $op2 = $op;
533 $op2['overwriteSame'] = true;
534 $cases[] = [
535 $op2, // operation
536 $source, // source
537 $dest, // dest
538 ];
539
540 $op2 = $op;
541 $op2['ignoreMissingSource'] = true;
542 $cases[] = [
543 $op2, // operation
544 $source, // source
545 $dest, // dest
546 ];
547
548 $op2 = $op;
549 $op2['ignoreMissingSource'] = true;
550 $cases[] = [
551 $op2, // operation
552 self::baseStorePath() . '/unittest-cont-bad/e/file.txt', // source
553 $dest, // dest
554 ];
555
556 return $cases;
557 }
558
559 /**
560 * @dataProvider provider_testDelete
561 */
562 public function testDelete( $op, $withSource, $okStatus ) {
563 $this->backend = $this->singleBackend;
564 $this->tearDownFiles();
565 $this->doTestDelete( $op, $withSource, $okStatus );
566 $this->tearDownFiles();
567
568 $this->backend = $this->multiBackend;
569 $this->tearDownFiles();
570 $this->doTestDelete( $op, $withSource, $okStatus );
571 $this->tearDownFiles();
572 }
573
574 private function doTestDelete( $op, $withSource, $okStatus ) {
575 $backendName = $this->backendClass();
576
577 $source = $op['src'];
578 $this->prepare( [ 'dir' => dirname( $source ) ] );
579
580 if ( $withSource ) {
581 $status = $this->backend->doOperation(
582 [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ] );
583 $this->assertGoodStatus( $status,
584 "Creation of file at $source succeeded ($backendName)." );
585 }
586
587 $status = $this->backend->doOperation( $op );
588 if ( $okStatus ) {
589 $this->assertGoodStatus( $status,
590 "Deletion of file at $source succeeded without warnings ($backendName)." );
591 $this->assertEquals( true, $status->isOK(),
592 "Deletion of file at $source succeeded ($backendName)." );
593 $this->assertEquals( [ 0 => true ], $status->success,
594 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
595 } else {
596 $this->assertEquals( false, $status->isOK(),
597 "Deletion of file at $source failed ($backendName)." );
598 }
599
600 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $source ] ),
601 "Source file $source does not exist after move ($backendName)." );
602
603 $this->assertFalse(
604 $this->backend->getFileSize( [ 'src' => $source ] ),
605 "Source file $source has correct size (false) ($backendName)." );
606
607 $props1 = $this->backend->getFileProps( [ 'src' => $source ] );
608 $this->assertFalse( $props1['fileExists'],
609 "Source file $source does not exist according to props ($backendName)." );
610
611 $this->assertBackendPathsConsistent( [ $source ] );
612 }
613
614 public static function provider_testDelete() {
615 $cases = [];
616
617 $source = self::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
618
619 $op = [ 'op' => 'delete', 'src' => $source ];
620 $cases[] = [
621 $op, // operation
622 true, // with source
623 true // succeeds
624 ];
625
626 $cases[] = [
627 $op, // operation
628 false, // without source
629 false // fails
630 ];
631
632 $op['ignoreMissingSource'] = true;
633 $cases[] = [
634 $op, // operation
635 false, // without source
636 true // succeeds
637 ];
638
639 $op['ignoreMissingSource'] = true;
640 $op['src'] = self::baseStorePath() . '/unittest-cont-bad/e/file.txt';
641 $cases[] = [
642 $op, // operation
643 false, // without source
644 true // succeeds
645 ];
646
647 return $cases;
648 }
649
650 /**
651 * @dataProvider provider_testDescribe
652 */
653 public function testDescribe( $op, $withSource, $okStatus ) {
654 $this->backend = $this->singleBackend;
655 $this->tearDownFiles();
656 $this->doTestDescribe( $op, $withSource, $okStatus );
657 $this->tearDownFiles();
658
659 $this->backend = $this->multiBackend;
660 $this->tearDownFiles();
661 $this->doTestDescribe( $op, $withSource, $okStatus );
662 $this->tearDownFiles();
663 }
664
665 private function doTestDescribe( $op, $withSource, $okStatus ) {
666 $backendName = $this->backendClass();
667
668 $source = $op['src'];
669 $this->prepare( [ 'dir' => dirname( $source ) ] );
670
671 if ( $withSource ) {
672 $status = $this->backend->doOperation(
673 [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source,
674 'headers' => [ 'Content-Disposition' => 'xxx' ] ] );
675 $this->assertGoodStatus( $status,
676 "Creation of file at $source succeeded ($backendName)." );
677 if ( $this->backend->hasFeatures( FileBackend::ATTR_HEADERS ) ) {
678 $attr = $this->backend->getFileXAttributes( [ 'src' => $source ] );
679 $this->assertHasHeaders( [ 'Content-Disposition' => 'xxx' ], $attr );
680 }
681
682 $status = $this->backend->describe( [ 'src' => $source,
683 'headers' => [ 'Content-Disposition' => '' ] ] ); // remove
684 $this->assertGoodStatus( $status,
685 "Removal of header for $source succeeded ($backendName)." );
686
687 if ( $this->backend->hasFeatures( FileBackend::ATTR_HEADERS ) ) {
688 $attr = $this->backend->getFileXAttributes( [ 'src' => $source ] );
689 $this->assertFalse( isset( $attr['headers']['content-disposition'] ),
690 "File 'Content-Disposition' header removed." );
691 }
692 }
693
694 $status = $this->backend->doOperation( $op );
695 if ( $okStatus ) {
696 $this->assertGoodStatus( $status,
697 "Describe of file at $source succeeded without warnings ($backendName)." );
698 $this->assertEquals( true, $status->isOK(),
699 "Describe of file at $source succeeded ($backendName)." );
700 $this->assertEquals( [ 0 => true ], $status->success,
701 "Describe of file at $source has proper 'success' field in Status ($backendName)." );
702 if ( $this->backend->hasFeatures( FileBackend::ATTR_HEADERS ) ) {
703 $attr = $this->backend->getFileXAttributes( [ 'src' => $source ] );
704 $this->assertHasHeaders( $op['headers'], $attr );
705 }
706 } else {
707 $this->assertEquals( false, $status->isOK(),
708 "Describe of file at $source failed ($backendName)." );
709 }
710
711 $this->assertBackendPathsConsistent( [ $source ] );
712 }
713
714 private function assertHasHeaders( array $headers, array $attr ) {
715 foreach ( $headers as $n => $v ) {
716 if ( $n !== '' ) {
717 $this->assertTrue( isset( $attr['headers'][strtolower( $n )] ),
718 "File has '$n' header." );
719 $this->assertEquals( $v, $attr['headers'][strtolower( $n )],
720 "File has '$n' header value." );
721 } else {
722 $this->assertFalse( isset( $attr['headers'][strtolower( $n )] ),
723 "File does not have '$n' header." );
724 }
725 }
726 }
727
728 public static function provider_testDescribe() {
729 $cases = [];
730
731 $source = self::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
732
733 $op = [ 'op' => 'describe', 'src' => $source,
734 'headers' => [ 'Content-Disposition' => 'inline' ], ];
735 $cases[] = [
736 $op, // operation
737 true, // with source
738 true // succeeds
739 ];
740
741 $cases[] = [
742 $op, // operation
743 false, // without source
744 false // fails
745 ];
746
747 return $cases;
748 }
749
750 /**
751 * @dataProvider provider_testCreate
752 */
753 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
754 $this->backend = $this->singleBackend;
755 $this->tearDownFiles();
756 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
757 $this->tearDownFiles();
758
759 $this->backend = $this->multiBackend;
760 $this->tearDownFiles();
761 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
762 $this->tearDownFiles();
763 }
764
765 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
766 $backendName = $this->backendClass();
767
768 $dest = $op['dst'];
769 $this->prepare( [ 'dir' => dirname( $dest ) ] );
770
771 $oldText = 'blah...blah...waahwaah';
772 if ( $alreadyExists ) {
773 $status = $this->backend->doOperation(
774 [ 'op' => 'create', 'content' => $oldText, 'dst' => $dest ] );
775 $this->assertGoodStatus( $status,
776 "Creation of file at $dest succeeded ($backendName)." );
777 }
778
779 $status = $this->backend->doOperation( $op );
780 if ( $okStatus ) {
781 $this->assertGoodStatus( $status,
782 "Creation of file at $dest succeeded without warnings ($backendName)." );
783 $this->assertEquals( true, $status->isOK(),
784 "Creation of file at $dest succeeded ($backendName)." );
785 $this->assertEquals( [ 0 => true ], $status->success,
786 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
787 } else {
788 $this->assertEquals( false, $status->isOK(),
789 "Creation of file at $dest failed ($backendName)." );
790 }
791
792 $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $dest ] ),
793 "Destination file $dest exists after creation ($backendName)." );
794
795 $props1 = $this->backend->getFileProps( [ 'src' => $dest ] );
796 $this->assertEquals( true, $props1['fileExists'],
797 "Destination file $dest exists according to props ($backendName)." );
798 if ( $okStatus ) { // file content is what we saved
799 $this->assertEquals( $newSize, $props1['size'],
800 "Destination file $dest has expected size according to props ($backendName)." );
801 $this->assertEquals( $newSize,
802 $this->backend->getFileSize( [ 'src' => $dest ] ),
803 "Destination file $dest has correct size ($backendName)." );
804 } else { // file content is some other previous text
805 $this->assertEquals( strlen( $oldText ), $props1['size'],
806 "Destination file $dest has original size according to props ($backendName)." );
807 $this->assertEquals( strlen( $oldText ),
808 $this->backend->getFileSize( [ 'src' => $dest ] ),
809 "Destination file $dest has original size according to props ($backendName)." );
810 }
811
812 $this->assertBackendPathsConsistent( [ $dest ] );
813 }
814
815 /**
816 * @dataProvider provider_testCreate
817 */
818 public static function provider_testCreate() {
819 $cases = [];
820
821 $dest = self::baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
822
823 $op = [ 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest ];
824 $cases[] = [
825 $op, // operation
826 false, // no dest already exists
827 true, // succeeds
828 strlen( $op['content'] )
829 ];
830
831 $op2 = $op;
832 $op2['content'] = "\n";
833 $cases[] = [
834 $op2, // operation
835 false, // no dest already exists
836 true, // succeeds
837 strlen( $op2['content'] )
838 ];
839
840 $op2 = $op;
841 $op2['content'] = "fsf\n waf 3kt";
842 $cases[] = [
843 $op2, // operation
844 true, // dest already exists
845 false, // fails
846 strlen( $op2['content'] )
847 ];
848
849 $op2 = $op;
850 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
851 $op2['overwrite'] = true;
852 $cases[] = [
853 $op2, // operation
854 true, // dest already exists
855 true, // succeeds
856 strlen( $op2['content'] )
857 ];
858
859 $op2 = $op;
860 $op2['content'] = "39qjmg3-qg";
861 $op2['overwriteSame'] = true;
862 $cases[] = [
863 $op2, // operation
864 true, // dest already exists
865 false, // succeeds
866 strlen( $op2['content'] )
867 ];
868
869 return $cases;
870 }
871
872 public function testDoQuickOperations() {
873 $this->backend = $this->singleBackend;
874 $this->doTestDoQuickOperations();
875 $this->tearDownFiles();
876
877 $this->backend = $this->multiBackend;
878 $this->doTestDoQuickOperations();
879 $this->tearDownFiles();
880 }
881
882 private function doTestDoQuickOperations() {
883 $backendName = $this->backendClass();
884
885 $base = self::baseStorePath();
886 $files = [
887 "$base/unittest-cont1/e/fileA.a",
888 "$base/unittest-cont1/e/fileB.a",
889 "$base/unittest-cont1/e/fileC.a"
890 ];
891 $createOps = [];
892 $purgeOps = [];
893 foreach ( $files as $path ) {
894 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
895 $this->assertGoodStatus( $status,
896 "Preparing $path succeeded without warnings ($backendName)." );
897 $createOps[] = [ 'op' => 'create', 'dst' => $path, 'content' => mt_rand( 0, 50000 ) ];
898 $copyOps[] = [ 'op' => 'copy', 'src' => $path, 'dst' => "$path-2" ];
899 $moveOps[] = [ 'op' => 'move', 'src' => "$path-2", 'dst' => "$path-3" ];
900 $purgeOps[] = [ 'op' => 'delete', 'src' => $path ];
901 $purgeOps[] = [ 'op' => 'delete', 'src' => "$path-3" ];
902 }
903 $purgeOps[] = [ 'op' => 'null' ];
904
905 $this->assertGoodStatus(
906 $this->backend->doQuickOperations( $createOps ),
907 "Creation of source files succeeded ($backendName)." );
908 foreach ( $files as $file ) {
909 $this->assertTrue( $this->backend->fileExists( [ 'src' => $file ] ),
910 "File $file exists." );
911 }
912
913 $this->assertGoodStatus(
914 $this->backend->doQuickOperations( $copyOps ),
915 "Quick copy of source files succeeded ($backendName)." );
916 foreach ( $files as $file ) {
917 $this->assertTrue( $this->backend->fileExists( [ 'src' => "$file-2" ] ),
918 "File $file-2 exists." );
919 }
920
921 $this->assertGoodStatus(
922 $this->backend->doQuickOperations( $moveOps ),
923 "Quick move of source files succeeded ($backendName)." );
924 foreach ( $files as $file ) {
925 $this->assertTrue( $this->backend->fileExists( [ 'src' => "$file-3" ] ),
926 "File $file-3 move in." );
927 $this->assertFalse( $this->backend->fileExists( [ 'src' => "$file-2" ] ),
928 "File $file-2 moved away." );
929 }
930
931 $this->assertGoodStatus(
932 $this->backend->quickCopy( [ 'src' => $files[0], 'dst' => $files[0] ] ),
933 "Copy of file {$files[0]} over itself succeeded ($backendName)." );
934 $this->assertTrue( $this->backend->fileExists( [ 'src' => $files[0] ] ),
935 "File {$files[0]} still exists." );
936
937 $this->assertGoodStatus(
938 $this->backend->quickMove( [ 'src' => $files[0], 'dst' => $files[0] ] ),
939 "Move of file {$files[0]} over itself succeeded ($backendName)." );
940 $this->assertTrue( $this->backend->fileExists( [ 'src' => $files[0] ] ),
941 "File {$files[0]} still exists." );
942
943 $this->assertGoodStatus(
944 $this->backend->doQuickOperations( $purgeOps ),
945 "Quick deletion of source files succeeded ($backendName)." );
946 foreach ( $files as $file ) {
947 $this->assertFalse( $this->backend->fileExists( [ 'src' => $file ] ),
948 "File $file purged." );
949 $this->assertFalse( $this->backend->fileExists( [ 'src' => "$file-3" ] ),
950 "File $file-3 purged." );
951 }
952 }
953
954 /**
955 * @dataProvider provider_testConcatenate
956 */
957 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
958 $this->backend = $this->singleBackend;
959 $this->tearDownFiles();
960 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
961 $this->tearDownFiles();
962
963 $this->backend = $this->multiBackend;
964 $this->tearDownFiles();
965 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
966 $this->tearDownFiles();
967 }
968
969 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
970 $backendName = $this->backendClass();
971
972 $expContent = '';
973 // Create sources
974 $ops = [];
975 foreach ( $srcs as $i => $source ) {
976 $this->prepare( [ 'dir' => dirname( $source ) ] );
977 $ops[] = [
978 'op' => 'create', // operation
979 'dst' => $source, // source
980 'content' => $srcsContent[$i]
981 ];
982 $expContent .= $srcsContent[$i];
983 }
984 $status = $this->backend->doOperations( $ops );
985
986 $this->assertGoodStatus( $status,
987 "Creation of source files succeeded ($backendName)." );
988
989 $dest = $params['dst'] = $this->getNewTempFile();
990 if ( $alreadyExists ) {
991 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
992 $this->assertEquals( true, $ok,
993 "Creation of file at $dest succeeded ($backendName)." );
994 } else {
995 $ok = file_put_contents( $dest, '' ) !== false;
996 $this->assertEquals( true, $ok,
997 "Creation of 0-byte file at $dest succeeded ($backendName)." );
998 }
999
1000 // Combine the files into one
1001 $status = $this->backend->concatenate( $params );
1002 if ( $okStatus ) {
1003 $this->assertGoodStatus( $status,
1004 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
1005 $this->assertEquals( true, $status->isOK(),
1006 "Creation of concat file at $dest succeeded ($backendName)." );
1007 } else {
1008 $this->assertEquals( false, $status->isOK(),
1009 "Creation of concat file at $dest failed ($backendName)." );
1010 }
1011
1012 if ( $okStatus ) {
1013 $this->assertEquals( true, is_file( $dest ),
1014 "Dest concat file $dest exists after creation ($backendName)." );
1015 } else {
1016 $this->assertEquals( true, is_file( $dest ),
1017 "Dest concat file $dest exists after failed creation ($backendName)." );
1018 }
1019
1020 $contents = file_get_contents( $dest );
1021 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
1022
1023 if ( $okStatus ) {
1024 $this->assertEquals( $expContent, $contents,
1025 "Concat file at $dest has correct contents ($backendName)." );
1026 } else {
1027 $this->assertNotEquals( $expContent, $contents,
1028 "Concat file at $dest has correct contents ($backendName)." );
1029 }
1030 }
1031
1032 public static function provider_testConcatenate() {
1033 $cases = [];
1034
1035 $srcs = [
1036 self::baseStorePath() . '/unittest-cont1/e/file1.txt',
1037 self::baseStorePath() . '/unittest-cont1/e/file2.txt',
1038 self::baseStorePath() . '/unittest-cont1/e/file3.txt',
1039 self::baseStorePath() . '/unittest-cont1/e/file4.txt',
1040 self::baseStorePath() . '/unittest-cont1/e/file5.txt',
1041 self::baseStorePath() . '/unittest-cont1/e/file6.txt',
1042 self::baseStorePath() . '/unittest-cont1/e/file7.txt',
1043 self::baseStorePath() . '/unittest-cont1/e/file8.txt',
1044 self::baseStorePath() . '/unittest-cont1/e/file9.txt',
1045 self::baseStorePath() . '/unittest-cont1/e/file10.txt'
1046 ];
1047 $content = [
1048 'egfage',
1049 'ageageag',
1050 'rhokohlr',
1051 'shgmslkg',
1052 'kenga',
1053 'owagmal',
1054 'kgmae',
1055 'g eak;g',
1056 'lkaem;a',
1057 'legma'
1058 ];
1059 $params = [ 'srcs' => $srcs ];
1060
1061 $cases[] = [
1062 $params, // operation
1063 $srcs, // sources
1064 $content, // content for each source
1065 false, // no dest already exists
1066 true, // succeeds
1067 ];
1068
1069 $cases[] = [
1070 $params, // operation
1071 $srcs, // sources
1072 $content, // content for each source
1073 true, // dest already exists
1074 false, // succeeds
1075 ];
1076
1077 return $cases;
1078 }
1079
1080 /**
1081 * @dataProvider provider_testGetFileStat
1082 */
1083 public function testGetFileStat( $path, $content, $alreadyExists ) {
1084 $this->backend = $this->singleBackend;
1085 $this->tearDownFiles();
1086 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1087 $this->tearDownFiles();
1088
1089 $this->backend = $this->multiBackend;
1090 $this->tearDownFiles();
1091 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1092 $this->tearDownFiles();
1093 }
1094
1095 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
1096 $backendName = $this->backendClass();
1097
1098 if ( $alreadyExists ) {
1099 $this->prepare( [ 'dir' => dirname( $path ) ] );
1100 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1101 $this->assertGoodStatus( $status,
1102 "Creation of file at $path succeeded ($backendName)." );
1103
1104 $size = $this->backend->getFileSize( [ 'src' => $path ] );
1105 $time = $this->backend->getFileTimestamp( [ 'src' => $path ] );
1106 $stat = $this->backend->getFileStat( [ 'src' => $path ] );
1107
1108 $this->assertEquals( strlen( $content ), $size,
1109 "Correct file size of '$path'" );
1110 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
1111 "Correct file timestamp of '$path'" );
1112
1113 $size = $stat['size'];
1114 $time = $stat['mtime'];
1115 $this->assertEquals( strlen( $content ), $size,
1116 "Correct file size of '$path'" );
1117 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
1118 "Correct file timestamp of '$path'" );
1119
1120 $this->backend->clearCache( [ $path ] );
1121
1122 $size = $this->backend->getFileSize( [ 'src' => $path ] );
1123
1124 $this->assertEquals( strlen( $content ), $size,
1125 "Correct file size of '$path'" );
1126
1127 $this->backend->preloadCache( [ $path ] );
1128
1129 $size = $this->backend->getFileSize( [ 'src' => $path ] );
1130
1131 $this->assertEquals( strlen( $content ), $size,
1132 "Correct file size of '$path'" );
1133 } else {
1134 $size = $this->backend->getFileSize( [ 'src' => $path ] );
1135 $time = $this->backend->getFileTimestamp( [ 'src' => $path ] );
1136 $stat = $this->backend->getFileStat( [ 'src' => $path ] );
1137
1138 $this->assertFalse( $size, "Correct file size of '$path'" );
1139 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1140 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1141 }
1142 }
1143
1144 public static function provider_testGetFileStat() {
1145 $cases = [];
1146
1147 $base = self::baseStorePath();
1148 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true ];
1149 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "", true ];
1150 $cases[] = [ "$base/unittest-cont1/e/b/some-diff_file.txt", null, false ];
1151
1152 return $cases;
1153 }
1154
1155 /**
1156 * @dataProvider provider_testGetFileStat
1157 */
1158 public function testStreamFile( $path, $content, $alreadyExists ) {
1159 $this->backend = $this->singleBackend;
1160 $this->tearDownFiles();
1161 $this->doTestStreamFile( $path, $content, $alreadyExists );
1162 $this->tearDownFiles();
1163
1164 $this->backend = $this->multiBackend;
1165 $this->tearDownFiles();
1166 $this->doTestStreamFile( $path, $content, $alreadyExists );
1167 $this->tearDownFiles();
1168 }
1169
1170 private function doTestStreamFile( $path, $content ) {
1171 $backendName = $this->backendClass();
1172
1173 if ( $content !== null ) {
1174 $this->prepare( [ 'dir' => dirname( $path ) ] );
1175 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1176 $this->assertGoodStatus( $status,
1177 "Creation of file at $path succeeded ($backendName)." );
1178
1179 ob_start();
1180 $this->backend->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
1181 $data = ob_get_contents();
1182 ob_end_clean();
1183
1184 $this->assertEquals( $content, $data, "Correct content streamed from '$path'" );
1185 } else { // 404 case
1186 ob_start();
1187 $this->backend->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
1188 $data = ob_get_contents();
1189 ob_end_clean();
1190
1191 $this->assertRegExp( '#<h1>File not found</h1>#', $data,
1192 "Correct content streamed from '$path' ($backendName)" );
1193 }
1194 }
1195
1196 public static function provider_testStreamFile() {
1197 $cases = [];
1198
1199 $base = self::baseStorePath();
1200 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
1201 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", null ];
1202
1203 return $cases;
1204 }
1205
1206 public function testStreamFileRange() {
1207 $this->backend = $this->singleBackend;
1208 $this->tearDownFiles();
1209 $this->doTestStreamFileRange();
1210 $this->tearDownFiles();
1211
1212 $this->backend = $this->multiBackend;
1213 $this->tearDownFiles();
1214 $this->doTestStreamFileRange();
1215 $this->tearDownFiles();
1216 }
1217
1218 private function doTestStreamFileRange() {
1219 $backendName = $this->backendClass();
1220
1221 $base = self::baseStorePath();
1222 $path = "$base/unittest-cont1/e/b/z/range_file.txt";
1223 $content = "0123456789ABCDEF";
1224
1225 $this->prepare( [ 'dir' => dirname( $path ) ] );
1226 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1227 $this->assertGoodStatus( $status,
1228 "Creation of file at $path succeeded ($backendName)." );
1229
1230 static $ranges = [
1231 'bytes=0-0' => '0',
1232 'bytes=0-3' => '0123',
1233 'bytes=4-8' => '45678',
1234 'bytes=15-15' => 'F',
1235 'bytes=14-15' => 'EF',
1236 'bytes=-5' => 'BCDEF',
1237 'bytes=-1' => 'F',
1238 'bytes=10-16' => 'ABCDEF',
1239 'bytes=10-99' => 'ABCDEF',
1240 ];
1241
1242 foreach ( $ranges as $range => $chunk ) {
1243 ob_start();
1244 $this->backend->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1,
1245 'options' => [ 'range' => $range ] ] );
1246 $data = ob_get_contents();
1247 ob_end_clean();
1248
1249 $this->assertEquals( $chunk, $data, "Correct chunk streamed from '$path' for '$range'" );
1250 }
1251 }
1252
1253 /**
1254 * @dataProvider provider_testGetFileContents
1255 */
1256 public function testGetFileContents( $source, $content ) {
1257 $this->backend = $this->singleBackend;
1258 $this->tearDownFiles();
1259 $this->doTestGetFileContents( $source, $content );
1260 $this->tearDownFiles();
1261
1262 $this->backend = $this->multiBackend;
1263 $this->tearDownFiles();
1264 $this->doTestGetFileContents( $source, $content );
1265 $this->tearDownFiles();
1266 }
1267
1268 private function doTestGetFileContents( $source, $content ) {
1269 $backendName = $this->backendClass();
1270
1271 $srcs = (array)$source;
1272 $content = (array)$content;
1273 foreach ( $srcs as $i => $src ) {
1274 $this->prepare( [ 'dir' => dirname( $src ) ] );
1275 $status = $this->backend->doOperation(
1276 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1277 $this->assertGoodStatus( $status,
1278 "Creation of file at $src succeeded ($backendName)." );
1279 }
1280
1281 if ( is_array( $source ) ) {
1282 $contents = $this->backend->getFileContentsMulti( [ 'srcs' => $source ] );
1283 foreach ( $contents as $path => $data ) {
1284 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1285 $this->assertEquals(
1286 current( $content ),
1287 $data,
1288 "Contents of $path is correct ($backendName)."
1289 );
1290 next( $content );
1291 }
1292 $this->assertEquals(
1293 $source,
1294 array_keys( $contents ),
1295 "Contents in right order ($backendName)."
1296 );
1297 $this->assertEquals(
1298 count( $source ),
1299 count( $contents ),
1300 "Contents array size correct ($backendName)."
1301 );
1302 } else {
1303 $data = $this->backend->getFileContents( [ 'src' => $source ] );
1304 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1305 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1306 }
1307 }
1308
1309 public static function provider_testGetFileContents() {
1310 $cases = [];
1311
1312 $base = self::baseStorePath();
1313 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
1314 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" ];
1315 $cases[] = [
1316 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1317 "$base/unittest-cont1/e/a/z.txt" ],
1318 [ "contents xx", "contents xy", "contents xz" ]
1319 ];
1320
1321 return $cases;
1322 }
1323
1324 /**
1325 * @dataProvider provider_testGetLocalCopy
1326 */
1327 public function testGetLocalCopy( $source, $content ) {
1328 $this->backend = $this->singleBackend;
1329 $this->tearDownFiles();
1330 $this->doTestGetLocalCopy( $source, $content );
1331 $this->tearDownFiles();
1332
1333 $this->backend = $this->multiBackend;
1334 $this->tearDownFiles();
1335 $this->doTestGetLocalCopy( $source, $content );
1336 $this->tearDownFiles();
1337 }
1338
1339 private function doTestGetLocalCopy( $source, $content ) {
1340 $backendName = $this->backendClass();
1341
1342 $srcs = (array)$source;
1343 $content = (array)$content;
1344 foreach ( $srcs as $i => $src ) {
1345 $this->prepare( [ 'dir' => dirname( $src ) ] );
1346 $status = $this->backend->doOperation(
1347 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1348 $this->assertGoodStatus( $status,
1349 "Creation of file at $src succeeded ($backendName)." );
1350 }
1351
1352 if ( is_array( $source ) ) {
1353 $tmpFiles = $this->backend->getLocalCopyMulti( [ 'srcs' => $source ] );
1354 foreach ( $tmpFiles as $path => $tmpFile ) {
1355 $this->assertNotNull( $tmpFile,
1356 "Creation of local copy of $path succeeded ($backendName)." );
1357 $contents = file_get_contents( $tmpFile->getPath() );
1358 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1359 $this->assertEquals(
1360 current( $content ),
1361 $contents,
1362 "Local copy of $path is correct ($backendName)."
1363 );
1364 next( $content );
1365 }
1366 $this->assertEquals(
1367 $source,
1368 array_keys( $tmpFiles ),
1369 "Local copies in right order ($backendName)."
1370 );
1371 $this->assertEquals(
1372 count( $source ),
1373 count( $tmpFiles ),
1374 "Local copies array size correct ($backendName)."
1375 );
1376 } else {
1377 $tmpFile = $this->backend->getLocalCopy( [ 'src' => $source ] );
1378 $this->assertNotNull( $tmpFile,
1379 "Creation of local copy of $source succeeded ($backendName)." );
1380 $contents = file_get_contents( $tmpFile->getPath() );
1381 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1382 $this->assertEquals(
1383 $content[0],
1384 $contents,
1385 "Local copy of $source is correct ($backendName)."
1386 );
1387 }
1388
1389 $obj = new stdClass();
1390 $tmpFile->bind( $obj );
1391 }
1392
1393 public static function provider_testGetLocalCopy() {
1394 $cases = [];
1395
1396 $base = self::baseStorePath();
1397 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1398 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1399 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1400 $cases[] = [
1401 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1402 "$base/unittest-cont1/e/a/z.txt" ],
1403 [ "contents xx $", "contents xy 111", "contents xz" ]
1404 ];
1405
1406 return $cases;
1407 }
1408
1409 /**
1410 * @dataProvider provider_testGetLocalReference
1411 */
1412 public function testGetLocalReference( $source, $content ) {
1413 $this->backend = $this->singleBackend;
1414 $this->tearDownFiles();
1415 $this->doTestGetLocalReference( $source, $content );
1416 $this->tearDownFiles();
1417
1418 $this->backend = $this->multiBackend;
1419 $this->tearDownFiles();
1420 $this->doTestGetLocalReference( $source, $content );
1421 $this->tearDownFiles();
1422 }
1423
1424 private function doTestGetLocalReference( $source, $content ) {
1425 $backendName = $this->backendClass();
1426
1427 $srcs = (array)$source;
1428 $content = (array)$content;
1429 foreach ( $srcs as $i => $src ) {
1430 $this->prepare( [ 'dir' => dirname( $src ) ] );
1431 $status = $this->backend->doOperation(
1432 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1433 $this->assertGoodStatus( $status,
1434 "Creation of file at $src succeeded ($backendName)." );
1435 }
1436
1437 if ( is_array( $source ) ) {
1438 $tmpFiles = $this->backend->getLocalReferenceMulti( [ 'srcs' => $source ] );
1439 foreach ( $tmpFiles as $path => $tmpFile ) {
1440 $this->assertNotNull( $tmpFile,
1441 "Creation of local copy of $path succeeded ($backendName)." );
1442 $contents = file_get_contents( $tmpFile->getPath() );
1443 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1444 $this->assertEquals(
1445 current( $content ),
1446 $contents,
1447 "Local ref of $path is correct ($backendName)."
1448 );
1449 next( $content );
1450 }
1451 $this->assertEquals(
1452 $source,
1453 array_keys( $tmpFiles ),
1454 "Local refs in right order ($backendName)."
1455 );
1456 $this->assertEquals(
1457 count( $source ),
1458 count( $tmpFiles ),
1459 "Local refs array size correct ($backendName)."
1460 );
1461 } else {
1462 $tmpFile = $this->backend->getLocalReference( [ 'src' => $source ] );
1463 $this->assertNotNull( $tmpFile,
1464 "Creation of local copy of $source succeeded ($backendName)." );
1465 $contents = file_get_contents( $tmpFile->getPath() );
1466 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1467 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1468 }
1469 }
1470
1471 public static function provider_testGetLocalReference() {
1472 $cases = [];
1473
1474 $base = self::baseStorePath();
1475 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1476 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1477 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1478 $cases[] = [
1479 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1480 "$base/unittest-cont1/e/a/z.txt" ],
1481 [ "contents xx 1111", "contents xy %", "contents xz $" ]
1482 ];
1483
1484 return $cases;
1485 }
1486
1487 public function testGetLocalCopyAndReference404() {
1488 $this->backend = $this->singleBackend;
1489 $this->tearDownFiles();
1490 $this->doTestGetLocalCopyAndReference404();
1491 $this->tearDownFiles();
1492
1493 $this->backend = $this->multiBackend;
1494 $this->tearDownFiles();
1495 $this->doTestGetLocalCopyAndReference404();
1496 $this->tearDownFiles();
1497 }
1498
1499 public function doTestGetLocalCopyAndReference404() {
1500 $backendName = $this->backendClass();
1501
1502 $base = self::baseStorePath();
1503
1504 $tmpFile = $this->backend->getLocalCopy( [
1505 'src' => "$base/unittest-cont1/not-there" ] );
1506 $this->assertEquals( null, $tmpFile, "Local copy of not existing file is null ($backendName)." );
1507
1508 $tmpFile = $this->backend->getLocalReference( [
1509 'src' => "$base/unittest-cont1/not-there" ] );
1510 $this->assertEquals( null, $tmpFile, "Local ref of not existing file is null ($backendName)." );
1511 }
1512
1513 /**
1514 * @dataProvider provider_testGetFileHttpUrl
1515 */
1516 public function testGetFileHttpUrl( $source, $content ) {
1517 $this->backend = $this->singleBackend;
1518 $this->tearDownFiles();
1519 $this->doTestGetFileHttpUrl( $source, $content );
1520 $this->tearDownFiles();
1521
1522 $this->backend = $this->multiBackend;
1523 $this->tearDownFiles();
1524 $this->doTestGetFileHttpUrl( $source, $content );
1525 $this->tearDownFiles();
1526 }
1527
1528 private function doTestGetFileHttpUrl( $source, $content ) {
1529 $backendName = $this->backendClass();
1530
1531 $this->prepare( [ 'dir' => dirname( $source ) ] );
1532 $status = $this->backend->doOperation(
1533 [ 'op' => 'create', 'content' => $content, 'dst' => $source ] );
1534 $this->assertGoodStatus( $status,
1535 "Creation of file at $source succeeded ($backendName)." );
1536
1537 $url = $this->backend->getFileHttpUrl( [ 'src' => $source ] );
1538
1539 if ( $url !== null ) { // supported
1540 $data = MediaWikiServices::getInstance()->getHttpRequestFactory()->
1541 get( $url, [], __METHOD__ );
1542 $this->assertEquals( $content, $data,
1543 "HTTP GET of URL has right contents ($backendName)." );
1544 }
1545 }
1546
1547 public static function provider_testGetFileHttpUrl() {
1548 $cases = [];
1549
1550 $base = self::baseStorePath();
1551 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1552 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1553 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1554
1555 return $cases;
1556 }
1557
1558 /**
1559 * @dataProvider provider_testPrepareAndClean
1560 */
1561 public function testPrepareAndClean( $path, $isOK ) {
1562 $this->backend = $this->singleBackend;
1563 $this->doTestPrepareAndClean( $path, $isOK );
1564 $this->tearDownFiles();
1565
1566 $this->backend = $this->multiBackend;
1567 $this->doTestPrepareAndClean( $path, $isOK );
1568 $this->tearDownFiles();
1569 }
1570
1571 public static function provider_testPrepareAndClean() {
1572 $base = self::baseStorePath();
1573
1574 return [
1575 [ "$base/unittest-cont1/e/a/z/some_file1.txt", true ],
1576 [ "$base/unittest-cont2/a/z/some_file2.txt", true ],
1577 # Specific to FS backend with no basePath field set
1578 # [ "$base/unittest-cont3/a/z/some_file3.txt", false ],
1579 ];
1580 }
1581
1582 private function doTestPrepareAndClean( $path, $isOK ) {
1583 $backendName = $this->backendClass();
1584
1585 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
1586 if ( $isOK ) {
1587 $this->assertGoodStatus( $status,
1588 "Preparing dir $path succeeded without warnings ($backendName)." );
1589 $this->assertEquals( true, $status->isOK(),
1590 "Preparing dir $path succeeded ($backendName)." );
1591 } else {
1592 $this->assertEquals( false, $status->isOK(),
1593 "Preparing dir $path failed ($backendName)." );
1594 }
1595
1596 $status = $this->backend->secure( [ 'dir' => dirname( $path ) ] );
1597 if ( $isOK ) {
1598 $this->assertGoodStatus( $status,
1599 "Securing dir $path succeeded without warnings ($backendName)." );
1600 $this->assertEquals( true, $status->isOK(),
1601 "Securing dir $path succeeded ($backendName)." );
1602 } else {
1603 $this->assertEquals( false, $status->isOK(),
1604 "Securing dir $path failed ($backendName)." );
1605 }
1606
1607 $status = $this->backend->publish( [ 'dir' => dirname( $path ) ] );
1608 if ( $isOK ) {
1609 $this->assertGoodStatus( $status,
1610 "Publishing dir $path succeeded without warnings ($backendName)." );
1611 $this->assertEquals( true, $status->isOK(),
1612 "Publishing dir $path succeeded ($backendName)." );
1613 } else {
1614 $this->assertEquals( false, $status->isOK(),
1615 "Publishing dir $path failed ($backendName)." );
1616 }
1617
1618 $status = $this->backend->clean( [ 'dir' => dirname( $path ) ] );
1619 if ( $isOK ) {
1620 $this->assertGoodStatus( $status,
1621 "Cleaning dir $path succeeded without warnings ($backendName)." );
1622 $this->assertEquals( true, $status->isOK(),
1623 "Cleaning dir $path succeeded ($backendName)." );
1624 } else {
1625 $this->assertEquals( false, $status->isOK(),
1626 "Cleaning dir $path failed ($backendName)." );
1627 }
1628 }
1629
1630 public function testRecursiveClean() {
1631 $this->backend = $this->singleBackend;
1632 $this->doTestRecursiveClean();
1633 $this->tearDownFiles();
1634
1635 $this->backend = $this->multiBackend;
1636 $this->doTestRecursiveClean();
1637 $this->tearDownFiles();
1638 }
1639
1640 private function doTestRecursiveClean() {
1641 $backendName = $this->backendClass();
1642
1643 $base = self::baseStorePath();
1644 $dirs = [
1645 "$base/unittest-cont1",
1646 "$base/unittest-cont1/e",
1647 "$base/unittest-cont1/e/a",
1648 "$base/unittest-cont1/e/a/b",
1649 "$base/unittest-cont1/e/a/b/c",
1650 "$base/unittest-cont1/e/a/b/c/d0",
1651 "$base/unittest-cont1/e/a/b/c/d1",
1652 "$base/unittest-cont1/e/a/b/c/d2",
1653 "$base/unittest-cont1/e/a/b/c/d0/1",
1654 "$base/unittest-cont1/e/a/b/c/d0/2",
1655 "$base/unittest-cont1/e/a/b/c/d1/3",
1656 "$base/unittest-cont1/e/a/b/c/d1/4",
1657 "$base/unittest-cont1/e/a/b/c/d2/5",
1658 "$base/unittest-cont1/e/a/b/c/d2/6"
1659 ];
1660 foreach ( $dirs as $dir ) {
1661 $status = $this->prepare( [ 'dir' => $dir ] );
1662 $this->assertGoodStatus( $status,
1663 "Preparing dir $dir succeeded without warnings ($backendName)." );
1664 }
1665
1666 if ( $this->backend instanceof FSFileBackend ) {
1667 foreach ( $dirs as $dir ) {
1668 $this->assertEquals( true, $this->backend->directoryExists( [ 'dir' => $dir ] ),
1669 "Dir $dir exists ($backendName)." );
1670 }
1671 }
1672
1673 $status = $this->backend->clean(
1674 [ 'dir' => "$base/unittest-cont1", 'recursive' => 1 ] );
1675 $this->assertGoodStatus( $status,
1676 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1677
1678 foreach ( $dirs as $dir ) {
1679 $this->assertEquals( false, $this->backend->directoryExists( [ 'dir' => $dir ] ),
1680 "Dir $dir no longer exists ($backendName)." );
1681 }
1682 }
1683
1684 public function testDoOperations() {
1685 $this->backend = $this->singleBackend;
1686 $this->tearDownFiles();
1687 $this->doTestDoOperations();
1688 $this->tearDownFiles();
1689
1690 $this->backend = $this->multiBackend;
1691 $this->tearDownFiles();
1692 $this->doTestDoOperations();
1693 $this->tearDownFiles();
1694 }
1695
1696 private function doTestDoOperations() {
1697 $base = self::baseStorePath();
1698
1699 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1700 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1701 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1702 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1703 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1704 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1705 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1706
1707 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1708 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1709 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1710 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
1711 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1712 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
1713 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1714
1715 $status = $this->backend->doOperations( [
1716 [ 'op' => 'describe', 'src' => $fileA,
1717 'headers' => [ 'X-Content-Length' => '91.3' ], 'disposition' => 'inline' ],
1718 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1719 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1720 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1721 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1722 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1723 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1724 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1725 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1726 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1727 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1728 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1729 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1730 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1731 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1732 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1733 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1734 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1735 // Does nothing
1736 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1737 // Does nothing
1738 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1739 // Does nothing
1740 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1741 // Does nothing
1742 [ 'op' => 'null' ],
1743 // Does nothing
1744 ] );
1745
1746 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1747 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1748 $this->assertEquals( 14, count( $status->success ),
1749 "Operation batch has correct success array" );
1750
1751 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileA ] ),
1752 "File does not exist at $fileA" );
1753 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileB ] ),
1754 "File does not exist at $fileB" );
1755 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileD ] ),
1756 "File does not exist at $fileD" );
1757
1758 $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $fileC ] ),
1759 "File exists at $fileC" );
1760 $this->assertEquals( $fileBContents,
1761 $this->backend->getFileContents( [ 'src' => $fileC ] ),
1762 "Correct file contents of $fileC" );
1763 $this->assertEquals( strlen( $fileBContents ),
1764 $this->backend->getFileSize( [ 'src' => $fileC ] ),
1765 "Correct file size of $fileC" );
1766 $this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ),
1767 $this->backend->getFileSha1Base36( [ 'src' => $fileC ] ),
1768 "Correct file SHA-1 of $fileC" );
1769 }
1770
1771 public function testDoOperationsPipeline() {
1772 $this->backend = $this->singleBackend;
1773 $this->tearDownFiles();
1774 $this->doTestDoOperationsPipeline();
1775 $this->tearDownFiles();
1776
1777 $this->backend = $this->multiBackend;
1778 $this->tearDownFiles();
1779 $this->doTestDoOperationsPipeline();
1780 $this->tearDownFiles();
1781 }
1782
1783 // concurrency orientated
1784 private function doTestDoOperationsPipeline() {
1785 $base = self::baseStorePath();
1786
1787 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1788 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1789 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1790
1791 $tmpNameA = TempFSFile::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1792 $tmpNameB = TempFSFile::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1793 $tmpNameC = TempFSFile::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1794 $this->addTmpFiles( [ $tmpNameA, $tmpNameB, $tmpNameC ] );
1795 file_put_contents( $tmpNameA, $fileAContents );
1796 file_put_contents( $tmpNameB, $fileBContents );
1797 file_put_contents( $tmpNameC, $fileCContents );
1798
1799 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1800 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1801 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1802 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1803
1804 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1805 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1806 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1807 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1808 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1809
1810 $status = $this->backend->doOperations( [
1811 [ 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ],
1812 [ 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ],
1813 [ 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ],
1814 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1815 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1816 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1817 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1818 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1819 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1820 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1821 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1822 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1823 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1824 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1825 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1826 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1827 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1828 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1829 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1830 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1831 // Does nothing
1832 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1833 // Does nothing
1834 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1835 // Does nothing
1836 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1837 // Does nothing
1838 [ 'op' => 'null' ],
1839 // Does nothing
1840 ] );
1841
1842 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1843 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1844 $this->assertEquals( 16, count( $status->success ),
1845 "Operation batch has correct success array" );
1846
1847 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileA ] ),
1848 "File does not exist at $fileA" );
1849 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileB ] ),
1850 "File does not exist at $fileB" );
1851 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileD ] ),
1852 "File does not exist at $fileD" );
1853
1854 $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $fileC ] ),
1855 "File exists at $fileC" );
1856 $this->assertEquals( $fileBContents,
1857 $this->backend->getFileContents( [ 'src' => $fileC ] ),
1858 "Correct file contents of $fileC" );
1859 $this->assertEquals( strlen( $fileBContents ),
1860 $this->backend->getFileSize( [ 'src' => $fileC ] ),
1861 "Correct file size of $fileC" );
1862 $this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ),
1863 $this->backend->getFileSha1Base36( [ 'src' => $fileC ] ),
1864 "Correct file SHA-1 of $fileC" );
1865 }
1866
1867 public function testDoOperationsFailing() {
1868 $this->backend = $this->singleBackend;
1869 $this->tearDownFiles();
1870 $this->doTestDoOperationsFailing();
1871 $this->tearDownFiles();
1872
1873 $this->backend = $this->multiBackend;
1874 $this->tearDownFiles();
1875 $this->doTestDoOperationsFailing();
1876 $this->tearDownFiles();
1877 }
1878
1879 private function doTestDoOperationsFailing() {
1880 $base = self::baseStorePath();
1881
1882 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1883 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1884 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1885 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1886 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1887 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1888 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1889
1890 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1891 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1892 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1893 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
1894 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1895 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
1896
1897 $status = $this->backend->doOperations( [
1898 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1899 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1900 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1901 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1902 [ 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ],
1903 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1904 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ],
1905 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1906 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ],
1907 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1908 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ],
1909 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1910 [ 'op' => 'delete', 'src' => $fileD ],
1911 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1912 [ 'op' => 'null' ],
1913 // Does nothing
1914 ], [ 'force' => 1 ] );
1915
1916 $this->assertNotEquals( [], $status->getErrors(), "Operation had warnings" );
1917 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1918 $this->assertEquals( 8, count( $status->success ),
1919 "Operation batch has correct success array" );
1920
1921 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileB ] ),
1922 "File does not exist at $fileB" );
1923 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileD ] ),
1924 "File does not exist at $fileD" );
1925
1926 $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $fileA ] ),
1927 "File does not exist at $fileA" );
1928 $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $fileC ] ),
1929 "File exists at $fileC" );
1930 $this->assertEquals( $fileBContents,
1931 $this->backend->getFileContents( [ 'src' => $fileA ] ),
1932 "Correct file contents of $fileA" );
1933 $this->assertEquals( strlen( $fileBContents ),
1934 $this->backend->getFileSize( [ 'src' => $fileA ] ),
1935 "Correct file size of $fileA" );
1936 $this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ),
1937 $this->backend->getFileSha1Base36( [ 'src' => $fileA ] ),
1938 "Correct file SHA-1 of $fileA" );
1939 }
1940
1941 public function testGetFileList() {
1942 $this->backend = $this->singleBackend;
1943 $this->tearDownFiles();
1944 $this->doTestGetFileList();
1945 $this->tearDownFiles();
1946
1947 $this->backend = $this->multiBackend;
1948 $this->tearDownFiles();
1949 $this->doTestGetFileList();
1950 $this->tearDownFiles();
1951 }
1952
1953 private function doTestGetFileList() {
1954 $backendName = $this->backendClass();
1955 $base = self::baseStorePath();
1956
1957 // Should have no errors
1958 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont-notexists" ] );
1959
1960 $files = [
1961 "$base/unittest-cont1/e/test1.txt",
1962 "$base/unittest-cont1/e/test2.txt",
1963 "$base/unittest-cont1/e/test3.txt",
1964 "$base/unittest-cont1/e/subdir1/test1.txt",
1965 "$base/unittest-cont1/e/subdir1/test2.txt",
1966 "$base/unittest-cont1/e/subdir2/test3.txt",
1967 "$base/unittest-cont1/e/subdir2/test4.txt",
1968 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1969 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1970 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1971 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1972 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1973 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1974 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1975 ];
1976
1977 // Add the files
1978 $ops = [];
1979 foreach ( $files as $file ) {
1980 $this->prepare( [ 'dir' => dirname( $file ) ] );
1981 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
1982 }
1983 $status = $this->backend->doQuickOperations( $ops );
1984 $this->assertGoodStatus( $status,
1985 "Creation of files succeeded ($backendName)." );
1986 $this->assertEquals( true, $status->isOK(),
1987 "Creation of files succeeded with OK status ($backendName)." );
1988
1989 // Expected listing at root
1990 $expected = [
1991 "e/test1.txt",
1992 "e/test2.txt",
1993 "e/test3.txt",
1994 "e/subdir1/test1.txt",
1995 "e/subdir1/test2.txt",
1996 "e/subdir2/test3.txt",
1997 "e/subdir2/test4.txt",
1998 "e/subdir2/subdir/test1.txt",
1999 "e/subdir2/subdir/test2.txt",
2000 "e/subdir2/subdir/test3.txt",
2001 "e/subdir2/subdir/test4.txt",
2002 "e/subdir2/subdir/test5.txt",
2003 "e/subdir2/subdir/sub/test0.txt",
2004 "e/subdir2/subdir/sub/120-px-file.txt",
2005 ];
2006 sort( $expected );
2007
2008 // Actual listing (no trailing slash) at root
2009 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1" ] );
2010 $list = $this->listToArray( $iter );
2011 sort( $list );
2012 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2013
2014 // Actual listing (no trailing slash) at root with advise
2015 $iter = $this->backend->getFileList( [
2016 'dir' => "$base/unittest-cont1",
2017 'adviseStat' => 1
2018 ] );
2019 $list = $this->listToArray( $iter );
2020 sort( $list );
2021 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2022
2023 // Actual listing (with trailing slash) at root
2024 $list = [];
2025 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/" ] );
2026 foreach ( $iter as $file ) {
2027 $list[] = $file;
2028 }
2029 sort( $list );
2030 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2031
2032 // Expected listing at subdir
2033 $expected = [
2034 "test1.txt",
2035 "test2.txt",
2036 "test3.txt",
2037 "test4.txt",
2038 "test5.txt",
2039 "sub/test0.txt",
2040 "sub/120-px-file.txt",
2041 ];
2042 sort( $expected );
2043
2044 // Actual listing (no trailing slash) at subdir
2045 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] );
2046 $list = $this->listToArray( $iter );
2047 sort( $list );
2048 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2049
2050 // Actual listing (no trailing slash) at subdir with advise
2051 $iter = $this->backend->getFileList( [
2052 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2053 'adviseStat' => 1
2054 ] );
2055 $list = $this->listToArray( $iter );
2056 sort( $list );
2057 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2058
2059 // Actual listing (with trailing slash) at subdir
2060 $list = [];
2061 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ] );
2062 foreach ( $iter as $file ) {
2063 $list[] = $file;
2064 }
2065 sort( $list );
2066 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2067
2068 // Actual listing (using iterator second time)
2069 $list = $this->listToArray( $iter );
2070 sort( $list );
2071 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
2072
2073 // Actual listing (top files only) at root
2074 $iter = $this->backend->getTopFileList( [ 'dir' => "$base/unittest-cont1" ] );
2075 $list = $this->listToArray( $iter );
2076 sort( $list );
2077 $this->assertEquals( [], $list, "Correct top file listing ($backendName)." );
2078
2079 // Expected listing (top files only) at subdir
2080 $expected = [
2081 "test1.txt",
2082 "test2.txt",
2083 "test3.txt",
2084 "test4.txt",
2085 "test5.txt"
2086 ];
2087 sort( $expected );
2088
2089 // Actual listing (top files only) at subdir
2090 $iter = $this->backend->getTopFileList(
2091 [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ]
2092 );
2093 $list = $this->listToArray( $iter );
2094 sort( $list );
2095 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2096
2097 // Actual listing (top files only) at subdir with advise
2098 $iter = $this->backend->getTopFileList( [
2099 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2100 'adviseStat' => 1
2101 ] );
2102 $list = $this->listToArray( $iter );
2103 sort( $list );
2104 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2105
2106 foreach ( $files as $file ) { // clean up
2107 $this->backend->doOperation( [ 'op' => 'delete', 'src' => $file ] );
2108 }
2109
2110 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
2111 foreach ( $iter as $iter ) {
2112 // no errors
2113 }
2114 }
2115
2116 public function testGetDirectoryList() {
2117 $this->backend = $this->singleBackend;
2118 $this->tearDownFiles();
2119 $this->doTestGetDirectoryList();
2120 $this->tearDownFiles();
2121
2122 $this->backend = $this->multiBackend;
2123 $this->tearDownFiles();
2124 $this->doTestGetDirectoryList();
2125 $this->tearDownFiles();
2126 }
2127
2128 private function doTestGetDirectoryList() {
2129 $backendName = $this->backendClass();
2130
2131 $base = self::baseStorePath();
2132 $files = [
2133 "$base/unittest-cont1/e/test1.txt",
2134 "$base/unittest-cont1/e/test2.txt",
2135 "$base/unittest-cont1/e/test3.txt",
2136 "$base/unittest-cont1/e/subdir1/test1.txt",
2137 "$base/unittest-cont1/e/subdir1/test2.txt",
2138 "$base/unittest-cont1/e/subdir2/test3.txt",
2139 "$base/unittest-cont1/e/subdir2/test4.txt",
2140 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
2141 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
2142 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
2143 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
2144 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
2145 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
2146 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
2147 ];
2148
2149 // Add the files
2150 $ops = [];
2151 foreach ( $files as $file ) {
2152 $this->prepare( [ 'dir' => dirname( $file ) ] );
2153 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
2154 }
2155 $status = $this->backend->doQuickOperations( $ops );
2156 $this->assertGoodStatus( $status,
2157 "Creation of files succeeded ($backendName)." );
2158 $this->assertEquals( true, $status->isOK(),
2159 "Creation of files succeeded with OK status ($backendName)." );
2160
2161 $this->assertEquals( true,
2162 $this->backend->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] ),
2163 "Directory exists in ($backendName)." );
2164 $this->assertEquals( true,
2165 $this->backend->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] ),
2166 "Directory exists in ($backendName)." );
2167 $this->assertEquals( false,
2168 $this->backend->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ] ),
2169 "Directory does not exists in ($backendName)." );
2170
2171 // Expected listing
2172 $expected = [
2173 "e",
2174 ];
2175 sort( $expected );
2176
2177 // Actual listing (no trailing slash)
2178 $list = [];
2179 $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1" ] );
2180 foreach ( $iter as $file ) {
2181 $list[] = $file;
2182 }
2183 sort( $list );
2184
2185 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2186
2187 // Expected listing
2188 $expected = [
2189 "subdir1",
2190 "subdir2",
2191 "subdir3",
2192 "subdir4",
2193 ];
2194 sort( $expected );
2195
2196 // Actual listing (no trailing slash)
2197 $list = [];
2198 $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e" ] );
2199 foreach ( $iter as $file ) {
2200 $list[] = $file;
2201 }
2202 sort( $list );
2203
2204 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2205
2206 // Actual listing (with trailing slash)
2207 $list = [];
2208 $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/" ] );
2209 foreach ( $iter as $file ) {
2210 $list[] = $file;
2211 }
2212 sort( $list );
2213
2214 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2215
2216 // Expected listing
2217 $expected = [
2218 "subdir",
2219 ];
2220 sort( $expected );
2221
2222 // Actual listing (no trailing slash)
2223 $list = [];
2224 $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir2" ] );
2225 foreach ( $iter as $file ) {
2226 $list[] = $file;
2227 }
2228 sort( $list );
2229
2230 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2231
2232 // Actual listing (with trailing slash)
2233 $list = [];
2234 $iter = $this->backend->getTopDirectoryList(
2235 [ 'dir' => "$base/unittest-cont1/e/subdir2/" ]
2236 );
2237
2238 foreach ( $iter as $file ) {
2239 $list[] = $file;
2240 }
2241 sort( $list );
2242
2243 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2244
2245 // Actual listing (using iterator second time)
2246 $list = [];
2247 foreach ( $iter as $file ) {
2248 $list[] = $file;
2249 }
2250 sort( $list );
2251
2252 $this->assertEquals(
2253 $expected,
2254 $list,
2255 "Correct top dir listing ($backendName), second iteration."
2256 );
2257
2258 // Expected listing (recursive)
2259 $expected = [
2260 "e",
2261 "e/subdir1",
2262 "e/subdir2",
2263 "e/subdir3",
2264 "e/subdir4",
2265 "e/subdir2/subdir",
2266 "e/subdir3/subdir",
2267 "e/subdir4/subdir",
2268 "e/subdir4/subdir/sub",
2269 ];
2270 sort( $expected );
2271
2272 // Actual listing (recursive)
2273 $list = [];
2274 $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/" ] );
2275 foreach ( $iter as $file ) {
2276 $list[] = $file;
2277 }
2278 sort( $list );
2279
2280 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2281
2282 // Expected listing (recursive)
2283 $expected = [
2284 "subdir",
2285 "subdir/sub",
2286 ];
2287 sort( $expected );
2288
2289 // Actual listing (recursive)
2290 $list = [];
2291 $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir4" ] );
2292 foreach ( $iter as $file ) {
2293 $list[] = $file;
2294 }
2295 sort( $list );
2296
2297 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2298
2299 // Actual listing (recursive, second time)
2300 $list = [];
2301 foreach ( $iter as $file ) {
2302 $list[] = $file;
2303 }
2304 sort( $list );
2305
2306 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2307
2308 $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] );
2309 $items = $this->listToArray( $iter );
2310 $this->assertEquals( [], $items, "Directory listing is empty." );
2311
2312 foreach ( $files as $file ) { // clean up
2313 $this->backend->doOperation( [ 'op' => 'delete', 'src' => $file ] );
2314 }
2315
2316 $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
2317 foreach ( $iter as $file ) {
2318 // no errors
2319 }
2320
2321 $items = $this->listToArray( $iter );
2322 $this->assertEquals( [], $items, "Directory listing is empty." );
2323
2324 $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/not/exists" ] );
2325 $items = $this->listToArray( $iter );
2326 $this->assertEquals( [], $items, "Directory listing is empty." );
2327 }
2328
2329 public function testLockCalls() {
2330 $this->backend = $this->singleBackend;
2331 $this->doTestLockCalls();
2332 }
2333
2334 private function doTestLockCalls() {
2335 $backendName = $this->backendClass();
2336 $base = $this->backend->getContainerStoragePath( 'test' );
2337
2338 $paths = [
2339 "$base/test1.txt",
2340 "$base/test2.txt",
2341 "$base/test3.txt",
2342 "$base/subdir1",
2343 "$base/subdir1", // duplicate
2344 "$base/subdir1/test1.txt",
2345 "$base/subdir1/test2.txt",
2346 "$base/subdir2",
2347 "$base/subdir2", // duplicate
2348 "$base/subdir2/test3.txt",
2349 "$base/subdir2/test4.txt",
2350 "$base/subdir2/subdir",
2351 "$base/subdir2/subdir/test1.txt",
2352 "$base/subdir2/subdir/test2.txt",
2353 "$base/subdir2/subdir/test3.txt",
2354 "$base/subdir2/subdir/test4.txt",
2355 "$base/subdir2/subdir/test5.txt",
2356 "$base/subdir2/subdir/sub",
2357 "$base/subdir2/subdir/sub/test0.txt",
2358 "$base/subdir2/subdir/sub/120-px-file.txt",
2359 ];
2360
2361 for ( $i = 0; $i < 25; $i++ ) {
2362 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
2363 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2364 "Locking of files succeeded ($backendName) ($i)." );
2365 $this->assertEquals( true, $status->isOK(),
2366 "Locking of files succeeded with OK status ($backendName) ($i)." );
2367
2368 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
2369 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2370 "Locking of files succeeded ($backendName) ($i)." );
2371 $this->assertEquals( true, $status->isOK(),
2372 "Locking of files succeeded with OK status ($backendName) ($i)." );
2373
2374 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
2375 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2376 "Locking of files succeeded ($backendName) ($i)." );
2377 $this->assertEquals( true, $status->isOK(),
2378 "Locking of files succeeded with OK status ($backendName) ($i)." );
2379
2380 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_EX );
2381 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2382 "Locking of files succeeded ($backendName). ($i)" );
2383 $this->assertEquals( true, $status->isOK(),
2384 "Locking of files succeeded with OK status ($backendName) ($i)." );
2385
2386 # # Flip the acquire/release ordering around ##
2387
2388 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
2389 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2390 "Locking of files succeeded ($backendName) ($i)." );
2391 $this->assertEquals( true, $status->isOK(),
2392 "Locking of files succeeded with OK status ($backendName) ($i)." );
2393
2394 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
2395 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2396 "Locking of files succeeded ($backendName) ($i)." );
2397 $this->assertEquals( true, $status->isOK(),
2398 "Locking of files succeeded with OK status ($backendName) ($i)." );
2399
2400 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_EX );
2401 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2402 "Locking of files succeeded ($backendName). ($i)" );
2403 $this->assertEquals( true, $status->isOK(),
2404 "Locking of files succeeded with OK status ($backendName) ($i)." );
2405
2406 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
2407 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2408 "Locking of files succeeded ($backendName) ($i)." );
2409 $this->assertEquals( true, $status->isOK(),
2410 "Locking of files succeeded with OK status ($backendName) ($i)." );
2411 }
2412
2413 $status = Status::newGood();
2414 $sl = $this->backend->getScopedFileLocks( $paths, LockManager::LOCK_EX, $status );
2415 $this->assertInstanceOf( ScopedLock::class, $sl,
2416 "Scoped locking of files succeeded ($backendName)." );
2417 $this->assertEquals( [], $status->getErrors(),
2418 "Scoped locking of files succeeded ($backendName)." );
2419 $this->assertEquals( true, $status->isOK(),
2420 "Scoped locking of files succeeded with OK status ($backendName)." );
2421
2422 ScopedLock::release( $sl );
2423 $this->assertEquals( null, $sl,
2424 "Scoped unlocking of files succeeded ($backendName)." );
2425 $this->assertEquals( [], $status->getErrors(),
2426 "Scoped unlocking of files succeeded ($backendName)." );
2427 $this->assertEquals( true, $status->isOK(),
2428 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2429 }
2430
2431 /**
2432 * @dataProvider provider_testGetContentType
2433 */
2434 public function testGetContentType( $mimeCallback, $mimeFromString ) {
2435 global $IP;
2436
2437 $be = TestingAccessWrapper::newFromObject( new MemoryFileBackend(
2438 [
2439 'name' => 'testing',
2440 'class' => MemoryFileBackend::class,
2441 'wikiId' => 'meow',
2442 'mimeCallback' => $mimeCallback
2443 ]
2444 ) );
2445
2446 $dst = 'mwstore://testing/container/path/to/file_no_ext';
2447 $src = "$IP/tests/phpunit/data/media/srgb.jpg";
2448 $this->assertEquals( 'image/jpeg', $be->getContentType( $dst, null, $src ) );
2449 $this->assertEquals(
2450 $mimeFromString ? 'image/jpeg' : 'unknown/unknown',
2451 $be->getContentType( $dst, file_get_contents( $src ), null ) );
2452
2453 $src = "$IP/tests/phpunit/data/media/Png-native-test.png";
2454 $this->assertEquals( 'image/png', $be->getContentType( $dst, null, $src ) );
2455 $this->assertEquals(
2456 $mimeFromString ? 'image/png' : 'unknown/unknown',
2457 $be->getContentType( $dst, file_get_contents( $src ), null ) );
2458 }
2459
2460 public static function provider_testGetContentType() {
2461 return [
2462 [ null, false ],
2463 [ [ FileBackendGroup::singleton(), 'guessMimeInternal' ], true ]
2464 ];
2465 }
2466
2467 public function testReadAffinity() {
2468 $be = TestingAccessWrapper::newFromObject(
2469 new FileBackendMultiWrite( [
2470 'name' => 'localtesting',
2471 'wikiId' => wfWikiID() . mt_rand(),
2472 'backends' => [
2473 [ // backend 0
2474 'name' => 'multitesting0',
2475 'class' => MemoryFileBackend::class,
2476 'isMultiMaster' => false,
2477 'readAffinity' => true
2478 ],
2479 [ // backend 1
2480 'name' => 'multitesting1',
2481 'class' => MemoryFileBackend::class,
2482 'isMultiMaster' => true
2483 ]
2484 ]
2485 ] )
2486 );
2487
2488 $this->assertEquals(
2489 1,
2490 $be->getReadIndexFromParams( [ 'latest' => 1 ] ),
2491 'Reads with "latest" flag use backend 1'
2492 );
2493 $this->assertEquals(
2494 0,
2495 $be->getReadIndexFromParams( [ 'latest' => 0 ] ),
2496 'Reads without "latest" flag use backend 0'
2497 );
2498
2499 $p = 'container/test-cont/file.txt';
2500 $be->backends[0]->quickCreate( [
2501 'dst' => "mwstore://multitesting0/$p", 'content' => 'cattitude' ] );
2502 $be->backends[1]->quickCreate( [
2503 'dst' => "mwstore://multitesting1/$p", 'content' => 'princess of power' ] );
2504
2505 $this->assertEquals(
2506 'cattitude',
2507 $be->getFileContents( [ 'src' => "mwstore://localtesting/$p" ] ),
2508 "Non-latest read came from backend 0"
2509 );
2510 $this->assertEquals(
2511 'princess of power',
2512 $be->getFileContents( [ 'src' => "mwstore://localtesting/$p", 'latest' => 1 ] ),
2513 "Latest read came from backend1"
2514 );
2515 }
2516
2517 public function testAsyncWrites() {
2518 $be = TestingAccessWrapper::newFromObject(
2519 new FileBackendMultiWrite( [
2520 'name' => 'localtesting',
2521 'wikiId' => wfWikiID() . mt_rand(),
2522 'backends' => [
2523 [ // backend 0
2524 'name' => 'multitesting0',
2525 'class' => MemoryFileBackend::class,
2526 'isMultiMaster' => false
2527 ],
2528 [ // backend 1
2529 'name' => 'multitesting1',
2530 'class' => MemoryFileBackend::class,
2531 'isMultiMaster' => true
2532 ]
2533 ],
2534 'replication' => 'async'
2535 ] )
2536 );
2537
2538 $this->setMwGlobals( 'wgCommandLineMode', false );
2539
2540 $p = 'container/test-cont/file.txt';
2541 $be->quickCreate( [
2542 'dst' => "mwstore://localtesting/$p", 'content' => 'cattitude' ] );
2543
2544 $this->assertEquals(
2545 false,
2546 $be->backends[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
2547 "File not yet written to backend 0"
2548 );
2549 $this->assertEquals(
2550 'cattitude',
2551 $be->backends[1]->getFileContents( [ 'src' => "mwstore://multitesting1/$p" ] ),
2552 "File already written to backend 1"
2553 );
2554
2555 DeferredUpdates::doUpdates();
2556
2557 $this->assertEquals(
2558 'cattitude',
2559 $be->backends[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
2560 "File now written to backend 0"
2561 );
2562 }
2563
2564 public function testSanitizeOpHeaders() {
2565 $be = TestingAccessWrapper::newFromObject( new MemoryFileBackend( [
2566 'name' => 'localtesting',
2567 'wikiId' => wfWikiID()
2568 ] ) );
2569
2570 $input = [
2571 'headers' => [
2572 'content-Disposition' => FileBackend::makeContentDisposition( 'inline', 'name' ),
2573 'Content-dUration' => 25.6,
2574 'X-LONG-VALUE' => str_pad( '0', 300 ),
2575 'CONTENT-LENGTH' => 855055,
2576 ]
2577 ];
2578 $expected = [
2579 'headers' => [
2580 'content-disposition' => FileBackend::makeContentDisposition( 'inline', 'name' ),
2581 'content-duration' => 25.6,
2582 'content-length' => 855055
2583 ]
2584 ];
2585
2586 Wikimedia\suppressWarnings();
2587 $actual = $be->sanitizeOpHeaders( $input );
2588 Wikimedia\restoreWarnings();
2589
2590 $this->assertEquals( $expected, $actual, "Header sanitized properly" );
2591 }
2592
2593 // helper function
2594 private function listToArray( $iter ) {
2595 return is_array( $iter ) ? $iter : iterator_to_array( $iter );
2596 }
2597
2598 // test helper wrapper for backend prepare() function
2599 private function prepare( array $params ) {
2600 return $this->backend->prepare( $params );
2601 }
2602
2603 // test helper wrapper for backend prepare() function
2604 private function create( array $params ) {
2605 $params['op'] = 'create';
2606
2607 return $this->backend->doQuickOperations( [ $params ] );
2608 }
2609
2610 function tearDownFiles() {
2611 $containers = [ 'unittest-cont1', 'unittest-cont2', 'unittest-cont-bad' ];
2612 foreach ( $containers as $container ) {
2613 $this->deleteFiles( $container );
2614 }
2615 }
2616
2617 private function deleteFiles( $container ) {
2618 $base = self::baseStorePath();
2619 $iter = $this->backend->getFileList( [ 'dir' => "$base/$container" ] );
2620 if ( $iter ) {
2621 foreach ( $iter as $file ) {
2622 $this->backend->quickDelete( [ 'src' => "$base/$container/$file" ] );
2623 }
2624 // free the directory, to avoid Permission denied under windows on rmdir
2625 unset( $iter );
2626 }
2627 $this->backend->clean( [ 'dir' => "$base/$container", 'recursive' => 1 ] );
2628 }
2629
2630 function assertBackendPathsConsistent( array $paths ) {
2631 if ( $this->backend instanceof FileBackendMultiWrite ) {
2632 $status = $this->backend->consistencyCheck( $paths );
2633 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2634 }
2635 }
2636
2637 function assertGoodStatus( StatusValue $status, $msg ) {
2638 $this->assertEquals( print_r( [], 1 ), print_r( $status->getErrors(), 1 ), $msg );
2639 }
2640 }