When trying to add a category for the contact form using the following page:
The page: http://localhost/drupal/admin/build/contact/add

Using a category name that already exists, the following error received:
PHP Fatal error: Cannot use object of type stdClass as array in /var/www/drupal/modules/contact/contact.admin.inc on line 114

Running on Debian, Apache/2.2.8, PHP 5.2.5-3, Drupal 6.2

On another server with the same apache/php version, the error appear even when using a new category name.

Both Drupals are pretty clean installations without any external module installed.

Comments

mr.baileys’s picture

Version: 6.2 » 7.x-dev
Assigned: Unassigned » mr.baileys
Status: Active » Needs review
StatusFileSize
new1.35 KB

Also happens on 7.x-dev.

Steps to reproduce:

  1. Enable contact module
  2. Add a contact form category (Administer > Site building > Contact Form and click on the "Add Category".)
  3. Add another contact form category with the same category name

Patch attached adds additional validation on the category field, making sure the category does not already exist.

Status: Needs review » Needs work

The last submitted patch failed testing.

mr.baileys’s picture

Status: Needs work » Needs review

Possible testbot failure, requesting re-test.

mr.baileys’s picture

Title: error when trying to add new category to contact form module » Error when adding duplicate category to contact form module.
pp’s picture

Status: Needs review » Reviewed & tested by the community

Patch is good, and resolve the problem.

catch’s picture

Status: Reviewed & tested by the community » Needs work

db_result() is deprecated, you can use db_query()->fetchField() instead. The conditions probably don't need wrapping in extra parentheses. Also we usually use double quotes for strings with single quotes in, rather than \' since it's easier for translators to deal with and more readable generally.

Other than that, looks good.

Is there a unique key on category name? We could consider adding one as well to enforce it there as well.

dave reid’s picture

Assigned: mr.baileys » dave reid

Assigning myself to reroll this patch with a test.

mr.baileys’s picture

Assigned: dave reid » Unassigned
Status: Needs work » Needs review
StatusFileSize
new2.45 KB

Re-rolled with DB:TNG and a simple test case.

dave reid’s picture

Status: Needs review » Needs work
+++ modules/contact/contact.admin.inc	31 Jan 2010 16:10:16 -0000
@@ -112,6 +112,18 @@
+    form_set_error('category', t('A contact form with category \'@category\' already exists.', array('@category' => $category)));

Let's just use %category instead of '@category'.

+++ modules/contact/contact.test	31 Jan 2010 16:10:17 -0000
@@ -93,6 +93,11 @@
+    $this->addCategory($category, implode(',', array($recipients[0], $recipients[1], $recipients[2])), '', FALSE);

Instead of adding e-mail recipients, let's just leave that parameter as an empty string.

+++ modules/contact/contact.test	31 Jan 2010 16:10:17 -0000
@@ -93,6 +93,11 @@
+    $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.'));    

Use assertText and assertNoText. We only use assertRaw when we need to. I know we're using it elsewhere in the contact test but I'd like to fix that eventually.

Powered by Dreditor.

mr.baileys’s picture

Status: Needs work » Needs review
StatusFileSize
new2.45 KB

Thanks for the review, new patch attached.

Use assertText and assertNoText. We only use assertRaw when we need to. I know we're using it elsewhere in the contact test but I'd like to fix that eventually.

I think in this case they are needed since assertText/assertNoText looks for stripped html, causing the assertion to fail on the <em>s in '%category'.

dave reid’s picture

Status: Needs review » Reviewed & tested by the community

I'm 100% sure assertText ignores HTML, but the tests are good anyway. I just prefer to use assertText since its simpler. :) Patch looks good and tested locally.

I also think we should always add 'Would you like to edit the existing [foo]?' whenever we get duplicate messages like this, but its something to work on in D8, not now.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed to HEAD. Thanks!

I agree assertText() seems better too, but the surrounding ones use assertRaw() so this is consistent. Could file a follow-up patch about that though.

mr.baileys’s picture

Status: Fixed » Needs review
StatusFileSize
new1.13 KB

Well, I agree that assertText looks better and makes more sense, but I'm also 100%99% sure that it won't work with the because of the <em>s :)

From the assertions page:

Asserts that the text in $text appears in the content of the current page in the SimpleTest browser (html-stripped).

Let's run it through the testbot to be sure.

Status: Needs review » Needs work

The last submitted patch, contact-assert-text.patch, failed testing.

mr.baileys’s picture

Status: Needs work » Fixed

setting back to fixed since we can't replace the assertRaw with assertText.

dave reid’s picture

The problem is you need to provide assertText() with actual non-HTML, so instead of
$this->assertText(t('A contact form with category %category already exists.', array('%category' => $category))

You'd do:
$this->assertText(t('A contact form with category @category already exists.', array('@category' => $category))

webchick’s picture

Eh, I'd say leave it for now. If we want to go through contact.test whole-sale and clean-up the assertRaw -> assertText in another issue, then cool. But this isn't the only instance in the file, at least from my quick check.

dave reid’s picture

@webchick: Oh agreed. It would be a follow-up if anything, but it's not important enough for me to work on it. :)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.