Commit b497d61f authored by alexpott's avatar alexpott

Issue #2484611 by jhedstrom, fgm, Berdir, borisson_, Wim Leers: Tracker...

Issue #2484611 by jhedstrom, fgm, Berdir, borisson_, Wim Leers: Tracker responses don't set cache tags & contexts
parent 215b967a
......@@ -9,8 +9,10 @@
use Drupal\comment\CommentInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Cache\Cache;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\simpletest\WebTestBase;
use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
/**
* Create and delete nodes and check for their display in the tracker listings.
......@@ -20,6 +22,7 @@
class TrackerTest extends WebTestBase {
use CommentTestTrait;
use AssertPageCacheContextsAndTagsTrait;
/**
* Modules to enable.
......@@ -73,6 +76,11 @@ function testTrackerAll() {
$this->assertText($published->label(), 'Published node shows up in the tracker listing.');
$this->assertLink(t('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', 'theme', 'url.query_args.pagers:0', 'user.node_grants:view', 'user.permissions']);
// Assert cache tags for the visible node and node list cache tag.
$this->assertCacheTags(Cache::mergeTags($published->getCacheTags(), $published->getOwner()->getCacheTags(), ['node_list', 'rendered']));
// Delete a node and ensure it no longer appears on the tracker.
$published->delete();
$this->drupalGet('activity');
......@@ -131,6 +139,21 @@ function testTrackerUser() {
$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; the node grant context is not directly visible due
// to it being implied by the user context.
$this->assertCacheContexts(['languages:language_interface', 'theme', 'url.query_args.pagers:0', 'user']);
// Assert cache tags for the visible nodes (including owners) and node list
// cache tag.
$tags = Cache::mergeTags(
$my_published->getCacheTags(),
$my_published->getOwner()->getCacheTags(),
$other_published_my_comment->getCacheTags(),
$other_published_my_comment->getOwner()->getCacheTags(),
['node_list', 'rendered']
);
$this->assertCacheTags($tags);
$this->assertLink($my_published->label());
$this->assertNoLink($unpublished->label());
// Verify that title and tab title have been set correctly.
......
......@@ -6,6 +6,7 @@
*/
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Cache\Cache;
use Drupal\node\Entity\Node;
/**
......@@ -43,7 +44,8 @@ function tracker_page($account = NULL) {
->execute()
->fetchAllAssoc('nid');
$rows = array();
$cache_tags = [];
$rows = [];
if (!empty($tracker_data)) {
// Load nodes into an array with the same order as $tracker_data.
$nodes = Node::loadMultiple(array_keys($tracker_data));
......@@ -120,9 +122,18 @@ function tracker_page($account = NULL) {
);
$rows[] = $row;
// Add node and node owner to cache tags.
$cache_tags = Cache::mergeTags($cache_tags, $node->getCacheTags());
if ($node->getOwner()) {
$cache_tags = Cache::mergeTags($cache_tags, $node->getOwner()->getCacheTags());
}
}
}
// Add the list cache tag for nodes.
$cache_tags = Cache::mergeTags($cache_tags, \Drupal::entityManager()->getDefinition('node')->getListCacheTags());
$page['tracker'] = array(
'#rows' => $rows,
'#header' => array(t('Type'), t('Title'), t('Author'), t('Comments'), t('Last updated')),
......@@ -134,6 +145,8 @@ function tracker_page($account = NULL) {
'#weight' => 10,
);
$page['#sorted'] = TRUE;
$page['#cache']['tags'] = $cache_tags;
$page['#cache']['contexts'][] = 'user.node_grants:view';
return $page;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment