GHOP #122: Write Simpletest tests for the Forum module

pwolanin - December 26, 2007 - 04:05
Project:SimpleTest
Version:6.x-1.x-dev
Component:Code
Category:task
Priority:normal
Assigned:Unassigned
Status:closed
Description

The main work for this task is to write a small suite of tests to check the basic functionality of the Forum module.

Suggested tests:

1. admin/content/forum/add/container Add a forum container.
2. admin/content/forum/add/forum Add a forum.
3. admin/content/taxonomy/edit/vocabulary/# Edit the forum vocabulary.
4. node/add/forum Create a new forum topic.
5. node/add/forum Attempt to add a forum topic to a container, rather than a forum (this should not work).
6. node/#/edit Move a forum topic from one forum to another.
7. node/#/edit Move a forum topic from one forum to another, leaving a shadow copy (confirm existence of shadow copy).

#1

pwolanin - December 26, 2007 - 04:09
Title:GHOP : Write Simpletest tests for the Forum module» GHOP #122: Write Simpletest tests for the Forum module

GHOP issue: http://code.google.com/p/google-highly-open-participation-drupal/issues/...

#2

Smartys - December 27, 2007 - 02:56
Status:active» patch (code needs review)

OK, I'm attaching the code for the tests.
One note: there is a lot of repeated code for, for example, submitting a form and creating forums/containers. The reason is that 4 of the tests require a forum to work, 2 others require a container, and I couldn't find a function that would create the forum/container for me. If I missed the function, I'll be more than happy to take some of the repetition out of my code. :)

AttachmentSize
forum_module.test.txt19.6 KB

#3

pwolanin - December 27, 2007 - 03:43
Status:patch (code needs review)» patch (code needs work)

Ok - looks like a good start. However - the 7 tests don't actually have to be separate DrupalTestCase objects - you can have all of this together in one "test" as long as these 7 pieces of functionality are tested. Or group them in 2 or 3 tests as seems most logical.

Also, if you still have repeated code, you could play the trick I used here: http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/simpletest/...

The latter tests extend the first object which defines common method in addition to those already in DrupalTestCase.

#4

Smartys - December 27, 2007 - 03:57

Ooh, interesting, thanks a lot :)

#5

Smartys - December 27, 2007 - 13:40
Status:patch (code needs work)» patch (code needs review)

OK, I've reduced it down to 3 classes, the last of which extends the first. The size has been cut quite a bit as a result. :)

AttachmentSize
forum_module.test.txt13.38 KB

#6

pwolanin - December 27, 2007 - 13:59
Status:patch (code needs review)» patch (code needs work)

ok - looking better.

A couple minor suggestions to start-

I'd rename AddForumTest to something like DrupalForumTestCase.

This should have no "testX" methods, since I think they will get run twice if a later object extends it. It should only have common methods like your function createForum().

Also, the second object should extend DrupalForumTestCase also, this way you can omit:

    // Enable the forum module and its dependencies
    $this->drupalModuleEnable('taxonomy');
    $this->drupalModuleEnable('comment');
    $this->drupalModuleEnable('forum');

since the setup function will run.

Finally - minor code style issue - don't have any white space between the function name and parens in the function definition.

#7

Smartys - December 27, 2007 - 14:12
Status:patch (code needs work)» patch (code needs review)

I actually don't think the methods would be run twice unless you were to redeclare the function in the subclass and call the parent function in there. Otherwise, simpletest would be showing duplicate tests for you to choose from, which it doesn't.
In any case, here's the code with the changes made. The one thing to note here is that the new class, DrupalForumTestCase, needed a get_info function even though there are no tests in it. So, I just clarified what it is a little bit.

AttachmentSize
forum_module.test.txt13.54 KB

#8

pwolanin - December 27, 2007 - 14:20

ok- obviously I'm not paying attention. You're right about the tests not running twice. Sorry.

How about merging the first two objects together so there is no get_info() that doesn't run an actual test?

#9

Smartys - December 27, 2007 - 14:37

I actually think we're wrong. I just made the changes and suddenly my number of passes jumped. Looking at the list of passes, it seems like the first two tests (the ones in the class that is extended) are being run in every class.

#10

pwolanin - December 27, 2007 - 15:48

testing this now - it's weird with the menu tests, they only run once, but if I add a trivial additional test it runs multiple times.

  function testFoo() {
    $this->assertFalse(1, 'foo');
  }

#11

pwolanin - December 27, 2007 - 16:43

oh wait - I see - I used the same function name in both classes in menu_module.test. I'm too clever for myself...

#12

pwolanin - December 27, 2007 - 17:05
Status:patch (code needs review)» patch (code needs work)

