You are here

public function TrackerTest::testTrackerUser 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::testTrackerUser()
  2. 10 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 146

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
    ->drupalPostForm('comment/reply/node/' . $other_published_my_comment
    ->id() . '/comment', $comment, t('Save'));
  $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($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.");

  // 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
    ->assertText('Activity', 'The user activity tab has the name "Activity".');
  $this
    ->assertTitle($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
    ->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.');

  // 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
    ->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
    ->assertNoEscaped('<em>' . $this->user
    ->id() . '</em>');
  $this
    ->assertRaw('<em>' . $this->user
    ->id() . '</em>');
}