3 use MediaWiki\MediaWikiServices
;
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 >
11 class AvailableRightsTest
extends PHPUnit\Framework\TestCase
{
13 use MediaWikiCoversValidator
;
16 * Returns all rights that should be in $wgAvailableRights + all rights
17 * registered via the 'UserGetAllRights' hook + all "core" rights.
21 private function getAllVisibleRights() {
22 global $wgGroupPermissions, $wgRevokePermissions;
24 $rights = MediaWikiServices
::getInstance()->getPermissionManager()->getAllPermissions();
26 foreach ( $wgGroupPermissions as $permissions ) {
27 $rights = array_merge( $rights, array_keys( $permissions ) );
30 foreach ( $wgRevokePermissions as $permissions ) {
31 $rights = array_merge( $rights, array_keys( $permissions ) );
34 $rights = array_unique( $rights );
40 public function testAvailableRights() {
41 $missingRights = array_diff(
42 $this->getAllVisibleRights(),
43 MediaWikiServices
::getInstance()->getPermissionManager()->getAllPermissions()
48 // Re-index to produce nicer output, keys are meaningless.
49 array_values( $missingRights ),
50 'Additional user rights need to be added to $wgAvailableRights or ' .
51 'via the "UserGetAllRights" hook. See the instructions at: ' .
52 'https://www.mediawiki.org/wiki/Manual:User_rights#Adding_new_rights'
57 * Test, if for all rights an action- message exist,
58 * which is used on Special:ListGroupRights as help text
63 public function testAllActionsWithMessages() {
64 $this->checkMessagesExist( 'action-' );
68 * Test, if for all rights a right- message exist,
69 * which is used on Special:ListGroupRights as help text
72 public function testAllRightsWithMessage() {
73 $this->checkMessagesExist( 'right-' );
77 * @param string $prefix
79 private function checkMessagesExist( $prefix ) {
80 // Getting all user rights, for core: User::$mCoreRights, for extensions: $wgAvailableRights
81 $allRights = MediaWikiServices
::getInstance()->getPermissionManager()->getAllPermissions();
82 $allMessageKeys = Language
::getMessageKeysFor( 'en' );
84 $messagesToCheck = [];
85 foreach ( $allMessageKeys as $message ) {
86 // === 0: must be at beginning of string (position 0)
87 if ( strpos( $message, $prefix ) === 0 ) {
88 $messagesToCheck[] = substr( $message, strlen( $prefix ) );
92 $missing = array_diff(
100 "Each user right (core/extensions) has a corresponding $prefix message."