You are here

public function ContactPersonalTest::testSendPersonalContactMessage in Drupal 9

Same name and namespace in other branches
  1. 8 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
    ->assertSession()
    ->assertEscaped($mail);
  $message = $this
    ->submitPersonalContact($this->contactUser);
  $mails = $this
    ->getMails();
  $this
    ->assertCount(1, $mails);
  $mail = $mails[0];
  $this
    ->assertEquals($this->contactUser
    ->getEmail(), $mail['to']);
  $this
    ->assertEquals($this
    ->config('system.site')
    ->get('mail'), $mail['from']);
  $this
    ->assertEquals($this->webUser
    ->getEmail(), $mail['reply-to']);
  $this
    ->assertEquals('user_mail', $mail['key']);
  $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
    ->assertEquals($subject, $mail['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
      ->getAccountName(),
    '@sender_email' => $this->webUser
      ->getEmail(),
    '@recipient_name' => $this->contactUser
      ->getAccountName(),
  ];
  $this
    ->assertSession()
    ->responseContains(new FormattableMarkup('@sender_name (@sender_email) sent @recipient_name an email.', $placeholders));

  // Ensure an unescaped version of the email does not exist anywhere.
  $this
    ->assertSession()
    ->responseNotContains($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);
}