Debian Codegraph @

tests/phpunit/structure/AvailableRightsTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MainConfigNames;
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 < [email protected] >
10 * @coversNothing
11 */
12 class AvailableRightsTest extends MediaWikiIntegrationTestCase {
13
14 use MediaWikiCoversValidator;
15
16 /**
17 * Returns all rights that should be in $wgAvailableRights + all rights
18 * registered via the 'UserGetAllRights' hook + all "core" rights.
19 *
20 * @return string[]
21 */
22 private function getAllVisibleRights() {
23 global $wgGroupPermissions, $wgRevokePermissions;
24
25 $rights = $this->getServiceContainer()->getPermissionManager()->getAllPermissions();
26
27 foreach ( $wgGroupPermissions as $permissions ) {
28 $rights = array_merge( $rights, array_keys( $permissions ) );
29 }
30
31 foreach ( $wgRevokePermissions as $permissions ) {
32 $rights = array_merge( $rights, array_keys( $permissions ) );
33 }
34
35 $rights = array_unique( $rights );
36 sort( $rights );
37
38 return $rights;
39 }
40
41 public function testAvailableRights() {
42 $missingRights = array_diff(
43 $this->getAllVisibleRights(),
44 $this->getServiceContainer()->getPermissionManager()->getAllPermissions()
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 public function testAvailableRightsShouldNotBeImplicitRights() {
58 $intersection = array_intersect(
59 $this->getServiceContainer()->getPermissionManager()->getImplicitRights(),
60 $this->getServiceContainer()->getPermissionManager()->getAllPermissions()
61 );
62
63 $this->assertEquals(
64 [],
65 // Re-index to produce nicer output, keys are meaningless.
66 array_values( $intersection ),
67 'Additional user rights can be added to $wgAvailableRights or $wgImplicitRights, ' .
68 'but not both!'
69 );
70 }
71
72 public function testLimitsAreRights() {
73 $knownRights = array_merge(
74 $this->getServiceContainer()->getPermissionManager()->getImplicitRights(),
75 $this->getServiceContainer()->getPermissionManager()->getAllPermissions()
76 );
77
78 $missingRights = array_diff(
79 array_keys( $this->getConfVar( MainConfigNames::RateLimits ) ),
80 $knownRights
81 );
82
83 $this->assertEquals(
84 [],
85 // Re-index to produce nicer output, keys are meaningless.
86 array_values( $missingRights ),
87 'All keys in $wgRateLimits must be listed in $wgAvailableRights or $wgImplicitRights, ' .
88 'unless the keys are defined as rights by MediaWiki core.'
89 );
90 }
91
92 /**
93 * Test, if for all rights an action- message exists,
94 * which is used on Special:ListGroupRights as help text
95 * Extensions and core
96 *
97 * @coversNothing
98 */
99 public function testAllActionsWithMessages() {
100 $this->checkMessagesExist( 'action-' );
101 }
102
103 /**
104 * Test, if for all rights a right- message exists,
105 * which is used on Special:ListGroupRights as help text
106 * Extensions and core
107 */
108 public function testAllRightsWithMessage() {
109 $this->checkMessagesExist( 'right-' );
110 }
111
112 /**
113 * @param string $prefix
114 */
115 private function checkMessagesExist( $prefix ) {
116 // Getting all user rights, for core: User::$mCoreRights, for extensions: $wgAvailableRights
117 $services = $this->getServiceContainer();
118 $allRights = $services->getPermissionManager()->getAllPermissions();
119 $allMessageKeys = $services->getLocalisationCache()->getSubitemList( 'en', 'messages' );
120
121 $messagesToCheck = [];
122 foreach ( $allMessageKeys as $message ) {
123 if ( str_starts_with( $message, $prefix ) ) {
124 $messagesToCheck[] = substr( $message, strlen( $prefix ) );
125 }
126 }
127
128 $missing = array_diff(
129 $allRights,
130 $messagesToCheck
131 );
132
133 $this->assertEquals(
134 [],
135 $missing,
136 "Each user right (core/extensions) has a corresponding $prefix message."
137 . ' See the instructions at: '
138 . 'https://www.mediawiki.org/wiki/Manual:User_rights#Adding_new_rights'
139 );
140 }
141 }