You are here

public function TrackerTest::testTrackerAll 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::testTrackerAll()

Tests for the presence of nodes on the global tracker listing.

File

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

Class

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

Namespace

Drupal\Tests\tracker\Functional

Code

public function testTrackerAll() {
  $this
    ->drupalLogin($this->user);
  $unpublished = $this
    ->drupalCreateNode([
    'title' => $this
      ->randomMachineName(8),
    'status' => 0,
  ]);
  $published = $this
    ->drupalCreateNode([
    'title' => $this
      ->randomMachineName(8),
    'status' => 1,
  ]);
  $this
    ->drupalGet('activity');
  $this
    ->assertSession()
    ->pageTextNotContains($unpublished
    ->label());
  $this
    ->assertSession()
    ->pageTextContains($published
    ->label());
  $this
    ->assertSession()
    ->linkExists('My recent content', 0, 'User tab shows up on the global tracker page.');

  // Assert cache contexts, specifically the pager and node access contexts.
  $this
    ->assertCacheContexts([
    'languages:language_interface',
    'route',
    'theme',
    'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
    'url.query_args.pagers:0',
    'user.node_grants:view',
    'user',
  ]);

  // Assert cache tags for the action/tabs blocks, visible node, and node list
  // cache tag.
  $expected_tags = Cache::mergeTags($published
    ->getCacheTags(), $published
    ->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);

  // Delete a node and ensure it no longer appears on the tracker.
  $published
    ->delete();
  $this
    ->drupalGet('activity');
  $this
    ->assertSession()
    ->pageTextNotContains($published
    ->label());

  // Test proper display of time on activity page when comments are disabled.
  // Disable comments.
  FieldStorageConfig::loadByName('node', 'comment')
    ->delete();
  $node = $this
    ->drupalCreateNode([
    // This title is required to trigger the custom changed time set in the
    // node_test module. This is needed in order to ensure a sufficiently
    // large 'time ago' interval that isn't numbered in seconds.
    'title' => 'testing_node_presave',
    'status' => 1,
  ]);
  $this
    ->drupalGet('activity');
  $this
    ->assertSession()
    ->pageTextContains($node
    ->label());
  $this
    ->assertSession()
    ->pageTextContains(\Drupal::service('date.formatter')
    ->formatTimeDiffSince($node
    ->getChangedTime()));
}