You are here

public function ContentRoleManagerTest::test in Lightning Core 8.5

Same name and namespace in other branches
  1. 8.4 modules/lightning_roles/tests/src/Kernel/ContentRoleManagerTest.php \Drupal\Tests\lightning_roles\Kernel\ContentRoleManagerTest::test()

Tests reacting to the creation and deletion of node types.

@covers \Drupal\lightning_roles\ContentRoleManager::grantPermissions

File

modules/lightning_roles/tests/src/Kernel/ContentRoleManagerTest.php, line 40

Class

ContentRoleManagerTest
@group lightning @group lightning_roles @group orca_public

Namespace

Drupal\Tests\lightning_roles\Kernel

Code

public function test() {

  // Don't bother with ContentTypeCreationTrait, since it will try to create
  // things like the 'body' field, which is irrelevant in this test.
  $node_type = NodeType::create([
    'type' => 'page',
  ]);
  $node_type
    ->save();

  // Give page creators the keys to the kingdom...
  $this->container
    ->get('lightning.content_roles')
    ->grantPermissions('creator', [
    'bypass node access',
  ]);

  // Ensure the 'creator' role has all the expected OOTB permissions, plus
  // the one we just granted them.

  /** @var \Drupal\user\RoleInterface $role */
  $role = Role::load('page_creator');
  $expected_creator_permissions = [
    'create page content',
    'edit own page content',
    'view page revisions',
    'view own unpublished content',
    'create url aliases',
    'access in-place editing',
    'access contextual links',
    'access toolbar',
    'bypass node access',
  ];
  foreach ($expected_creator_permissions as $permission) {
    $this
      ->assertTrue($role
      ->hasPermission($permission));
  }

  // Content roles should never be administrators.
  $this
    ->assertFalse($role
    ->isAdmin());

  // Ensure the 'reviewer' role has all the expected OOTB permissions.
  $role = Role::load('page_reviewer');
  $expected_reviewer_permissions = [
    'access content overview',
    'edit any page content',
    'delete any page content',
  ];
  foreach ($expected_reviewer_permissions as $permission) {
    $this
      ->assertTrue($role
      ->hasPermission($permission));
  }
  $this
    ->assertFalse($role
    ->isAdmin());

  // After deleting the content type, both roles should be gone too.
  $node_type
    ->delete();
  $roles = Role::loadMultiple([
    'page_creator',
    'page_reviewer',
  ]);
  $this
    ->assertEmpty($roles);
}