View source
<?php
namespace Drupal\Tests\cas\Functional;
use Composer\Semver\Comparator;
class CasPasswordResetTest extends CasBrowserTestBase {
protected $settings;
protected $casUser;
protected $nonCasUser;
protected function setUp() : void {
parent::setUp();
$this->settings = $this
->config('cas.settings');
$this->casUser = $this
->drupalCreateUser([], 'user_with_cas');
$this->container
->get('cas.user_manager')
->setCasUsernameForAccount($this->casUser, 'user_with_cas');
$this->nonCasUser = $this
->drupalCreateUser([], 'user_without_cas');
}
public function testPasswordResetAsAnonymous() {
$this->settings
->set('user_accounts.restrict_password_management', FALSE)
->save();
$this
->drupalPostForm('/user/password', [
'name' => 'user_with_cas',
], 'Submit');
$this
->assertStatusMessage('user_with_cas');
$this
->drupalPostForm('/user/password', [
'name' => 'user_without_cas',
], 'Submit');
$this
->assertStatusMessage('user_without_cas');
$this->settings
->set('user_accounts.restrict_password_management', TRUE)
->save();
$this
->drupalPostForm('/user/password', [
'name' => 'user_with_cas',
], 'Submit');
$this
->assertSession()
->addressEquals('user/password');
$this
->assertSession()
->pageTextContains('The requested account is associated with CAS and its password cannot be managed from this website.');
$this->settings
->set('error_handling.message_restrict_password_management', 'You cannot manage your password. Back to <a href="[site:url]">homepage</a>.')
->save();
$this
->getSession()
->reload();
$this
->assertSession()
->pageTextContains('You cannot manage your password. Back to homepage.');
$this
->assertSession()
->linkExists('homepage');
$this
->drupalPostForm('/user/password', [
'name' => 'user_without_cas',
], 'Submit');
$this
->assertStatusMessage('user_without_cas');
}
public function testPasswordResetAsAuthenticated() {
$this->settings
->set('user_accounts.restrict_password_management', FALSE)
->set('user_accounts.prevent_normal_login', FALSE)
->save();
$this
->drupalLogin($this->nonCasUser);
$this
->drupalPostForm('/user/password', [], 'Submit');
$this
->assertSession()
->addressEquals($this->nonCasUser
->toUrl());
$this
->assertStatusMessage('user_without_cas@example.com');
$this
->drupalLogin($this->casUser);
$this
->drupalPostForm('/user/password', [], 'Submit');
$this
->assertSession()
->addressEquals($this->casUser
->toUrl());
$this
->assertStatusMessage('user_with_cas@example.com');
$this->settings
->set('user_accounts.restrict_password_management', TRUE)
->save();
$this
->drupalGet('/user/password');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->nonCasUser);
$this
->drupalPostForm('/user/password', [], 'Submit');
$this
->assertSession()
->addressEquals($this->nonCasUser
->toUrl());
$this
->assertStatusMessage('user_without_cas@example.com');
}
protected function assertStatusMessage(string $username_or_email) : void {
if (Comparator::greaterThanOrEqualTo(\Drupal::VERSION, '9.2')) {
$message = "If {$username_or_email} is a valid account, an email will be sent with instructions to reset your password.";
}
else {
$message = 'Further instructions have been sent to your email address.';
}
$this
->assertSession()
->pageTextContains($message);
}
}