function ContactSitewideTest::testSiteWideContact

Tests configuration options and the site-wide contact form.

File

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

Class

ContactSitewideTest
Tests the site-wide contact form.

Namespace

Drupal\contact\Tests

Code

function testSiteWideContact() {

  // Create and login administrative user.
  $admin_user = $this
    ->drupalCreateUser(array(
    'access site-wide contact form',
    'administer contact forms',
    'administer users',
  ));
  $this
    ->drupalLogin($admin_user);
  $flood_limit = 3;
  config('contact.settings')
    ->set('flood.limit', $flood_limit)
    ->set('flood.interval', 600)
    ->save();

  // Set settings.
  $edit = array();
  $edit['contact_default_status'] = TRUE;
  $this
    ->drupalPost('admin/config/people/accounts', $edit, t('Save configuration'));
  $this
    ->assertText(t('The configuration options have been saved.'));
  $this
    ->drupalGet('admin/structure/contact');

  // Default category exists.
  $this
    ->assertLinkByHref('admin/structure/contact/manage/feedback/delete');

  // User category could not be changed or deleted.
  $this
    ->assertNoLinkByHref('admin/structure/contact/manage/personal');
  $this
    ->assertNoLinkByHref('admin/structure/contact/manage/personal/delete');
  $this
    ->drupalGet('admin/structure/contact/manage/personal');
  $this
    ->assertResponse(403);

  // Delete old categories to ensure that new categories are used.
  $this
    ->deleteCategories();
  $this
    ->drupalGet('admin/structure/contact');
  $this
    ->assertText('Personal', 'Personal category was not deleted');
  $this
    ->assertNoLinkByHref('admin/structure/contact/manage/feedback');

  // Ensure that the contact form won't be shown without categories.
  user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array(
    'access site-wide contact form',
  ));
  $this
    ->drupalLogout();
  $this
    ->drupalGet('contact');
  $this
    ->assertResponse(404);
  $this
    ->drupalLogin($admin_user);
  $this
    ->drupalGet('contact');
  $this
    ->assertResponse(200);
  $this
    ->assertText(t('The contact form has not been configured.'));

  // Add categories.
  // Test invalid recipients.
  $invalid_recipients = array(
    'invalid',
    'invalid@',
    'invalid@site.',
    '@site.',
    '@site.com',
  );
  foreach ($invalid_recipients as $invalid_recipient) {
    $this
      ->addCategory($this
      ->randomName(16), $this
      ->randomName(16), $invalid_recipient, '', FALSE);
    $this
      ->assertRaw(t('%recipient is an invalid e-mail address.', array(
      '%recipient' => $invalid_recipient,
    )));
  }

  // Test validation of empty category and recipients fields.
  $this
    ->addCategory('', '', '', '', TRUE);
  $this
    ->assertText(t('Label field is required.'));
  $this
    ->assertText(t('Machine-readable name field is required.'));
  $this
    ->assertText(t('Recipients field is required.'));

  // Create first valid category.
  $recipients = array(
    'simpletest@example.com',
    'simpletest2@example.com',
    'simpletest3@example.com',
  );
  $this
    ->addCategory($id = drupal_strtolower($this
    ->randomName(16)), $label = $this
    ->randomName(16), implode(',', array(
    $recipients[0],
  )), '', TRUE);
  $this
    ->assertRaw(t('Category %label has been added.', array(
    '%label' => $label,
  )));

  // Check that the category was created in site default language.
  $langcode = config('contact.category.' . $id)
    ->get('langcode');
  $default_langcode = language_default()->langcode;
  $this
    ->assertEqual($langcode, $default_langcode);

  // Make sure the newly created category is included in the list of categories.
  $this
    ->assertNoUniqueText($label, 'New category included in categories list.');

  // Test update contact form category.
  $this
    ->updateCategory($id, $label = $this
    ->randomName(16), $recipients_str = implode(',', array(
    $recipients[0],
    $recipients[1],
  )), $reply = $this
    ->randomName(30), FALSE);
  $config = config('contact.category.' . $id)
    ->get();
  $this
    ->assertEqual($config['label'], $label);
  $this
    ->assertEqual($config['recipients'], array(
    $recipients[0],
    $recipients[1],
  ));
  $this
    ->assertEqual($config['reply'], $reply);
  $this
    ->assertNotEqual($id, config('contact.settings')
    ->get('default_category'));
  $this
    ->assertRaw(t('Category %label has been updated.', array(
    '%label' => $label,
  )));

  // Reset the category back to be the default category.
  config('contact.settings')
    ->set('default_category', $id)
    ->save();

  // Ensure that the contact form is shown without a category selection input.
  user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array(
    'access site-wide contact form',
  ));
  $this
    ->drupalLogout();
  $this
    ->drupalGet('contact');
  $this
    ->assertText(t('Your e-mail address'));
  $this
    ->assertNoText(t('Category'));
  $this
    ->drupalLogin($admin_user);

  // Add more categories.
  $this
    ->addCategory(drupal_strtolower($this
    ->randomName(16)), $label = $this
    ->randomName(16), implode(',', array(
    $recipients[0],
    $recipients[1],
  )), '', FALSE);
  $this
    ->assertRaw(t('Category %label has been added.', array(
    '%label' => $label,
  )));
  $this
    ->addCategory($name = drupal_strtolower($this
    ->randomName(16)), $label = $this
    ->randomName(16), implode(',', array(
    $recipients[0],
    $recipients[1],
    $recipients[2],
  )), '', FALSE);
  $this
    ->assertRaw(t('Category %label has been added.', array(
    '%label' => $label,
  )));

  // Try adding a category that already exists.
  $this
    ->addCategory($name, $label, '', '', FALSE);
  $this
    ->assertNoRaw(t('Category %label has been saved.', array(
    '%label' => $label,
  )));
  $this
    ->assertRaw(t('The machine-readable name is already in use. It must be unique.'));

  // Clear flood table in preparation for flood test and allow other checks to complete.
  db_delete('flood')
    ->execute();
  $num_records_after = db_query("SELECT COUNT(*) FROM {flood}")
    ->fetchField();
  $this
    ->assertIdentical($num_records_after, '0', 'Flood table emptied.');
  $this
    ->drupalLogout();

  // Check to see that anonymous user cannot see contact page without permission.
  user_role_revoke_permissions(DRUPAL_ANONYMOUS_RID, array(
    'access site-wide contact form',
  ));
  $this
    ->drupalGet('contact');
  $this
    ->assertResponse(403);

  // Give anonymous user permission and see that page is viewable.
  user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array(
    'access site-wide contact form',
  ));
  $this
    ->drupalGet('contact');
  $this
    ->assertResponse(200);

  // Submit contact form with invalid values.
  $this
    ->submitContact('', $recipients[0], $this
    ->randomName(16), $id, $this
    ->randomName(64));
  $this
    ->assertText(t('Your name field is required.'));
  $this
    ->submitContact($this
    ->randomName(16), '', $this
    ->randomName(16), $id, $this
    ->randomName(64));
  $this
    ->assertText(t('Your e-mail address field is required.'));
  $this
    ->submitContact($this
    ->randomName(16), $invalid_recipients[0], $this
    ->randomName(16), $id, $this
    ->randomName(64));
  $this
    ->assertRaw(t('The e-mail address %mail is not valid.', array(
    '%mail' => 'invalid',
  )));
  $this
    ->submitContact($this
    ->randomName(16), $recipients[0], '', $id, $this
    ->randomName(64));
  $this
    ->assertText(t('Subject field is required.'));
  $this
    ->submitContact($this
    ->randomName(16), $recipients[0], $this
    ->randomName(16), $id, '');
  $this
    ->assertText(t('Message field is required.'));

  // Test contact form with no default category selected.
  config('contact.settings')
    ->set('default_category', '')
    ->save();
  $this
    ->drupalGet('contact');
  $this
    ->assertResponse(404);

  // Try to access contact form with non-existing category IDs.
  $this
    ->drupalGet('contact/0');
  $this
    ->assertResponse(404);
  $this
    ->drupalGet('contact/' . $this
    ->randomName());
  $this
    ->assertResponse(404);

  // Submit contact form with correct values and check flood interval.
  for ($i = 0; $i < $flood_limit; $i++) {
    $this
      ->submitContact($this
      ->randomName(16), $recipients[0], $this
      ->randomName(16), $id, $this
      ->randomName(64));
    $this
      ->assertText(t('Your message has been sent.'));
  }

  // Submit contact form one over limit.
  $this
    ->drupalGet('contact');
  $this
    ->assertResponse(403);
  $this
    ->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array(
    '%number' => config('contact.settings')
      ->get('flood.limit'),
    '@interval' => format_interval(600),
  )));
}