forum.test

Tests for forum.module.

File

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