diff --git a/includes/classes/Indexable/Post/SyncManager.php b/includes/classes/Indexable/Post/SyncManager.php index a5feafcaa..7eadf049b 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 000000000..d10261874 --- /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 a97135c13..942d8703f 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);