You are here

public function UserEntityTest::testUserMethods in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/Tests/UserEntityTest.php \Drupal\user\Tests\UserEntityTest::testUserMethods()

Tests some of the methods.

See also

\Drupal\user\Entity\User::getRoles()

\Drupal\user\Entity\User::addRole()

\Drupal\user\Entity\User::removeRole()

File

core/modules/user/src/Tests/UserEntityTest.php, line 36
Contains \Drupal\user\Tests\UserEntityTest.

Class

UserEntityTest
Tests the user entity class.

Namespace

Drupal\user\Tests

Code

public function testUserMethods() {
  $role_storage = $this->container
    ->get('entity.manager')
    ->getStorage('user_role');
  $role_storage
    ->create(array(
    'id' => 'test_role_one',
  ))
    ->save();
  $role_storage
    ->create(array(
    'id' => 'test_role_two',
  ))
    ->save();
  $role_storage
    ->create(array(
    'id' => 'test_role_three',
  ))
    ->save();
  $values = array(
    'uid' => 1,
    'roles' => array(
      'test_role_one',
    ),
  );
  $user = User::create($values);
  $this
    ->assertTrue($user
    ->hasRole('test_role_one'));
  $this
    ->assertFalse($user
    ->hasRole('test_role_two'));
  $this
    ->assertEqual(array(
    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
    ->assertEqual(array(
    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
    ->assertEqual(array(
    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
    ->assertEqual(array(
    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
    ->assertEqual(array(
    RoleInterface::AUTHENTICATED_ID,
    'test_role_two',
  ), $user
    ->getRoles());
}