AvailableRightsTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Try to make sure that extensions register all rights in $wgAvailableRights
  4. * or via the 'UserGetAllRights' hook.
  5. *
  6. * @author Marius Hoch < hoo@online.de >
  7. */
  8. class AvailableRightsTest extends PHPUnit\Framework\TestCase {
  9. use MediaWikiCoversValidator;
  10. /**
  11. * Returns all rights that should be in $wgAvailableRights + all rights
  12. * registered via the 'UserGetAllRights' hook + all "core" rights.
  13. *
  14. * @return string[]
  15. */
  16. private function getAllVisibleRights() {
  17. global $wgGroupPermissions, $wgRevokePermissions;
  18. $rights = User::getAllRights();
  19. foreach ( $wgGroupPermissions as $permissions ) {
  20. $rights = array_merge( $rights, array_keys( $permissions ) );
  21. }
  22. foreach ( $wgRevokePermissions as $permissions ) {
  23. $rights = array_merge( $rights, array_keys( $permissions ) );
  24. }
  25. $rights = array_unique( $rights );
  26. sort( $rights );
  27. return $rights;
  28. }
  29. /**
  30. * @coversNothing
  31. */
  32. public function testAvailableRights() {
  33. $missingRights = array_diff(
  34. $this->getAllVisibleRights(),
  35. User::getAllRights()
  36. );
  37. $this->assertEquals(
  38. [],
  39. // Re-index to produce nicer output, keys are meaningless.
  40. array_values( $missingRights ),
  41. 'Additional user rights need to be added to $wgAvailableRights or ' .
  42. 'via the "UserGetAllRights" hook. See the instructions at: ' .
  43. 'https://www.mediawiki.org/wiki/Manual:User_rights#Adding_new_rights'
  44. );
  45. }
  46. /**
  47. * Test, if for all rights a right- message exist,
  48. * which is used on Special:ListGroupRights as help text
  49. * Extensions and core
  50. *
  51. * @coversNothing
  52. */
  53. public function testAllRightsWithMessage() {
  54. // Getting all user rights, for core: User::$mCoreRights, for extensions: $wgAvailableRights
  55. $allRights = User::getAllRights();
  56. $allMessageKeys = Language::getMessageKeysFor( 'en' );
  57. $rightsWithMessage = [];
  58. foreach ( $allMessageKeys as $message ) {
  59. // === 0: must be at beginning of string (position 0)
  60. if ( strpos( $message, 'right-' ) === 0 ) {
  61. $rightsWithMessage[] = substr( $message, strlen( 'right-' ) );
  62. }
  63. }
  64. $missing = array_diff(
  65. $allRights,
  66. $rightsWithMessage
  67. );
  68. $this->assertEquals(
  69. [],
  70. $missing,
  71. 'Each user rights (core/extensions) has a corresponding right- message.'
  72. );
  73. }
  74. }