function ContactSitewideTest::testAutoReply

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

File

drupal/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php, line 197
Definition of Drupal\contact\Tests\ContactSitewideTest.

Class

ContactSitewideTest
Tests the site-wide contact form.

Namespace

Drupal\contact\Tests

Code

function testAutoReply() {

  // Create and login administrative user.
  $admin_user = $this
    ->drupalCreateUser(array(
    'access site-wide contact form',
    'administer contact forms',
    'administer permissions',
    'administer users',
  ));
  $this
    ->drupalLogin($admin_user);

  // Set up three categories, 2 with an auto-reply and one without.
  $foo_autoreply = $this
    ->randomName(40);
  $bar_autoreply = $this
    ->randomName(40);
  $this
    ->addCategory('foo', 'foo', 'foo@example.com', $foo_autoreply, FALSE);
  $this
    ->addCategory('bar', 'bar', 'bar@example.com', $bar_autoreply, FALSE);
  $this
    ->addCategory('no_autoreply', 'no_autoreply', 'bar@example.com', '', FALSE);

  // Log the current user out in order to test the name and e-mail fields.
  $this
    ->drupalLogout();
  user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array(
    'access site-wide contact form',
  ));

  // Test the auto-reply for category 'foo'.
  $email = $this
    ->randomName(32) . '@example.com';
  $subject = $this
    ->randomName(64);
  $this
    ->submitContact($this
    ->randomName(16), $email, $subject, 'foo', $this
    ->randomString(128));

  // We are testing the auto-reply, so there should be one e-mail going to the sender.
  $captured_emails = $this
    ->drupalGetMails(array(
    'id' => 'contact_page_autoreply',
    'to' => $email,
  ));
  $this
    ->assertEqual(count($captured_emails), 1);
  $this
    ->assertEqual(trim($captured_emails[0]['body']), trim(drupal_html_to_text($foo_autoreply)));

  // Test the auto-reply for category 'bar'.
  $email = $this
    ->randomName(32) . '@example.com';
  $this
    ->submitContact($this
    ->randomName(16), $email, $this
    ->randomString(64), 'bar', $this
    ->randomString(128));

  // Auto-reply for category 'bar' should result in one auto-reply e-mail to the sender.
  $captured_emails = $this
    ->drupalGetMails(array(
    'id' => 'contact_page_autoreply',
    'to' => $email,
  ));
  $this
    ->assertEqual(count($captured_emails), 1);
  $this
    ->assertEqual(trim($captured_emails[0]['body']), trim(drupal_html_to_text($bar_autoreply)));

  // Verify that no auto-reply is sent when the auto-reply field is left blank.
  $email = $this
    ->randomName(32) . '@example.com';
  $this
    ->submitContact($this
    ->randomName(16), $email, $this
    ->randomString(64), 'no_autoreply', $this
    ->randomString(128));
  $captured_emails = $this
    ->drupalGetMails(array(
    'id' => 'contact_page_autoreply',
    'to' => $email,
  ));
  $this
    ->assertEqual(count($captured_emails), 0);
}