You are here

public function ContactStorageTest::testAutoReplyField in Contact Storage 8

Tests the Auto-reply field.

File

tests/src/Functional/ContactStorageTest.php, line 419

Class

ContactStorageTest
Tests storing contact messages and viewing them through UI.

Namespace

Drupal\Tests\contact_storage\Functional

Code

public function testAutoReplyField() {

  // Create a new contact form with an auto-reply.
  $this
    ->addContactForm('test_auto_reply_id_1', 'test_auto_reply_label_1', 'simpletest@example.com', TRUE, [
    'reply[value]' => "auto_reply_1\nsecond_line",
  ]);
  $this
    ->assertText('Contact form test_auto_reply_label_1 has been added.');

  // Verify that the auto-reply shows up in the field and only offers
  // one format (plain text), since html e-mails are disabled.
  $this
    ->drupalGet('admin/structure/contact/manage/test_auto_reply_id_1');
  $this
    ->assertNotEmpty($this
    ->xpath('//textarea[@id="edit-reply-value" and text()=:text]', [
    ':text' => "auto_reply_1\nsecond_line",
  ]));
  $this
    ->assertEmpty($this
    ->xpath('//select[@name="reply[format]"]'));
  $this
    ->drupalGet('contact');
  $edit = [
    'subject[0][value]' => 'Test subject',
    'message[0][value]' => 'Test message',
  ];
  $this
    ->drupalPostForm('contact', $edit, t('Send message'));
  $this
    ->assertText('Your message has been sent.');
  $captured_emails = $this
    ->drupalGetMails();

  // Checks that the last captured email is the auto-reply, has a correct
  // body and is in html format.
  $this
    ->assertEqual(end($captured_emails)['key'], 'page_autoreply');
  $this
    ->assertStringContainsString("auto_reply_1\nsecond_line", end($captured_emails)['body']);
  $this
    ->assertStringContainsString('text/plain', end($captured_emails)['headers']['Content-Type']);

  // Enable sending messages in html format and verify that the available
  // formats correctly show up on the contact form edit page.
  $this
    ->drupalPostForm('/admin/structure/contact/settings', [
    'send_html' => TRUE,
  ], t('Save configuration'));
  $this
    ->drupalGet('admin/structure/contact/manage/test_auto_reply_id_1');
  $this
    ->assertNotEmpty($this
    ->xpath('//select[@name="reply[format]"]//option[@value="plain_text" and @selected="selected"]'));
  $this
    ->assertNotEmpty($this
    ->xpath('//select[@name="reply[format]"]//option[@value="full_html"]'));

  // Use custom testing mail system to support HTML mails.
  $mail_config = $this
    ->config('system.mail');
  $mail_config
    ->set('interface.default', 'test_contact_storage_html_mail');
  $mail_config
    ->save();

  // Test sending a HTML mail.
  $this
    ->drupalGet('contact');
  $edit = [
    'subject[0][value]' => 'Test subject',
    'message[0][value]' => 'Test message',
  ];
  $this
    ->drupalPostForm('contact', $edit, t('Send message'));
  $this
    ->assertText('Your message has been sent.');
  $captured_emails = $this
    ->drupalGetMails();
  $this
    ->assertEqual(end($captured_emails)['key'], 'page_autoreply');
  $this
    ->assertTrue(strpos(end($captured_emails)['body'], "auto_reply_1<br />\nsecond_line") !== FALSE);
  $this
    ->assertEqual(end($captured_emails)['headers']['Content-Type'], 'text/html');

  // Select full html format (not selected by default) and verify that it is
  // properly set.
  $this
    ->drupalPostForm('admin/structure/contact/manage/test_auto_reply_id_1', [
    'reply[format]' => 'full_html',
  ], t('Save'));
  $this
    ->drupalGet('admin/structure/contact/manage/test_auto_reply_id_1');
  $this
    ->assertNotEmpty($this
    ->xpath('//select[@name="reply[format]"]//option[@value="full_html" and @selected="selected"]'));
}