Skip to content
Snippets Groups Projects
Select Git revision
  • 8494b0632a554b0b835e2f8c5eae98a5bd1da9f1
  • 11.x default protected
  • 10.5.x protected
  • 10.6.x protected
  • 11.2.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.2 protected
  • 11.2.3 protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
41 results

MailCaptureTest.php

  • Alex Pott's avatar
    Issue #2262195 by sun: Various test classes are missing phpDoc.
    Alex Pott authored
    8494b063
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    MailCaptureTest.php 3.69 KiB
    <?php
    
    /**
     * @file
     * Definition of \Drupal\simpletest\Tests\MailCaptureTest.
     */
    
    namespace Drupal\simpletest\Tests;
    
    use Drupal\simpletest\WebTestBase;
    
    /**
     * Tests Simpletest email capturing (TestMailCollector) and assertion methods.
     */
    class MailCaptureTest extends WebTestBase {
      /**
       * Implement getInfo().
       */
      public static function getInfo() {
        return array(
          'name' => 'SimpleTest e-mail capturing',
          'description' => 'Test the SimpleTest e-mail capturing logic, the assertMail assertion and the drupalGetMails function.',
          'group' => 'SimpleTest',
        );
      }
    
      /**
       * Test to see if the wrapper function is executed correctly.
       */
      function testMailSend() {
        // Create an e-mail.
        $subject = $this->randomString(64);
        $body = $this->randomString(128);
        $message = array(
          'id' => 'drupal_mail_test',
          'headers' => array('Content-type'=> 'text/html'),
          'subject' => $subject,
          'to' => 'foobar@example.com',
          'body' => $body,
        );
    
        // Before we send the e-mail, drupalGetMails should return an empty array.
        $captured_emails = $this->drupalGetMails();
        $this->assertEqual(count($captured_emails), 0, 'The captured e-mails queue is empty.', 'E-mail');
    
        // Send the e-mail.
        drupal_mail_system('simpletest', 'drupal_mail_test')->mail($message);
    
        // Ensure that there is one e-mail in the captured e-mails array.
        $captured_emails = $this->drupalGetMails();
        $this->assertEqual(count($captured_emails), 1, 'One e-mail was captured.', 'E-mail');
    
        // Assert that the e-mail was sent by iterating over the message properties
        // and ensuring that they are captured intact.
        foreach ($message as $field => $value) {
          $this->assertMail($field, $value, format_string('The e-mail was sent and the value for property @field is intact.', array('@field' => $field)), 'E-mail');
        }
    
        // Send additional e-mails so more than one e-mail is captured.
        for ($index = 0; $index < 5; $index++) {
          $message = array(
            'id' => 'drupal_mail_test_' . $index,
            'headers' => array('Content-type'=> 'text/html'),
            'subject' => $this->randomString(64),
            'to' => $this->randomName(32) . '@example.com',
            'body' => $this->randomString(512),
          );
          drupal_mail_system('drupal_mail_test', $index)->mail($message);
        }
    
        // There should now be 6 e-mails captured.
        $captured_emails = $this->drupalGetMails();
        $this->assertEqual(count($captured_emails), 6, 'All e-mails were captured.', 'E-mail');
    
        // Test different ways of getting filtered e-mails via drupalGetMails().
        $captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test'));
        $this->assertEqual(count($captured_emails), 1, 'Only one e-mail is returned when filtering by id.', 'E-mail');
        $captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test', 'subject' => $subject));
        $this->assertEqual(count($captured_emails), 1, 'Only one e-mail is returned when filtering by id and subject.', 'E-mail');
        $captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test', 'subject' => $subject, 'from' => 'this_was_not_used@example.com'));
        $this->assertEqual(count($captured_emails), 0, 'No e-mails are returned when querying with an unused from address.', 'E-mail');
    
        // Send the last e-mail again, so we can confirm that the
        // drupalGetMails-filter correctly returns all e-mails with a given
        // property/value.
        drupal_mail_system('drupal_mail_test', $index)->mail($message);
        $captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test_4'));
        $this->assertEqual(count($captured_emails), 2, 'All e-mails with the same id are returned when filtering by id.', 'E-mail');
      }
    }