You are here

public function DevelMailLogTest::testDevelMailLogOutput in Devel 8

Same name and namespace in other branches
  1. 8.3 tests/src/Kernel/DevelMailLogTest.php \Drupal\Tests\devel\Kernel\DevelMailLogTest::testDevelMailLogOutput()
  2. 8.2 tests/src/Kernel/DevelMailLogTest.php \Drupal\Tests\devel\Kernel\DevelMailLogTest::testDevelMailLogOutput()
  3. 4.x tests/src/Kernel/DevelMailLogTest.php \Drupal\Tests\devel\Kernel\DevelMailLogTest::testDevelMailLogOutput()

Tests devel mail log output.

File

tests/src/Kernel/DevelMailLogTest.php, line 96

Class

DevelMailLogTest
Tests sending mails with debug interface.

Namespace

Drupal\Tests\devel\Kernel

Code

public function testDevelMailLogOutput() {
  $config = \Drupal::config('devel.settings');

  // Parameters used for send the email.
  $mail = [
    'module' => 'devel_test',
    'key' => 'devel_mail_log',
    'to' => 'drupal@example.com',
    'reply' => 'replyto@example.com',
    'lang' => \Drupal::languageManager()
      ->getCurrentLanguage(),
  ];

  // Parameters used for compose the email in devel_test module.
  // @see devel_test_mail()
  $params = [
    'subject' => 'Devel mail log subject',
    'body' => 'Devel mail log body',
    'headers' => [
      'from' => 'postmaster@example.com',
      'additional' => [
        'X-stupid' => 'dumb',
      ],
    ],
  ];

  // Configure devel_mail_log as default mail backends.
  $this
    ->setDevelMailLogAsDefaultBackend();

  // Changes the default filename pattern removing the dynamic date
  // placeholder for a more predictable filename output.
  $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;

  // Ensures that the mail is captured by devel_mail_log and the placeholders
  // in the filename are properly resolved.
  $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);

  // Ensures that even changing the default output directory devel_mail_log
  // works as expected.
  $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);

  // Ensures that if the default output directory is a public directory it
  // will be protected by adding an .htaccess.
  $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');
}