3 use MediaWiki\MainConfigNames
;
6 * Try to make sure that extensions register all rights in $wgAvailableRights
7 * or via the 'UserGetAllRights' hook.
9 * @author Marius Hoch < hoo@online.de >
12 class AvailableRightsTest
extends MediaWikiIntegrationTestCase
{
14 use MediaWikiCoversValidator
;
17 * Returns all rights that should be in $wgAvailableRights + all rights
18 * registered via the 'UserGetAllRights' hook + all "core" rights.
22 private function getAllVisibleRights() {
23 global $wgGroupPermissions, $wgRevokePermissions;
25 $rights = $this->getServiceContainer()->getPermissionManager()->getAllPermissions();
27 foreach ( $wgGroupPermissions as $permissions ) {
28 $rights = array_merge( $rights, array_keys( $permissions ) );
31 foreach ( $wgRevokePermissions as $permissions ) {
32 $rights = array_merge( $rights, array_keys( $permissions ) );
35 $rights = array_unique( $rights );
41 public function testAvailableRights() {
42 $missingRights = array_diff(
43 $this->getAllVisibleRights(),
44 $this->getServiceContainer()->getPermissionManager()->getAllPermissions()
49 // Re-index to produce nicer output, keys are meaningless.
50 array_values( $missingRights ),
51 'Additional user rights need to be added to $wgAvailableRights or ' .
52 'via the "UserGetAllRights" hook. See the instructions at: ' .
53 'https://www.mediawiki.org/wiki/Manual:User_rights#Adding_new_rights'
57 public function testAvailableRightsShouldNotBeImplicitRights() {
58 $intersection = array_intersect(
59 $this->getServiceContainer()->getPermissionManager()->getImplicitRights(),
60 $this->getServiceContainer()->getPermissionManager()->getAllPermissions()
65 // Re-index to produce nicer output, keys are meaningless.
66 array_values( $intersection ),
67 'Additional user rights can be added to $wgAvailableRights or $wgImplicitRights, ' .
72 public function testLimitsAreRights() {
73 $knownRights = array_merge(
74 $this->getServiceContainer()->getPermissionManager()->getImplicitRights(),
75 $this->getServiceContainer()->getPermissionManager()->getAllPermissions()
78 $missingRights = array_diff(
79 array_keys( $this->getConfVar( MainConfigNames
::RateLimits
) ),
85 // Re-index to produce nicer output, keys are meaningless.
86 array_values( $missingRights ),
87 'All keys in $wgRateLimits must be listed in $wgAvailableRights or $wgImplicitRights, ' .
88 'unless the keys are defined as rights by MediaWiki core.'
93 * Test, if for all rights an action- message exists,
94 * which is used on Special:ListGroupRights as help text
99 public function testAllActionsWithMessages() {
100 $this->checkMessagesExist( 'action-' );
104 * Test, if for all rights a right- message exists,
105 * which is used on Special:ListGroupRights as help text
106 * Extensions and core
108 public function testAllRightsWithMessage() {
109 $this->checkMessagesExist( 'right-' );
113 * @param string $prefix
115 private function checkMessagesExist( $prefix ) {
116 // Getting all user rights, for core: User::$mCoreRights, for extensions: $wgAvailableRights
117 $services = $this->getServiceContainer();
118 $allRights = $services->getPermissionManager()->getAllPermissions();
119 $allMessageKeys = $services->getLocalisationCache()->getSubitemList( 'en', 'messages' );
121 $messagesToCheck = [];
122 foreach ( $allMessageKeys as $message ) {
123 if ( str_starts_with( $message, $prefix ) ) {
124 $messagesToCheck[] = substr( $message, strlen( $prefix ) );
128 $missing = array_diff(
136 "Each user right (core/extensions) has a corresponding $prefix message."
137 . ' See the instructions at: '
138 . 'https://www.mediawiki.org/wiki/Manual:User_rights#Adding_new_rights'