You are here

function UserRolesAssignmentTest::testCreateUserWithRole in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/user/src/Tests/UserRolesAssignmentTest.php \Drupal\user\Tests\UserRolesAssignmentTest::testCreateUserWithRole()

Tests that when creating a user the role can be assigned. And that it can be removed again.

File

core/modules/user/src/Tests/UserRolesAssignmentTest.php, line 50
Contains \Drupal\user\Tests\UserRolesAssignmentTest.

Class

UserRolesAssignmentTest
Tests that users can be assigned and unassigned roles.

Namespace

Drupal\user\Tests

Code

function testCreateUserWithRole() {
  $rid = $this
    ->drupalCreateRole(array(
    'administer users',
  ));

  // Create a new user and add the role at the same time.
  $edit = array(
    'name' => $this
      ->randomMachineName(),
    'mail' => $this
      ->randomMachineName() . '@example.com',
    'pass[pass1]' => $pass = $this
      ->randomString(),
    'pass[pass2]' => $pass,
    "roles[{$rid}]" => $rid,
  );
  $this
    ->drupalPostForm('admin/people/create', $edit, t('Create new account'));
  $this
    ->assertText(t('Created a new user account for @name.', array(
    '@name' => $edit['name'],
  )));

  // Get the newly added user.
  $account = user_load_by_name($edit['name']);
  $this
    ->drupalGet('user/' . $account
    ->id() . '/edit');
  $this
    ->assertFieldChecked('edit-roles-' . $rid, 'Role is assigned.');
  $this
    ->userLoadAndCheckRoleAssigned($account, $rid);

  // Remove the role again.
  $this
    ->drupalPostForm('user/' . $account
    ->id() . '/edit', array(
    "roles[{$rid}]" => FALSE,
  ), t('Save'));
  $this
    ->assertText(t('The changes have been saved.'));
  $this
    ->assertNoFieldChecked('edit-roles-' . $rid, 'Role is removed from user.');
  $this
    ->userLoadAndCheckRoleAssigned($account, $rid, FALSE);
}