diff --git a/core/modules/user/src/PermissionHandler.php b/core/modules/user/src/PermissionHandler.php index c396bcf18cce28de6c659feee7f1039685e0fe4d..436e11e9717d73c6dedf9c2ce374310b498eab81 100644 --- a/core/modules/user/src/PermissionHandler.php +++ b/core/modules/user/src/PermissionHandler.php @@ -80,7 +80,7 @@ public function getPermissions() { $all_permissions += $this->buildPermissionsModules(); - return $this->sortPermissionsByProviderName($all_permissions); + return $this->sortPermissions($all_permissions); } /** @@ -138,7 +138,7 @@ protected function buildPermissionsModules() { } /** - * Sorts the given permissions by provider name. + * Sorts the given permissions by provider name and title. * * @param array $all_permissions * The permissions to be sorted. @@ -149,13 +149,18 @@ protected function buildPermissionsModules() { * - description: The description of the permission, defaults to NULL. * - provider: The provider of the permission. */ - protected function sortPermissionsByProviderName(array $all_permissions = array()) { + protected function sortPermissions(array $all_permissions = array()) { // Get a list of all the modules providing permissions and sort by // display name. $modules = $this->getModuleNames(); uasort($all_permissions, function (array $permission_a, array $permission_b) use ($modules) { - return $modules[$permission_a['provider']] > $modules[$permission_b['provider']]; + if ($modules[$permission_a['provider']] == $modules[$permission_b['provider']]) { + return $permission_a['title'] > $permission_b['title']; + } + else { + return $modules[$permission_a['provider']] > $modules[$permission_b['provider']]; + } }); return $all_permissions; } diff --git a/core/modules/user/tests/src/PermissionHandlerTest.php b/core/modules/user/tests/src/PermissionHandlerTest.php index 8856dd8432a53e21662725ad44f0de74039a84b1..9a5597f5bdd479dc223b0ba647f4b2529087fb10 100644 --- a/core/modules/user/tests/src/PermissionHandlerTest.php +++ b/core/modules/user/tests/src/PermissionHandlerTest.php @@ -75,7 +75,7 @@ protected function mockModuleExtension($module, $name) { * @covers ::getPermissions * @covers ::buildPermissions * @covers ::buildPermissionsModules - * @covers ::sortPermissionsByProviderName + * @covers ::sortPermissions * @covers ::getModuleNames */ public function testBuildPermissionsModules() { @@ -124,6 +124,7 @@ public function testBuildPermissionsModules() { $this->assertSame(array('access_module_a', 'access_module_c', 'access module b'), array_keys($actual_permissions)); } + /** * Tests permissions provided by YML files. * @@ -188,6 +189,56 @@ public function testBuildPermissionsYaml() { $this->assertPermissions($actual_permissions); } + /** + * Tests permissions sort inside a module. + * + * @covers ::__construct + * @covers ::getPermissions + * @covers ::buildPermissions + * @covers ::buildPermissionsYaml + * @covers ::sortPermissions + */ + public function testBuildPermissionsSortPerModule() { + vfsStreamWrapper::register(); + $root = new vfsStreamDirectory('modules'); + vfsStreamWrapper::setRoot($root); + + $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); + $this->moduleHandler->expects($this->once()) + ->method('getModuleDirectories') + ->willReturn([ + 'module_a' => vfsStream::url('modules/module_a'), + ]); + + $url = vfsStream::url('modules'); + mkdir($url . '/module_a'); + file_put_contents($url . '/module_a/module_a.permissions.yml', +"access_module_a2: single_description +access_module_a1: single_description" + ); + $modules = ['module_a']; + $extensions = [ + 'module_a' => $this->mockModuleExtension('module_a', 'Module a'), + ]; + $this->moduleHandler->expects($this->any()) + ->method('getImplementations') + ->with('permission') + ->willReturn([]); + + $this->moduleHandler->expects($this->any()) + ->method('getModuleList') + ->willReturn(array_flip($modules)); + + $this->permissionHandler = new TestPermissionHandler($this->moduleHandler, $this->stringTranslation); + + // Setup system_rebuild_module_data(). + $this->permissionHandler->setSystemRebuildModuleData($extensions); + + $actual_permissions = $this->permissionHandler->getPermissions(); + + $this->assertEquals(['access_module_a1', 'access_module_a2'], array_keys($actual_permissions)); + } + /** * Checks that the permissions are like expected. *