You are here

public function TrackerTest::testTrackerUser in Drupal 9

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

Tests for the presence of nodes on a user's tracker listing.

File

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

Class

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

Namespace

Drupal\Tests\tracker\Functional

Code

public function testTrackerUser() {
  $this
    ->drupalLogin($this->user);
  $unpublished = $this
    ->drupalCreateNode([
    'title' => $this
      ->randomMachineName(8),
    'uid' => $this->user
      ->id(),
    'status' => 0,
  ]);
  $my_published = $this
    ->drupalCreateNode([
    'title' => $this
      ->randomMachineName(8),
    'uid' => $this->user
      ->id(),
    'status' => 1,
  ]);
  $other_published_no_comment = $this
    ->drupalCreateNode([
    'title' => $this
      ->randomMachineName(8),
    'uid' => $this->otherUser
      ->id(),
    'status' => 1,
  ]);
  $other_published_my_comment = $this
    ->drupalCreateNode([
    'title' => $this
      ->randomMachineName(8),
    'uid' => $this->otherUser
      ->id(),
    'status' => 1,
  ]);
  $comment = [
    'subject[0][value]' => $this
      ->randomMachineName(),
    'comment_body[0][value]' => $this
      ->randomMachineName(20),
  ];
  $this
    ->drupalGet('comment/reply/node/' . $other_published_my_comment
    ->id() . '/comment');
  $this
    ->submitForm($comment, 'Save');
  $this
    ->drupalGet('user/' . $this->user
    ->id() . '/activity');
  $this
    ->assertSession()
    ->pageTextNotContains($unpublished
    ->label());
  $this
    ->assertSession()
    ->pageTextContains($my_published
    ->label());
  $this
    ->assertSession()
    ->pageTextNotContains($other_published_no_comment
    ->label());
  $this
    ->assertSession()
    ->pageTextContains($other_published_my_comment
    ->label());

  // 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());

  // Because the 'user.permissions' cache context is being optimized away.
  $role_tags = [];
  foreach ($this->user
    ->getRoles() as $rid) {
    $role_tags[] = "config:user.role.{$rid}";
  }
  $expected_tags = Cache::mergeTags($expected_tags, $role_tags);
  $block_tags = [
    'block_view',
    'local_task',
    'config:block.block.page_actions_block',
    'config:block.block.page_tabs_block',
    'config:block_list',
  ];
  $expected_tags = Cache::mergeTags($expected_tags, $block_tags);
  $additional_tags = [
    'node_list',
    'rendered',
  ];
  $expected_tags = Cache::mergeTags($expected_tags, $additional_tags);
  $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
    ->assertSession()
    ->linkExists($my_published
    ->label());
  $this
    ->assertSession()
    ->linkNotExists($unpublished
    ->label());

  // Verify that title and tab title have been set correctly.
  $this
    ->assertSession()
    ->pageTextContains('Activity');
  $this
    ->assertSession()
    ->titleEquals($this->user
    ->getAccountName() . ' | Drupal');

  // Verify that unpublished comments are removed from the tracker.
  $admin_user = $this
    ->drupalCreateUser([
    'post comments',
    'administer comments',
    'access user profiles',
  ]);
  $this
    ->drupalLogin($admin_user);
  $this
    ->drupalGet('comment/1/edit');
  $this
    ->submitForm([
    'status' => CommentInterface::NOT_PUBLISHED,
  ], 'Save');
  $this
    ->drupalGet('user/' . $this->user
    ->id() . '/activity');
  $this
    ->assertSession()
    ->pageTextNotContains($other_published_my_comment
    ->label());

  // Test escaping of title on user's tracker tab.
  \Drupal::service('module_installer')
    ->install([
    'user_hooks_test',
  ]);
  Cache::invalidateTags([
    'rendered',
  ]);
  \Drupal::state()
    ->set('user_hooks_test_user_format_name_alter', TRUE);
  $this
    ->drupalGet('user/' . $this->user
    ->id() . '/activity');
  $this
    ->assertSession()
    ->assertEscaped('<em>' . $this->user
    ->id() . '</em>');
  \Drupal::state()
    ->set('user_hooks_test_user_format_name_alter_safe', TRUE);
  Cache::invalidateTags([
    'rendered',
  ]);
  $this
    ->drupalGet('user/' . $this->user
    ->id() . '/activity');
  $this
    ->assertSession()
    ->assertNoEscaped('<em>' . $this->user
    ->id() . '</em>');
  $this
    ->assertSession()
    ->responseContains('<em>' . $this->user
    ->id() . '</em>');
}