Merge "HistoryAction: Implement HistoryPageToolLinks hook for adding more links"
[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 a right- message exist,
59 * which is used on Special:ListGroupRights as help text
60 * Extensions and core
61 *
62 * @coversNothing
63 */
64 public function testAllRightsWithMessage() {
65 // Getting all user rights, for core: User::$mCoreRights, for extensions: $wgAvailableRights
66 $allRights = User::getAllRights();
67 $allMessageKeys = Language::getMessageKeysFor( 'en' );
68
69 $rightsWithMessage = [];
70 foreach ( $allMessageKeys as $message ) {
71 // === 0: must be at beginning of string (position 0)
72 if ( strpos( $message, 'right-' ) === 0 ) {
73 $rightsWithMessage[] = substr( $message, strlen( 'right-' ) );
74 }
75 }
76
77 $missing = array_diff(
78 $allRights,
79 $rightsWithMessage
80 );
81
82 $this->assertEquals(
83 [],
84 $missing,
85 'Each user rights (core/extensions) has a corresponding right- message.'
86 );
87 }
88 }