forum.test 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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.'), t('Modules status has been updated.'));
  84. module_list(TRUE);
  85. $this->assertFalse(module_exists('forum'), t('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.'), t('Modules status has been updated.'));
  92. module_list(TRUE);
  93. $this->assertTrue(module_exists('forum'), t('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', t('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, t('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', t('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. }
  170. /**
  171. * Tests that forum nodes can't be added without a parent.
  172. *
  173. * Verifies that forum nodes are not created without choosing "forum" from the
  174. * select list.
  175. */
  176. function testAddOrphanTopic() {
  177. // Must remove forum topics to test creating orphan topics.
  178. $vid = variable_get('forum_nav_vocabulary');
  179. $tree = taxonomy_get_tree($vid);
  180. foreach ($tree as $term) {
  181. taxonomy_term_delete($term->tid);
  182. }
  183. // Create an orphan forum item.
  184. $this->drupalLogin($this->admin_user);
  185. $this->drupalPost('node/add/forum', array('title' => $this->randomName(10), 'body[' . LANGUAGE_NONE .'][0][value]' => $this->randomName(120)), t('Save'));
  186. $nid_count = db_query('SELECT COUNT(nid) FROM {node}')->fetchField();
  187. $this->assertEqual(0, $nid_count, t('A forum node was not created when missing a forum vocabulary.'));
  188. // Reset the defaults for future tests.
  189. module_enable(array('forum'));
  190. }
  191. /**
  192. * Runs admin tests on the admin user.
  193. *
  194. * @param object $user
  195. * The logged in user.
  196. */
  197. private function doAdminTests($user) {
  198. // Login the user.
  199. $this->drupalLogin($user);
  200. // Enable the active forum block.
  201. $edit = array();
  202. $edit['blocks[forum_active][region]'] = 'sidebar_second';
  203. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  204. $this->assertResponse(200);
  205. $this->assertText(t('The block settings have been updated.'), t('Active forum topics forum block was enabled'));
  206. // Enable the new forum block.
  207. $edit = array();
  208. $edit['blocks[forum_new][region]'] = 'sidebar_second';
  209. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  210. $this->assertResponse(200);
  211. $this->assertText(t('The block settings have been updated.'), t('[New forum topics] Forum block was enabled'));
  212. // Retrieve forum menu id.
  213. $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();
  214. // Add forum to navigation menu.
  215. $edit = array();
  216. $this->drupalPost('admin/structure/menu/manage/navigation', $edit, t('Save configuration'));
  217. $this->assertResponse(200);
  218. // Edit forum taxonomy.
  219. // Restoration of the settings fails and causes subsequent tests to fail.
  220. $this->container = $this->editForumTaxonomy();
  221. // Create forum container.
  222. $this->container = $this->createForum('container');
  223. // Verify "edit container" link exists and functions correctly.
  224. $this->drupalGet('admin/structure/forum');
  225. $this->clickLink('edit container');
  226. $this->assertRaw('Edit container', t('Followed the link to edit the container'));
  227. // Create forum inside the forum container.
  228. $this->forum = $this->createForum('forum', $this->container['tid']);
  229. // Verify the "edit forum" link exists and functions correctly.
  230. $this->drupalGet('admin/structure/forum');
  231. $this->clickLink('edit forum');
  232. $this->assertRaw('Edit forum', t('Followed the link to edit the forum'));
  233. // Navigate back to forum structure page.
  234. $this->drupalGet('admin/structure/forum');
  235. // Create second forum in container.
  236. $this->delete_forum = $this->createForum('forum', $this->container['tid']);
  237. // Save forum overview.
  238. $this->drupalPost('admin/structure/forum/', array(), t('Save'));
  239. $this->assertRaw(t('The configuration options have been saved.'));
  240. // Delete this second forum.
  241. $this->deleteForum($this->delete_forum['tid']);
  242. // Create forum at the top (root) level.
  243. $this->root_forum = $this->createForum('forum');
  244. // Test vocabulary form alterations.
  245. $this->drupalGet('admin/structure/taxonomy/forums/edit');
  246. $this->assertFieldByName('op', t('Save'), 'Save button found.');
  247. $this->assertNoFieldByName('op', t('Delete'), 'Delete button not found.');
  248. // Test term edit form alterations.
  249. $this->drupalGet('taxonomy/term/' . $this->container['tid'] . '/edit');
  250. // Test parent field been hidden by forum module.
  251. $this->assertNoField('parent[]', 'Parent field not found.');
  252. // Test tags vocabulary form is not affected.
  253. $this->drupalGet('admin/structure/taxonomy/tags/edit');
  254. $this->assertFieldByName('op', t('Save'), 'Save button found.');
  255. $this->assertFieldByName('op', t('Delete'), 'Delete button found.');
  256. // Test tags vocabulary term form is not affected.
  257. $this->drupalGet('admin/structure/taxonomy/tags/add');
  258. $this->assertField('parent[]', 'Parent field found.');
  259. // Test relations fieldset exists.
  260. $relations_fieldset = $this->xpath("//fieldset[@id='edit-relations']");
  261. $this->assertTrue(isset($relations_fieldset[0]), 'Relations fieldset element found.');
  262. }
  263. /**
  264. * Edits the forum taxonomy.
  265. */
  266. function editForumTaxonomy() {
  267. // Backup forum taxonomy.
  268. $vid = variable_get('forum_nav_vocabulary', '');
  269. $original_settings = taxonomy_vocabulary_load($vid);
  270. // Generate a random name/description.
  271. $title = $this->randomName(10);
  272. $description = $this->randomName(100);
  273. $edit = array(
  274. 'name' => $title,
  275. 'description' => $description,
  276. 'machine_name' => drupal_strtolower(drupal_substr($this->randomName(), 3, 9)),
  277. );
  278. // Edit the vocabulary.
  279. $this->drupalPost('admin/structure/taxonomy/' . $original_settings->machine_name . '/edit', $edit, t('Save'));
  280. $this->assertResponse(200);
  281. $this->assertRaw(t('Updated vocabulary %name.', array('%name' => $title)), t('Vocabulary was edited'));
  282. // Grab the newly edited vocabulary.
  283. entity_get_controller('taxonomy_vocabulary')->resetCache();
  284. $current_settings = taxonomy_vocabulary_load($vid);
  285. // Make sure we actually edited the vocabulary properly.
  286. $this->assertEqual($current_settings->name, $title, t('The name was updated'));
  287. $this->assertEqual($current_settings->description, $description, t('The description was updated'));
  288. // Restore the original vocabulary.
  289. taxonomy_vocabulary_save($original_settings);
  290. drupal_static_reset('taxonomy_vocabulary_load');
  291. $current_settings = taxonomy_vocabulary_load($vid);
  292. $this->assertEqual($current_settings->name, $original_settings->name, 'The original vocabulary settings were restored');
  293. }
  294. /**
  295. * Creates a forum container or a forum.
  296. *
  297. * @param $type
  298. * The forum type (forum container or forum).
  299. * @param $parent
  300. * The forum parent. This defaults to 0, indicating a root forum.
  301. * another forum).
  302. *
  303. * @return
  304. * The created taxonomy term data.
  305. */
  306. function createForum($type, $parent = 0) {
  307. // Generate a random name/description.
  308. $name = $this->randomName(10);
  309. $description = $this->randomName(100);
  310. $edit = array(
  311. 'name' => $name,
  312. 'description' => $description,
  313. 'parent[0]' => $parent,
  314. 'weight' => '0',
  315. );
  316. // Create forum.
  317. $this->drupalPost('admin/structure/forum/add/' . $type, $edit, t('Save'));
  318. $this->assertResponse(200);
  319. $type = ($type == 'container') ? 'forum container' : 'forum';
  320. $this->assertRaw(t('Created new @type %term.', array('%term' => $name, '@type' => t($type))), t(ucfirst($type) . ' was created'));
  321. // Verify forum.
  322. $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();
  323. $this->assertTrue(!empty($term), 'The ' . $type . ' exists in the database');
  324. // Verify forum hierarchy.
  325. $tid = $term['tid'];
  326. $parent_tid = db_query("SELECT t.parent FROM {taxonomy_term_hierarchy} t WHERE t.tid = :tid", array(':tid' => $tid))->fetchField();
  327. $this->assertTrue($parent == $parent_tid, 'The ' . $type . ' is linked to its container');
  328. return $term;
  329. }
  330. /**
  331. * Deletes a forum.
  332. *
  333. * @param $tid
  334. * The forum ID.
  335. */
  336. function deleteForum($tid) {
  337. // Delete the forum.
  338. $this->drupalPost('admin/structure/forum/edit/forum/' . $tid, array(), t('Delete'));
  339. $this->drupalPost(NULL, array(), t('Delete'));
  340. // Assert that the forum no longer exists.
  341. $this->drupalGet('forum/' . $tid);
  342. $this->assertResponse(404, 'The forum was not found');
  343. // Assert that the associated term has been removed from the
  344. // forum_containers variable.
  345. $containers = variable_get('forum_containers', array());
  346. $this->assertFalse(in_array($tid, $containers), 'The forum_containers variable has been updated.');
  347. }
  348. /**
  349. * Runs basic tests on the indicated user.
  350. *
  351. * @param $user
  352. * The logged in user.
  353. * @param $admin
  354. * User has 'access administration pages' privilege.
  355. */
  356. private function doBasicTests($user, $admin) {
  357. // Login the user.
  358. $this->drupalLogin($user);
  359. // Attempt to create forum topic under a container.
  360. $this->createForumTopic($this->container, TRUE);
  361. // Create forum node.
  362. $node = $this->createForumTopic($this->forum, FALSE);
  363. // Verify the user has access to all the forum nodes.
  364. $this->verifyForums($user, $node, $admin);
  365. }
  366. /**
  367. * Creates forum topic.
  368. *
  369. * @param array $forum
  370. * A forum array.
  371. * @param boolean $container
  372. * TRUE if $forum is a container; FALSE otherwise.
  373. *
  374. * @return object
  375. * The created topic node.
  376. */
  377. function createForumTopic($forum, $container = FALSE) {
  378. // Generate a random subject/body.
  379. $title = $this->randomName(20);
  380. $body = $this->randomName(200);
  381. $langcode = LANGUAGE_NONE;
  382. $edit = array(
  383. "title" => $title,
  384. "body[$langcode][0][value]" => $body,
  385. );
  386. $tid = $forum['tid'];
  387. // Create the forum topic, preselecting the forum ID via a URL parameter.
  388. $this->drupalPost('node/add/forum/' . $tid, $edit, t('Save'));
  389. $type = t('Forum topic');
  390. if ($container) {
  391. $this->assertNoRaw(t('@type %title has been created.', array('@type' => $type, '%title' => $title)), t('Forum topic was not created'));
  392. $this->assertRaw(t('The item %title is a forum container, not a forum.', array('%title' => $forum['name'])), t('Error message was shown'));
  393. return;
  394. }
  395. else {
  396. $this->assertRaw(t('@type %title has been created.', array('@type' => $type, '%title' => $title)), t('Forum topic was created'));
  397. $this->assertNoRaw(t('The item %title is a forum container, not a forum.', array('%title' => $forum['name'])), t('No error message was shown'));
  398. }
  399. // Retrieve node object, ensure that the topic was created and in the proper forum.
  400. $node = $this->drupalGetNodeByTitle($title);
  401. $this->assertTrue($node != NULL, t('Node @title was loaded', array('@title' => $title)));
  402. $this->assertEqual($node->taxonomy_forums[LANGUAGE_NONE][0]['tid'], $tid, 'Saved forum topic was in the expected forum');
  403. // View forum topic.
  404. $this->drupalGet('node/' . $node->nid);
  405. $this->assertRaw($title, t('Subject was found'));
  406. $this->assertRaw($body, t('Body was found'));
  407. return $node;
  408. }
  409. /**
  410. * Verifies that the logged in user has access to a forum nodes.
  411. *
  412. * @param $node_user
  413. * The user who creates the node.
  414. * @param $node
  415. * The node being checked.
  416. * @param $admin
  417. * Boolean to indicate whether the user can 'access administration pages'.
  418. * @param $response
  419. * The exptected HTTP response code.
  420. */
  421. private function verifyForums($node_user, $node, $admin, $response = 200) {
  422. $response2 = ($admin) ? 200 : 403;
  423. // View forum help node.
  424. $this->drupalGet('admin/help/forum');
  425. $this->assertResponse($response2);
  426. if ($response2 == 200) {
  427. $this->assertTitle(t('Forum | Drupal'), t('Forum help title was displayed'));
  428. $this->assertText(t('Forum'), t('Forum help node was displayed'));
  429. }
  430. // Verify the forum blocks were displayed.
  431. $this->drupalGet('');
  432. $this->assertResponse(200);
  433. $this->assertText(t('New forum topics'), t('[New forum topics] Forum block was displayed'));
  434. // View forum container page.
  435. $this->verifyForumView($this->container);
  436. // View forum page.
  437. $this->verifyForumView($this->forum, $this->container);
  438. // View root forum page.
  439. $this->verifyForumView($this->root_forum);
  440. // View forum node.
  441. $this->drupalGet('node/' . $node->nid);
  442. $this->assertResponse(200);
  443. $this->assertTitle($node->title . ' | Drupal', t('Forum node was displayed'));
  444. $breadcrumb = array(
  445. l(t('Home'), NULL),
  446. l(t('Forums'), 'forum'),
  447. l($this->container['name'], 'forum/' . $this->container['tid']),
  448. l($this->forum['name'], 'forum/' . $this->forum['tid']),
  449. );
  450. $this->assertRaw(theme('breadcrumb', array('breadcrumb' => $breadcrumb)), t('Breadcrumbs were displayed'));
  451. // View forum edit node.
  452. $this->drupalGet('node/' . $node->nid . '/edit');
  453. $this->assertResponse($response);
  454. if ($response == 200) {
  455. $this->assertTitle('Edit Forum topic ' . $node->title . ' | Drupal', t('Forum edit node was displayed'));
  456. }
  457. if ($response == 200) {
  458. // Edit forum node (including moving it to another forum).
  459. $edit = array();
  460. $langcode = LANGUAGE_NONE;
  461. $edit["title"] = 'node/' . $node->nid;
  462. $edit["body[$langcode][0][value]"] = $this->randomName(256);
  463. // Assume the topic is initially associated with $forum.
  464. $edit["taxonomy_forums[$langcode]"] = $this->root_forum['tid'];
  465. $edit['shadow'] = TRUE;
  466. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  467. $this->assertRaw(t('Forum topic %title has been updated.', array('%title' => $edit["title"])), t('Forum node was edited'));
  468. // Verify topic was moved to a different forum.
  469. $forum_tid = db_query("SELECT tid FROM {forum} WHERE nid = :nid AND vid = :vid", array(
  470. ':nid' => $node->nid,
  471. ':vid' => $node->vid,
  472. ))->fetchField();
  473. $this->assertTrue($forum_tid == $this->root_forum['tid'], 'The forum topic is linked to a different forum');
  474. // Delete forum node.
  475. $this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
  476. $this->assertResponse($response);
  477. $this->assertRaw(t('Forum topic %title has been deleted.', array('%title' => $edit['title'])), t('Forum node was deleted'));
  478. }
  479. }
  480. /**
  481. * Verifies display of forum page.
  482. *
  483. * @param $forum
  484. * A row from the taxonomy_term_data table in an array.
  485. * @param $parent
  486. * (optional) An array representing the forum's parent.
  487. */
  488. private function verifyForumView($forum, $parent = NULL) {
  489. // View forum page.
  490. $this->drupalGet('forum/' . $forum['tid']);
  491. $this->assertResponse(200);
  492. $this->assertTitle($forum['name'] . ' | Drupal', t('Forum name was displayed'));
  493. $breadcrumb = array(
  494. l(t('Home'), NULL),
  495. l(t('Forums'), 'forum'),
  496. );
  497. if (isset($parent)) {
  498. $breadcrumb[] = l($parent['name'], 'forum/' . $parent['tid']);
  499. }
  500. $this->assertRaw(theme('breadcrumb', array('breadcrumb' => $breadcrumb)), t('Breadcrumbs were displayed'));
  501. }
  502. /**
  503. * Generates forum topics to test the display of an active forum block.
  504. *
  505. * @param array $forum
  506. * The foorum array (a row from taxonomy_term_data table).
  507. */
  508. private function generateForumTopics($forum) {
  509. $this->nids = array();
  510. for ($i = 0; $i < 5; $i++) {
  511. $node = $this->createForumTopic($this->forum, FALSE);
  512. $this->nids[] = $node->nid;
  513. }
  514. }
  515. /**
  516. * Views forum topics to test the display of an active forum block.
  517. *
  518. * @todo The logic here is completely incorrect, since the active forum topics
  519. * block is determined by comments on the node, not by views.
  520. * @todo DIE
  521. *
  522. * @param $nids
  523. * An array of forum node IDs.
  524. */
  525. private function viewForumTopics($nids) {
  526. for ($i = 0; $i < 2; $i++) {
  527. foreach ($nids as $nid) {
  528. $this->drupalGet('node/' . $nid);
  529. $this->drupalGet('node/' . $nid);
  530. $this->drupalGet('node/' . $nid);
  531. }
  532. }
  533. }
  534. }
  535. /**
  536. * Tests the forum index listing.
  537. */
  538. class ForumIndexTestCase extends DrupalWebTestCase {
  539. public static function getInfo() {
  540. return array(
  541. 'name' => 'Forum index',
  542. 'description' => 'Tests the forum index listing.',
  543. 'group' => 'Forum',
  544. );
  545. }
  546. function setUp() {
  547. parent::setUp('taxonomy', 'comment', 'forum');
  548. // Create a test user.
  549. $web_user = $this->drupalCreateUser(array('create forum content', 'edit own forum content', 'edit any forum content', 'administer nodes'));
  550. $this->drupalLogin($web_user);
  551. }
  552. /**
  553. * Tests the forum index for published and unpublished nodes.
  554. */
  555. function testForumIndexStatus() {
  556. $langcode = LANGUAGE_NONE;
  557. // The forum ID to use.
  558. $tid = 1;
  559. // Create a test node.
  560. $title = $this->randomName(20);
  561. $edit = array(
  562. "title" => $title,
  563. "body[$langcode][0][value]" => $this->randomName(200),
  564. );
  565. // Create the forum topic, preselecting the forum ID via a URL parameter.
  566. $this->drupalPost('node/add/forum/' . $tid, $edit, t('Save'));
  567. // Check that the node exists in the database.
  568. $node = $this->drupalGetNodeByTitle($title);
  569. $this->assertTrue(!empty($node), 'New forum node found in database.');
  570. // Verify that the node appears on the index.
  571. $this->drupalGet('forum/' . $tid);
  572. $this->assertText($title, 'Published forum topic appears on index.');
  573. // Unpublish the node.
  574. $edit = array(
  575. 'status' => FALSE,
  576. );
  577. $this->drupalPost("node/{$node->nid}/edit", $edit, t('Save'));
  578. $this->assertText(t('Access denied'), 'Unpublished node is no longer accessible.');
  579. // Verify that the node no longer appears on the index.
  580. $this->drupalGet('forum/' . $tid);
  581. $this->assertNoText($title, 'Unpublished forum topic no longer appears on index.');
  582. }
  583. }