Expand testDoQuickOperations() tests for FileBackend
[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 /**
927 * @dataProvider provider_quickOperations
928 */
929 public function testDoQuickOperations(
930 $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, $batchSize
931 ) {
932 $this->backend = $this->singleBackend;
933 $this->doTestDoQuickOperations(
934 $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, $batchSize
935 );
936 $this->tearDownFiles();
937
938 $this->backend = $this->multiBackend;
939 $this->doTestDoQuickOperations(
940 $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, $batchSize
941 );
942 $this->tearDownFiles();
943 }
944
945 private function doTestDoQuickOperations(
946 $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, $batchSize
947 ) {
948 $backendName = $this->backendClass();
949
950 foreach ( $files as $path ) {
951 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
952 $this->assertGoodStatus( $status,
953 "Preparing $path succeeded without warnings ($backendName)." );
954 }
955
956 foreach ( array_chunk( $createOps, $batchSize ) as $batchOps ) {
957 $this->assertGoodStatus(
958 $this->backend->doQuickOperations( $batchOps ),
959 "Creation of source files succeeded ($backendName)."
960 );
961 }
962 foreach ( $files as $file ) {
963 $this->assertTrue(
964 $this->backend->fileExists( [ 'src' => $file ] ),
965 "File $file exists."
966 );
967 }
968
969 foreach ( array_chunk( $copyOps, $batchSize ) as $batchOps ) {
970 $this->assertGoodStatus(
971 $this->backend->doQuickOperations( $batchOps ),
972 "Quick copy of source files succeeded ($backendName)."
973 );
974 }
975 foreach ( $files as $file ) {
976 $this->assertTrue(
977 $this->backend->fileExists( [ 'src' => "$file-2" ] ),
978 "File $file-2 exists."
979 );
980 }
981
982 foreach ( array_chunk( $moveOps, $batchSize ) as $batchOps ) {
983 $this->assertGoodStatus(
984 $this->backend->doQuickOperations( $batchOps ),
985 "Quick move of source files succeeded ($backendName)."
986 );
987 }
988 foreach ( $files as $file ) {
989 $this->assertTrue(
990 $this->backend->fileExists( [ 'src' => "$file-3" ] ),
991 "File $file-3 move in."
992 );
993 $this->assertFalse(
994 $this->backend->fileExists( [ 'src' => "$file-2" ] ),
995 "File $file-2 moved away."
996 );
997 }
998
999 foreach ( array_chunk( $overSelfOps, $batchSize ) as $batchOps ) {
1000 $this->assertGoodStatus(
1001 $this->backend->doQuickOperations( $batchOps ),
1002 "Quick copy/move of source files over themselves succeeded ($backendName)."
1003 );
1004 }
1005 foreach ( $files as $file ) {
1006 $this->assertTrue(
1007 $this->backend->fileExists( [ 'src' => $file ] ),
1008 "File $file still exists after copy/move over self."
1009 );
1010 }
1011
1012 foreach ( array_chunk( $deleteOps, $batchSize ) as $batchOps ) {
1013 $this->assertGoodStatus(
1014 $this->backend->doQuickOperations( $batchOps ),
1015 "Quick deletion of source files succeeded ($backendName)."
1016 );
1017 }
1018 foreach ( $files as $file ) {
1019 $this->assertFalse( $this->backend->fileExists( [ 'src' => $file ] ),
1020 "File $file purged." );
1021 $this->assertFalse( $this->backend->fileExists( [ 'src' => "$file-3" ] ),
1022 "File $file-3 purged." );
1023 }
1024 }
1025
1026 function provider_quickOperations() {
1027 $base = self::baseStorePath();
1028 $files = [
1029 "$base/unittest-cont1/e/fileA.a",
1030 "$base/unittest-cont1/e/fileB.a",
1031 "$base/unittest-cont1/e/fileC.a"
1032 ];
1033
1034 $createOps = $copyOps = $moveOps = $overSelfOps = $deleteOps = [];
1035 foreach ( $files as $path ) {
1036 $createOps[] = [ 'op' => 'create', 'dst' => $path, 'content' => 52525 ];
1037 $createOps[] = [ 'op' => 'create', 'dst' => "$path-x", 'content' => 832 ];
1038 $createOps[] = [ 'op' => 'null' ];
1039
1040 $copyOps[] = [ 'op' => 'copy', 'src' => $path, 'dst' => "$path-2" ];
1041 $copyOps[] = [
1042 'op' => 'copy',
1043 'src' => "$path-nothing",
1044 'dst' => "$path-nowhere",
1045 'ignoreMissingSource' => true
1046 ];
1047 $copyOps[] = [ 'op' => 'null' ];
1048
1049 $moveOps[] = [ 'op' => 'move', 'src' => "$path-2", 'dst' => "$path-3" ];
1050 $moveOps[] = [
1051 'op' => 'move',
1052 'src' => "$path-nothing",
1053 'dst' => "$path-nowhere",
1054 'ignoreMissingSource' => true
1055 ];
1056 $moveOps[] = [ 'op' => 'null' ];
1057
1058 $overSelfOps[] = [ 'op' => 'copy', 'src' => $path, 'dst' => $path ];
1059 $overSelfOps[] = [ 'op' => 'move', 'src' => $path, 'dst' => $path ];
1060
1061 $deleteOps[] = [ 'op' => 'delete', 'src' => $path ];
1062 $deleteOps[] = [ 'op' => 'delete', 'src' => "$path-3" ];
1063 $deleteOps[] = [
1064 'op' => 'delete',
1065 'src' => "$path-gone",
1066 'ignoreMissingSource' => true
1067 ];
1068 $deleteOps[] = [ 'op' => 'null' ];
1069 }
1070
1071 return [
1072 [ $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, 1 ],
1073 [ $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, 2 ],
1074 [ $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, 100 ]
1075 ];
1076 }
1077
1078 /**
1079 * @dataProvider provider_testConcatenate
1080 */
1081 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
1082 $this->backend = $this->singleBackend;
1083 $this->tearDownFiles();
1084 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
1085 $this->tearDownFiles();
1086
1087 $this->backend = $this->multiBackend;
1088 $this->tearDownFiles();
1089 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
1090 $this->tearDownFiles();
1091 }
1092
1093 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
1094 $backendName = $this->backendClass();
1095
1096 $expContent = '';
1097 // Create sources
1098 $ops = [];
1099 foreach ( $srcs as $i => $source ) {
1100 $this->prepare( [ 'dir' => dirname( $source ) ] );
1101 $ops[] = [
1102 'op' => 'create', // operation
1103 'dst' => $source, // source
1104 'content' => $srcsContent[$i]
1105 ];
1106 $expContent .= $srcsContent[$i];
1107 }
1108 $status = $this->backend->doOperations( $ops );
1109
1110 $this->assertGoodStatus( $status,
1111 "Creation of source files succeeded ($backendName)." );
1112
1113 $dest = $params['dst'] = $this->getNewTempFile();
1114 if ( $alreadyExists ) {
1115 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
1116 $this->assertEquals( true, $ok,
1117 "Creation of file at $dest succeeded ($backendName)." );
1118 } else {
1119 $ok = file_put_contents( $dest, '' ) !== false;
1120 $this->assertEquals( true, $ok,
1121 "Creation of 0-byte file at $dest succeeded ($backendName)." );
1122 }
1123
1124 // Combine the files into one
1125 $status = $this->backend->concatenate( $params );
1126 if ( $okStatus ) {
1127 $this->assertGoodStatus( $status,
1128 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
1129 $this->assertEquals( true, $status->isOK(),
1130 "Creation of concat file at $dest succeeded ($backendName)." );
1131 } else {
1132 $this->assertEquals( false, $status->isOK(),
1133 "Creation of concat file at $dest failed ($backendName)." );
1134 }
1135
1136 if ( $okStatus ) {
1137 $this->assertEquals( true, is_file( $dest ),
1138 "Dest concat file $dest exists after creation ($backendName)." );
1139 } else {
1140 $this->assertEquals( true, is_file( $dest ),
1141 "Dest concat file $dest exists after failed creation ($backendName)." );
1142 }
1143
1144 $contents = file_get_contents( $dest );
1145 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
1146
1147 if ( $okStatus ) {
1148 $this->assertEquals( $expContent, $contents,
1149 "Concat file at $dest has correct contents ($backendName)." );
1150 } else {
1151 $this->assertNotEquals( $expContent, $contents,
1152 "Concat file at $dest has correct contents ($backendName)." );
1153 }
1154 }
1155
1156 public static function provider_testConcatenate() {
1157 $cases = [];
1158
1159 $srcs = [
1160 self::baseStorePath() . '/unittest-cont1/e/file1.txt',
1161 self::baseStorePath() . '/unittest-cont1/e/file2.txt',
1162 self::baseStorePath() . '/unittest-cont1/e/file3.txt',
1163 self::baseStorePath() . '/unittest-cont1/e/file4.txt',
1164 self::baseStorePath() . '/unittest-cont1/e/file5.txt',
1165 self::baseStorePath() . '/unittest-cont1/e/file6.txt',
1166 self::baseStorePath() . '/unittest-cont1/e/file7.txt',
1167 self::baseStorePath() . '/unittest-cont1/e/file8.txt',
1168 self::baseStorePath() . '/unittest-cont1/e/file9.txt',
1169 self::baseStorePath() . '/unittest-cont1/e/file10.txt'
1170 ];
1171 $content = [
1172 'egfage',
1173 'ageageag',
1174 'rhokohlr',
1175 'shgmslkg',
1176 'kenga',
1177 'owagmal',
1178 'kgmae',
1179 'g eak;g',
1180 'lkaem;a',
1181 'legma'
1182 ];
1183 $params = [ 'srcs' => $srcs ];
1184
1185 $cases[] = [
1186 $params, // operation
1187 $srcs, // sources
1188 $content, // content for each source
1189 false, // no dest already exists
1190 true, // succeeds
1191 ];
1192
1193 $cases[] = [
1194 $params, // operation
1195 $srcs, // sources
1196 $content, // content for each source
1197 true, // dest already exists
1198 false, // succeeds
1199 ];
1200
1201 return $cases;
1202 }
1203
1204 /**
1205 * @dataProvider provider_testGetFileStat
1206 */
1207 public function testGetFileStat( $path, $content, $alreadyExists ) {
1208 $this->backend = $this->singleBackend;
1209 $this->tearDownFiles();
1210 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1211 $this->tearDownFiles();
1212
1213 $this->backend = $this->multiBackend;
1214 $this->tearDownFiles();
1215 $this->doTestGetFileStat( $path, $content, $alreadyExists );
1216 $this->tearDownFiles();
1217 }
1218
1219 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
1220 $backendName = $this->backendClass();
1221
1222 if ( $alreadyExists ) {
1223 $this->prepare( [ 'dir' => dirname( $path ) ] );
1224 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1225 $this->assertGoodStatus( $status,
1226 "Creation of file at $path succeeded ($backendName)." );
1227
1228 $size = $this->backend->getFileSize( [ 'src' => $path ] );
1229 $time = $this->backend->getFileTimestamp( [ 'src' => $path ] );
1230 $stat = $this->backend->getFileStat( [ 'src' => $path ] );
1231
1232 $this->assertEquals( strlen( $content ), $size,
1233 "Correct file size of '$path'" );
1234 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
1235 "Correct file timestamp of '$path'" );
1236
1237 $size = $stat['size'];
1238 $time = $stat['mtime'];
1239 $this->assertEquals( strlen( $content ), $size,
1240 "Correct file size of '$path'" );
1241 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
1242 "Correct file timestamp of '$path'" );
1243
1244 $this->backend->clearCache( [ $path ] );
1245
1246 $size = $this->backend->getFileSize( [ 'src' => $path ] );
1247
1248 $this->assertEquals( strlen( $content ), $size,
1249 "Correct file size of '$path'" );
1250
1251 $this->backend->preloadCache( [ $path ] );
1252
1253 $size = $this->backend->getFileSize( [ 'src' => $path ] );
1254
1255 $this->assertEquals( strlen( $content ), $size,
1256 "Correct file size of '$path'" );
1257 } else {
1258 $size = $this->backend->getFileSize( [ 'src' => $path ] );
1259 $time = $this->backend->getFileTimestamp( [ 'src' => $path ] );
1260 $stat = $this->backend->getFileStat( [ 'src' => $path ] );
1261
1262 $this->assertFalse( $size, "Correct file size of '$path'" );
1263 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
1264 $this->assertFalse( $stat, "Correct file stat of '$path'" );
1265 }
1266 }
1267
1268 public static function provider_testGetFileStat() {
1269 $cases = [];
1270
1271 $base = self::baseStorePath();
1272 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true ];
1273 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "", true ];
1274 $cases[] = [ "$base/unittest-cont1/e/b/some-diff_file.txt", null, false ];
1275
1276 return $cases;
1277 }
1278
1279 /**
1280 * @dataProvider provider_testGetFileStat
1281 */
1282 public function testStreamFile( $path, $content, $alreadyExists ) {
1283 $this->backend = $this->singleBackend;
1284 $this->tearDownFiles();
1285 $this->doTestStreamFile( $path, $content, $alreadyExists );
1286 $this->tearDownFiles();
1287
1288 $this->backend = $this->multiBackend;
1289 $this->tearDownFiles();
1290 $this->doTestStreamFile( $path, $content, $alreadyExists );
1291 $this->tearDownFiles();
1292 }
1293
1294 private function doTestStreamFile( $path, $content ) {
1295 $backendName = $this->backendClass();
1296
1297 if ( $content !== null ) {
1298 $this->prepare( [ 'dir' => dirname( $path ) ] );
1299 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1300 $this->assertGoodStatus( $status,
1301 "Creation of file at $path succeeded ($backendName)." );
1302
1303 ob_start();
1304 $this->backend->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
1305 $data = ob_get_contents();
1306 ob_end_clean();
1307
1308 $this->assertEquals( $content, $data, "Correct content streamed from '$path'" );
1309 } else { // 404 case
1310 ob_start();
1311 $this->backend->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
1312 $data = ob_get_contents();
1313 ob_end_clean();
1314
1315 $this->assertRegExp( '#<h1>File not found</h1>#', $data,
1316 "Correct content streamed from '$path' ($backendName)" );
1317 }
1318 }
1319
1320 public static function provider_testStreamFile() {
1321 $cases = [];
1322
1323 $base = self::baseStorePath();
1324 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
1325 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", null ];
1326
1327 return $cases;
1328 }
1329
1330 public function testStreamFileRange() {
1331 $this->backend = $this->singleBackend;
1332 $this->tearDownFiles();
1333 $this->doTestStreamFileRange();
1334 $this->tearDownFiles();
1335
1336 $this->backend = $this->multiBackend;
1337 $this->tearDownFiles();
1338 $this->doTestStreamFileRange();
1339 $this->tearDownFiles();
1340 }
1341
1342 private function doTestStreamFileRange() {
1343 $backendName = $this->backendClass();
1344
1345 $base = self::baseStorePath();
1346 $path = "$base/unittest-cont1/e/b/z/range_file.txt";
1347 $content = "0123456789ABCDEF";
1348
1349 $this->prepare( [ 'dir' => dirname( $path ) ] );
1350 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
1351 $this->assertGoodStatus( $status,
1352 "Creation of file at $path succeeded ($backendName)." );
1353
1354 static $ranges = [
1355 'bytes=0-0' => '0',
1356 'bytes=0-3' => '0123',
1357 'bytes=4-8' => '45678',
1358 'bytes=15-15' => 'F',
1359 'bytes=14-15' => 'EF',
1360 'bytes=-5' => 'BCDEF',
1361 'bytes=-1' => 'F',
1362 'bytes=10-16' => 'ABCDEF',
1363 'bytes=10-99' => 'ABCDEF',
1364 ];
1365
1366 foreach ( $ranges as $range => $chunk ) {
1367 ob_start();
1368 $this->backend->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1,
1369 'options' => [ 'range' => $range ] ] );
1370 $data = ob_get_contents();
1371 ob_end_clean();
1372
1373 $this->assertEquals( $chunk, $data, "Correct chunk streamed from '$path' for '$range'" );
1374 }
1375 }
1376
1377 /**
1378 * @dataProvider provider_testGetFileContents
1379 */
1380 public function testGetFileContents( $source, $content ) {
1381 $this->backend = $this->singleBackend;
1382 $this->tearDownFiles();
1383 $this->doTestGetFileContents( $source, $content );
1384 $this->tearDownFiles();
1385
1386 $this->backend = $this->multiBackend;
1387 $this->tearDownFiles();
1388 $this->doTestGetFileContents( $source, $content );
1389 $this->tearDownFiles();
1390 }
1391
1392 private function doTestGetFileContents( $source, $content ) {
1393 $backendName = $this->backendClass();
1394
1395 $srcs = (array)$source;
1396 $content = (array)$content;
1397 foreach ( $srcs as $i => $src ) {
1398 $this->prepare( [ 'dir' => dirname( $src ) ] );
1399 $status = $this->backend->doOperation(
1400 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1401 $this->assertGoodStatus( $status,
1402 "Creation of file at $src succeeded ($backendName)." );
1403 }
1404
1405 if ( is_array( $source ) ) {
1406 $contents = $this->backend->getFileContentsMulti( [ 'srcs' => $source ] );
1407 foreach ( $contents as $path => $data ) {
1408 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
1409 $this->assertEquals(
1410 current( $content ),
1411 $data,
1412 "Contents of $path is correct ($backendName)."
1413 );
1414 next( $content );
1415 }
1416 $this->assertEquals(
1417 $source,
1418 array_keys( $contents ),
1419 "Contents in right order ($backendName)."
1420 );
1421 $this->assertEquals(
1422 count( $source ),
1423 count( $contents ),
1424 "Contents array size correct ($backendName)."
1425 );
1426 } else {
1427 $data = $this->backend->getFileContents( [ 'src' => $source ] );
1428 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
1429 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1430 }
1431 }
1432
1433 public static function provider_testGetFileContents() {
1434 $cases = [];
1435
1436 $base = self::baseStorePath();
1437 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
1438 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" ];
1439 $cases[] = [
1440 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1441 "$base/unittest-cont1/e/a/z.txt" ],
1442 [ "contents xx", "contents xy", "contents xz" ]
1443 ];
1444
1445 return $cases;
1446 }
1447
1448 /**
1449 * @dataProvider provider_testGetLocalCopy
1450 */
1451 public function testGetLocalCopy( $source, $content ) {
1452 $this->backend = $this->singleBackend;
1453 $this->tearDownFiles();
1454 $this->doTestGetLocalCopy( $source, $content );
1455 $this->tearDownFiles();
1456
1457 $this->backend = $this->multiBackend;
1458 $this->tearDownFiles();
1459 $this->doTestGetLocalCopy( $source, $content );
1460 $this->tearDownFiles();
1461 }
1462
1463 private function doTestGetLocalCopy( $source, $content ) {
1464 $backendName = $this->backendClass();
1465
1466 $srcs = (array)$source;
1467 $content = (array)$content;
1468 foreach ( $srcs as $i => $src ) {
1469 $this->prepare( [ 'dir' => dirname( $src ) ] );
1470 $status = $this->backend->doOperation(
1471 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1472 $this->assertGoodStatus( $status,
1473 "Creation of file at $src succeeded ($backendName)." );
1474 }
1475
1476 if ( is_array( $source ) ) {
1477 $tmpFiles = $this->backend->getLocalCopyMulti( [ 'srcs' => $source ] );
1478 foreach ( $tmpFiles as $path => $tmpFile ) {
1479 $this->assertNotNull( $tmpFile,
1480 "Creation of local copy of $path succeeded ($backendName)." );
1481 $contents = file_get_contents( $tmpFile->getPath() );
1482 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1483 $this->assertEquals(
1484 current( $content ),
1485 $contents,
1486 "Local copy of $path is correct ($backendName)."
1487 );
1488 next( $content );
1489 }
1490 $this->assertEquals(
1491 $source,
1492 array_keys( $tmpFiles ),
1493 "Local copies in right order ($backendName)."
1494 );
1495 $this->assertEquals(
1496 count( $source ),
1497 count( $tmpFiles ),
1498 "Local copies array size correct ($backendName)."
1499 );
1500 } else {
1501 $tmpFile = $this->backend->getLocalCopy( [ 'src' => $source ] );
1502 $this->assertNotNull( $tmpFile,
1503 "Creation of local copy of $source succeeded ($backendName)." );
1504 $contents = file_get_contents( $tmpFile->getPath() );
1505 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1506 $this->assertEquals(
1507 $content[0],
1508 $contents,
1509 "Local copy of $source is correct ($backendName)."
1510 );
1511 }
1512
1513 $obj = new stdClass();
1514 $tmpFile->bind( $obj );
1515 }
1516
1517 public static function provider_testGetLocalCopy() {
1518 $cases = [];
1519
1520 $base = self::baseStorePath();
1521 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1522 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1523 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1524 $cases[] = [
1525 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1526 "$base/unittest-cont1/e/a/z.txt" ],
1527 [ "contents xx $", "contents xy 111", "contents xz" ]
1528 ];
1529
1530 return $cases;
1531 }
1532
1533 /**
1534 * @dataProvider provider_testGetLocalReference
1535 */
1536 public function testGetLocalReference( $source, $content ) {
1537 $this->backend = $this->singleBackend;
1538 $this->tearDownFiles();
1539 $this->doTestGetLocalReference( $source, $content );
1540 $this->tearDownFiles();
1541
1542 $this->backend = $this->multiBackend;
1543 $this->tearDownFiles();
1544 $this->doTestGetLocalReference( $source, $content );
1545 $this->tearDownFiles();
1546 }
1547
1548 private function doTestGetLocalReference( $source, $content ) {
1549 $backendName = $this->backendClass();
1550
1551 $srcs = (array)$source;
1552 $content = (array)$content;
1553 foreach ( $srcs as $i => $src ) {
1554 $this->prepare( [ 'dir' => dirname( $src ) ] );
1555 $status = $this->backend->doOperation(
1556 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1557 $this->assertGoodStatus( $status,
1558 "Creation of file at $src succeeded ($backendName)." );
1559 }
1560
1561 if ( is_array( $source ) ) {
1562 $tmpFiles = $this->backend->getLocalReferenceMulti( [ 'srcs' => $source ] );
1563 foreach ( $tmpFiles as $path => $tmpFile ) {
1564 $this->assertNotNull( $tmpFile,
1565 "Creation of local copy of $path succeeded ($backendName)." );
1566 $contents = file_get_contents( $tmpFile->getPath() );
1567 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1568 $this->assertEquals(
1569 current( $content ),
1570 $contents,
1571 "Local ref of $path is correct ($backendName)."
1572 );
1573 next( $content );
1574 }
1575 $this->assertEquals(
1576 $source,
1577 array_keys( $tmpFiles ),
1578 "Local refs in right order ($backendName)."
1579 );
1580 $this->assertEquals(
1581 count( $source ),
1582 count( $tmpFiles ),
1583 "Local refs array size correct ($backendName)."
1584 );
1585 } else {
1586 $tmpFile = $this->backend->getLocalReference( [ 'src' => $source ] );
1587 $this->assertNotNull( $tmpFile,
1588 "Creation of local copy of $source succeeded ($backendName)." );
1589 $contents = file_get_contents( $tmpFile->getPath() );
1590 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1591 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1592 }
1593 }
1594
1595 public static function provider_testGetLocalReference() {
1596 $cases = [];
1597
1598 $base = self::baseStorePath();
1599 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1600 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1601 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1602 $cases[] = [
1603 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1604 "$base/unittest-cont1/e/a/z.txt" ],
1605 [ "contents xx 1111", "contents xy %", "contents xz $" ]
1606 ];
1607
1608 return $cases;
1609 }
1610
1611 public function testGetLocalCopyAndReference404() {
1612 $this->backend = $this->singleBackend;
1613 $this->tearDownFiles();
1614 $this->doTestGetLocalCopyAndReference404();
1615 $this->tearDownFiles();
1616
1617 $this->backend = $this->multiBackend;
1618 $this->tearDownFiles();
1619 $this->doTestGetLocalCopyAndReference404();
1620 $this->tearDownFiles();
1621 }
1622
1623 public function doTestGetLocalCopyAndReference404() {
1624 $backendName = $this->backendClass();
1625
1626 $base = self::baseStorePath();
1627
1628 $tmpFile = $this->backend->getLocalCopy( [
1629 'src' => "$base/unittest-cont1/not-there" ] );
1630 $this->assertNull( $tmpFile, "Local copy of not existing file is null ($backendName)." );
1631
1632 $tmpFile = $this->backend->getLocalReference( [
1633 'src' => "$base/unittest-cont1/not-there" ] );
1634 $this->assertNull( $tmpFile, "Local ref of not existing file is null ($backendName)." );
1635 }
1636
1637 /**
1638 * @dataProvider provider_testGetFileHttpUrl
1639 */
1640 public function testGetFileHttpUrl( $source, $content ) {
1641 $this->backend = $this->singleBackend;
1642 $this->tearDownFiles();
1643 $this->doTestGetFileHttpUrl( $source, $content );
1644 $this->tearDownFiles();
1645
1646 $this->backend = $this->multiBackend;
1647 $this->tearDownFiles();
1648 $this->doTestGetFileHttpUrl( $source, $content );
1649 $this->tearDownFiles();
1650 }
1651
1652 private function doTestGetFileHttpUrl( $source, $content ) {
1653 $backendName = $this->backendClass();
1654
1655 $this->prepare( [ 'dir' => dirname( $source ) ] );
1656 $status = $this->backend->doOperation(
1657 [ 'op' => 'create', 'content' => $content, 'dst' => $source ] );
1658 $this->assertGoodStatus( $status,
1659 "Creation of file at $source succeeded ($backendName)." );
1660
1661 $url = $this->backend->getFileHttpUrl( [ 'src' => $source ] );
1662
1663 if ( $url !== null ) { // supported
1664 $data = MediaWikiServices::getInstance()->getHttpRequestFactory()->
1665 get( $url, [], __METHOD__ );
1666 $this->assertEquals( $content, $data,
1667 "HTTP GET of URL has right contents ($backendName)." );
1668 }
1669 }
1670
1671 public static function provider_testGetFileHttpUrl() {
1672 $cases = [];
1673
1674 $base = self::baseStorePath();
1675 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1676 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1677 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1678
1679 return $cases;
1680 }
1681
1682 /**
1683 * @dataProvider provider_testPrepareAndClean
1684 */
1685 public function testPrepareAndClean( $path, $isOK ) {
1686 $this->backend = $this->singleBackend;
1687 $this->doTestPrepareAndClean( $path, $isOK );
1688 $this->tearDownFiles();
1689
1690 $this->backend = $this->multiBackend;
1691 $this->doTestPrepareAndClean( $path, $isOK );
1692 $this->tearDownFiles();
1693 }
1694
1695 public static function provider_testPrepareAndClean() {
1696 $base = self::baseStorePath();
1697
1698 return [
1699 [ "$base/unittest-cont1/e/a/z/some_file1.txt", true ],
1700 [ "$base/unittest-cont2/a/z/some_file2.txt", true ],
1701 # Specific to FS backend with no basePath field set
1702 # [ "$base/unittest-cont3/a/z/some_file3.txt", false ],
1703 ];
1704 }
1705
1706 private function doTestPrepareAndClean( $path, $isOK ) {
1707 $backendName = $this->backendClass();
1708
1709 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
1710 if ( $isOK ) {
1711 $this->assertGoodStatus( $status,
1712 "Preparing dir $path succeeded without warnings ($backendName)." );
1713 $this->assertEquals( true, $status->isOK(),
1714 "Preparing dir $path succeeded ($backendName)." );
1715 } else {
1716 $this->assertEquals( false, $status->isOK(),
1717 "Preparing dir $path failed ($backendName)." );
1718 }
1719
1720 $status = $this->backend->secure( [ 'dir' => dirname( $path ) ] );
1721 if ( $isOK ) {
1722 $this->assertGoodStatus( $status,
1723 "Securing dir $path succeeded without warnings ($backendName)." );
1724 $this->assertEquals( true, $status->isOK(),
1725 "Securing dir $path succeeded ($backendName)." );
1726 } else {
1727 $this->assertEquals( false, $status->isOK(),
1728 "Securing dir $path failed ($backendName)." );
1729 }
1730
1731 $status = $this->backend->publish( [ 'dir' => dirname( $path ) ] );
1732 if ( $isOK ) {
1733 $this->assertGoodStatus( $status,
1734 "Publishing dir $path succeeded without warnings ($backendName)." );
1735 $this->assertEquals( true, $status->isOK(),
1736 "Publishing dir $path succeeded ($backendName)." );
1737 } else {
1738 $this->assertEquals( false, $status->isOK(),
1739 "Publishing dir $path failed ($backendName)." );
1740 }
1741
1742 $status = $this->backend->clean( [ 'dir' => dirname( $path ) ] );
1743 if ( $isOK ) {
1744 $this->assertGoodStatus( $status,
1745 "Cleaning dir $path succeeded without warnings ($backendName)." );
1746 $this->assertEquals( true, $status->isOK(),
1747 "Cleaning dir $path succeeded ($backendName)." );
1748 } else {
1749 $this->assertEquals( false, $status->isOK(),
1750 "Cleaning dir $path failed ($backendName)." );
1751 }
1752 }
1753
1754 public function testRecursiveClean() {
1755 $this->backend = $this->singleBackend;
1756 $this->doTestRecursiveClean();
1757 $this->tearDownFiles();
1758
1759 $this->backend = $this->multiBackend;
1760 $this->doTestRecursiveClean();
1761 $this->tearDownFiles();
1762 }
1763
1764 private function doTestRecursiveClean() {
1765 $backendName = $this->backendClass();
1766
1767 $base = self::baseStorePath();
1768 $dirs = [
1769 "$base/unittest-cont1",
1770 "$base/unittest-cont1/e",
1771 "$base/unittest-cont1/e/a",
1772 "$base/unittest-cont1/e/a/b",
1773 "$base/unittest-cont1/e/a/b/c",
1774 "$base/unittest-cont1/e/a/b/c/d0",
1775 "$base/unittest-cont1/e/a/b/c/d1",
1776 "$base/unittest-cont1/e/a/b/c/d2",
1777 "$base/unittest-cont1/e/a/b/c/d0/1",
1778 "$base/unittest-cont1/e/a/b/c/d0/2",
1779 "$base/unittest-cont1/e/a/b/c/d1/3",
1780 "$base/unittest-cont1/e/a/b/c/d1/4",
1781 "$base/unittest-cont1/e/a/b/c/d2/5",
1782 "$base/unittest-cont1/e/a/b/c/d2/6"
1783 ];
1784 foreach ( $dirs as $dir ) {
1785 $status = $this->prepare( [ 'dir' => $dir ] );
1786 $this->assertGoodStatus( $status,
1787 "Preparing dir $dir succeeded without warnings ($backendName)." );
1788 }
1789
1790 if ( $this->backend instanceof FSFileBackend ) {
1791 foreach ( $dirs as $dir ) {
1792 $this->assertEquals( true, $this->backend->directoryExists( [ 'dir' => $dir ] ),
1793 "Dir $dir exists ($backendName)." );
1794 }
1795 }
1796
1797 $status = $this->backend->clean(
1798 [ 'dir' => "$base/unittest-cont1", 'recursive' => 1 ] );
1799 $this->assertGoodStatus( $status,
1800 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1801
1802 foreach ( $dirs as $dir ) {
1803 $this->assertEquals( false, $this->backend->directoryExists( [ 'dir' => $dir ] ),
1804 "Dir $dir no longer exists ($backendName)." );
1805 }
1806 }
1807
1808 public function testDoOperations() {
1809 $this->backend = $this->singleBackend;
1810 $this->tearDownFiles();
1811 $this->doTestDoOperations();
1812 $this->tearDownFiles();
1813
1814 $this->backend = $this->multiBackend;
1815 $this->tearDownFiles();
1816 $this->doTestDoOperations();
1817 $this->tearDownFiles();
1818 }
1819
1820 private function doTestDoOperations() {
1821 $base = self::baseStorePath();
1822
1823 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1824 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1825 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1826 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1827 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1828 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1829 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1830
1831 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1832 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1833 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1834 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
1835 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1836 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
1837 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1838
1839 $status = $this->backend->doOperations( [
1840 [ 'op' => 'describe', 'src' => $fileA,
1841 'headers' => [ 'X-Content-Length' => '91.3' ], 'disposition' => 'inline' ],
1842 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1843 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1844 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1845 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1846 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1847 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1848 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1849 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1850 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1851 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1852 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1853 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1854 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1855 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1856 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1857 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1858 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1859 // Does nothing
1860 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1861 // Does nothing
1862 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1863 // Does nothing
1864 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1865 // Does nothing
1866 [ 'op' => 'null' ],
1867 // Does nothing
1868 ] );
1869
1870 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1871 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1872 $this->assertEquals( 14, count( $status->success ),
1873 "Operation batch has correct success array" );
1874
1875 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileA ] ),
1876 "File does not exist at $fileA" );
1877 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileB ] ),
1878 "File does not exist at $fileB" );
1879 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileD ] ),
1880 "File does not exist at $fileD" );
1881
1882 $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $fileC ] ),
1883 "File exists at $fileC" );
1884 $this->assertEquals( $fileBContents,
1885 $this->backend->getFileContents( [ 'src' => $fileC ] ),
1886 "Correct file contents of $fileC" );
1887 $this->assertEquals( strlen( $fileBContents ),
1888 $this->backend->getFileSize( [ 'src' => $fileC ] ),
1889 "Correct file size of $fileC" );
1890 $this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ),
1891 $this->backend->getFileSha1Base36( [ 'src' => $fileC ] ),
1892 "Correct file SHA-1 of $fileC" );
1893 }
1894
1895 public function testDoOperationsPipeline() {
1896 $this->backend = $this->singleBackend;
1897 $this->tearDownFiles();
1898 $this->doTestDoOperationsPipeline();
1899 $this->tearDownFiles();
1900
1901 $this->backend = $this->multiBackend;
1902 $this->tearDownFiles();
1903 $this->doTestDoOperationsPipeline();
1904 $this->tearDownFiles();
1905 }
1906
1907 // concurrency orientated
1908 private function doTestDoOperationsPipeline() {
1909 $base = self::baseStorePath();
1910
1911 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1912 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1913 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1914
1915 $tmpNameA = TempFSFile::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1916 $tmpNameB = TempFSFile::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1917 $tmpNameC = TempFSFile::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1918 $this->addTmpFiles( [ $tmpNameA, $tmpNameB, $tmpNameC ] );
1919 file_put_contents( $tmpNameA, $fileAContents );
1920 file_put_contents( $tmpNameB, $fileBContents );
1921 file_put_contents( $tmpNameC, $fileCContents );
1922
1923 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1924 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1925 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1926 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1927
1928 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1929 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1930 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1931 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1932 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1933
1934 $status = $this->backend->doOperations( [
1935 [ 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ],
1936 [ 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ],
1937 [ 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ],
1938 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1939 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1940 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1941 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1942 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1943 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1944 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1945 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1946 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1947 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1948 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1949 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1950 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1951 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1952 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1953 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1954 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1955 // Does nothing
1956 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1957 // Does nothing
1958 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1959 // Does nothing
1960 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1961 // Does nothing
1962 [ 'op' => 'null' ],
1963 // Does nothing
1964 ] );
1965
1966 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1967 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1968 $this->assertEquals( 16, count( $status->success ),
1969 "Operation batch has correct success array" );
1970
1971 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileA ] ),
1972 "File does not exist at $fileA" );
1973 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileB ] ),
1974 "File does not exist at $fileB" );
1975 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileD ] ),
1976 "File does not exist at $fileD" );
1977
1978 $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $fileC ] ),
1979 "File exists at $fileC" );
1980 $this->assertEquals( $fileBContents,
1981 $this->backend->getFileContents( [ 'src' => $fileC ] ),
1982 "Correct file contents of $fileC" );
1983 $this->assertEquals( strlen( $fileBContents ),
1984 $this->backend->getFileSize( [ 'src' => $fileC ] ),
1985 "Correct file size of $fileC" );
1986 $this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ),
1987 $this->backend->getFileSha1Base36( [ 'src' => $fileC ] ),
1988 "Correct file SHA-1 of $fileC" );
1989 }
1990
1991 public function testDoOperationsFailing() {
1992 $this->backend = $this->singleBackend;
1993 $this->tearDownFiles();
1994 $this->doTestDoOperationsFailing();
1995 $this->tearDownFiles();
1996
1997 $this->backend = $this->multiBackend;
1998 $this->tearDownFiles();
1999 $this->doTestDoOperationsFailing();
2000 $this->tearDownFiles();
2001 }
2002
2003 private function doTestDoOperationsFailing() {
2004 $base = self::baseStorePath();
2005
2006 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
2007 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
2008 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
2009 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
2010 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
2011 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
2012 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
2013
2014 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
2015 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
2016 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
2017 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
2018 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
2019 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
2020
2021 $status = $this->backend->doOperations( [
2022 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
2023 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
2024 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
2025 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
2026 [ 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ],
2027 // Now: A:<A>, B:<B>, C:<A>, D:<B>
2028 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ],
2029 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
2030 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ],
2031 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
2032 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ],
2033 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
2034 [ 'op' => 'delete', 'src' => $fileD ],
2035 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
2036 [ 'op' => 'null' ],
2037 // Does nothing
2038 ], [ 'force' => 1 ] );
2039
2040 $this->assertNotEquals( [], $status->getErrors(), "Operation had warnings" );
2041 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
2042 $this->assertEquals( 8, count( $status->success ),
2043 "Operation batch has correct success array" );
2044
2045 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileB ] ),
2046 "File does not exist at $fileB" );
2047 $this->assertEquals( false, $this->backend->fileExists( [ 'src' => $fileD ] ),
2048 "File does not exist at $fileD" );
2049
2050 $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $fileA ] ),
2051 "File does not exist at $fileA" );
2052 $this->assertEquals( true, $this->backend->fileExists( [ 'src' => $fileC ] ),
2053 "File exists at $fileC" );
2054 $this->assertEquals( $fileBContents,
2055 $this->backend->getFileContents( [ 'src' => $fileA ] ),
2056 "Correct file contents of $fileA" );
2057 $this->assertEquals( strlen( $fileBContents ),
2058 $this->backend->getFileSize( [ 'src' => $fileA ] ),
2059 "Correct file size of $fileA" );
2060 $this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ),
2061 $this->backend->getFileSha1Base36( [ 'src' => $fileA ] ),
2062 "Correct file SHA-1 of $fileA" );
2063 }
2064
2065 public function testGetFileList() {
2066 $this->backend = $this->singleBackend;
2067 $this->tearDownFiles();
2068 $this->doTestGetFileList();
2069 $this->tearDownFiles();
2070
2071 $this->backend = $this->multiBackend;
2072 $this->tearDownFiles();
2073 $this->doTestGetFileList();
2074 $this->tearDownFiles();
2075 }
2076
2077 private function doTestGetFileList() {
2078 $backendName = $this->backendClass();
2079 $base = self::baseStorePath();
2080
2081 // Should have no errors
2082 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont-notexists" ] );
2083
2084 $files = [
2085 "$base/unittest-cont1/e/test1.txt",
2086 "$base/unittest-cont1/e/test2.txt",
2087 "$base/unittest-cont1/e/test3.txt",
2088 "$base/unittest-cont1/e/subdir1/test1.txt",
2089 "$base/unittest-cont1/e/subdir1/test2.txt",
2090 "$base/unittest-cont1/e/subdir2/test3.txt",
2091 "$base/unittest-cont1/e/subdir2/test4.txt",
2092 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
2093 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
2094 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
2095 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
2096 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
2097 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
2098 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
2099 ];
2100
2101 // Add the files
2102 $ops = [];
2103 foreach ( $files as $file ) {
2104 $this->prepare( [ 'dir' => dirname( $file ) ] );
2105 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
2106 }
2107 $status = $this->backend->doQuickOperations( $ops );
2108 $this->assertGoodStatus( $status,
2109 "Creation of files succeeded ($backendName)." );
2110 $this->assertEquals( true, $status->isOK(),
2111 "Creation of files succeeded with OK status ($backendName)." );
2112
2113 // Expected listing at root
2114 $expected = [
2115 "e/test1.txt",
2116 "e/test2.txt",
2117 "e/test3.txt",
2118 "e/subdir1/test1.txt",
2119 "e/subdir1/test2.txt",
2120 "e/subdir2/test3.txt",
2121 "e/subdir2/test4.txt",
2122 "e/subdir2/subdir/test1.txt",
2123 "e/subdir2/subdir/test2.txt",
2124 "e/subdir2/subdir/test3.txt",
2125 "e/subdir2/subdir/test4.txt",
2126 "e/subdir2/subdir/test5.txt",
2127 "e/subdir2/subdir/sub/test0.txt",
2128 "e/subdir2/subdir/sub/120-px-file.txt",
2129 ];
2130 sort( $expected );
2131
2132 // Actual listing (no trailing slash) at root
2133 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1" ] );
2134 $list = $this->listToArray( $iter );
2135 sort( $list );
2136 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2137
2138 // Actual listing (no trailing slash) at root with advise
2139 $iter = $this->backend->getFileList( [
2140 'dir' => "$base/unittest-cont1",
2141 'adviseStat' => 1
2142 ] );
2143 $list = $this->listToArray( $iter );
2144 sort( $list );
2145 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2146
2147 // Actual listing (with trailing slash) at root
2148 $list = [];
2149 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/" ] );
2150 foreach ( $iter as $file ) {
2151 $list[] = $file;
2152 }
2153 sort( $list );
2154 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2155
2156 // Expected listing at subdir
2157 $expected = [
2158 "test1.txt",
2159 "test2.txt",
2160 "test3.txt",
2161 "test4.txt",
2162 "test5.txt",
2163 "sub/test0.txt",
2164 "sub/120-px-file.txt",
2165 ];
2166 sort( $expected );
2167
2168 // Actual listing (no trailing slash) at subdir
2169 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] );
2170 $list = $this->listToArray( $iter );
2171 sort( $list );
2172 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2173
2174 // Actual listing (no trailing slash) at subdir with advise
2175 $iter = $this->backend->getFileList( [
2176 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2177 'adviseStat' => 1
2178 ] );
2179 $list = $this->listToArray( $iter );
2180 sort( $list );
2181 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2182
2183 // Actual listing (with trailing slash) at subdir
2184 $list = [];
2185 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ] );
2186 foreach ( $iter as $file ) {
2187 $list[] = $file;
2188 }
2189 sort( $list );
2190 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
2191
2192 // Actual listing (using iterator second time)
2193 $list = $this->listToArray( $iter );
2194 sort( $list );
2195 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
2196
2197 // Actual listing (top files only) at root
2198 $iter = $this->backend->getTopFileList( [ 'dir' => "$base/unittest-cont1" ] );
2199 $list = $this->listToArray( $iter );
2200 sort( $list );
2201 $this->assertEquals( [], $list, "Correct top file listing ($backendName)." );
2202
2203 // Expected listing (top files only) at subdir
2204 $expected = [
2205 "test1.txt",
2206 "test2.txt",
2207 "test3.txt",
2208 "test4.txt",
2209 "test5.txt"
2210 ];
2211 sort( $expected );
2212
2213 // Actual listing (top files only) at subdir
2214 $iter = $this->backend->getTopFileList(
2215 [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ]
2216 );
2217 $list = $this->listToArray( $iter );
2218 sort( $list );
2219 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2220
2221 // Actual listing (top files only) at subdir with advise
2222 $iter = $this->backend->getTopFileList( [
2223 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
2224 'adviseStat' => 1
2225 ] );
2226 $list = $this->listToArray( $iter );
2227 sort( $list );
2228 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
2229
2230 foreach ( $files as $file ) { // clean up
2231 $this->backend->doOperation( [ 'op' => 'delete', 'src' => $file ] );
2232 }
2233
2234 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
2235 foreach ( $iter as $iter ) {
2236 // no errors
2237 }
2238 }
2239
2240 public function testGetDirectoryList() {
2241 $this->backend = $this->singleBackend;
2242 $this->tearDownFiles();
2243 $this->doTestGetDirectoryList();
2244 $this->tearDownFiles();
2245
2246 $this->backend = $this->multiBackend;
2247 $this->tearDownFiles();
2248 $this->doTestGetDirectoryList();
2249 $this->tearDownFiles();
2250 }
2251
2252 private function doTestGetDirectoryList() {
2253 $backendName = $this->backendClass();
2254
2255 $base = self::baseStorePath();
2256 $files = [
2257 "$base/unittest-cont1/e/test1.txt",
2258 "$base/unittest-cont1/e/test2.txt",
2259 "$base/unittest-cont1/e/test3.txt",
2260 "$base/unittest-cont1/e/subdir1/test1.txt",
2261 "$base/unittest-cont1/e/subdir1/test2.txt",
2262 "$base/unittest-cont1/e/subdir2/test3.txt",
2263 "$base/unittest-cont1/e/subdir2/test4.txt",
2264 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
2265 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
2266 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
2267 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
2268 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
2269 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
2270 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
2271 ];
2272
2273 // Add the files
2274 $ops = [];
2275 foreach ( $files as $file ) {
2276 $this->prepare( [ 'dir' => dirname( $file ) ] );
2277 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
2278 }
2279 $status = $this->backend->doQuickOperations( $ops );
2280 $this->assertGoodStatus( $status,
2281 "Creation of files succeeded ($backendName)." );
2282 $this->assertEquals( true, $status->isOK(),
2283 "Creation of files succeeded with OK status ($backendName)." );
2284
2285 $this->assertEquals( true,
2286 $this->backend->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] ),
2287 "Directory exists in ($backendName)." );
2288 $this->assertEquals( true,
2289 $this->backend->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] ),
2290 "Directory exists in ($backendName)." );
2291 $this->assertEquals( false,
2292 $this->backend->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ] ),
2293 "Directory does not exists in ($backendName)." );
2294
2295 // Expected listing
2296 $expected = [
2297 "e",
2298 ];
2299 sort( $expected );
2300
2301 // Actual listing (no trailing slash)
2302 $list = [];
2303 $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1" ] );
2304 foreach ( $iter as $file ) {
2305 $list[] = $file;
2306 }
2307 sort( $list );
2308
2309 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2310
2311 // Expected listing
2312 $expected = [
2313 "subdir1",
2314 "subdir2",
2315 "subdir3",
2316 "subdir4",
2317 ];
2318 sort( $expected );
2319
2320 // Actual listing (no trailing slash)
2321 $list = [];
2322 $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e" ] );
2323 foreach ( $iter as $file ) {
2324 $list[] = $file;
2325 }
2326 sort( $list );
2327
2328 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2329
2330 // Actual listing (with trailing slash)
2331 $list = [];
2332 $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/" ] );
2333 foreach ( $iter as $file ) {
2334 $list[] = $file;
2335 }
2336 sort( $list );
2337
2338 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2339
2340 // Expected listing
2341 $expected = [
2342 "subdir",
2343 ];
2344 sort( $expected );
2345
2346 // Actual listing (no trailing slash)
2347 $list = [];
2348 $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir2" ] );
2349 foreach ( $iter as $file ) {
2350 $list[] = $file;
2351 }
2352 sort( $list );
2353
2354 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2355
2356 // Actual listing (with trailing slash)
2357 $list = [];
2358 $iter = $this->backend->getTopDirectoryList(
2359 [ 'dir' => "$base/unittest-cont1/e/subdir2/" ]
2360 );
2361
2362 foreach ( $iter as $file ) {
2363 $list[] = $file;
2364 }
2365 sort( $list );
2366
2367 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
2368
2369 // Actual listing (using iterator second time)
2370 $list = [];
2371 foreach ( $iter as $file ) {
2372 $list[] = $file;
2373 }
2374 sort( $list );
2375
2376 $this->assertEquals(
2377 $expected,
2378 $list,
2379 "Correct top dir listing ($backendName), second iteration."
2380 );
2381
2382 // Expected listing (recursive)
2383 $expected = [
2384 "e",
2385 "e/subdir1",
2386 "e/subdir2",
2387 "e/subdir3",
2388 "e/subdir4",
2389 "e/subdir2/subdir",
2390 "e/subdir3/subdir",
2391 "e/subdir4/subdir",
2392 "e/subdir4/subdir/sub",
2393 ];
2394 sort( $expected );
2395
2396 // Actual listing (recursive)
2397 $list = [];
2398 $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/" ] );
2399 foreach ( $iter as $file ) {
2400 $list[] = $file;
2401 }
2402 sort( $list );
2403
2404 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2405
2406 // Expected listing (recursive)
2407 $expected = [
2408 "subdir",
2409 "subdir/sub",
2410 ];
2411 sort( $expected );
2412
2413 // Actual listing (recursive)
2414 $list = [];
2415 $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir4" ] );
2416 foreach ( $iter as $file ) {
2417 $list[] = $file;
2418 }
2419 sort( $list );
2420
2421 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2422
2423 // Actual listing (recursive, second time)
2424 $list = [];
2425 foreach ( $iter as $file ) {
2426 $list[] = $file;
2427 }
2428 sort( $list );
2429
2430 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
2431
2432 $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] );
2433 $items = $this->listToArray( $iter );
2434 $this->assertEquals( [], $items, "Directory listing is empty." );
2435
2436 foreach ( $files as $file ) { // clean up
2437 $this->backend->doOperation( [ 'op' => 'delete', 'src' => $file ] );
2438 }
2439
2440 $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
2441 foreach ( $iter as $file ) {
2442 // no errors
2443 }
2444
2445 $items = $this->listToArray( $iter );
2446 $this->assertEquals( [], $items, "Directory listing is empty." );
2447
2448 $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/not/exists" ] );
2449 $items = $this->listToArray( $iter );
2450 $this->assertEquals( [], $items, "Directory listing is empty." );
2451 }
2452
2453 public function testLockCalls() {
2454 $this->backend = $this->singleBackend;
2455 $this->doTestLockCalls();
2456 }
2457
2458 private function doTestLockCalls() {
2459 $backendName = $this->backendClass();
2460 $base = $this->backend->getContainerStoragePath( 'test' );
2461
2462 $paths = [
2463 "$base/test1.txt",
2464 "$base/test2.txt",
2465 "$base/test3.txt",
2466 "$base/subdir1",
2467 "$base/subdir1", // duplicate
2468 "$base/subdir1/test1.txt",
2469 "$base/subdir1/test2.txt",
2470 "$base/subdir2",
2471 "$base/subdir2", // duplicate
2472 "$base/subdir2/test3.txt",
2473 "$base/subdir2/test4.txt",
2474 "$base/subdir2/subdir",
2475 "$base/subdir2/subdir/test1.txt",
2476 "$base/subdir2/subdir/test2.txt",
2477 "$base/subdir2/subdir/test3.txt",
2478 "$base/subdir2/subdir/test4.txt",
2479 "$base/subdir2/subdir/test5.txt",
2480 "$base/subdir2/subdir/sub",
2481 "$base/subdir2/subdir/sub/test0.txt",
2482 "$base/subdir2/subdir/sub/120-px-file.txt",
2483 ];
2484
2485 for ( $i = 0; $i < 2; $i++ ) {
2486 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
2487 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2488 "Locking of files succeeded ($backendName) ($i)." );
2489 $this->assertEquals( true, $status->isOK(),
2490 "Locking of files succeeded with OK status ($backendName) ($i)." );
2491
2492 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
2493 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2494 "Locking of files succeeded ($backendName) ($i)." );
2495 $this->assertEquals( true, $status->isOK(),
2496 "Locking of files succeeded with OK status ($backendName) ($i)." );
2497
2498 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
2499 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2500 "Locking of files succeeded ($backendName) ($i)." );
2501 $this->assertEquals( true, $status->isOK(),
2502 "Locking of files succeeded with OK status ($backendName) ($i)." );
2503
2504 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_EX );
2505 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2506 "Locking of files succeeded ($backendName). ($i)" );
2507 $this->assertEquals( true, $status->isOK(),
2508 "Locking of files succeeded with OK status ($backendName) ($i)." );
2509
2510 # # Flip the acquire/release ordering around ##
2511
2512 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
2513 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2514 "Locking of files succeeded ($backendName) ($i)." );
2515 $this->assertEquals( true, $status->isOK(),
2516 "Locking of files succeeded with OK status ($backendName) ($i)." );
2517
2518 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
2519 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2520 "Locking of files succeeded ($backendName) ($i)." );
2521 $this->assertEquals( true, $status->isOK(),
2522 "Locking of files succeeded with OK status ($backendName) ($i)." );
2523
2524 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_EX );
2525 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2526 "Locking of files succeeded ($backendName). ($i)" );
2527 $this->assertEquals( true, $status->isOK(),
2528 "Locking of files succeeded with OK status ($backendName) ($i)." );
2529
2530 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
2531 $this->assertEquals( print_r( [], true ), print_r( $status->getErrors(), true ),
2532 "Locking of files succeeded ($backendName) ($i)." );
2533 $this->assertEquals( true, $status->isOK(),
2534 "Locking of files succeeded with OK status ($backendName) ($i)." );
2535 }
2536
2537 $status = Status::newGood();
2538 $sl = $this->backend->getScopedFileLocks( $paths, LockManager::LOCK_EX, $status );
2539 $this->assertInstanceOf( ScopedLock::class, $sl,
2540 "Scoped locking of files succeeded ($backendName)." );
2541 $this->assertEquals( [], $status->getErrors(),
2542 "Scoped locking of files succeeded ($backendName)." );
2543 $this->assertEquals( true, $status->isOK(),
2544 "Scoped locking of files succeeded with OK status ($backendName)." );
2545
2546 ScopedLock::release( $sl );
2547 $this->assertNull( $sl,
2548 "Scoped unlocking of files succeeded ($backendName)." );
2549 $this->assertEquals( [], $status->getErrors(),
2550 "Scoped unlocking of files succeeded ($backendName)." );
2551 $this->assertEquals( true, $status->isOK(),
2552 "Scoped unlocking of files succeeded with OK status ($backendName)." );
2553 }
2554
2555 /**
2556 * @dataProvider provider_testGetContentType
2557 */
2558 public function testGetContentType( $mimeCallback, $mimeFromString ) {
2559 global $IP;
2560
2561 $be = TestingAccessWrapper::newFromObject( new MemoryFileBackend(
2562 [
2563 'name' => 'testing',
2564 'class' => MemoryFileBackend::class,
2565 'wikiId' => 'meow',
2566 'mimeCallback' => $mimeCallback
2567 ]
2568 ) );
2569
2570 $dst = 'mwstore://testing/container/path/to/file_no_ext';
2571 $src = "$IP/tests/phpunit/data/media/srgb.jpg";
2572 $this->assertEquals( 'image/jpeg', $be->getContentType( $dst, null, $src ) );
2573 $this->assertEquals(
2574 $mimeFromString ? 'image/jpeg' : 'unknown/unknown',
2575 $be->getContentType( $dst, file_get_contents( $src ), null ) );
2576
2577 $src = "$IP/tests/phpunit/data/media/Png-native-test.png";
2578 $this->assertEquals( 'image/png', $be->getContentType( $dst, null, $src ) );
2579 $this->assertEquals(
2580 $mimeFromString ? 'image/png' : 'unknown/unknown',
2581 $be->getContentType( $dst, file_get_contents( $src ), null ) );
2582 }
2583
2584 public static function provider_testGetContentType() {
2585 return [
2586 [ null, false ],
2587 [ [ FileBackendGroup::singleton(), 'guessMimeInternal' ], true ]
2588 ];
2589 }
2590
2591 public function testReadAffinity() {
2592 $be = TestingAccessWrapper::newFromObject(
2593 new FileBackendMultiWrite( [
2594 'name' => 'localtesting',
2595 'wikiId' => wfWikiID() . mt_rand(),
2596 'backends' => [
2597 [ // backend 0
2598 'name' => 'multitesting0',
2599 'class' => MemoryFileBackend::class,
2600 'isMultiMaster' => false,
2601 'readAffinity' => true
2602 ],
2603 [ // backend 1
2604 'name' => 'multitesting1',
2605 'class' => MemoryFileBackend::class,
2606 'isMultiMaster' => true
2607 ]
2608 ]
2609 ] )
2610 );
2611
2612 $this->assertEquals(
2613 1,
2614 $be->getReadIndexFromParams( [ 'latest' => 1 ] ),
2615 'Reads with "latest" flag use backend 1'
2616 );
2617 $this->assertSame(
2618 0,
2619 $be->getReadIndexFromParams( [ 'latest' => 0 ] ),
2620 'Reads without "latest" flag use backend 0'
2621 );
2622
2623 $p = 'container/test-cont/file.txt';
2624 $be->backends[0]->quickCreate( [
2625 'dst' => "mwstore://multitesting0/$p", 'content' => 'cattitude' ] );
2626 $be->backends[1]->quickCreate( [
2627 'dst' => "mwstore://multitesting1/$p", 'content' => 'princess of power' ] );
2628
2629 $this->assertEquals(
2630 'cattitude',
2631 $be->getFileContents( [ 'src' => "mwstore://localtesting/$p" ] ),
2632 "Non-latest read came from backend 0"
2633 );
2634 $this->assertEquals(
2635 'princess of power',
2636 $be->getFileContents( [ 'src' => "mwstore://localtesting/$p", 'latest' => 1 ] ),
2637 "Latest read came from backend1"
2638 );
2639 }
2640
2641 public function testAsyncWrites() {
2642 $be = TestingAccessWrapper::newFromObject(
2643 new FileBackendMultiWrite( [
2644 'name' => 'localtesting',
2645 'wikiId' => wfWikiID() . mt_rand(),
2646 'backends' => [
2647 [ // backend 0
2648 'name' => 'multitesting0',
2649 'class' => MemoryFileBackend::class,
2650 'isMultiMaster' => false
2651 ],
2652 [ // backend 1
2653 'name' => 'multitesting1',
2654 'class' => MemoryFileBackend::class,
2655 'isMultiMaster' => true
2656 ]
2657 ],
2658 'replication' => 'async'
2659 ] )
2660 );
2661
2662 $this->setMwGlobals( 'wgCommandLineMode', false );
2663
2664 $p = 'container/test-cont/file.txt';
2665 $be->quickCreate( [
2666 'dst' => "mwstore://localtesting/$p", 'content' => 'cattitude' ] );
2667
2668 $this->assertEquals(
2669 false,
2670 $be->backends[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
2671 "File not yet written to backend 0"
2672 );
2673 $this->assertEquals(
2674 'cattitude',
2675 $be->backends[1]->getFileContents( [ 'src' => "mwstore://multitesting1/$p" ] ),
2676 "File already written to backend 1"
2677 );
2678
2679 DeferredUpdates::doUpdates();
2680
2681 $this->assertEquals(
2682 'cattitude',
2683 $be->backends[0]->getFileContents( [ 'src' => "mwstore://multitesting0/$p" ] ),
2684 "File now written to backend 0"
2685 );
2686 }
2687
2688 public function testSanitizeOpHeaders() {
2689 $be = TestingAccessWrapper::newFromObject( new MemoryFileBackend( [
2690 'name' => 'localtesting',
2691 'wikiId' => wfWikiID()
2692 ] ) );
2693
2694 $input = [
2695 'headers' => [
2696 'content-Disposition' => FileBackend::makeContentDisposition( 'inline', 'name' ),
2697 'Content-dUration' => 25.6,
2698 'X-LONG-VALUE' => str_pad( '0', 300 ),
2699 'CONTENT-LENGTH' => 855055,
2700 ]
2701 ];
2702 $expected = [
2703 'headers' => [
2704 'content-disposition' => FileBackend::makeContentDisposition( 'inline', 'name' ),
2705 'content-duration' => 25.6,
2706 'content-length' => 855055
2707 ]
2708 ];
2709
2710 Wikimedia\suppressWarnings();
2711 $actual = $be->sanitizeOpHeaders( $input );
2712 Wikimedia\restoreWarnings();
2713
2714 $this->assertEquals( $expected, $actual, "Header sanitized properly" );
2715 }
2716
2717 // helper function
2718 private function listToArray( $iter ) {
2719 return is_array( $iter ) ? $iter : iterator_to_array( $iter );
2720 }
2721
2722 // test helper wrapper for backend prepare() function
2723 private function prepare( array $params ) {
2724 return $this->backend->prepare( $params );
2725 }
2726
2727 // test helper wrapper for backend prepare() function
2728 private function create( array $params ) {
2729 $params['op'] = 'create';
2730
2731 return $this->backend->doQuickOperations( [ $params ] );
2732 }
2733
2734 function tearDownFiles() {
2735 $containers = [ 'unittest-cont1', 'unittest-cont2' ];
2736 foreach ( $containers as $container ) {
2737 $this->deleteFiles( $container );
2738 }
2739 }
2740
2741 private function deleteFiles( $container ) {
2742 $base = self::baseStorePath();
2743 $iter = $this->backend->getFileList( [ 'dir' => "$base/$container" ] );
2744 if ( $iter ) {
2745 foreach ( $iter as $file ) {
2746 $this->backend->quickDelete( [ 'src' => "$base/$container/$file" ] );
2747 }
2748 // free the directory, to avoid Permission denied under windows on rmdir
2749 unset( $iter );
2750 }
2751 $this->backend->clean( [ 'dir' => "$base/$container", 'recursive' => 1 ] );
2752 }
2753
2754 private function assertBackendPathsConsistent( array $paths, $okSyncStatus ) {
2755 if ( !$this->backend instanceof FileBackendMultiWrite ) {
2756 return;
2757 }
2758
2759 $status = $this->backend->consistencyCheck( $paths );
2760 if ( $okSyncStatus ) {
2761 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
2762 } else {
2763 $this->assertBadStatus( $status, "Files not synced: " . implode( ',', $paths ) );
2764 }
2765 }
2766
2767 private function assertGoodStatus( StatusValue $status, $msg ) {
2768 $this->assertEquals( print_r( [], 1 ), print_r( $status->getErrors(), 1 ), $msg );
2769 }
2770
2771 private function assertBadStatus( StatusValue $status, $msg ) {
2772 $this->assertNotEquals( print_r( [], 1 ), print_r( $status->getErrors(), 1 ), $msg );
2773 }
2774 }