From 434af922dade7431c7a2c2e2d508c5bb4bbd5c99 Mon Sep 17 00:00:00 2001 From: nagyad Date: Wed, 16 Nov 2022 15:00:00 +0100 Subject: [PATCH 1/8] EWPP-2724: Bubble up facet cache. --- src/ListBuilder.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ListBuilder.php b/src/ListBuilder.php index 3d346e4d..2671c430 100644 --- a/src/ListBuilder.php +++ b/src/ListBuilder.php @@ -293,6 +293,7 @@ public function buildSelectedFilters(ListPageConfiguration $configuration): arra $list_execution = $this->listExecutionManager->executeList($configuration); $facets = $this->getKeyedFacetsFromSource($list_execution->getListSource()); foreach ($facets as $facet) { + $cache->addCacheableDependency($facet); $active_items = $facet->getActiveItems(); if (!empty($active_items) && $this->facetHasDefaultStatus($facet, ['raw' => reset($active_items)])) { $active_filters[$facet->id()] = $facet->getActiveItems(); From dfff78503ce3555829d74066a7e46270a8852593 Mon Sep 17 00:00:00 2001 From: Hernani Borges de Freitas Date: Mon, 19 Dec 2022 16:36:30 +0000 Subject: [PATCH 2/8] EWPP-2724: Add extra cache information to form filters. --- src/Form/ListFacetsForm.php | 1 + src/Plugin/ExtraField/Display/ListPageFilters.php | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/Form/ListFacetsForm.php b/src/Form/ListFacetsForm.php index 8df3bec4..540eb1f1 100644 --- a/src/Form/ListFacetsForm.php +++ b/src/Form/ListFacetsForm.php @@ -115,6 +115,7 @@ public function buildForm(array $form, FormStateInterface $form_state, ListSourc if ($widget instanceof ListPagesWidgetInterface) { $form['facets'][$facet->id()] = $this->facetsManager->getFacetManager()->build($facet); + $cache->addCacheTags($facet->getCacheTags()); } } diff --git a/src/Plugin/ExtraField/Display/ListPageFilters.php b/src/Plugin/ExtraField/Display/ListPageFilters.php index 5e6cf644..14ecbb14 100644 --- a/src/Plugin/ExtraField/Display/ListPageFilters.php +++ b/src/Plugin/ExtraField/Display/ListPageFilters.php @@ -37,6 +37,10 @@ public function view(ContentEntityInterface $entity) { return isset($form['#cache']) ? ['#cache' => $form['#cache']] : []; } + if (!empty($form['#cache']['tags'])) { + $entity->addCacheTags($form['#cache']['tags']); + } + return $form; } From a825527e86a31d78434bf3a8a1236cba189011b8 Mon Sep 17 00:00:00 2001 From: Hernani Borges de Freitas Date: Tue, 20 Dec 2022 12:31:06 +0000 Subject: [PATCH 3/8] EWPP-2724: Add cache tags to generated query. --- src/EventSubscriber/QuerySubscriber.php | 5 +++++ src/ListBuilder.php | 1 + 2 files changed, 6 insertions(+) diff --git a/src/EventSubscriber/QuerySubscriber.php b/src/EventSubscriber/QuerySubscriber.php index 86e2af13..0c0bfcaa 100644 --- a/src/EventSubscriber/QuerySubscriber.php +++ b/src/EventSubscriber/QuerySubscriber.php @@ -132,6 +132,11 @@ public function queryAlter(QueryPreExecuteEvent $event) { 'facet' => $facet, ]); $query_type_plugin->execute(); + + // Add query tags to the query. + if (!empty($facet)) { + $query->addCacheTags($facet->getCacheTags()); + } } catch (InvalidQueryTypeException $exception) { // If the facet doesn't have a query type, continue and don't crash diff --git a/src/ListBuilder.php b/src/ListBuilder.php index 2671c430..0d9d5ce6 100644 --- a/src/ListBuilder.php +++ b/src/ListBuilder.php @@ -183,6 +183,7 @@ public function buildList(ListPageConfiguration $configuration): array { $query = $list_execution->getQuery(); $cache->addCacheableDependency($query); + $cache->addCacheTags($query->getCacheTags()); $result = $list_execution->getResults(); $configuration = $list_execution->getConfiguration(); From 2c6358ed02b38e4b83d4c6621e18919bc05cbe64 Mon Sep 17 00:00:00 2001 From: Hernani Borges de Freitas Date: Wed, 11 Jan 2023 15:43:40 +0000 Subject: [PATCH 4/8] EWPP-2724: Add extra technical comments where the cache tags are added. --- src/EventSubscriber/QuerySubscriber.php | 3 ++- src/Plugin/ExtraField/Display/ListPageFilters.php | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/EventSubscriber/QuerySubscriber.php b/src/EventSubscriber/QuerySubscriber.php index 0c0bfcaa..b1197922 100644 --- a/src/EventSubscriber/QuerySubscriber.php +++ b/src/EventSubscriber/QuerySubscriber.php @@ -133,7 +133,8 @@ public function queryAlter(QueryPreExecuteEvent $event) { ]); $query_type_plugin->execute(); - // Add query tags to the query. + // Add facet cache tags to the query. + // Facets can modify the query, we add the same cache tags to it. if (!empty($facet)) { $query->addCacheTags($facet->getCacheTags()); } diff --git a/src/Plugin/ExtraField/Display/ListPageFilters.php b/src/Plugin/ExtraField/Display/ListPageFilters.php index 14ecbb14..1d9883cb 100644 --- a/src/Plugin/ExtraField/Display/ListPageFilters.php +++ b/src/Plugin/ExtraField/Display/ListPageFilters.php @@ -37,6 +37,9 @@ public function view(ContentEntityInterface $entity) { return isset($form['#cache']) ? ['#cache' => $form['#cache']] : []; } + // Facets might have added cache tags to the form related with their + // content. We guarantee the cache tags are added also to the entity + // here so they bubble up to the page. if (!empty($form['#cache']['tags'])) { $entity->addCacheTags($form['#cache']['tags']); } From a35b2a80e60291fed5f7e5be29f8acb2174755ce Mon Sep 17 00:00:00 2001 From: nagyad Date: Fri, 13 Jan 2023 17:39:12 +0100 Subject: [PATCH 5/8] EWPP-2724: Assert facet cache tags on the query. --- docker-compose.yml | 2 +- tests/src/Kernel/DateStatusTest.php | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index d5216746..e73cd1f5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,7 +15,7 @@ services: XDEBUG_MODE: "develop, debug" PHP_IDE_CONFIG: "serverName=Docker" # Enable step debugging for all PHP request. See ./README.md#step-debugging for more information. - # XDEBUG_SESSION: 1 + XDEBUG_SESSION: 1 mysql: image: percona/percona-server:5.7 command: --innodb-log-file-size=1G --max_allowed_packet=1G --innodb-buffer-pool-size=512M --wait_timeout=3000 --net_write_timeout=3000 --log_error_verbosity=3 diff --git a/tests/src/Kernel/DateStatusTest.php b/tests/src/Kernel/DateStatusTest.php index 0697f7ef..7f5a2656 100644 --- a/tests/src/Kernel/DateStatusTest.php +++ b/tests/src/Kernel/DateStatusTest.php @@ -71,18 +71,22 @@ public function testWithConfiguredDefaults(): void { DateStatus::UPCOMING => t('Upcoming'), DateStatus::PAST => t('Past'), ]; + // Assert the query has the corresponding cache tag from the facet. + $this->assertContains($this->facet->getCacheTags()[0], $query->getCacheTags()); $this->assertEquals($default_options, $actual['#options']); $this->assertEquals([], $actual['#default_value']); + $build = $this->facetManager->build($facet_with_config); $actual = $build[0][$facet_with_config->id()]; $this->assertSame('array', gettype($actual)); $this->assertEquals('select', $actual['#type']); - $default_options = [ DateStatus::UPCOMING => t('Coming items'), DateStatus::PAST => t('Past items'), ]; + // Assert the query has the corresponding cache tag from the facet. + $this->assertContains($this->facet->getCacheTags()[0], $query->getCacheTags()); $this->assertEquals($default_options, $actual['#options']); $this->assertEquals([DateStatus::UPCOMING], $actual['#default_value']); From e9fe3376cb6da69f53363587241be281ccdc55ec Mon Sep 17 00:00:00 2001 From: nagyad Date: Mon, 16 Jan 2023 16:47:46 +0100 Subject: [PATCH 6/8] EWPP-2724: Assert facet cache on list facet form. --- tests/src/Kernel/ListFacetsFormTest.php | 44 +++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/src/Kernel/ListFacetsFormTest.php diff --git a/tests/src/Kernel/ListFacetsFormTest.php b/tests/src/Kernel/ListFacetsFormTest.php new file mode 100644 index 00000000..7f6206db --- /dev/null +++ b/tests/src/Kernel/ListFacetsFormTest.php @@ -0,0 +1,44 @@ +facet = $this->createFacet('created', $default_list_id, '', 'oe_list_pages_multiselect', []); + $this->facet->addProcessor([ + 'processor_id' => 'oe_list_pages_date_status_processor', + 'weights' => [], + 'settings' => [], + ]); + + $this->facet->save(); + + $list = $this->listFactory->get('entity_test_mulrev_changed', 'item'); + + $form_state = new FormState(); + $form_state->setRequestMethod('POST'); + $form_state->setCached(); + $form = ListFacetsForm::create($this->container); + $form_array = $form->buildForm([], $form_state, $list, []); + $expected_cache_tags = [ + 'config:facets_facet_list', + 'config:facets.facet.list_facet_source_entity_test_mulrev_changed_itemcreated', + ]; + $this->assertEquals($expected_cache_tags, $form_array['#cache']['tags']); + } + +} From adccdb17052841d22ed1b5412cd5e9c36069a1a5 Mon Sep 17 00:00:00 2001 From: nagyad Date: Tue, 17 Jan 2023 12:21:57 +0100 Subject: [PATCH 7/8] EWPP-2724: Assert facet cache on list builder render arrays. --- docker-compose.yml | 2 +- tests/src/Kernel/ListBuilderTest.php | 67 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/src/Kernel/ListBuilderTest.php diff --git a/docker-compose.yml b/docker-compose.yml index e73cd1f5..d5216746 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,7 +15,7 @@ services: XDEBUG_MODE: "develop, debug" PHP_IDE_CONFIG: "serverName=Docker" # Enable step debugging for all PHP request. See ./README.md#step-debugging for more information. - XDEBUG_SESSION: 1 + # XDEBUG_SESSION: 1 mysql: image: percona/percona-server:5.7 command: --innodb-log-file-size=1G --max_allowed_packet=1G --innodb-buffer-pool-size=512M --wait_timeout=3000 --net_write_timeout=3000 --log_error_verbosity=3 diff --git a/tests/src/Kernel/ListBuilderTest.php b/tests/src/Kernel/ListBuilderTest.php new file mode 100644 index 00000000..9db2871c --- /dev/null +++ b/tests/src/Kernel/ListBuilderTest.php @@ -0,0 +1,67 @@ +facet = $this->createFacet('created', $default_list_id, '', 'oe_list_pages_multiselect', []); + $this->facet->addProcessor([ + 'processor_id' => 'oe_list_pages_date_status_processor', + 'weights' => [], + 'settings' => [], + ]); + $this->facet->save(); + + $configuration = [ + 'entity_type' => 'entity_test_mulrev_changed', + 'bundle' => 'item', + 'exposed_filters' => [], + 'exposed_filters_overridden' => FALSE, + 'default_filter_values' => [], + 'contextual_filters' => [], + ]; + $listPageConfiguration = new ListPageConfiguration($configuration); + + $listExecutionManager = $this->container->get('oe_list_pages.execution_manager'); + $pager = $this->container->get('pager.manager'); + $entityRepository = $this->container->get('entity.repository'); + $formBuilder = $this->container->get('form_builder'); + $facetsUrlGenerator = $this->container->get('facets.utility.url_generator'); + $processorManager = $this->container->get('plugin.manager.facets.processor'); + $requestStack = $this->container->get('request_stack'); + $urlProcessorManager = $this->container->get('plugin.manager.facets.url_processor'); + $multiselectFilterManager = $this->container->get('plugin.manager.multiselect_filter_field'); + $listSourceFactory = $this->container->get('oe_list_pages.list_source.factory'); + + $listBuilder = new ListBuilder($listExecutionManager, $this->entityTypeManager, $pager, $entityRepository, $formBuilder, $facetsUrlGenerator, $processorManager, $requestStack, $urlProcessorManager, $multiselectFilterManager, $listSourceFactory); + $render_array = $listBuilder->buildList($listPageConfiguration); + + $expected_cache_tags = [ + 'config:search_api.index.database_search_index', + 'config:facets.facet.list_facet_source_entity_test_mulrev_changed_itemcreated', + 'entity_test_mulrev_changed_list:item', + ]; + $this->assertEquals($expected_cache_tags, $render_array['#cache']['tags']); + + $render_array = $listBuilder->buildSelectedFilters($listPageConfiguration); + $expected_cache_tags = [ + 'config:facets.facet.list_facet_source_entity_test_mulrev_changed_itemcreated', + ]; + $this->assertEquals($expected_cache_tags, $render_array['#cache']['tags']); + } + +} From 6f38a828e1cdca7f49b10a15463513ff6b795990 Mon Sep 17 00:00:00 2001 From: nagyad Date: Tue, 24 Jan 2023 13:26:39 +0100 Subject: [PATCH 8/8] EWPP-2724: Remove unnecessary object instantiation in test. --- tests/src/Kernel/DateStatusTest.php | 8 ++++++-- tests/src/Kernel/ListBuilderTest.php | 14 +------------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/tests/src/Kernel/DateStatusTest.php b/tests/src/Kernel/DateStatusTest.php index 7f5a2656..fa98612e 100644 --- a/tests/src/Kernel/DateStatusTest.php +++ b/tests/src/Kernel/DateStatusTest.php @@ -72,7 +72,9 @@ public function testWithConfiguredDefaults(): void { DateStatus::PAST => t('Past'), ]; // Assert the query has the corresponding cache tag from the facet. - $this->assertContains($this->facet->getCacheTags()[0], $query->getCacheTags()); + $expected_tag = 'config:facets.facet.list_facet_source_entity_test_mulrev_changed_itemcreated'; + $this->assertContains($expected_tag, $this->facet->getCacheTags()); + $this->assertContains($expected_tag, $query->getCacheTags()); $this->assertEquals($default_options, $actual['#options']); $this->assertEquals([], $actual['#default_value']); @@ -86,7 +88,9 @@ public function testWithConfiguredDefaults(): void { DateStatus::PAST => t('Past items'), ]; // Assert the query has the corresponding cache tag from the facet. - $this->assertContains($this->facet->getCacheTags()[0], $query->getCacheTags()); + $expected_tag = 'config:facets.facet.list_facet_source_entity_test_mulrev_changed_itemcreated'; + $this->assertContains($expected_tag, $this->facet->getCacheTags()); + $this->assertContains($expected_tag, $query->getCacheTags()); $this->assertEquals($default_options, $actual['#options']); $this->assertEquals([DateStatus::UPCOMING], $actual['#default_value']); diff --git a/tests/src/Kernel/ListBuilderTest.php b/tests/src/Kernel/ListBuilderTest.php index 9db2871c..71ca76b6 100644 --- a/tests/src/Kernel/ListBuilderTest.php +++ b/tests/src/Kernel/ListBuilderTest.php @@ -4,7 +4,6 @@ namespace Drupal\Tests\oe_list_pages\Kernel; -use Drupal\oe_list_pages\ListBuilder; use Drupal\oe_list_pages\ListPageConfiguration; use Drupal\oe_list_pages\ListSourceFactory; @@ -36,18 +35,7 @@ public function testFacetCacheTags(): void { ]; $listPageConfiguration = new ListPageConfiguration($configuration); - $listExecutionManager = $this->container->get('oe_list_pages.execution_manager'); - $pager = $this->container->get('pager.manager'); - $entityRepository = $this->container->get('entity.repository'); - $formBuilder = $this->container->get('form_builder'); - $facetsUrlGenerator = $this->container->get('facets.utility.url_generator'); - $processorManager = $this->container->get('plugin.manager.facets.processor'); - $requestStack = $this->container->get('request_stack'); - $urlProcessorManager = $this->container->get('plugin.manager.facets.url_processor'); - $multiselectFilterManager = $this->container->get('plugin.manager.multiselect_filter_field'); - $listSourceFactory = $this->container->get('oe_list_pages.list_source.factory'); - - $listBuilder = new ListBuilder($listExecutionManager, $this->entityTypeManager, $pager, $entityRepository, $formBuilder, $facetsUrlGenerator, $processorManager, $requestStack, $urlProcessorManager, $multiselectFilterManager, $listSourceFactory); + $listBuilder = $this->container->get('oe_list_pages.builder'); $render_array = $listBuilder->buildList($listPageConfiguration); $expected_cache_tags = [