View source
<?php
namespace Drupal\Tests\swiftmailer\Kernel\Plugin\Mail;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Render\Markup;
use Drupal\KernelTests\KernelTestBase;
class FormatTest extends KernelTestBase {
protected static $modules = [
'user',
'filter',
'swiftmailer',
'system',
];
protected $plugin;
public function setUp() {
parent::setUp();
$this
->installConfig([
'swiftmailer',
'filter',
]);
$this
->installEntitySchema('user');
$this
->installSchema('user', 'users_data');
$this->plugin = $this->container
->get('plugin.manager.mail')
->createInstance('swiftmailer');
\Drupal::service('theme_installer')
->install([
'swiftmailer_test_theme',
]);
\Drupal::configFactory()
->getEditable('system.theme')
->set('default', 'swiftmailer_test_theme')
->save();
}
public function testFormat(array $message, $expected, $expected_plain) {
$message['module'] = 'swiftmailer';
$message['key'] = 'FormatTest';
$message['subject'] = 'FormatTest';
$message['params']['content_type'] = SWIFTMAILER_FORMAT_HTML;
$actual = $this->plugin
->format($message);
$expected = implode(PHP_EOL, $expected);
$this
->assertSame($expected, $this
->extractBody($actual));
$message['params']['content_type'] = SWIFTMAILER_FORMAT_PLAIN;
$actual = $this->plugin
->format($message);
$expected_plain = implode(PHP_EOL, $expected_plain) . PHP_EOL;
$this
->assertSame($expected_plain, (string) $actual['body']);
}
public function testCss() {
$message['module'] = 'swiftmailer';
$message['key'] = 'FormatTest';
$message['subject'] = 'FormatTest';
$message['params']['content_type'] = SWIFTMAILER_FORMAT_HTML;
$message['body'] = [
Markup::create('<p class="red">Red text</p>'),
];
$expected = '<p class="red" style="color: red;">Red text</p>';
$actual = $this->plugin
->format($message);
$this
->assertSame($expected, $this
->extractBody($actual));
}
public function bodyDataProvider() {
return [
'with html' => [
'message' => [
'body' => [
Markup::create('<p>Lorem ipsum & dolor sit amet</p>'),
Markup::create('<p>consetetur < sadipscing elitr</p>'),
],
],
'expected' => [
"<p>Lorem ipsum & dolor sit amet</p>",
"<p>consetetur < sadipscing elitr</p>",
],
'expected_plain' => [
"Lorem ipsum & dolor sit amet\n",
"consetetur < sadipscing elitr",
],
],
'no html' => [
'message' => [
'body' => [
"Lorem ipsum & dolor sit amet\nconsetetur < sadipscing elitr",
"URL is http://example.com",
],
],
'expected' => [
"<p>Lorem ipsum & dolor sit amet<br>\nconsetetur < sadipscing elitr</p>",
'<p>URL is <a href="http://example.com">http://example.com</a></p>',
],
'expected_plain' => [
"Lorem ipsum & dolor sit amet\nconsetetur < sadipscing elitr",
"URL is http://example.com",
],
],
'mixed' => [
'message' => [
'body' => [
'Hello & World',
'Hello & <strong>World</strong>',
new FormattableMarkup('<p>Hello & World #@number</p>', [
'@number' => 2,
]),
Markup::create('<p>Hello & <strong>World</strong></p>'),
],
],
'expected' => [
"<p>Hello & World</p>",
"<p>Hello & <strong>World</strong></p>",
"<p>Hello & World #2</p>",
"<p>Hello & <strong>World</strong></p>",
],
'expected_plain' => [
"Hello & World",
"Hello & <strong>World</strong>\n",
"Hello & World #2\n",
"Hello & WORLD",
],
],
];
}
protected function extractBody($message) {
preg_match('|<html><body>(.*)</body></html>|s', $message['body'], $matches);
return trim($matches[1]);
}
}