- <?php
- * @file
- * Tests for the Contact module.
- */
-
- * Tests the site-wide contact form.
- */
- class ContactSitewideTestCase extends BackdropWebTestCase {
- protected $profile = 'testing';
-
-
- * @var User
- */
- protected $admin_user;
-
- function setUp() {
- parent::setUp('contact');
-
-
- $this->admin_user = $this->backdropCreateUser(array('access site-wide contact form', 'administer contact forms', 'administer account settings', 'administer permissions', 'administer users'));
- $this->backdropLogin($this->admin_user);
- }
-
-
- * Tests configuration options and the site-wide contact form.
- */
- 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'));
-
-
- $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.');
-
-
- $this->deleteCategories();
-
-
- 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.'));
-
-
-
- $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)));
- }
-
-
- $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.');
-
-
- $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.');
-
-
- $this->assertNoUniqueText($category, 'New category included in categories list.');
-
-
- $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.');
-
-
- $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.');
-
-
- $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.');
-
-
- $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.');
-
-
- 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);
-
-
- $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.');
-
-
- $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.');
-
-
- 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();
-
-
- 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.');
-
-
- 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.');
-
-
- $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.');
-
-
- $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.');
-
-
- $this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), 0, '');
- $this->assertText(t('You must select a valid category.'), 'Valid category required.');
-
-
- 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.');
- }
-
- $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.');
-
-
- $this->backdropLogin($this->admin_user);
- $this->deleteCategories();
- }
-
-
- * Tests auto-reply on the site-wide contact form.
- */
- function testAutoReply() {
-
- $foo_autoreply = $this->randomName(40);
- $bar_autoreply = $this->randomName(40);
- $this->addCategory('foo', 'foo@example.com', $foo_autoreply, FALSE);
- $this->addCategory('bar', 'bar@example.com', $bar_autoreply, FALSE);
- $this->addCategory('no_autoreply', 'no_autoreply@example.com', '', FALSE);
-
-
- $email = $this->randomName(32) . '@example.com';
- $subject = $this->randomName(64);
- $this->submitContact($this->randomName(16), $email, $subject, 2, $this->randomString(128));
-
-
- $captured_emails = $this->backdropGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'reply-to' => 'foo@example.com'));
- $this->assertEqual(count($captured_emails), 1, 'Auto-reply email was sent to the sender for category "foo".', 'Contact');
- $this->assertEqual($captured_emails[0]['body'], backdrop_html_to_text($foo_autoreply), 'Auto-reply email body is correct for category "foo".', 'Contact');
-
-
- $email = $this->randomName(32) . '@example.com';
- $this->submitContact($this->randomName(16), $email, $this->randomString(64), 3, $this->randomString(128));
-
-
- $captured_emails = $this->backdropGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'reply-to' => 'bar@example.com'));
- $this->assertEqual(count($captured_emails), 1, 'Auto-reply email was sent to the sender for category "bar".', 'Contact');
- $this->assertEqual($captured_emails[0]['body'], backdrop_html_to_text($bar_autoreply), 'Auto-reply email body is correct for category "bar".', 'Contact');
-
-
- $email = $this->randomName(32) . '@example.com';
- $this->submitContact($this->randomName(16), $email, $this->randomString(64), 4, $this->randomString(128));
- $captured_emails = $this->backdropGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'reply-to' => 'no_autoreply@example.com'));
- $this->assertEqual(count($captured_emails), 0, 'No auto-reply email was sent to the sender for category "no-autoreply".', 'Contact');
- }
-
-
- * Adds a category.
- *
- * @param string $category
- * The category name.
- * @param string $recipients
- * The list of recipient email addresses.
- * @param string $reply
- * The auto-reply text that is sent to a user upon completing the contact
- * form.
- * @param boolean $selected
- * Boolean indicating whether the category should be selected by default.
- */
- function addCategory($category, $recipients, $reply, $selected) {
- $edit = array();
- $edit['category'] = $category;
- $edit['recipients'] = $recipients;
- $edit['reply'] = $reply;
- $edit['selected'] = ($selected ? '1' : '0');
- $this->backdropPost('admin/structure/contact/add', $edit, t('Save'));
- }
-
-
- * Updates a category.
- *
- * @param string $category
- * The category name.
- * @param string $recipients
- * The list of recipient email addresses.
- * @param string $reply
- * The auto-reply text that is sent to a user upon completing the contact
- * form.
- * @param boolean $selected
- * Boolean indicating whether the category should be selected by default.
- */
- function updateCategory($categories, $category, $recipients, $reply, $selected) {
- $category_id = $categories[array_rand($categories)];
- $edit = array();
- $edit['category'] = $category;
- $edit['recipients'] = $recipients;
- $edit['reply'] = $reply;
- $edit['selected'] = ($selected ? '1' : '0');
- $this->backdropPost('admin/structure/contact/configure/' . $category_id, $edit, t('Save'));
- return ($category_id);
- }
-
-
- * Submits the contact form.
- *
- * @param string $name
- * The name of the sender.
- * @param string $mail
- * The email address of the sender.
- * @param string $subject
- * The subject of the message.
- * @param integer $cid
- * The category ID of the message.
- * @param string $message
- * The message body.
- */
- function submitContact($name, $mail, $subject, $cid, $message) {
- $edit = array();
- $edit['name'] = $name;
- $edit['mail'] = $mail;
- $edit['subject'] = $subject;
- $edit['cid'] = $cid;
- $edit['message'] = $message;
- $this->backdropPost('contact', $edit, t('Send message'));
- }
-
-
- * Deletes all categories.
- */
- function deleteCategories() {
- $category_ids = $this->getCategories();
- foreach ($category_ids as $cid) {
- $category = contact_load($cid);
- $category_name = $category['category'];
- $this->backdropPost('admin/structure/contact/delete/' . $cid, array(), t('Delete'));
- $this->assertRaw(t('Contact form category %category has been deleted.', array('%category' => $category_name)), 'Category deleted successfully.');
- }
- }
-
-
- * Gets a list of all category IDs.
- *
- * @return array
- * A list of the category IDs.
- */
- function getCategories() {
- $config_data = contact_config_data();
- $categories = $config_data['categories'];
- foreach ($categories as $cat) {
- $cids[] = $cat['cid'];
- }
- return $cids;
- }
- }
-
- * Tests the personal contact form.
- */
- class ContactPersonalTestCase extends BackdropWebTestCase {
- protected $profile = 'testing';
-
- private $admin_user;
- private $web_user;
- private $contact_user;
-
- function setUp() {
- parent::setUp('contact');
-
-
- $this->admin_user = $this->backdropCreateUser(array('administer contact forms', 'administer users', 'administer account settings'));
-
-
- config_set('contact.settings', 'contact_default_status', TRUE);
-
- $this->web_user = $this->backdropCreateUser(array('access user contact forms'));
- $this->contact_user = $this->backdropCreateUser();
- }
-
-
- * Tests access to the personal contact form.
- */
- function testPersonalContactAccess() {
-
- $this->backdropLogin($this->web_user);
- $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(200);
-
-
- $this->backdropGet('user/' . $this->web_user->uid . '/contact');
- $this->assertResponse(403);
-
-
- $this->backdropGet('user/0/contact');
- $this->assertResponse(403);
-
-
- $this->backdropLogout();
- user_role_grant_permissions(BACKDROP_ANONYMOUS_ROLE, array('access user contact forms'));
- $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(200);
-
-
- $this->backdropLogin($this->contact_user);
- $edit = array('contact' => FALSE);
- $this->backdropPost('user/' . $this->contact_user->uid . '/edit', $edit, 'Save');
- $this->backdropLogout();
- $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(403);
-
-
- $contact_user_temp = user_load($this->contact_user->uid, TRUE);
- user_save($contact_user_temp);
- $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(403);
-
-
- $this->backdropLogin($this->contact_user);
- $edit = array('contact' => TRUE);
- $this->backdropPost('user/' . $this->contact_user->uid . '/edit', $edit, 'Save');
- $this->backdropLogout();
- $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(200);
-
-
- user_role_revoke_permissions(BACKDROP_ANONYMOUS_ROLE, array('access user contact forms'));
- $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(403);
-
-
- $this->backdropLogin($this->admin_user);
- $edit = array('contact_default_status' => FALSE);
- $this->backdropPost('admin/config/people/settings', $edit, t('Save configuration'));
- $this->assertText(t('The configuration options have been saved.'), 'Setting successfully saved.');
- $this->backdropLogout();
-
-
-
- $this->contact_user = $this->backdropCreateUser();
-
-
- $this->backdropLogin($this->web_user);
- $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(403);
-
-
- $this->backdropLogin($this->admin_user);
- $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(200);
-
-
- $this->contact_user = $this->backdropCreateUser();
- $this->contact_user->status = 0;
- $this->contact_user->save();
-
-
- $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(200);
-
-
- $this->backdropLogin($this->web_user);
- $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(403);
- }
-
-
- * Tests the personal contact form flood protection.
- */
- function testPersonalContactFlood() {
- $flood_limit = 3;
- $config = config('contact.settings');
- $config->set('contact_threshold_limit', $flood_limit);
- $config->save();
-
-
- db_delete('flood')->execute();
- $num_records_flood = db_query("SELECT COUNT(*) FROM {flood}")->fetchField();
- $this->assertIdentical($num_records_flood, '0', 'Flood table emptied.');
-
- $this->backdropLogin($this->web_user);
-
-
- for ($i = 0; $i < $flood_limit; $i++) {
- $this->submitPersonalContact($this->contact_user);
- $this->assertText(t('Your message has been sent.'), 'Message sent.');
- }
-
-
- $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => $flood_limit, '@interval' => format_interval($config->get('contact_threshold_window')))), 'Normal user denied access to flooded contact form.');
-
-
-
- $this->backdropLogin($this->admin_user);
- $this->assertNoText('Try again later.', 'Admin user not denied access to flooded contact form.');
- }
-
-
- * Fills out a user's personal contact form and submits it.
- *
- * @param $account
- * A user object of the user being contacted.
- * @param $message
- * An optional array with the form fields being used.
- */
- protected function submitPersonalContact($account, array $message = array()) {
- $message += array(
- 'subject' => $this->randomName(16),
- 'message' => $this->randomName(64),
- );
- $this->backdropPost('user/' . $account->uid . '/contact', $message, t('Send message'));
- }
- }