From faffe6f2bb54f94e193594e60e65cdc68a8a68c5 Mon Sep 17 00:00:00 2001 From: Ilias Dimopoulos <idimopoulos@hotmail.com> Date: Mon, 3 Mar 2025 13:14:24 +0200 Subject: [PATCH] ISAICP-9536: Add an update hook to delete the orphaned entities. --- .../group.delete_with_content.feature | 34 +++++++++++++++++++ tests/features/update/ISAICP-9536.feature | 6 ++++ tests/src/Context/DebugContext.php | 21 ++++++++++++ .../custom/joinup_core/joinup_core.deploy.php | 33 ++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 tests/features/joinup_group/group.delete_with_content.feature create mode 100644 tests/features/update/ISAICP-9536.feature diff --git a/tests/features/joinup_group/group.delete_with_content.feature b/tests/features/joinup_group/group.delete_with_content.feature new file mode 100644 index 0000000000..063429b11e --- /dev/null +++ b/tests/features/joinup_group/group.delete_with_content.feature @@ -0,0 +1,34 @@ +@api @group-e +Feature: + In order to not leave orphaned entities + As a collection owner + I need to be unable to delete groups that have group content. + + Scenario Outline: Delete a group with group content + Given <group type> content: + | title | state | + | <group label> with children | <state> | + And news content: + | title | state | + | News | published | + And event content: + | title | state | + | Event | published | + And document content: + | title | state | + | Document | published | + And discussion content: + | title | state | + | Discussion | published | + + When I am logged in as a moderator + And I go to the delete form of the "<group label> with children" <group type> + And I press "Delete" + Then I should see the text "The <group label> <group label> with children has been deleted." + When I go to the "Discussion" discussion + Then I should not see the heading "Discussion" + + Examples: + | group label | group type | + | Collection | collection | + | Solution | solution | diff --git a/tests/features/update/ISAICP-9536.feature b/tests/features/update/ISAICP-9536.feature new file mode 100644 index 0000000000..ef8f1f5b84 --- /dev/null +++ b/tests/features/update/ISAICP-9536.feature @@ -0,0 +1,6 @@ +@api @group-clone +Feature: Test orphaned content + + @update:joinup_core_deploy_200305 + Scenario: Assert that no orphaned content exist in the website. + Then no orphaned content should exist in the website diff --git a/tests/src/Context/DebugContext.php b/tests/src/Context/DebugContext.php index c4a9490bc4..cc1c071a94 100644 --- a/tests/src/Context/DebugContext.php +++ b/tests/src/Context/DebugContext.php @@ -24,4 +24,25 @@ public function printMessages(): void { } } + /** + * Asserts that no orphaned content exists in the website. + * + * @Then /^no orphaned content should exist in the website$/ + */ + public function noOprhanedContentShouldExistInTheWebsite(): void { + $query = <<<SQL +SELECT n1.nid, n1.type, n1.title, n2.type, n2.title +FROM node_field_data n1 + LEFT JOIN node__og_audience oa ON n1.nid = oa.entity_id + LEFT JOIN node_field_data n2 ON oa.og_audience_target_id = n2.nid +WHERE n1.type IN('news', 'discussion', 'document', 'event', 'solution', 'distribution', 'release') + AND n2.nid IS NULL +SQL; + + $nids = \Drupal::database()->query($query)->fetchCol(); + if (!empty($nids)) { + throw new \Exception('Orphaned content found: ' . implode(', ', $nids)); + } + } + } diff --git a/web/modules/custom/joinup_core/joinup_core.deploy.php b/web/modules/custom/joinup_core/joinup_core.deploy.php index b989aa0b66..0130856b02 100644 --- a/web/modules/custom/joinup_core/joinup_core.deploy.php +++ b/web/modules/custom/joinup_core/joinup_core.deploy.php @@ -183,3 +183,36 @@ function joinup_core_deploy_200304(): string { \Drupal::state()->delete('joinup_core_deploy_200200'); return 'Solution type field and revision values have been restored.'; } + +/** + * Delete content of an orphaned group. + */ +function joinup_core_deploy_200305(array &$sandbox): string { + if (empty($sandbox['nids'])) { + $query = <<<SQL +SELECT n1.nid, n1.type, n1.title, n2.type, n2.title +FROM node_field_data n1 + LEFT JOIN node__og_audience oa ON n1.nid = oa.entity_id + LEFT JOIN node_field_data n2 ON oa.og_audience_target_id = n2.nid +WHERE n1.type IN('news', 'discussion', 'document', 'event', 'solution', 'distribution', 'release') + AND n2.nid IS NULL +SQL; + + $sandbox['nids'] = \Drupal::database()->query($query)->fetchCol(); + if (empty($sandbox['nids'])) { + return 'No orphaned content found.'; + } + + $sandbox['total'] = count($sandbox['nids']); + $sandbox['progress'] = 0; + } + + $nids = array_splice($sandbox['nids'], 0, 100); + $storage = \Drupal::entityTypeManager()->getStorage('node'); + $storage->delete($storage->loadMultiple($nids)); + + $sandbox['progress'] += count($nids); + $sandbox['#finished'] = (int) empty($sandbox['nids']); + + return "Deleted {$sandbox['progress']} out of {$sandbox['total']} orphaned content."; +} -- GitLab