Merge "Improve formatting of constructor documentation for block classes"
[lhc/web/wiklou.git] / tests / phpunit / structure / AvailableRightsTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * Try to make sure that extensions register all rights in $wgAvailableRights
7 * or via the 'UserGetAllRights' hook.
8 *
9 * @author Marius Hoch < hoo@online.de >
10 */
11 class AvailableRightsTest extends PHPUnit\Framework\TestCase {
12
13 use MediaWikiCoversValidator;
14
15 /**
16 * Returns all rights that should be in $wgAvailableRights + all rights
17 * registered via the 'UserGetAllRights' hook + all "core" rights.
18 *
19 * @return string[]
20 */
21 private function getAllVisibleRights() {
22 global $wgGroupPermissions, $wgRevokePermissions;
23
24 $rights = MediaWikiServices::getInstance()->getPermissionManager()->getAllPermissions();
25
26 foreach ( $wgGroupPermissions as $permissions ) {
27 $rights = array_merge( $rights, array_keys( $permissions ) );
28 }
29
30 foreach ( $wgRevokePermissions as $permissions ) {
31 $rights = array_merge( $rights, array_keys( $permissions ) );
32 }
33
34 $rights = array_unique( $rights );
35 sort( $rights );
36
37 return $rights;
38 }
39
40 public function testAvailableRights() {
41 $missingRights = array_diff(
42 $this->getAllVisibleRights(),
43 MediaWikiServices::getInstance()->getPermissionManager()->getAllPermissions()
44 );
45
46 $this->assertEquals(
47 [],
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'
53 );
54 }
55
56 /**
57 * Test, if for all rights an action- message exist,
58 * which is used on Special:ListGroupRights as help text
59 * Extensions and core
60 *
61 * @coversNothing
62 */
63 public function testAllActionsWithMessages() {
64 $this->checkMessagesExist( 'action-' );
65 }
66
67 /**
68 * Test, if for all rights a right- message exist,
69 * which is used on Special:ListGroupRights as help text
70 * Extensions and core
71 */
72 public function testAllRightsWithMessage() {
73 $this->checkMessagesExist( 'right-' );
74 }
75
76 /**
77 * @param string $prefix
78 */
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' );
83
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 ) );
89 }
90 }
91
92 $missing = array_diff(
93 $allRights,
94 $messagesToCheck
95 );
96
97 $this->assertEquals(
98 [],
99 $missing,
100 "Each user right (core/extensions) has a corresponding $prefix message."
101 );
102 }
103 }