View source
<?php
namespace Drupal\Tests\user\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\User;
use Drupal\user\RoleInterface;
class UserEntityTest extends KernelTestBase {
protected static $modules = [
'system',
'user',
'field',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
}
public function testUserMethods() {
$role_storage = $this->container
->get('entity_type.manager')
->getStorage('user_role');
$role_storage
->create([
'id' => 'test_role_one',
'label' => 'Test role 1',
])
->save();
$role_storage
->create([
'id' => 'test_role_two',
'label' => 'Test role 2',
])
->save();
$role_storage
->create([
'id' => 'test_role_three',
'label' => 'Test role 3',
])
->save();
$values = [
'uid' => 1,
'roles' => [
'test_role_one',
],
];
$user = User::create($values);
$this
->assertTrue($user
->hasRole('test_role_one'));
$this
->assertFalse($user
->hasRole('test_role_two'));
$this
->assertEquals([
RoleInterface::AUTHENTICATED_ID,
'test_role_one',
], $user
->getRoles());
$user
->addRole('test_role_one');
$this
->assertTrue($user
->hasRole('test_role_one'));
$this
->assertFalse($user
->hasRole('test_role_two'));
$this
->assertEquals([
RoleInterface::AUTHENTICATED_ID,
'test_role_one',
], $user
->getRoles());
$user
->addRole('test_role_two');
$this
->assertTrue($user
->hasRole('test_role_one'));
$this
->assertTrue($user
->hasRole('test_role_two'));
$this
->assertEquals([
RoleInterface::AUTHENTICATED_ID,
'test_role_one',
'test_role_two',
], $user
->getRoles());
$user
->removeRole('test_role_three');
$this
->assertTrue($user
->hasRole('test_role_one'));
$this
->assertTrue($user
->hasRole('test_role_two'));
$this
->assertEquals([
RoleInterface::AUTHENTICATED_ID,
'test_role_one',
'test_role_two',
], $user
->getRoles());
$user
->removeRole('test_role_one');
$this
->assertFalse($user
->hasRole('test_role_one'));
$this
->assertTrue($user
->hasRole('test_role_two'));
$this
->assertEquals([
RoleInterface::AUTHENTICATED_ID,
'test_role_two',
], $user
->getRoles());
}
public function testUserValidation() {
$user = User::create([]);
foreach ($user as $field_name => $field) {
if (!in_array($field_name, [
'uid',
])) {
$user->{$field_name}
->generateSampleItems();
}
}
$violations = $user
->validate();
$this
->assertFalse((bool) $violations
->count());
}
public function testChainExistingPasswordMethod() {
$user = User::create([
'name' => $this
->randomMachineName(),
]);
$user = $user
->setExistingPassword('existing_pass');
$this
->assertInstanceOf(User::class, $user);
}
}