You are here

public function PasswordResetTest::testPasswordReset in Legal 2.0.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/PasswordResetTest.php \Drupal\Tests\legal\Functional\PasswordResetTest::testPasswordReset()

Test loging in with default Legal seetings.

File

tests/src/Functional/PasswordResetTest.php, line 39

Class

PasswordResetTest
Tests password reset workflow when T&Cs need to be accepted.

Namespace

Drupal\Tests\legal\Functional

Code

public function testPasswordReset() {

  // Reset the password by username via the password reset page.
  $this
    ->drupalGet('user/password');
  $edit['name'] = $this->loginDetails['name'];
  $this
    ->drupalPostForm(NULL, $edit, 'Submit');

  // Get one time login URL from email (assume the most recent email).
  $_emails = $this
    ->drupalGetMails();
  $email = end($_emails);
  $urls = [];
  preg_match('#.+user/reset/.+#', $email['body'], $urls);

  // Use one time login URL.
  $this
    ->drupalGet($urls[0]);

  // Log in.
  $this
    ->submitForm([], 'Log in', 'user-pass-reset');

  // Check user is redirected to T&C acceptance page.
  $expected_query = [
    'destination' => $this->account
      ->toUrl('edit-form')
      ->toString(),
    'token' => '',
  ];
  $expected_url = Url::fromRoute('legal.legal_login', [], [
    'query' => $expected_query,
  ])
    ->setAbsolute()
    ->toString();
  $this
    ->assertStringStartsWith($expected_url, $this
    ->getUrl());
  $this
    ->assertResponse(200);

  // Accept T&Cs and submit form.
  $edit = [
    'edit-legal-accept' => TRUE,
  ];
  $this
    ->submitForm($edit, 'Confirm', 'legal-login');

  // Check user is logged in.
  $account = User::load($this->uid);
  $this
    ->drupalUserIsLoggedIn($account);

  // Check user is redirected to their user page.
  $current_url = $this
    ->getUrl();
  $expected_url = $this->baseUrl . '/user/' . $this->uid . '/edit';
  $this
    ->assertEquals($current_url, $expected_url);
}