UserRoleDeleteTest.php in Drupal 9
File
core/modules/user/tests/src/Kernel/UserRoleDeleteTest.php
View source
<?php
namespace Drupal\Tests\user\Kernel;
use Drupal\filter\Entity\FilterFormat;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
class UserRoleDeleteTest extends KernelTestBase {
protected static $modules = [
'system',
'user',
'field',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
}
public function testRoleDeleteUserRoleReferenceDelete() {
$role_storage = $this->container
->get('entity_type.manager')
->getStorage('user_role');
$role_storage
->create([
'id' => 'test_role_one',
])
->save();
$role_storage
->create([
'id' => 'test_role_two',
])
->save();
$values = [
'uid' => 1,
'name' => $this
->randomString(),
'roles' => [
'test_role_one',
'test_role_two',
],
];
$user = User::create($values);
$user
->save();
$this
->assertTrue($user
->hasRole('test_role_one'));
$this
->assertTrue($user
->hasRole('test_role_two'));
$test_role_one = $role_storage
->load('test_role_one');
$test_role_one
->delete();
$user = User::load($user
->id());
$this
->assertFalse($user
->hasRole('test_role_one'));
$this
->assertTrue($user
->hasRole('test_role_two'));
$role_storage
->create([
'id' => 'test_role_one',
])
->save();
$user = User::load($user
->id());
$this
->assertFalse($user
->hasRole('test_role_one'));
$this
->assertTrue($user
->hasRole('test_role_two'));
}
public function testDependenciesRemoval() {
$this
->enableModules([
'node',
'filter',
]);
$role_storage = $this->container
->get('entity_type.manager')
->getStorage('user_role');
$role = Role::create([
'id' => 'test_role',
'label' => $this
->randomString(),
]);
$role
->save();
$node_type = NodeType::create([
'type' => mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomString(),
]);
$node_type
->save();
$format = FilterFormat::create([
'format' => mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomString(),
]);
$format
->save();
$permission_format = "use text format {$format->id()}";
$permission_node_type = "edit any {$node_type->id()} content";
$permission_node_type_create = "create {$node_type->id()} content";
$role
->grantPermission('access content')
->grantPermission($permission_format)
->grantPermission($permission_node_type)
->grantPermission($permission_node_type_create)
->save();
$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));
$format
->delete();
$role_storage
->resetCache();
$role = Role::load($role
->id());
$this
->assertNotNull($role);
$this
->assertFalse($role
->hasPermission($permission_format));
$this
->assertTrue($role
->hasPermission($permission_node_type));
$this
->assertTrue($role
->hasPermission($permission_node_type_create));
$this->container
->get('config.manager')
->uninstall('module', 'node');
$this
->disableModules([
'node',
]);
$role_storage
->resetCache();
$role = Role::load($role
->id());
$this
->assertNotNull($role);
$this
->assertFalse($role
->hasPermission($permission_format));
$this
->assertFalse($role
->hasPermission($permission_node_type));
$this
->assertFalse($role
->hasPermission($permission_node_type_create));
$this
->assertTrue($role
->hasPermission('access content'));
}
}