function ContactSitewideTest::testAutoReply

Same name in other branches
  1. 8.9.x core/modules/contact/tests/src/Functional/ContactSitewideTest.php \Drupal\Tests\contact\Functional\ContactSitewideTest::testAutoReply()
  2. 10 core/modules/contact/tests/src/Functional/ContactSitewideTest.php \Drupal\Tests\contact\Functional\ContactSitewideTest::testAutoReply()
  3. 11.x core/modules/contact/tests/src/Functional/ContactSitewideTest.php \Drupal\Tests\contact\Functional\ContactSitewideTest::testAutoReply()

Tests auto-reply on the site-wide contact form.

File

core/modules/contact/tests/src/Functional/ContactSitewideTest.php, line 442

Class

ContactSitewideTest
Tests site-wide contact form functionality.

Namespace

Drupal\Tests\contact\Functional

Code

public function testAutoReply() {
    // Create and log in administrative user.
    $admin_user = $this->drupalCreateUser([
        'access site-wide contact form',
        'administer contact forms',
        'administer permissions',
        'administer users',
        'access site reports',
    ]);
    $this->drupalLogin($admin_user);
    // Set up three forms, 2 with an auto-reply and one without.
    $foo_autoreply = $this->randomMachineName(40);
    $bar_autoreply = $this->randomMachineName(40);
    $this->addContactForm('foo', 'foo', 'foo@example.com', $foo_autoreply, FALSE);
    $this->addContactForm('bar', 'bar', 'bar@example.com', $bar_autoreply, FALSE);
    $this->addContactForm('no_autoreply', 'no_autoreply', 'bar@example.com', '', FALSE);
    // Log the current user out in order to test the name and email fields.
    $this->drupalLogout();
    user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
        'access site-wide contact form',
    ]);
    // Test the auto-reply for form 'foo'.
    $email = $this->randomMachineName(32) . '@example.com';
    $subject = $this->randomMachineName(64);
    $this->submitContact($this->randomMachineName(16), $email, $subject, 'foo', $this->randomString(128));
    // We are testing the auto-reply, so there should be one email going to the sender.
    $captured_emails = $this->getMails([
        'id' => 'contact_page_autoreply',
        'to' => $email,
    ]);
    $this->assertCount(1, $captured_emails);
    $this->assertEquals(trim(MailFormatHelper::htmlToText($foo_autoreply)), trim($captured_emails[0]['body']));
    // Test the auto-reply for form 'bar'.
    $email = $this->randomMachineName(32) . '@example.com';
    $this->submitContact($this->randomMachineName(16), $email, $this->randomString(64), 'bar', $this->randomString(128));
    // Auto-reply for form 'bar' should result in one auto-reply email to the sender.
    $captured_emails = $this->getMails([
        'id' => 'contact_page_autoreply',
        'to' => $email,
    ]);
    $this->assertCount(1, $captured_emails);
    $this->assertEquals(trim(MailFormatHelper::htmlToText($bar_autoreply)), trim($captured_emails[0]['body']));
    // Verify that no auto-reply is sent when the auto-reply field is left blank.
    $email = $this->randomMachineName(32) . '@example.com';
    $this->submitContact($this->randomMachineName(16), $email, $this->randomString(64), 'no_autoreply', $this->randomString(128));
    $captured_emails = $this->getMails([
        'id' => 'contact_page_autoreply',
        'to' => $email,
    ]);
    $this->assertCount(0, $captured_emails);
    // Verify that the current error message doesn't show, that the auto-reply
    // doesn't get sent and the correct silent error gets logged.
    $email = '';
    \Drupal::service('entity_display.repository')->getFormDisplay('contact_message', 'foo')
        ->removeComponent('mail')
        ->save();
    $this->submitContact($this->randomMachineName(16), $email, $this->randomString(64), 'foo', $this->randomString(128));
    $this->assertSession()
        ->pageTextNotContains('Unable to send email. Contact the site administrator if the problem persists.');
    $captured_emails = $this->getMails([
        'id' => 'contact_page_autoreply',
        'to' => $email,
    ]);
    $this->assertCount(0, $captured_emails);
    $this->drupalLogin($admin_user);
    $this->drupalGet('admin/reports/dblog');
    $this->assertSession()
        ->responseContains('Error sending auto-reply, missing sender e-mail address in foo');
}

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