assertMail example

$editunblock['action'] = 'user_unblock_user_action';
    $editunblock['user_bulk_form[4]'] = TRUE;
    $this->drupalGet('admin/people', [
      // Sort the table by username so that we know reliably which user will be       // targeted with the blocking action.       'query' => ['order' => 'name', 'sort' => 'asc'],
    ]);
    $this->submitForm($editunblock, 'Apply to selected items');
    $user_storage->resetCache([$user_c->id()]);
    $account = $user_storage->load($user_c->id());
    $this->assertTrue($account->isActive(), 'User C unblocked');
    $this->assertMail("to", $account->getEmail(), "Activation mail sent to user C");

    // Test blocking and unblocking another user from /user/[uid]/edit form and sending of activation mail     $user_d = $this->drupalCreateUser([]);
    $user_storage->resetCache([$user_d->id()]);
    $account1 = $user_storage->load($user_d->id());
    $this->drupalGet('user/' . $account1->id() . '/edit');
    $this->submitForm(['status' => 0], 'Save');
    $user_storage->resetCache([$user_d->id()]);
    $account1 = $user_storage->load($user_d->id());
    $this->assertTrue($account1->isBlocked(), 'User D blocked');
    $this->drupalGet('user/' . $account1->id() . '/edit');
    
// 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.     foreach ($message as $field => $value) {
      $this->assertMail($field$valuenew FormattableMarkup('The email was sent and the value for property @field is intact.', ['@field' => $field]));
    }

    // Send additional emails so more than one email is captured.     for ($index = 0; $index < 5; $index++) {
      $message = [
        'id' => 'drupal_mail_test_' . $index,
        'headers' => ['Content-type' => 'text/html'],
        'subject' => $this->randomString(64),
        'to' => $this->randomMachineName(32) . '@example.com',
        'body' => $this->randomString(512),
      ];
      
// Ensure another email was sent.     $this->assertCount($before + 4, $this->drupalGetMails(['id' => 'user_password_reset']), 'Another email was sent after clearing flood control.');
  }

  /** * Helper function to make assertions about a valid password reset. * * @internal */
  public function assertValidPasswordReset(string $name): void {
    $this->assertSession()->pageTextContains("If $name is a valid account, an email will be sent with instructions to reset your password.");
    $this->assertMail('to', $this->account->getEmail(), 'Password e-mail sent to user.');
    $subject = 'Replacement login information for ' . $this->account->getAccountName() . ' at Drupal';
    $this->assertMail('subject', $subject, 'Password reset e-mail subject is correct.');
  }

  /** * Helper function to make assertions about an invalid password reset. * * @param string $name * The user name. * * @internal */
$this->assertCount(1, $captured_emails, 'One email was captured.');

    // Asserts that the message fields have the pattern in it.     $this->assertMailPattern('id', $message['id']);
    $this->assertMailPattern('subject', "^.{64}$");
    $this->assertMailPattern('to', "[a-z]{6}@example\.com$");
    $this->assertMailPattern('body', "^.{128}$");

    // Assert that the email was sent by iterating over the message properties     // and ensuring that they are captured intact.     foreach ($message as $field => $value) {
      $this->assertMail($field$value, "The email was sent and the value for property $field is intact.");
    }

    // Send additional emails so more than one email is captured.     for ($index = 0; $index < 5; $index++) {
      $message = [
        'id' => 'drupal_mail_test_' . $index,
        'headers' => ['Content-type' => 'text/html'],
        'subject' => $this->randomString(64),
        'to' => $this->randomMachineName(32) . '@example.com',
        'body' => $this->randomString(512),
      ];
      
Home | Imprint | This part of the site doesn't use cookies.