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