tracker.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. ));
  131. // Add a comment to the page.
  132. $comment = array(
  133. 'subject' => $this->randomName(),
  134. 'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
  135. );
  136. // The new comment is automatically viewed by the current user.
  137. $this->drupalPost('comment/reply/' . $node->nid, $comment, t('Save'));
  138. $this->drupalLogin($this->other_user);
  139. $this->drupalGet('tracker');
  140. $this->assertText('1 new', 'New comments are counted on the tracker listing pages.');
  141. $this->drupalGet('node/' . $node->nid);
  142. // Add another comment as other_user.
  143. $comment = array(
  144. 'subject' => $this->randomName(),
  145. 'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
  146. );
  147. // If the comment is posted in the same second as the last one then Drupal
  148. // can't tell the difference, so we wait one second here.
  149. sleep(1);
  150. $this->drupalPost('comment/reply/' . $node->nid, $comment, t('Save'));
  151. $this->drupalLogin($this->user);
  152. $this->drupalGet('tracker');
  153. $this->assertText('1 new', 'New comments are counted on the tracker listing pages.');
  154. }
  155. /**
  156. * Tests for ordering on a users tracker listing when comments are posted.
  157. */
  158. function testTrackerOrderingNewComments() {
  159. $this->drupalLogin($this->user);
  160. $node_one = $this->drupalCreateNode(array(
  161. 'title' => $this->randomName(8),
  162. ));
  163. $node_two = $this->drupalCreateNode(array(
  164. 'title' => $this->randomName(8),
  165. ));
  166. // Now get other_user to track these pieces of content.
  167. $this->drupalLogin($this->other_user);
  168. // Add a comment to the first page.
  169. $comment = array(
  170. 'subject' => $this->randomName(),
  171. 'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
  172. );
  173. $this->drupalPost('comment/reply/' . $node_one->nid, $comment, t('Save'));
  174. // If the comment is posted in the same second as the last one then Drupal
  175. // can't tell the difference, so we wait one second here.
  176. sleep(1);
  177. // Add a comment to the second page.
  178. $comment = array(
  179. 'subject' => $this->randomName(),
  180. 'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
  181. );
  182. $this->drupalPost('comment/reply/' . $node_two->nid, $comment, t('Save'));
  183. // We should at this point have in our tracker for other_user:
  184. // 1. node_two
  185. // 2. node_one
  186. // Because that's the reverse order of the posted comments.
  187. // Now we're going to post a comment to node_one which should jump it to the
  188. // top of the list.
  189. $this->drupalLogin($this->user);
  190. // If the comment is posted in the same second as the last one then Drupal
  191. // can't tell the difference, so we wait one second here.
  192. sleep(1);
  193. // Add a comment to the second page.
  194. $comment = array(
  195. 'subject' => $this->randomName(),
  196. 'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
  197. );
  198. $this->drupalPost('comment/reply/' . $node_one->nid, $comment, t('Save'));
  199. // Switch back to the other_user and assert that the order has swapped.
  200. $this->drupalLogin($this->other_user);
  201. $this->drupalGet('user/' . $this->other_user->uid . '/track');
  202. // This is a cheeky way of asserting that the nodes are in the right order
  203. // on the tracker page.
  204. // It's almost certainly too brittle.
  205. $pattern = '/' . preg_quote($node_one->title) . '.+' . preg_quote($node_two->title) . '/s';
  206. $this->verbose($pattern);
  207. $this->assertPattern($pattern, 'Most recently commented on node appears at the top of tracker');
  208. }
  209. /**
  210. * Tests that existing nodes are indexed by cron.
  211. */
  212. function testTrackerCronIndexing() {
  213. $this->drupalLogin($this->user);
  214. // Create 3 nodes.
  215. $edits = array();
  216. $nodes = array();
  217. for ($i = 1; $i <= 3; $i++) {
  218. $edits[$i] = array(
  219. 'comment' => 2,
  220. 'title' => $this->randomName(),
  221. );
  222. $nodes[$i] = $this->drupalCreateNode($edits[$i]);
  223. }
  224. // Add a comment to the last node as other user.
  225. $this->drupalLogin($this->other_user);
  226. $comment = array(
  227. 'subject' => $this->randomName(),
  228. 'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
  229. );
  230. $this->drupalPost('comment/reply/' . $nodes[3]->nid, $comment, t('Save'));
  231. // Start indexing backwards from node 3.
  232. variable_set('tracker_index_nid', 3);
  233. // Clear the current tracker tables and rebuild them.
  234. db_delete('tracker_node')
  235. ->execute();
  236. db_delete('tracker_user')
  237. ->execute();
  238. tracker_cron();
  239. $this->drupalLogin($this->user);
  240. // Fetch the user's tracker.
  241. $this->drupalGet('tracker/' . $this->user->uid);
  242. // Assert that all node titles are displayed.
  243. foreach ($nodes as $i => $node) {
  244. $this->assertText($node->title, format_string('Node @i is displayed on the tracker listing pages.', array('@i' => $i)));
  245. }
  246. $this->assertText('1 new', 'New comment is counted on the tracker listing pages.');
  247. $this->assertText('updated', 'Node is listed as updated');
  248. // Fetch the site-wide tracker.
  249. $this->drupalGet('tracker');
  250. // Assert that all node titles are displayed.
  251. foreach ($nodes as $i => $node) {
  252. $this->assertText($node->title, format_string('Node @i is displayed on the tracker listing pages.', array('@i' => $i)));
  253. }
  254. $this->assertText('1 new', 'New comment is counted on the tracker listing pages.');
  255. }
  256. /**
  257. * Tests that publish/unpublish works at admin/content/node.
  258. */
  259. function testTrackerAdminUnpublish() {
  260. $admin_user = $this->drupalCreateUser(array('access content overview', 'administer nodes', 'bypass node access'));
  261. $this->drupalLogin($admin_user);
  262. $node = $this->drupalCreateNode(array(
  263. 'comment' => 2,
  264. 'title' => $this->randomName(),
  265. ));
  266. // Assert that the node is displayed.
  267. $this->drupalGet('tracker');
  268. $this->assertText($node->title, 'Node is displayed on the tracker listing pages.');
  269. // Unpublish the node and ensure that it's no longer displayed.
  270. $edit = array(
  271. 'operation' => 'unpublish',
  272. 'nodes[' . $node->nid . ']' => $node->nid,
  273. );
  274. $this->drupalPost('admin/content', $edit, t('Update'));
  275. $this->drupalGet('tracker');
  276. $this->assertText(t('No content available.'), 'Node is displayed on the tracker listing pages.');
  277. }
  278. }