Add test to check action- messages exist
[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 /**
39 * @coversNothing
40 */
41 public function testAvailableRights() {
42 $missingRights = array_diff(
43 $this->getAllVisibleRights(),
44 User::getAllRights()
45 );
46
47 $this->assertEquals(
48 [],
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'
54 );
55 }
56
57 /**
58 * Test, if for all rights an action- message exist,
59 * which is used on Special:ListGroupRights as help text
60 * Extensions and core
61 *
62 * @coversNothing
63 */
64 public function testAllActionsWithMessages() {
65 $this->checkMessagesExist( 'action-' );
66 }
67
68 /**
69 * Test, if for all rights a right- message exist,
70 * which is used on Special:ListGroupRights as help text
71 * Extensions and core
72 *
73 * @coversNothing
74 */
75 public function testAllRightsWithMessage() {
76 $this->checkMessagesExist( 'right-' );
77 }
78
79 /**
80 * @param string $prefix
81 */
82 private function checkMessagesExist( $prefix ) {
83 // Getting all user rights, for core: User::$mCoreRights, for extensions: $wgAvailableRights
84 $allRights = User::getAllRights();
85 $allMessageKeys = Language::getMessageKeysFor( 'en' );
86
87 $messagesToCheck = [];
88 foreach ( $allMessageKeys as $message ) {
89 // === 0: must be at beginning of string (position 0)
90 if ( strpos( $message, $prefix ) === 0 ) {
91 $messagesToCheck[] = substr( $message, strlen( $prefix ) );
92 }
93 }
94
95 $missing = array_diff(
96 $allRights,
97 $messagesToCheck
98 );
99
100 $this->assertEquals(
101 [],
102 $missing,
103 "Each user right (core/extensions) has a corresponding $prefix message."
104 );
105 }
106 }