You are here

public function PermissionHandlerTest::testBuildPermissionsYamlCallback 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::testBuildPermissionsYamlCallback()

Tests dynamic callback permissions provided by YML files.

@covers ::__construct @covers ::getPermissions @covers ::buildPermissionsYaml

File

core/modules/user/tests/src/Unit/PermissionHandlerTest.php, line 218
Contains \Drupal\Tests\user\Unit\PermissionHandlerTest.

Class

PermissionHandlerTest
Tests the permission handler.

Namespace

Drupal\Tests\user\Unit

Code

public function testBuildPermissionsYamlCallback() {
  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'),
    'module_b' => vfsStream::url('modules/module_b'),
    'module_c' => vfsStream::url('modules/module_c'),
  ));
  $url = vfsStream::url('modules');
  mkdir($url . '/module_a');
  file_put_contents($url . '/module_a/module_a.permissions.yml', "permission_callbacks:\n  - 'Drupal\\user\\Tests\\TestPermissionCallbacks::singleDescription'\n");
  mkdir($url . '/module_b');
  file_put_contents($url . '/module_b/module_b.permissions.yml', "permission_callbacks:\n  - 'Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription'\n");
  mkdir($url . '/module_c');
  file_put_contents($url . '/module_c/module_c.permissions.yml', "permission_callbacks:\n  - 'Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescriptionRestrictAccess'\n");
  $modules = array(
    'module_a',
    'module_b',
    'module_c',
  );
  $extensions = array(
    'module_a' => $this
      ->mockModuleExtension('module_a', 'Module a'),
    'module_b' => $this
      ->mockModuleExtension('module_b', 'Module b'),
    'module_c' => $this
      ->mockModuleExtension('module_c', 'Module c'),
  );
  $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
    ->at(0))
    ->method('getControllerFromDefinition')
    ->with('Drupal\\user\\Tests\\TestPermissionCallbacks::singleDescription')
    ->willReturn(array(
    new TestPermissionCallbacks(),
    'singleDescription',
  ));
  $this->controllerResolver
    ->expects($this
    ->at(1))
    ->method('getControllerFromDefinition')
    ->with('Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription')
    ->willReturn(array(
    new TestPermissionCallbacks(),
    'titleDescription',
  ));
  $this->controllerResolver
    ->expects($this
    ->at(2))
    ->method('getControllerFromDefinition')
    ->with('Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescriptionRestrictAccess')
    ->willReturn(array(
    new TestPermissionCallbacks(),
    'titleDescriptionRestrictAccess',
  ));
  $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
    ->assertPermissions($actual_permissions);
}