cms/drupal/modules/tracker/tracker.test
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Tests for tracker.module.
       
     6  */
       
     7 
       
     8 /**
       
     9  * Defines a base class for testing tracker.module.
       
    10  */
       
    11 class TrackerTest extends DrupalWebTestCase {
       
    12 
       
    13   /**
       
    14    * The main user for testing.
       
    15    *
       
    16    * @var object
       
    17    */
       
    18   protected $user;
       
    19 
       
    20   /**
       
    21    * A second user that will 'create' comments and nodes.
       
    22    *
       
    23    * @var object
       
    24    */
       
    25   protected $other_user;
       
    26 
       
    27   public static function getInfo() {
       
    28     return array(
       
    29       'name' => 'Tracker',
       
    30       'description' => 'Create and delete nodes and check for their display in the tracker listings.',
       
    31       'group' => 'Tracker'
       
    32     );
       
    33   }
       
    34 
       
    35   function setUp() {
       
    36     parent::setUp('comment', 'tracker');
       
    37 
       
    38     $permissions = array('access comments', 'create page content', 'post comments', 'skip comment approval');
       
    39     $this->user = $this->drupalCreateUser($permissions);
       
    40     $this->other_user = $this->drupalCreateUser($permissions);
       
    41 
       
    42     // Make node preview optional.
       
    43     variable_set('comment_preview_page', 0);
       
    44   }
       
    45 
       
    46   /**
       
    47    * Tests for the presence of nodes on the global tracker listing.
       
    48    */
       
    49   function testTrackerAll() {
       
    50     $this->drupalLogin($this->user);
       
    51 
       
    52     $unpublished = $this->drupalCreateNode(array(
       
    53       'title' => $this->randomName(8),
       
    54       'status' => 0,
       
    55     ));
       
    56     $published = $this->drupalCreateNode(array(
       
    57       'title' => $this->randomName(8),
       
    58       'status' => 1,
       
    59     ));
       
    60 
       
    61     $this->drupalGet('tracker');
       
    62     $this->assertNoText($unpublished->title, 'Unpublished node do not show up in the tracker listing.');
       
    63     $this->assertText($published->title, 'Published node show up in the tracker listing.');
       
    64     $this->assertLink(t('My recent content'), 0, 'User tab shows up on the global tracker page.');
       
    65 
       
    66     // Delete a node and ensure it no longer appears on the tracker.
       
    67     node_delete($published->nid);
       
    68     $this->drupalGet('tracker');
       
    69     $this->assertNoText($published->title, 'Deleted node do not show up in the tracker listing.');
       
    70   }
       
    71 
       
    72   /**
       
    73    * Tests for the presence of nodes on a user's tracker listing.
       
    74    */
       
    75   function testTrackerUser() {
       
    76     $this->drupalLogin($this->user);
       
    77 
       
    78     $unpublished = $this->drupalCreateNode(array(
       
    79       'title' => $this->randomName(8),
       
    80       'uid' => $this->user->uid,
       
    81       'status' => 0,
       
    82     ));
       
    83     $my_published = $this->drupalCreateNode(array(
       
    84       'title' => $this->randomName(8),
       
    85       'uid' => $this->user->uid,
       
    86       'status' => 1,
       
    87     ));
       
    88     $other_published_no_comment = $this->drupalCreateNode(array(
       
    89       'title' => $this->randomName(8),
       
    90       'uid' => $this->other_user->uid,
       
    91       'status' => 1,
       
    92     ));
       
    93     $other_published_my_comment = $this->drupalCreateNode(array(
       
    94       'title' => $this->randomName(8),
       
    95       'uid' => $this->other_user->uid,
       
    96       'status' => 1,
       
    97     ));
       
    98     $comment = array(
       
    99       'subject' => $this->randomName(),
       
   100       'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
       
   101     );
       
   102     $this->drupalPost('comment/reply/' . $other_published_my_comment->nid, $comment, t('Save'));
       
   103 
       
   104     $this->drupalGet('user/' . $this->user->uid . '/track');
       
   105     $this->assertNoText($unpublished->title, "Unpublished nodes do not show up in the users's tracker listing.");
       
   106     $this->assertText($my_published->title, "Published nodes show up in the user's tracker listing.");
       
   107     $this->assertNoText($other_published_no_comment->title, "Other user's nodes do not show up in the user's tracker listing.");
       
   108     $this->assertText($other_published_my_comment->title, "Nodes that the user has commented on appear in the user's tracker listing.");
       
   109 
       
   110     // Verify that unpublished comments are removed from the tracker.
       
   111     $admin_user = $this->drupalCreateUser(array('administer comments', 'access user profiles'));
       
   112     $this->drupalLogin($admin_user);
       
   113     $this->drupalPost('comment/1/edit', array('status' => COMMENT_NOT_PUBLISHED), t('Save'));
       
   114     $this->drupalGet('user/' . $this->user->uid . '/track');
       
   115     $this->assertNoText($other_published_my_comment->title, 'Unpublished comments are not counted on the tracker listing.');
       
   116   }
       
   117 
       
   118   /**
       
   119    * Tests for the presence of the "new" flag for nodes.
       
   120    */
       
   121   function testTrackerNewNodes() {
       
   122     $this->drupalLogin($this->user);
       
   123 
       
   124     $edit = array(
       
   125       'title' => $this->randomName(8),
       
   126     );
       
   127 
       
   128     $node = $this->drupalCreateNode($edit);
       
   129     $title = $edit['title'];
       
   130     $this->drupalGet('tracker');
       
   131     $this->assertPattern('/' . $title . '.*new/', 'New nodes are flagged as such in the tracker listing.');
       
   132 
       
   133     $this->drupalGet('node/' . $node->nid);
       
   134     $this->drupalGet('tracker');
       
   135     $this->assertNoPattern('/' . $title . '.*new/', 'Visited nodes are not flagged as new.');
       
   136 
       
   137     $this->drupalLogin($this->other_user);
       
   138     $this->drupalGet('tracker');
       
   139     $this->assertPattern('/' . $title . '.*new/', 'For another user, new nodes are flagged as such in the tracker listing.');
       
   140 
       
   141     $this->drupalGet('node/' . $node->nid);
       
   142     $this->drupalGet('tracker');
       
   143     $this->assertNoPattern('/' . $title . '.*new/', 'For another user, visited nodes are not flagged as new.');
       
   144   }
       
   145 
       
   146   /**
       
   147    * Tests for comment counters on the tracker listing.
       
   148    */
       
   149   function testTrackerNewComments() {
       
   150     $this->drupalLogin($this->user);
       
   151 
       
   152     $node = $this->drupalCreateNode(array(
       
   153       'comment' => 2,
       
   154     ));
       
   155 
       
   156     // Add a comment to the page.
       
   157     $comment = array(
       
   158       'subject' => $this->randomName(),
       
   159       'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
       
   160     );
       
   161     // The new comment is automatically viewed by the current user.
       
   162     $this->drupalPost('comment/reply/' . $node->nid, $comment, t('Save'));
       
   163 
       
   164     $this->drupalLogin($this->other_user);
       
   165     $this->drupalGet('tracker');
       
   166     $this->assertText('1 new', 'New comments are counted on the tracker listing pages.');
       
   167     $this->drupalGet('node/' . $node->nid);
       
   168 
       
   169     // Add another comment as other_user.
       
   170     $comment = array(
       
   171       'subject' => $this->randomName(),
       
   172       'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
       
   173     );
       
   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     $this->drupalPost('comment/reply/' . $node->nid, $comment, t('Save'));
       
   178 
       
   179     $this->drupalLogin($this->user);
       
   180     $this->drupalGet('tracker');
       
   181     $this->assertText('1 new', 'New comments are counted on the tracker listing pages.');
       
   182   }
       
   183 
       
   184   /**
       
   185    * Tests for ordering on a users tracker listing when comments are posted.
       
   186    */
       
   187   function testTrackerOrderingNewComments() {
       
   188     $this->drupalLogin($this->user);
       
   189 
       
   190     $node_one = $this->drupalCreateNode(array(
       
   191       'title' => $this->randomName(8),
       
   192     ));
       
   193 
       
   194     $node_two = $this->drupalCreateNode(array(
       
   195       'title' => $this->randomName(8),
       
   196     ));
       
   197 
       
   198     // Now get other_user to track these pieces of content.
       
   199     $this->drupalLogin($this->other_user);
       
   200 
       
   201     // Add a comment to the first page.
       
   202     $comment = array(
       
   203       'subject' => $this->randomName(),
       
   204       'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
       
   205     );
       
   206     $this->drupalPost('comment/reply/' . $node_one->nid, $comment, t('Save'));
       
   207 
       
   208     // If the comment is posted in the same second as the last one then Drupal
       
   209     // can't tell the difference, so we wait one second here.
       
   210     sleep(1);
       
   211 
       
   212     // Add a comment to the second page.
       
   213     $comment = array(
       
   214       'subject' => $this->randomName(),
       
   215       'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
       
   216     );
       
   217     $this->drupalPost('comment/reply/' . $node_two->nid, $comment, t('Save'));
       
   218 
       
   219     // We should at this point have in our tracker for other_user:
       
   220     // 1. node_two
       
   221     // 2. node_one
       
   222     // Because that's the reverse order of the posted comments.
       
   223 
       
   224     // Now we're going to post a comment to node_one which should jump it to the
       
   225     // top of the list.
       
   226 
       
   227     $this->drupalLogin($this->user);
       
   228     // If the comment is posted in the same second as the last one then Drupal
       
   229     // can't tell the difference, so we wait one second here.
       
   230     sleep(1);
       
   231 
       
   232     // Add a comment to the second page.
       
   233     $comment = array(
       
   234       'subject' => $this->randomName(),
       
   235       'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
       
   236     );
       
   237     $this->drupalPost('comment/reply/' . $node_one->nid, $comment, t('Save'));
       
   238 
       
   239     // Switch back to the other_user and assert that the order has swapped.
       
   240     $this->drupalLogin($this->other_user);
       
   241     $this->drupalGet('user/' . $this->other_user->uid . '/track');
       
   242     // This is a cheeky way of asserting that the nodes are in the right order
       
   243     // on the tracker page.
       
   244     // It's almost certainly too brittle.
       
   245     $pattern = '/' . preg_quote($node_one->title) . '.+' . preg_quote($node_two->title) . '/s';
       
   246     $this->verbose($pattern);
       
   247     $this->assertPattern($pattern, 'Most recently commented on node appears at the top of tracker');
       
   248   }
       
   249 
       
   250   /**
       
   251    * Tests that existing nodes are indexed by cron.
       
   252    */
       
   253   function testTrackerCronIndexing() {
       
   254     $this->drupalLogin($this->user);
       
   255 
       
   256     // Create 3 nodes.
       
   257     $edits = array();
       
   258     $nodes = array();
       
   259     for ($i = 1; $i <= 3; $i++) {
       
   260       $edits[$i] = array(
       
   261         'comment' => 2,
       
   262         'title' => $this->randomName(),
       
   263       );
       
   264       $nodes[$i] = $this->drupalCreateNode($edits[$i]);
       
   265     }
       
   266 
       
   267     // Add a comment to the last node as other user.
       
   268     $this->drupalLogin($this->other_user);
       
   269     $comment = array(
       
   270       'subject' => $this->randomName(),
       
   271       'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(20),
       
   272     );
       
   273     $this->drupalPost('comment/reply/' . $nodes[3]->nid, $comment, t('Save'));
       
   274 
       
   275     // Start indexing backwards from node 3.
       
   276     variable_set('tracker_index_nid', 3);
       
   277 
       
   278     // Clear the current tracker tables and rebuild them.
       
   279     db_delete('tracker_node')
       
   280       ->execute();
       
   281     db_delete('tracker_user')
       
   282       ->execute();
       
   283     tracker_cron();
       
   284 
       
   285     $this->drupalLogin($this->user);
       
   286 
       
   287     // Fetch the user's tracker.
       
   288     $this->drupalGet('tracker/' . $this->user->uid);
       
   289 
       
   290     // Assert that all node titles are displayed.
       
   291     foreach ($nodes as $i => $node) {
       
   292       $this->assertText($node->title, format_string('Node @i is displayed on the tracker listing pages.', array('@i' => $i)));
       
   293     }
       
   294     $this->assertText('1 new', 'New comment is counted on the tracker listing pages.');
       
   295     $this->assertText('updated', 'Node is listed as updated');
       
   296 
       
   297     // Fetch the site-wide tracker.
       
   298     $this->drupalGet('tracker');
       
   299 
       
   300     // Assert that all node titles are displayed.
       
   301     foreach ($nodes as $i => $node) {
       
   302       $this->assertText($node->title, format_string('Node @i is displayed on the tracker listing pages.', array('@i' => $i)));
       
   303     }
       
   304     $this->assertText('1 new', 'New comment is counted on the tracker listing pages.');
       
   305   }
       
   306 
       
   307   /**
       
   308    * Tests that publish/unpublish works at admin/content/node.
       
   309    */
       
   310   function testTrackerAdminUnpublish() {
       
   311     $admin_user = $this->drupalCreateUser(array('access content overview', 'administer nodes', 'bypass node access'));
       
   312     $this->drupalLogin($admin_user);
       
   313 
       
   314     $node = $this->drupalCreateNode(array(
       
   315       'comment' => 2,
       
   316       'title' => $this->randomName(),
       
   317     ));
       
   318 
       
   319     // Assert that the node is displayed.
       
   320     $this->drupalGet('tracker');
       
   321     $this->assertText($node->title, 'Node is displayed on the tracker listing pages.');
       
   322 
       
   323     // Unpublish the node and ensure that it's no longer displayed.
       
   324     $edit = array(
       
   325       'operation' => 'unpublish',
       
   326       'nodes[' . $node->nid . ']' => $node->nid,
       
   327     );
       
   328     $this->drupalPost('admin/content', $edit, t('Update'));
       
   329 
       
   330     $this->drupalGet('tracker');
       
   331     $this->assertText(t('No content available.'), 'Node is displayed on the tracker listing pages.');
       
   332   }
       
   333 }