Commit b497d61f authored by alexpott's avatar alexpott
Browse files

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 @@ ...@@ -9,8 +9,10 @@
use Drupal\comment\CommentInterface; use Drupal\comment\CommentInterface;
use Drupal\comment\Tests\CommentTestTrait; use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Cache\Cache;
use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldStorageConfig;
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\WebTestBase;
use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
/** /**
* Create and delete nodes and check for their display in the tracker listings. * Create and delete nodes and check for their display in the tracker listings.
...@@ -20,6 +22,7 @@ ...@@ -20,6 +22,7 @@
class TrackerTest extends WebTestBase { class TrackerTest extends WebTestBase {
use CommentTestTrait; use CommentTestTrait;
use AssertPageCacheContextsAndTagsTrait;
/** /**
* Modules to enable. * Modules to enable.
...@@ -73,6 +76,11 @@ function testTrackerAll() { ...@@ -73,6 +76,11 @@ function testTrackerAll() {
$this->assertText($published->label(), 'Published node shows up in the tracker listing.'); $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.'); $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. // Delete a node and ensure it no longer appears on the tracker.
$published->delete(); $published->delete();
$this->drupalGet('activity'); $this->drupalGet('activity');
...@@ -131,6 +139,21 @@ function testTrackerUser() { ...@@ -131,6 +139,21 @@ function testTrackerUser() {
$this->assertText($my_published->label(), "Published nodes 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->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."); $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->assertLink($my_published->label());
$this->assertNoLink($unpublished->label()); $this->assertNoLink($unpublished->label());
// Verify that title and tab title have been set correctly. // Verify that title and tab title have been set correctly.
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Cache\Cache;
use Drupal\node\Entity\Node; use Drupal\node\Entity\Node;
/** /**
...@@ -43,7 +44,8 @@ function tracker_page($account = NULL) { ...@@ -43,7 +44,8 @@ function tracker_page($account = NULL) {
->execute() ->execute()
->fetchAllAssoc('nid'); ->fetchAllAssoc('nid');
$rows = array(); $cache_tags = [];
$rows = [];
if (!empty($tracker_data)) { if (!empty($tracker_data)) {
// Load nodes into an array with the same order as $tracker_data. // Load nodes into an array with the same order as $tracker_data.
$nodes = Node::loadMultiple(array_keys($tracker_data)); $nodes = Node::loadMultiple(array_keys($tracker_data));
...@@ -120,9 +122,18 @@ function tracker_page($account = NULL) { ...@@ -120,9 +122,18 @@ function tracker_page($account = NULL) {
); );
$rows[] = $row; $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( $page['tracker'] = array(
'#rows' => $rows, '#rows' => $rows,
'#header' => array(t('Type'), t('Title'), t('Author'), t('Comments'), t('Last updated')), '#header' => array(t('Type'), t('Title'), t('Author'), t('Comments'), t('Last updated')),
...@@ -134,6 +145,8 @@ function tracker_page($account = NULL) { ...@@ -134,6 +145,8 @@ function tracker_page($account = NULL) {
'#weight' => 10, '#weight' => 10,
); );
$page['#sorted'] = TRUE; $page['#sorted'] = TRUE;
$page['#cache']['tags'] = $cache_tags;
$page['#cache']['contexts'][] = 'user.node_grants:view';
return $page; 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