View source
<?php
class RoleExpireWebTestCase extends DrupalWebTestCase {
protected $privileged_user;
public static function getInfo() {
return array(
'name' => 'Role Expire web tests',
'description' => 'Web tests for the Role Expire module.',
'group' => 'Role Expire',
);
}
public function setUp() {
parent::setUp('role_expire');
$this->privileged_user = $this
->drupalCreateUser(array(
'administer permissions',
'administer role expire',
));
$this
->drupalLogin($this->privileged_user);
}
public function testRoleExpire_tests() {
$this
->drupalGet('user/' . $this->privileged_user->uid . '/edit');
$str = 'role expiration time';
if (!$this
->assertText(t($str), t('Testing for "%str" on page.', array(
'%str' => $str,
)))) {
$this
->error(t('Testing aborted: %str missing', array(
'%str' => $str,
)));
return FALSE;
}
$roles = user_roles(TRUE);
$rid = max(array_keys($roles));
$rname = $roles[$rid];
if ($rname == t('authenticated user')) {
$this
->error(t('Testing aborted before completion: no roles to test on.'));
return FALSE;
}
$field = 'role_expire';
foreach (array(
'12321',
'',
) as $str) {
$edit = array(
$field => $str,
);
$this
->drupalPost('admin/people/permissions/roles/edit/' . $rid, $edit, t('Save role'));
$result = $this
->drupalGet('admin/people/permissions/roles/edit/' . $rid);
$this
->assertFieldByName($field, $str, t('Looking for value "%str" in %field field', array(
'%str' => $str,
'%field' => $field,
)));
}
$field = 'role_expire_' . $rid;
$user_edit_path = 'user/' . $this->privileged_user->uid . '/edit';
$yesterday = date('Y-m-d H:i:s', strtotime('-1 day'));
$edit = array(
$field => $yesterday,
);
$result = $this
->drupalPost($user_edit_path, $edit, t('Save'));
$this
->assertText(t('Role expiry must be in the future.'), t('Yesterday should be invalid'));
$invalid_date = 'completely invalid date';
$edit = array(
$field => $invalid_date,
);
$this
->drupalPost($user_edit_path, $edit, t('Save'));
$this
->assertText(t('Role expiry is not in correct format.'), t('Date must be a date'));
$blank = '';
$edit = array(
$field => $blank,
);
$this
->drupalPost($user_edit_path, $edit, t('Save'));
$this
->assertNoRaw(t('error'), t('Blank date should be valid.'));
$tomorrow = date('Y-m-d H:i:s', strtotime('+1 day'));
$edit = array(
$field => $tomorrow,
);
$this
->drupalPost($user_edit_path, $edit, t('Save'));
$this
->assertFieldByName($field, $tomorrow, t('Tomorrow (%t) should be valid', array(
'%t' => $tomorrow,
)));
$checkbox = "roles[{$rid}]";
$edit = array(
$checkbox => FALSE,
);
$this
->drupalPost($user_edit_path, $edit, t('Save'));
if (!$this
->assertRaw($checkbox, t('Making sure role checkbox is still on page.'))) {
$this
->error(t('Testing aborted before completion: checkbox missing.'));
return FALSE;
}
$this
->assertNoRaw(t('error'), t('Unassigning role with expiration should be OK.'));
$edit = array(
$checkbox => TRUE,
$field => $tomorrow,
);
$this
->drupalPost($user_edit_path, $edit, t('Save'));
$this
->assertText($rname, t('Looking for role name "%r" on page.', array(
'%r' => $name,
)));
$this
->assertNoRaw(t('error'), t('Re-adding role with expiration date.'));
$admin_user = $this
->drupalCreateUser(array(
'administer users',
));
$this
->drupalLogin($admin_user);
$this
->drupalGet($user_edit_path);
$this
->drupalPost(NULL, NULL, t('Cancel account'));
$this
->drupalPost(NULL, NULL, t('Cancel account'));
$this
->assertRaw(t('%name has been deleted.', array(
'%name' => $account->name,
)), t('User deleted.'));
$this
->assertNoRaw(t('error'), t('Deleting user with a role that has an expiration date.'));
return TRUE;
}
public function testAssignAndRemoveRole() {
$admin_user = $this
->drupalCreateUser(array(
'administer users',
'administer role expire',
));
$this
->drupalLogin($admin_user);
$victim_user = $this
->drupalCreateUser();
$all_rids = array_keys($victim_user->roles);
sort($all_rids);
$rid = array_pop($all_rids);
$this
->drupalPost('user/' . $victim_user->uid . '/edit', array(
"roles[{$rid}]" => $rid,
), t('Save'));
$this
->assertText(t('The changes have been saved.'));
$this
->assertFieldChecked('edit-roles-' . $rid, t('Role is assigned.'));
$this
->drupalPost('user/' . $victim_user->uid . '/edit', array(
"roles[{$rid}]" => FALSE,
), t('Save'));
$this
->assertText(t('The changes have been saved.'));
$this
->assertNoFieldChecked('edit-roles-' . $rid, t('Role is removed from user.'));
}
}