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 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
|
<?php
namespace MediaWiki\Tests\Unit\Permissions;
use DatabaseTestHelper;
use MediaWiki\Cache\LinkCache;
use MediaWiki\CommentStore\CommentStore;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\DAO\WikiAwareEntity;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\Linker\LinksMigration;
use MediaWiki\MainConfigNames;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Page\PageIdentityValue;
use MediaWiki\Page\PageReferenceValue;
use MediaWiki\Page\PageStore;
use MediaWiki\Permissions\RestrictionStore;
use MediaWiki\Tests\Unit\DummyServicesTrait;
use MediaWiki\Title\Title;
use MediaWikiUnitTestCase;
use MockTitleTrait;
use ReflectionClass;
use ReflectionMethod;
use RuntimeException;
use UnexpectedValueException;
use Wikimedia\Assert\PreconditionException;
use Wikimedia\ObjectCache\WANObjectCache;
use Wikimedia\Rdbms\DeleteQueryBuilder;
use Wikimedia\Rdbms\FakeResultWrapper;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\ILoadBalancer;
use Wikimedia\Rdbms\SelectQueryBuilder;
use Wikimedia\Rdbms\UnionQueryBuilder;
/**
* @covers \MediaWiki\Permissions\RestrictionStore
*/
class RestrictionStoreTest extends MediaWikiUnitTestCase {
use DummyServicesTrait;
use MockTitleTrait;
private const DEFAULT_RESTRICTION_TYPES = [ 'create', 'edit', 'move', 'upload' ];
/**
* @param array $expectedCalls E.g.:
* [
* DB_REPLICA => [
* 'update' => callback, # to be called exactly once
* 'delete' => [ callback, 2 ], # to be called exactly twice
* 'select' => [ callback, -1 ], # may be called 0 or more times
* ],
* ]
* @return ILoadBalancer
*/
private function newMockLoadBalancer( array $expectedCalls = [] ): ILoadBalancer {
if ( !isset( $expectedCalls[DB_REPLICA] ) ) {
$expectedCalls[DB_REPLICA] = [];
}
$expectedCalls[DB_REPLICA]['decodeExpiry'] = [
static function ( string $expiry ): string {
return $expiry;
},
-1
];
$dbs = [];
foreach ( $expectedCalls as $index => $calls ) {
$dbs[$index] = $this->createNoOpMock(
IDatabase::class,
array_merge( array_keys( $calls ), [ 'newSelectQueryBuilder', 'newDeleteQueryBuilder', 'newUnionQueryBuilder' ] )
);
foreach ( $calls as $method => $callback ) {
$count = 1;
if ( is_array( $callback ) ) {
[ $callback, $count ] = $callback;
}
$dbs[$index]->expects( $count < 0 ? $this->any() : $this->exactly( $count ) )
->method( $method )->willReturnCallback( $callback );
}
$dbs[$index]
->method( 'newSelectQueryBuilder' )
->willReturnCallback( static function () use ( $dbs, $index ) {
return new SelectQueryBuilder( $dbs[$index] );
} );
$dbs[$index]
->method( 'newUnionQueryBuilder' )
->willReturnCallback( static function () use ( $dbs, $index ) {
return new UnionQueryBuilder( $dbs[$index] );
} );
$dbs[$index]
->method( 'newDeleteQueryBuilder' )
->willReturnCallback( static function () use ( $dbs, $index ) {
return new DeleteQueryBuilder( $dbs[$index] );
} );
}
$lb = $this->createMock( ILoadBalancer::class, [ 'getConnection' ] );
$lb->method( 'getConnection' )->willReturnCallback(
function ( int $index ) use ( $dbs ): IDatabase {
$this->assertArrayHasKey( $index, $dbs );
return $dbs[$index];
}
);
return $lb;
}
private function newRestrictionStore( array $options = [] ): RestrictionStore {
return new RestrictionStore(
new ServiceOptions( RestrictionStore::CONSTRUCTOR_OPTIONS, $options + [
MainConfigNames::NamespaceProtection => [],
MainConfigNames::RestrictionLevels => [ '', 'autoconfirmed', 'sysop' ],
MainConfigNames::RestrictionTypes => self::DEFAULT_RESTRICTION_TYPES,
MainConfigNames::SemiprotectedRestrictionLevels => [ 'autoconfirmed' ],
] ),
$this->createNoOpMock( WANObjectCache::class ),
$this->newMockLoadBalancer( $options['db'] ?? [] ),
// @todo test that these calls work correctly
$this->createNoOpMock( LinkCache::class, [ 'addLinkObj', 'getGoodLinkFieldObj' ] ),
$this->createNoOpMock( LinksMigration::class, [ 'getLinksConditions' ] ),
$this->getDummyCommentStore(),
$this->createHookContainer( isset( $options['hookFn'] )
? [ 'TitleGetRestrictionTypes' => $options['hookFn'] ]
: [] ),
$this->createNoOpMock( PageStore::class )
);
}
private static function newImproperPageIdentity(
int $ns, string $dbKey, $wikiId = PageIdentity::LOCAL
): PageIdentity {
// PageIdentityValue doesn't allow negative namespaces, and Title::exists accesses services
// for hooks, so we need another solution to unit-test PageIdentity objects with negative
// namespaces (until they cease to exist).
return new class( $ns, $dbKey, $wikiId ) extends PageReferenceValue implements PageIdentity {
public function getId( $wikiId = PageIdentity::LOCAL ): int {
throw new RuntimeException;
}
public function canExist(): bool {
return false;
}
public function exists(): bool {
return false;
}
};
}
/**
* @dataProvider provideNonLocalPage
*/
public function testNonLocalPage( string $method, ...$extraArgs ) {
$this->expectException( PreconditionException::class );
$this->expectExceptionMessage( 'otherwiki' );
$obj = $this->newRestrictionStore();
$page = new PageIdentityValue( 1, NS_MAIN, 'X', 'otherwiki' );
$obj->$method( $page, ...$extraArgs );
}
public static function provideNonLocalPage() {
// We programmatically get all public methods whose first parameter is a PageIdentity. This
// way we'll make sure to include any new methods that are added in the future.
$ret = [];
$methods = ( new ReflectionClass( RestrictionStore::class ) )
->getMethods( ReflectionMethod::IS_PUBLIC );
foreach ( $methods as $method ) {
$params = $method->getParameters();
if ( !$params[0]->hasType() || $params[0]->getType()->getName() !== PageIdentity::class ) {
continue;
}
$ret[$method->getName()] = [ $method->getName() ];
foreach ( array_slice( $params, 1 ) as $param ) {
// Extra required arguments
if ( $param->isOptional() ) {
break;
}
switch ( $param->getType()->getName() ) {
case 'string':
$ret[$method->getName()][] = 'x';
break;
case 'array':
$ret[$method->getName()][] = [];
break;
default:
throw new UnexpectedValueException(
"{$param->getType()->getName} type not supported" );
}
}
}
return $ret;
}
/**
* @dataProvider provideGetRestrictions
*/
public function testGetRestrictions(
array $expected,
PageIdentity $page,
string $action,
?array $rowsToLoad,
array $options = []
): void {
$obj = $this->newRestrictionStore( $options );
if ( is_array( $rowsToLoad ) ) {
$obj->loadRestrictionsFromRows( $page, $rowsToLoad );
}
$this->assertSame( $expected, $obj->getRestrictions( $page, $action ) );
}
public function provideGetRestrictions(): array {
$all = $this->provideGetAllRestrictions();
$ret = [];
foreach ( $all as $name => $arr ) {
[ $expected, $page ] = $arr;
$actions = array_merge( self::DEFAULT_RESTRICTION_TYPES, [ 'vaporize' ],
array_keys( $expected ) );
foreach ( $actions as $action ) {
$ret["$name ($action)"] =
array_merge(
[ $expected[$action] ?? [], $page, $action ],
array_slice( $arr, 2 )
);
}
}
return $ret;
}
/**
* @dataProvider provideGetAllRestrictions
*/
public function testGetAllRestrictions(
array $expected, PageIdentity $page, ?array $rowsToLoad, array $options = []
): void {
$obj = $this->newRestrictionStore( $options );
if ( is_array( $rowsToLoad ) ) {
$obj->loadRestrictionsFromRows( $page, $rowsToLoad );
}
$this->assertSame( $expected, $obj->getAllRestrictions( $page ) );
}
public static function provideGetAllRestrictions(): array {
return [
'Special page' => [
[],
self::newImproperPageIdentity( NS_SPECIAL, 'X' ),
null,
],
'Media page' => [
[],
self::newImproperPageIdentity( NS_MEDIA, 'X' ),
null,
],
'Simple existing unprotected page' => [
[ 'edit' => [], 'move' => [] ],
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
[],
],
'Simple existing protected page' => [
[ 'edit' => [ 'sysop', 'bureaucrat' ], 'move' => [ 'sysop' ] ],
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
[
(object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop,bureaucrat',
'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
(object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
],
],
'Protection type not allowed' => [
[ 'edit' => [ 'sysop' ] ],
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
[
(object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
(object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
],
[ MainConfigNames::RestrictionTypes => [ 'create', 'edit', 'upload' ] ],
],
'Expired protection' => [
[ 'edit' => [], 'move' => [ 'sysop' ] ],
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
[
(object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => '20200101000000', 'pr_cascade' => 0 ],
(object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
],
],
];
}
/**
* @dataProvider provideGetRestrictionExpiry
*/
public function testGetRestrictionExpiry(
?string $expected,
PageIdentity $page,
string $action,
?array $rowsToLoad,
array $options = []
): void {
$obj = $this->newRestrictionStore( $options );
if ( is_array( $rowsToLoad ) ) {
$obj->loadRestrictionsFromRows( $page, $rowsToLoad );
}
$this->assertSame( $expected, $obj->getRestrictionExpiry( $page, $action ) );
}
public static function provideGetRestrictionExpiry(): array {
return [
'Special page' => [
null,
self::newImproperPageIdentity( NS_SPECIAL, 'X' ),
'edit',
null,
],
'Media page' => [
null,
self::newImproperPageIdentity( NS_MEDIA, 'X' ),
'edit',
null,
],
'Simple existing unprotected page' => [
'infinity',
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
'edit',
[],
],
'Simple existing protected page (edit)' => [
'20760101000000',
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
'edit',
[
(object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => '20760101000000', 'pr_cascade' => 0 ],
(object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
],
],
'Simple existing protected page (move)' => [
'20670101000000',
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
'move',
[
(object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
(object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
'pr_expiry' => '20670101000000', 'pr_cascade' => 0 ],
],
],
'Simple existing protected page (unrecognized)' => [
null,
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
'unrecognized',
[
(object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
(object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
],
],
'Simple existing expired protected page (edit)' => [
'infinity',
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
'edit',
[
(object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => '20160101000000', 'pr_cascade' => 0 ],
(object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
],
],
'Simple existing expired protected page (move)' => [
'infinity',
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
'move',
[
(object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
(object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
'pr_expiry' => '20170101000000', 'pr_cascade' => 0 ],
],
],
'Simple existing expired protected page (unrecognized)' => [
null,
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
'unrecognized',
[
(object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => '20170101000000', 'pr_cascade' => 0 ],
(object)[ 'pr_type' => 'unrecognized', 'pr_level' => 'sysop',
'pr_expiry' => '20170101000000', 'pr_cascade' => 0 ],
],
],
];
}
/**
* @dataProvider provideGetCreateProtection
*/
public function testGetCreateProtection(
?array $expected, PageIdentity $page, $return, array $options = []
): void {
if ( $page->canExist() && !$page->exists() ) {
$options['db'] = [ DB_REPLICA => [ 'selectRow' =>
function (
$table, $vars, $conds, string $fname, $options = [], $join_conds = []
) use ( $page, $return ) {
$options = (array)$options;
$options['LIMIT'] = 1;
$db = new DatabaseTestHelper( __CLASS__ );
$sql = trim( preg_replace( '/\s+/', ' ', $db->selectSQLText(
$table, $vars, $conds, $fname, $options, $join_conds ) ) );
$this->assertSame(
'SELECT pt_user,pt_expiry,pt_create_perm,comment_pt_reason.comment_text ' .
'AS pt_reason_text,comment_pt_reason.comment_data AS pt_reason_data,' .
'comment_pt_reason.comment_id AS pt_reason_cid ' .
'FROM protected_titles JOIN comment comment_pt_reason ON ' .
'((comment_pt_reason.comment_id = pt_reason_id)) ' .
"WHERE pt_namespace = {$page->getNamespace()} AND " .
"pt_title = '{$page->getDBkey()}' LIMIT 1",
$sql );
return is_array( $return ) ? (object)$return : $return;
}
] ];
}
$obj = $this->newRestrictionStore( $options );
$this->assertSame( $expected, $obj->getCreateProtection( $page ) );
}
public static function provideGetCreateProtection(): array {
$ret = [
'Special page' => [ null, self::newImproperPageIdentity( NS_SPECIAL, 'X' ), null ],
'Media page' => [ null, self::newImproperPageIdentity( NS_MEDIA, 'X' ), null ],
'Existing page' => [ null, PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ), null ],
'Unprotected' => [ null, PageIdentityValue::localIdentity( 0, NS_MAIN, 'X' ), false ],
];
$protectedTests = [
'sysop' => 'editprotected',
'autoconfirmed' => 'editsemiprotected',
'editprotected' => 'editprotected',
'editsemiprotected' => 'editsemiprotected',
'custom' => 'custom',
];
foreach ( $protectedTests as $db => $returned ) {
$ret["Protected ($db)"] = [
[
'user' => 123,
'expiry' => 'infinity',
'permission' => $returned,
'reason' => 'reason',
],
PageIdentityValue::localIdentity( 0, NS_MAIN, 'X' ),
[
'pt_user' => 123,
'pt_expiry' => 'infinity',
'pt_create_perm' => $db,
'pt_reason_id' => 456,
'pt_reason_data' => '{}',
'pt_reason_text' => 'reason',
],
];
}
return $ret;
}
/**
* @dataProvider provideDeleteCreateProtection
*/
public function testDeleteCreateProtection( PageIdentity $page ): void {
$obj = $this->newRestrictionStore( [ 'db' => [ DB_PRIMARY => [ 'delete' =>
function ( string $table, array $where, string $method ) use ( $page ): bool {
$this->assertSame( 'protected_titles', $table );
$this->assertSame(
[ 'pt_namespace' => $page->getNamespace(), 'pt_title' => $page->getDBkey() ],
$where
);
return true;
}
] ] ] );
$obj->deleteCreateProtection( $page );
}
public static function provideDeleteCreateProtection(): array {
return [
// Most of these don't actually make sense, but test current behavior regardless.
'Special page' => [ self::newImproperPageIdentity( NS_SPECIAL, 'X' ) ],
'Media page' => [ self::newImproperPageIdentity( NS_MEDIA, 'X' ) ],
'Nonexistent page' => [ PageIdentityValue::localIdentity( 0, NS_MAIN, 'X' ) ],
'Existing page' => [ PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ) ],
];
}
/**
* @dataProvider provideIsSemiProtected
*/
public function testIsSemiProtected(
bool $expected, ?array $rowsToLoad, array $options = []
): void {
$page = $options['page'] ?? PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' );
$obj = $this->newRestrictionStore( $options );
if ( is_array( $rowsToLoad ) ) {
$obj->loadRestrictionsFromRows( $page, $rowsToLoad );
}
$this->assertSame( $expected, $obj->isSemiProtected( $page, 'edit' ) );
}
public static function provideIsSemiProtected(): array {
return [
'Special page' => [ false, null,
[ 'page' => self::newImproperPageIdentity( NS_SPECIAL, 'X' ) ] ],
'Media page' => [ false, null,
[ 'page' => self::newImproperPageIdentity( NS_MEDIA, 'X' ) ] ],
'Unprotected page' => [
false,
[ (object)[ 'pr_type' => 'move', 'pr_level' => 'autoconfirmed',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
],
'Semiprotected page' => [
true,
[
(object)[ 'pr_type' => 'edit', 'pr_level' => 'autoconfirmed',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
(object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
],
],
'Fully protected page' => [
false,
[
(object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
(object)[ 'pr_type' => 'move', 'pr_level' => 'autoconfirmed',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
],
],
'No semiprotection configured' => [
false,
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'autoconfirmed',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
[ MainConfigNames::SemiprotectedRestrictionLevels => [] ],
],
'Config with editsemiprotected' => [
true,
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'autoconfirmed',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
[ MainConfigNames::SemiprotectedRestrictionLevels => [ 'editsemiprotected' ] ],
],
'Data with editsemiprotected' => [
true,
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'editsemiprotected',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
],
'Config and data with editsemiprotected' => [
true,
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'editsemiprotected',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
[ MainConfigNames::SemiprotectedRestrictionLevels => [ 'editsemiprotected' ] ],
],
'Semiprotection plus other protection level' => [
false,
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'autoconfirmed,superman',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
[ MainConfigNames::RestrictionLevels => [ '', 'autoconfirmed', 'sysop', 'superman' ] ],
],
'Two semiprotections' => [
true,
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'autoconfirmed,superman',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
[ MainConfigNames::RestrictionLevels => [ '', 'autoconfirmed', 'sysop', 'superman' ],
MainConfigNames::SemiprotectedRestrictionLevels => [ 'autoconfirmed', 'superman' ] ],
],
];
}
/**
* @dataProvider provideIsProtected
*/
public function testIsProtected(
bool $expected, ?array $rowsToLoad, array $options = []
): void {
$page = $options['page'] ?? PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' );
$action = $options['action'] ?? '';
$obj = $this->newRestrictionStore( $options );
if ( is_array( $rowsToLoad ) ) {
$obj->loadRestrictionsFromRows( $page, $rowsToLoad );
}
$this->assertSame( $expected, $obj->isProtected( $page, $action ) );
}
public static function provideIsProtected(): array {
return [
'Special page' => [ true, null,
[ 'page' => self::newImproperPageIdentity( NS_SPECIAL, 'X' ) ] ],
'Media page' => [ false, null,
[ 'page' => self::newImproperPageIdentity( NS_MEDIA, 'X' ) ] ],
'Unprotected page' => [ false, [] ],
'Semiprotected page' => [
true,
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'autoconfirmed',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
],
'Fully protected page' => [
true,
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
],
'Protected against empty string' => [
false,
[ (object)[ 'pr_type' => 'edit', 'pr_level' => '',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
],
'Unrecognized protection' => [
false,
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'unrecognized',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
],
'Unrecognized plus recognized protection' => [
true,
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop,unrecognized',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
],
'Check unrecognized protection type' => [
false,
[
(object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
(object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
(object)[ 'pr_type' => 'unrecognized', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
],
[ 'action' => 'unrecognized' ],
],
'Check custom protection type' => [
true,
[ (object)[ 'pr_type' => 'custom', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ], ],
[ 'action' => 'custom', MainConfigNames::RestrictionTypes =>
array_merge( self::DEFAULT_RESTRICTION_TYPES, [ 'custom' ] ) ],
],
'Check custom protection level' => [
true,
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'custom',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ], ],
[ 'action' => 'edit', 'RestrictionLevels' =>
[ '', 'autoconfirmed', 'sysop', 'custom' ] ],
],
'Check edit protection of edit-protected page' => [
true,
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
[ 'action' => 'edit' ],
],
'Check move protection of edit-protected page' => [
false,
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
[ 'action' => 'move' ],
],
'Check move protection of move-protected page' => [
true,
[ (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
[ 'action' => 'move' ],
],
'Check edit protection of move-protected page' => [
false,
[ (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
[ 'action' => 'edit' ],
],
'Check edit protection of edit- and move-protected page' => [
true,
[
(object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
(object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
],
[ 'action' => 'edit' ],
],
'Check move protection of edit- and move-protected page' => [
true,
[
(object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
(object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
],
[ 'action' => 'move' ],
],
];
}
/**
* @dataProvider provideListApplicableRestrictionTypes
*/
public function testListApplicableRestrictionTypes(
array $expected, PageIdentity $page, array $options = []
): void {
$obj = $this->newRestrictionStore( $options );
$this->assertSame( $expected, $obj->listApplicableRestrictionTypes( $page ) );
}
public function provideListApplicableRestrictionTypes(): array {
$expandedRestrictions = array_merge( self::DEFAULT_RESTRICTION_TYPES, [ 'liquify' ] );
return [
'Special page' => [
[],
self::newImproperPageIdentity( NS_SPECIAL, 'X' ),
],
'Media page' => [
[],
self::newImproperPageIdentity( NS_MEDIA, 'X' ),
],
'Nonexistent page' => [
[ 'create' ],
PageIdentityValue::localIdentity( 0, NS_MAIN, 'X' ),
],
'Existing page' => [
[ 'edit', 'move' ],
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
],
'Nonexistent file' => [
[ 'create' ],
PageIdentityValue::localIdentity( 0, NS_FILE, 'X' ),
],
'Existing file' => [
[ 'edit', 'move', 'upload' ],
PageIdentityValue::localIdentity( 1, NS_FILE, 'X' ),
],
'Nonexistent page with no create' => [
[],
PageIdentityValue::localIdentity( 0, NS_MAIN, 'X' ),
[ MainConfigNames::RestrictionTypes => [ 'edit', 'move', 'upload' ] ],
],
'Existing page with no move' => [
[ 'edit' ],
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
[ MainConfigNames::RestrictionTypes => [ 'create', 'edit', 'upload' ] ],
],
'Nonexistent file with no upload' => [
[ 'create' ],
PageIdentityValue::localIdentity( 0, NS_FILE, 'X' ),
[ MainConfigNames::RestrictionTypes => [ 'create', 'edit', 'move' ] ],
],
'Special page with extra type' => [
[],
self::newImproperPageIdentity( NS_SPECIAL, 'X' ),
[ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
],
'Media page with extra type' => [
[],
self::newImproperPageIdentity( NS_MEDIA, 'X' ),
[ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
],
'Nonexistent page with extra type' => [
[ 'create' ],
PageIdentityValue::localIdentity( 0, NS_MAIN, 'X' ),
[ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
],
'Existing page with extra type' => [
[ 'edit', 'move', 'liquify' ],
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
[ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
],
'Nonexistent file with extra type' => [
[ 'create' ],
PageIdentityValue::localIdentity( 0, NS_FILE, 'X' ),
[ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
],
'Existing file with extra type' => [
[ 'edit', 'move', 'upload', 'liquify' ],
PageIdentityValue::localIdentity( 1, NS_FILE, 'X' ),
[ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
],
'Hook' => [
[ 'move', 'liquify' ],
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
[ 'hookFn' => static function ( Title $title, array &$types ): bool {
self::assertEquals( Title::castFromPageIdentity(
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ) ), $title );
self::assertSame( [ 'edit', 'move' ], $types );
$types = [ 'move', 'liquify' ];
return false;
} ],
],
'Hook not run for special page' => [
[],
self::newImproperPageIdentity( NS_SPECIAL, 'X' ),
[ 'hookFn' => function () {
$this->fail( 'Should be unreached' );
} ],
],
'Hook not run for media page' => [
[],
self::newImproperPageIdentity( NS_MEDIA, 'X' ),
[ 'hookFn' => function () {
$this->fail( 'Should be unreached' );
} ],
],
];
}
/**
* @dataProvider provideListAllRestrictionTypes
*/
public function testListAllRestrictionTypes(
array $expected, array $args, array $options = []
) {
$obj = $this->newRestrictionStore( $options );
$this->assertSame( $expected, $obj->listAllRestrictionTypes( ...$args ) );
}
public static function provideListAllRestrictionTypes() {
$expandedRestrictions = array_merge( self::DEFAULT_RESTRICTION_TYPES, [ 'solidify' ] );
return [
'Exists' => [ [ 'edit', 'move', 'upload' ], [ true ] ],
'Default is exists' => [ [ 'edit', 'move', 'upload' ], [] ],
'Nonexistent' => [ [ 'create' ], [ false ] ],
'Exists with extra restriction type' => [
[ 'edit', 'move', 'upload', 'solidify' ],
[ true ],
[ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
],
'Default is exists with extra restriction type' => [
[ 'edit', 'move', 'upload', 'solidify' ],
[],
[ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
],
'Nonexistent with extra restriction type' => [
[ 'create' ],
[ false ],
[ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
],
'Exists with no edit' => [
[ 'move', 'upload' ],
[ true ],
[ MainConfigNames::RestrictionTypes => [ 'create', 'move', 'upload' ] ],
],
'Exists with only create' => [
[],
[ true ],
[ MainConfigNames::RestrictionTypes => [ 'create' ] ],
],
'Nonexistent with no create' => [
[],
[ false ],
[ MainConfigNames::RestrictionTypes => [ 'edit', 'move', 'upload', 'solidify' ] ],
],
'Nonexistent with no upload' => [
[ 'create' ],
[ false ],
[ MainConfigNames::RestrictionTypes => [ 'create', 'edit', 'move', 'solidify' ] ],
],
'Nonexistent with no create or upload' => [
[],
[ false ],
[ MainConfigNames::RestrictionTypes => [ 'edit', 'move', 'solidify' ] ],
],
];
}
/**
* @dataProvider provideAreRestrictionsLoaded
*/
public function testAreRestrictionsLoaded(
bool $expected, PageIdentity $page, ?array $rowsToLoad = null, array $options = []
): void {
$obj = $this->newRestrictionStore( $options );
if ( is_array( $rowsToLoad ) ) {
$obj->loadRestrictionsFromRows( $page, $rowsToLoad );
}
$this->assertSame( $expected, $obj->areRestrictionsLoaded( $page ) );
}
public static function provideAreRestrictionsLoaded(): array {
return [
'Special page' => [ false, self::newImproperPageIdentity( NS_SPECIAL, 'X' ) ],
'Media page' => [ false, self::newImproperPageIdentity( NS_MEDIA, 'X' ) ],
'Regular page' => [ false, PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ) ],
'Regular page with no restrictions' =>
[ true, PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ), [] ],
'Regular page with restrictions' => [
true,
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => 0 ] ],
],
];
}
/**
* @dataProvider provideAreRestrictionsCascading
*/
public function testAreRestrictionsCascading(
bool $expected, PageIdentity $page, ?array $rowsToLoad, array $options = []
): void {
$obj = $this->newRestrictionStore( $options );
if ( is_array( $rowsToLoad ) ) {
$obj->loadRestrictionsFromRows( $page, $rowsToLoad );
}
$this->assertSame( $expected, $obj->areRestrictionsCascading( $page ) );
}
public static function provideAreRestrictionsCascading(): array {
return [
'Special page' => [ false, self::newImproperPageIdentity( NS_SPECIAL, 'X' ), null ],
'Media page' => [ false, self::newImproperPageIdentity( NS_MEDIA, 'X' ), null ],
'Regular page with no restrictions' =>
[ false, PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ), [] ],
'Regular page with restrictions' => [
false,
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => 0 ] ],
],
'Regular page with cascading restrictions' => [
true,
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
[ (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => 1 ] ],
],
'Regular page with some cascading restrictions and some not' => [
true,
PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
[
(object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
(object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
'pr_expiry' => 'infinity', 'pr_cascade' => 1 ],
],
],
];
}
public function testFlushRestrictions(): void {
$obj = $this->newRestrictionStore();
$page = PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' );
$this->assertFalse( $obj->areRestrictionsLoaded( $page ) );
$obj->loadRestrictionsFromRows( $page, [] );
$this->assertTrue( $obj->areRestrictionsLoaded( $page ) );
$obj->flushRestrictions( $page );
$this->assertFalse( $obj->areRestrictionsLoaded( $page ) );
}
public function testGetCascadeProtectionSources() {
$obj = $this->newRestrictionStore( [ 'db' => [ DB_REPLICA => [ 'select' =>
static function () {
return new FakeResultWrapper( [
(object)[ 'pr_page' => 1, 'page_namespace' => NS_MAIN, 'page_title' => 'test',
'pr_expiry' => 'infinity', 'pr_type' => 'edit', 'pr_level' => 'Sysop',
'type' => 'tl' ]
] );
},
'addQuotes' => [
static function () {
return 'noop';
},
2
]
] ] ] );
$page = PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' );
[ $sources, $restrictions, $tlSources, $ilSources ] = $obj->getCascadeProtectionSources( $page );
$this->assertCount( 1, $sources );
$this->assertArrayHasKey( 'edit', $restrictions );
$this->assertCount( 0, $ilSources );
$this->assertCount( 1, $tlSources );
}
public function testGetCascadeProtectionSourcesFile() {
$obj = $this->newRestrictionStore( [ 'db' => [ DB_REPLICA => [
'addQuotes' => [
static function () {
return 'noop';
},
2
],
'selectSQLText' => [
static function () {
return 'noop';
},
2
],
'unionQueries' => static function () {
return 'noop';
},
'query' => static function () {
return new FakeResultWrapper( [
(object)[ 'pr_page' => 1, 'page_namespace' => NS_MAIN, 'page_title' => 'test1',
'pr_expiry' => 'infinity', 'pr_type' => 'edit', 'pr_level' => 'Sysop',
'type' => 'il' ],
(object)[ 'pr_page' => 2, 'page_namespace' => NS_MAIN, 'page_title' => 'test2',
'pr_expiry' => 'infinity', 'pr_type' => 'edit', 'pr_level' => 'Sysop',
'type' => 'tl' ]
] );
},
] ] ] );
$page = PageIdentityValue::localIdentity( 1, NS_FILE, 'Image.jpg' );
[ $sources, $restrictions, $tlSources, $ilSources ] = $obj->getCascadeProtectionSources( $page );
$this->assertCount( 2, $sources );
$this->assertCount( 1, $ilSources );
$this->assertCount( 1, $tlSources );
$this->assertArrayHasKey( 'edit', $restrictions );
}
public function testGetCascadeProtectionSourcesSpecialPage() {
$obj = $this->newRestrictionStore( [ 'db' => [ DB_REPLICA => [ 'select' => [
static function () {
return [];
},
0
] ] ] ] );
$page = $this->makeMockTitle( 'Whatlinkshere', [ 'namespace' => NS_SPECIAL ] );
[ $sources, $restrictions, $ilSources ] = $obj->getCascadeProtectionSources( $page );
$this->assertCount( 0, $sources );
$this->assertCount( 0, $restrictions );
$this->assertCount( 0, $ilSources );
}
public function testShouldNotFetchProtectionSettingsIfActionCannotBeRestricted(): void {
$lb = $this->createMock( ILoadBalancer::class );
$lb->expects( $this->never() )
->method( $this->anything() );
$store = new RestrictionStore(
new ServiceOptions( RestrictionStore::CONSTRUCTOR_OPTIONS, [
MainConfigNames::NamespaceProtection => [],
MainConfigNames::RestrictionLevels => [ '', 'autoconfirmed', 'sysop' ],
MainConfigNames::RestrictionTypes => self::DEFAULT_RESTRICTION_TYPES,
MainConfigNames::SemiprotectedRestrictionLevels => [ 'autoconfirmed' ],
] ),
WANObjectCache::newEmpty(),
$lb,
$this->createMock( LinkCache::class ),
$this->createMock( LinksMigration::class ),
$this->createMock( CommentStore::class ),
$this->createMock( HookContainer::class ),
$this->createMock( PageStore::class )
);
$page = new PageIdentityValue( 1, NS_MAIN, 'Test', WikiAwareEntity::LOCAL );
$this->assertSame( [], $store->getRestrictions( $page, 'non-restrictable-action' ) );
}
}
|