tracker.test

Tests for tracker.module.

File

drupal-7.x/modules/tracker/tracker.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Tests for tracker.module.
  5. */
  6. /**
  7. * Defines a base class for testing tracker.module.
  8. */
  9. class TrackerTest extends DrupalWebTestCase {
  10. /**
  11. * The main user for testing.
  12. *
  13. * @var object
  14. */
  15. protected $user;
  16. /**
  17. * A second user that will 'create' comments and nodes.
  18. *
  19. * @var object
  20. */
  21. protected $other_user;
  22. public static function getInfo() {
  23. return array(
  24. 'name' => 'Tracker',
  25. 'description' => 'Create and delete nodes and check for their display in the tracker listings.',
  26. 'group' => 'Tracker'
  27. );
  28. }
  29. function setUp() {
  30. parent::setUp('comment', 'tracker');
  31. $permissions = array('access comments', 'create page content', 'post comments', 'skip comment approval');
  32. $this->user = $this->drupalCreateUser($permissions);
  33. $this->other_user = $this->drupalCreateUser($permissions);
  34. // Make node preview optional.
  35. variable_set('comment_preview_page', 0);
  36. }
  37. /**
  38. * Tests for the presence of nodes on the global tracker listing.
  39. */
  40. function testTrackerAll() {
  41. $this->drupalLogin($this->user);
  42. $unpublished = $this->drupalCreateNode(array(
  43. 'title' => $this->randomName(8),
  44. 'status' => 0,
  45. ));
  46. $published = $this->drupalCreateNode(array(
  47. 'title' => $this->randomName(8),
  48. 'status' => 1,
  49. ));
  50. $this->drupalGet('tracker');
  51. $this->assertNoText($unpublished->title, 'Unpublished node do not show up in the tracker listing.');
  52. $this->assertText($published->title, 'Published node show up in the tracker listing.');
  53. $this->assertLink(t('My recent content'), 0, 'User tab shows up on the global tracker page.');
  54. // Delete a node and ensure it no longer appears on the tracker.
  55. node_delete($published->nid);
  56. $this->drupalGet('tracker');
  57. $this->assertNoText($published->title, 'Deleted node do not show up in the tracker listing.');
  58. }
  59. /**
  60. * Tests for the presence of nodes on a user's tracker listing.
  61. */
  62. function testTrackerUser() {
  63. $this->drupalLogin($this->user);
  64. $unpublished = $this->drupalCreateNode(array(
  65. 'title' => $this->randomName(8),
  66. 'uid' => $this->user->uid,
  67. 'status' => 0,
  68. ));
  69. $my_published = $this->drupalCreateNode(array(
  70. 'title' => $this->randomName(8),
  71. 'uid' => $this->user->uid,
  72. 'status' => 1,
  73. ));
  74. $other_published_no_comment = $this->drupalCreateNode(array(
  75. 'title' => $this->randomName(8),
  76. 'uid' => $this->other_user->uid,
  77. 'status' => 1,
  78. ));
  79. $other_published_my_comment = $this->drupalCreateNode(array(
  80. 'title' => $this->randomName(8),
  81. 'uid' => $this->other_user->uid,
  82. 'status' => 1,
  83. ));
  84. $comment = array(
  85. 'subject' => $this->randomName(),
  86. 'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
  87. );
  88. $this->drupalPost('comment/reply/' . $other_published_my_comment->nid, $comment, t('Save'));
  89. $this->drupalGet('user/' . $this->user->uid . '/track');
  90. $this->assertNoText($unpublished->title, "Unpublished nodes do not show up in the users's tracker listing.");
  91. $this->assertText($my_published->title, "Published nodes show up in the user's tracker listing.");
  92. $this->assertNoText($other_published_no_comment->title, "Other user's nodes do not show up in the user's tracker listing.");
  93. $this->assertText($other_published_my_comment->title, "Nodes that the user has commented on appear in the user's tracker listing.");
  94. // Verify that unpublished comments are removed from the tracker.
  95. $admin_user = $this->drupalCreateUser(array('administer comments', 'access user profiles'));
  96. $this->drupalLogin($admin_user);
  97. $this->drupalPost('comment/1/edit', array('status' => COMMENT_NOT_PUBLISHED), t('Save'));
  98. $this->drupalGet('user/' . $this->user->uid . '/track');
  99. $this->assertNoText($other_published_my_comment->title, 'Unpublished comments are not counted on the tracker listing.');
  100. }
  101. /**
  102. * Tests for the presence of the "new" flag for nodes.
  103. */
  104. function testTrackerNewNodes() {
  105. $this->drupalLogin($this->user);
  106. $edit = array(
  107. 'title' => $this->randomName(8),
  108. );
  109. $node = $this->drupalCreateNode($edit);
  110. $title = $edit['title'];
  111. $this->drupalGet('tracker');
  112. $this->assertPattern('/' . $title . '.*new/', 'New nodes are flagged as such in the tracker listing.');
  113. $this->drupalGet('node/' . $node->nid);
  114. $this->drupalGet('tracker');
  115. $this->assertNoPattern('/' . $title . '.*new/', 'Visited nodes are not flagged as new.');
  116. $this->drupalLogin($this->other_user);
  117. $this->drupalGet('tracker');
  118. $this->assertPattern('/' . $title . '.*new/', 'For another user, new nodes are flagged as such in the tracker listing.');
  119. $this->drupalGet('node/' . $node->nid);
  120. $this->drupalGet('tracker');
  121. $this->assertNoPattern('/' . $title . '.*new/', 'For another user, visited nodes are not flagged as new.');
  122. }
  123. /**
  124. * Tests for comment counters on the tracker listing.
  125. */
  126. function testTrackerNewComments() {
  127. $this->drupalLogin($this->user);
  128. $node = $this->drupalCreateNode(array(
  129. 'comment' => 2,
  130. 'title' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(8)))),
  131. ));
  132. // Add a comment to the page.
  133. $comment = array(
  134. 'subject' => $this->randomName(),
  135. 'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
  136. );
  137. // The new comment is automatically viewed by the current user.
  138. $this->drupalPost('comment/reply/' . $node->nid, $comment, t('Save'));
  139. $this->drupalLogin($this->other_user);
  140. $this->drupalGet('tracker');
  141. $this->assertText('1 new', 'New comments are counted on the tracker listing pages.');
  142. $this->drupalGet('node/' . $node->nid);
  143. // Add another comment as other_user.
  144. $comment = array(
  145. 'subject' => $this->randomName(),
  146. 'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
  147. );
  148. // If the comment is posted in the same second as the last one then Drupal
  149. // can't tell the difference, so we wait one second here.
  150. sleep(1);
  151. $this->drupalPost('comment/reply/' . $node->nid, $comment, t('Save'));
  152. $this->drupalLogin($this->user);
  153. $this->drupalGet('tracker');
  154. $this->assertText('1 new', 'New comments are counted on the tracker listing pages.');
  155. }
  156. /**
  157. * Tests that existing nodes are indexed by cron.
  158. */
  159. function testTrackerCronIndexing() {
  160. $this->drupalLogin($this->user);
  161. // Create 3 nodes.
  162. $edits = array();
  163. $nodes = array();
  164. for ($i = 1; $i <= 3; $i++) {
  165. $edits[$i] = array(
  166. 'comment' => 2,
  167. 'title' => $this->randomName(),
  168. );
  169. $nodes[$i] = $this->drupalCreateNode($edits[$i]);
  170. }
  171. // Add a comment to the last node as other user.
  172. $this->drupalLogin($this->other_user);
  173. $comment = array(
  174. 'subject' => $this->randomName(),
  175. 'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
  176. );
  177. $this->drupalPost('comment/reply/' . $nodes[3]->nid, $comment, t('Save'));
  178. // Start indexing backwards from node 3.
  179. variable_set('tracker_index_nid', 3);
  180. // Clear the current tracker tables and rebuild them.
  181. db_delete('tracker_node')
  182. ->execute();
  183. db_delete('tracker_user')
  184. ->execute();
  185. tracker_cron();
  186. $this->drupalLogin($this->user);
  187. // Fetch the user's tracker.
  188. $this->drupalGet('tracker/' . $this->user->uid);
  189. // Assert that all node titles are displayed.
  190. foreach ($nodes as $i => $node) {
  191. $this->assertText($node->title, format_string('Node @i is displayed on the tracker listing pages.', array('@i' => $i)));
  192. }
  193. $this->assertText('1 new', 'New comment is counted on the tracker listing pages.');
  194. $this->assertText('updated', 'Node is listed as updated');
  195. // Fetch the site-wide tracker.
  196. $this->drupalGet('tracker');
  197. // Assert that all node titles are displayed.
  198. foreach ($nodes as $i => $node) {
  199. $this->assertText($node->title, format_string('Node @i is displayed on the tracker listing pages.', array('@i' => $i)));
  200. }
  201. $this->assertText('1 new', 'New comment is counted on the tracker listing pages.');
  202. }
  203. /**
  204. * Tests that publish/unpublish works at admin/content/node.
  205. */
  206. function testTrackerAdminUnpublish() {
  207. $admin_user = $this->drupalCreateUser(array('access content overview', 'administer nodes', 'bypass node access'));
  208. $this->drupalLogin($admin_user);
  209. $node = $this->drupalCreateNode(array(
  210. 'comment' => 2,
  211. 'title' => $this->randomName(),
  212. ));
  213. // Assert that the node is displayed.
  214. $this->drupalGet('tracker');
  215. $this->assertText($node->title, 'Node is displayed on the tracker listing pages.');
  216. // Unpublish the node and ensure that it's no longer displayed.
  217. $edit = array(
  218. 'operation' => 'unpublish',
  219. 'nodes[' . $node->nid . ']' => $node->nid,
  220. );
  221. $this->drupalPost('admin/content', $edit, t('Update'));
  222. $this->drupalGet('tracker');
  223. $this->assertText(t('No content available.'), 'Node is displayed on the tracker listing pages.');
  224. }
  225. }