Chris@0: drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); Chris@0: Chris@0: $permissions = ['access comments', 'create page content', 'post comments', 'skip comment approval']; Chris@0: $this->user = $this->drupalCreateUser($permissions); Chris@0: $this->otherUser = $this->drupalCreateUser($permissions); Chris@0: $this->addDefaultCommentField('node', 'page'); Chris@0: user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, [ Chris@0: 'access content', Chris@0: 'access user profiles', Chris@0: ]); Chris@0: $this->drupalPlaceBlock('local_tasks_block', ['id' => 'page_tabs_block']); Chris@0: $this->drupalPlaceBlock('local_actions_block', ['id' => 'page_actions_block']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests for the presence of nodes on the global tracker listing. Chris@0: */ Chris@0: public function testTrackerAll() { Chris@0: $this->drupalLogin($this->user); Chris@0: Chris@0: $unpublished = $this->drupalCreateNode([ Chris@0: 'title' => $this->randomMachineName(8), Chris@0: 'status' => 0, Chris@0: ]); Chris@0: $published = $this->drupalCreateNode([ Chris@0: 'title' => $this->randomMachineName(8), Chris@0: 'status' => 1, Chris@0: ]); Chris@0: Chris@0: $this->drupalGet('activity'); Chris@0: $this->assertNoText($unpublished->label(), 'Unpublished node does not show up in the tracker listing.'); Chris@0: $this->assertText($published->label(), 'Published node shows up in the tracker listing.'); Chris@0: $this->assertLink(t('My recent content'), 0, 'User tab shows up on the global tracker page.'); Chris@0: Chris@0: // Assert cache contexts, specifically the pager and node access contexts. Chris@0: $this->assertCacheContexts(['languages:language_interface', 'route', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'url.query_args.pagers:0', 'user.node_grants:view', 'user']); Chris@0: // Assert cache tags for the action/tabs blocks, visible node, and node list Chris@0: // cache tag. Chris@0: $expected_tags = Cache::mergeTags($published->getCacheTags(), $published->getOwner()->getCacheTags()); Chris@0: // Because the 'user.permissions' cache context is being optimized away. Chris@0: $role_tags = []; Chris@0: foreach ($this->user->getRoles() as $rid) { Chris@0: $role_tags[] = "config:user.role.$rid"; Chris@0: } Chris@0: $expected_tags = Cache::mergeTags($expected_tags, $role_tags); Chris@0: $block_tags = [ Chris@0: 'block_view', Chris@17: 'local_task', Chris@0: 'config:block.block.page_actions_block', Chris@0: 'config:block.block.page_tabs_block', Chris@0: 'config:block_list', Chris@0: ]; Chris@0: $expected_tags = Cache::mergeTags($expected_tags, $block_tags); Chris@0: $additional_tags = [ Chris@0: 'node_list', Chris@0: 'rendered', Chris@0: ]; Chris@0: $expected_tags = Cache::mergeTags($expected_tags, $additional_tags); Chris@0: $this->assertCacheTags($expected_tags); Chris@0: Chris@0: // Delete a node and ensure it no longer appears on the tracker. Chris@0: $published->delete(); Chris@0: $this->drupalGet('activity'); Chris@0: $this->assertNoText($published->label(), 'Deleted node does not show up in the tracker listing.'); Chris@0: Chris@0: // Test proper display of time on activity page when comments are disabled. Chris@0: // Disable comments. Chris@0: FieldStorageConfig::loadByName('node', 'comment')->delete(); Chris@0: $node = $this->drupalCreateNode([ Chris@0: // This title is required to trigger the custom changed time set in the Chris@0: // node_test module. This is needed in order to ensure a sufficiently Chris@0: // large 'time ago' interval that isn't numbered in seconds. Chris@0: 'title' => 'testing_node_presave', Chris@0: 'status' => 1, Chris@0: ]); Chris@0: Chris@0: $this->drupalGet('activity'); Chris@0: $this->assertText($node->label(), 'Published node shows up in the tracker listing.'); Chris@0: $this->assertText(\Drupal::service('date.formatter')->formatTimeDiffSince($node->getChangedTime()), 'The changed time was displayed on the tracker listing.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests for the presence of nodes on a user's tracker listing. Chris@0: */ Chris@0: public function testTrackerUser() { Chris@0: $this->drupalLogin($this->user); Chris@0: Chris@0: $unpublished = $this->drupalCreateNode([ Chris@0: 'title' => $this->randomMachineName(8), Chris@0: 'uid' => $this->user->id(), Chris@0: 'status' => 0, Chris@0: ]); Chris@0: $my_published = $this->drupalCreateNode([ Chris@0: 'title' => $this->randomMachineName(8), Chris@0: 'uid' => $this->user->id(), Chris@0: 'status' => 1, Chris@0: ]); Chris@0: $other_published_no_comment = $this->drupalCreateNode([ Chris@0: 'title' => $this->randomMachineName(8), Chris@0: 'uid' => $this->otherUser->id(), Chris@0: 'status' => 1, Chris@0: ]); Chris@0: $other_published_my_comment = $this->drupalCreateNode([ Chris@0: 'title' => $this->randomMachineName(8), Chris@0: 'uid' => $this->otherUser->id(), Chris@0: 'status' => 1, Chris@0: ]); Chris@0: $comment = [ Chris@0: 'subject[0][value]' => $this->randomMachineName(), Chris@0: 'comment_body[0][value]' => $this->randomMachineName(20), Chris@0: ]; Chris@0: $this->drupalPostForm('comment/reply/node/' . $other_published_my_comment->id() . '/comment', $comment, t('Save')); Chris@0: Chris@0: $this->drupalGet('user/' . $this->user->id() . '/activity'); Chris@0: $this->assertNoText($unpublished->label(), "Unpublished nodes do not show up in the user's tracker listing."); Chris@0: $this->assertText($my_published->label(), "Published nodes show up in the user's tracker listing."); Chris@0: $this->assertNoText($other_published_no_comment->label(), "Another user's nodes do not show up in the user's tracker listing."); Chris@0: $this->assertText($other_published_my_comment->label(), "Nodes that the user has commented on appear in the user's tracker listing."); Chris@0: Chris@0: // Assert cache contexts. Chris@0: $this->assertCacheContexts(['languages:language_interface', 'route', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'url.query_args.pagers:0', 'user', 'user.node_grants:view']); Chris@0: // Assert cache tags for the visible nodes (including owners) and node list Chris@0: // cache tag. Chris@0: $expected_tags = Cache::mergeTags($my_published->getCacheTags(), $my_published->getOwner()->getCacheTags()); Chris@0: $expected_tags = Cache::mergeTags($expected_tags, $other_published_my_comment->getCacheTags()); Chris@0: $expected_tags = Cache::mergeTags($expected_tags, $other_published_my_comment->getOwner()->getCacheTags()); Chris@0: // Because the 'user.permissions' cache context is being optimized away. Chris@0: $role_tags = []; Chris@0: foreach ($this->user->getRoles() as $rid) { Chris@0: $role_tags[] = "config:user.role.$rid"; Chris@0: } Chris@0: $expected_tags = Cache::mergeTags($expected_tags, $role_tags); Chris@0: $block_tags = [ Chris@0: 'block_view', Chris@17: 'local_task', Chris@0: 'config:block.block.page_actions_block', Chris@0: 'config:block.block.page_tabs_block', Chris@0: 'config:block_list', Chris@0: ]; Chris@0: $expected_tags = Cache::mergeTags($expected_tags, $block_tags); Chris@0: $additional_tags = [ Chris@0: 'node_list', Chris@0: 'rendered', Chris@0: ]; Chris@0: $expected_tags = Cache::mergeTags($expected_tags, $additional_tags); Chris@0: Chris@0: $this->assertCacheTags($expected_tags); Chris@0: $this->assertCacheContexts(['languages:language_interface', 'route', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'url.query_args.pagers:0', 'user', 'user.node_grants:view']); Chris@0: Chris@0: $this->assertLink($my_published->label()); Chris@0: $this->assertNoLink($unpublished->label()); Chris@0: // Verify that title and tab title have been set correctly. Chris@0: $this->assertText('Activity', 'The user activity tab has the name "Activity".'); Chris@18: $this->assertTitle(t('@name | @site', ['@name' => $this->user->getAccountName(), '@site' => $this->config('system.site')->get('name')]), 'The user tracker page has the correct page title.'); Chris@0: Chris@0: // Verify that unpublished comments are removed from the tracker. Chris@0: $admin_user = $this->drupalCreateUser(['post comments', 'administer comments', 'access user profiles']); Chris@0: $this->drupalLogin($admin_user); Chris@0: $this->drupalPostForm('comment/1/edit', ['status' => CommentInterface::NOT_PUBLISHED], t('Save')); Chris@0: $this->drupalGet('user/' . $this->user->id() . '/activity'); Chris@0: $this->assertNoText($other_published_my_comment->label(), 'Unpublished comments are not counted on the tracker listing.'); Chris@0: Chris@0: // Test escaping of title on user's tracker tab. Chris@0: \Drupal::service('module_installer')->install(['user_hooks_test']); Chris@0: Cache::invalidateTags(['rendered']); Chris@0: \Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE); Chris@0: $this->drupalGet('user/' . $this->user->id() . '/activity'); Chris@0: $this->assertEscaped('' . $this->user->id() . ''); Chris@0: Chris@0: \Drupal::state()->set('user_hooks_test_user_format_name_alter_safe', TRUE); Chris@0: Cache::invalidateTags(['rendered']); Chris@0: $this->drupalGet('user/' . $this->user->id() . '/activity'); Chris@0: $this->assertNoEscaped('' . $this->user->id() . ''); Chris@0: $this->assertRaw('' . $this->user->id() . ''); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the metadata for the "new"/"updated" indicators. Chris@0: */ Chris@0: public function testTrackerHistoryMetadata() { Chris@0: $this->drupalLogin($this->user); Chris@0: Chris@0: // Create a page node. Chris@0: $edit = [ Chris@0: 'title' => $this->randomMachineName(8), Chris@0: ]; Chris@0: $node = $this->drupalCreateNode($edit); Chris@0: Chris@0: // Verify that the history metadata is present. Chris@0: $this->drupalGet('activity'); Chris@0: $this->assertHistoryMetadata($node->id(), $node->getChangedTime(), $node->getChangedTime()); Chris@0: $this->drupalGet('activity/' . $this->user->id()); Chris@0: $this->assertHistoryMetadata($node->id(), $node->getChangedTime(), $node->getChangedTime()); Chris@0: $this->drupalGet('user/' . $this->user->id() . '/activity'); Chris@0: $this->assertHistoryMetadata($node->id(), $node->getChangedTime(), $node->getChangedTime()); Chris@0: Chris@0: // Add a comment to the page, make sure it is created after the node by Chris@0: // sleeping for one second, to ensure the last comment timestamp is Chris@0: // different from before. Chris@0: $comment = [ Chris@0: 'subject[0][value]' => $this->randomMachineName(), Chris@0: 'comment_body[0][value]' => $this->randomMachineName(20), Chris@0: ]; Chris@0: sleep(1); Chris@0: $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $comment, t('Save')); Chris@0: // Reload the node so that comment.module's hook_node_load() Chris@0: // implementation can set $node->last_comment_timestamp for the freshly Chris@0: // posted comment. Chris@0: $node = Node::load($node->id()); Chris@0: Chris@0: // Verify that the history metadata is updated. Chris@0: $this->drupalGet('activity'); Chris@0: $this->assertHistoryMetadata($node->id(), $node->getChangedTime(), $node->get('comment')->last_comment_timestamp); Chris@0: $this->drupalGet('activity/' . $this->user->id()); Chris@0: $this->assertHistoryMetadata($node->id(), $node->getChangedTime(), $node->get('comment')->last_comment_timestamp); Chris@0: $this->drupalGet('user/' . $this->user->id() . '/activity'); Chris@0: $this->assertHistoryMetadata($node->id(), $node->getChangedTime(), $node->get('comment')->last_comment_timestamp); Chris@0: Chris@0: // Log out, now verify that the metadata is still there, but the library is Chris@0: // not. Chris@0: $this->drupalLogout(); Chris@0: $this->drupalGet('activity'); Chris@0: $this->assertHistoryMetadata($node->id(), $node->getChangedTime(), $node->get('comment')->last_comment_timestamp, FALSE); Chris@0: $this->drupalGet('user/' . $this->user->id() . '/activity'); Chris@0: $this->assertHistoryMetadata($node->id(), $node->getChangedTime(), $node->get('comment')->last_comment_timestamp, FALSE); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests for ordering on a users tracker listing when comments are posted. Chris@0: */ Chris@0: public function testTrackerOrderingNewComments() { Chris@0: $this->drupalLogin($this->user); Chris@0: Chris@0: $node_one = $this->drupalCreateNode([ Chris@0: 'title' => $this->randomMachineName(8), Chris@0: ]); Chris@0: Chris@0: $node_two = $this->drupalCreateNode([ Chris@0: 'title' => $this->randomMachineName(8), Chris@0: ]); Chris@0: Chris@0: // Now get otherUser to track these pieces of content. Chris@0: $this->drupalLogin($this->otherUser); Chris@0: Chris@0: // Add a comment to the first page. Chris@0: $comment = [ Chris@0: 'subject[0][value]' => $this->randomMachineName(), Chris@0: 'comment_body[0][value]' => $this->randomMachineName(20), Chris@0: ]; Chris@0: $this->drupalPostForm('comment/reply/node/' . $node_one->id() . '/comment', $comment, t('Save')); Chris@0: Chris@0: // If the comment is posted in the same second as the last one then Drupal Chris@0: // can't tell the difference, so we wait one second here. Chris@0: sleep(1); Chris@0: Chris@0: // Add a comment to the second page. Chris@0: $comment = [ Chris@0: 'subject[0][value]' => $this->randomMachineName(), Chris@0: 'comment_body[0][value]' => $this->randomMachineName(20), Chris@0: ]; Chris@0: $this->drupalPostForm('comment/reply/node/' . $node_two->id() . '/comment', $comment, t('Save')); Chris@0: Chris@0: // We should at this point have in our tracker for otherUser: Chris@0: // 1. node_two Chris@0: // 2. node_one Chris@0: // Because that's the reverse order of the posted comments. Chris@0: Chris@0: // Now we're going to post a comment to node_one which should jump it to the Chris@0: // top of the list. Chris@0: Chris@0: $this->drupalLogin($this->user); Chris@0: // If the comment is posted in the same second as the last one then Drupal Chris@0: // can't tell the difference, so we wait one second here. Chris@0: sleep(1); Chris@0: Chris@0: // Add a comment to the second page. Chris@0: $comment = [ Chris@0: 'subject[0][value]' => $this->randomMachineName(), Chris@0: 'comment_body[0][value]' => $this->randomMachineName(20), Chris@0: ]; Chris@0: $this->drupalPostForm('comment/reply/node/' . $node_one->id() . '/comment', $comment, t('Save')); Chris@0: Chris@0: // Switch back to the otherUser and assert that the order has swapped. Chris@0: $this->drupalLogin($this->otherUser); Chris@0: $this->drupalGet('user/' . $this->otherUser->id() . '/activity'); Chris@0: // This is a cheeky way of asserting that the nodes are in the right order Chris@0: // on the tracker page. Chris@0: // It's almost certainly too brittle. Chris@0: $pattern = '/' . preg_quote($node_one->getTitle()) . '.+' . preg_quote($node_two->getTitle()) . '/s'; Chris@0: $this->verbose($pattern); Chris@0: $this->assertPattern($pattern, 'Most recently commented on node appears at the top of tracker'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that existing nodes are indexed by cron. Chris@0: */ Chris@0: public function testTrackerCronIndexing() { Chris@0: $this->drupalLogin($this->user); Chris@0: Chris@0: // Create 3 nodes. Chris@0: $edits = []; Chris@0: $nodes = []; Chris@0: for ($i = 1; $i <= 3; $i++) { Chris@0: $edits[$i] = [ Chris@0: 'title' => $this->randomMachineName(), Chris@0: ]; Chris@0: $nodes[$i] = $this->drupalCreateNode($edits[$i]); Chris@0: } Chris@0: Chris@0: // Add a comment to the last node as other user. Chris@0: $this->drupalLogin($this->otherUser); Chris@0: $comment = [ Chris@0: 'subject[0][value]' => $this->randomMachineName(), Chris@0: 'comment_body[0][value]' => $this->randomMachineName(20), Chris@0: ]; Chris@0: $this->drupalPostForm('comment/reply/node/' . $nodes[3]->id() . '/comment', $comment, t('Save')); Chris@0: Chris@16: // Create an unpublished node. Chris@16: $unpublished = $this->drupalCreateNode([ Chris@16: 'title' => $this->randomMachineName(8), Chris@16: 'status' => 0, Chris@16: ]); Chris@16: Chris@16: // Start indexing backwards from node 4. Chris@16: \Drupal::state()->set('tracker.index_nid', 4); Chris@0: Chris@0: // Clear the current tracker tables and rebuild them. Chris@18: $connection = Database::getConnection(); Chris@18: $connection->delete('tracker_node') Chris@0: ->execute(); Chris@18: $connection->delete('tracker_user') Chris@0: ->execute(); Chris@0: tracker_cron(); Chris@0: Chris@0: $this->drupalLogin($this->user); Chris@0: Chris@0: // Fetch the user's tracker. Chris@0: $this->drupalGet('activity/' . $this->user->id()); Chris@0: Chris@0: // Assert that all node titles are displayed. Chris@0: foreach ($nodes as $i => $node) { Chris@0: $this->assertText($node->label(), format_string('Node @i is displayed on the tracker listing pages.', ['@i' => $i])); Chris@0: } Chris@0: Chris@0: // Fetch the site-wide tracker. Chris@0: $this->drupalGet('activity'); Chris@0: Chris@0: // Assert that all node titles are displayed. Chris@0: foreach ($nodes as $i => $node) { Chris@0: $this->assertText($node->label(), format_string('Node @i is displayed on the tracker listing pages.', ['@i' => $i])); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that publish/unpublish works at admin/content/node. Chris@0: */ Chris@0: public function testTrackerAdminUnpublish() { Chris@0: \Drupal::service('module_installer')->install(['views']); Chris@0: \Drupal::service('router.builder')->rebuild(); Chris@0: $admin_user = $this->drupalCreateUser(['access content overview', 'administer nodes', 'bypass node access']); Chris@0: $this->drupalLogin($admin_user); Chris@0: Chris@0: $node = $this->drupalCreateNode([ Chris@0: 'title' => $this->randomMachineName(), Chris@0: ]); Chris@0: Chris@0: // Assert that the node is displayed. Chris@0: $this->drupalGet('activity'); Chris@0: $this->assertText($node->label(), 'A node is displayed on the tracker listing pages.'); Chris@0: Chris@0: // Unpublish the node and ensure that it's no longer displayed. Chris@0: $edit = [ Chris@0: 'action' => 'node_unpublish_action', Chris@0: 'node_bulk_form[0]' => $node->id(), Chris@0: ]; Chris@0: $this->drupalPostForm('admin/content', $edit, t('Apply to selected items')); Chris@0: Chris@0: $this->drupalGet('activity'); Chris@0: $this->assertText(t('No content available.'), 'A node is displayed on the tracker listing pages.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if the appropriate history metadata exists. Chris@0: * Chris@0: * Verify the data-history-node-id, data-history-node-timestamp and Chris@0: * data-history-node-last-comment-timestamp attributes, which are used by the Chris@0: * drupal.tracker-history library to add the appropriate "new" and "updated" Chris@0: * indicators, as well as the "x new" replies link to the tracker. Chris@0: * We do this in JavaScript to prevent breaking the render cache. Chris@0: * Chris@0: * @param int $node_id Chris@0: * A node ID, that must exist as a data-history-node-id attribute Chris@0: * @param int $node_timestamp Chris@0: * A node timestamp, that must exist as a data-history-node-timestamp Chris@0: * attribute. Chris@0: * @param int $node_last_comment_timestamp Chris@0: * A node's last comment timestamp, that must exist as a Chris@0: * data-history-node-last-comment-timestamp attribute. Chris@0: * @param bool $library_is_present Chris@0: * Whether the drupal.tracker-history library should be present or not. Chris@0: */ Chris@0: public function assertHistoryMetadata($node_id, $node_timestamp, $node_last_comment_timestamp, $library_is_present = TRUE) { Chris@0: $settings = $this->getDrupalSettings(); Chris@0: $this->assertIdentical($library_is_present, isset($settings['ajaxPageState']) && in_array('tracker/history', explode(',', $settings['ajaxPageState']['libraries'])), 'drupal.tracker-history library is present.'); Chris@0: $this->assertIdentical(1, count($this->xpath('//table/tbody/tr/td[@data-history-node-id="' . $node_id . '" and @data-history-node-timestamp="' . $node_timestamp . '"]')), 'Tracker table cell contains the data-history-node-id and data-history-node-timestamp attributes for the node.'); Chris@0: $this->assertIdentical(1, count($this->xpath('//table/tbody/tr/td[@data-history-node-last-comment-timestamp="' . $node_last_comment_timestamp . '"]')), 'Tracker table cell contains the data-history-node-last-comment-timestamp attribute for the node.'); Chris@0: } Chris@0: Chris@0: }