From 3852bb10805a4ea1781eae5895305a55d0349a58 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea <clau.cristea@gmail.com> Date: Mon, 9 Sep 2024 17:10:07 +0300 Subject: [PATCH 1/3] ISAICP-9062: Fix the state of OSS Catalogue. --- .opts.yml | 7 +- .../oss_catalogue_visibility.feature | 91 +++++++++++-------- .../dashboard/src/Form/DashboardForm.php | 44 +++++++++ .../joinup_oss_catalogue.install | 44 +++++++++ 4 files changed, 143 insertions(+), 43 deletions(-) diff --git a/.opts.yml b/.opts.yml index 6ba43c612e..08b2313c75 100644 --- a/.opts.yml +++ b/.opts.yml @@ -14,17 +14,14 @@ upgrade_commands: - rm disable-config-readonly - vendor/bin/drush joinup:unpublish-alert --category - scripts/check_status_report.php + # TODO: Remove this command in ISAICP-9053. + - vendor/bin/drush state:set joinup.oss_catalogue_is_disabled 1 --input-format=boolean append: - production: - # TODO: Remove this command in ISAICP-9053. - - vendor/bin/drush state:set joinup.oss_catalogue_is_disabled 1 --input-format=boolean acceptance: - touch disable-config-readonly - vendor/bin/drush joinup:acc - vendor/bin/drush cache:rebuild - rm disable-config-readonly - # TODO: Remove this command in ISAICP-9053. - - vendor/bin/drush state:set joinup.oss_catalogue_is_disabled 1 --input-format=boolean ephemeral: - touch disable-config-readonly - vendor/bin/drush joinup:acc diff --git a/tests/features/communities/oss_catalogue/oss_catalogue_visibility.feature b/tests/features/communities/oss_catalogue/oss_catalogue_visibility.feature index de3d282ba2..36fc5ecd6b 100644 --- a/tests/features/communities/oss_catalogue/oss_catalogue_visibility.feature +++ b/tests/features/communities/oss_catalogue/oss_catalogue_visibility.feature @@ -7,44 +7,6 @@ Feature: Test EU OSS Catalogue killswitch | title | oss_development_status | oss_source | oss_categories | oss_short_description | status | | The Opensource Panacea | development | developers_italia | content-management | The cure | published | - When I go to "/eu-oss-catalogue" - Then the response status code should be 200 - But I should not see "Sign in to continue" - And I go to "/eu-oss-catalogue/solutions" - Then the response status code should be 200 - But I should not see "Sign in to continue" - And I go to the content page of the type "oss_solution" with the title "The Opensource Panacea" - Then the response status code should be 200 - But I should not see "Sign in to continue" - - Given I am logged in as an "authenticated" - When I go to "/eu-oss-catalogue" - And I check "I agree to the Legal notice document" - And I press "Submit" - Then the response status code should be 200 - And I go to "/eu-oss-catalogue/solutions" - Then the response status code should be 200 - And I go to the content page of the type "oss_solution" with the title "The Opensource Panacea" - Then the response status code should be 200 - - Given I am logged in as a "developer" - When I go to "/eu-oss-catalogue" - Then the response status code should be 200 - And I go to "/eu-oss-catalogue/solutions" - Then the response status code should be 200 - And I go to "/admin/content/eu-oss-catalogue" - Then the response status code should be 200 - And I go to "/admin/content/eu-oss-catalogue/oss-solutions" - Then the response status code should be 200 - And I go to "/admin/content/eu-oss-catalogue/log" - Then the response status code should be 200 - And I go to the content page of the type "oss_solution" with the title "The Opensource Panacea" - Then the response status code should be 200 - - Given I visit "/dashboard" - When I press "Disable EU OSS Catalogue" - Then I should see the success message "EU OSS Catalogue has been disabled" - Given I am an anonymous user When I go to "/eu-oss-catalogue" Then I should see the heading "Sign in to continue" @@ -52,6 +14,8 @@ Feature: Test EU OSS Catalogue killswitch Then I should see the heading "Sign in to continue" And I go to the content page of the type "oss_solution" with the title "The Opensource Panacea" Then I should see the heading "Sign in to continue" + When I am on the homepage + Then I should not see "EU Open Source Solutions Catalogue" Given I am logged in as an "authenticated" When I go to "/eu-oss-catalogue" @@ -62,6 +26,8 @@ Feature: Test EU OSS Catalogue killswitch Then the response status code should be 403 And I go to the content page of the type "oss_solution" with the title "The Opensource Panacea" Then the response status code should be 403 + When I am on the homepage + Then I should not see "EU Open Source Solutions Catalogue" Given I am logged in as a "developer" When I go to "/eu-oss-catalogue" @@ -77,6 +43,8 @@ Feature: Test EU OSS Catalogue killswitch # Developer is and amin role, they can still see the node. And I go to the content page of the type "oss_solution" with the title "The Opensource Panacea" Then the response status code should be 200 + When I am on the homepage + Then I should not see "EU Open Source Solutions Catalogue" Given I visit "/dashboard" When I press "Enable EU OSS Catalogue" @@ -105,6 +73,8 @@ Feature: Test EU OSS Catalogue killswitch And I go to the content page of the type "oss_solution" with the title "The Opensource Panacea" Then the response status code should be 200 But I should not see "Sign in to continue" + When I am on the homepage + Then I should see "EU Open Source Solutions Catalogue" Given I am logged in as an "authenticated" When I go to "/eu-oss-catalogue" @@ -115,3 +85,48 @@ Feature: Test EU OSS Catalogue killswitch Then the response status code should be 200 And I go to the content page of the type "oss_solution" with the title "The Opensource Panacea" Then the response status code should be 200 + When I am on the homepage + Then I should see "EU Open Source Solutions Catalogue" + + Given I am logged in as a "developer" + When I visit "/dashboard" + And I press "Disable EU OSS Catalogue" + Then I should see the success message "EU OSS Catalogue has been disabled" + + When I go to "/eu-oss-catalogue" + Then the response status code should be 403 + And I go to "/eu-oss-catalogue/solutions" + Then the response status code should be 403 + And I go to "/admin/content/eu-oss-catalogue" + Then the response status code should be 403 + And I go to "/admin/content/eu-oss-catalogue/oss-solutions" + Then the response status code should be 403 + And I go to "/admin/content/eu-oss-catalogue/log" + Then the response status code should be 403 + # Developer is and amin role, they can still see the node. + And I go to the content page of the type "oss_solution" with the title "The Opensource Panacea" + Then the response status code should be 200 + When I am on the homepage + Then I should not see "EU Open Source Solutions Catalogue" + + Given I am an anonymous user + When I go to "/eu-oss-catalogue" + Then I should see the heading "Sign in to continue" + And I go to "/eu-oss-catalogue/solutions" + Then I should see the heading "Sign in to continue" + And I go to the content page of the type "oss_solution" with the title "The Opensource Panacea" + Then I should see the heading "Sign in to continue" + When I am on the homepage + Then I should not see "EU Open Source Solutions Catalogue" + + Given I am logged in as an "authenticated" + When I go to "/eu-oss-catalogue" + And I check "I agree to the Legal notice document" + And I press "Submit" + Then the response status code should be 403 + And I go to "/eu-oss-catalogue/solutions" + Then the response status code should be 403 + And I go to the content page of the type "oss_solution" with the title "The Opensource Panacea" + Then the response status code should be 403 + When I am on the homepage + Then I should not see "EU Open Source Solutions Catalogue" diff --git a/web/modules/custom/dashboard/src/Form/DashboardForm.php b/web/modules/custom/dashboard/src/Form/DashboardForm.php index 0c0a74d909..50bb8f838a 100644 --- a/web/modules/custom/dashboard/src/Form/DashboardForm.php +++ b/web/modules/custom/dashboard/src/Form/DashboardForm.php @@ -6,6 +6,8 @@ use Drupal\Core\Access\AccessResultInterface; use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Database\Connection; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element; @@ -13,6 +15,9 @@ use Drupal\Core\Url; use Drupal\joinup_core\Logger\Handler\MailHandler; use Drupal\joinup_core\Logger\Handler\TelegramHandler; +use Drupal\joinup_oss_catalogue\OssCatalogueCollectionInterface; +use Drupal\rdf_sync\RdfSyncConnectionInterface; +use Solarium\Core\Query\Helper; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -23,6 +28,9 @@ class DashboardForm extends FormBase { public function __construct( protected StateInterface $state, protected CacheBackendInterface $pageCache, + protected EntityTypeManagerInterface $entityTypeManager, + protected RdfSyncConnectionInterface $rdfSyncConnection, + protected Connection $db, ) { } @@ -33,6 +41,9 @@ public static function create(ContainerInterface $container): self { return new static( $container->get('state'), $container->get('cache.page'), + $container->get('entity_type.manager'), + $container->get('rdf_sync.connection'), + $container->get('database'), ); } @@ -125,6 +136,10 @@ public function buildForm(array $form, FormStateInterface $form_state): array { */ public function enableEuOssCatalogueSubmitForm(): void { $this->state->delete('joinup.oss_catalogue_is_disabled'); + $this->entityTypeManager->getStorage('rdf_entity') + ->load(OssCatalogueCollectionInterface::COLLECTION_ENTITY_ID) + ->set('field_ar_state', 'published') + ->save(); // Only clear cache for anonymous. $this->pageCache->deleteAll(); $this->messenger()->addStatus($this->t('EU OSS Catalogue has been enabled')); @@ -137,6 +152,35 @@ public function enableEuOssCatalogueSubmitForm(): void { */ public function disableEuOssCatalogueSubmitForm(): void { $this->state->set('joinup.oss_catalogue_is_disabled', TRUE); + $uri = OssCatalogueCollectionInterface::COLLECTION_ENTITY_ID; + $collection = $this->entityTypeManager->getStorage('rdf_entity')->load($uri); + $collection->set('field_ar_state', 'draft')->save(); + + // Unfortunately, we cannot unpublish a collection. When setting the state + // to 'draft', we're creating a forward revision. In order to unpublish, we: + // - Remove the published triples. + // - Remove the document from Solr and from search_api_item table. + $sparql = <<<SPARQL + WITH <http://joinup.eu/collection/published> + DELETE { <$uri> ?p ?o } + WHERE { <$uri> ?p ?o } + SPARQL; + $this->rdfSyncConnection->update($sparql); + $sapiId = 'entity:rdf_entity/' . $uri; + $solrHelper = new Helper(); + /** @var \Drupal\search_api_solr\SolrConnectorInterface $connector */ + $connector = $this->entityTypeManager->getStorage('search_api_server') + ->load('joinup')->getBackend()->getSolrConnector(); + $updateQuery = $connector + ->getUpdateQuery() + ->addDeleteQuery('ss_search_api_id:' . $solrHelper->escapePhrase($sapiId)) + ->addCommit(); + $connector->update($updateQuery); + $this->db->delete('search_api_item') + ->condition('index_id', ['published', 'unpublished'], 'IN') + ->condition('item_id', $sapiId) + ->execute(); + // Only clear cache for anonymous. $this->pageCache->deleteAll(); $this->messenger()->addStatus($this->t('EU OSS Catalogue has been disabled')); diff --git a/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.install b/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.install index 8de55f0c53..bc81a4d60b 100644 --- a/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.install +++ b/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.install @@ -8,6 +8,7 @@ declare(strict_types=1); use Drupal\joinup_oss_catalogue\OssCatalogueCollectionInterface; +use Solarium\Core\Query\Helper; /** * Publish the EU OSS Catalogue collection. @@ -18,3 +19,46 @@ function joinup_oss_catalogue_update_200001(): void { $collection->skip_notification = TRUE; $collection->set('field_ar_state', 'published')->save(); } + +/** + * Fix OSS Catalogue collection fields. + */ +function joinup_oss_catalogue_update_200002(): void { + $entityTypeManger = \Drupal::entityTypeManager(); + $uri = OssCatalogueCollectionInterface::COLLECTION_ENTITY_ID; + $collection = $entityTypeManger->getStorage('rdf_entity')->load($uri); + $collection->skip_notification = TRUE; + $collection + ->set('field_ar_state', 'draft') + ->set('field_topic', 11195) + ->save(); + + // Unfortunately, we cannot unpublish a collection. When setting the state + // to 'draft', we're creating a forward revision. + $sparql = <<<SPARQL + WITH <http://joinup.eu/collection/published> + DELETE { <$uri> ?p ?o } + WHERE { <$uri> ?p ?o } + SPARQL; + \Drupal::getContainer()->get('rdf_sync.connection')->update($sparql); + $sapiId = 'entity:rdf_entity/' . $uri; + $solrHelper = new Helper(); + /** @var \Drupal\search_api_solr\SolrConnectorInterface $connector */ + $connector = $entityTypeManger->getStorage('search_api_server') + ->load('joinup')->getBackend()->getSolrConnector(); + $updateQuery = $connector + ->getUpdateQuery() + ->addDeleteQuery('ss_search_api_id:' . $solrHelper->escapePhrase($sapiId)) + ->addCommit(); + $connector->update($updateQuery); + // Update SAPI items. + \Drupal::database()->delete('search_api_item') + ->condition('index_id', ['published', 'unpublished'], 'IN') + ->condition('item_id', $sapiId) + ->execute(); + + \Drupal::getContainer()->get('entity.repository') + ->loadEntityByUuid('node', OssCatalogueCollectionInterface::LANDING_PAGE) + ->set('field_topic', 11195) + ->save(); +} -- GitLab From 921d68d579a07d07506cebfb7a683fdab6267c68 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea <clau.cristea@gmail.com> Date: Mon, 9 Sep 2024 20:41:10 +0300 Subject: [PATCH 2/3] ISAICP-9062: Enable functionality during tests. --- .../oss_catalogue/oss_catalogue.feature | 2 +- .../oss_catalogue.relevant_language.feature | 2 +- .../src/Context/JoinupOSSSolutionContext.php | 25 +++++++- .../dashboard/src/Form/DashboardForm.php | 39 +------------ .../joinup_oss_catalogue.install | 37 ++---------- .../joinup_oss_catalogue.module | 57 +++++++++++++++++++ 6 files changed, 88 insertions(+), 74 deletions(-) diff --git a/tests/features/communities/oss_catalogue/oss_catalogue.feature b/tests/features/communities/oss_catalogue/oss_catalogue.feature index fa76710b68..212e636281 100644 --- a/tests/features/communities/oss_catalogue/oss_catalogue.feature +++ b/tests/features/communities/oss_catalogue/oss_catalogue.feature @@ -1,4 +1,4 @@ -@api @group-clone +@api @group-clone @eu-oss-catalogue Feature: In order to present the details about EU OSS catalogue I want to view a dedicated collection and landing page diff --git a/tests/features/communities/oss_catalogue/oss_catalogue.relevant_language.feature b/tests/features/communities/oss_catalogue/oss_catalogue.relevant_language.feature index db9bfc4503..b4c5fb9ce1 100644 --- a/tests/features/communities/oss_catalogue/oss_catalogue.relevant_language.feature +++ b/tests/features/communities/oss_catalogue/oss_catalogue.relevant_language.feature @@ -1,4 +1,4 @@ -@api @group-clone +@api @group-clone @eu-oss-catalogue Feature: In order to show the most data to visitors, EU OSS Catalogue provides a special equivalent of the OSS Solution's translatable fields which fetch and diff --git a/tests/src/Context/JoinupOSSSolutionContext.php b/tests/src/Context/JoinupOSSSolutionContext.php index 8f392676e1..1bc4b5a980 100644 --- a/tests/src/Context/JoinupOSSSolutionContext.php +++ b/tests/src/Context/JoinupOSSSolutionContext.php @@ -5,15 +5,12 @@ namespace Drupal\joinup\Context; use Drupal\DrupalExtension\Context\RawDrupalContext; -use Drupal\joinup\Traits\EntityTrait; /** * Behat step definitions for testing OSS solution. */ class JoinupOSSSolutionContext extends RawDrupalContext { - use EntityTrait; - /** * OSS welcome page path. */ @@ -52,4 +49,26 @@ public static function disableOssCron(): void { $state->set('eu_oss_catalogue.last_check', strtotime('- 1 hour')); } + /** + * Enables EU OSS Catalogue functionality. + * + * @BeforeScenario @api&&@eu-oss-catalogue + * + * @todo Remove this method and all @eu-oss-catalogue tags in ISAICP-9053. + */ + public function enableEuOssCatalogue(): void { + joinup_oss_catalogue_toggle('enable'); + } + + /** + * Disables EU OSS Catalogue functionality. + * + * @AfterScenario @api&&@eu-oss-catalogue + * + * @todo Remove this method and all @eu-oss-catalogue tags in ISAICP-9053. + */ + public function disableEuOssCatalogue(): void { + joinup_oss_catalogue_toggle('disable'); + } + } diff --git a/web/modules/custom/dashboard/src/Form/DashboardForm.php b/web/modules/custom/dashboard/src/Form/DashboardForm.php index 50bb8f838a..c06b5662e5 100644 --- a/web/modules/custom/dashboard/src/Form/DashboardForm.php +++ b/web/modules/custom/dashboard/src/Form/DashboardForm.php @@ -15,9 +15,7 @@ use Drupal\Core\Url; use Drupal\joinup_core\Logger\Handler\MailHandler; use Drupal\joinup_core\Logger\Handler\TelegramHandler; -use Drupal\joinup_oss_catalogue\OssCatalogueCollectionInterface; use Drupal\rdf_sync\RdfSyncConnectionInterface; -use Solarium\Core\Query\Helper; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -135,11 +133,7 @@ public function buildForm(array $form, FormStateInterface $form_state): array { * @todo Remove this method in ISAICP-9053. */ public function enableEuOssCatalogueSubmitForm(): void { - $this->state->delete('joinup.oss_catalogue_is_disabled'); - $this->entityTypeManager->getStorage('rdf_entity') - ->load(OssCatalogueCollectionInterface::COLLECTION_ENTITY_ID) - ->set('field_ar_state', 'published') - ->save(); + joinup_oss_catalogue_toggle('enable'); // Only clear cache for anonymous. $this->pageCache->deleteAll(); $this->messenger()->addStatus($this->t('EU OSS Catalogue has been enabled')); @@ -151,36 +145,7 @@ public function enableEuOssCatalogueSubmitForm(): void { * @todo Remove this method in ISAICP-9053. */ public function disableEuOssCatalogueSubmitForm(): void { - $this->state->set('joinup.oss_catalogue_is_disabled', TRUE); - $uri = OssCatalogueCollectionInterface::COLLECTION_ENTITY_ID; - $collection = $this->entityTypeManager->getStorage('rdf_entity')->load($uri); - $collection->set('field_ar_state', 'draft')->save(); - - // Unfortunately, we cannot unpublish a collection. When setting the state - // to 'draft', we're creating a forward revision. In order to unpublish, we: - // - Remove the published triples. - // - Remove the document from Solr and from search_api_item table. - $sparql = <<<SPARQL - WITH <http://joinup.eu/collection/published> - DELETE { <$uri> ?p ?o } - WHERE { <$uri> ?p ?o } - SPARQL; - $this->rdfSyncConnection->update($sparql); - $sapiId = 'entity:rdf_entity/' . $uri; - $solrHelper = new Helper(); - /** @var \Drupal\search_api_solr\SolrConnectorInterface $connector */ - $connector = $this->entityTypeManager->getStorage('search_api_server') - ->load('joinup')->getBackend()->getSolrConnector(); - $updateQuery = $connector - ->getUpdateQuery() - ->addDeleteQuery('ss_search_api_id:' . $solrHelper->escapePhrase($sapiId)) - ->addCommit(); - $connector->update($updateQuery); - $this->db->delete('search_api_item') - ->condition('index_id', ['published', 'unpublished'], 'IN') - ->condition('item_id', $sapiId) - ->execute(); - + joinup_oss_catalogue_toggle('disable'); // Only clear cache for anonymous. $this->pageCache->deleteAll(); $this->messenger()->addStatus($this->t('EU OSS Catalogue has been disabled')); diff --git a/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.install b/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.install index bc81a4d60b..ce8d1aa03f 100644 --- a/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.install +++ b/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.install @@ -8,7 +8,6 @@ declare(strict_types=1); use Drupal\joinup_oss_catalogue\OssCatalogueCollectionInterface; -use Solarium\Core\Query\Helper; /** * Publish the EU OSS Catalogue collection. @@ -24,38 +23,12 @@ function joinup_oss_catalogue_update_200001(): void { * Fix OSS Catalogue collection fields. */ function joinup_oss_catalogue_update_200002(): void { - $entityTypeManger = \Drupal::entityTypeManager(); - $uri = OssCatalogueCollectionInterface::COLLECTION_ENTITY_ID; - $collection = $entityTypeManger->getStorage('rdf_entity')->load($uri); - $collection->skip_notification = TRUE; - $collection - ->set('field_ar_state', 'draft') - ->set('field_topic', 11195) - ->save(); + joinup_oss_catalogue_toggle('disable'); - // Unfortunately, we cannot unpublish a collection. When setting the state - // to 'draft', we're creating a forward revision. - $sparql = <<<SPARQL - WITH <http://joinup.eu/collection/published> - DELETE { <$uri> ?p ?o } - WHERE { <$uri> ?p ?o } - SPARQL; - \Drupal::getContainer()->get('rdf_sync.connection')->update($sparql); - $sapiId = 'entity:rdf_entity/' . $uri; - $solrHelper = new Helper(); - /** @var \Drupal\search_api_solr\SolrConnectorInterface $connector */ - $connector = $entityTypeManger->getStorage('search_api_server') - ->load('joinup')->getBackend()->getSolrConnector(); - $updateQuery = $connector - ->getUpdateQuery() - ->addDeleteQuery('ss_search_api_id:' . $solrHelper->escapePhrase($sapiId)) - ->addCommit(); - $connector->update($updateQuery); - // Update SAPI items. - \Drupal::database()->delete('search_api_item') - ->condition('index_id', ['published', 'unpublished'], 'IN') - ->condition('item_id', $sapiId) - ->execute(); + $collection = \Drupal::entityTypeManager()->getStorage('rdf_entity') + ->load(OssCatalogueCollectionInterface::COLLECTION_ENTITY_ID); + $collection->skip_notification = TRUE; + $collection->set('field_topic', 11195)->save(); \Drupal::getContainer()->get('entity.repository') ->loadEntityByUuid('node', OssCatalogueCollectionInterface::LANDING_PAGE) diff --git a/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.module b/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.module index 8212527be9..0adfdf2379 100644 --- a/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.module +++ b/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.module @@ -20,6 +20,7 @@ use Drupal\joinup_oss_catalogue\Entity\JoinupOssSolution; use Drupal\joinup_oss_catalogue\OssCatalogueCollectionInterface; use Drupal\node\NodeInterface; +use Solarium\Core\Query\Helper; /** * Implements hook_views_plugins_argument_alter(). @@ -247,3 +248,59 @@ function joinup_oss_catalogue_cron(): void { eu_oss_vitality_index_cron(); } } + +/** + * Toggles the state of EU OSS Catalogue functionality. + * + * @param string $toggle + * The state: 'enable', 'disable'. + * + * @todo Remove this function in ISAICP-9053. + */ +function joinup_oss_catalogue_toggle(string $toggle): void { + assert(in_array($toggle, ['enable', 'disable'], TRUE)); + $entityTypeManager = \Drupal::entityTypeManager(); + $state = \Drupal::state(); + + if ($toggle === 'enable') { + $state->delete('joinup.oss_catalogue_is_disabled'); + $collection = $entityTypeManager->getStorage('rdf_entity') + ->load(OssCatalogueCollectionInterface::COLLECTION_ENTITY_ID); + $collection->skip_notification = TRUE; + $collection->set('field_ar_state', 'published')->save(); + return; + } + + $state->set('joinup.oss_catalogue_is_disabled', TRUE); + + $uri = OssCatalogueCollectionInterface::COLLECTION_ENTITY_ID; + $collection = \Drupal::entityTypeManager()->getStorage('rdf_entity')->load($uri); + $collection->skip_notification = TRUE; + $collection->set('field_ar_state', 'draft')->save(); + + // Unfortunately, we cannot unpublish a collection. When setting the state + // to 'draft', we're creating a forward revision. In order to unpublish, we: + // - Remove the published triples. + // - Remove the document from Solr and from search_api_item table. + $sparql = <<<SPARQL + WITH <http://joinup.eu/collection/published> + DELETE { <$uri> ?p ?o } + WHERE { <$uri> ?p ?o } + SPARQL; + \Drupal::getContainer()->get('rdf_sync.connection')->update($sparql); + $sapiId = 'entity:rdf_entity/' . $uri; + $solrHelper = new Helper(); + /** @var \Drupal\search_api_solr\SolrConnectorInterface $connector */ + $connector = $entityTypeManager->getStorage('search_api_server')->load('joinup') + ->getBackend() + ->getSolrConnector(); + $deleteQuery = $connector + ->getUpdateQuery() + ->addDeleteQuery('ss_search_api_id:' . $solrHelper->escapePhrase($sapiId)) + ->addCommit(); + $connector->update($deleteQuery); + \Drupal::database()->delete('search_api_item') + ->condition('index_id', ['published', 'unpublished'], 'IN') + ->condition('item_id', $sapiId) + ->execute(); +} -- GitLab From f6231a16b913021935a336261a48d8546559ba77 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea <clau.cristea@gmail.com> Date: Tue, 10 Sep 2024 11:33:26 +0300 Subject: [PATCH 3/3] ISAICP-9062: Rely on collection state. --- .opts.yml | 2 - .../oss_catalogue_visibility.feature | 2 + .../dashboard/src/Form/DashboardForm.php | 4 +- .../joinup_oss_catalogue.install | 3 + .../joinup_oss_catalogue.module | 100 ++++++++++-------- .../src/Access/KillSwitchCheck.php | 3 +- .../Commands/JoinupOssCatalogueCommands.php | 4 +- .../JoinupDataSanitizationCommands.php | 17 --- .../src/Plugin/Block/ExploreBlock.php | 2 +- 9 files changed, 67 insertions(+), 70 deletions(-) diff --git a/.opts.yml b/.opts.yml index 08b2313c75..1ae028cf44 100644 --- a/.opts.yml +++ b/.opts.yml @@ -14,8 +14,6 @@ upgrade_commands: - rm disable-config-readonly - vendor/bin/drush joinup:unpublish-alert --category - scripts/check_status_report.php - # TODO: Remove this command in ISAICP-9053. - - vendor/bin/drush state:set joinup.oss_catalogue_is_disabled 1 --input-format=boolean append: acceptance: - touch disable-config-readonly diff --git a/tests/features/communities/oss_catalogue/oss_catalogue_visibility.feature b/tests/features/communities/oss_catalogue/oss_catalogue_visibility.feature index 36fc5ecd6b..d1970cbae4 100644 --- a/tests/features/communities/oss_catalogue/oss_catalogue_visibility.feature +++ b/tests/features/communities/oss_catalogue/oss_catalogue_visibility.feature @@ -62,6 +62,8 @@ Feature: Test EU OSS Catalogue killswitch Then the response status code should be 200 And I go to the content page of the type "oss_solution" with the title "The Opensource Panacea" Then the response status code should be 200 + When I am on the homepage + Then I should see "EU Open Source Solutions Catalogue" Given I am an anonymous user When I go to "/eu-oss-catalogue" diff --git a/web/modules/custom/dashboard/src/Form/DashboardForm.php b/web/modules/custom/dashboard/src/Form/DashboardForm.php index c06b5662e5..0f02afb930 100644 --- a/web/modules/custom/dashboard/src/Form/DashboardForm.php +++ b/web/modules/custom/dashboard/src/Form/DashboardForm.php @@ -114,14 +114,14 @@ public function buildForm(array $form, FormStateInterface $form_state): array { '#type' => 'submit', '#value' => $this->t('Enable EU OSS Catalogue'), '#submit' => ['::enableEuOssCatalogueSubmitForm'], - '#access' => $hasEnableDisablePermission && !joinup_oss_catalogue_is_enabled(), + '#access' => $hasEnableDisablePermission && !joinup_oss_catalogue_feature()->isAllowed(), ]; // @todo Remove this button in ISAICP-9053. $form['actions']['disable'] = [ '#type' => 'submit', '#value' => $this->t('Disable EU OSS Catalogue'), '#submit' => ['::disableEuOssCatalogueSubmitForm'], - '#access' => $hasEnableDisablePermission && joinup_oss_catalogue_is_enabled(), + '#access' => $hasEnableDisablePermission && joinup_oss_catalogue_feature()->isAllowed(), ]; return $form; diff --git a/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.install b/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.install index ce8d1aa03f..a91e60a0c8 100644 --- a/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.install +++ b/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.install @@ -34,4 +34,7 @@ function joinup_oss_catalogue_update_200002(): void { ->loadEntityByUuid('node', OssCatalogueCollectionInterface::LANDING_PAGE) ->set('field_topic', 11195) ->save(); + + // We're not using anymore this switch. + \Drupal::state()->delete('joinup.oss_catalogue_is_disabled'); } diff --git a/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.module b/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.module index 0adfdf2379..9eedf4aabb 100644 --- a/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.module +++ b/web/modules/custom/joinup_communities/joinup_oss_catalogue/joinup_oss_catalogue.module @@ -191,13 +191,25 @@ function joinup_oss_catalogue_preprocess_views_view__eu_oss_catalogue_admin_caro /** * Checks whether the EU OSS Catalogue functionality is enabled. * - * @return bool - * Whether the EU OSS Catalogue functionality is enabled. + * @return \Drupal\Core\Access\AccessResultInterface + * Access result. * * @todo Remove this function in ISAICP-9053. */ -function joinup_oss_catalogue_is_enabled(): bool { - return !\Drupal::state()->get('joinup.oss_catalogue_is_disabled', FALSE); +function joinup_oss_catalogue_feature(): AccessResultInterface { + static $cache; + if (!isset($cache)) { + $collection = \Drupal::entityTypeManager()->getStorage('rdf_entity') + ->load(OssCatalogueCollectionInterface::COLLECTION_ENTITY_ID); + if (!$collection instanceof CollectionInterface) { + $cache = AccessResult::allowed(); + } + else { + $state = $collection->get('field_ar_state')->first()->getValue()['value'] ?? NULL; + $cache = AccessResult::allowedIf($state === 'published')->addCacheableDependency($collection); + } + } + return $cache; } /** @@ -215,7 +227,7 @@ function joinup_oss_catalogue_entity_access(EntityInterface $entity): AccessResu ($entity instanceof OssSolutionInterface) ) { return AccessResult::forbiddenIf( - !joinup_oss_catalogue_is_enabled(), + !joinup_oss_catalogue_feature()->isAllowed(), 'EU OSS Catalogue is disabled', )->addCacheableDependency($entity); } @@ -242,7 +254,7 @@ function joinup_oss_catalogue_module_implements_alter(array &$implementations, s * @todo Remove this function in ISAICP-9053. */ function joinup_oss_catalogue_cron(): void { - if (joinup_oss_catalogue_is_enabled()) { + if (joinup_oss_catalogue_feature()->isAllowed()) { // Only run if EU OSS Catalogue functionality is enabled. eu_oss_catalogue_cron(); eu_oss_vitality_index_cron(); @@ -259,48 +271,48 @@ function joinup_oss_catalogue_cron(): void { */ function joinup_oss_catalogue_toggle(string $toggle): void { assert(in_array($toggle, ['enable', 'disable'], TRUE)); - $entityTypeManager = \Drupal::entityTypeManager(); - $state = \Drupal::state(); - if ($toggle === 'enable') { - $state->delete('joinup.oss_catalogue_is_disabled'); - $collection = $entityTypeManager->getStorage('rdf_entity') - ->load(OssCatalogueCollectionInterface::COLLECTION_ENTITY_ID); - $collection->skip_notification = TRUE; - $collection->set('field_ar_state', 'published')->save(); + $entityTypeManager = \Drupal::entityTypeManager(); + $uri = OssCatalogueCollectionInterface::COLLECTION_ENTITY_ID; + if (!$collection = $entityTypeManager->getStorage('rdf_entity')->load($uri)) { + // If the EU OSS Catalogue collection is missing exit there's nothing to do. return; } - $state->set('joinup.oss_catalogue_is_disabled', TRUE); - - $uri = OssCatalogueCollectionInterface::COLLECTION_ENTITY_ID; - $collection = \Drupal::entityTypeManager()->getStorage('rdf_entity')->load($uri); $collection->skip_notification = TRUE; - $collection->set('field_ar_state', 'draft')->save(); + $state = $toggle === 'enable' ? 'published' : 'draft'; + $collection->set('field_ar_state', $state)->save(); + + if ($toggle === 'disable') { + // Unfortunately, we don't have an API to unpublish a collection because + // when setting the state field to 'draft', we're creating a forward + // revision and the published version still exist and is shown on UIs. In + // order to unpublish, we perform the following operations: + // - Remove the published triples. + // - Remove the document from Solr and from search_api_item table. + $sparql = <<<SPARQL + WITH <http://joinup.eu/collection/published> + DELETE { <$uri> ?p ?o } + WHERE { <$uri> ?p ?o } + SPARQL; + \Drupal::getContainer()->get('rdf_sync.connection')->update($sparql); + $sapiId = 'entity:rdf_entity/' . $uri; + $solrHelper = new Helper(); + /** @var \Drupal\search_api_solr\SolrConnectorInterface $connector */ + $connector = $entityTypeManager->getStorage('search_api_server')->load('joinup') + ->getBackend() + ->getSolrConnector(); + $deleteQuery = $connector + ->getUpdateQuery() + ->addDeleteQuery('ss_search_api_id:' . $solrHelper->escapePhrase($sapiId)) + ->addCommit(); + $connector->update($deleteQuery); + \Drupal::database()->delete('search_api_item') + ->condition('index_id', ['published', 'unpublished'], 'IN') + ->condition('item_id', $sapiId) + ->execute(); + } - // Unfortunately, we cannot unpublish a collection. When setting the state - // to 'draft', we're creating a forward revision. In order to unpublish, we: - // - Remove the published triples. - // - Remove the document from Solr and from search_api_item table. - $sparql = <<<SPARQL - WITH <http://joinup.eu/collection/published> - DELETE { <$uri> ?p ?o } - WHERE { <$uri> ?p ?o } - SPARQL; - \Drupal::getContainer()->get('rdf_sync.connection')->update($sparql); - $sapiId = 'entity:rdf_entity/' . $uri; - $solrHelper = new Helper(); - /** @var \Drupal\search_api_solr\SolrConnectorInterface $connector */ - $connector = $entityTypeManager->getStorage('search_api_server')->load('joinup') - ->getBackend() - ->getSolrConnector(); - $deleteQuery = $connector - ->getUpdateQuery() - ->addDeleteQuery('ss_search_api_id:' . $solrHelper->escapePhrase($sapiId)) - ->addCommit(); - $connector->update($deleteQuery); - \Drupal::database()->delete('search_api_item') - ->condition('index_id', ['published', 'unpublished'], 'IN') - ->condition('item_id', $sapiId) - ->execute(); + // The 'Explore' block caches is time-based (6h). Explicitly clear the cache. + \Drupal::getContainer()->get('cache_tags.invalidator')->invalidateTags(['homepage_explore_block']); } diff --git a/web/modules/custom/joinup_communities/joinup_oss_catalogue/src/Access/KillSwitchCheck.php b/web/modules/custom/joinup_communities/joinup_oss_catalogue/src/Access/KillSwitchCheck.php index 5c68df86ad..b7f3a37723 100644 --- a/web/modules/custom/joinup_communities/joinup_oss_catalogue/src/Access/KillSwitchCheck.php +++ b/web/modules/custom/joinup_communities/joinup_oss_catalogue/src/Access/KillSwitchCheck.php @@ -4,7 +4,6 @@ namespace Drupal\joinup_oss_catalogue\Access; -use Drupal\Core\Access\AccessResult; use Drupal\Core\Access\AccessResultInterface; use Drupal\Core\Routing\Access\AccessInterface; use Drupal\Core\Routing\RouteMatchInterface; @@ -33,7 +32,7 @@ public function access(Route $route, RouteMatchInterface $routeMatch): AccessRes if ($requirement !== 'TRUE') { throw new \Exception("The _eu_oss_catalogue_check_enabled route requirement when present can only have 'TRUE' as value but '$requirement' was given."); } - return joinup_oss_catalogue_is_enabled() ? AccessResult::allowed() : AccessResult::forbidden('EU OSS Catalogue functionality is disabled'); + return joinup_oss_catalogue_feature(); } } diff --git a/web/modules/custom/joinup_communities/joinup_oss_catalogue/src/Drush/Commands/JoinupOssCatalogueCommands.php b/web/modules/custom/joinup_communities/joinup_oss_catalogue/src/Drush/Commands/JoinupOssCatalogueCommands.php index 78e167c5de..6c5ca72201 100644 --- a/web/modules/custom/joinup_communities/joinup_oss_catalogue/src/Drush/Commands/JoinupOssCatalogueCommands.php +++ b/web/modules/custom/joinup_communities/joinup_oss_catalogue/src/Drush/Commands/JoinupOssCatalogueCommands.php @@ -40,7 +40,7 @@ public function __construct( */ #[CLI\Hook(type: HookManager::REPLACE_COMMAND_HOOK, target: 'eu-oss:fetch')] public function fetch(string $provider): string { - if (joinup_oss_catalogue_is_enabled()) { + if (joinup_oss_catalogue_feature()->isAllowed()) { $commands = new EuOssCatalogueCommands($this->euOssCatalogueService, $this->providerPluginManager); return $commands->fetch($provider); } @@ -52,7 +52,7 @@ public function fetch(string $provider): string { */ #[CLI\Hook(type: HookManager::REPLACE_COMMAND_HOOK, target: 'eu-oss:generate-vitality-index')] public function generate(string $nid): string { - if (joinup_oss_catalogue_is_enabled()) { + if (joinup_oss_catalogue_feature()->isAllowed()) { $commands = new EuOssVitalityIndexCommands($this->vitalityIndexGenerator, $this->entityTypeManager); return $commands->generate($nid); } diff --git a/web/modules/custom/joinup_core/src/Commands/JoinupDataSanitizationCommands.php b/web/modules/custom/joinup_core/src/Commands/JoinupDataSanitizationCommands.php index 8734c8dff3..fc64beb5af 100644 --- a/web/modules/custom/joinup_core/src/Commands/JoinupDataSanitizationCommands.php +++ b/web/modules/custom/joinup_core/src/Commands/JoinupDataSanitizationCommands.php @@ -47,8 +47,6 @@ public function sanitize($result, CommandData $commandData): void { $this->sanitizeLogAlertsSettings(); $this->sanitizeEmailConfirmerData(); $this->sanitizeDownloadEventData(); - // @todo Remove this call in ISAICP-9053. - $this->sanitizeEuOssCatalogueSwitcher(); } /** @@ -62,8 +60,6 @@ public function messages(&$messages, InputInterface $input): void { $messages[] = dt('Sanitize log alerts settings.'); $messages[] = dt('Sanitize email confirmer data.'); $messages[] = dt('Sanitize download event data.'); - // @todo Remove this message in ISAICP-9053. - $messages[] = dt('Sanitize EU OSS Catalogue switcher.'); } /** @@ -183,17 +179,4 @@ protected function sanitizeEntityFieldsData(string $entityType, array $fieldName } } - /** - * Sanitizes the EU OSS Catalogue switcher. - * - * @todo Remove this method in ISAICP-9053. - */ - protected function sanitizeEuOssCatalogueSwitcher(): void { - $this->db->delete('key_value') - ->condition('collection', 'state') - ->condition('name', 'joinup.oss_catalogue_is_disabled') - ->execute(); - $this->logger()->success(dt('joinup.oss_catalogue_is_disabled state var sanitized.')); - } - } diff --git a/web/modules/custom/joinup_front_page/src/Plugin/Block/ExploreBlock.php b/web/modules/custom/joinup_front_page/src/Plugin/Block/ExploreBlock.php index 2a23ee1dca..bd560af3ac 100644 --- a/web/modules/custom/joinup_front_page/src/Plugin/Block/ExploreBlock.php +++ b/web/modules/custom/joinup_front_page/src/Plugin/Block/ExploreBlock.php @@ -138,7 +138,7 @@ public function getCacheTags(): array { // whenever any of the content types shown in it change. The front page is // heavily trafficked and too frequent cache invalidations might affect // performance. - return []; + return ['homepage_explore_block']; } /** -- GitLab