Re-introduce AvailableRightsTest for User::getAllRights completeness
authorHoo man <hoo@online.de>
Sat, 21 Feb 2015 17:21:19 +0000 (17:21 +0000)
committerHoo man <hoo@online.de>
Sun, 1 Mar 2015 17:38:15 +0000 (17:38 +0000)
Because extensions often don't add their rights to $wgAvailableRights
or via the 'UserGetAllRights' hook, thus User::getAllRights is incomplete.

As announced on https://lists.wikimedia.org/pipermail/wikitech-l/2015-February/080953.html

This reverts commit 9a16d3a37e0a00951139bc5e8b62bfa16306a6e4.

Change-Id: Ic394145d123cd74bdd5b912559f56c393f1d5919

includes/User.php
tests/phpunit/structure/AvailableRightsTest.php [new file with mode: 0644]

index 7025717..027f1c1 100644 (file)
@@ -4416,7 +4416,7 @@ class User implements IDBAccessObject {
 
        /**
         * Get a list of all available permissions.
-        * @return array Array of permission names
+        * @return string[] Array of permission names
         */
        public static function getAllRights() {
                if ( self::$mAllRights === false ) {
diff --git a/tests/phpunit/structure/AvailableRightsTest.php b/tests/phpunit/structure/AvailableRightsTest.php
new file mode 100644 (file)
index 0000000..51d31aa
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+
+/**
+ * Try to make sure that extensions register all rights in $wgAvailableRights
+ * or via the 'UserGetAllRights' hook.
+ *
+ * @author Marius Hoch < hoo@online.de >
+ */
+class AvailableRightsTest extends PHPUnit_Framework_TestCase {
+
+       /**
+        * Returns all rights that should be in $wgAvailableRights + all rights
+        * registered via the 'UserGetAllRights' hook + all "core" rights.
+        *
+        * @return string[]
+        */
+       private function getAllVisibleRights() {
+               global $wgGroupPermissions, $wgRevokePermissions;
+
+               $rights = User::getAllRights();
+
+               foreach( $wgGroupPermissions as $permissions ) {
+                       $rights = array_merge( $rights, array_keys( $permissions ) );
+               }
+
+               foreach( $wgRevokePermissions as $permissions ) {
+                       $rights = array_merge( $rights, array_keys( $permissions ) );
+               }
+
+               $rights = array_unique( $rights );
+               sort( $rights );
+
+               return $rights;
+       }
+
+       public function testAvailableRights() {
+               $missingRights = array_diff(
+                       $this->getAllVisibleRights(),
+                       User::getAllRights()
+               );
+
+               $this->assertEquals(
+                       array(),
+                       // Re-index to produce nicer output, keys are meaningless.
+                       array_values( $missingRights ),
+                       'Additional user rights need to be added to $wgAvailableRights or ' .
+                       'via the "UserGetAllRights" hook. See the instructions at: ' .
+                       'https://www.mediawiki.org/wiki/Manual:User_rights#Adding_new_rights'
+               );
+       }
+}