function PermissionHandlerTest::testPermissionsYamlStaticAndCallback

Same name in other branches
  1. 9 core/modules/user/tests/src/Unit/PermissionHandlerTest.php \Drupal\Tests\user\Unit\PermissionHandlerTest::testPermissionsYamlStaticAndCallback()
  2. 10 core/modules/user/tests/src/Unit/PermissionHandlerTest.php \Drupal\Tests\user\Unit\PermissionHandlerTest::testPermissionsYamlStaticAndCallback()
  3. 11.x 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 296

Class

PermissionHandlerTest
Tests the permission handler.

Namespace

Drupal\Tests\user\Unit

Code

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',
    ];
    $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->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($actual_permissions['access module a']['title'], 'Access A');
    $this->assertEquals($actual_permissions['access module a']['provider'], 'module_a');
    $this->assertEquals($actual_permissions['access module a']['description'], 'bla bla');
    $this->assertEquals($actual_permissions['access module b']['title'], 'Access B');
    $this->assertEquals($actual_permissions['access module b']['provider'], 'module_a');
    $this->assertEquals($actual_permissions['access module b']['description'], 'bla bla');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.