View source
<?php
namespace Drupal\Tests\force_password_change\Functional;
class ForcePasswordChangeFunctionalityTest extends ForcePasswordChangeBrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'force_password_change',
];
public function testImmedidatePasswordForceForAuthenticated() {
$admin_user = $this
->drupalCreateUser([
'administer force password change',
'access administration pages',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/people/force_password_change');
$this
->assertStatusCodeEquals(200);
$this
->checkCheckbox('#edit-roles-authenticated');
$this
->click('#edit-submit');
$this
->assertSession()
->addressMatches('/\\/user\\/2\\/edit$/');
$this
->assertSession()
->pageTextContains('An administrator has required that you change your password. Please change your password to proceed.');
$this
->assertSession()
->pageTextContains('An administrator has required that you change your password. Please change your password to proceed.');
$this
->fillTextValue('#edit-current-pass', $admin_user->passRaw);
$this
->fillTextValue('#edit-pass-pass1', 'asdf');
$this
->fillTextValue('#edit-pass-pass2', 'asdf');
$this
->click('#edit-submit');
$this
->assertSession()
->addressMatches('/\\/admin\\/config\\/people\\/force_password_change$/');
$this
->drupalGet('admin/config/people/force_password_change/list/authenticated');
$this
->assertStatusCodeEquals(200);
$this
->assertCheckboxExists('#edit-force-password-change');
$this
->checkCheckbox('#edit-force-password-change');
$this
->click('#edit-submit');
$this
->assertSession()
->addressMatches('/\\/user\\/2\\/edit$/');
$this
->assertSession()
->pageTextContains('An administrator has required that you change your password. Please change your password to proceed.');
}
public function testImmedidatePasswordForceForSecondaryGroup() {
$admin_user = $this
->drupalCreateUser([
'administer force password change',
'access administration pages',
]);
$this
->drupalLogin($admin_user);
$this
->createRole([], 'admin');
$admin_user
->addRole('admin');
$admin_user
->save();
$this
->drupalGet('admin/config/people/force_password_change');
$this
->assertStatusCodeEquals(200);
$this
->checkCheckbox('#edit-roles-admin');
$this
->click('#edit-submit');
$this
->assertSession()
->addressMatches('/\\/user\\/2\\/edit$/');
$this
->assertSession()
->pageTextContains('An administrator has required that you change your password. Please change your password to proceed.');
$this
->assertSession()
->pageTextContains('An administrator has required that you change your password. Please change your password to proceed.');
$this
->fillTextValue('#edit-current-pass', $admin_user->passRaw);
$this
->fillTextValue('#edit-pass-pass1', 'asdf');
$this
->fillTextValue('#edit-pass-pass2', 'asdf');
$this
->click('#edit-submit');
$this
->assertSession()
->addressMatches('/\\/admin\\/config\\/people\\/force_password_change$/');
$this
->drupalGet('admin/config/people/force_password_change/list/admin');
$this
->assertStatusCodeEquals(200);
$this
->assertCheckboxExists('#edit-force-password-change');
$this
->checkCheckbox('#edit-force-password-change');
$this
->click('#edit-submit');
$this
->assertSession()
->addressMatches('/\\/user\\/2\\/edit$/');
$this
->assertSession()
->pageTextContains('An administrator has required that you change your password. Please change your password to proceed.');
}
public function testNextLoginPasswordForce() {
$admin_user = $this
->drupalCreateUser([
'administer force password change',
'access administration pages',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/people/force_password_change');
$this
->assertStatusCodeEquals(200);
$this
->checkCheckbox('#edit-roles-authenticated');
$this
->selectRadio('#edit-login-only-1');
$this
->click('#edit-submit');
$this
->assertSession()
->addressMatches('/\\/admin\\/config\\/people\\/force_password_change$/');
$this
->assertSession()
->pageTextContains('Users in the following roles will be required to change their password on their next login:');
$this
->drupalLogout();
$this
->drupalLogin($admin_user);
$this
->assertSession()
->addressMatches('/\\/user\\/2\\/edit$/');
$this
->assertSession()
->pageTextContains('An administrator has required that you change your password. Please change your password to proceed.');
$this
->fillTextValue('#edit-current-pass', $admin_user->passRaw);
$this
->fillTextValue('#edit-pass-pass1', 'asdf');
$this
->fillTextValue('#edit-pass-pass2', 'asdf');
$this
->click('#edit-submit');
$this
->assertSession()
->pageTextNotContains('An administrator has required that you change your password. Please change your password to proceed.');
}
public function testFirstTimeLoginForce() {
$admin_user = $this
->drupalCreateUser([
'administer force password change',
'access administration pages',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/people/force_password_change');
$this
->assertStatusCodeEquals(200);
$this
->checkCheckbox('#edit-first-time-login-password-change');
$this
->click('#edit-submit');
$this
->assertCheckboxChecked('#edit-first-time-login-password-change');
$regular_user = $this
->drupalCreateUser([]);
$this
->drupalLogout();
$this
->drupalLogin($regular_user);
$this
->assertSession()
->addressMatches('/\\/user\\/' . $regular_user
->id() . '\\/edit$/');
$this
->assertSession()
->pageTextContains('An administrator has required that you change your password. Please change your password to proceed.');
}
public function testAuthenticatedUserPasswordExpire() {
$admin_user = $this
->drupalCreateUser([
'administer force password change',
'access administration pages',
]);
$admin_role = $this
->createRole([], 'admin');
$admin_user
->addRole($admin_role);
$admin_user
->save();
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/people/force_password_change');
$this
->assertStatusCodeEquals(200);
$this
->checkCheckbox('#edit-expire-password');
$this
->fillTextValue('#edit-table-authenticated-time-time-quantity', '1');
$this
->selectSelectOption('#edit-table-authenticated-time-time-period', 'hour');
$this
->selectSelectOption('#edit-table-authenticated-weight', '-1');
$this
->click('#edit-submit');
\Drupal::database()
->query('UPDATE {force_password_change_expiry} SET expiry = :expiry WHERE rid = :authenticated', [
':expiry' => 1,
':authenticated' => 'authenticated',
]);
sleep(2);
$this
->drupalGet('admin/config/people/force_password_change');
$this
->assertSession()
->addressMatches('/\\/user\\/' . $admin_user
->id() . '\\/edit$/');
$this
->assertSession()
->pageTextContains('An administrator has required that you change your password. Please change your password to proceed.');
$this
->fillTextValue('#edit-current-pass', $admin_user->passRaw);
$this
->fillTextValue('#edit-pass-pass1', 'asdf');
$this
->fillTextValue('#edit-pass-pass2', 'asdf');
$this
->click('#edit-submit');
$this
->assertSession()
->pageTextNotContains('An administrator has required that you change your password. Please change your password to proceed.');
$this
->drupalGet('admin/config/people/force_password_change');
$this
->assertStatusCodeEquals(200);
$this
->assertSession()
->addressMatches('/^\\/admin\\/config\\/people\\/force_password_change$/');
$this
->fillTextValue('#edit-table-admin-time-time-quantity', '1');
$this
->selectSelectOption('#edit-table-admin-time-time-period', 'hour');
$this
->selectSelectOption('#edit-table-admin-weight', '-1');
$this
->fillTextValue('#edit-table-authenticated-time-time-quantity', '1');
$this
->selectSelectOption('#edit-table-authenticated-time-time-period', 'hour');
$this
->selectSelectOption('#edit-table-authenticated-weight', '1');
$request_time = \Drupal::time()
->getRequestTime();
\Drupal::database()
->query('UPDATE {force_password_change_expiry} SET expiry = :expiry WHERE rid = :authenticated', [
':expiry' => $request_time - 1,
':authenticated' => 'authenticated',
]);
$this
->drupalGet('admin/config/people/force_password_change');
$this
->drupalGet('admin/config/people/force_password_change');
$this
->assertStatusCodeEquals(200);
}
public function testUserEditPageForcePassword() {
$admin_user = $this
->drupalCreateUser([
'administer force password change',
'access administration pages',
'administer users',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('user/' . $admin_user
->id() . '/edit');
$this
->assertStatusCodeEquals(200);
$this
->checkCheckbox('#edit-force-password-change');
$this
->click('#edit-submit');
$this
->drupalGet('user/' . $admin_user
->id());
$this
->assertSession()
->addressMatches('/\\/user\\/' . $admin_user
->id() . '\\/edit$/');
$this
->assertSession()
->pageTextContains('An administrator has required that you change your password. Please change your password to proceed.');
$this
->fillTextValue('#edit-current-pass', $admin_user->passRaw);
$this
->fillTextValue('#edit-pass-pass1', 'asdf');
$this
->fillTextValue('#edit-pass-pass2', 'asdf');
$this
->click('#edit-submit');
$this
->drupalGet('admin/config/people/force_password_change');
$this
->assertStatusCodeEquals(200);
$this
->selectRadio('#edit-login-only-1');
$this
->click('#edit-submit');
$this
->drupalGet('user/' . $admin_user
->id() . '/edit');
$this
->assertStatusCodeEquals(200);
$this
->checkCheckbox('#edit-force-password-change');
$this
->click('#edit-submit');
$this
->drupalGet('user/' . $admin_user
->id());
$this
->assertSession()
->addressMatches('/\\/user\\/' . $admin_user
->id() . '$/');
$regular_user = $this
->drupalCreateUser([]);
$this
->drupalGet('user/' . $regular_user
->id() . '/edit');
$this
->assertStatusCodeEquals(200);
$this
->checkCheckbox('#edit-force-password-change');
$this
->click('#edit-submit');
$this
->drupalLogout();
$this
->drupalLogin($regular_user);
$this
->assertSession()
->addressMatches('/\\/user\\/' . $regular_user
->id() . '\\/edit$/');
$this
->assertSession()
->pageTextContains('An administrator has required that you change your password. Please change your password to proceed.');
}
public function testDisableModuleInSettingsPhp() {
$admin_user = $this
->drupalCreateUser([
'administer force password change',
'access administration pages',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/people/force_password_change');
$this
->assertStatusCodeEquals(200);
$this
->checkCheckbox('#edit-roles-authenticated');
$this
->click('#edit-submit');
$this
->assertSession()
->addressMatches('/\\/user\\/2\\/edit$/');
$this
->assertSession()
->pageTextContains('An administrator has required that you change your password. Please change your password to proceed.');
\Drupal::service('config.factory')
->getEditable('force_password_change.settings')
->set('enabled', FALSE)
->save();
$this
->drupalGet('admin/config/people/force_password_change');
$this
->assertStatusCodeEquals(200);
}
}