diff --git a/core/modules/content_moderation/src/Plugin/views/filter/ModerationStateFilter.php b/core/modules/content_moderation/src/Plugin/views/filter/ModerationStateFilter.php index bf90923..d412f95 100644 --- a/core/modules/content_moderation/src/Plugin/views/filter/ModerationStateFilter.php +++ b/core/modules/content_moderation/src/Plugin/views/filter/ModerationStateFilter.php @@ -2,6 +2,7 @@ namespace Drupal\content_moderation\Plugin\views\filter; +use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\views\Plugin\views\filter\InOperator; use Drupal\views\Views; @@ -48,6 +49,13 @@ public static function create(ContainerInterface $container, array $configuratio /** * {@inheritdoc} */ + public function getCacheTags() { + return Cache::mergeTags(parent::getCacheTags(), $this->entityTypeManager->getDefinition('workflow')->getListCacheTags()); + } + + /** + * {@inheritdoc} + */ public function getValueOptions() { if (isset($this->valueOptions)) { return $this->valueOptions; diff --git a/core/modules/content_moderation/tests/modules/content_moderation_test_views/config/install/views.view.test_content_moderation_state_filter.yml b/core/modules/content_moderation/tests/modules/content_moderation_test_views/config/install/views.view.test_content_moderation_state_filter.yml index 052f17f..eb0a7c4 100644 --- a/core/modules/content_moderation/tests/modules/content_moderation_test_views/config/install/views.view.test_content_moderation_state_filter.yml +++ b/core/modules/content_moderation/tests/modules/content_moderation_test_views/config/install/views.view.test_content_moderation_state_filter.yml @@ -226,3 +226,20 @@ display: - 'user.node_grants:view' - user.permissions tags: { } + page_1: + display_plugin: page + id: page_1 + display_title: Page + position: 1 + display_options: + display_extenders: { } + path: filter-test-path + cache_metadata: + max-age: -1 + contexts: + - 'languages:language_content' + - 'languages:language_interface' + - url + - 'user.node_grants:view' + - user.permissions + tags: { } diff --git a/core/modules/content_moderation/tests/src/Functional/ViewsModerationStateFilterTest.php b/core/modules/content_moderation/tests/src/Functional/ViewsModerationStateFilterTest.php new file mode 100644 index 0000000..0cb0622 --- /dev/null +++ b/core/modules/content_moderation/tests/src/Functional/ViewsModerationStateFilterTest.php @@ -0,0 +1,83 @@ + 'example_a', + ])->save(); + NodeType::create([ + 'type' => 'example_b', + ])->save(); + } + + /** + * Tests the content moderation state filter caching is correct. + */ + public function testFilterRenderCache() { + $workflow = Workflow::load('editorial'); + + // Initially there should be no filters. + $this->assertFilterStates(['All']); + + // Adding the example node should ensure the filters appear. + $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example_a'); + $workflow->save(); + $this->assertFilterStates(['All', 'draft', 'published', 'archived']); + + // Adding a new workflow will also invalidate the cache. + $new_workflow = Workflow::create([ + 'type' => 'content_moderation', + 'id' => 'new_workflow', + ]); + $new_workflow->getTypePlugin()->addState('bar', 'Bar'); + $new_workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example_b'); + $new_workflow->save(); + $this->assertFilterStates(['All', 'draft', 'published', 'archived', 'bar']); + } + + /** + * Assert the states which appear in the filter. + * + * @param array $states + * The states which should appear in the filter. + */ + protected function assertFilterStates($states) { + $this->drupalGet('/filter-test-path'); + $this->assertSession()->elementsCount('css', '#edit-default-revision-state option', count($states)); + foreach ($states as $state) { + $this->assertSession()->elementExists('css', "#edit-default-revision-state option[value=$state]"); + } + } + +}