class SymfonyMailerTest

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

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

Hierarchy

Expanded class hierarchy of SymfonyMailerTest

File

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

Namespace

Drupal\Tests\Core\Mail\Plugin\Mail
View source
class SymfonyMailerTest extends UnitTestCase {
    
    /**
     * Tests that mixed plain text and html body is converted correctly.
     *
     * @covers ::format
     */
    public function testFormatResemblesHtml() : void {
        // Populate global $base_path to avoid notices generated by
        // MailFormatHelper::htmlToMailUrls()
        global $base_path;
        $original_base_path = $base_path;
        $base_path = '/';
        $variables = [
            '@form-url' => 'https://www.example.com/contact',
            '@sender-url' => 'https://www.example.com/user/123',
            '@sender-name' => $this->randomString(),
        ];
        $plain = "In HTML, ampersand must be written as &amp;.\nI saw your house and <wow> it is great. There is too much to say about that beautiful building, it will never fit on one line of text.\nIf a<b and b<c then a<c.";
        $template = "@sender-name (@sender-url) sent a message using the contact form at @form-url.";
        $markup = new FormattableMarkup($template, $variables);
        $message = [
            'body' => [
                $plain,
                $markup,
            ],
        ];
        
        /** @var \Symfony\Component\Mailer\MailerInterface|\PHPUnit\Framework\MockObject\MockObject */
        $mailer = $this->getMockBuilder(MailerInterface::class)
            ->getMock();
        
        /** @var \Psr\Log\LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
        $logger = $this->getMockBuilder(LoggerInterface::class)
            ->getMock();
        $plugin = new SymfonyMailer($logger, $mailer);
        $message = $plugin->format($message);
        $expect = MailFormatHelper::wrapMail($plain . "\n\n" . strtr($template, $variables) . "\n");
        $this->assertEquals($expect, $message['body']);
        $base_path = $original_base_path;
    }
    
    /**
     * Tests sending a mail using a From address with a comma in it.
     *
     * @covers ::mail
     */
    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));
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
SymfonyMailerTest::testFormatResemblesHtml public function Tests that mixed plain text and html body is converted correctly.
SymfonyMailerTest::testMail public function Tests sending a mail using a From address with a comma in it.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 358
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

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