View source
<?php
namespace Drupal\Tests\user_expire\Functional;
use Drupal\Core\Database\Database;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class UserExpireTest extends BrowserTestBase {
use StringTranslationTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'user_expire',
];
public function testUserExpire() {
$connection = Database::getConnection();
$basic_account = $this
->drupalCreateUser();
$this
->assertTrue($basic_account
->isActive(), $this
->t('User account is currently enabled.'));
user_expire_set_expiration($basic_account, \Drupal::time()
->getRequestTime() - 1);
user_expire_expire_users([
$basic_account,
]);
$this
->assertFalse($basic_account
->isActive(), $this
->t('User account has been successfully disabled.'));
$admin_user = $this
->drupalCreateUser([
'administer permissions',
'administer users',
'set user expiration',
'view expiring users report',
'administer user expire settings',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/reports/expiring-users');
$this
->assertNoText('0 sec from now', $this
->t('Processed expiration does not show in Expiring users report'));
$edit = [];
$edit['status'] = 1;
$edit['user_expiration'] = 1;
$edit['user_expiration_date[date]'] = "2002-08-18";
$this
->drupalPostForm("user/" . $basic_account
->id() . "/edit", $edit, $this
->t('Save'));
$this
->assertRaw('type="radio" id="edit-status-1" name="status" value="1" checked="checked" class="form-radio"', $this
->t('User account is currently enabled.'));
$this
->assertRaw('expiration date is set to Sun, 08/18/2002 - 00:00.');
$this
->drupalGet('admin/reports/expiring-users');
$this
->assertRaw('0 sec from now', 'Expiration shows in Expiring users report');
$this
->drupalLogout($admin_user);
$this
->drupalLogin($basic_account);
$edit = [];
$edit['pass[pass1]'] = $new_pass = $this
->randomMachineName();
$edit['pass[pass2]'] = $new_pass;
$edit['current_pass'] = $basic_account->pass_raw;
$this
->drupalPostForm("user/" . $basic_account
->id() . "/edit", $edit, $this
->t('Save'));
$this
->assertRaw($this
->t("The changes have been saved."));
$this
->drupalLogout($basic_account);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/reports/expiring-users');
$this
->assertRaw('0 sec from now', 'Expiration shows in Expiring users report');
user_expire_cron();
$this
->drupalGet('admin/reports/expiring-users');
$this
->assertNoText('0 sec from now', $this
->t('Processed expiration does not show in Expiring users report'));
$this
->drupalGet("user/" . $basic_account
->id() . "/edit");
$this
->assertRaw('type="radio" id="edit-status-0" name="status" value="0" checked="checked" class="form-radio"', $this
->t('User account is currently disabled.'));
$rid = $this
->drupalCreateRole([]);
$edit = [
'label' => $rid,
'id' => $rid . '_role',
];
$this
->drupalPostForm('admin/people/roles/add', $edit, $this
->t('Save'));
$this
->assertText($this
->t('Role @name has been added.', [
'@name' => $rid,
]), 'Role ' . $rid . ' has been added.');
$role = Role::load($rid);
$this
->assertTrue(is_object($role), 'The role was successfully retrieved from the database.');
$edit = [];
$edit['status'] = 1;
$edit['user_expiration'] = FALSE;
$edit['roles[' . $rid . ']'] = $rid;
$this
->drupalPostForm("user/" . $basic_account
->id() . "/edit", $edit, $this
->t('Save'));
$this
->assertRaw('type="radio" id="edit-status-1" name="status" value="1" checked="checked" class="form-radio"', $this
->t('User account is currently enabled.'));
$this
->drupalGet('admin/reports/expiring-users');
$this
->assertNoText('0 sec from now', $this
->t('Processed expiration does not show in Expiring users report'));
$connection
->query('UPDATE {users_field_data} SET access = :time WHERE uid = :uid', [
':time' => \Drupal::time()
->getRequestTime() - 7776002,
':uid' => $basic_account
->id(),
]);
$edit = [
'user_expire_' . $rid => 7776000,
];
$this
->drupalPostForm("admin/config/people/user-expire", $edit, $this
->t('Save configuration'));
user_expire_expire_by_role();
$this
->drupalGet("user/" . $basic_account
->id() . "/edit");
$this
->assertRaw('type="radio" id="edit-status-0" name="status" value="0" checked="checked" class="form-radio"', $this
->t('User account is currently disabled.'));
$new_basic_account = $this
->drupalCreateUser();
$edit = [
'user_expire_' . RoleInterface::AUTHENTICATED_ID => 7776000,
];
$this
->drupalPostForm("admin/config/people/user-expire", $edit, $this
->t('Save configuration'));
user_expire_expire_by_role();
$this
->drupalGet("user/" . $new_basic_account
->id() . "/edit");
$this
->assertRaw('type="radio" id="edit-status-1" name="status" value="1" checked="checked" class="form-radio"', $this
->t('New user account stays active.'));
$connection
->query('UPDATE {users_field_data} SET created = :time WHERE uid = :uid', [
':time' => \Drupal::time()
->getRequestTime() - 7776002,
':uid' => $new_basic_account
->id(),
]);
user_expire_expire_by_role();
$this
->drupalGet("user/" . $new_basic_account
->id() . "/edit");
$this
->assertRaw('type="radio" id="edit-status-0" name="status" value="0" checked="checked" class="form-radio"', $this
->t('User account is currently disabled.'));
}
}