function UserRolesAssignmentTestCase::testAssignAndRemoveRole

Tests that a user can be assigned a role and that the role can be removed again.

File

modules/user/user.test, line 2723

Class

UserRolesAssignmentTestCase
Test role assignment.

Code

function testAssignAndRemoveRole() {
    $rid = $this->drupalCreateRole(array(
        'administer content types',
    ));
    $account = $this->drupalCreateUser();
    // Assign the role to the user.
    $this->drupalPost('user/' . $account->uid . '/edit', array(
        "roles[{$rid}]" => $rid,
    ), t('Save'));
    $this->assertText(t('The changes have been saved.'));
    $this->assertFieldChecked('edit-roles-' . $rid, 'Role is assigned.');
    $this->userLoadAndCheckRoleAssigned($account, $rid);
    // Remove the role from the user.
    $this->drupalPost('user/' . $account->uid . '/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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.