Ok, a minor problem with the tests for the expected text like:

$this->assertWantedRaw('Created new forum container <em>'. $title .'</em>.', t('New forum container has been created'));

This will break if localization is turned on. You need to write it with t() mimicking the exact form from forum module like from modules/forum/forum.admin.inc line 90:

drupal_set_message(t('Created new @type %term.', array('%term' => $form_state['values']['name'], '@type' => $type)));

So your code needs to be like:

$type = t('forum container');
$this->assertWantedRaw(t('Created new @type %term.', array('%term' => $title '@type' => $type)), t('New forum container has been created'));

There are several spots with this problem.

#13

Smartys - December 27, 2007 - 22:11
Status:patch (code needs work)» patch (code needs review)

Alright, thanks :)
Here's a corrected version

AttachmentSize
forum_module.test.txt14.08 KB

#14

Smartys - December 27, 2007 - 22:24

And a new copy to correct a small capitalization error pwolanin found.

AttachmentSize
forum_module.test.txt14.08 KB

#15

pwolanin - December 28, 2007 - 20:01

Looking better - all tests pass.

#16

pwolanin - December 28, 2007 - 20:18
Status:patch (code needs review)» patch (code needs work)

some code style issues still - for example an extra space in array declarations like: array () which should be array()

as before (http://drupal.org/node/201134#comment-669753) try running coder on this.

also, some code could be made more compact, like this:

<?php
   
// Loop through to find the newly inserted container
   
$result = db_query(db_rewrite_sql('SELECT t.tid, t.vid, t.name, t.description, t.weight FROM {term_data} t WHERE t.vid = %d', 't', 'tid'), variable_get('forum_nav_vocabulary', ''));

   
$term = array();
    while (
$cur_term = db_fetch_array($result)) {
      if (
$cur_term['name'] == $title && $cur_term['description'] == $description) {
       
$term = $cur_term;
        break;
      }
    }
?>

could be rewritten as something much simpler like:

<?php
$term
= db_fetch_array(db_query("SELECT * FROM {term_data} t WHERE t.vid = %d' AND t.name = '%s' AND t.description = '%s'", variable_get('forum_nav_vocabulary', ''), $title, $description);
?>

There's probably no reason to use db_rewrite_sql for simpletests.

There are a couple cases of this.

Also - you generally want to write all query strings with double quotes ( " not ' ) since you need to wrap single qoues around %s placeholders.

#17

Smartys - December 28, 2007 - 21:14
Status:patch (code needs work)» patch (code needs review)

The coder module seems to not like me, it still says the code looks perfectly fine. :/

I've corrected the parenthesis spacing and made those queries more compact.

AttachmentSize
forum_module.test.txt13.61 KB

#18

pwolanin - December 28, 2007 - 21:22

looks better- though a couple code comments like:

// Loop through to find the newly edited vocabulary

that should be changed since there is no looping. Maybe just:

// Find the newly edited vocabulary

#19

Smartys - December 28, 2007 - 21:33

Whoops, I KNEW there was something I forgot to change :P
I changed it for one of the loops and then didn't for the others. New version, coming right up:

AttachmentSize
forum_module.test.txt13.58 KB

#20

pwolanin - December 29, 2007 - 20:49

ok - looks very close. A suggestion after reviewing the code again.

This part:

    // Restore the original settings, and use it as an opportunity to test the form again
    $edit = array(
      'name' => $original_settings->name,
      'description' => $original_settings->description,
      'help' => $original_settings->help,
      'weight' => $original_settings->weight
    );

    // Double check that the page says it has edited the vocabulary
    $this->drupalPostRequest('admin/content/taxonomy/edit/vocabulary/'. $vid, $edit, 'Save');
    $this->assertWantedRaw(t('Updated vocabulary %name.', array('%name' => $original_settings->name)), t('Vocabulary has been edited'));

could probably be simplified to a single API call like:

$original_settings = (array) $original_settings;
taxonomy_save_vocabulary($original_settings);

rather than re-testing the thing we tested before. It doesn't matter much, but as it is now will give a redundant failure message.

#21

Smartys - December 29, 2007 - 22:28

Fair enough :)

New version with the change

AttachmentSize
forum_module.test.txt12.77 KB

#22

pwolanin - December 30, 2007 - 02:16
Status:patch (code needs review)» patch (reviewed & tested by the community)

I don't see any other problems - good job.

#23

Rok Žlender - December 31, 2007 - 08:57
Status:patch (reviewed & tested by the community)» fixed

Thanks for you great work Smartys and pwolanin. Committed.

#24

Anonymous - January 14, 2008 - 09:14
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.