From 8151b56c28de3f0ae5b4f67b7666d6457721ac4f Mon Sep 17 00:00:00 2001 From: Angie Byron Date: Wed, 17 Feb 2010 03:59:43 +0000 Subject: #246880 by mr.baileys: Fixed Error when adding duplicate category to contact form module (with tests). --- modules/contact/contact.admin.inc | 12 ++++++++++++ modules/contact/contact.test | 5 +++++ 2 files changed, 17 insertions(+) (limited to 'modules/contact') diff --git a/modules/contact/contact.admin.inc b/modules/contact/contact.admin.inc index 585d5f8a0..6677962a5 100644 --- a/modules/contact/contact.admin.inc +++ b/modules/contact/contact.admin.inc @@ -112,6 +112,18 @@ function contact_category_edit_form($form, &$form_state, array $category = array 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))); + } + foreach ($recipients as &$recipient) { $recipient = trim($recipient); if (!valid_email_address($recipient)) { diff --git a/modules/contact/contact.test b/modules/contact/contact.test index 02adc9709..b54dbe3d0 100644 --- a/modules/contact/contact.test +++ b/modules/contact/contact.test @@ -94,6 +94,11 @@ class ContactSitewideTestCase extends DrupalWebTestCase { $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)), t('Category successfully saved.')); + // Try adding a category that already exists. + $this->addCategory($category, '', '', 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.')); + // 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(); -- cgit v1.2.3