You are here

public function PermissionHandlerTest::testPermissionsYamlStaticAndCallback in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 290
Contains \Drupal\Tests\user\Unit\PermissionHandlerTest.

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
    ->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
  $this->moduleHandler
    ->expects($this
    ->once())
    ->method('getModuleDirectories')
    ->willReturn(array(
    '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 a':\n  title: 'Access A'\n  description: 'bla bla'\npermission_callbacks:\n  - 'Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription'\n");
  $modules = array(
    'module_a',
  );
  $extensions = array(
    'module_a' => $this
      ->mockModuleExtension('module_a', 'Module a'),
  );
  $this->moduleHandler
    ->expects($this
    ->any())
    ->method('getImplementations')
    ->with('permission')
    ->willReturn(array());
  $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(array(
    new TestPermissionCallbacks(),
    'titleDescription',
  ));
  $this->permissionHandler = new TestPermissionHandler($this->moduleHandler, $this->stringTranslation, $this->controllerResolver);

  // Setup system_rebuild_module_data().
  $this->permissionHandler
    ->setSystemRebuildModuleData($extensions);
  $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');
}