function ContactPersonalTest::testSendPersonalContactMessage

Same name and namespace in other branches
  1. 8.9.x core/modules/contact/tests/src/Functional/ContactPersonalTest.php \Drupal\Tests\contact\Functional\ContactPersonalTest::testSendPersonalContactMessage()
  2. 10 core/modules/contact/tests/src/Functional/ContactPersonalTest.php \Drupal\Tests\contact\Functional\ContactPersonalTest::testSendPersonalContactMessage()
  3. 11.x 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 82

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']);
    $subject = '[' . $this->config('system.site')
        ->get('name') . '] ' . $message['subject[0][value]'];
    $this->assertEquals($subject, $mail['subject'], 'Subject is in sent message.');
    $this->assertStringContainsString('Hello ' . $this->contactUser
        ->getDisplayName(), $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 ' . $this->contactUser
        ->getDisplayName(), 1);
    $this->assertMailString('body', $this->webUser
        ->getDisplayName(), 1);
    $this->assertMailString('body', Html::Escape($message['message[0][value]']), 1);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.