function SymfonyMailerTest::testMail

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

Tests sending a mail using a From address with a comma in it.

@covers ::mail

File

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

Class

SymfonyMailerTest
@coversDefaultClass \Drupal\Core\Mail\Plugin\Mail\SymfonyMailer @group Mail

Namespace

Drupal\Tests\Core\Mail\Plugin\Mail

Code

public function testMail() : void {
    // Setup a mail message.
    $message = [
        'id' => 'example_key',
        'module' => 'example',
        'key' => 'key',
        'to' => 'to@example.org',
        'from' => 'from@example.org',
        'reply-to' => 'from@example.org',
        'langcode' => 'en',
        'params' => [],
        'send' => TRUE,
        'subject' => "test\r\nsubject",
        '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',
        ],
    ];
    // Verify we use line endings consistent with the PHP mail() function, which
    // changed with PHP 8. See:
    // - https://www.drupal.org/node/3270647
    // - https://bugs.php.net/bug.php?id=81158
    $line_end = "\r\n";
    
    /** @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) => $email->getHeaders()
        ->get('mime-version')
        ->getBodyAsString() === '1.0'), $this->callback(fn(Email $email) => $email->getHeaders()
        ->has('content-type') === FALSE), $this->callback(fn(Email $email) => $email->getHeaders()
        ->has('content-transfer-encoding') === FALSE), $this->callback(fn(Email $email) => $email->getHeaders()
        ->get('x-mailer')
        ->getBodyAsString() === 'Drupal'), $this->callback(fn(Email $email) => $email->getHeaders()
        ->get('from')
        ->getBodyAsString() === '"Foo, Bar, and Baz" <from@example.org>'), $this->callback(fn(Email $email) => $email->getHeaders()
        ->get('reply-to')
        ->getBodyAsString() === 'from@example.org'), $this->callback(fn(Email $email) => $email->getHeaders()
        ->get('to')
        ->getBodyAsString() === 'to@example.org'), $this->callback(fn(Email $email) => $email->getHeaders()
        ->get('subject')
        ->getBodyAsString() === "=?utf-8?Q?test?={$line_end} =?utf-8?Q?subject?="), $this->callback(fn(Email $email) => $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.