From 48bf613d4ef6546e11753263d5b96c58397cfca5 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Wed, 7 Sep 2022 17:26:59 +0500 Subject: [PATCH 1/8] Add Support for site__not_in --- .../Indexable/Comment/QueryIntegration.php | 41 +- .../Indexable/Post/QueryIntegration.php | 41 +- .../Indexable/Term/QueryIntegration.php | 39 +- tests/php/bootstrap.php | 2 + tests/php/includes/classes/BaseTestCase.php | 22 + .../classes/factory/CategoryFactory.php | 34 ++ .../classes/factory/CommentFactory.php | 32 ++ tests/php/indexables/TestCommentMultisite.php | 253 ++++++++++++ tests/php/indexables/TestPostMultisite.php | 318 +++++++++++++-- tests/php/indexables/TestTermMultisite.php | 375 ++++++++++++++++++ 10 files changed, 1102 insertions(+), 55 deletions(-) create mode 100644 tests/php/includes/classes/factory/CategoryFactory.php create mode 100644 tests/php/includes/classes/factory/CommentFactory.php create mode 100644 tests/php/indexables/TestCommentMultisite.php create mode 100644 tests/php/indexables/TestTermMultisite.php diff --git a/includes/classes/Indexable/Comment/QueryIntegration.php b/includes/classes/Indexable/Comment/QueryIntegration.php index 8f279b298a..06b95d4d9f 100644 --- a/includes/classes/Indexable/Comment/QueryIntegration.php +++ b/includes/classes/Indexable/Comment/QueryIntegration.php @@ -116,8 +116,24 @@ public function maybe_filter_query( $results, WP_Comment_Query $query ) { $formatted_args = $this->indexable->format_args( $query->query_vars ); $scope = 'current'; + + $site__in = []; + $site__not_in = []; + if ( ! empty( $query->query_vars['sites'] ) ) { - $scope = $query->query_vars['sites']; + + _deprecated_argument( __FUNCTION__, '4.4.0', esc_html__( 'sites is deprecated. Use site__in instead.', 'elasticpress' ) ); + $site__in = ! is_array( $query->query_vars['sites'] ) ? array( $query->query_vars['sites'] ) : $query->query_vars['sites']; + $scope = 'all' === $query->query_vars['sites'] ? 'all' : $site__in; + } + + if ( ! empty( $query->query_vars['site__in'] ) ) { + $site__in = ! is_array( $query->query_vars['site__in'] ) ? array( $query->query_vars['site__in'] ) : $query->query_vars['site__in']; + $scope = 'all' === $query->query_vars['site__in'] ? 'all' : $site__in; + } + + if ( ! empty( $query->query_vars['site__not_in'] ) ) { + $site__not_in = ! is_array( $query->query_vars['site__not_in'] ) ? array( $query->query_vars['site__not_in'] ) : $query->query_vars['site__not_in']; } /** @@ -138,16 +154,31 @@ public function maybe_filter_query( $results, WP_Comment_Query $query ) { if ( 'all' === $scope ) { $this->index = $this->indexable->get_network_alias(); - } elseif ( is_numeric( $scope ) ) { - $this->index = $this->indexable->get_index_name( (int) $scope ); - } elseif ( is_array( $scope ) ) { + } elseif ( ! empty( $site__in ) ) { $this->index = []; - foreach ( $scope as $site_id ) { + foreach ( $site__in as $site_id ) { $this->index[] = $this->indexable->get_index_name( $site_id ); } $this->index = implode( ',', $this->index ); + } elseif ( ! empty( $site__not_in ) ) { + + $sites = get_sites( + array( + 'fields' => 'ids', + 'site__not_in' => $site__not_in, + ) + ); + foreach ( $sites as $site_id ) { + if ( ! Utils\is_site_indexable( $site_id ) ) { + continue; + } + $index[] = Indexables::factory()->get( 'post' )->get_index_name( $site_id ); + } + + $this->index = implode( ',', $index ); + } $ep_query = $this->indexable->query_es( $formatted_args, $query->query_vars, $this->index, $query ); diff --git a/includes/classes/Indexable/Post/QueryIntegration.php b/includes/classes/Indexable/Post/QueryIntegration.php index d46e1be3c7..82f366adf7 100644 --- a/includes/classes/Indexable/Post/QueryIntegration.php +++ b/includes/classes/Indexable/Post/QueryIntegration.php @@ -275,8 +275,25 @@ public function get_es_posts( $posts, $query ) { if ( count( $new_posts ) < 1 ) { $scope = 'current'; + + $site__in = ''; + $site__not_in = ''; + if ( ! empty( $query_vars['sites'] ) ) { - $scope = $query_vars['sites']; + + _deprecated_argument( __FUNCTION__, '4.4.0', esc_html__( 'sites is deprecated. Use site__in instead.', 'elasticpress' ) ); + $site__in = ! is_array( $query_vars['sites'] ) ? array( $query_vars['sites'] ) : $query_vars['sites']; + $scope = 'all' === $query_vars['sites'] ? 'all' : $site__in; + } + + if ( ! empty( $query_vars['site__in'] ) ) { + + $site__in = ! is_array( $query_vars['site__in'] ) ? array( $query_vars['site__in'] ) : $query_vars['site__in']; + $scope = 'all' === $query_vars['site__in'] ? 'all' : $site__in; + } + + if ( ! empty( $query_vars['site__not_in'] ) ) { + $site__not_in = ! is_array( $query_vars['site__not_in'] ) ? array( $query_vars['site__not_in'] ) : $query_vars['site__not_in']; } $formatted_args = Indexables::factory()->get( 'post' )->format_args( $query_vars, $query ); @@ -301,12 +318,26 @@ public function get_es_posts( $posts, $query ) { if ( 'all' === $scope ) { $index = Indexables::factory()->get( 'post' )->get_network_alias(); - } elseif ( is_numeric( $scope ) ) { - $index = Indexables::factory()->get( 'post' )->get_index_name( (int) $scope ); - } elseif ( is_array( $scope ) ) { + } elseif ( ! empty( $site__in ) ) { $index = []; - foreach ( $scope as $site_id ) { + foreach ( $site__in as $site_id ) { + $index[] = Indexables::factory()->get( 'post' )->get_index_name( $site_id ); + } + + $index = implode( ',', $index ); + } elseif ( ! empty( $site__not_in ) ) { + + $sites = get_sites( + array( + 'fields' => 'ids', + 'site__not_in' => $site__not_in, + ) + ); + foreach ( $sites as $site_id ) { + if ( ! Utils\is_site_indexable( $site_id ) ) { + continue; + } $index[] = Indexables::factory()->get( 'post' )->get_index_name( $site_id ); } diff --git a/includes/classes/Indexable/Term/QueryIntegration.php b/includes/classes/Indexable/Term/QueryIntegration.php index b061566666..76a14a9a3b 100644 --- a/includes/classes/Indexable/Term/QueryIntegration.php +++ b/includes/classes/Indexable/Term/QueryIntegration.php @@ -85,8 +85,24 @@ public function maybe_filter_query( $results, WP_Term_Query $query ) { $formatted_args = $indexable->format_args( $query->query_vars ); $scope = 'current'; + + $site__in = []; + $site__not_in = []; + if ( ! empty( $query->query_vars['sites'] ) ) { - $scope = $query->query_vars['sites']; + + _deprecated_argument( __FUNCTION__, '4.4.0', esc_html__( 'sites is deprecated. Use site__in instead.', 'elasticpress' ) ); + $site__in = ! is_array( $query->query_vars['sites'] ) ? array( $query->query_vars['sites'] ) : $query->query_vars['sites']; + $scope = 'all' === $query->query_vars['sites'] ? 'all' : $site__in; + } + + if ( ! empty( $query->query_vars['site__in'] ) ) { + $site__in = ! is_array( $query->query_vars['site__in'] ) ? array( $query->query_vars['site__in'] ) : $query->query_vars['site__in']; + $scope = 'all' === $query->query_vars['site__in'] ? 'all' : $site__in; + } + + if ( ! empty( $query->query_vars['site__not_in'] ) ) { + $site__not_in = ! is_array( $query->query_vars['site__not_in'] ) ? array( $query->query_vars['site__not_in'] ) : $query->query_vars['site__not_in']; } /** @@ -107,15 +123,28 @@ public function maybe_filter_query( $results, WP_Term_Query $query ) { if ( 'all' === $scope ) { $index = $indexable->get_network_alias(); - } elseif ( is_numeric( $scope ) ) { - $index = $indexable->get_index_name( (int) $scope ); - } elseif ( is_array( $scope ) ) { + } elseif ( ! empty( $site__in ) ) { $index = []; - foreach ( $scope as $site_id ) { + foreach ( $site__in as $site_id ) { $index[] = $indexable->get_index_name( $site_id ); } + $index = implode( ',', $index ); + } elseif ( ! empty( $site__not_in ) ) { + + $sites = get_sites( + array( + 'fields' => 'ids', + 'site__not_in' => $site__not_in, + ) + ); + foreach ( $sites as $site_id ) { + if ( ! Utils\is_site_indexable( $site_id ) ) { + continue; + } + $index[] = Indexables::factory()->get( 'term' )->get_index_name( $site_id ); + } $index = implode( ',', $index ); } diff --git a/tests/php/bootstrap.php b/tests/php/bootstrap.php index bf5d31f93d..180c7b057b 100644 --- a/tests/php/bootstrap.php +++ b/tests/php/bootstrap.php @@ -120,5 +120,7 @@ function skip_translations_api() { require_once $_tests_dir . '/includes/bootstrap.php'; +require_once __DIR__ . '/includes/classes/factory/CategoryFactory.php'; +require_once __DIR__ . '/includes/classes/factory/CommentFactory.php'; require_once __DIR__ . '/includes/classes/BaseTestCase.php'; require_once __DIR__ . '/includes/classes/FeatureTest.php'; diff --git a/tests/php/includes/classes/BaseTestCase.php b/tests/php/includes/classes/BaseTestCase.php index 3cf1d38258..dca9a2c3da 100644 --- a/tests/php/includes/classes/BaseTestCase.php +++ b/tests/php/includes/classes/BaseTestCase.php @@ -30,6 +30,28 @@ class BaseTestCase extends WP_UnitTestCase { */ protected $applied_filters = array(); + /** + * Holds the factory object + * + * @var obj + * @since 4.4.0 + */ + protected $ep_factory; + + /** + * Set up the test case. + * + * @var obj + * @since 4.4.0 + */ + public function setup() { + + $this->ep_factory = new \stdClass(); + $this->ep_factory->category = new CategoryFactory(); + $this->ep_factory->comment = new CommentFactory(); + parent::setup(); + } + /** * Helper function to test whether a post sync has happened * diff --git a/tests/php/includes/classes/factory/CategoryFactory.php b/tests/php/includes/classes/factory/CategoryFactory.php new file mode 100644 index 0000000000..f28d59fe20 --- /dev/null +++ b/tests/php/includes/classes/factory/CategoryFactory.php @@ -0,0 +1,34 @@ +get( 'term' )->index( $id, true ); + + return $id; + } + +} diff --git a/tests/php/includes/classes/factory/CommentFactory.php b/tests/php/includes/classes/factory/CommentFactory.php new file mode 100644 index 0000000000..4a1d2c730a --- /dev/null +++ b/tests/php/includes/classes/factory/CommentFactory.php @@ -0,0 +1,32 @@ +addslashes_deep( $args ) ); + + ElasticPress\Indexables::factory()->get( 'comment' )->index( $id ); + + return $id; + } + +} diff --git a/tests/php/indexables/TestCommentMultisite.php b/tests/php/indexables/TestCommentMultisite.php new file mode 100644 index 0000000000..b031547749 --- /dev/null +++ b/tests/php/indexables/TestCommentMultisite.php @@ -0,0 +1,253 @@ +suppress_errors(); + + $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); + grant_super_admin( $admin_id ); + + ElasticPress\Features::factory()->activate_feature( 'comments' ); + ElasticPress\Features::factory()->setup_features(); + + ElasticPress\Elasticsearch::factory()->delete_all_indices(); + + ElasticPress\Indexables::factory()->get( 'comment' )->put_mapping(); + + // Need to call this since it's hooked to init. + ElasticPress\Features::factory()->get_registered_feature( 'comments' )->search_setup(); + + $this->factory->blog->create_many( 2, array( 'user_id' => $admin_id ) ); + + $sites = ElasticPress\Utils\get_sites(); + $indexes = array(); + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + ElasticPress\Indexables::factory()->get( 'comment' )->put_mapping(); + $indexes[] = ElasticPress\Indexables::factory()->get( 'comment' )->get_index_name(); + + restore_current_blog(); + } + + ElasticPress\Indexables::factory()->get( 'comment' )->delete_network_alias(); + ElasticPress\Indexables::factory()->get( 'comment' )->create_network_alias( $indexes ); + + wp_set_current_user( $admin_id ); + + } + + /** + * Clean up after each test. Reset our mocks + * + * @since 4.4.0 + */ + public function tearDown() { + if ( ! is_multisite() ) { + return; + } + + parent::tearDown(); + ElasticPress\Indexables::factory()->get( 'comment' )->delete_network_alias(); + } + + /** + * Test Comment Query return comments from all sites. + * + * @since 4.4.0 + */ + public function testCommentQueryForAllSites() { + + $sites = ElasticPress\Utils\get_sites(); + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + $post_id = Functions\create_and_sync_post(); + + $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); + $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); + $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + restore_current_blog(); + } + + $query = new \WP_Comment_Query( array( + 'ep_integrate' => true, + 'site__in' => 'all' + ) ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 9, count( $query->get_comments() ) ); + } + + + /** + * Test Comment Query returns comments from sites subset. + * + * @since 4.4.0 + */ + public function testCommentQueryForSitesSubset() { + + $sites = ElasticPress\Utils\get_sites(); + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + $post_id = Functions\create_and_sync_post(); + + $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); + $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); + $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + restore_current_blog(); + } + + $query = new \WP_Comment_Query( array( + 'ep_integrate' => true, + 'site__in' => [ $sites[0]['blog_id'], $sites[2]['blog_id'] ] + ) ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 6, count( $query->get_comments() ) ); + } + + /** + * Test Comment Query return comments from all sites except one. + * + * @since 4.4.0 + */ + public function testCommentQueryForSitesExceptOne() { + + $sites = ElasticPress\Utils\get_sites(); + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + $post_id = Functions\create_and_sync_post(); + + $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); + $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); + $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + restore_current_blog(); + } + + $query = new \WP_Comment_Query( array( + 'ep_integrate' => true, + 'site__not_in' => [ $sites[2]['blog_id'] ] + ) ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 6, count( $query->get_comments() ) ); + } + + /** + * Test Comment Query search returns result from all sites. + * + * @since 4.4.0 + */ + public function testCommentQuerySearchForAllSites() { + + $sites = ElasticPress\Utils\get_sites(); + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + $post_id = Functions\create_and_sync_post(); + + $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); + $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); + $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id, 'comment_content' => 'Hello World' ) ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + restore_current_blog(); + } + + $query = new \WP_Comment_Query( array( + 'search' => 'Hello World', + 'site__in' => 'all' + ) ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 3, count( $query->get_comments() ) ); + } + + /** + * Test Comment Query with the deprecated `sites` param + * + * @since 4.4.0 + * @expectedDeprecated maybe_filter_query + */ + public function testCommentQueryWithDeprecatedSitesParam() { + + $sites = ElasticPress\Utils\get_sites(); + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + $post_id = Functions\create_and_sync_post(); + + $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); + $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); + $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + restore_current_blog(); + } + + $query = new \WP_Comment_Query( array( + 'ep_integrate' => true, + 'sites' => 'all' + ) ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 9, count( $query->get_comments() ) ); + } + +} diff --git a/tests/php/indexables/TestPostMultisite.php b/tests/php/indexables/TestPostMultisite.php index 61d32101f1..5c00709c11 100644 --- a/tests/php/indexables/TestPostMultisite.php +++ b/tests/php/indexables/TestPostMultisite.php @@ -103,7 +103,7 @@ public function cleanUpSites( $sites ) { ElasticPress\Elasticsearch::factory()->refresh_indices(); - $sql = "select ID from {$wpdb->posts}"; + $sql = "select ID from {$wpdb->posts}"; $post_ids = $wpdb->get_col( $sql ); // phpcs:ignore foreach ( $post_ids as $post_id ) { @@ -194,8 +194,8 @@ public function testWPQuerySearchContent() { } $args = array( - 's' => 'findme', - 'sites' => 'all', + 's' => 'findme', + 'site__in' => 'all', ); add_action( 'ep_wp_query_search', array( $this, 'action_wp_query_search' ), 10, 0 ); @@ -267,8 +267,8 @@ public function testWPQuerySearchContentSiteSubset() { } $args = array( - 's' => 'findme', - 'sites' => array( $sites[1]['blog_id'], $sites[2]['blog_id'] ), + 's' => 'findme', + 'site__in' => array( $sites[1]['blog_id'], $sites[2]['blog_id'] ), ); $query = new \WP_Query( $args ); @@ -281,7 +281,7 @@ public function testWPQuerySearchContentSiteSubset() { } /** - * Test to ensure that if we pass an invalid blog_id to the 'sites' parameter that it doesn't break the search + * Test to ensure that if we pass an invalid blog_id to the 'site__in' parameter that it doesn't break the search * * @since 0.9.2 * @group testMultipleTests @@ -308,8 +308,8 @@ public function testInvalidSubsites() { // 200 is an invalid blog_id which we're going to pass to test $args = array( - 's' => 'findme', - 'sites' => array( $sites[1]['blog_id'], $sites[2]['blog_id'], 200 ), + 's' => 'findme', + 'site__in' => array( $sites[1]['blog_id'], $sites[2]['blog_id'], 200 ), ); $query = new \WP_Query( $args ); @@ -348,8 +348,8 @@ public function testWPQuerySearchContentSingleSite() { } $args = array( - 's' => 'findme', - 'sites' => $sites[1]['blog_id'], + 's' => 'findme', + 'site__in' => $sites[1]['blog_id'], ); $query = new \WP_Query( $args ); @@ -390,8 +390,8 @@ public function testWPQueryPostDataSetup() { } $args = array( - 's' => 'findme', - 'sites' => 'all', + 's' => 'findme', + 'site__in' => 'all', ); $query = new \WP_Query( $args ); @@ -446,8 +446,8 @@ public function testWPQuerySearchTitle() { } $args = array( - 's' => 'findme', - 'sites' => 'all', + 's' => 'findme', + 'site__in' => 'all', ); add_action( 'ep_wp_query_search', array( $this, 'action_wp_query_search' ), 10, 0 ); @@ -495,8 +495,8 @@ public function testWPQuerySearchExcerpt() { } $args = array( - 's' => 'findme', - 'sites' => 'all', + 's' => 'findme', + 'site__in' => 'all', ); add_action( 'ep_wp_query_search', array( $this, 'action_wp_query_search' ), 10, 0 ); @@ -560,7 +560,7 @@ public function testTaxQuery() { $args = array( 's' => 'findme', - 'sites' => 'all', + 'site__in' => 'all', 'tax_query' => array( array( 'taxonomy' => 'post_tag', @@ -618,7 +618,7 @@ public function testPostTypeSearchQueryPage() { $args = array( 's' => 'findme', - 'sites' => 'all', + 'site__in' => 'all', 'post_type' => 'page', ); @@ -670,7 +670,7 @@ public function testPostTypeSearchQueryPost() { $args = array( 's' => 'findme', - 'sites' => 'all', + 'site__in' => 'all', 'post_type' => 'post', ); @@ -721,8 +721,8 @@ public function testNoPostTypeSearchQuery() { } $args = array( - 's' => 'findme', - 'sites' => 'all', + 's' => 'findme', + 'site__in' => 'all', ); $query = new \WP_Query( $args ); @@ -773,7 +773,7 @@ public function testNoPostTypeNoSearchQuery() { $args = array( 'ep_integrate' => true, - 'sites' => 'all', + 'site__in' => 'all', ); $query = new \WP_Query( $args ); @@ -830,9 +830,9 @@ public function testAuthorIDQuery() { } $args = array( - 's' => 'findme', - 'sites' => 'all', - 'author' => $user_id, + 's' => 'findme', + 'site__in' => 'all', + 'author' => $user_id, ); $query = new \WP_Query( $args ); @@ -894,7 +894,7 @@ public function testAuthorNameQuery() { $args = array( 's' => 'findme', - 'sites' => 'all', + 'site__in' => 'all', 'author_name' => 'john', ); @@ -943,7 +943,7 @@ public function testSearchMetaQuery() { $args = array( 's' => 'findme', - 'sites' => 'all', + 'site__in' => 'all', 'search_fields' => array( 'post_title', 'post_excerpt', @@ -1023,7 +1023,7 @@ public function testFilterMetaQuery() { $args = array( 's' => 'findme', - 'sites' => 'all', + 'site__in' => 'all', 'meta_query' => array( array( 'key' => 'test_key', @@ -1092,7 +1092,7 @@ public function testSearchTaxQuery() { $args = array( 's' => 'one findme two', - 'sites' => 'all', + 'site__in' => 'all', 'search_fields' => array( 'post_title', 'post_excerpt', @@ -1161,7 +1161,7 @@ public function testSearchAuthorQuery() { $args = array( 's' => 'john boy', - 'sites' => 'all', + 'site__in' => 'all', 'search_fields' => array( 'post_title', 'post_excerpt', @@ -1247,7 +1247,7 @@ public function testAdvancedQuery() { $args = array( 's' => 'findme', - 'sites' => 'all', + 'site__in' => 'all', 'post_type' => 'ep_test', 'author' => $user_id, 'search_fields' => array( @@ -1294,7 +1294,7 @@ public function testPagination() { $args = array( 's' => 'findme', - 'sites' => 'all', + 'site__in' => 'all', 'posts_per_page' => 2, ); @@ -1311,7 +1311,7 @@ public function testPagination() { $args = array( 's' => 'findme', - 'sites' => 'all', + 'site__in' => 'all', 'posts_per_page' => 2, 'paged' => 2, ); @@ -1368,8 +1368,8 @@ public function testQueryRestorationResetPostData() { } $args = array( - 's' => 'notfirstblog', - 'sites' => 'all', + 's' => 'notfirstblog', + 'site__in' => 'all', ); $query = new \WP_Query( $args ); @@ -1433,8 +1433,8 @@ public function testQueryRestorationResetQuery() { } $args = array( - 's' => 'notfirstblog', - 'sites' => 'all', + 's' => 'notfirstblog', + 'site__in' => 'all', ); $query = new \WP_Query( $args ); @@ -1502,8 +1502,8 @@ public function testQueryStack() { } $args = array( - 's' => 'notfirstblog', - 'sites' => (int) $sites[1]['blog_id'], + 's' => 'notfirstblog', + 'site__in' => (int) $sites[1]['blog_id'], ); $query = new \WP_Query( $args ); @@ -1643,7 +1643,7 @@ public function testPostObject() { $args = array( 's' => 'findme', - 'sites' => 'all', + 'site__in' => 'all', 'posts_per_page' => 10, ); @@ -1770,4 +1770,242 @@ public function testArchivedSite() { $this->assertNotEquals( $count_indexes, $post_count_indexes ); } + + /** + * Tests WP Query returns the result of only those sites which are defined in `site__in` when both `site__in` and `site__not_in` are defined + * + * @since 4.4.0 + * @group testMultipleTests + */ + public function testWPQueryWithSiteInAndNotSiteInParam() { + + $sites = ElasticPress\Utils\get_sites(); + + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + Functions\create_and_sync_post( array( 'post_content' => 'findme' ) ); + Functions\create_and_sync_post(); + Functions\create_and_sync_post( array( 'post_content' => 'findme' ) ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + restore_current_blog(); + } + + $args = array( + 's' => 'findme', + 'site__in' => $sites[1]['blog_id'], + 'site__not_in' => $sites[1]['blog_id'], + ); + + $query = new \WP_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( $query->post_count, 2 ); + $this->assertEquals( $query->found_posts, 2 ); + + $this->cleanUpSites( $sites ); + } + + /** + * Test a simple post content search on a subset of network sites with depreciated `sites` parameter + * + * @since 4.4.0 + * @expectedDeprecated get_es_posts + * @group testMultipleTests + */ + public function testWPQuerySearchContentSiteSubsetWithDeprecatedSitesParam() { + + $sites = ElasticPress\Utils\get_sites(); + + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + Functions\create_and_sync_post( array( 'post_content' => 'findme' ) ); + Functions\create_and_sync_post(); + Functions\create_and_sync_post( array( 'post_content' => 'findme' ) ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + restore_current_blog(); + } + + $args = array( + 's' => 'findme', + 'sites' => array( $sites[1]['blog_id'], $sites[2]['blog_id'] ), + ); + + $query = new \WP_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( $query->post_count, 4 ); + $this->assertEquals( $query->found_posts, 4 ); + + $this->cleanUpSites( $sites ); + } + + /** + * Test a simple post content search with depreciated `sites` parameter + * + * @since 4.4.0 + * @expectedDeprecated get_es_posts + * @group testMultipleTests + */ + public function testWPQuerySearchContentWithDeprecatedSitesParam() { + $sites = ElasticPress\Utils\get_sites(); + + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + Functions\create_and_sync_post( array( 'post_content' => 'findme' ) ); + Functions\create_and_sync_post(); + Functions\create_and_sync_post( array( 'post_content' => 'findme' ) ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + restore_current_blog(); + } + + $args = array( + 's' => 'findme', + 'sites' => 'all', + ); + + add_action( 'ep_wp_query_search', array( $this, 'action_wp_query_search' ), 10, 0 ); + + $query = new \WP_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + + $this->assertEquals( $query->post_count, 6 ); + $this->assertEquals( $query->found_posts, 6 ); + + $other_site_post_count = 0; + $original_site_id = get_current_blog_id(); + + while ( $query->have_posts() ) { + $query->the_post(); + + global $post; + + $wp_post = get_post( get_the_ID() ); + + $this->assertEquals( $post->post_title, get_the_title() ); + $this->assertEquals( $post->post_content, get_the_content() ); + $this->assertEquals( $post->post_date, $wp_post->post_date ); + $this->assertEquals( $post->post_modified, $wp_post->post_modified ); + $this->assertEquals( $post->post_date_gmt, $wp_post->post_date_gmt ); + $this->assertEquals( $post->post_modified_gmt, $wp_post->post_modified_gmt ); + $this->assertEquals( $post->post_name, $wp_post->post_name ); + $this->assertEquals( $post->post_parent, $wp_post->post_parent ); + $this->assertEquals( $post->post_excerpt, $wp_post->post_excerpt ); + $this->assertEquals( $post->site_id, get_current_blog_id() ); + + if ( get_current_blog_id() !== $original_site_id ) { + $other_site_post_count++; + } + } + + $this->assertEquals( 4, $other_site_post_count ); + + wp_reset_postdata(); + + $this->cleanUpSites( $sites ); + } + + /** + * Tests WP Query returns the data from all sites except one. + * + * @since 4.4.0 + * @group testMultipleTests + */ + public function testWPQueryForAllSiteExceptOne() { + + $sites = ElasticPress\Utils\get_sites(); + + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + Functions\create_and_sync_post(); + Functions\create_and_sync_post(); + Functions\create_and_sync_post(); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + restore_current_blog(); + } + + $args = array( + 'ep_integrate' => true, + 'site__not_in' => array( $sites[1]['blog_id'] ), + ); + + $query = new \WP_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 6, $query->post_count ); + $this->assertEquals( 6, $query->found_posts ); + } + + /** + * Tests a simple post content search returns data from all the sites except one. + * + * @since 4.4.0 + * group testMultipleTests + */ + public function testWPQuerySearchContentForAllSiteExceptOne() { + + $sites = ElasticPress\Utils\get_sites(); + + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + Functions\create_and_sync_post( array( 'post_content' => 'findme' ) ); + Functions\create_and_sync_post(); + Functions\create_and_sync_post( array( 'post_content' => 'findme' ) ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + restore_current_blog(); + } + + $args = array( + 's' => 'findme', + 'site__not_in' => array( $sites[1]['blog_id'] ), + ); + + $query = new \WP_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 4, $query->post_count ); + $this->assertEquals( 4, $query->found_posts ); + } + + } diff --git a/tests/php/indexables/TestTermMultisite.php b/tests/php/indexables/TestTermMultisite.php new file mode 100644 index 0000000000..503adae075 --- /dev/null +++ b/tests/php/indexables/TestTermMultisite.php @@ -0,0 +1,375 @@ +suppress_errors(); + + $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); + grant_super_admin( $admin_id ); + + ElasticPress\Features::factory()->activate_feature( 'terms' ); + ElasticPress\Features::factory()->setup_features(); + + ElasticPress\Elasticsearch::factory()->delete_all_indices(); + + ElasticPress\Indexables::factory()->get( 'term' )->put_mapping(); + + // Need to call this since it's hooked to init. + ElasticPress\Features::factory()->get_registered_feature( 'terms' )->search_setup(); + + $this->factory->blog->create_many( 2, array( 'user_id' => $admin_id ) ); + + $sites = ElasticPress\Utils\get_sites(); + $indexes = array(); + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + ElasticPress\Indexables::factory()->get( 'term' )->put_mapping(); + $indexes[] = ElasticPress\Indexables::factory()->get( 'term' )->get_index_name(); + + restore_current_blog(); + } + + ElasticPress\Indexables::factory()->get( 'term' )->delete_network_alias(); + ElasticPress\Indexables::factory()->get( 'term' )->create_network_alias( $indexes ); + + wp_set_current_user( $admin_id ); + + } + + /** + * Clean up after each test. Reset our mocks + * + * @since 4.4.0 + */ + public function tearDown() { + if ( ! is_multisite() ) { + return; + } + + parent::tearDown(); + + ElasticPress\Indexables::factory()->get( 'term' )->delete_network_alias(); + } + + /** + * Test WP Term Query returns the data from all the sites. + * + * @since 4.4.0 + */ + public function testTermQueryForAllSites() { + + $sites = ElasticPress\Utils\get_sites(); + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + restore_current_blog(); + } + + $args = array( + 'taxonomy' => 'category', + 'ep_integrate' => true, + 'site__in' => 'all', + 'hide_empty' => false, + ); + $query = new \WP_Term_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 12, count( $query->get_terms() ) ); + } + + /** + * Test WP Term Query returns the data from the selected sites. + * + * @since 4.4.0 + */ + public function testTermQueryForSitesSubset() { + + $sites = ElasticPress\Utils\get_sites(); + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + restore_current_blog(); + } + + $args = array( + 'taxonomy' => 'category', + 'ep_integrate' => true, + 'site__in' => array( $sites[0]['blog_id'], $sites[1]['blog_id'] ), + 'hide_empty' => false, + ); + $query = new \WP_Term_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 8, count( $query->get_terms() ) ); + + } + + /** + * Test WP Term Query returns the search data from all the sites. + * + * @since 4.4.0 + */ + public function testTermQuerySearchForAllSites() { + + $sites = ElasticPress\Utils\get_sites(); + + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + $this->ep_factory->category->create( + array( + 'slug' => 'apple', + 'name' => 'Big Apple ' . $site['blog_id'], + 'description' => 'The apple fruit term', + ) + ); + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + restore_current_blog(); + } + + $args = array( + 'search' => 'apple', + 'site__in' => 'all', + 'taxonomy' => 'category', + 'hide_empty' => false, + ); + $query = new \WP_Term_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 3, count( $query->get_terms() ) ); + } + + /** + * Test WP Term Query returns the search data from the selected sites. + * + * @since 4.4.0 + */ + public function testTermQuerySearchForSitesSubset() { + + $sites = ElasticPress\Utils\get_sites(); + + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + $this->ep_factory->category->create( + array( + 'slug' => 'apple', + 'name' => 'Big Apple ' . $site['blog_id'], + 'description' => 'The apple fruit term', + ) + ); + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + restore_current_blog(); + } + + $args = array( + 'search' => 'apple', + 'site__in' => array( $sites[0]['blog_id'], $sites[1]['blog_id'] ), + 'taxonomy' => 'category', + 'hide_empty' => false, + ); + $query = new \WP_Term_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 2, count( $query->get_terms() ) ); + } + + /** + * Test WP Term Query returns the data from all the sites except one. + * + * @since 4.4.0 + */ + public function testTermQueryForAllSitesExceptOne() { + + $sites = ElasticPress\Utils\get_sites(); + + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + restore_current_blog(); + } + + $args = array( + 'site__not_in' => array( $sites[0]['blog_id'] ), + 'taxonomy' => 'category', + 'hide_empty' => false, + 'ep_integrate' => true, + ); + $query = new \WP_Term_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 8, count( $query->get_terms() ) ); + + } + + /** + * Test WP Term Query returns the search data from all the sites except one. + * + * @since 4.4.0 + */ + public function testTermQuerySearchForAllSitesExceptOne() { + + $sites = ElasticPress\Utils\get_sites(); + + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + $this->ep_factory->category->create( + array( + 'slug' => 'apple', + 'name' => 'Big Apple ' . $site['blog_id'], + 'description' => 'The apple fruit term', + ) + ); + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + restore_current_blog(); + } + + $args = array( + 'search' => 'apple', + 'site__not_in' => array( $sites[1]['blog_id'] ), + 'taxonomy' => 'category', + 'hide_empty' => false, + ); + $query = new \WP_Term_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 2, count( $query->get_terms() ) ); + } + + /** + * Test WP Term Query with the deprecated `sites` param + * + * @since 4.4.0 + * @expectedDeprecated maybe_filter_query + */ + public function testTermQuerySearchWithDeprecatedSitesParam() { + + $sites = ElasticPress\Utils\get_sites(); + + if ( ! is_multisite() ) { + $this->assertEmpty( $sites ); + return; + } + + foreach ( $sites as $site ) { + switch_to_blog( $site['blog_id'] ); + + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + $this->ep_factory->category->create(); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + restore_current_blog(); + } + + $args = array( + 'sites' => 'all', + 'taxonomy' => 'category', + 'hide_empty' => false, + 'ep_integrate' => true, + ); + $query = new \WP_Term_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 12, count( $query->get_terms() ) ); + + // test for only one site. + $args = array( + 'sites' => $sites[0]['blog_id'], + 'taxonomy' => 'category', + 'hide_empty' => false, + 'ep_integrate' => true, + ); + $query = new \WP_Term_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 4, count( $query->get_terms() ) ); + + } +} From 0f184a1bad2f5e8ed1337305f8717b07d47c0e71 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Tue, 20 Sep 2022 14:59:30 +0500 Subject: [PATCH 2/8] Fix linting --- tests/php/indexables/TestCommentMultisite.php | 57 ++++++++++++------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/tests/php/indexables/TestCommentMultisite.php b/tests/php/indexables/TestCommentMultisite.php index b031547749..4e256b493a 100644 --- a/tests/php/indexables/TestCommentMultisite.php +++ b/tests/php/indexables/TestCommentMultisite.php @@ -99,10 +99,12 @@ public function testCommentQueryForAllSites() { restore_current_blog(); } - $query = new \WP_Comment_Query( array( - 'ep_integrate' => true, - 'site__in' => 'all' - ) ); + $query = new \WP_Comment_Query( + array( + 'ep_integrate' => true, + 'site__in' => 'all', + ) + ); $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 9, count( $query->get_comments() ) ); @@ -135,10 +137,12 @@ public function testCommentQueryForSitesSubset() { restore_current_blog(); } - $query = new \WP_Comment_Query( array( - 'ep_integrate' => true, - 'site__in' => [ $sites[0]['blog_id'], $sites[2]['blog_id'] ] - ) ); + $query = new \WP_Comment_Query( + array( + 'ep_integrate' => true, + 'site__in' => array( $sites[0]['blog_id'], $sites[2]['blog_id'] ), + ) + ); $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 6, count( $query->get_comments() ) ); @@ -170,10 +174,12 @@ public function testCommentQueryForSitesExceptOne() { restore_current_blog(); } - $query = new \WP_Comment_Query( array( - 'ep_integrate' => true, - 'site__not_in' => [ $sites[2]['blog_id'] ] - ) ); + $query = new \WP_Comment_Query( + array( + 'ep_integrate' => true, + 'site__not_in' => array( $sites[2]['blog_id'] ), + ) + ); $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 6, count( $query->get_comments() ) ); @@ -199,16 +205,23 @@ public function testCommentQuerySearchForAllSites() { $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id ) ); - $this->ep_factory->comment->create( array( 'comment_post_ID' => $post_id, 'comment_content' => 'Hello World' ) ); + $this->ep_factory->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_content' => 'Hello World', + ) + ); ElasticPress\Elasticsearch::factory()->refresh_indices(); restore_current_blog(); } - $query = new \WP_Comment_Query( array( - 'search' => 'Hello World', - 'site__in' => 'all' - ) ); + $query = new \WP_Comment_Query( + array( + 'search' => 'Hello World', + 'site__in' => 'all', + ) + ); $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 3, count( $query->get_comments() ) ); @@ -241,10 +254,12 @@ public function testCommentQueryWithDeprecatedSitesParam() { restore_current_blog(); } - $query = new \WP_Comment_Query( array( - 'ep_integrate' => true, - 'sites' => 'all' - ) ); + $query = new \WP_Comment_Query( + array( + 'ep_integrate' => true, + 'sites' => 'all', + ) + ); $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 9, count( $query->get_comments() ) ); From d2d260064998aee97afe07672f71e0cd9ce6a3ea Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Tue, 20 Sep 2022 15:18:53 +0500 Subject: [PATCH 3/8] Fix Tests --- includes/classes/Indexable/Comment/QueryIntegration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/classes/Indexable/Comment/QueryIntegration.php b/includes/classes/Indexable/Comment/QueryIntegration.php index 06b95d4d9f..31d82a5ecd 100644 --- a/includes/classes/Indexable/Comment/QueryIntegration.php +++ b/includes/classes/Indexable/Comment/QueryIntegration.php @@ -174,7 +174,7 @@ public function maybe_filter_query( $results, WP_Comment_Query $query ) { if ( ! Utils\is_site_indexable( $site_id ) ) { continue; } - $index[] = Indexables::factory()->get( 'post' )->get_index_name( $site_id ); + $index[] = Indexables::factory()->get( 'comment' )->get_index_name( $site_id ); } $this->index = implode( ',', $index ); From 73f9d708243c450f0de83ab6e45720718f8749bc Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Tue, 20 Sep 2022 18:34:07 +0500 Subject: [PATCH 4/8] Cleanup --- includes/classes/Indexable/Comment/QueryIntegration.php | 4 ++-- includes/classes/Indexable/Post/QueryIntegration.php | 4 ++-- includes/classes/Indexable/Term/QueryIntegration.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/includes/classes/Indexable/Comment/QueryIntegration.php b/includes/classes/Indexable/Comment/QueryIntegration.php index 31d82a5ecd..c58da04dea 100644 --- a/includes/classes/Indexable/Comment/QueryIntegration.php +++ b/includes/classes/Indexable/Comment/QueryIntegration.php @@ -123,12 +123,12 @@ public function maybe_filter_query( $results, WP_Comment_Query $query ) { if ( ! empty( $query->query_vars['sites'] ) ) { _deprecated_argument( __FUNCTION__, '4.4.0', esc_html__( 'sites is deprecated. Use site__in instead.', 'elasticpress' ) ); - $site__in = ! is_array( $query->query_vars['sites'] ) ? array( $query->query_vars['sites'] ) : $query->query_vars['sites']; + $site__in = (array) $query->query_vars['sites']; $scope = 'all' === $query->query_vars['sites'] ? 'all' : $site__in; } if ( ! empty( $query->query_vars['site__in'] ) ) { - $site__in = ! is_array( $query->query_vars['site__in'] ) ? array( $query->query_vars['site__in'] ) : $query->query_vars['site__in']; + $site__in = (array) $query->query_vars['site__in']; $scope = 'all' === $query->query_vars['site__in'] ? 'all' : $site__in; } diff --git a/includes/classes/Indexable/Post/QueryIntegration.php b/includes/classes/Indexable/Post/QueryIntegration.php index 82f366adf7..6806fc8aca 100644 --- a/includes/classes/Indexable/Post/QueryIntegration.php +++ b/includes/classes/Indexable/Post/QueryIntegration.php @@ -282,13 +282,13 @@ public function get_es_posts( $posts, $query ) { if ( ! empty( $query_vars['sites'] ) ) { _deprecated_argument( __FUNCTION__, '4.4.0', esc_html__( 'sites is deprecated. Use site__in instead.', 'elasticpress' ) ); - $site__in = ! is_array( $query_vars['sites'] ) ? array( $query_vars['sites'] ) : $query_vars['sites']; + $site__in = (array) $query_vars['sites']; $scope = 'all' === $query_vars['sites'] ? 'all' : $site__in; } if ( ! empty( $query_vars['site__in'] ) ) { - $site__in = ! is_array( $query_vars['site__in'] ) ? array( $query_vars['site__in'] ) : $query_vars['site__in']; + $site__in = (array) $query_vars['site__in']; $scope = 'all' === $query_vars['site__in'] ? 'all' : $site__in; } diff --git a/includes/classes/Indexable/Term/QueryIntegration.php b/includes/classes/Indexable/Term/QueryIntegration.php index 76a14a9a3b..93589a29a1 100644 --- a/includes/classes/Indexable/Term/QueryIntegration.php +++ b/includes/classes/Indexable/Term/QueryIntegration.php @@ -92,12 +92,12 @@ public function maybe_filter_query( $results, WP_Term_Query $query ) { if ( ! empty( $query->query_vars['sites'] ) ) { _deprecated_argument( __FUNCTION__, '4.4.0', esc_html__( 'sites is deprecated. Use site__in instead.', 'elasticpress' ) ); - $site__in = ! is_array( $query->query_vars['sites'] ) ? array( $query->query_vars['sites'] ) : $query->query_vars['sites']; + $site__in = (array) $query->query_vars['sites']; $scope = 'all' === $query->query_vars['sites'] ? 'all' : $site__in; } if ( ! empty( $query->query_vars['site__in'] ) ) { - $site__in = ! is_array( $query->query_vars['site__in'] ) ? array( $query->query_vars['site__in'] ) : $query->query_vars['site__in']; + $site__in = (array) $query->query_vars['site__in']; $scope = 'all' === $query->query_vars['site__in'] ? 'all' : $site__in; } From e7ab6376e492fc7a12ad01d9982273b47c3ca806 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Fri, 30 Sep 2022 18:46:14 +0500 Subject: [PATCH 5/8] Adjust Feedback --- includes/classes/Indexable/Comment/QueryIntegration.php | 2 +- includes/classes/Indexable/Post/QueryIntegration.php | 2 +- includes/classes/Indexable/Term/QueryIntegration.php | 2 +- tests/php/indexables/TestPostMultisite.php | 6 ++---- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/includes/classes/Indexable/Comment/QueryIntegration.php b/includes/classes/Indexable/Comment/QueryIntegration.php index c58da04dea..93cabe70b4 100644 --- a/includes/classes/Indexable/Comment/QueryIntegration.php +++ b/includes/classes/Indexable/Comment/QueryIntegration.php @@ -133,7 +133,7 @@ public function maybe_filter_query( $results, WP_Comment_Query $query ) { } if ( ! empty( $query->query_vars['site__not_in'] ) ) { - $site__not_in = ! is_array( $query->query_vars['site__not_in'] ) ? array( $query->query_vars['site__not_in'] ) : $query->query_vars['site__not_in']; + $site__not_in = (array) $query->query_vars['site__not_in']; } /** diff --git a/includes/classes/Indexable/Post/QueryIntegration.php b/includes/classes/Indexable/Post/QueryIntegration.php index 6806fc8aca..611ba5341a 100644 --- a/includes/classes/Indexable/Post/QueryIntegration.php +++ b/includes/classes/Indexable/Post/QueryIntegration.php @@ -293,7 +293,7 @@ public function get_es_posts( $posts, $query ) { } if ( ! empty( $query_vars['site__not_in'] ) ) { - $site__not_in = ! is_array( $query_vars['site__not_in'] ) ? array( $query_vars['site__not_in'] ) : $query_vars['site__not_in']; + $site__not_in = (array) $query_vars['site__not_in']; } $formatted_args = Indexables::factory()->get( 'post' )->format_args( $query_vars, $query ); diff --git a/includes/classes/Indexable/Term/QueryIntegration.php b/includes/classes/Indexable/Term/QueryIntegration.php index 93589a29a1..f0b327e924 100644 --- a/includes/classes/Indexable/Term/QueryIntegration.php +++ b/includes/classes/Indexable/Term/QueryIntegration.php @@ -102,7 +102,7 @@ public function maybe_filter_query( $results, WP_Term_Query $query ) { } if ( ! empty( $query->query_vars['site__not_in'] ) ) { - $site__not_in = ! is_array( $query->query_vars['site__not_in'] ) ? array( $query->query_vars['site__not_in'] ) : $query->query_vars['site__not_in']; + $site__not_in = (array) $query->query_vars['site__not_in']; } /** diff --git a/tests/php/indexables/TestPostMultisite.php b/tests/php/indexables/TestPostMultisite.php index 5c00709c11..f8a4dc3b15 100644 --- a/tests/php/indexables/TestPostMultisite.php +++ b/tests/php/indexables/TestPostMultisite.php @@ -1814,7 +1814,7 @@ public function testWPQueryWithSiteInAndNotSiteInParam() { } /** - * Test a simple post content search on a subset of network sites with depreciated `sites` parameter + * Test a simple post content search on a subset of network sites with deprecated `sites` parameter * * @since 4.4.0 * @expectedDeprecated get_es_posts @@ -1856,7 +1856,7 @@ public function testWPQuerySearchContentSiteSubsetWithDeprecatedSitesParam() { } /** - * Test a simple post content search with depreciated `sites` parameter + * Test a simple post content search with deprecated `sites` parameter * * @since 4.4.0 * @expectedDeprecated get_es_posts @@ -1887,8 +1887,6 @@ public function testWPQuerySearchContentWithDeprecatedSitesParam() { 'sites' => 'all', ); - add_action( 'ep_wp_query_search', array( $this, 'action_wp_query_search' ), 10, 0 ); - $query = new \WP_Query( $args ); $this->assertTrue( $query->elasticsearch_success ); From 0301e1d9e0805908d98a4c063644f23be7551d59 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Fri, 30 Sep 2022 20:45:44 +0500 Subject: [PATCH 6/8] Exclude multisite tests --- single-site.xml.dist | 3 +++ 1 file changed, 3 insertions(+) diff --git a/single-site.xml.dist b/single-site.xml.dist index 6354ee9432..9927dd2c15 100644 --- a/single-site.xml.dist +++ b/single-site.xml.dist @@ -12,6 +12,9 @@ ./tests/php/ + ./tests/php/indexables/TestCommentMultisite.php + ./tests/php/indexables/TestPostMultisite.php + ./tests/php/indexables/TestTermMultisite.php From 5dfa701345686b1f483be7dddf3cc8a66c82bff3 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Fri, 30 Sep 2022 21:20:46 +0500 Subject: [PATCH 7/8] Add new workflow --- .github/workflows/test.yml | 58 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6b5a7601ab..b3766a1c23 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,8 +17,61 @@ on: - '[0-9].[0-9x]*' # Version branches: 4.x.x, 4.1.x, 5.x jobs: - phpunit: - name: PHP Unit + phpunit_single_site: + name: PHP Unit Tests (Single Site) + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Start MySQL + run: sudo systemctl start mysql.service + + - name: Configure sysctl limits + run: | + sudo swapoff -a + sudo sysctl -w vm.swappiness=1 + sudo sysctl -w fs.file-max=262144 + sudo sysctl -w vm.max_map_count=262144 + + - name: Setup Elasticsearch + uses: getong/elasticsearch-action@v1.2 + with: + elasticsearch version: '7.5.0' + + - name: Set standard 10up cache directories + run: | + composer config -g cache-dir "${{ env.COMPOSER_CACHE }}" + + - name: Prepare composer cache + uses: actions/cache@v2 + with: + path: ${{ env.COMPOSER_CACHE }} + key: composer-${{ env.COMPOSER_VERSION }}-${{ hashFiles('**/composer.lock') }} + restore-keys: | + composer-${{ env.COMPOSER_VERSION }}- + + - name: Set PHP version + uses: shivammathur/setup-php@v2 + with: + php-version: '7.4' + coverage: none + + - name: Install dependencies + run: composer install + + - name: Setup WP Tests + run: | + bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 + sleep 10 + + - name: PHPUnit + run: | + composer run-script test-single-site + + phpunit_multisite: + name: PHP Unit Tests (Multisite) runs-on: ubuntu-latest steps: @@ -69,4 +122,3 @@ jobs: - name: PHPUnit run: | composer run-script test - composer run-script test-single-site \ No newline at end of file From 4d726f32bc066ef9b08fc9e4dcc50552cef11dac Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Fri, 30 Sep 2022 21:22:11 +0500 Subject: [PATCH 8/8] Minor change --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b3766a1c23..cf2384c9e0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,7 +18,7 @@ on: jobs: phpunit_single_site: - name: PHP Unit Tests (Single Site) + name: PHP Unit (Single Site) runs-on: ubuntu-latest steps: @@ -71,7 +71,7 @@ jobs: composer run-script test-single-site phpunit_multisite: - name: PHP Unit Tests (Multisite) + name: PHP Unit (Multisite) runs-on: ubuntu-latest steps: