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