function UserRoleDeleteTest::testDependenciesRemoval

Same name and namespace in other branches
  1. 9 core/modules/user/tests/src/Kernel/UserRoleDeleteTest.php \Drupal\Tests\user\Kernel\UserRoleDeleteTest::testDependenciesRemoval()
  2. 11.x core/modules/user/tests/src/Kernel/UserRoleDeleteTest.php \Drupal\Tests\user\Kernel\UserRoleDeleteTest::testDependenciesRemoval()

Tests the removal of user role dependencies.

File

core/modules/user/tests/src/Kernel/UserRoleDeleteTest.php, line 83

Class

UserRoleDeleteTest
Tests the handling of user_role entity from the user module.

Namespace

Drupal\Tests\user\Kernel

Code

public function testDependenciesRemoval() : void {
  $this->enableModules([
    'node',
    'filter',
  ]);
  /** @var \Drupal\user\RoleStorage $role_storage */
  $role_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('user_role');
  /** @var \Drupal\user\RoleInterface $role */
  $role = Role::create([
    'id' => 'test_role',
    'label' => $this->randomString(),
  ]);
  $role->save();
  /** @var \Drupal\node\NodeTypeInterface $node_type */
  $node_type = NodeType::create([
    'type' => $this->randomMachineName(),
    'name' => $this->randomString(),
  ]);
  $node_type->save();
  // Create a new text format to be used by role $role.
  $format = FilterFormat::create([
    'format' => $this->randomMachineName(),
    'name' => $this->randomString(),
  ]);
  $format->save();
  $permission_format = "use text format {$format->id()}";
  // Add two permissions with the same dependency to ensure both are removed
  // and the role is not deleted.
  $permission_node_type = "edit any {$node_type->id()} content";
  $permission_node_type_create = "create {$node_type->id()} content";
  // Grant $role permission to access content, use $format, edit $node_type.
  $role->grantPermission('access content')
    ->grantPermission($permission_format)
    ->grantPermission($permission_node_type)
    ->grantPermission($permission_node_type_create)
    ->save();
  // The role $role has the permissions to use $format and edit $node_type.
  $role_storage->resetCache();
  $role = Role::load($role->id());
  $this->assertTrue($role->hasPermission($permission_format));
  $this->assertTrue($role->hasPermission($permission_node_type));
  $this->assertTrue($role->hasPermission($permission_node_type_create));
  // Remove the format.
  $format->delete();
  // The $role config entity exists after removing the config dependency.
  $role_storage->resetCache();
  $role = Role::load($role->id());
  $this->assertNotNull($role);
  // The $format permission should have been revoked.
  $this->assertFalse($role->hasPermission($permission_format));
  $this->assertTrue($role->hasPermission($permission_node_type));
  $this->assertTrue($role->hasPermission($permission_node_type_create));
  // We have to manually trigger the removal of configuration belonging to the
  // module because KernelTestBase::disableModules() is not aware of this.
  $this->container
    ->get('config.manager')
    ->uninstall('module', 'node');
  // Disable the node module.
  $this->disableModules([
    'node',
  ]);
  // The $role config entity exists after removing the module dependency.
  $role_storage->resetCache();
  $role = Role::load($role->id());
  $this->assertNotNull($role);
  // The $node_type permission should have been revoked too.
  $this->assertFalse($role->hasPermission($permission_format));
  $this->assertFalse($role->hasPermission($permission_node_type));
  $this->assertFalse($role->hasPermission($permission_node_type_create));
  // The 'access content' permission should not have been revoked.
  $this->assertTrue($role->hasPermission('access content'));
}

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