View source
<?php
namespace Drupal\Tests\maillog\Kernel;
use Drupal\KernelTests\KernelTestBase;
class MailTest extends KernelTestBase {
public static $modules = [
'maillog',
'maillog_test',
'user',
'system',
'views',
];
protected function setUp() {
parent::setUp();
$this
->installSchema('maillog', 'maillog');
$this
->installConfig([
'system',
'maillog',
]);
$this
->config('system.site')
->set('mail', 'simpletest@example.com')
->save();
$GLOBALS['config']['system.mail']['interface']['default'] = 'maillog';
$this
->config('maillog.settings')
->set('send', FALSE)
->save();
}
public function testLogging() {
$language = \Drupal::languageManager()
->getCurrentLanguage();
$from_email = 'simpletest@example.com';
$reply_email = 'someone_else@example.com';
\Drupal::service('plugin.manager.mail')
->mail('simpletest', 'from_test', 'from_test@example.com', $language, [], $reply_email);
$logged_email = $this
->getLatestMaillogEntry();
$this
->assertTrue(is_array($logged_email), 'Email is captured.');
$this
->assertEqual($from_email, $logged_email['header_from'], 'Email is sent correctly.');
$this
->assertEqual($reply_email, $logged_email['header_all']['Reply-to'], 'Message is sent with correct reply address.');
}
protected function getLatestMaillogEntry() {
$result = \Drupal::database()
->queryRange('SELECT id, header_from, header_to, header_reply_to, header_all, subject, body
FROM {maillog}
ORDER BY id DESC', 0, 1);
if ($maillog = $result
->fetchAssoc()) {
$maillog['header_all'] = unserialize($maillog['header_all']);
}
return $maillog;
}
public function testLongSubject() {
$from_email = 'from_test@example.com';
$to_email = 'to_test@example.com';
\Drupal::service('plugin.manager.mail')
->mail('maillog_test', 'maillog_long_subject_test', $to_email, 'en', [], $from_email);
$logged_email = $this
->getLatestMaillogEntry();
$this
->assertTrue(!empty($logged_email), 'The test email was captured.');
$subject = str_repeat('Test email subject ', 20);
$subject_trimmed = mb_substr($subject, 0, 255);
self::assertEquals($subject_trimmed, $logged_email['subject'], 'Email subject was trimmed correctly.');
self::assertNotEquals($subject, $logged_email['subject'], 'Email subject is not untrimmed.');
}
}