1 contact.test ContactSitewideTestCase::testSiteWideContact()

Tests configuration options and the site-wide contact form.

File

core/modules/contact/tests/contact.test, line 29
Tests for the Contact module.

Class

ContactSitewideTestCase
Tests the site-wide contact form.

Code

function testSiteWideContact() {
  $flood_limit = 3;
  $edit = array(
    'contact_threshold_limit' => $flood_limit,
    'contact_threshold_window' => 600,
  );
  $this->backdropPost('admin/structure/contact/settings', $edit, t('Save configuration'));

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

  // Delete old categories to ensure that new categories are used.
  $this->deleteCategories();

  // Ensure that the contact form won't be shown without categories.
  user_role_grant_permissions(BACKDROP_ANONYMOUS_ROLE, array('access site-wide contact form'));
  $this->backdropLogout();
  $this->backdropGet('contact');
  $this->assertResponse(404);
  $this->backdropLogin($this->admin_user);
  $this->backdropGet('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), $invalid_recipient, '', FALSE);
    $this->assertRaw(t('%recipient is an invalid email address.', array('%recipient' => $invalid_recipient)), format_string('Caught invalid recipient (@invalid_recipient).', array('@invalid_recipient' => $invalid_recipient)));
  }

  // Test validation of empty category and recipients fields.
  $this->addCategory($category = '', '', '', TRUE);
  $this->assertText(t('Category field is required.'), 'Caught empty category field');
  $this->assertText(t('Recipients field is required.'), 'Caught empty recipients field.');

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

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

  // Test update contact form category.
  $categories = $this->getCategories();
  $recipients_str = implode(',', $recipients);
  $category_id = $this->updateCategory($categories, $category = $this->randomName(16), implode(', ', $recipients), $reply = $this->randomName(30), FALSE);
  $category_array = contact_load($category_id);
  $test_category = array_shift($categories);
  $this->assertEqual($category_array['category'], $category);
  $this->assertEqual($category_array['recipients'], $recipients_str);
  $this->assertEqual($category_array['reply'], $reply);
  $this->assertRaw(t('Contact form category %category has been saved.', array('%category' => $category)), 'Category successfully saved.');

  // Test separating contacts with new lines.
  $categories = $this->getCategories();
  $category_id = $this->updateCategory($categories, $category = $this->randomName(16), implode("\n", $recipients), $this->randomName(30), FALSE);
  $category_array = contact_load($category_id);
  $this->assertEqual($category_array['recipients'], $recipients_str);
  $this->assertRaw(t('Contact form category %category has been saved.', array('%category' => $category)), 'Category successfully saved.');

  // Test separating contacts with spaces.
  $categories = $this->getCategories();
  $category_id = $this->updateCategory($categories, $category = $this->randomName(16), implode(' ', $recipients), $this->randomName(30), FALSE);
  $category_array = contact_load($category_id);
  $this->assertEqual($category_array['recipients'], $recipients_str);
  $this->assertRaw(t('Contact form category %category has been saved.', array('%category' => $category)), 'Category successfully saved.');

  // Test separating contacts with semicolons.
  $categories = $this->getCategories();
  $category_id = $this->updateCategory($categories, $category = $this->randomName(16), implode(';', $recipients), $this->randomName(30), FALSE);
  $category_array = contact_load($category_id);
  $this->assertEqual($category_array['recipients'], $recipients_str);
  $this->assertRaw(t('Contact form category %category has been saved.', array('%category' => $category)), 'Category successfully saved.');

  // Ensure that the contact form is shown without a category selection input.
  user_role_grant_permissions(BACKDROP_ANONYMOUS_ROLE, array('access site-wide contact form'));
  $this->backdropLogout();
  $this->backdropGet('contact');
  $this->assertText(t('Your email address'), 'Contact form is shown when there is one category.');
  $this->assertNoText(t('Category'), 'When there is only one category, the category selection element is hidden.');
  $this->backdropLogin($this->admin_user);

  // Add more categories.
  $this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1])), '', FALSE);
  $this->assertRaw(t('Contact form category %category has been saved.', array('%category' => $category)), 'Category successfully saved.');

  $this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1], $recipients[2])), '', FALSE);
  $this->assertRaw(t('Contact form category %category has been saved.', array('%category' => $category)), 'Category successfully saved.');

  // Try adding a category that already exists.
  $this->addCategory($category, '', '', FALSE);
  $this->assertNoRaw(t('Contact form category %category has been saved.', array('%category' => $category)), 'Category not saved.');
  $this->assertRaw(t('A contact form with category %category already exists.', array('%category' => $category)), 'Duplicate category error found.');

  // 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->backdropLogout();

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

  // Give anonymous user permission and see that page is viewable.
  user_role_grant_permissions(BACKDROP_ANONYMOUS_ROLE, array('access site-wide contact form'));
  $this->backdropGet('contact');
  $this->assertResponse(200, 'Access granted to anonymous user with permission.');

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

  // Set contact_default_category to none
  $config_data = contact_config_data();
  $config_data['contact_default_category'] = 0;
  $config = config('contact.categories');
  $config->setData($config_data);
  $config->save();

  $this->backdropGet('contact');
  $this->assertRaw(t('- Please choose -'), 'Without selected categories the visitor is asked to chose a category.');

  // Submit contact form with invalid category id (cid 0).
  $this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), 0, '');
  $this->assertText(t('You must select a valid category.'), 'Valid category required.');

  // 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), $test_category, $this->randomName(64));
    $this->assertText(t('Your message has been sent.'), 'Message sent.');
  }
  // Submit contact form one over limit.
  $this->backdropGet('contact');
  $config = config('contact.settings');
  $this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => $config->get('contact_threshold_limit'), '@interval' => format_interval(600))), 'Message threshold reached.');

  // Delete created categories.
  $this->backdropLogin($this->admin_user);
  $this->deleteCategories();
}