function SymfonyMailerTest::testFormatResemblesHtml

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

Tests that mixed plain text and html body is converted correctly.

@covers ::format

File

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

Class

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

Namespace

Drupal\Tests\Core\Mail\Plugin\Mail

Code

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;
}

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