From 41560b32962e0d36fe5da246f3bb6912e36e08c3 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 16 Jun 2015 16:46:10 +0100 Subject: [PATCH] Issue #2484619 by borisson_, Wim Leers, lauriii, larowlan, dawehner: Forum responses don't set cache tags --- .../forum/src/Controller/ForumController.php | 50 +++++++++++++++++-- .../forum/src/Tests/ForumIndexTest.php | 18 ++++++- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/core/modules/forum/src/Controller/ForumController.php b/core/modules/forum/src/Controller/ForumController.php index 38cc0971eb9..f0ea9b3d070 100644 --- a/core/modules/forum/src/Controller/ForumController.php +++ b/core/modules/forum/src/Controller/ForumController.php @@ -11,6 +11,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Entity\EntityAccessControlHandlerInterface; use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; @@ -74,6 +75,20 @@ class ForumController extends ControllerBase { */ protected $renderer; + /** + * Node entity type, we need to get cache tags from here. + * + * @var \Drupal\Core\Entity\EntityTypeInterface + */ + protected $nodeEntityTypeDefinition; + + /** + * Comment entity type, we need to get cache tags from here. + * + * @var \Drupal\Core\Entity\EntityTypeInterface + */ + protected $commentEntityTypeDefinition; + /** * Constructs a ForumController object. * @@ -93,8 +108,12 @@ class ForumController extends ControllerBase { * Node type storage handler. * @param \Drupal\Core\Render\RendererInterface $renderer * The renderer. + * @param \Drupal\Core\Entity\EntityTypeInterface $node_entity_type_definition + * Node entity type definition object + * @param \Drupal\Core\Entity\EntityTypeInterface $comment_entity_type_definition + * Comment entity type definition object */ - public function __construct(ForumManagerInterface $forum_manager, VocabularyStorageInterface $vocabulary_storage, TermStorageInterface $term_storage, AccountInterface $current_user, EntityAccessControlHandlerInterface $node_access, array $field_map, EntityStorageInterface $node_type_storage, RendererInterface $renderer) { + public function __construct(ForumManagerInterface $forum_manager, VocabularyStorageInterface $vocabulary_storage, TermStorageInterface $term_storage, AccountInterface $current_user, EntityAccessControlHandlerInterface $node_access, array $field_map, EntityStorageInterface $node_type_storage, RendererInterface $renderer, EntityTypeInterface $node_entity_type_definition, EntityTypeInterface $comment_entity_type_definition) { $this->forumManager = $forum_manager; $this->vocabularyStorage = $vocabulary_storage; $this->termStorage = $term_storage; @@ -103,6 +122,8 @@ class ForumController extends ControllerBase { $this->fieldMap = $field_map; $this->nodeTypeStorage = $node_type_storage; $this->renderer = $renderer; + $this->nodeEntityTypeDefinition = $node_entity_type_definition; + $this->commentEntityTypeDefinition = $comment_entity_type_definition; } /** @@ -111,6 +132,7 @@ class ForumController extends ControllerBase { public static function create(ContainerInterface $container) { /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */ $entity_manager = $container->get('entity.manager'); + return new static( $container->get('forum_manager'), $entity_manager->getStorage('taxonomy_vocabulary'), @@ -119,7 +141,9 @@ class ForumController extends ControllerBase { $entity_manager->getAccessControlHandler('node'), $entity_manager->getFieldMap(), $entity_manager->getStorage('node_type'), - $container->get('renderer') + $container->get('renderer'), + $entity_manager->getDefinition('node'), + $entity_manager->getDefinition('comment') ); } @@ -143,8 +167,8 @@ class ForumController extends ControllerBase { $header = $build['header']; } else { - $topics = ''; - $header = array(); + $topics = []; + $header = []; } return $this->build($taxonomy_term->forums, $taxonomy_term, $topics, $taxonomy_term->parents, $header); } @@ -205,9 +229,23 @@ class ForumController extends ControllerBase { } $this->renderer->addCacheableDependency($build, $config); + foreach ($forums as $forum) { + $this->renderer->addCacheableDependency($build, $forum); + } + foreach ($topics as $topic) { + $this->renderer->addCacheableDependency($build, $topic); + } + foreach ($parents as $parent) { + $this->renderer->addCacheableDependency($build, $parent); + } + $this->renderer->addCacheableDependency($build, $term); + return [ 'action' => $this->buildActionLinks($config->get('vocabulary'), $term), 'forum' => $build, + '#cache' => [ + 'tags' => Cache::mergeTags($this->nodeEntityTypeDefinition->getListCacheTags(), $this->commentEntityTypeDefinition->getListCacheTags()), + ], ]; } @@ -259,6 +297,7 @@ class ForumController extends ControllerBase { // Loop through all bundles for forum taxonomy vocabulary field. foreach ($this->fieldMap['node']['taxonomy_forums']['bundles'] as $type) { if ($this->nodeAccess->createAccess($type)) { + $node_type = $this->nodeTypeStorage->load($type); $links[$type] = [ '#attributes' => ['class' => ['action-links']], '#theme' => 'menu_local_action', @@ -268,6 +307,9 @@ class ForumController extends ControllerBase { ]), 'url' => Url::fromRoute('node.add', ['node_type' => $type]), ], + '#cache' => [ + 'tags' => $node_type->getCacheTags(), + ], ]; if ($forum_term && $forum_term->bundle() == $vid) { // We are viewing a forum term (specific forum), append the tid to diff --git a/core/modules/forum/src/Tests/ForumIndexTest.php b/core/modules/forum/src/Tests/ForumIndexTest.php index b9ee5377c45..d598c6f933e 100644 --- a/core/modules/forum/src/Tests/ForumIndexTest.php +++ b/core/modules/forum/src/Tests/ForumIndexTest.php @@ -27,7 +27,7 @@ class ForumIndexTest extends WebTestBase { parent::setUp(); // Create a test user. - $web_user = $this->drupalCreateUser(array('create forum content', 'edit own forum content', 'edit any forum content', 'administer nodes')); + $web_user = $this->drupalCreateUser(['create forum content', 'edit own forum content', 'edit any forum content', 'administer nodes', 'administer forums']); $this->drupalLogin($web_user); } @@ -55,9 +55,25 @@ class ForumIndexTest extends WebTestBase { $node = $this->drupalGetNodeByTitle($title); $this->assertTrue(!empty($node), 'New forum node found in database.'); + // Create a child forum. + $edit = array( + 'name[0][value]' => $this->randomMachineName(20), + 'description[0][value]' => $this->randomMachineName(200), + 'parent[0]' => $tid, + ); + $this->drupalPostForm('admin/structure/forum/add/forum', $edit, t('Save')); + $tid_child = $tid + 1; + // Verify that the node appears on the index. $this->drupalGet('forum/' . $tid); $this->assertText($title, 'Published forum topic appears on index.'); + $this->assertCacheTag('node_list'); + $this->assertCacheTag('config:node.type.forum'); + $this->assertCacheTag('comment_list'); + $this->assertCacheTag('node:' . $node->id()); + $this->assertCacheTag('taxonomy_term:' . $tid); + $this->assertCacheTag('taxonomy_term:' . $tid_child); + // Unpublish the node. $this->drupalPostForm('node/' . $node->id() . '/edit', array(), t('Save and unpublish'));