diff --git a/core/modules/tracker/tests/src/Functional/TrackerCommentTest.php b/core/modules/tracker/tests/src/Functional/TrackerCommentTest.php index 2a90fd1..6b6394c 100644 --- a/core/modules/tracker/tests/src/Functional/TrackerCommentTest.php +++ b/core/modules/tracker/tests/src/Functional/TrackerCommentTest.php @@ -66,12 +66,12 @@ public function testTrackerAll() { public function testTrackerUser() { parent::testTrackerUser(); - $other_published_no_comment = $this->drupalCreateNode([ + $this->nodes['other_published_no_comment'] = $this->drupalCreateNode([ 'title' => $this->randomMachineName(8), 'uid' => $this->otherUser->id(), 'status' => 1, ]); - $other_published_my_comment = $this->drupalCreateNode([ + $this->nodes['other_published_my_comment'] = $this->drupalCreateNode([ 'title' => $this->randomMachineName(8), 'uid' => $this->otherUser->id(), 'status' => 1, @@ -80,19 +80,19 @@ public function testTrackerUser() { 'subject[0][value]' => $this->randomMachineName(), 'comment_body[0][value]' => $this->randomMachineName(20), ]; - $this->drupalPostForm('comment/reply/node/' . $other_published_my_comment->id() . '/comment', $comment, t('Save')); + $this->drupalPostForm('comment/reply/node/' . $this->nodes['other_published_my_comment']->id() . '/comment', $comment, t('Save')); $this->drupalGet('user/' . $this->user->id() . '/activity'); - $this->assertNoText($other_published_no_comment->label(), "Another user's nodes do not show up in the user's tracker listing."); - $this->assertText($other_published_my_comment->label(), "Nodes that the user has commented on appear in the user's tracker listing."); + $this->assertNoText($this->nodes['other_published_no_comment']->label(), "Another user's nodes do not show up in the user's tracker listing."); + $this->assertText($this->nodes['other_published_my_comment']->label(), "Nodes that the user has commented on appear in the user's tracker listing."); // Assert cache contexts. $this->assertCacheContexts(['languages:language_interface', 'route', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'url.query_args.pagers:0', 'user', 'user.node_grants:view']); // Assert cache tags for the visible nodes (including owners) and node list // cache tag. - $expected_tags = Cache::mergeTags($my_published->getCacheTags(), $my_published->getOwner()->getCacheTags()); - $expected_tags = Cache::mergeTags($expected_tags, $other_published_my_comment->getCacheTags()); - $expected_tags = Cache::mergeTags($expected_tags, $other_published_my_comment->getOwner()->getCacheTags()); + $expected_tags = Cache::mergeTags($this->nodes['my_published']->getCacheTags(), $my_published->getOwner()->getCacheTags()); + $expected_tags = Cache::mergeTags($expected_tags, $this->nodes['other_published_my_comment']->getCacheTags()); + $expected_tags = Cache::mergeTags($expected_tags, $this->nodes['other_published_my_comment']->getOwner()->getCacheTags()); // Because the 'user.permissions' cache context is being optimized away. $role_tags = []; foreach ($this->user->getRoles() as $rid) { @@ -119,14 +119,14 @@ public function testTrackerUser() { $this->drupalLogin($admin_user); $this->drupalPostForm('comment/1/edit', ['status' => CommentInterface::NOT_PUBLISHED], t('Save')); $this->drupalGet('user/' . $this->user->id() . '/activity'); - $this->assertNoText($other_published_my_comment->label(), 'Unpublished comments are not counted on the tracker listing.'); + $this->assertNoText($this->nodes['other_published_my_comment']->label(), 'Unpublished comments are not counted on the tracker listing.'); } /** * Tests the metadata for the "new"/"updated" indicators. */ public function testTrackerHistoryMetadata() { - $node = parent::testTrackerHistoryMetadata(); + parent::testTrackerHistoryMetadata(); // Add a comment to the page, make sure it is created after the node by // sleeping for one second, to ensure the last comment timestamp is @@ -136,11 +136,11 @@ public function testTrackerHistoryMetadata() { 'comment_body[0][value]' => $this->randomMachineName(20), ]; sleep(1); - $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $comment, t('Save')); + $this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $comment, t('Save')); // Reload the node so that comment.module's hook_node_load() // implementation can set $node->last_comment_timestamp for the freshly // posted comment. - $node = Node::load($node->id()); + $node = Node::load($this->node->id()); // Verify that the history metadata is updated. $this->drupalGet('activity'); @@ -249,33 +249,8 @@ public function testTrackerCronIndexing() { ]; $this->drupalPostForm('comment/reply/node/' . $nodes[3]->id() . '/comment', $comment, t('Save')); - // Start indexing backwards from node 3. - \Drupal::state()->set('tracker.index_nid', 3); - - // Clear the current tracker tables and rebuild them. - db_delete('tracker_node') - ->execute(); - db_delete('tracker_user') - ->execute(); - tracker_cron(); - - $this->drupalLogin($this->user); - - // Fetch the user's tracker. - $this->drupalGet('activity/' . $this->user->id()); - - // Assert that all node titles are displayed. - foreach ($nodes as $i => $node) { - $this->assertText($node->label(), format_string('Node @i is displayed on the tracker listing pages.', ['@i' => $i])); - } - - // Fetch the site-wide tracker. - $this->drupalGet('activity'); - - // Assert that all node titles are displayed. - foreach ($nodes as $i => $node) { - $this->assertText($node->label(), format_string('Node @i is displayed on the tracker listing pages.', ['@i' => $i])); - } + // Run the tests. + $this->doTestTrackerCronIndexing($nodes); } /** diff --git a/core/modules/tracker/tests/src/Functional/TrackerTest.php b/core/modules/tracker/tests/src/Functional/TrackerTest.php index 333a872..dd2fe27 100644 --- a/core/modules/tracker/tests/src/Functional/TrackerTest.php +++ b/core/modules/tracker/tests/src/Functional/TrackerTest.php @@ -117,26 +117,27 @@ public function testTrackerAll() { public function testTrackerUser() { $this->drupalLogin($this->user); - $unpublished = $this->drupalCreateNode([ + $this->nodes = []; + $this->nodes['unpublished'] = $this->drupalCreateNode([ 'title' => $this->randomMachineName(8), 'uid' => $this->user->id(), 'status' => 0, ]); - $my_published = $this->drupalCreateNode([ + $this->nodes['my_published'] = $this->drupalCreateNode([ 'title' => $this->randomMachineName(8), 'uid' => $this->user->id(), 'status' => 1, ]); $this->drupalGet('user/' . $this->user->id() . '/activity'); - $this->assertNoText($unpublished->label(), "Unpublished nodes do not show up in the user's tracker listing."); - $this->assertText($my_published->label(), "Published nodes show up in the user's tracker listing."); + $this->assertNoText($this->nodes['unpublished']->label(), "Unpublished nodes do not show up in the user's tracker listing."); + $this->assertText($this->nodes['my_published']->label(), "Published nodes show up in the user's tracker listing."); // Assert cache contexts. $this->assertCacheContexts(['languages:language_interface', 'route', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'url.query_args.pagers:0', 'user', 'user.node_grants:view']); // Assert cache tags for the visible nodes (including owners) and node list // cache tag. - $expected_tags = Cache::mergeTags($my_published->getCacheTags(), $my_published->getOwner()->getCacheTags()); + $expected_tags = Cache::mergeTags($this->nodes['my_published']->getCacheTags(), $this->nodes['my_published']->getOwner()->getCacheTags()); // Because the 'user.permissions' cache context is being optimized away. $role_tags = []; foreach ($this->user->getRoles() as $rid) { @@ -159,8 +160,8 @@ public function testTrackerUser() { $this->assertCacheTags($expected_tags); $this->assertCacheContexts(['languages:language_interface', 'route', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'url.query_args.pagers:0', 'user', 'user.node_grants:view']); - $this->assertLink($my_published->label()); - $this->assertNoLink($unpublished->label()); + $this->assertLink($this->nodes['my_published']->label()); + $this->assertNoLink($this->nodes['unpublished']->label()); // Verify that title and tab title have been set correctly. $this->assertText('Activity', 'The user activity tab has the name "Activity".'); $this->assertTitle(t('@name | @site', ['@name' => $this->user->getUsername(), '@site' => $this->config('system.site')->get('name')]), 'The user tracker page has the correct page title.'); @@ -189,17 +190,15 @@ public function testTrackerHistoryMetadata() { $edit = [ 'title' => $this->randomMachineName(8), ]; - $node = $this->drupalCreateNode($edit); + $this->node = $this->drupalCreateNode($edit); // Verify that the history metadata is present. $this->drupalGet('activity'); - $this->assertHistoryMetadata($node->id(), $node->getChangedTime()); + $this->assertHistoryMetadata($this->node->id(), $this->node->getChangedTime()); $this->drupalGet('activity/' . $this->user->id()); - $this->assertHistoryMetadata($node->id(), $node->getChangedTime()); + $this->assertHistoryMetadata($this->node->id(), $this->node->getChangedTime()); $this->drupalGet('user/' . $this->user->id() . '/activity'); - $this->assertHistoryMetadata($node->id(), $node->getChangedTime()); - - return $node; + $this->assertHistoryMetadata($this->node->id(), $this->node->getChangedTime()); } /** @@ -218,6 +217,21 @@ public function testTrackerCronIndexing() { $nodes[$i] = $this->drupalCreateNode($edits[$i]); } + // Run the tests. + $this->doTestTrackerCronIndexing($nodes); + } + + /** + * Runs assertions for testTrackerCronIndexing(). + * + * This has been moved into a separate method to ease tests of the submethod + * \Drupal\Tests\tracker\Functional\TrackerCommentTest::testTrackerCronIndexing() + * and reduce code duplication. + * + * @param \Drupal\node\Entity\Node[] $nodes + * The created nodes to test. + */ + protected function doTestTrackerCronIndexing(array $nodes) { // Start indexing backwards from node 3. \Drupal::state()->set('tracker.index_nid', 3);