cms/drupal/modules/forum/forum.test
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Tests for forum.module.
       
     6  */
       
     7 
       
     8 /**
       
     9  * Provides automated tests for the Forum module.
       
    10  */
       
    11 class ForumTestCase extends DrupalWebTestCase {
       
    12 
       
    13   /**
       
    14    * A user with various administrative privileges.
       
    15    */
       
    16   protected $admin_user;
       
    17 
       
    18   /**
       
    19    * A user that can create forum topics and edit its own topics.
       
    20    */
       
    21   protected $edit_own_topics_user;
       
    22 
       
    23   /**
       
    24    * A user that can create, edit, and delete forum topics.
       
    25    */
       
    26   protected $edit_any_topics_user;
       
    27 
       
    28   /**
       
    29    * A user with no special privileges.
       
    30    */
       
    31   protected $web_user;
       
    32 
       
    33   /**
       
    34    * An array representing a container.
       
    35    */
       
    36   protected $container;
       
    37 
       
    38   /**
       
    39    * An array representing a forum.
       
    40    */
       
    41   protected $forum;
       
    42 
       
    43   /**
       
    44    * An array representing a root forum.
       
    45    */
       
    46   protected $root_forum;
       
    47 
       
    48   /**
       
    49    * An array of forum topic node IDs.
       
    50    */
       
    51   protected $nids;
       
    52 
       
    53   public static function getInfo() {
       
    54     return array(
       
    55       'name' => 'Forum functionality',
       
    56       'description' => 'Create, view, edit, delete, and change forum entries and verify its consistency in the database.',
       
    57       'group' => 'Forum',
       
    58     );
       
    59   }
       
    60 
       
    61   function setUp() {
       
    62     parent::setUp('taxonomy', 'comment', 'forum');
       
    63     // Create users.
       
    64     $this->admin_user = $this->drupalCreateUser(array(
       
    65       'access administration pages',
       
    66       'administer modules',
       
    67       'administer blocks',
       
    68       'administer forums',
       
    69       'administer menu',
       
    70       'administer taxonomy',
       
    71       'create forum content',
       
    72     ));
       
    73     $this->edit_any_topics_user = $this->drupalCreateUser(array(
       
    74       'access administration pages',
       
    75       'create forum content',
       
    76       'edit any forum content',
       
    77       'delete any forum content',
       
    78     ));
       
    79     $this->edit_own_topics_user = $this->drupalCreateUser(array(
       
    80       'create forum content',
       
    81       'edit own forum content',
       
    82       'delete own forum content',
       
    83     ));
       
    84     $this->web_user = $this->drupalCreateUser(array());
       
    85   }
       
    86 
       
    87   /**
       
    88    * Tests disabling and re-enabling the Forum module.
       
    89    */
       
    90   function testEnableForumField() {
       
    91     $this->drupalLogin($this->admin_user);
       
    92 
       
    93     // Disable the Forum module.
       
    94     $edit = array();
       
    95     $edit['modules[Core][forum][enable]'] = FALSE;
       
    96     $this->drupalPost('admin/modules', $edit, t('Save configuration'));
       
    97     $this->assertText(t('The configuration options have been saved.'), 'Modules status has been updated.');
       
    98     module_list(TRUE);
       
    99     $this->assertFalse(module_exists('forum'), 'Forum module is not enabled.');
       
   100 
       
   101     // Attempt to re-enable the Forum module and ensure it does not try to
       
   102     // recreate the taxonomy_forums field.
       
   103     $edit = array();
       
   104     $edit['modules[Core][forum][enable]'] = 'forum';
       
   105     $this->drupalPost('admin/modules', $edit, t('Save configuration'));
       
   106     $this->assertText(t('The configuration options have been saved.'), 'Modules status has been updated.');
       
   107     module_list(TRUE);
       
   108     $this->assertTrue(module_exists('forum'), 'Forum module is enabled.');
       
   109   }
       
   110 
       
   111   /**
       
   112    * Tests forum functionality through the admin and user interfaces.
       
   113    */
       
   114   function testForum() {
       
   115     //Check that the basic forum install creates a default forum topic
       
   116     $this->drupalGet("/forum");
       
   117     // Look for the "General discussion" default forum
       
   118     $this->assertText(t("General discussion"), "Found the default forum at the /forum listing");
       
   119 
       
   120     // Do the admin tests.
       
   121     $this->doAdminTests($this->admin_user);
       
   122     // Generate topics to populate the active forum block.
       
   123     $this->generateForumTopics($this->forum);
       
   124 
       
   125     // Login an unprivileged user to view the forum topics and generate an
       
   126     // active forum topics list.
       
   127     $this->drupalLogin($this->web_user);
       
   128     // Verify that this user is shown a message that they may not post content.
       
   129     $this->drupalGet('forum/' . $this->forum['tid']);
       
   130     $this->assertText(t('You are not allowed to post new content in the forum'), "Authenticated user without permission to post forum content is shown message in local tasks to that effect.");
       
   131 
       
   132     $this->viewForumTopics($this->nids);
       
   133 
       
   134     // Log in, and do basic tests for a user with permission to edit any forum
       
   135     // content.
       
   136     $this->doBasicTests($this->edit_any_topics_user, TRUE);
       
   137     // Create a forum node authored by this user.
       
   138     $any_topics_user_node = $this->createForumTopic($this->forum, FALSE);
       
   139 
       
   140     // Log in, and do basic tests for a user with permission to edit only its
       
   141     // own forum content.
       
   142     $this->doBasicTests($this->edit_own_topics_user, FALSE);
       
   143     // Create a forum node authored by this user.
       
   144     $own_topics_user_node = $this->createForumTopic($this->forum, FALSE);
       
   145     // Verify that this user cannot edit forum content authored by another user.
       
   146     $this->verifyForums($this->edit_any_topics_user, $any_topics_user_node, FALSE, 403);
       
   147 
       
   148     // Verify that this user is shown a local task to add new forum content.
       
   149     $this->drupalGet('forum');
       
   150     $this->assertLink(t('Add new Forum topic'));
       
   151     $this->drupalGet('forum/' . $this->forum['tid']);
       
   152     $this->assertLink(t('Add new Forum topic'));
       
   153 
       
   154     // Login a user with permission to edit any forum content.
       
   155     $this->drupalLogin($this->edit_any_topics_user);
       
   156     // Verify that this user can edit forum content authored by another user.
       
   157     $this->verifyForums($this->edit_own_topics_user, $own_topics_user_node, TRUE);
       
   158 
       
   159     // Verify the topic and post counts on the forum page.
       
   160     $this->drupalGet('forum');
       
   161 
       
   162     // Verify row for testing forum.
       
   163     $forum_arg = array(':forum' => 'forum-list-' . $this->forum['tid']);
       
   164 
       
   165     // Topics cell contains number of topics and number of unread topics.
       
   166     $xpath = $this->buildXPathQuery('//tr[@id=:forum]//td[@class="topics"]', $forum_arg);
       
   167     $topics = $this->xpath($xpath);
       
   168     $topics = trim($topics[0]);
       
   169     $this->assertEqual($topics, '6', 'Number of topics found.');
       
   170 
       
   171     // Verify the number of unread topics.
       
   172     $unread_topics = _forum_topics_unread($this->forum['tid'], $this->edit_any_topics_user->uid);
       
   173     $unread_topics = format_plural($unread_topics, '1 new', '@count new');
       
   174     $xpath = $this->buildXPathQuery('//tr[@id=:forum]//td[@class="topics"]//a', $forum_arg);
       
   175     $this->assertFieldByXPath($xpath, $unread_topics, 'Number of unread topics found.');
       
   176 
       
   177     // Verify total number of posts in forum.
       
   178     $xpath = $this->buildXPathQuery('//tr[@id=:forum]//td[@class="posts"]', $forum_arg);
       
   179     $this->assertFieldByXPath($xpath, '6', 'Number of posts found.');
       
   180 
       
   181     // Test loading multiple forum nodes on the front page.
       
   182     $this->drupalLogin($this->drupalCreateUser(array('administer content types', 'create forum content')));
       
   183     $this->drupalPost('admin/structure/types/manage/forum', array('node_options[promote]' => 'promote'), t('Save content type'));
       
   184     $this->createForumTopic($this->forum, FALSE);
       
   185     $this->createForumTopic($this->forum, FALSE);
       
   186     $this->drupalGet('node');
       
   187 
       
   188     // Test adding a comment to a forum topic.
       
   189     $node = $this->createForumTopic($this->forum, FALSE);
       
   190     $edit = array();
       
   191     $edit['comment_body[' . LANGUAGE_NONE . '][0][value]'] = $this->randomName();
       
   192     $this->drupalPost("node/$node->nid", $edit, t('Save'));
       
   193     $this->assertResponse(200);
       
   194 
       
   195     // Test editing a forum topic that has a comment.
       
   196     $this->drupalLogin($this->edit_any_topics_user);
       
   197     $this->drupalGet('forum/' . $this->forum['tid']);
       
   198     $this->drupalPost("node/$node->nid/edit", array(), t('Save'));
       
   199     $this->assertResponse(200);
       
   200 
       
   201     // Make sure constructing a forum node programmatically produces no notices.
       
   202     $node = new stdClass;
       
   203     $node->type = 'forum';
       
   204     $node->title = 'Test forum notices';
       
   205     $node->uid = 1;
       
   206     $node->taxonomy_forums[LANGUAGE_NONE][0]['tid'] = $this->root_forum['tid'];
       
   207     node_save($node);
       
   208   }
       
   209 
       
   210   /**
       
   211    * Tests that forum nodes can't be added without a parent.
       
   212    *
       
   213    * Verifies that forum nodes are not created without choosing "forum" from the
       
   214    * select list.
       
   215    */
       
   216   function testAddOrphanTopic() {
       
   217     // Must remove forum topics to test creating orphan topics.
       
   218     $vid = variable_get('forum_nav_vocabulary');
       
   219     $tree = taxonomy_get_tree($vid);
       
   220     foreach ($tree as $term) {
       
   221       taxonomy_term_delete($term->tid);
       
   222     }
       
   223 
       
   224     // Create an orphan forum item.
       
   225     $this->drupalLogin($this->admin_user);
       
   226     $this->drupalPost('node/add/forum', array('title' => $this->randomName(10), 'body[' . LANGUAGE_NONE .'][0][value]' => $this->randomName(120)), t('Save'));
       
   227 
       
   228     $nid_count = db_query('SELECT COUNT(nid) FROM {node}')->fetchField();
       
   229     $this->assertEqual(0, $nid_count, 'A forum node was not created when missing a forum vocabulary.');
       
   230 
       
   231     // Reset the defaults for future tests.
       
   232     module_enable(array('forum'));
       
   233   }
       
   234 
       
   235   /**
       
   236    * Runs admin tests on the admin user.
       
   237    *
       
   238    * @param object $user
       
   239    *   The logged in user.
       
   240    */
       
   241   private function doAdminTests($user) {
       
   242     // Login the user.
       
   243     $this->drupalLogin($user);
       
   244 
       
   245     // Enable the active forum block.
       
   246     $edit = array();
       
   247     $edit['blocks[forum_active][region]'] = 'sidebar_second';
       
   248     $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
       
   249     $this->assertResponse(200);
       
   250     $this->assertText(t('The block settings have been updated.'), 'Active forum topics forum block was enabled');
       
   251 
       
   252     // Enable the new forum block.
       
   253     $edit = array();
       
   254     $edit['blocks[forum_new][region]'] = 'sidebar_second';
       
   255     $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
       
   256     $this->assertResponse(200);
       
   257     $this->assertText(t('The block settings have been updated.'), '[New forum topics] Forum block was enabled');
       
   258 
       
   259     // Retrieve forum menu id.
       
   260     $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = 'forum' AND menu_name = 'navigation' AND module = 'system' ORDER BY mlid ASC", 0, 1)->fetchField();
       
   261 
       
   262     // Add forum to navigation menu.
       
   263     $edit = array();
       
   264     $this->drupalPost('admin/structure/menu/manage/navigation', $edit, t('Save configuration'));
       
   265     $this->assertResponse(200);
       
   266 
       
   267     // Edit forum taxonomy.
       
   268     // Restoration of the settings fails and causes subsequent tests to fail.
       
   269     $this->container = $this->editForumTaxonomy();
       
   270     // Create forum container.
       
   271     $this->container = $this->createForum('container');
       
   272     // Verify "edit container" link exists and functions correctly.
       
   273     $this->drupalGet('admin/structure/forum');
       
   274     $this->clickLink('edit container');
       
   275     $this->assertRaw('Edit container', 'Followed the link to edit the container');
       
   276     // Create forum inside the forum container.
       
   277     $this->forum = $this->createForum('forum', $this->container['tid']);
       
   278     // Verify the "edit forum" link exists and functions correctly.
       
   279     $this->drupalGet('admin/structure/forum');
       
   280     $this->clickLink('edit forum');
       
   281     $this->assertRaw('Edit forum', 'Followed the link to edit the forum');
       
   282     // Navigate back to forum structure page.
       
   283     $this->drupalGet('admin/structure/forum');
       
   284     // Create second forum in container.
       
   285     $this->delete_forum = $this->createForum('forum', $this->container['tid']);
       
   286     // Save forum overview.
       
   287     $this->drupalPost('admin/structure/forum/', array(), t('Save'));
       
   288     $this->assertRaw(t('The configuration options have been saved.'));
       
   289     // Delete this second forum.
       
   290     $this->deleteForum($this->delete_forum['tid']);
       
   291     // Create forum at the top (root) level.
       
   292     $this->root_forum = $this->createForum('forum');
       
   293 
       
   294     // Test vocabulary form alterations.
       
   295     $this->drupalGet('admin/structure/taxonomy/forums/edit');
       
   296     $this->assertFieldByName('op', t('Save'), 'Save button found.');
       
   297     $this->assertNoFieldByName('op', t('Delete'), 'Delete button not found.');
       
   298 
       
   299     // Test term edit form alterations.
       
   300     $this->drupalGet('taxonomy/term/' . $this->container['tid'] . '/edit');
       
   301     // Test parent field been hidden by forum module.
       
   302     $this->assertNoField('parent[]', 'Parent field not found.');
       
   303 
       
   304     // Test tags vocabulary form is not affected.
       
   305     $this->drupalGet('admin/structure/taxonomy/tags/edit');
       
   306     $this->assertFieldByName('op', t('Save'), 'Save button found.');
       
   307     $this->assertFieldByName('op', t('Delete'), 'Delete button found.');
       
   308     // Test tags vocabulary term form is not affected.
       
   309     $this->drupalGet('admin/structure/taxonomy/tags/add');
       
   310     $this->assertField('parent[]', 'Parent field found.');
       
   311     // Test relations fieldset exists.
       
   312     $relations_fieldset = $this->xpath("//fieldset[@id='edit-relations']");
       
   313     $this->assertTrue(isset($relations_fieldset[0]), 'Relations fieldset element found.');
       
   314   }
       
   315 
       
   316   /**
       
   317    * Edits the forum taxonomy.
       
   318    */
       
   319   function editForumTaxonomy() {
       
   320     // Backup forum taxonomy.
       
   321     $vid = variable_get('forum_nav_vocabulary', '');
       
   322     $original_settings = taxonomy_vocabulary_load($vid);
       
   323 
       
   324     // Generate a random name/description.
       
   325     $title = $this->randomName(10);
       
   326     $description = $this->randomName(100);
       
   327 
       
   328     $edit = array(
       
   329       'name' => $title,
       
   330       'description' => $description,
       
   331       'machine_name' => drupal_strtolower(drupal_substr($this->randomName(), 3, 9)),
       
   332     );
       
   333 
       
   334     // Edit the vocabulary.
       
   335     $this->drupalPost('admin/structure/taxonomy/' . $original_settings->machine_name . '/edit', $edit, t('Save'));
       
   336     $this->assertResponse(200);
       
   337     $this->assertRaw(t('Updated vocabulary %name.', array('%name' => $title)), 'Vocabulary was edited');
       
   338 
       
   339     // Grab the newly edited vocabulary.
       
   340     entity_get_controller('taxonomy_vocabulary')->resetCache();
       
   341     $current_settings = taxonomy_vocabulary_load($vid);
       
   342 
       
   343     // Make sure we actually edited the vocabulary properly.
       
   344     $this->assertEqual($current_settings->name, $title, 'The name was updated');
       
   345     $this->assertEqual($current_settings->description, $description, 'The description was updated');
       
   346 
       
   347     // Restore the original vocabulary.
       
   348     taxonomy_vocabulary_save($original_settings);
       
   349     drupal_static_reset('taxonomy_vocabulary_load');
       
   350     $current_settings = taxonomy_vocabulary_load($vid);
       
   351     $this->assertEqual($current_settings->name, $original_settings->name, 'The original vocabulary settings were restored');
       
   352   }
       
   353 
       
   354   /**
       
   355    * Creates a forum container or a forum.
       
   356    *
       
   357    * @param $type
       
   358    *   The forum type (forum container or forum).
       
   359    * @param $parent
       
   360    *   The forum parent. This defaults to 0, indicating a root forum.
       
   361    *   another forum).
       
   362    *
       
   363    * @return
       
   364    *   The created taxonomy term data.
       
   365    */
       
   366   function createForum($type, $parent = 0) {
       
   367     // Generate a random name/description.
       
   368     $name = $this->randomName(10);
       
   369     $description = $this->randomName(100);
       
   370 
       
   371     $edit = array(
       
   372       'name' => $name,
       
   373       'description' => $description,
       
   374       'parent[0]' => $parent,
       
   375       'weight' => '0',
       
   376     );
       
   377 
       
   378     // Create forum.
       
   379     $this->drupalPost('admin/structure/forum/add/' . $type, $edit, t('Save'));
       
   380     $this->assertResponse(200);
       
   381     $type = ($type == 'container') ? 'forum container' : 'forum';
       
   382     $this->assertRaw(t('Created new @type %term.', array('%term' => $name, '@type' => t($type))), format_string('@type was created', array('@type' => ucfirst($type))));
       
   383 
       
   384     // Verify forum.
       
   385     $term = db_query("SELECT * FROM {taxonomy_term_data} t WHERE t.vid = :vid AND t.name = :name AND t.description = :desc", array(':vid' => variable_get('forum_nav_vocabulary', ''), ':name' => $name, ':desc' => $description))->fetchAssoc();
       
   386     $this->assertTrue(!empty($term), 'The ' . $type . ' exists in the database');
       
   387 
       
   388     // Verify forum hierarchy.
       
   389     $tid = $term['tid'];
       
   390     $parent_tid = db_query("SELECT t.parent FROM {taxonomy_term_hierarchy} t WHERE t.tid = :tid", array(':tid' => $tid))->fetchField();
       
   391     $this->assertTrue($parent == $parent_tid, 'The ' . $type . ' is linked to its container');
       
   392 
       
   393     return $term;
       
   394   }
       
   395 
       
   396   /**
       
   397    * Deletes a forum.
       
   398    *
       
   399    * @param $tid
       
   400    *   The forum ID.
       
   401    */
       
   402   function deleteForum($tid) {
       
   403     // Delete the forum.
       
   404     $this->drupalPost('admin/structure/forum/edit/forum/' . $tid, array(), t('Delete'));
       
   405     $this->drupalPost(NULL, array(), t('Delete'));
       
   406 
       
   407     // Assert that the forum no longer exists.
       
   408     $this->drupalGet('forum/' . $tid);
       
   409     $this->assertResponse(404, 'The forum was not found');
       
   410 
       
   411     // Assert that the associated term has been removed from the
       
   412     // forum_containers variable.
       
   413     $containers = variable_get('forum_containers', array());
       
   414     $this->assertFalse(in_array($tid, $containers), 'The forum_containers variable has been updated.');
       
   415   }
       
   416 
       
   417   /**
       
   418    * Runs basic tests on the indicated user.
       
   419    *
       
   420    * @param $user
       
   421    *   The logged in user.
       
   422    * @param $admin
       
   423    *   User has 'access administration pages' privilege.
       
   424    */
       
   425   private function doBasicTests($user, $admin) {
       
   426     // Login the user.
       
   427     $this->drupalLogin($user);
       
   428     // Attempt to create forum topic under a container.
       
   429     $this->createForumTopic($this->container, TRUE);
       
   430     // Create forum node.
       
   431     $node = $this->createForumTopic($this->forum, FALSE);
       
   432     // Verify the user has access to all the forum nodes.
       
   433     $this->verifyForums($user, $node, $admin);
       
   434   }
       
   435 
       
   436   /**
       
   437    * Creates forum topic.
       
   438    *
       
   439    * @param array $forum
       
   440    *   A forum array.
       
   441    * @param boolean $container
       
   442    *   TRUE if $forum is a container; FALSE otherwise.
       
   443    *
       
   444    * @return object
       
   445    *   The created topic node.
       
   446    */
       
   447   function createForumTopic($forum, $container = FALSE) {
       
   448     // Generate a random subject/body.
       
   449     $title = $this->randomName(20);
       
   450     $body = $this->randomName(200);
       
   451 
       
   452     $langcode = LANGUAGE_NONE;
       
   453     $edit = array(
       
   454       "title" => $title,
       
   455       "body[$langcode][0][value]" => $body,
       
   456     );
       
   457     $tid = $forum['tid'];
       
   458 
       
   459     // Create the forum topic, preselecting the forum ID via a URL parameter.
       
   460     $this->drupalPost('node/add/forum/' . $tid, $edit, t('Save'));
       
   461 
       
   462     $type = t('Forum topic');
       
   463     if ($container) {
       
   464       $this->assertNoRaw(t('@type %title has been created.', array('@type' => $type, '%title' => $title)), 'Forum topic was not created');
       
   465       $this->assertRaw(t('The item %title is a forum container, not a forum.', array('%title' => $forum['name'])), 'Error message was shown');
       
   466       return;
       
   467     }
       
   468     else {
       
   469       $this->assertRaw(t('@type %title has been created.', array('@type' => $type, '%title' => $title)), 'Forum topic was created');
       
   470       $this->assertNoRaw(t('The item %title is a forum container, not a forum.', array('%title' => $forum['name'])), 'No error message was shown');
       
   471     }
       
   472 
       
   473     // Retrieve node object, ensure that the topic was created and in the proper forum.
       
   474     $node = $this->drupalGetNodeByTitle($title);
       
   475     $this->assertTrue($node != NULL, format_string('Node @title was loaded', array('@title' => $title)));
       
   476     $this->assertEqual($node->taxonomy_forums[LANGUAGE_NONE][0]['tid'], $tid, 'Saved forum topic was in the expected forum');
       
   477 
       
   478     // View forum topic.
       
   479     $this->drupalGet('node/' . $node->nid);
       
   480     $this->assertRaw($title, 'Subject was found');
       
   481     $this->assertRaw($body, 'Body was found');
       
   482 
       
   483     return $node;
       
   484   }
       
   485 
       
   486   /**
       
   487    * Verifies that the logged in user has access to a forum nodes.
       
   488    *
       
   489    * @param $node_user
       
   490    *   The user who creates the node.
       
   491    * @param $node
       
   492    *   The node being checked.
       
   493    * @param $admin
       
   494    *   Boolean to indicate whether the user can 'access administration pages'.
       
   495    * @param $response
       
   496    *   The exptected HTTP response code.
       
   497    */
       
   498   private function verifyForums($node_user, $node, $admin, $response = 200) {
       
   499     $response2 = ($admin) ? 200 : 403;
       
   500 
       
   501     // View forum help node.
       
   502     $this->drupalGet('admin/help/forum');
       
   503     $this->assertResponse($response2);
       
   504     if ($response2 == 200) {
       
   505       $this->assertTitle(t('Forum | Drupal'), 'Forum help title was displayed');
       
   506       $this->assertText(t('Forum'), 'Forum help node was displayed');
       
   507     }
       
   508 
       
   509     // Verify the forum blocks were displayed.
       
   510     $this->drupalGet('');
       
   511     $this->assertResponse(200);
       
   512     $this->assertText(t('New forum topics'), '[New forum topics] Forum block was displayed');
       
   513 
       
   514     // View forum container page.
       
   515     $this->verifyForumView($this->container);
       
   516     // View forum page.
       
   517     $this->verifyForumView($this->forum, $this->container);
       
   518     // View root forum page.
       
   519     $this->verifyForumView($this->root_forum);
       
   520 
       
   521     // View forum node.
       
   522     $this->drupalGet('node/' . $node->nid);
       
   523     $this->assertResponse(200);
       
   524     $this->assertTitle($node->title . ' | Drupal', 'Forum node was displayed');
       
   525     $breadcrumb = array(
       
   526       l(t('Home'), NULL),
       
   527       l(t('Forums'), 'forum'),
       
   528       l($this->container['name'], 'forum/' . $this->container['tid']),
       
   529       l($this->forum['name'], 'forum/' . $this->forum['tid']),
       
   530     );
       
   531     $this->assertRaw(theme('breadcrumb', array('breadcrumb' => $breadcrumb)), 'Breadcrumbs were displayed');
       
   532 
       
   533     // View forum edit node.
       
   534     $this->drupalGet('node/' . $node->nid . '/edit');
       
   535     $this->assertResponse($response);
       
   536     if ($response == 200) {
       
   537       $this->assertTitle('Edit Forum topic ' . $node->title . ' | Drupal', 'Forum edit node was displayed');
       
   538     }
       
   539 
       
   540     if ($response == 200) {
       
   541       // Edit forum node (including moving it to another forum).
       
   542       $edit = array();
       
   543       $langcode = LANGUAGE_NONE;
       
   544       $edit["title"] = 'node/' . $node->nid;
       
   545       $edit["body[$langcode][0][value]"] = $this->randomName(256);
       
   546       // Assume the topic is initially associated with $forum.
       
   547       $edit["taxonomy_forums[$langcode]"] = $this->root_forum['tid'];
       
   548       $edit['shadow'] = TRUE;
       
   549       $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
       
   550       $this->assertRaw(t('Forum topic %title has been updated.', array('%title' => $edit["title"])), 'Forum node was edited');
       
   551 
       
   552       // Verify topic was moved to a different forum.
       
   553       $forum_tid = db_query("SELECT tid FROM {forum} WHERE nid = :nid AND vid = :vid", array(
       
   554         ':nid' => $node->nid,
       
   555         ':vid' => $node->vid,
       
   556       ))->fetchField();
       
   557       $this->assertTrue($forum_tid == $this->root_forum['tid'], 'The forum topic is linked to a different forum');
       
   558 
       
   559       // Delete forum node.
       
   560       $this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
       
   561       $this->assertResponse($response);
       
   562       $this->assertRaw(t('Forum topic %title has been deleted.', array('%title' => $edit['title'])), 'Forum node was deleted');
       
   563     }
       
   564   }
       
   565 
       
   566   /**
       
   567    * Verifies display of forum page.
       
   568    *
       
   569    * @param $forum
       
   570    *   A row from the taxonomy_term_data table in an array.
       
   571    * @param $parent
       
   572    *   (optional) An array representing the forum's parent.
       
   573    */
       
   574   private function verifyForumView($forum, $parent = NULL) {
       
   575     // View forum page.
       
   576     $this->drupalGet('forum/' . $forum['tid']);
       
   577     $this->assertResponse(200);
       
   578     $this->assertTitle($forum['name'] . ' | Drupal', 'Forum name was displayed');
       
   579 
       
   580     $breadcrumb = array(
       
   581       l(t('Home'), NULL),
       
   582       l(t('Forums'), 'forum'),
       
   583     );
       
   584     if (isset($parent)) {
       
   585       $breadcrumb[] = l($parent['name'], 'forum/' . $parent['tid']);
       
   586     }
       
   587 
       
   588     $this->assertRaw(theme('breadcrumb', array('breadcrumb' => $breadcrumb)), 'Breadcrumbs were displayed');
       
   589   }
       
   590 
       
   591   /**
       
   592    * Generates forum topics to test the display of an active forum block.
       
   593    *
       
   594    * @param array $forum
       
   595    *   The foorum array (a row from taxonomy_term_data table).
       
   596    */
       
   597   private function generateForumTopics($forum) {
       
   598     $this->nids = array();
       
   599     for ($i = 0; $i < 5; $i++) {
       
   600       $node = $this->createForumTopic($this->forum, FALSE);
       
   601       $this->nids[] = $node->nid;
       
   602     }
       
   603   }
       
   604 
       
   605   /**
       
   606    * Views forum topics to test the display of an active forum block.
       
   607    *
       
   608    * @todo The logic here is completely incorrect, since the active forum topics
       
   609    *   block is determined by comments on the node, not by views.
       
   610    * @todo DIE
       
   611    *
       
   612    * @param $nids
       
   613    *   An array of forum node IDs.
       
   614    */
       
   615   private function viewForumTopics($nids) {
       
   616     for ($i = 0; $i < 2; $i++) {
       
   617       foreach ($nids as $nid) {
       
   618         $this->drupalGet('node/' . $nid);
       
   619         $this->drupalGet('node/' . $nid);
       
   620         $this->drupalGet('node/' . $nid);
       
   621       }
       
   622     }
       
   623   }
       
   624 }
       
   625 
       
   626 /**
       
   627  * Tests the forum index listing.
       
   628  */
       
   629 class ForumIndexTestCase extends DrupalWebTestCase {
       
   630 
       
   631   public static function getInfo() {
       
   632     return array(
       
   633       'name' => 'Forum index',
       
   634       'description' => 'Tests the forum index listing.',
       
   635       'group' => 'Forum',
       
   636     );
       
   637   }
       
   638 
       
   639   function setUp() {
       
   640     parent::setUp('taxonomy', 'comment', 'forum');
       
   641 
       
   642     // Create a test user.
       
   643     $web_user = $this->drupalCreateUser(array('create forum content', 'edit own forum content', 'edit any forum content', 'administer nodes'));
       
   644     $this->drupalLogin($web_user);
       
   645   }
       
   646 
       
   647   /**
       
   648    * Tests the forum index for published and unpublished nodes.
       
   649    */
       
   650   function testForumIndexStatus() {
       
   651 
       
   652     $langcode = LANGUAGE_NONE;
       
   653 
       
   654     // The forum ID to use.
       
   655     $tid = 1;
       
   656 
       
   657     // Create a test node.
       
   658     $title = $this->randomName(20);
       
   659     $edit = array(
       
   660       "title" => $title,
       
   661       "body[$langcode][0][value]" => $this->randomName(200),
       
   662     );
       
   663 
       
   664     // Create the forum topic, preselecting the forum ID via a URL parameter.
       
   665     $this->drupalPost('node/add/forum/' . $tid, $edit, t('Save'));
       
   666 
       
   667     // Check that the node exists in the database.
       
   668     $node = $this->drupalGetNodeByTitle($title);
       
   669     $this->assertTrue(!empty($node), 'New forum node found in database.');
       
   670 
       
   671     // Verify that the node appears on the index.
       
   672     $this->drupalGet('forum/' . $tid);
       
   673     $this->assertText($title, 'Published forum topic appears on index.');
       
   674 
       
   675     // Unpublish the node.
       
   676     $edit = array(
       
   677       'status' => FALSE,
       
   678     );
       
   679     $this->drupalPost("node/{$node->nid}/edit", $edit, t('Save'));
       
   680     $this->drupalGet("node/{$node->nid}");
       
   681     $this->assertText(t('Access denied'), 'Unpublished node is no longer accessible.');
       
   682 
       
   683     // Verify that the node no longer appears on the index.
       
   684     $this->drupalGet('forum/' . $tid);
       
   685     $this->assertNoText($title, 'Unpublished forum topic no longer appears on index.');
       
   686   }
       
   687 }