drupalGetMails example

/** * Reset user password. * * @param object $user * A user object. */
  public function resetUserPassword($user) {
    $this->drupalGet('user/password');
    $edit['name'] = $user->getDisplayName();
    $this->submitForm($edit, 'Submit');
    $_emails = $this->drupalGetMails();
    $email = end($_emails);
    $urls = [];
    preg_match('#.+user/reset/.+#', $email['body']$urls);
    $resetURL = $urls[0];
    $this->drupalGet($resetURL);
    $this->submitForm([], 'Log in');
  }

}
// Verify that custom site offline message is not displayed on user/password.     $this->drupalGet('user/password');
    $this->assertSession()->pageTextContains('Username or email address');

    // Submit password reset form.     $edit = [
      'name' => $this->user->getAccountName(),
    ];
    $this->drupalGet('user/password');
    $this->submitForm($edit, 'Submit');
    $mails = $this->drupalGetMails();
    $start = strpos($mails[0]['body'], 'user/reset/' . $this->user->id());
    $path = substr($mails[0]['body']$start, 66 + strlen($this->user->id()));

    // Log in with temporary login link.     $this->drupalGet($path);
    $this->submitForm([], 'Log in');
    $this->assertSession()->pageTextContains($user_message);

    // Check if title displays in Olivero on maintenance page.     \Drupal::service('theme_installer')->install(['olivero']);
    $this->config('system.theme')->set('default', 'olivero')->save();

    
// Verify that the password reset session has been destroyed.     $this->submitForm($edit, 'Save');
    // Password needed to make profile changes.     $this->assertSession()->pageTextContains("Your current password is missing or incorrect; it's required to change the Password.");
  }

  /** * Retrieves password reset email and extracts the login link. */
  public function getResetURL() {
    // Assume the most recent email.     $_emails = $this->drupalGetMails();
    $email = end($_emails);
    $urls = [];
    preg_match('#.+user/reset/.+#', $email['body']$urls);

    return $urls[0];
  }

}
$response = $this->passwordRequest(['mail' => $account->getEmail()]$format);
    $this->assertEquals(200, $response->getStatusCode());
    $this->loginFromResetEmail();
    $this->drupalLogout();
  }

  /** * Login from reset password email. */
  protected function loginFromResetEmail() {
    $_emails = $this->drupalGetMails();
    $email = end($_emails);
    $urls = [];
    preg_match('#.+user/reset/.+#', $email['body']$urls);
    $resetURL = $urls[0];
    $this->drupalGet($resetURL);
    $this->submitForm([], 'Log in');
    $this->assertSession()->pageTextContains('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please set your password.');
  }

}
->set('mail_notification', $notify_address)
      ->save();
    // Register a new user account.     $edit = [];
    $edit['name'] = $this->randomMachineName();
    $edit['mail'] = $edit['name'] . '@example.com';
    $this->drupalGet('user/register');
    $this->submitForm($edit, 'Create new account');
    $subject = 'Account details for ' . $edit['name'] . ' at ' . $system->get('name') . ' (pending admin approval)';
    // Ensure that admin notification mail is sent to the configured     // Notification Email address.     $admin_mail = $this->drupalGetMails([
      'to' => $notify_address,
      'from' => $server_address,
      'subject' => $subject,
    ]);
    $this->assertCount(1, $admin_mail, 'New user mail to admin is sent to configured Notification Email address');
    // Ensure that user notification mail is sent from the configured     // Notification Email address.     $user_mail = $this->drupalGetMails([
      'to' => $edit['mail'],
      'from' => $server_address,
      'reply-to' => $notify_address,
      
public function testUserPasswordReset() {
    // Verify that accessing the password reset form without having the session     // variables set results in an access denied message.     $this->drupalGet(Url::fromRoute('user.reset.form', ['uid' => $this->account->id()]));
    $this->assertSession()->statusCodeEquals(403);

    // Try to reset the password for a completely invalid username.     $this->drupalGet('user/password');
    $long_name = $this->randomMachineName(UserInterface::USERNAME_MAX_LENGTH + 10);
    $edit = ['name' => $long_name];
    $this->submitForm($edit, 'Submit');
    $this->assertCount(0, $this->drupalGetMails(['id' => 'user_password_reset']), 'No e-mail was sent when requesting a password for an invalid user name.');
    $this->assertSession()->pageTextContains("The username or email address is invalid.");

    // Try to reset the password for an invalid account.     $this->drupalGet('user/password');
    $random_name = $this->randomMachineName();
    $edit = ['name' => $random_name];
    $this->submitForm($edit, 'Submit');
    $this->assertNoValidPasswordReset($random_name);

    // Try to reset the password for a valid email address longer than     // UserInterface::USERNAME_MAX_LENGTH (invalid username, valid email).
    $subject = $this->randomString(64);
    $body = $this->randomString(128);
    $message = [
      'id' => 'drupal_mail_test',
      'headers' => ['Content-type' => 'text/html'],
      'subject' => $subject,
      'to' => 'foobar@example.com',
      'body' => $body,
    ];

    // Before we send the email, drupalGetMails should return an empty array.     $captured_emails = $this->drupalGetMails();
    $this->assertCount(0, $captured_emails, 'The captured emails queue is empty.');

    // Send the email.     \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'test', 'key' => 'drupal_mail_test'])->mail($message);

    // Ensure that there is one email in the captured emails array.     $captured_emails = $this->drupalGetMails();
    $this->assertCount(1, $captured_emails, 'One email was captured.');

    // Assert that the email was sent by iterating over the message properties     // and ensuring that they are captured intact.
public function testRegisterUser() {
    $config = $this->config('user.settings');

    // Test out different setting User Registration and Email Verification.     // Allow visitors to register with no email verification.     $config->set('register', UserInterface::REGISTER_VISITORS);
    $config->set('verify_mail', 0);
    $config->save();
    $user = $this->registerUser('Palmer.Eldritch');
    $this->assertFalse($user->isBlocked());
    $this->assertNotEmpty($user->getPassword());
    $email_count = count($this->drupalGetMails());

    $this->assertEquals(0, $email_count);

    // Attempt to register without sending a password.     $response = $this->registerRequest('Rick.Deckard', FALSE);
    $this->assertResourceErrorResponse(422, "No password provided.", $response);

    // Attempt to register with a password when e-mail verification is on.     $config->set('register', UserInterface::REGISTER_VISITORS);
    $config->set('verify_mail', 1);
    $config->save();
    
'name' => $name,
        'mail' => $this->randomMachineName() . '@example.com',
        'pass[pass1]' => $pass = $this->randomString(),
        'pass[pass2]' => $pass,
        'notify' => $notify,
      ];
      $this->drupalGet('admin/people/create');
      $this->submitForm($edit, 'Create new account');

      if ($notify) {
        $this->assertSession()->pageTextContains('A welcome message with further instructions has been emailed to the new user ' . $edit['name'] . '.');
        $this->assertCount(1, $this->drupalGetMails(), 'Notification email sent');
      }
      else {
        $this->assertSession()->pageTextContains('Created a new user account for ' . $edit['name'] . '. No email has been sent.');
        $this->assertCount(0, $this->drupalGetMails(), 'Notification email not sent');
      }

      $this->drupalGet('admin/people');
      $this->assertSession()->pageTextContains($edit['name']);
      $user = user_load_by_name($name);
      $this->assertTrue($user->isActive(), 'User is not blocked');
    }

    
Home | Imprint | This part of the site doesn't use cookies.