UserRoleDeleteTest.php in Drupal 8
File
core/modules/user/tests/src/Kernel/UserRoleDeleteTest.php
View source
<?php
namespace Drupal\Tests\user\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\User;
class UserRoleDeleteTest extends KernelTestBase {
public static $modules = [
'system',
'user',
'field',
];
protected function setUp() {
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'));
}
}