function PermissionHandlerTest::testPermissionsYamlStaticAndCallback

Same name in other branches
  1. 8.9.x 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 269

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',
    ];
    $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']);
}

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