123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- <?php
- class ContactSitewideTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Site-wide contact form',
- 'description' => 'Tests site-wide contact form functionality.',
- 'group' => 'Contact',
- );
- }
- function setUp() {
- parent::setUp('contact');
- }
-
- function testSiteWideContact() {
-
- $admin_user = $this->drupalCreateUser(array('access site-wide contact form', 'administer contact forms', 'administer users'));
- $this->drupalLogin($admin_user);
- $flood_limit = 3;
- variable_set('contact_threshold_limit', $flood_limit);
- variable_set('contact_threshold_window', 600);
-
- $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.'), 'Setting successfully saved.');
-
- $this->deleteCategories();
-
- 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.'));
-
-
- $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 e-mail 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('Category %category has been saved.', array('%category' => $category)), 'Category successfully saved.');
-
- $this->assertNoUniqueText($category, 'New category included in categories list.');
-
- $categories = $this->getCategories();
- $category_id = $this->updateCategory($categories, $category = $this->randomName(16), $recipients_str = implode(',', array($recipients[0], $recipients[1])), $reply = $this->randomName(30), FALSE);
- $category_array = db_query("SELECT category, recipients, reply, selected FROM {contact} WHERE cid = :cid", array(':cid' => $category_id))->fetchAssoc();
- $this->assertEqual($category_array['category'], $category);
- $this->assertEqual($category_array['recipients'], $recipients_str);
- $this->assertEqual($category_array['reply'], $reply);
- $this->assertFalse($category_array['selected']);
- $this->assertRaw(t('Category %category has been saved.', array('%category' => $category)), 'Category successfully saved.');
-
- 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'), '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->drupalLogin($admin_user);
-
- $this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1])), '', FALSE);
- $this->assertRaw(t('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('Category %category has been saved.', array('%category' => $category)), 'Category successfully saved.');
-
- $this->addCategory($category, '', '', FALSE);
- $this->assertNoRaw(t('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->drupalLogout();
-
- user_role_revoke_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form'));
- $this->drupalGet('contact');
- $this->assertResponse(403, 'Access denied to anonymous user without permission.');
-
- user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form'));
- $this->drupalGet('contact');
- $this->assertResponse(200, 'Access granted to anonymous user with permission.');
-
- $this->submitContact('', $recipients[0], $this->randomName(16), $categories[0], $this->randomName(64));
- $this->assertText(t('Your name field is required.'), 'Name required.');
- $this->submitContact($this->randomName(16), '', $this->randomName(16), $categories[0], $this->randomName(64));
- $this->assertText(t('Your e-mail address field is required.'), 'E-mail required.');
- $this->submitContact($this->randomName(16), $invalid_recipients[0], $this->randomName(16), $categories[0], $this->randomName(64));
- $this->assertText(t('You must enter a valid e-mail address.'), 'Valid e-mail required.');
- $this->submitContact($this->randomName(16), $recipients[0], '', $categories[0], $this->randomName(64));
- $this->assertText(t('Subject field is required.'), 'Subject required.');
- $this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), $categories[0], '');
- $this->assertText(t('Message field is required.'), 'Message required.');
-
- db_update('contact')
- ->fields(array('selected' => 0))
- ->execute();
- $this->drupalGet('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), $categories[0], $this->randomName(64));
- $this->assertText(t('Your message has been sent.'), 'Message sent.');
- }
-
- $this->drupalGet('contact');
- $this->assertResponse(403, 'Access denied to anonymous user after reaching message treshold.');
- $this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => variable_get('contact_threshold_limit', 3), '@interval' => format_interval(600))), 'Message threshold reached.');
-
- $this->drupalLogin($admin_user);
- $this->deleteCategories();
- }
-
- function testAutoReply() {
-
- $admin_user = $this->drupalCreateUser(array('access site-wide contact form', 'administer contact forms', 'administer permissions', 'administer users'));
- $this->drupalLogin($admin_user);
-
- $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', 'bar@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->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'foo@example.com'));
- $this->assertEqual(count($captured_emails), 1, 'Auto-reply e-mail was sent to the sender for category "foo".', 'Contact');
- $this->assertEqual($captured_emails[0]['body'], drupal_html_to_text($foo_autoreply), 'Auto-reply e-mail 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->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'bar@example.com'));
- $this->assertEqual(count($captured_emails), 1, 'Auto-reply e-mail was sent to the sender for category "bar".', 'Contact');
- $this->assertEqual($captured_emails[0]['body'], drupal_html_to_text($bar_autoreply), 'Auto-reply e-mail 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->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'no_autoreply@example.com'));
- $this->assertEqual(count($captured_emails), 0, 'No auto-reply e-mail was sent to the sender for category "no-autoreply".', 'Contact');
- }
-
- function addCategory($category, $recipients, $reply, $selected) {
- $edit = array();
- $edit['category'] = $category;
- $edit['recipients'] = $recipients;
- $edit['reply'] = $reply;
- $edit['selected'] = ($selected ? '1' : '0');
- $this->drupalPost('admin/structure/contact/add', $edit, t('Save'));
- }
-
- 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->drupalPost('admin/structure/contact/edit/' . $category_id, $edit, t('Save'));
- return ($category_id);
- }
-
- 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->drupalPost('contact', $edit, t('Send message'));
- }
-
- function deleteCategories() {
- $categories = $this->getCategories();
- foreach ($categories as $category) {
- $category_name = db_query("SELECT category FROM {contact} WHERE cid = :cid", array(':cid' => $category))->fetchField();
- $this->drupalPost('admin/structure/contact/delete/' . $category, array(), t('Delete'));
- $this->assertRaw(t('Category %category has been deleted.', array('%category' => $category_name)), 'Category deleted successfully.');
- }
- }
-
- function getCategories() {
- $categories = db_query('SELECT cid FROM {contact}')->fetchCol();
- return $categories;
- }
- }
- class ContactPersonalTestCase extends DrupalWebTestCase {
- private $admin_user;
- private $web_user;
- private $contact_user;
- public static function getInfo() {
- return array(
- 'name' => 'Personal contact form',
- 'description' => 'Tests personal contact form functionality.',
- 'group' => 'Contact',
- );
- }
- function setUp() {
- parent::setUp('contact');
-
- $this->admin_user = $this->drupalCreateUser(array('administer contact forms', 'administer users'));
-
- variable_set('contact_default_status', TRUE);
- $this->web_user = $this->drupalCreateUser(array('access user contact forms'));
- $this->contact_user = $this->drupalCreateUser();
- }
-
- function testPersonalContactAccess() {
-
- $this->drupalLogin($this->web_user);
- $this->drupalGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(200);
-
- $this->drupalGet('user/' . $this->web_user->uid . '/contact');
- $this->assertResponse(403);
-
- $this->drupalGet('user/0/contact');
- $this->assertResponse(403);
-
- $this->drupalLogout();
- user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access user contact forms'));
- $this->drupalGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(200);
-
- user_role_revoke_permissions(DRUPAL_ANONYMOUS_RID, array('access user contact forms'));
- $this->drupalGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(403);
-
- $this->drupalLogin($this->admin_user);
- $edit = array('contact_default_status' => FALSE);
- $this->drupalPost('admin/config/people/accounts', $edit, t('Save configuration'));
- $this->assertText(t('The configuration options have been saved.'), 'Setting successfully saved.');
- $this->drupalLogout();
-
-
- $this->contact_user = $this->drupalCreateUser();
-
- $this->drupalLogin($this->web_user);
- $this->drupalGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(403);
-
- $this->drupalLogin($this->admin_user);
- $this->drupalGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(200);
-
- $this->contact_user = $this->drupalCreateUser();
- user_save($this->contact_user, array('status' => 0));
-
- $this->drupalGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(200);
-
- $this->drupalLogin($this->web_user);
- $this->drupalGet('user/' . $this->contact_user->uid . '/contact');
- $this->assertResponse(403);
- }
-
- function testPersonalContactFlood() {
- $flood_limit = 3;
- variable_set('contact_threshold_limit', $flood_limit);
-
- db_delete('flood')->execute();
- $num_records_flood = db_query("SELECT COUNT(*) FROM {flood}")->fetchField();
- $this->assertIdentical($num_records_flood, '0', 'Flood table emptied.');
- $this->drupalLogin($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->drupalGet('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(variable_get('contact_threshold_window', 3600)))), 'Normal user denied access to flooded contact form.');
-
-
- $this->drupalLogin($this->admin_user);
- $this->assertNoText('Try again later.', 'Admin user not denied access to flooded contact form.');
- }
-
- protected function submitPersonalContact($account, array $message = array()) {
- $message += array(
- 'subject' => $this->randomName(16),
- 'message' => $this->randomName(64),
- );
- $this->drupalPost('user/' . $account->uid . '/contact', $message, t('Send message'));
- }
- }
|