getMails example

// Check that the field is displayed.     $this->drupalGet('contact/' . $contact_form);
    $this->assertSession()->pageTextContains($field_label);

    // Submit the contact form and verify the content.     $edit = [
      'subject[0][value]' => $this->randomMachineName(),
      'message[0][value]' => $this->randomMachineName(),
      $field_name . '[0][value]' => $this->randomMachineName(),
    ];
    $this->submitForm($edit, 'Send message');
    $mails = $this->getMails();
    $mail = array_pop($mails);
    $this->assertEquals(t('[@label] @subject', ['@label' => $label, '@subject' => $edit['subject[0][value]']])$mail['subject']);
    $this->assertStringContainsString($field_label$mail['body']);
    $this->assertStringContainsString($edit[$field_name . '[0][value]']$mail['body']);

    // Test messages and redirect.     /** @var \Drupal\contact\ContactFormInterface $form */
    $form = ContactForm::load($contact_form);
    $form->setMessage('Thanks for your submission.');
    $form->setRedirectPath('/user/' . $admin_user->id());
    $form->save();
    
/** @var \Drupal\Core\Action\ActionManager $plugin_manager */
    $plugin_manager = $this->container->get('plugin.manager.action');
    $configuration = [
      'recipient' => 'test@example.com',
      'subject' => 'Test subject',
      'message' => 'Test message',
    ];
    $plugin_manager
      ->createInstance('action_send_email_action', $configuration)
      ->execute();

    $mails = $this->getMails();
    $this->assertCount(1, $this->getMails());
    $this->assertEquals('test@example.com', $mails[0]['to']);
    $this->assertEquals('Test subject', $mails[0]['subject']);
    $this->assertEquals("Test message\n", $mails[0]['body']);

    // Ensure that the email sending is logged.     $log = \Drupal::database()
      ->select('watchdog', 'w')
      ->fields('w', ['message', 'variables'])
      ->orderBy('wid', 'DESC')
      ->range(0, 1)
      

  public function testSendPersonalContactMessage() {
    // Ensure that the web user's email needs escaping.     $mail = $this->webUser->getAccountName() . '&escaped@example.com';
    $this->webUser->setEmail($mail)->save();
    $this->drupalLogin($this->webUser);

    $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
    $this->assertSession()->assertEscaped($mail);
    $message = $this->submitPersonalContact($this->contactUser);
    $mails = $this->getMails();
    $this->assertCount(1, $mails);
    $mail = $mails[0];
    $this->assertEquals($this->contactUser->getEmail()$mail['to']);
    $this->assertEquals($this->config('system.site')->get('mail')$mail['from']);
    $this->assertEquals($this->webUser->getEmail()$mail['reply-to']);
    $this->assertEquals('user_mail', $mail['key']);
    $subject = '[' . $this->config('system.site')->get('name') . '] ' . $message['subject[0][value]'];
    $this->assertEquals($subject$mail['subject'], 'Subject is in sent message.');
    $this->assertStringContainsString('Hello ' . $this->contactUser->getDisplayName()$mail['body'], 'Recipient name is in sent message.');
    $this->assertStringContainsString($this->webUser->getDisplayName()$mail['body'], 'Sender name is in sent message.');
    $this->assertStringContainsString($message['message[0][value]']$mail['body'], 'Message body is in sent message.');

    

  protected function assertMailString($field_name$string$email_depth$message = '') {
    $mails = $this->getMails();
    $string_found = FALSE;
    // Cast MarkupInterface objects to string.     $string = (string) $string;
    for ($i = count($mails) - 1; $i >= count($mails) - $email_depth && $i >= 0; $i--) {
      $mail = $mails[$i];
      // Normalize whitespace, as we don't know what the mail system might have       // done. Any run of whitespace becomes a single space.       $normalized_mail = preg_replace('/\s+/', ' ', $mail[$field_name]);
      $normalized_string = preg_replace('/\s+/', ' ', $string);
      $string_found = str_contains($normalized_mail$normalized_string);
      if ($string_found) {
        

  public function testUserMailsSent($op, array $mail_keys) {
    $this->installConfig('user');
    $this->config('system.site')->set('mail', 'test@example.com')->save();
    $this->config('user.settings')->set('notify.' . $op, TRUE)->save();
    $return = _user_mail_notify($op$this->createUser());
    $this->assertTrue($return);
    foreach ($mail_keys as $key) {
      $filter = ['key' => $key];
      $this->assertNotEmpty($this->getMails($filter));
    }
    $this->assertSameSize($mail_keys$this->getMails());
  }

  /** * Tests mails are not sent when notify.$op is FALSE. * * @param string $op * The operation being performed on the account. * * @dataProvider userMailsProvider */
    // forms.     foreach ($this->langcodes as $langcode) {
      $langcode_prefixes = array_merge(['']$this->langcodes);
      foreach ($langcode_prefixes as $langcode_prefix) {
        $this->drupalGet(ltrim("$langcode_prefix/$translation_base_url/$langcode/edit", '/'));
        $this->assertSession()->fieldValueEquals('translation[config_names][contact.form.feedback][label]', 'Website feedback - ' . $langcode);
        $this->assertSession()->pageTextContains($label);
      }
    }

    // We get all emails so no need to check inside the loop.     $captured_emails = $this->getMails();

    // Check language specific auto reply text in email body.     foreach ($captured_emails as $email) {
      if ($email['id'] == 'contact_page_autoreply') {
        // Trim because we get an added newline for the body.         $this->assertEquals('Thank you for your mail - ' . $email['langcode']trim($email['body']));
      }
    }

    // Test that delete links work and operations perform properly.     foreach ($this->langcodes as $langcode) {
      
$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, \Drupal\Core\Test\AssertMailTrait::getMails()     // should return an empty array.     $captured_emails = $this->getMails();
    $this->assertCount(0, $captured_emails, 'The captured emails queue is empty.');

    // Send the email.     $mail_service->getInstance(['module' => 'simpletest', 'key' => 'drupal_mail_test'])->mail($message);

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

    // Asserts that the message fields have the pattern in it.     $this->assertMailPattern('id', $message['id']);
    
Home | Imprint | This part of the site doesn't use cookies.