| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Unit\Permissions; |
| 4 |
|
| 5 |
use MediaWiki\Config\ServiceOptions; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\Permissions\GrantsInfo; |
| 8 |
use MediaWikiUnitTestCase; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class GrantsInfoTest extends MediaWikiUnitTestCase { |
| 14 |
|
| 15 |
private GrantsInfo $grantsInfo; |
| 16 |
|
| 17 |
protected function setUp(): void { |
| 18 |
parent::setUp(); |
| 19 |
|
| 20 |
$config = [ |
| 21 |
MainConfigNames::GrantPermissions => [ |
| 22 |
'hidden1' => [ 'read' => true, 'autoconfirmed' => false ], |
| 23 |
'hidden2' => [ 'autoconfirmed' => true ], |
| 24 |
'normal' => [ 'edit' => true ], |
| 25 |
'normal2' => [ 'edit' => true, 'create' => true ], |
| 26 |
'admin' => [ 'protect' => true, 'delete' => true ], |
| 27 |
], |
| 28 |
MainConfigNames::GrantPermissionGroups => [ |
| 29 |
'hidden1' => 'hidden', |
| 30 |
'hidden2' => 'hidden', |
| 31 |
'normal' => 'normal-group', |
| 32 |
'admin' => 'admin', |
| 33 |
], |
| 34 |
MainConfigNames::GrantRiskGroups => [ |
| 35 |
'hidden1' => 'low', |
| 36 |
'hidden2' => 'low', |
| 37 |
'normal' => 'low', |
| 38 |
'normal2' => 'vandalism', |
| 39 |
'admin' => 'security', |
| 40 |
], |
| 41 |
]; |
| 42 |
|
| 43 |
$this->grantsInfo = new GrantsInfo( |
| 44 |
new ServiceOptions( |
| 45 |
GrantsInfo::CONSTRUCTOR_OPTIONS, |
| 46 |
$config |
| 47 |
) |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
public function testGetValidGrants() { |
| 52 |
$this->assertSame( |
| 53 |
[ 'hidden1', 'hidden2', 'normal', 'normal2', 'admin' ], |
| 54 |
$this->grantsInfo->getValidGrants() |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
public function testGetRightsByGrant() { |
| 59 |
$this->assertSame( |
| 60 |
[ |
| 61 |
'hidden1' => [ 'read' ], |
| 62 |
'hidden2' => [ 'autoconfirmed' ], |
| 63 |
'normal' => [ 'edit' ], |
| 64 |
'normal2' => [ 'edit', 'create' ], |
| 65 |
'admin' => [ 'protect', 'delete' ], |
| 66 |
], |
| 67 |
$this->grantsInfo->getRightsByGrant() |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function testGetGrantRights( $grants, $rights ) { |
| 77 |
$this->assertSame( $rights, $this->grantsInfo->getGrantRights( $grants ) ); |
| 78 |
} |
| 79 |
|
| 80 |
public static function provideGetGrantRights() { |
| 81 |
return [ |
| 82 |
'anon' => [ 'hidden1', [ 'read' ] ], |
| 83 |
'newbie' => [ [ 'hidden1', 'hidden2', 'hidden3' ], [ 'read', 'autoconfirmed' ] ], |
| 84 |
'basic' => [ [ 'normal1', 'normal2' ], [ 'edit', 'create' ] ], |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function testGrantsAreValid( $grants, $valid ) { |
| 94 |
$this->assertSame( $valid, $this->grantsInfo->grantsAreValid( $grants ) ); |
| 95 |
} |
| 96 |
|
| 97 |
public static function provideGrantsAreValid() { |
| 98 |
return [ |
| 99 |
[ [ 'hidden1', 'hidden2' ], true ], |
| 100 |
[ [ 'hidden1', 'hidden3' ], false ], |
| 101 |
]; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function testGetGrantGroups( $grants, $expect ) { |
| 110 |
$this->assertSame( $expect, $this->grantsInfo->getGrantGroups( $grants ) ); |
| 111 |
} |
| 112 |
|
| 113 |
public static function provideGetGrantGroups() { |
| 114 |
return [ |
| 115 |
[ null, [ |
| 116 |
'hidden' => [ 'hidden1', 'hidden2' ], |
| 117 |
'normal-group' => [ 'normal' ], |
| 118 |
'other' => [ 'normal2' ], |
| 119 |
'admin' => [ 'admin' ], |
| 120 |
] ], |
| 121 |
[ [ 'hidden1', 'normal' ], [ |
| 122 |
'hidden' => [ 'hidden1' ], |
| 123 |
'normal-group' => [ 'normal' ], |
| 124 |
] ], |
| 125 |
]; |
| 126 |
} |
| 127 |
|
| 128 |
public function testGetHiddenGrants() { |
| 129 |
$this->assertSame( |
| 130 |
[ 'hidden1', 'hidden2' ], |
| 131 |
$this->grantsInfo->getHiddenGrants() |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
public function testGetRiskGroupsByGrant() { |
| 136 |
$this->assertSame( |
| 137 |
[ |
| 138 |
'hidden1' => 'low', |
| 139 |
'hidden2' => 'low', |
| 140 |
'normal' => 'low', |
| 141 |
'normal2' => 'vandalism', |
| 142 |
'admin' => 'security', |
| 143 |
], |
| 144 |
$this->grantsInfo->getRiskGroupsByGrant() |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
} |