From d333cd8f7be190d7e296721caa79478ab7ef1b20 Mon Sep 17 00:00:00 2001 From: Marius Hoch Date: Mon, 9 Feb 2015 23:50:35 +0100 Subject: [PATCH] Add AvailableRightsTest for User::getAllRights completeness Because extensions often don't add their rights to $wgAvailableRights or via the 'UserGetAllRights' hook, thus User::getAllRights is incomplete. Change-Id: Id9ae9eff71e822ec5c038c1c1f990ac36f05cea8 --- includes/User.php | 2 +- .../phpunit/structure/AvailableRightsTest.php | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/structure/AvailableRightsTest.php diff --git a/includes/User.php b/includes/User.php index c2db67a2a7..961c510b19 100644 --- a/includes/User.php +++ b/includes/User.php @@ -4399,7 +4399,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 index 0000000000..9737e82848 --- /dev/null +++ b/tests/phpunit/structure/AvailableRightsTest.php @@ -0,0 +1,45 @@ + + */ +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(), + array_values( $missingRights ), // Re-Index to produce nicer output, keys are meaningless + 'Additional user rights need to be added to $wgAvailableRights or via the "UserGetAllRights" hook' + ); + } +} -- 2.20.1