View source
<?php
namespace Drupal\Tests\devel\Kernel;
use Drupal\Core\Mail\Plugin\Mail\TestMailCollector;
use Drupal\devel\Plugin\Mail\DevelMailLog;
use Drupal\KernelTests\KernelTestBase;
class DevelMailLogTest extends KernelTestBase {
public static $modules = [
'devel',
'devel_test',
'system',
];
protected $mailManager;
protected function setUp() {
parent::setUp();
$this
->installSchema('system', 'mail');
$this
->installConfig([
'system',
'devel',
]);
$this
->config('system.site')
->set('mail', 'devel-test@example.com')
->save();
$this->mailManager = $this->container
->get('plugin.manager.mail');
}
public function testDevelMailLogDefaultBackend() {
$this
->setDevelMailLogAsDefaultBackend();
$mail_backend = $this->mailManager
->getInstance([
'module' => 'default',
'key' => 'default',
]);
$this
->assertInstanceOf(DevelMailLog::class, $mail_backend);
$mail_backend = $this->mailManager
->getInstance([
'module' => 'somemodule',
'key' => 'default',
]);
$this
->assertInstanceOf(DevelMailLog::class, $mail_backend);
}
public function testDevelMailLogMultipleBackend() {
$this
->config('system.mail')
->set('interface.default', 'test_mail_collector')
->save();
$this
->config('system.mail')
->set('interface.somemodule', 'devel_mail_log')
->save();
$mail_backend = $this->mailManager
->getInstance([
'module' => 'default',
'key' => 'default',
]);
$this
->assertInstanceOf(TestMailCollector::class, $mail_backend);
$mail_backend = $this->mailManager
->getInstance([
'module' => 'somemodule',
'key' => 'default',
]);
$this
->assertInstanceOf(DevelMailLog::class, $mail_backend);
}
public function testDevelMailDefaultSettings() {
$config = \Drupal::config('devel.settings');
$this
->assertEquals('temporary://devel-mails', $config
->get('debug_mail_directory'));
$this
->assertEquals('%to-%subject-%datetime.mail.txt', $config
->get('debug_mail_file_format'));
}
public function testDevelMailLogOutput() {
$config = \Drupal::config('devel.settings');
$mail = [
'module' => 'devel_test',
'key' => 'devel_mail_log',
'to' => 'drupal@example.com',
'reply' => 'replyto@example.com',
'lang' => \Drupal::languageManager()
->getCurrentLanguage(),
];
$params = [
'subject' => 'Devel mail log subject',
'body' => 'Devel mail log body',
'headers' => [
'from' => 'postmaster@example.com',
'additional' => [
'X-stupid' => 'dumb',
],
],
];
$this
->setDevelMailLogAsDefaultBackend();
$random = $this
->randomMachineName();
$filename_pattern = '%to-%subject-' . $random . '.mail.txt';
$this
->config('devel.settings')
->set('debug_mail_file_format', $filename_pattern)
->save();
$expected_filename = 'drupal@example.com-Devel_mail_log_subject-' . $random . '.mail.txt';
$expected_output = <<<EOF
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed; delsp=yes
Content-Transfer-Encoding: 8Bit
X-Mailer: Drupal
Return-Path: devel-test@example.com
Sender: devel-test@example.com
From: postmaster@example.com
Reply-to: replyto@example.com
X-stupid: dumb
To: drupal@example.com
Subject: Devel mail log subject
Devel mail log body
EOF;
$default_output_directory = $config
->get('debug_mail_directory');
$expected_file_path = $default_output_directory . '/' . $expected_filename;
$this->mailManager
->mail($mail['module'], $mail['key'], $mail['to'], $mail['lang'], $params, $mail['reply']);
$this
->assertFileExists($expected_file_path);
$this
->assertStringEqualsFile($expected_file_path, $expected_output);
$changed_output_directory = 'temporary://my-folder';
$expected_file_path = $changed_output_directory . '/' . $expected_filename;
$this
->config('devel.settings')
->set('debug_mail_directory', $changed_output_directory)
->save();
$result = $this->mailManager
->mail($mail['module'], $mail['key'], $mail['to'], $mail['lang'], $params, $mail['reply']);
$this
->assertSame(TRUE, $result['result']);
$this
->assertFileExists($expected_file_path);
$this
->assertStringEqualsFile($expected_file_path, $expected_output);
$public_output_directory = 'public://my-folder';
$expected_file_path = $public_output_directory . '/' . $expected_filename;
$this
->config('devel.settings')
->set('debug_mail_directory', $public_output_directory)
->save();
$this->mailManager
->mail($mail['module'], $mail['key'], $mail['to'], $mail['lang'], $params, $mail['reply']);
$this
->assertFileExists($expected_file_path);
$this
->assertStringEqualsFile($expected_file_path, $expected_output);
$this
->assertFileExists($public_output_directory . '/.htaccess');
}
private function setDevelMailLogAsDefaultBackend() {
$GLOBALS['config']['system.mail']['interface']['default'] = 'devel_mail_log';
$this
->config('system.mail')
->set('interface.default', 'devel_mail_log')
->save();
}
}