You are here

function TrackerTest::testTrackerUser in Zircon Profile 8.0

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

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

File

core/modules/tracker/src/Tests/TrackerTest.php, line 137
Contains \Drupal\tracker\Tests\TrackerTest.

Class

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

Namespace

Drupal\tracker\Tests

Code

function testTrackerUser() {
  $this
    ->drupalLogin($this->user);
  $unpublished = $this
    ->drupalCreateNode(array(
    'title' => $this
      ->randomMachineName(8),
    'uid' => $this->user
      ->id(),
    'status' => 0,
  ));
  $my_published = $this
    ->drupalCreateNode(array(
    'title' => $this
      ->randomMachineName(8),
    'uid' => $this->user
      ->id(),
    'status' => 1,
  ));
  $other_published_no_comment = $this
    ->drupalCreateNode(array(
    'title' => $this
      ->randomMachineName(8),
    'uid' => $this->otherUser
      ->id(),
    'status' => 1,
  ));
  $other_published_my_comment = $this
    ->drupalCreateNode(array(
    'title' => $this
      ->randomMachineName(8),
    'uid' => $this->otherUser
      ->id(),
    'status' => 1,
  ));
  $comment = array(
    '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',
    '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
    ->assertLink($my_published
    ->label());
  $this
    ->assertNoLink($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', array(
    '@name' => $this->user
      ->getUsername(),
    '@site' => $this
      ->config('system.site')
      ->get('name'),
  )), 'The user tracker page has the correct page title.');

  // Verify that unpublished comments are removed from the tracker.
  $admin_user = $this
    ->drupalCreateUser(array(
    'post comments',
    'administer comments',
    'access user profiles',
  ));
  $this
    ->drupalLogin($admin_user);
  $this
    ->drupalPostForm('comment/1/edit', array(
    '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>');
}