1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
|
<?php
declare( strict_types = 1 );
namespace Wikimedia\Tests\FileBackend;
use Closure;
use InvalidArgumentException;
use LockManager;
use MediaWiki\FileBackend\FSFile\TempFSFileFactory;
use MediaWikiUnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\NullLogger;
use ScopedLock;
use StatusValue;
use Wikimedia\FileBackend\FileBackend;
use Wikimedia\ScopedCallback;
use Wikimedia\TestingAccessWrapper;
/**
* @covers \Wikimedia\FileBackend\FileBackend
*/
class FileBackendTest extends MediaWikiUnitTestCase {
/**
* createMock() stubs out all methods, which isn't desirable for testing an abstract base class,
* since we often want to test that the base class calls certain methods that the derived class
* is meant to override. getMockBuilder() can be set to override only certain methods, but then
* you have to manually specify all abstract methods or else it doesn't work.
* getMockForAbstractClass() automatically fills in stubs for the abstract methods, but by
* default doesn't allow overriding any other methods. So we have to write our own.
*
* @param string|array ...$args Zero or more of the following:
* - A nonempty associative array, interpreted as $config to be passed to the constructor. The
* 'name' and 'domainId' will be given default values if not present.
* - A nonempty indexed array or a string, interpreted as a list of methods to override.
* - An empty array, which is ignored.
* @return FileBackend|MockObject A mock with no methods overridden except those specified in
* $methodsToMock, and all abstract methods.
*/
private function newMockFileBackend( ...$args ): FileBackend {
$methodsToMock = [];
$config = [];
foreach ( $args as $arg ) {
if ( is_string( $arg ) ) {
$methodsToMock = [ $arg ];
} elseif ( is_array( $arg ) ) {
if ( isset( $arg[0] ) ) {
$methodsToMock = $arg;
} elseif ( $arg ) {
$config = $arg;
}
} else {
throw new InvalidArgumentException(
'Arguments must be strings or nonempty arrays' );
}
}
$config += [ 'name' => 'test_name' ];
if ( !array_key_exists( 'wikiId', $config ) ) {
$config += [ 'domainId' => '' ];
}
return $this->getMockBuilder( FileBackend::class )
->setConstructorArgs( [ $config ] )
->onlyMethods( $methodsToMock )
->getMockForAbstractClass();
}
/**
* @dataProvider provideConstruct_validName
*/
public function testConstruct_validName( $name ): void {
$this->newMockFileBackend( [ 'name' => $name ] );
// No exception
$this->assertTrue( true );
}
public static function provideConstruct_validName(): array {
return [
'simple' => [ 'foobar' ],
'dash and underscore' => [ 'foo_bar-baz' ],
'capital and numbers' => [ 'Duck-Car313' ],
'255 chars' => [ str_repeat( 'a', 255 ) ],
];
}
/**
* @dataProvider provideConstruct_invalidName
* @param mixed $name
*/
public function testConstruct_invalidName( $name ): void {
$this->expectException( InvalidArgumentException::class );
$this->expectExceptionMessage( "Backend name '$name' is invalid." );
$this->newMockFileBackend( [ 'name' => $name, 'domainId' => false ] );
}
public static function provideConstruct_invalidName(): array {
return [
'Empty string' => [ '' ],
'Illegal slash' => [ 'foo/bar' ],
'Illegal space' => [ 'foo bar' ],
'Illegal percent' => [ 'foo%20bar' ],
'256 chars' => [ str_repeat( 'a', 256 ) ],
'Bang' => [ '!' ],
'With space' => [ 'a b' ],
'False' => [ false ],
'Null' => [ null ],
'Positive float' => [ 13.402 ],
'Negative float' => [ -13.402 ],
'True' => [ true ],
'Positive integer' => [ 7 ],
'Zero integer' => [ 0 ],
'Zero float' => [ 0.0 ],
'Negative integer' => [ -7 ],
];
}
public function testConstruct_noName(): void {
$this->expectException( InvalidArgumentException::class );
$this->expectExceptionMessage( 'Backend name not specified' );
$this->getMockBuilder( FileBackend::class )
->setConstructorArgs( [ [] ] )
->getMock();
}
/**
* @dataProvider provideConstruct_validDomainId
*/
public function testConstruct_validDomainId( string $domainId ): void {
$this->newMockFileBackend( [ 'domainId' => $domainId ] );
// No exception
$this->assertTrue( true );
}
/**
* @dataProvider provideConstruct_validDomainId
*/
public function testConstruct_validWikiId( string $wikiId ): void {
$this->newMockFileBackend( [ 'wikiId' => $wikiId ] );
// No exception
$this->assertTrue( true );
}
public static function provideConstruct_validDomainId(): array {
return [
'Empty string' => [ '' ],
'1000 chars' => [ str_repeat( 'a', 1000 ) ],
'Null character' => [ "\0" ],
'Invalid UTF-8' => [ "\xff" ],
];
}
/**
* @dataProvider provideConstruct_invalidDomainId
*/
public function testConstruct_invalidDomainId( $domainId ): void {
$this->expectException( InvalidArgumentException::class );
$this->expectExceptionMessage( "Backend domain ID not provided for 'test_name'." );
$this->newMockFileBackend( [ 'domainId' => $domainId ] );
}
public static function provideConstruct_invalidDomainId(): array {
return [
// We don't include null because that will fall back to wikiId
'False' => [ false ],
'True' => [ true ],
'Integer' => [ 7 ],
'Function' => [ static function () {
} ],
'Float' => [ -13.402 ],
'Object' => [ (object)[] ],
'Array' => [ [] ],
];
}
/**
* @dataProvider provideConstruct_invalidWikiId
*/
public function testConstruct_invalidWikiId( $wikiId ): void {
$this->expectException( InvalidArgumentException::class );
$this->expectExceptionMessage( "Backend domain ID not provided for 'test_name'." );
$this->newMockFileBackend( [ 'wikiId' => $wikiId ] );
}
public static function provideConstruct_invalidWikiId(): array {
return [
'Null' => [ null ],
] + self::provideConstruct_invalidDomainId();
}
public function testConstruct_noDomainId(): void {
$this->expectException( InvalidArgumentException::class );
$this->expectExceptionMessage( "Backend domain ID not provided for 'test_name'" );
$this->getMockBuilder( FileBackend::class )
->setConstructorArgs( [ [ 'name' => 'test_name' ] ] )
->getMock();
}
/**
* @dataProvider provideConstruct_properties
* @param string $property
* @param mixed $expected
* @param array $config Can also include the key 'inexact' to tell us to not check equality
* strictly.
*/
public function testConstruct_properties(
string $property, $expected, array $config = []
): void {
$backend = $this->newMockFileBackend( $config );
if ( $expected instanceof Closure ) {
$expected = $expected( $backend );
}
$assertMethod = isset( $config['inexact'] ) ? 'assertEquals' : 'assertSame';
unset( $config['inexact'] );
// We need to test this for the sake of subclasses that actually use the property. There
// doesn't seem to be any better way to do it. It shouldn't be tested in the subclasses,
// because we're testing the behavior of this class' constructor. We could make our own
// subclass, but we'd have to stub 26 abstract methods.
$this->$assertMethod( $expected,
TestingAccessWrapper::newFromObject( $backend )->$property );
}
public static function provideConstruct_properties(): array {
$tmpFileFactory = new TempFSFileFactory( 'some_unique_path' );
return [
'parallelize default value' => [ 'parallelize', 'off' ],
'parallelize null' => [ 'parallelize', 'off', [ 'parallelize' => null ] ],
'parallelize cast to string' => [ 'parallelize', '1', [ 'parallelize' => true ] ],
'parallelize case-preserving' =>
[ 'parallelize', 'iMpLiCiT', [ 'parallelize' => 'iMpLiCiT' ] ],
'concurrency default value' => [ 'concurrency', 50 ],
'concurrency null' => [ 'concurrency', 50, [ 'concurrency' => null ] ],
'concurrency cast to int' => [ 'concurrency', 51, [ 'concurrency' => '51x' ] ],
'obResetFunc default value' =>
[ 'obResetFunc', [ FileBackend::class, 'resetOutputBufferTheDefaultWay' ] ],
'obResetFunc null' => [
'obResetFunc',
[ FileBackend::class, 'resetOutputBufferTheDefaultWay' ],
[ 'obResetFunc' => null ]
],
'obResetFunc set' => [
'obResetFunc',
'wfSomeImaginaryFunction',
[ 'obResetFunc' => 'wfSomeImaginaryFunction' ]
],
'headerFunc default value' => [ 'headerFunc', 'header' ],
'headerFunc set' => [ 'headerFunc', 'myHeaderFunc', [ 'headerFunc' => 'myHeaderFunc' ] ],
'profiler default value' => [ 'profiler', null ],
'profiler not callable' => [ 'profiler', null, [ 'profiler' => '!' ] ],
'logger default value' => [ 'logger', new NullLogger, [ 'inexact' => true ] ],
'logger set' => [ 'logger', 'abcd', [ 'logger' => 'abcd' ] ],
'statusWrapper default value' => [ 'statusWrapper', null ],
'tmpFileFactory default value' =>
[ 'tmpFileFactory', new TempFSFileFactory, [ 'inexact' => true ] ],
'tmpDirectory null' => [ 'tmpFileFactory', new TempFSFileFactory,
[ 'tmpDirectory' => null, 'inexact' => true ] ],
'tmpDirectory set' => [ 'tmpFileFactory', new TempFSFileFactory( 'dir' ),
[ 'tmpDirectory' => 'dir', 'inexact' => true ] ],
'tmpFileFactory null' => [ 'tmpFileFactory', new TempFSFileFactory,
[ 'tmpFileFactory' => null, 'inexact' => true ] ],
'tmpFileFactory set' => [ 'tmpFileFactory', $tmpFileFactory,
[ 'tmpFileFactory' => $tmpFileFactory ] ],
'tmpDirectory and tmpFileFactory set' => [
'tmpFileFactory',
new TempFSFileFactory( 'dir' ),
[ 'tmpDirectory' => 'dir', 'tmpFileFactory' => $tmpFileFactory, 'inexact' => true ],
],
'tmpDirectory null and tmpFileFactory set' => [ 'tmpFileFactory', $tmpFileFactory,
[ 'tmpDirectory' => null, 'tmpFileFactory' => $tmpFileFactory ] ],
];
}
public function testSetLogger(): void {
$backend = $this->newMockFileBackend();
$logger = new NullLogger;
// See comment in testConstruct_properties about use of TestingAccessWrapper.
$this->assertNotSame( $logger, TestingAccessWrapper::newFromObject( $backend )->logger );
$backend->setLogger( $logger );
$this->assertSame( $logger, TestingAccessWrapper::newFromObject( $backend )->logger );
}
public function testGetName(): void {
$backend = $this->newMockFileBackend();
$this->assertSame( 'test_name', $backend->getName() );
}
/**
* @dataProvider provideGetDomainId
*/
public function testGetDomainId( array $config ): void {
$backend = $this->newMockFileBackend( $config );
$this->assertSame( 'test_domain', $backend->getDomainId() );
}
/**
* @dataProvider provideGetDomainId
*/
public function testGetWikiId( array $config ): void {
$backend = $this->newMockFileBackend( $config );
$this->assertSame( 'test_domain', $backend->getWikiId() );
}
public static function provideGetDomainId(): array {
return [
'Only domainId' => [ [ 'domainId' => 'test_domain' ] ],
'Only wikiId' => [ [ 'wikiId' => 'test_domain' ] ],
'null domainId' => [ [ 'domainId' => null, 'wikiId' => 'test_domain' ] ],
'wikiId is ignored if domainId is present' =>
[ [ 'domainId' => 'test_domain', 'wikiId' => 'other_domain' ] ],
];
}
public function testIsReadOnly_default(): void {
$backend = $this->newMockFileBackend();
$this->assertFalse( $backend->isReadOnly() );
$this->assertFalse( $backend->getReadOnlyReason() );
}
public function testIsReadOnly(): void {
$backend = $this->newMockFileBackend( [ 'readOnly' => '.' ] );
$this->assertTrue( $backend->isReadOnly() );
$this->assertSame( '.', $backend->getReadOnlyReason() );
}
public function testGetFeatures(): void {
$backend = $this->newMockFileBackend();
$this->assertSame( FileBackend::ATTR_UNICODE_PATHS, $backend->getFeatures() );
}
/**
* @dataProvider provideHasFeatures
*/
public function testHasFeatures(
bool $expected, int $actualFeatures, int $testedFeatures
): void {
$backend = $this->createMock( FileBackend::class );
$backend->method( 'getFeatures' )->willReturn( $actualFeatures );
$this->assertSame( $expected, $backend->hasFeatures( $testedFeatures ) );
}
public static function provideHasFeatures(): array {
return [
'Nothing has nothing' => [ true, 0, 0 ],
"Nothing doesn't have something" => [ false, 0, 1 ],
'Something has nothing' => [ true, 1, 0 ],
'Something has itself' => [ true, 1, 1 ],
"Something doesn't have something else" => [ false, 0b01, 0b10 ],
"Something doesn't have itself and something else" => [ false, 0b01, 0b11 ],
'Two things have the first one' => [ true, 0b11, 0b01 ],
'Two things have the second one' => [ true, 0b11, 0b10 ],
'Two things have both' => [ true, 0b11, 0b11 ],
"Two things don't have a third" => [ false, 0b11, 0b100 ],
];
}
/**
* @dataProvider provideReadOnly
*/
public function testReadOnly( string $method ): void {
$backend = $this->newMockFileBackend( [ 'readOnly' => '.' ] );
$status = $backend->$method( [] );
$this->assertStatusError( 'backend-fail-readonly', $status );
}
public static function provideReadOnly(): array {
return [
'doOperations' => [ 'doOperations', 'doOperationsInternal', [ [ [] ] ] ],
'doOperation' => [ 'doOperation', 'doOperationsInternal', [ [ 'op' => '' ] ] ],
'doQuickOperations' => [ 'doQuickOperations', 'doQuickOperationsInternal', [ [ [] ] ] ],
'doQuickOperation' => [
'doQuickOperation',
'doQuickOperationsInternal',
[ [ 'op' => '' ] ]
],
'prepare' => [ 'prepare', 'doPrepare' ],
'secure' => [ 'secure', 'doSecure' ],
'publish' => [ 'publish', 'doPublish' ],
'clean' => [ 'clean', 'doClean' ],
];
}
/**
* @dataProvider provideReadOnly
* @param string $method Method to call
* @param string $internalMethod Internal method the call will be forwarded to
* @param array $args To be passed to $method before a final argument of
* [ 'bypassReadOnly' => true ]
*/
public function testDoOperations_bypassReadOnly(
string $method, string $internalMethod, array $args = []
): void {
$backend = $this->newMockFileBackend( [ 'readOnly' => '.' ], $internalMethod );
$backend->expects( $this->once() )->method( $internalMethod )
->willReturn( StatusValue::newGood( 'myvalue' ) );
$status = $backend->$method( ...array_merge( $args, [ [ 'bypassReadOnly' => true ] ] ) );
$this->assertStatusGood( $status );
$this->assertStatusValue( 'myvalue', $status );
}
/**
* @dataProvider provideDoMultipleOperations
*/
public function testDoOperations_noOp( string $method ): void {
$backend = $this->newMockFileBackend(
[ 'doOperationsInternal', 'doQuickOperationsInternal' ] );
$backend->expects( $this->never() )->method( 'doOperationsInternal' );
$backend->expects( $this->never() )->method( 'doQuickOperationsInternal' );
$status = $backend->$method( [] );
$this->assertStatusGood( $status );
}
public static function provideDoMultipleOperations(): array {
return [
'doOperations' => [ 'doOperations' ],
'doQuickOperations' => [ 'doQuickOperations' ],
];
}
/**
* @dataProvider provideDoOperations
* @param string $method 'doOperation' or 'doOperations'
*/
public function testDoOperations_nonLockingNoForce( string $method ): void {
$backend = $this->newMockFileBackend( [ 'doOperationsInternal' ] );
$backend->expects( $this->once() )->method( 'doOperationsInternal' )
->with( [ [] ], [] );
$backend->$method( $method === 'doOperation' ? [] : [ [] ], [ 'nonLocking' => true ] );
}
public static function provideDoOperations(): array {
return [
'doOperations' => [ 'doOperations' ],
'doOperation' => [ 'doOperation' ],
];
}
/**
* @dataProvider provideDoOperations
* @param string $method 'doOperation' or 'doOperations'
*/
public function testDoOperations_nonLockingForce( string $method ): void {
$backend = $this->newMockFileBackend( [ 'doOperationsInternal' ] );
$backend->expects( $this->once() )->method( 'doOperationsInternal' )
->with( [ [] ], [ 'nonLocking' => true, 'force' => true ] );
$backend->$method(
$method === 'doOperation' ? [] : [ [] ],
[ 'nonLocking' => true, 'force' => true ]
);
}
// XXX Can't test newScopedIgnoreUserAbort() because it's a no-op in CLI
/**
* @dataProvider provideAction
* @param string $prefix '' or 'quick'
* @param string $action
*/
public function testAction( string $prefix, string $action ): void {
$backend = $this->newMockFileBackend( 'do' . ucfirst( $prefix ) . 'OperationsInternal' );
$expectedOp = [ 'op' => $action, 'foo' => 'bar' ];
if ( $prefix === 'quick' ) {
$expectedOp['overwrite'] = true;
}
$backend->expects( $this->once() )
->method( 'do' . ucfirst( $prefix ) . 'OperationsInternal' )
->with( [ $expectedOp ], [ 'baz' => 'quuz' ] )
->willReturn( StatusValue::newGood( 'myvalue' ) );
$method = $prefix ? $prefix . ucfirst( $action ) : $action;
$status = $backend->$method( [ 'op' => 'ignored', 'foo' => 'bar' ], [ 'baz' => 'quuz' ] );
$this->assertStatusOK( $status );
$this->assertStatusValue( 'myvalue', $status );
}
public static function provideAction(): array {
$ret = [];
foreach ( [ '', 'quick' ] as $prefix ) {
foreach ( [ 'create', 'store', 'copy', 'move', 'delete', 'describe' ] as $action ) {
$key = $prefix ? $prefix . ucfirst( $action ) : $action;
$ret[$key] = [ $prefix, $action ];
}
}
return $ret;
}
/**
* @dataProvider provideForwardToDo
*/
public function testForwardToDo( string $method ): void {
$backend = $this->newMockFileBackend( 'do' . ucfirst( $method ) );
$backend->expects( $this->once() )->method( 'do' . ucfirst( $method ) )
->with( [ 'foo' => 'bar' ] )
->willReturn( StatusValue::newGood( 'myvalue' ) );
$status = $backend->$method( [ 'foo' => 'bar' ] );
$this->assertStatusGood( $status );
$this->assertStatusValue( 'myvalue', $status );
}
public static function provideForwardToDo(): array {
return [
'prepare' => [ 'prepare' ],
'secure' => [ 'secure' ],
'publish' => [ 'publish' ],
'clean' => [ 'clean' ],
];
}
/**
* @dataProvider provideForwardToMulti
*/
public function testForwardToMulti( string $method ): void {
$backend = $this->newMockFileBackend( "{$method}Multi" );
$backend->expects( $this->once() )->method( "{$method}Multi" )
->with( [ 'srcs' => [ 'mysrc' ], 'foo' => 'bar', 'src' => 'mysrc' ] )
->willReturn( [ 'mysrc' => 'mycontents' ] );
$result = $backend->$method( [ 'srcs' => 'ignored', 'foo' => 'bar', 'src' => 'mysrc' ] );
$this->assertSame( 'mycontents', $result );
}
public static function provideForwardToMulti(): array {
return [
'getFileContents' => [ 'getFileContents' ],
'getLocalReference' => [ 'getLocalReference' ],
'getLocalCopy' => [ 'getLocalCopy' ],
];
}
/**
* @dataProvider provideForwardFromTop
*/
public function testForwardFromTop( string $methodSuffix ): void {
$backend = $this->newMockFileBackend( "get$methodSuffix" );
$backend->expects( $this->once() )->method( "get$methodSuffix" )
->with( [ 'topOnly' => true, 'foo' => 'bar' ] )
->willReturn( [ 'something' ] );
$method = "getTop$methodSuffix";
$result = $backend->$method( [ 'topOnly' => 'ignored', 'foo' => 'bar' ] );
$this->assertSame( [ 'something' ], $result );
}
public static function provideForwardFromTop(): array {
return [
'getTopDirectoryList' => [ 'DirectoryList' ],
'getTopFileList' => [ 'FileList' ],
];
}
/**
* @dataProvider provideLockUnlockFiles
* @param string $method
* @param int|null $timeout Only relevant for lockFiles
*/
public function testLockUnlockFiles( string $method, ?int $timeout = null ): void {
$args = [ [ 'mwstore://a/b/', 'mwstore://c/d//e' ], LockManager::LOCK_SH ];
$mockLm = $this->getMockBuilder( LockManager::class )
->disableOriginalConstructor()
->onlyMethods( [ 'do' . ucfirst( $method ) . 'ByType', 'doLock', 'doUnlock' ] )
->getMock();
// XXX PHPUnit can't override final methods (T231419)
//$mockLm->expects( $this->once() )->method( $method )
// ->with( ...array_merge( $args, [ $timeout ?? 0 ] ) )
// ->willReturn( StatusValue::newGood( 'myvalue' ) );
//$mockLm->expects( $this->never() )->method( $this->anythingBut( $method ) );
$mockLm->expects( $this->once() )->method( 'do' . ucfirst( $method ) . 'ByType' )
->with( [ LockManager::LOCK_SH => [ 'mwstore://a/b', 'mwstore://c/d/e' ] ] )
->willReturn( StatusValue::newGood( 'myvalue' ) );
$backend = $this->newMockFileBackend( [ 'lockManager' => $mockLm ] );
$backendMethod = "{$method}Files";
$status = $backend->$backendMethod( ...array_merge( $args, (array)$timeout ) );
$this->assertStatusGood( $status );
$this->assertStatusValue( 'myvalue', $status );
}
public static function provideLockUnlockFiles(): array {
return [
[ 'lock' ],
[ 'lock', 731 ],
[ 'unlock' ],
];
}
/**
* @dataProvider provideGetScopedFileLocks
* @param array $paths
* @param int|string $type
* @param array $expectedPathsByType Expected to be passed to the LockManager
* @param StatusValue $lockStatus Returned from doLockByType()
* @param StatusValue|null $unlockStatus Returned from doUnlockByType() (if locking succeeded)
*/
public function testGetScopedFileLocks(
array $paths, $type, array $expectedPathsByType, StatusValue $lockStatus,
?StatusValue $unlockStatus = null
): void {
$mockLm = $this->getMockBuilder( LockManager::class )
->disableOriginalConstructor()
->onlyMethods( [ 'doLockByType', 'doUnlockByType', 'doLock', 'doUnlock' ] )
->getMock();
$mockLm->expects( $this->once() )->method( 'doLockByType' )
->with( $expectedPathsByType )
->willReturn( $lockStatus );
$mockLm->expects( $this->exactly( $unlockStatus ? 1 : 0 ) )->method( 'doUnlockByType' )
->with( $expectedPathsByType )
->willReturn( $unlockStatus );
$backend = $this->newMockFileBackend( [ 'lockManager' => $mockLm ] );
$status = StatusValue::newGood( 'myvalue' );
$scopedLock = $backend->getScopedFileLocks( $paths, $type, $status );
$this->assertStatusValue( 'myvalue', $status );
$this->assertSame( $lockStatus->isOK(), $status->isOK() );
$this->assertStatusMessagesExactly( $lockStatus, $status );
if ( !$lockStatus->isOK() ) {
$this->assertNull( $scopedLock );
return;
}
$this->assertInstanceOf( ScopedLock::class, $scopedLock );
unset( $scopedLock );
$this->assertStatusValue( 'myvalue', $status );
$this->assertSame( $lockStatus->isOK(), $status->isOK() );
$this->assertStatusMessagesExactly( $lockStatus->merge( $unlockStatus ), $status );
}
public static function provideGetScopedFileLocks(): array {
return [
'Simple successful shared lock' => [
[ 'mwstore://a/b/' ], LockManager::LOCK_SH,
[ LockManager::LOCK_SH => [ 'mwstore://a/b' ] ],
StatusValue::newGood( 'value2' ), StatusValue::newGood( 'value3' ),
],
'Mixed lock' => [
[ LockManager::LOCK_SH => [ 'mwstore://a/b/' ],
LockManager::LOCK_EX => [ 'mwstore://c/d//e' ] ], 'mixed',
[ LockManager::LOCK_SH => [ 'mwstore://a/b' ],
LockManager::LOCK_EX => [ 'mwstore://c/d/e' ] ],
StatusValue::newGood(), StatusValue::newGood(),
],
'Mixed with only shared locks' => [
[ LockManager::LOCK_SH => [ 'mwstore://a/b/', 'mwstore://c/d//e' ] ], 'mixed',
[ LockManager::LOCK_SH => [ 'mwstore://a/b', 'mwstore://c/d/e' ] ],
StatusValue::newGood(), StatusValue::newGood(),
],
'Locking error' => [
[ 'mwstore://a/b/' ], LockManager::LOCK_EX,
[ LockManager::LOCK_EX => [ 'mwstore://a/b' ] ],
StatusValue::newFatal( 'XXX' ),
],
'Unlocking error' => [
[ 'mwstore://a/b/', 'mwstore://c/d//e' ], LockManager::LOCK_EX,
[ LockManager::LOCK_EX => [ 'mwstore://a/b', 'mwstore://c/d/e' ] ],
StatusValue::newGood(), StatusValue::newFatal( 'XXXX' ),
],
];
}
/**
* @dataProvider provideConstruct_validName
* @param mixed $name
*/
public function testGetRootStoragePath( $name ): void {
$backend = $this->newMockFileBackend( [ 'name' => $name ] );
$this->assertSame( "mwstore://$name", $backend->getRootStoragePath() );
}
/**
* @dataProvider provideConstruct_validName
*/
public function testGetContainerStoragePath( $name ): void {
$backend = $this->newMockFileBackend( [ 'name' => $name ] );
$this->assertSame( "mwstore://$name/mycontainer",
$backend->getContainerStoragePath( 'mycontainer' ) );
}
/**
* @dataProvider provideIsStoragePath
*/
public function testIsStoragePath( string $path, bool $expected ): void {
$this->assertSame( $expected, FileBackend::isStoragePath( $path ) );
}
public static function provideIsStoragePath(): array {
$paths = [
'mwstore://' => true,
'mwstore://backend' => true,
'mwstore://backend/container' => true,
'mwstore://backend/container/' => true,
'mwstore://backend/container/path' => true,
'mwstore://backend//container/' => true,
'mwstore://backend//container//' => true,
'mwstore://backend//container//path' => true,
'mwstore:///' => true,
'mwstore:/' => false,
'mwstore:' => false,
];
$ret = [];
foreach ( $paths as $path => $expected ) {
$ret[$path] = [ $path, $expected ];
}
return $ret;
}
/**
* @dataProvider provideSplitStoragePath
*/
public function testSplitStoragePath( string $path, array $expected ): void {
$this->assertSame( $expected, FileBackend::splitStoragePath( $path ) );
}
public static function provideSplitStoragePath(): array {
$paths = [
'mwstore://backend/container' => [ 'backend', 'container', '' ],
'mwstore://backend/container/' => [ 'backend', 'container', '' ],
'mwstore://backend/container/path' => [ 'backend', 'container', 'path' ],
'mwstore://backend/container//path' => [ 'backend', 'container', '/path' ],
'mwstore://backend//container/path' => [ null, null, null ],
'mwstore://backend//container' => [ null, null, null ],
'mwstore://backend//container//path' => [ null, null, null ],
'mwstore://' => [ null, null, null ],
'mwstore://backend' => [ null, null, null ],
'mwstore:///' => [ null, null, null ],
'mwstore:/' => [ null, null, null ],
'mwstore:' => [ null, null, null ],
];
$ret = [];
foreach ( $paths as $path => $expected ) {
$ret[$path] = [ $path, $expected ];
}
return $ret;
}
/**
* @dataProvider provideNormalizeStoragePath
* @param string $path
* @param string|null $expected
*/
public function testNormalizeStoragePath( string $path, ?string $expected ): void {
$this->assertSame( $expected, FileBackend::normalizeStoragePath( $path ) );
}
public static function provideNormalizeStoragePath(): array {
$paths = [
'mwstore://backend/container' => 'mwstore://backend/container',
'mwstore://backend/container/' => 'mwstore://backend/container',
'mwstore://backend/container/path' => 'mwstore://backend/container/path',
'mwstore://backend/container//path' => 'mwstore://backend/container/path',
'mwstore://backend/container///path' => 'mwstore://backend/container/path',
'mwstore://backend/container///path//to///obj' =>
'mwstore://backend/container/path/to/obj',
'mwstore://' => null,
'mwstore://backend' => null,
'mwstore://backend//container' => null,
'mwstore://backend//container/path' => null,
'mwstore://backend//container//path' => null,
'mwstore:///' => null,
'mwstore:/' => null,
'mwstore:' => null,
];
$ret = [];
foreach ( $paths as $path => $expected ) {
$ret[$path] = [ $path, $expected ];
}
return $ret;
}
/**
* @dataProvider provideParentStoragePath
* @param string $path
* @param string|null $expected
*/
public function testParentStoragePath( string $path, ?string $expected ): void {
$this->assertSame( $expected, FileBackend::parentStoragePath( $path ) );
}
public static function provideParentStoragePath(): array {
$paths = [
'mwstore://backend/container/path/to/obj' => 'mwstore://backend/container/path/to',
'mwstore://backend/container/path/to' => 'mwstore://backend/container/path',
'mwstore://backend/container/path' => 'mwstore://backend/container',
'mwstore://backend/container' => null,
'mwstore://backend/container/path/to/obj/' => 'mwstore://backend/container/path/to',
'mwstore://backend/container/path/to/' => 'mwstore://backend/container/path',
'mwstore://backend/container/path/' => 'mwstore://backend/container',
'mwstore://backend/container/' => null,
];
$ret = [];
foreach ( $paths as $path => $expected ) {
$ret[$path] = [ $path, $expected ];
}
return $ret;
}
/**
* @dataProvider provideExtensionFromPath
*/
public function testExtensionFromPath( array $args, string $expected ): void {
$this->assertSame( $expected, FileBackend::extensionFromPath( ...$args ) );
}
public static function provideExtensionFromPath(): array {
$paths = [
'mwstore://backend/container/path.Txt' => 'Txt',
'mwstore://backend/container/path.svg.pNG' => 'pNG',
'mwstore://backend/container/path' => '',
'mwstore://backend/container/path.' => '',
];
$ret = [];
foreach ( $paths as $path => $expected ) {
$ret[$path] = [ [ $path ], strtolower( $expected ) ];
$ret["$path (lowercase)"] = [ [ $path, 'lowercase' ], strtolower( $expected ) ];
$ret["$path (uppercase)"] = [ [ $path, 'uppercase' ], strtoupper( $expected ) ];
$ret["$path (rawcase)"] = [ [ $path, 'rawcase' ], $expected ];
}
return $ret;
}
/**
* @dataProvider provideIsPathTraversalFree
*/
public function testIsPathTraversalFree( string $path, bool $expected ): void {
$this->assertSame( $expected, FileBackend::isPathTraversalFree( $path ) );
}
public static function provideIsPathTraversalFree(): array {
$traversalFree = [
'a\\b',
'a//b',
'/a',
'\\a//b/',
];
$hasTraversal = [];
$strippedPrefixes = [ '', '/', '//', '///', '\\', '\\\\', '\\\\\\' ];
$unstrippedPrefixes = [ '.', 'a', 'a/', '/a', ' ', "\0" ];
$suffixes = [ '', '.', 'a', '/', '/a', ' ', "\0" ];
foreach ( [ '.', '..' ] as $basePath ) {
foreach ( $strippedPrefixes as $prefix ) {
foreach ( $suffixes as $suffix ) {
if ( $suffix === '' ) {
$hasTraversal[] = "$prefix$basePath";
} else {
$traversalFree[] = "$prefix$basePath$suffix";
}
}
}
foreach ( $unstrippedPrefixes as $prefix ) {
foreach ( $suffixes as $suffix ) {
$traversalFree[] = "$prefix$basePath$suffix";
}
}
}
foreach ( [ './', '.\\', '../', '..\\' ] as $basePath ) {
foreach ( $strippedPrefixes as $prefix ) {
foreach ( $suffixes as $suffix ) {
$hasTraversal[] = "$prefix$basePath$suffix";
}
}
foreach ( $unstrippedPrefixes as $prefix ) {
foreach ( $suffixes as $suffix ) {
$traversalFree[] = "$prefix$basePath$suffix";
}
}
}
foreach (
[ '/./', '\\./', '/.\\', '\\.\\', '/../', '\\../', '/..\\', '\\..\\' ] as $basePath
) {
foreach ( array_merge( $strippedPrefixes, $unstrippedPrefixes ) as $prefix ) {
foreach ( $suffixes as $suffix ) {
$hasTraversal[] = "$prefix$basePath$suffix";
}
}
}
// Some things might be traversal-free vis-a-vis one base path but a traversal for another
$traversalFree = array_diff( $traversalFree, $hasTraversal );
$ret = [];
foreach ( $traversalFree as $path ) {
$ret[$path] = [ $path, true ];
}
foreach ( $hasTraversal as $path ) {
$ret[$path] = [ $path, false ];
}
return $ret;
}
/**
* @dataProvider provideMakeContentDisposition
*/
public function testMakeContentDisposition( array $args, string $expected ): void {
$this->assertSame( $expected, FileBackend::makeContentDisposition( ...$args ) );
}
public static function provideMakeContentDisposition(): array {
$tests = [
[ [ 'inline' ], 'inline' ],
[ [ 'inLINE' ], 'inline' ],
[ [ 'inLINE', '' ], 'inline' ],
[ [ 'attachment' ], 'attachment' ],
[ [ 'atTACHment' ], 'attachment' ],
[ [ 'atTACHment', '' ], 'attachment' ],
[ [ 'inline', 'filename.txt' ], "inline;filename*=UTF-8''filename.txt" ],
[ [ 'attachment', 'filename.txt' ], "attachment;filename*=UTF-8''filename.txt" ],
[ [ 'inline', 'path/filename!!!' ], "inline;filename*=UTF-8''filename%21%21%21" ],
];
$ret = [];
foreach ( $tests as [ $args, $expected ] ) {
$ret[implode( ', ', $args )] = [ $args, $expected ];
}
return $ret;
}
/**
* @dataProvider provideMakeContentDisposition_invalid
*/
public function testMakeContentDisposition_invalid( string ...$args ): void {
$this->expectException( InvalidArgumentException::class );
$this->expectExceptionMessage( "Invalid Content-Disposition type '{$args[0]}'." );
FileBackend::makeContentDisposition( ...$args );
}
public static function provideMakeContentDisposition_invalid(): array {
return [
[ 'foo' ],
[ 'foo', '' ],
[ 'foo', 'bar' ],
[ ' inline' ],
[ 'inline ' ],
];
}
/**
* @dataProvider provideDoOperations
* @param string $method 'doOperation' or 'doOperations'
*/
public function testResolveFSFileObjects( string $method ): void {
$tmpFile = ( new TempFSFileFactory )->newTempFSFile( 'a' );
$backend = $this->newMockFileBackend( 'doOperationsInternal' );
$backend->expects( $this->once() )->method( 'doOperationsInternal' )
->with( [ [ 'src' => $tmpFile->getPath(), 'srcRef' => $tmpFile ] ] )
->willReturn( StatusValue::newGood() );
$op = [ 'src' => $tmpFile ];
if ( $method === 'doOperations' ) {
$op = [ $op ];
}
$status = $backend->$method( $op );
$this->assertStatusGood( $status );
}
/**
* @dataProvider provideDoOperations
* @param string $method 'doOperation' or 'doOperations'
*/
public function testResolveFSFileObjects_preservesTempFiles( string $method ): void {
$tmpFile = ( new TempFSFileFactory )->newTempFSFile( 'a' );
$path = $tmpFile->getPath();
$backend = $this->newMockFileBackend();
$op = [ 'src' => $tmpFile ];
if ( $method === 'doOperations' ) {
$op = [ $op ];
}
$status = $backend->$method( $op );
$this->assertTrue( is_file( $path ) );
}
public function testWrapStatus(): void {
$expectedSv = StatusValue::newGood( 'myvalue' );
$backend = $this->newMockFileBackend( [ 'statusWrapper' =>
function ( StatusValue $sv ) use ( $expectedSv ): StatusValue {
$this->assertEquals( StatusValue::newGood(), $sv );
return $expectedSv;
}
] );
$this->assertSame( $expectedSv, $backend->doOperations( [] ) );
}
public function testScopedProfileSection(): void {
$scopedCallback = new ScopedCallback( static function () {
} );
$backend = $this->newMockFileBackend( [ 'profiler' =>
function ( string $section ) use ( $scopedCallback ): ScopedCallback {
$this->assertSame( 'mysection', $section );
return $scopedCallback;
}
] );
// See comment in testConstruct_properties about use of TestingAccessWrapper.
$this->assertSame( $scopedCallback,
TestingAccessWrapper::newFromObject( $backend )->scopedProfileSection( 'mysection' ) );
}
}
|