You are here

public function ContactStoragePersonalTest::testSendPersonalContactMessage in Contact Storage 8

Tests that mails for contact messages are correctly sent.

File

tests/src/Functional/ContactStoragePersonalTest.php, line 69

Class

ContactStoragePersonalTest
Tests personal contact form functionality.

Namespace

Drupal\Tests\contact_storage\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
    ->drupalGetMails();
  $this
    ->assertEqual(1, count($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
    ->assertTrue(strpos($mail['body'], 'Hello ' . $variables['@recipient-name']) !== FALSE, 'Recipient name is in sent message.');
  $this
    ->assertTrue(strpos($mail['body'], $this->webUser
    ->getDisplayName()) !== FALSE, 'Sender name is in sent message.');
  $this
    ->assertTrue(strpos($mail['body'], $message['message[0][value]']) !== FALSE, '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
      ->getDisplayName(),
  ];
  $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());
}