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