public function PermissionHandlerTest::testPermissionsYamlStaticAndCallback in Drupal 9
Same name and namespace in other branches
- 8 core/modules/user/tests/src/Unit/PermissionHandlerTest.php \Drupal\Tests\user\Unit\PermissionHandlerTest::testPermissionsYamlStaticAndCallback()
Tests a YAML file containing both static permissions and a callback.
File
- core/
modules/ user/ tests/ src/ Unit/ PermissionHandlerTest.php, line 278 - Contains \Drupal\Tests\user\Unit\PermissionHandlerTest.
Class
- PermissionHandlerTest
- Tests the permission handler.
Namespace
Drupal\Tests\user\UnitCode
public function testPermissionsYamlStaticAndCallback() {
vfsStreamWrapper::register();
$root = new vfsStreamDirectory('modules');
vfsStreamWrapper::setRoot($root);
$this->moduleHandler = $this
->createMock('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', <<<EOF
'access module a':
title: 'Access A'
description: 'bla bla'
permission_callbacks:
- 'Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription'
EOF
);
$modules = [
'module_a',
];
$this->moduleHandler
->expects($this
->any())
->method('getImplementations')
->with('permission')
->willReturn([]);
$this->moduleHandler
->expects($this
->any())
->method('getModuleList')
->willReturn(array_flip($modules));
$this->controllerResolver
->expects($this
->once())
->method('getControllerFromDefinition')
->with('Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription')
->willReturn([
new TestPermissionCallbacks(),
'titleDescription',
]);
$this->permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->controllerResolver);
$actual_permissions = $this->permissionHandler
->getPermissions();
$this
->assertCount(2, $actual_permissions);
$this
->assertEquals('Access A', $actual_permissions['access module a']['title']);
$this
->assertEquals('module_a', $actual_permissions['access module a']['provider']);
$this
->assertEquals('bla bla', $actual_permissions['access module a']['description']);
$this
->assertEquals('Access B', $actual_permissions['access module b']['title']);
$this
->assertEquals('module_a', $actual_permissions['access module b']['provider']);
$this
->assertEquals('bla bla', $actual_permissions['access module b']['description']);
}