Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / phpunit / structure / AvailableRightsTest.php
1 <?php
2
3 /**
4 * Try to make sure that extensions register all rights in $wgAvailableRights
5 * or via the 'UserGetAllRights' hook.
6 *
7 * @author Marius Hoch < hoo@online.de >
8 */
9 class AvailableRightsTest extends PHPUnit\Framework\TestCase {
10
11 use MediaWikiCoversValidator;
12
13 /**
14 * Returns all rights that should be in $wgAvailableRights + all rights
15 * registered via the 'UserGetAllRights' hook + all "core" rights.
16 *
17 * @return string[]
18 */
19 private function getAllVisibleRights() {
20 global $wgGroupPermissions, $wgRevokePermissions;
21
22 $rights = User::getAllRights();
23
24 foreach ( $wgGroupPermissions as $permissions ) {
25 $rights = array_merge( $rights, array_keys( $permissions ) );
26 }
27
28 foreach ( $wgRevokePermissions as $permissions ) {
29 $rights = array_merge( $rights, array_keys( $permissions ) );
30 }
31
32 $rights = array_unique( $rights );
33 sort( $rights );
34
35 return $rights;
36 }
37
38 public function testAvailableRights() {
39 $missingRights = array_diff(
40 $this->getAllVisibleRights(),
41 User::getAllRights()
42 );
43
44 $this->assertEquals(
45 [],
46 // Re-index to produce nicer output, keys are meaningless.
47 array_values( $missingRights ),
48 'Additional user rights need to be added to $wgAvailableRights or ' .
49 'via the "UserGetAllRights" hook. See the instructions at: ' .
50 'https://www.mediawiki.org/wiki/Manual:User_rights#Adding_new_rights'
51 );
52 }
53
54 /**
55 * Test, if for all rights an action- message exist,
56 * which is used on Special:ListGroupRights as help text
57 * Extensions and core
58 *
59 * @coversNothing
60 */
61 public function testAllActionsWithMessages() {
62 $this->checkMessagesExist( 'action-' );
63 }
64
65 /**
66 * Test, if for all rights a right- message exist,
67 * which is used on Special:ListGroupRights as help text
68 * Extensions and core
69 */
70 public function testAllRightsWithMessage() {
71 $this->checkMessagesExist( 'right-' );
72 }
73
74 /**
75 * @param string $prefix
76 */
77 private function checkMessagesExist( $prefix ) {
78 // Getting all user rights, for core: User::$mCoreRights, for extensions: $wgAvailableRights
79 $allRights = User::getAllRights();
80 $allMessageKeys = Language::getMessageKeysFor( 'en' );
81
82 $messagesToCheck = [];
83 foreach ( $allMessageKeys as $message ) {
84 // === 0: must be at beginning of string (position 0)
85 if ( strpos( $message, $prefix ) === 0 ) {
86 $messagesToCheck[] = substr( $message, strlen( $prefix ) );
87 }
88 }
89
90 $missing = array_diff(
91 $allRights,
92 $messagesToCheck
93 );
94
95 $this->assertEquals(
96 [],
97 $missing,
98 "Each user right (core/extensions) has a corresponding $prefix message."
99 );
100 }
101 }