tracker.test 11 KB

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