You are here

public function ContactPersonalTest::testSendPersonalContactMessage in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/contact/tests/src/Functional/ContactPersonalTest.php \Drupal\Tests\contact\Functional\ContactPersonalTest::testSendPersonalContactMessage()

Tests that mails for contact messages are correctly sent.

File

core/modules/contact/tests/src/Functional/ContactPersonalTest.php, line 80

Class

ContactPersonalTest
Tests personal contact form functionality.

Namespace

Drupal\Tests\contact\Functional

Code

public function testSendPersonalContactMessage() {

  // Ensure that the web user's email needs escaping.
  $mail = $this->webUser
    ->getAccountName() . '&escaped@example.com';
  $this->webUser
    ->setEmail($mail)
    ->save();
  $this
    ->drupalLogin($this->webUser);
  $this
    ->drupalGet('user/' . $this->contactUser
    ->id() . '/contact');
  $this
    ->assertEscaped($mail);
  $message = $this
    ->submitPersonalContact($this->contactUser);
  $mails = $this
    ->getMails();
  $this
    ->assertCount(1, $mails);
  $mail = $mails[0];
  $this
    ->assertEqual($mail['to'], $this->contactUser
    ->getEmail());
  $this
    ->assertEqual($mail['from'], $this
    ->config('system.site')
    ->get('mail'));
  $this
    ->assertEqual($mail['reply-to'], $this->webUser
    ->getEmail());
  $this
    ->assertEqual($mail['key'], 'user_mail');
  $variables = [
    '@site-name' => $this
      ->config('system.site')
      ->get('name'),
    '@subject' => $message['subject[0][value]'],
    '@recipient-name' => $this->contactUser
      ->getDisplayName(),
  ];
  $subject = PlainTextOutput::renderFromHtml(t('[@site-name] @subject', $variables));
  $this
    ->assertEqual($mail['subject'], $subject, 'Subject is in sent message.');
  $this
    ->assertStringContainsString('Hello ' . $variables['@recipient-name'], $mail['body'], 'Recipient name is in sent message.');
  $this
    ->assertStringContainsString($this->webUser
    ->getDisplayName(), $mail['body'], 'Sender name is in sent message.');
  $this
    ->assertStringContainsString($message['message[0][value]'], $mail['body'], 'Message body is in sent message.');

  // Check there was no problems raised during sending.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->adminUser);

  // Verify that the correct watchdog message has been logged.
  $this
    ->drupalGet('/admin/reports/dblog');
  $placeholders = [
    '@sender_name' => $this->webUser->username,
    '@sender_email' => $this->webUser
      ->getEmail(),
    '@recipient_name' => $this->contactUser
      ->getAccountName(),
  ];
  $this
    ->assertRaw(new FormattableMarkup('@sender_name (@sender_email) sent @recipient_name an email.', $placeholders));

  // Ensure an unescaped version of the email does not exist anywhere.
  $this
    ->assertNoRaw($this->webUser
    ->getEmail());

  // Test HTML mails.
  $mail_config = $this
    ->config('system.mail');
  $mail_config
    ->set('interface.default', 'test_html_mail_collector');
  $mail_config
    ->save();
  $this
    ->drupalLogin($this->webUser);
  $message['message[0][value]'] = 'This <i>is</i> a more <b>specific</b> <sup>test</sup>, the emails are formatted now.';
  $message = $this
    ->submitPersonalContact($this->contactUser, $message);

  // Assert mail content.
  $this
    ->assertMailString('body', 'Hello ' . $variables['@recipient-name'], 1);
  $this
    ->assertMailString('body', $this->webUser
    ->getDisplayName(), 1);
  $this
    ->assertMailString('body', Html::Escape($message['message[0][value]']), 1);
}