You are here

public function TrackerTest::testTrackerOrderingNewComments in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/tracker/tests/src/Functional/TrackerTest.php \Drupal\Tests\tracker\Functional\TrackerTest::testTrackerOrderingNewComments()

Tests for ordering on a users tracker listing when comments are posted.

File

core/modules/tracker/tests/src/Functional/TrackerTest.php, line 296

Class

TrackerTest
Create and delete nodes and check for their display in the tracker listings.

Namespace

Drupal\Tests\tracker\Functional

Code

public function testTrackerOrderingNewComments() {
  $this
    ->drupalLogin($this->user);
  $node_one = $this
    ->drupalCreateNode([
    'title' => $this
      ->randomMachineName(8),
  ]);
  $node_two = $this
    ->drupalCreateNode([
    'title' => $this
      ->randomMachineName(8),
  ]);

  // Now get otherUser to track these pieces of content.
  $this
    ->drupalLogin($this->otherUser);

  // Add a comment to the first page.
  $comment = [
    'subject[0][value]' => $this
      ->randomMachineName(),
    'comment_body[0][value]' => $this
      ->randomMachineName(20),
  ];
  $this
    ->drupalPostForm('comment/reply/node/' . $node_one
    ->id() . '/comment', $comment, t('Save'));

  // If the comment is posted in the same second as the last one then Drupal
  // can't tell the difference, so we wait one second here.
  sleep(1);

  // Add a comment to the second page.
  $comment = [
    'subject[0][value]' => $this
      ->randomMachineName(),
    'comment_body[0][value]' => $this
      ->randomMachineName(20),
  ];
  $this
    ->drupalPostForm('comment/reply/node/' . $node_two
    ->id() . '/comment', $comment, t('Save'));

  // We should at this point have in our tracker for otherUser:
  // 1. node_two
  // 2. node_one
  // Because that's the reverse order of the posted comments.
  // Now we're going to post a comment to node_one which should jump it to the
  // top of the list.
  $this
    ->drupalLogin($this->user);

  // If the comment is posted in the same second as the last one then Drupal
  // can't tell the difference, so we wait one second here.
  sleep(1);

  // Add a comment to the second page.
  $comment = [
    'subject[0][value]' => $this
      ->randomMachineName(),
    'comment_body[0][value]' => $this
      ->randomMachineName(20),
  ];
  $this
    ->drupalPostForm('comment/reply/node/' . $node_one
    ->id() . '/comment', $comment, t('Save'));

  // Switch back to the otherUser and assert that the order has swapped.
  $this
    ->drupalLogin($this->otherUser);
  $this
    ->drupalGet('user/' . $this->otherUser
    ->id() . '/activity');

  // This is a cheeky way of asserting that the nodes are in the right order
  // on the tracker page.
  // It's almost certainly too brittle.
  $pattern = '/' . preg_quote($node_one
    ->getTitle()) . '.+' . preg_quote($node_two
    ->getTitle()) . '/s';
  $this
    ->verbose($pattern);

  // Verify that the most recent comment on node appears at the top of
  // tracker.
  $this
    ->assertPattern($pattern);
}