diff --git a/core/modules/contact/config/contact.category.feedback.yml b/core/modules/contact/config/contact.category.feedback.yml new file mode 100644 index 0000000..ef6159a --- /dev/null +++ b/core/modules/contact/config/contact.category.feedback.yml @@ -0,0 +1,4 @@ +name: 'feedback' +category: 'Website feedback' +recipients: 'mail@example.com' +weight: '0' diff --git a/core/modules/contact/config/contact.settings.yml b/core/modules/contact/config/contact.settings.yml new file mode 100644 index 0000000..6f76e6f --- /dev/null +++ b/core/modules/contact/config/contact.settings.yml @@ -0,0 +1,4 @@ +default_category: 'feedback' +contact_default_status: '1' +contact_threshold_limit: '5' +contact_threshold_window: '3600' diff --git a/core/modules/contact/contact.admin.inc b/core/modules/contact/contact.admin.inc index f6835f9..57469b8 100644 --- a/core/modules/contact/contact.admin.inc +++ b/core/modules/contact/contact.admin.inc @@ -20,22 +20,17 @@ function contact_category_list() { $rows = array(); // Get all the contact categories from the database. - $categories = db_select('contact', 'c') - ->addTag('translatable') - ->fields('c', array('cid', 'category', 'recipients', 'selected')) - ->orderBy('weight') - ->orderBy('category') - ->execute() - ->fetchAll(); + $categories = contact_categories(); + $default_category = config('contact.settings')->get('default_category'); // Loop through the categories and add them to the table. foreach ($categories as $category) { $rows[] = array( - check_plain($category->category), - check_plain($category->recipients), - ($category->selected ? t('Yes') : t('No')), - l(t('Edit'), 'admin/structure/contact/edit/' . $category->cid), - l(t('Delete'), 'admin/structure/contact/delete/' . $category->cid), + check_plain($category['category']), + check_plain($category['recipients']), + ($default_category == $category['name'] ? t('Yes') : t('No')), + l(t('Edit'), 'admin/structure/contact/edit/' . $category['name']), + l(t('Delete'), 'admin/structure/contact/delete/' . $category['name']), ); } @@ -77,13 +72,14 @@ function contact_category_edit_form($form, &$form_state, array $category = array // If this is a new category, add the default values. $category += array( 'category' => '', + 'name' => '', 'recipients' => '', 'reply' => '', 'weight' => 0, - 'selected' => 0, - 'cid' => NULL, ); + $default_category = config('contact.settings')->get('default_category'); + $form['category'] = array( '#type' => 'textfield', '#title' => t('Category'), @@ -92,6 +88,23 @@ function contact_category_edit_form($form, &$form_state, array $category = array '#description' => t("Example: 'website feedback' or 'product information'."), '#required' => TRUE, ); + $form['name'] = array( + '#type' => 'machine_name', + '#required' => TRUE, + '#default_value' => isset($category['name']) ? $category['name'] : '', + '#maxlength' => 255, + '#machine_name' => array( + 'exists' => 'contact_load', + 'source' => array('category'), + ), + '#disabled' => !empty($category['name']), + ); + + $form['is_new'] = array( + '#type' => 'value', + '#value' => empty($category['name']), + ); + $form['recipients'] = array( '#type' => 'textarea', '#title' => t('Recipients'), @@ -114,11 +127,7 @@ function contact_category_edit_form($form, &$form_state, array $category = array $form['selected'] = array( '#type' => 'checkbox', '#title' => t('Make this the default category.'), - '#default_value' => $category['selected'], - ); - $form['cid'] = array( - '#type' => 'value', - '#value' => $category['cid'], + '#default_value' => !empty($category['name']) && $default_category == $category['name'], ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( @@ -138,15 +147,10 @@ function contact_category_edit_form_validate($form, &$form_state) { // Validate and each e-mail recipient. $recipients = explode(',', $form_state['values']['recipients']); - // When creating a new contact form, or renaming the category on an existing - // contact form, make sure that the given category is unique. - $category = $form_state['values']['category']; - $query = db_select('contact', 'c')->condition('c.category', $category, '='); - if (!empty($form_state['values']['cid'])) { - $query->condition('c.cid', $form_state['values']['cid'], '<>'); - } - if ($query->countQuery()->execute()->fetchField()) { - form_set_error('category', t('A contact form with category %category already exists.', array('%category' => $category))); + // When creating a new contact form make sure that the given category is unique. + $name = $form_state['values']['name']; + if ($form_state['values']['is_new'] && contact_load($name)) { + form_set_error('category', t('A contact form with name %name already exists.', array('%name' => $name))); } foreach ($recipients as &$recipient) { @@ -164,22 +168,34 @@ function contact_category_edit_form_validate($form, &$form_state) { * @see contact_category_edit_form_validate() */ function contact_category_edit_form_submit($form, &$form_state) { + $default_category = config('contact.settings')->get('default_category'); if ($form_state['values']['selected']) { - // Unselect all other contact categories. - db_update('contact') - ->fields(array('selected' => '0')) - ->execute(); + // Update new default category. + config('contact.settings') + ->set('default_category', $form_state['values']['name']) + ->save(); } - - if (empty($form_state['values']['cid'])) { - drupal_write_record('contact', $form_state['values']); + elseif ($default_category == $form_state['values']['name']) { + // Set to empty when changed to be not default category. + config('contact.settings') + ->set('default_category', '') + ->save(); } - else { - drupal_write_record('contact', $form_state['values'], array('cid')); + + $config = config('contact.category.' . $form_state['values']['name']); + // Set name only for new category. + if ($form_state['values']['is_new']) { + $config->set('name', $form_state['values']['name']); } + $config + ->set('category', $form_state['values']['category']) + ->set('recipients', $form_state['values']['recipients']) + ->set('reply', $form_state['values']['reply']) + ->set('weight', $form_state['values']['weight']) + ->save(); drupal_set_message(t('Category %category has been saved.', array('%category' => $form_state['values']['category']))); - watchdog('contact', 'Category %category has been saved.', array('%category' => $form_state['values']['category']), WATCHDOG_NOTICE, l(t('Edit'), 'admin/structure/contact/edit/' . $form_state['values']['cid'])); + watchdog('contact', 'Category %category has been saved.', array('%category' => $form_state['values']['category']), WATCHDOG_NOTICE, l(t('Edit'), 'admin/structure/contact/edit/' . $form_state['values']['name'])); $form_state['redirect'] = 'admin/structure/contact'; } @@ -215,9 +231,7 @@ function contact_category_delete_form($form, &$form_state, array $contact) { function contact_category_delete_form_submit($form, &$form_state) { $contact = $form['contact']['#value']; - db_delete('contact') - ->condition('cid', $contact['cid']) - ->execute(); + config('contact.category.' . $contact['name'])->delete(); drupal_set_message(t('Category %category has been deleted.', array('%category' => $contact['category']))); watchdog('contact', 'Category %category has been deleted.', array('%category' => $contact['category']), WATCHDOG_NOTICE); diff --git a/core/modules/contact/contact.install b/core/modules/contact/contact.install index 7ddea45..7f5fc37 100644 --- a/core/modules/contact/contact.install +++ b/core/modules/contact/contact.install @@ -6,84 +6,58 @@ */ /** - * Implements hook_schema(). + * Implements hook_install(). */ -function contact_schema() { - $schema['contact'] = array( - 'description' => 'Contact form category settings.', - 'fields' => array( - 'cid' => array( - 'type' => 'serial', - 'unsigned' => TRUE, - 'not null' => TRUE, - 'description' => 'Primary Key: Unique category ID.', - ), - 'category' => array( - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - 'default' => '', - 'description' => 'Category name.', - 'translatable' => TRUE, - ), - 'recipients' => array( - 'type' => 'text', - 'not null' => TRUE, - 'size' => 'big', - 'description' => 'Comma-separated list of recipient e-mail addresses.', - ), - 'reply' => array( - 'type' => 'text', - 'not null' => TRUE, - 'size' => 'big', - 'description' => 'Text of the auto-reply message.', - ), - 'weight' => array( - 'type' => 'int', - 'not null' => TRUE, - 'default' => 0, - 'description' => "The category's weight.", - ), - 'selected' => array( - 'type' => 'int', - 'not null' => TRUE, - 'default' => 0, - 'size' => 'tiny', - 'description' => 'Flag to indicate whether or not category is selected by default. (1 = Yes, 0 = No)', - ), - ), - 'primary key' => array('cid'), - 'unique keys' => array( - 'category' => array('category'), - ), - 'indexes' => array( - 'list' => array('weight', 'category'), - ), - ); - - return $schema; +function contact_install() { + config('contact.category.feedback') + ->set('recipients', variable_get('site_mail', ini_get('sendmail_from'))) + ->save(); } /** - * Implements hook_install(). + * @defgroup updates-7.x-to-8.x Updates from 7.x to 8.x + * @{ + * Update functions from 7.x to 8.x. */ -function contact_install() { - // Insert a default contact category. - db_insert('contact') - ->fields(array( - 'category' => 'Website feedback', - 'recipients' => variable_get('site_mail', ini_get('sendmail_from')), - 'selected' => 1, - 'reply' => '', - )) - ->execute(); -} /** - * Implements hook_uninstall(). + * Moves contact settings from variables to config. */ -function contact_uninstall() { - variable_del('contact_default_status'); - variable_del('contact_threshold_limit'); - variable_del('contact_threshold_window'); +function contact_update_8000() { + update_variables_to_config('contact.settings'); + // Migrate and drop {contact} table. + if (db_table_exists('contact')) { + $default_category = 0; + $result = db_query('SELECT * FROM {contact}'); + foreach ($result as $category) { + // Generate machine readable names. + if ($category->category == 'Website feedback') { + // Use default name for unchanged category. + $category->name = 'feedback'; + } + else { + $category->name = 'category_' . $category->cid; + } + if ($category->selected) { + // Save default category. + config('contact.settings') + ->set('default_category', $category->name) + ->save(); + } + // Save the config object. + config('contact.category.' . $category->name) + ->set('name', $category->name) + ->set('category', $category->category) + ->set('recipients', $category->recipients) + ->set('reply', $category->reply) + ->set('weight', $category->weight) + ->save(); + } + db_drop_table('contact'); + } } + +/** + * @} End of "defgroup updates-7.x-to-8.x". + * The next series of updates should start at 9000. + */ diff --git a/core/modules/contact/contact.module b/core/modules/contact/contact.module index 48c12d9..8cd07f3 100644 --- a/core/modules/contact/contact.module +++ b/core/modules/contact/contact.module @@ -149,19 +149,14 @@ function _contact_personal_tab_access($account) { /** * Loads a contact category. * - * @param $cid - * The contact category ID. + * @param $name + * The contact category name. * * @return * An array with the contact category's data. */ -function contact_load($cid) { - return db_select('contact', 'c') - ->addTag('translatable') - ->fields('c') - ->condition('cid', $cid) - ->execute() - ->fetchAssoc(); +function contact_load($name) { + return config('contact.category.' . $name)->get(); } /** @@ -234,7 +229,7 @@ function contact_form_user_profile_form_alter(&$form, &$form_state) { * Implements hook_user_presave(). */ function contact_user_presave($account) { - $account->data['contact'] = isset($account->contact) ? $account->contact : variable_get('contact_default_status', 1); + $account->data['contact'] = isset($account->contact) ? $account->contact : config('contact.settings')->get('contact_default_status'); } /** @@ -254,6 +249,60 @@ function contact_form_user_admin_settings_alter(&$form, &$form_state) { '#type' => 'checkbox', '#title' => t('Enable the personal contact form by default for new users.'), '#description' => t('Changing this setting will not affect existing users.'), - '#default_value' => variable_get('contact_default_status', 1), + '#default_value' => config('contact.settings')->get('contact_default_status'), ); + $form['#submit'][] = 'contact_form_user_admin_settings_alter_submit'; +} + +/** + * Submission handler to record our custom setting. + */ +function contact_form_user_admin_settings_alter_submit($form, $form_state) { + config('contact.settings') + ->set('contact_default_status', $form_state['values']['contact_default_status']) + ->save(); +} + +/** + * Get an array of all contact categories and their settings. + * + * @return + * An array of categories keyed by the category machine name. + * @see contact_load() + */ +function contact_categories() { + // @todo Configuration must not be statically cached nor cache-system cached. + //$categories = &drupal_static(__FUNCTION__); + $categories = array(); + + // Select the categories we have configured. + $configured_categories = config_get_storage_names_with_prefix('contact.category'); + foreach ($configured_categories as $config_name) { + // @todo Allow to retrieve the name without prefix only. + $category = contact_load(str_replace('contact.category.', '', $config_name)); + $categories[$category['name']] = $category; + } + // Sort the categories if necessary. + if (!empty($categories)) { + uasort($categories, '_contact_categories_sort'); + } + return $categories; +} + +/** + * Sorts a structured array by 'weight' and 'category' property. + * + * Callback for uasort() within contact_categories(). + * + * @param $a + * First item for comparison. The compared items should be associative arrays. + * @param $b + * Second item for comparison. + */ +function _contact_categories_sort($a, $b) { + if ($a['weight'] == $b['weight']) { + // Compare category names. + return ($a['category'] < $b['category']) ? -1 : 1; + } + return ($a['weight'] < $b['weight']) ? -1 : 1; } diff --git a/core/modules/contact/contact.pages.inc b/core/modules/contact/contact.pages.inc index bf096a7..dfd2964 100644 --- a/core/modules/contact/contact.pages.inc +++ b/core/modules/contact/contact.pages.inc @@ -17,8 +17,8 @@ function contact_site_form($form, &$form_state) { global $user; // Check if flood control has been activated for sending e-mails. - $limit = variable_get('contact_threshold_limit', 5); - $window = variable_get('contact_threshold_window', 3600); + $limit = config('contact.settings')->get('contact_threshold_limit'); + $window = config('contact.settings')->get('contact_threshold_window'); if (!flood_is_allowed('contact', $limit, $window) && !user_access('administer contact forms')) { drupal_set_message(t("You cannot send more than %limit messages in @interval. Try again later.", array('%limit' => $limit, '@interval' => format_interval($window))), 'error'); drupal_access_denied(); @@ -26,14 +26,13 @@ function contact_site_form($form, &$form_state) { } // Get an array of the categories and the current default category. - $categories = db_select('contact', 'c') - ->addTag('translatable') - ->fields('c', array('cid', 'category')) - ->orderBy('weight') - ->orderBy('category') - ->execute() - ->fetchAllKeyed(); - $default_category = db_query("SELECT cid FROM {contact} WHERE selected = 1")->fetchField(); + $categories_configured = contact_categories(); + // Prepare array for select options. + $categories = array(); + foreach ($categories_configured as $name => $category) { + $categories[$name] = $category['category']; + } + $default_category = config('contact.settings')->get('default_category'); // If there are no categories, do not display the form. if (!$categories) { @@ -82,7 +81,7 @@ function contact_site_form($form, &$form_state) { '#maxlength' => 255, '#required' => TRUE, ); - $form['cid'] = array( + $form['category_name'] = array( '#type' => 'select', '#title' => t('Category'), '#default_value' => $default_category, @@ -117,8 +116,8 @@ function contact_site_form($form, &$form_state) { * @see contact_site_form_submit() */ function contact_site_form_validate($form, &$form_state) { - if (!$form_state['values']['cid']) { - form_set_error('cid', t('You must select a valid category.')); + if (!$form_state['values']['category_name']) { + form_set_error('category_name', t('You must select a valid category.')); } } @@ -134,7 +133,7 @@ function contact_site_form_submit($form, &$form_state) { $values['sender'] = $user; $values['sender']->name = $values['name']; $values['sender']->mail = $values['mail']; - $values['category'] = contact_load($values['cid']); + $values['category'] = contact_load($values['category_name']); // Save the anonymous user information to a cookie for reuse. if (!$user->uid) { @@ -158,7 +157,7 @@ function contact_site_form_submit($form, &$form_state) { drupal_mail('contact', 'page_autoreply', $from, $language_interface, $values, $to); } - flood_register_event('contact', variable_get('contact_threshold_window', 3600)); + flood_register_event('contact', config('contact.settings')->get('contact_threshold_window')); watchdog('mail', '%sender-name (@sender-from) sent an e-mail regarding %category.', array('%sender-name' => $values['name'], '@sender-from' => $from, '%category' => $values['category']['category'])); // Jump to home page rather than back to contact page to avoid @@ -179,8 +178,8 @@ function contact_personal_form($form, &$form_state, $recipient) { global $user; // Check if flood control has been activated for sending e-mails. - $limit = variable_get('contact_threshold_limit', 5); - $window = variable_get('contact_threshold_window', 3600); + $limit = config('contact.settings')->get('contact_threshold_limit'); + $window = config('contact.settings')->get('contact_threshold_window'); if (!flood_is_allowed('contact', $limit, $window) && !user_access('administer contact forms') && !user_access('administer users')) { drupal_set_message(t("You cannot send more than %limit messages in @interval. Try again later.", array('%limit' => $limit, '@interval' => format_interval($window))), 'error'); drupal_access_denied(); @@ -274,7 +273,7 @@ function contact_personal_form_submit($form, &$form_state) { drupal_mail('contact', 'user_copy', $from, $language_interface, $values, $from); } - flood_register_event('contact', variable_get('contact_threshold_window', 3600)); + flood_register_event('contact', config('contact.settings')->get('contact_threshold_window')); watchdog('mail', '%sender-name (@sender-from) sent %recipient-name an e-mail.', array('%sender-name' => $values['name'], '@sender-from' => $from, '%recipient-name' => $values['recipient']->name)); // Jump to the contacted user's profile page. diff --git a/core/modules/contact/contact.test b/core/modules/contact/contact.test index 3c7f61a..984a7e8 100644 --- a/core/modules/contact/contact.test +++ b/core/modules/contact/contact.test @@ -31,8 +31,10 @@ class ContactSitewideTestCase extends WebTestBase { $this->drupalLogin($admin_user); $flood_limit = 3; - variable_set('contact_threshold_limit', $flood_limit); - variable_set('contact_threshold_window', 600); + config('contact.settings') + ->set('contact_threshold_limit', $flood_limit) + ->set('contact_threshold_window', 600) + ->save(); // Set settings. $edit = array(); @@ -57,18 +59,18 @@ class ContactSitewideTestCase extends WebTestBase { // 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->addCategory($this->randomName(16), $this->randomName(16), $invalid_recipient, '', FALSE); $this->assertRaw(t('%recipient is an invalid e-mail address.', array('%recipient' => $invalid_recipient)), t('Caught invalid recipient (' . $invalid_recipient . ').')); } // Test validation of empty category and recipients fields. - $this->addCategory($category = '', '', '', TRUE); + $this->addCategory($category = '', '', '', '', TRUE); $this->assertText(t('Category field is required.'), t('Caught empty category field')); $this->assertText(t('Recipients field is required.'), t('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->addCategory($category = $this->randomName(16), drupal_strtolower($this->randomName(16)), implode(',', array($recipients[0])), '', TRUE); $this->assertRaw(t('Category %category has been saved.', array('%category' => $category)), t('Category successfully saved.')); // Make sure the newly created category is included in the list of categories. @@ -76,12 +78,12 @@ class ContactSitewideTestCase extends WebTestBase { // Test update contact form category. $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(); + $category_name = $this->updateCategory($categories, $category = $this->randomName(16), drupal_strtolower($this->randomName(16)), $recipients_str = implode(',', array($recipients[0], $recipients[1])), $reply = $this->randomName(30), FALSE); + $category_array = config('contact.category.' . $category_name)->get(); $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->assertNotEqual($category_name, config('contact.settings')->get('default_category')); $this->assertRaw(t('Category %category has been saved.', array('%category' => $category)), t('Category successfully saved.')); // Ensure that the contact form is shown without a category selection input. @@ -93,16 +95,16 @@ class ContactSitewideTestCase extends WebTestBase { $this->drupalLogin($admin_user); // Add more categories. - $this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1])), '', FALSE); + $this->addCategory($category = $this->randomName(16), drupal_strtolower($this->randomName(16)), implode(',', array($recipients[0], $recipients[1])), '', FALSE); $this->assertRaw(t('Category %category has been saved.', array('%category' => $category)), t('Category successfully saved.')); - $this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1], $recipients[2])), '', FALSE); + $this->addCategory($category = $this->randomName(16), $name = drupal_strtolower($this->randomName(16)), implode(',', array($recipients[0], $recipients[1], $recipients[2])), '', FALSE); $this->assertRaw(t('Category %category has been saved.', array('%category' => $category)), t('Category successfully saved.')); // Try adding a category that already exists. - $this->addCategory($category, '', '', FALSE); + $this->addCategory($category, $name, '', '', FALSE); $this->assertNoRaw(t('Category %category has been saved.', array('%category' => $category)), t('Category not saved.')); - $this->assertRaw(t('A contact form with category %category already exists.', array('%category' => $category)), t('Duplicate category error found.')); + $this->assertRaw(t('A contact form with name %category already exists.', array('%category' => $name)), t('Duplicate category error found.')); // Clear flood table in preparation for flood test and allow other checks to complete. db_delete('flood')->execute(); @@ -137,9 +139,9 @@ class ContactSitewideTestCase extends WebTestBase { $this->assertText(t('Message field is required.'), t('Message required.')); // Test contact form with no default category selected. - db_update('contact') - ->fields(array('selected' => 0)) - ->execute(); + config('contact.settings') + ->set('default_category', '') + ->save(); $this->drupalGet('contact'); $this->assertRaw(t('- Please choose -'), t('Without selected categories the visitor is asked to chose a category.')); @@ -155,7 +157,7 @@ class ContactSitewideTestCase extends WebTestBase { // Submit contact form one over limit. $this->drupalGet('contact'); $this->assertResponse(403, t('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))), t('Message threshold reached.')); + $this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => config('contact.settings')->get('contact_threshold_limit'), '@interval' => format_interval(600))), t('Message threshold reached.')); // Delete created categories. $this->drupalLogin($admin_user); @@ -173,14 +175,14 @@ class ContactSitewideTestCase extends WebTestBase { // 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@example.com', $foo_autoreply, FALSE); - $this->addCategory('bar', 'bar@example.com', $bar_autoreply, FALSE); - $this->addCategory('no_autoreply', 'bar@example.com', '', FALSE); + $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); // Test the auto-reply for category 'foo'. $email = $this->randomName(32) . '@example.com'; $subject = $this->randomName(64); - $this->submitContact($this->randomName(16), $email, $subject, 2, $this->randomString(128)); + $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, 'from' => 'foo@example.com')); @@ -189,7 +191,7 @@ class ContactSitewideTestCase extends WebTestBase { // Test the auto-reply for category 'bar'. $email = $this->randomName(32) . '@example.com'; - $this->submitContact($this->randomName(16), $email, $this->randomString(64), 3, $this->randomString(128)); + $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, 'from' => 'bar@example.com')); @@ -198,7 +200,7 @@ class ContactSitewideTestCase extends WebTestBase { // 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), 4, $this->randomString(128)); + $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, 'from' => 'no_autoreply@example.com')); $this->assertEqual(count($captured_emails), 0, t('No auto-reply e-mail was sent to the sender for category "no-autoreply".'), t('Contact')); } @@ -207,7 +209,9 @@ class ContactSitewideTestCase extends WebTestBase { * Adds a category. * * @param string $category - * The category name. + * The category title. + * @param string $name + * The category machine name. * @param string $recipients * The list of recipient e-mail addresses. * @param string $reply @@ -216,9 +220,10 @@ class ContactSitewideTestCase extends WebTestBase { * @param boolean $selected * Boolean indicating whether the category should be selected by default. */ - function addCategory($category, $recipients, $reply, $selected) { + function addCategory($category, $name, $recipients, $reply, $selected) { $edit = array(); $edit['category'] = $category; + $edit['name'] = $name; $edit['recipients'] = $recipients; $edit['reply'] = $reply; $edit['selected'] = ($selected ? TRUE : FALSE); @@ -228,7 +233,10 @@ class ContactSitewideTestCase extends WebTestBase { /** * Updates a category. * + * @param array $categories * @param string $category + * The category title. + * @param string $name * The category name. * @param string $recipients * The list of recipient e-mail addresses. @@ -238,7 +246,7 @@ class ContactSitewideTestCase extends WebTestBase { * @param boolean $selected * Boolean indicating whether the category should be selected by default. */ - function updateCategory($categories, $category, $recipients, $reply, $selected) { + function updateCategory($categories, $category, $name, $recipients, $reply, $selected) { $category_id = $categories[array_rand($categories)]; $edit = array(); $edit['category'] = $category; @@ -258,17 +266,17 @@ class ContactSitewideTestCase extends WebTestBase { * The e-mail address of the sender. * @param string $subject * The subject of the message. - * @param integer $cid - * The category ID of the message. + * @param string $category_name + * The category name of the message. * @param string $message * The message body. */ - function submitContact($name, $mail, $subject, $cid, $message) { + function submitContact($name, $mail, $subject, $category_name, $message) { $edit = array(); $edit['name'] = $name; $edit['mail'] = $mail; $edit['subject'] = $subject; - $edit['cid'] = $cid; + $edit['category_name'] = $category_name; $edit['message'] = $message; $this->drupalPost('contact', $edit, t('Send message')); } @@ -278,21 +286,26 @@ class ContactSitewideTestCase extends WebTestBase { */ 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)), t('Category deleted successfully.')); + foreach ($categories as $category_name) { + $category = config('contact.category.' . $category_name)->get('category'); + $this->drupalPost('admin/structure/contact/delete/' . $category_name, array(), t('Delete')); + $this->assertRaw(t('Category %category has been deleted.', array('%category' => $category)), t('Category deleted successfully.')); } } /** - * Gets a list of all category IDs. + * Gets a list of all category names. * * @return array - * A list of the category IDs. + * A list of the category names. */ function getCategories() { - $categories = db_query('SELECT cid FROM {contact}')->fetchCol(); + $categories_names = config_get_storage_names_with_prefix('contact.category'); + $categories = array(); + foreach ($categories_names as $name) { + $categories[] = str_replace('contact.category.', '', $name); + } + return $categories; } } @@ -394,7 +407,9 @@ class ContactPersonalTestCase extends WebTestBase { */ function testPersonalContactFlood() { $flood_limit = 3; - variable_set('contact_threshold_limit', $flood_limit); + config('contact.settings') + ->set('contact_threshold_limit', $flood_limit) + ->save(); // Clear flood table in preparation for flood test and allow other checks to complete. db_delete('flood')->execute(); @@ -411,7 +426,7 @@ class ContactPersonalTestCase extends WebTestBase { // Submit contact form one over limit. $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->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => $flood_limit, '@interval' => format_interval(config('contact.settings')->get('contact_threshold_window')))), 'Normal user denied access to flooded contact form.'); // Test that the admin user can still access the contact form even though // the flood limit was reached.