From 1f22e8699f847adcb8e2527d1ceccba5164bcd51 Mon Sep 17 00:00:00 2001 From: Dik Takken Date: Tue, 11 Jun 2024 16:30:42 +0200 Subject: [PATCH] Remove post from queue when not indexable This change fixes updates that trigger multiple filters for the same post. Currently, when any of the filters adds the post to the update queue the post is indexed, even when other filters determine that the post should not be indexed. This change makes the last filter have the final word about this. --- .../classes/Indexable/Post/SyncManager.php | 3 ++ .../features/protected-content-removal.cy.js | 47 +++++++++++++++++++ tests/cypress/support/commands.js | 20 ++++++++ 3 files changed, 70 insertions(+) create mode 100644 tests/cypress/integration/features/protected-content-removal.cy.js diff --git a/includes/classes/Indexable/Post/SyncManager.php b/includes/classes/Indexable/Post/SyncManager.php index a5feafcaa0..7eadf049bc 100644 --- a/includes/classes/Indexable/Post/SyncManager.php +++ b/includes/classes/Indexable/Post/SyncManager.php @@ -533,6 +533,7 @@ public function action_set_object_terms( $post_id, $terms, $tt_ids, $taxonomy, $ } if ( ! $this->should_reindex_post( $post_id, $taxonomy ) ) { + $this->remove_from_queue( $post_id ); return; } @@ -629,6 +630,7 @@ public function action_edited_term( $term_id, $tt_id, $taxonomy ) { // Add all of them to the queue foreach ( $object_ids as $post_id ) { if ( ! $this->should_reindex_post( $post_id, $taxonomy ) ) { + $this->remove_from_queue( $post_id ); continue; } @@ -693,6 +695,7 @@ public function action_deleted_term_relationships( $post_id, $tt_ids, $taxonomy } if ( ! $this->should_reindex_post( $post_id, $taxonomy ) ) { + $this->remove_from_queue( $post_id ); return; } diff --git a/tests/cypress/integration/features/protected-content-removal.cy.js b/tests/cypress/integration/features/protected-content-removal.cy.js new file mode 100644 index 0000000000..d10261874a --- /dev/null +++ b/tests/cypress/integration/features/protected-content-removal.cy.js @@ -0,0 +1,47 @@ +describe('Set password on post', () => { + it('Removes Post from Index when Password is Set', () => { + cy.login(); + cy.maybeDisableFeature('protected_content'); + + // Delete previous posts, so we can be sure we just expect 1 post. + cy.wpCli('post list --format=ids').then((wpCliResponse) => { + if (wpCliResponse.stdout !== '') { + cy.wpCli(`post delete ${wpCliResponse.stdout}`); + } + }); + + cy.publishPost({ + title: 'Protected Post Removal Test', + }); + + /** + * Give Elasticsearch some time to process the new post. + * + * @todo instead of waiting for an arbitrary time, we should ensure the post is stored. + */ + // eslint-disable-next-line cypress/no-unnecessary-waiting + cy.wait(2000); + + // Post is indexed + cy.visit('/?s=Protected+Post+Removal+Test'); + cy.contains('.site-content article h2', 'Protected Post Removal Test').should('exist'); + + cy.wpCli('post list --format=ids').then((wpCliResponse) => { + if (wpCliResponse.stdout !== '') { + cy.postSetPassword(wpCliResponse.stdout, 'enter'); + } + }); + + /** + * Give Elasticsearch some time to process the update. + * + * @todo instead of waiting for an arbitrary time, we should ensure the post is stored. + */ + // eslint-disable-next-line cypress/no-unnecessary-waiting + cy.wait(2000); + + // Post is removed from index + cy.visit('/?s=Protected+Post+Removal+Test'); + cy.contains('.site-content article h2', 'Protected Post Removal Test').should('not.exist'); + }); +}); diff --git a/tests/cypress/support/commands.js b/tests/cypress/support/commands.js index a97135c136..942d8703f4 100644 --- a/tests/cypress/support/commands.js +++ b/tests/cypress/support/commands.js @@ -176,6 +176,26 @@ Cypress.Commands.add('publishPost', (postData, viewPost) => { cy.wait(2000); }); +Cypress.Commands.add('postSetPassword', (id, password) => { + cy.visitAdminPage(`post.php?post=${id}&action=edit`); + cy.get('h1.editor-post-title__input').click(); + cy.get('body').then(($body) => { + const $button = $body.find('.edit-post-post-visibility__toggle'); + if (!$button.is(':visible')) { + cy.get('.edit-post-header__settings button[aria-label="Settings"]').click(); + } + }); + cy.get('.edit-post-post-visibility__toggle').click(); + cy.get('.editor-post-visibility__dialog-radio, .editor-post-visibility__radio').check( + 'password', + ); + cy.get( + '.editor-post-visibility__dialog-password-input, .editor-post-visibility__password-input', + ).type(password); + + cy.get('.editor-post-publish-button').click(); +}); + Cypress.Commands.add('updateFeatures', (featureName, newValues) => { const escapedNewValues = JSON.stringify(newValues);