annotate core/modules/simpletest/tests/src/Functional/MailCaptureTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\simpletest\Functional;
Chris@0 4
Chris@0 5 use Drupal\Tests\BrowserTestBase;
Chris@0 6 use Drupal\Core\Test\AssertMailTrait;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests the SimpleTest email capturing logic, the assertMail assertion and the
Chris@0 10 * drupalGetMails function.
Chris@0 11 *
Chris@0 12 * @group simpletest
Chris@0 13 */
Chris@0 14 class MailCaptureTest extends BrowserTestBase {
Chris@0 15 use AssertMailTrait {
Chris@0 16 getMails as drupalGetMails;
Chris@0 17 }
Chris@0 18 /**
Chris@0 19 * Test to see if the wrapper function is executed correctly.
Chris@0 20 */
Chris@0 21 public function testMailSend() {
Chris@0 22 // Create an email.
Chris@0 23 $subject = $this->randomString(64);
Chris@0 24 $body = $this->randomString(128);
Chris@0 25 $message = [
Chris@0 26 'id' => 'drupal_mail_test',
Chris@0 27 'headers' => ['Content-type' => 'text/html'],
Chris@0 28 'subject' => $subject,
Chris@0 29 'to' => 'foobar@example.com',
Chris@0 30 'body' => $body,
Chris@0 31 ];
Chris@0 32
Chris@0 33 // Before we send the email, drupalGetMails should return an empty array.
Chris@0 34 $captured_emails = $this->drupalGetMails();
Chris@0 35 $this->assertEqual(count($captured_emails), 0, 'The captured emails queue is empty.', 'Email');
Chris@0 36
Chris@0 37 // Send the email.
Chris@0 38 \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'simpletest', 'key' => 'drupal_mail_test'])->mail($message);
Chris@0 39
Chris@0 40 // Ensure that there is one email in the captured emails array.
Chris@0 41 $captured_emails = $this->drupalGetMails();
Chris@0 42 $this->assertEqual(count($captured_emails), 1, 'One email was captured.', 'Email');
Chris@0 43
Chris@0 44 // Assert that the email was sent by iterating over the message properties
Chris@0 45 // and ensuring that they are captured intact.
Chris@0 46 foreach ($message as $field => $value) {
Chris@0 47 $this->assertMail($field, $value, format_string('The email was sent and the value for property @field is intact.', ['@field' => $field]), 'Email');
Chris@0 48 }
Chris@0 49
Chris@0 50 // Send additional emails so more than one email is captured.
Chris@0 51 for ($index = 0; $index < 5; $index++) {
Chris@0 52 $message = [
Chris@0 53 'id' => 'drupal_mail_test_' . $index,
Chris@0 54 'headers' => ['Content-type' => 'text/html'],
Chris@0 55 'subject' => $this->randomString(64),
Chris@0 56 'to' => $this->randomMachineName(32) . '@example.com',
Chris@0 57 'body' => $this->randomString(512),
Chris@0 58 ];
Chris@0 59 \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'drupal_mail_test', 'key' => $index])->mail($message);
Chris@0 60 }
Chris@0 61
Chris@0 62 // There should now be 6 emails captured.
Chris@0 63 $captured_emails = $this->drupalGetMails();
Chris@0 64 $this->assertEqual(count($captured_emails), 6, 'All emails were captured.', 'Email');
Chris@0 65
Chris@0 66 // Test different ways of getting filtered emails via drupalGetMails().
Chris@0 67 $captured_emails = $this->drupalGetMails(['id' => 'drupal_mail_test']);
Chris@0 68 $this->assertEqual(count($captured_emails), 1, 'Only one email is returned when filtering by id.', 'Email');
Chris@0 69 $captured_emails = $this->drupalGetMails(['id' => 'drupal_mail_test', 'subject' => $subject]);
Chris@0 70 $this->assertEqual(count($captured_emails), 1, 'Only one email is returned when filtering by id and subject.', 'Email');
Chris@0 71 $captured_emails = $this->drupalGetMails(['id' => 'drupal_mail_test', 'subject' => $subject, 'from' => 'this_was_not_used@example.com']);
Chris@0 72 $this->assertEqual(count($captured_emails), 0, 'No emails are returned when querying with an unused from address.', 'Email');
Chris@0 73
Chris@0 74 // Send the last email again, so we can confirm that the
Chris@0 75 // drupalGetMails-filter correctly returns all emails with a given
Chris@0 76 // property/value.
Chris@0 77 \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'drupal_mail_test', 'key' => $index])->mail($message);
Chris@0 78 $captured_emails = $this->drupalGetMails(['id' => 'drupal_mail_test_4']);
Chris@0 79 $this->assertEqual(count($captured_emails), 2, 'All emails with the same id are returned when filtering by id.', 'Email');
Chris@0 80 }
Chris@0 81
Chris@0 82 }