function SymfonyMailerTest::testMail

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Mail/Plugin/Mail/SymfonyMailerTest.php \Drupal\Tests\Core\Mail\Plugin\Mail\SymfonyMailerTest::testMail()
  2. 10 core/tests/Drupal/Tests/Core/Mail/Plugin/Mail/SymfonyMailerTest.php \Drupal\Tests\Core\Mail\Plugin\Mail\SymfonyMailerTest::testMail()

Tests sending a mail with special characters in various fields.

@legacy-covers ::mail

Attributes

#[DataProvider('providerMail')]

Parameters

array $message: An array containing the following keys for an email message:

  • from
  • reply-to
  • to
  • subject.

array $expected: An array containing the following keys from the email headers from the sent email:

  • from
  • reply-to
  • to
  • subject.

File

core/tests/Drupal/Tests/Core/Mail/Plugin/Mail/SymfonyMailerTest.php, line 86

Class

SymfonyMailerTest
Tests Drupal\Core\Mail\Plugin\Mail\SymfonyMailer.

Namespace

Drupal\Tests\Core\Mail\Plugin\Mail

Code

public function testMail(array $message, array $expected) : void {
  // Setup a mail message.
  $message += [
    'id' => 'example_key',
    'module' => 'example',
    'key' => 'key',
    'langcode' => 'en',
    'params' => [],
    'send' => TRUE,
    'body' => '',
    'headers' => [
      'MIME-Version' => '1.0',
      'Content-Type' => 'text/plain; charset=utf-8; format=flowed; delsp=yes',
      'Content-Transfer-Encoding' => '8Bit',
      'X-Mailer' => 'Drupal',
      'From' => '"Foo, Bar, and Baz" <from@example.org>',
      'Reply-to' => 'from@example.org',
      'Return-Path' => 'from@example.org',
    ],
  ];
  /** @var \Symfony\Component\Mailer\MailerInterface|\PHPUnit\Framework\MockObject\MockObject */
  $mailer = $this->getMockBuilder(MailerInterface::class)
    ->getMock();
  $mailer->expects($this->once())
    ->method('send')
    ->with($this->logicalAnd($this->callback(fn(Email $email): bool => $email->getHeaders()
    ->get('mime-version')
    ->getBodyAsString() === '1.0'), $this->callback(fn(Email $email): bool => $email->getHeaders()
    ->has('content-type') === FALSE), $this->callback(fn(Email $email): bool => $email->getHeaders()
    ->has('content-transfer-encoding') === FALSE), $this->callback(fn(Email $email): bool => $email->getHeaders()
    ->get('x-mailer')
    ->getBodyAsString() === 'Drupal'), $this->callback(fn(Email $email): bool => $email->getHeaders()
    ->get('from')
    ->getBodyAsString() === $expected['from']), $this->callback(fn(Email $email): bool => $email->getHeaders()
    ->get('reply-to')
    ->getBodyAsString() === $expected['reply-to']), $this->callback(fn(Email $email): bool => $email->getHeaders()
    ->get('to')
    ->getBodyAsString() === $expected['to']), $this->callback(fn(Email $email): bool => $email->getHeaders()
    ->get('subject')
    ->getBodyAsString() === $expected['subject']), $this->callback(fn(Email $email): bool => $email->getTextBody() === '')));
  /** @var \Psr\Log\LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  $logger = $this->getMockBuilder(LoggerInterface::class)
    ->getMock();
  $plugin = new SymfonyMailer($logger, $mailer);
  $this->assertTrue($plugin->mail($message));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.