Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for WooCommerce #3259

Merged
merged 21 commits into from
Feb 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add New tests
  • Loading branch information
burhandodhy committed Jan 31, 2023
commit 9f3cd39fef89a551628191783f51beaa5eeaddda
176 changes: 125 additions & 51 deletions tests/php/features/TestWooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function testShopOrderPostTypeQueryOn() {

ElasticPress\Elasticsearch::factory()->refresh_indices();

$args = array(
$args = array(
'post_type' => 'shop_order',
);
$query = new \WP_Query( $args );
Expand Down Expand Up @@ -188,7 +188,7 @@ public function testShopOrderPostTypeQueryWhenProtectedContentEnable() {

ElasticPress\Elasticsearch::factory()->refresh_indices();

$args = array(
$args = array(
'post_type' => 'shop_order',
);
$query = new \WP_Query( $args );
Expand All @@ -211,14 +211,14 @@ public function testShopOrderPostTypeQueryWhenEPIntegrateSetFalse() {
$this->ep_factory->post->create();
$this->ep_factory->post->create(
array(
'post_type' => 'shop_order'
'post_type' => 'shop_order',
)
);

ElasticPress\Elasticsearch::factory()->refresh_indices();

$args = array(
'post_type' => 'shop_order',
$args = array(
'post_type' => 'shop_order',
'ep_integrate' => false,
);
$query = new \WP_Query( $args );
Expand Down Expand Up @@ -633,13 +633,13 @@ public function testProductQueryOrderWhenAverageRatingMetaKeyIsPass() {

$product_1 = $this->ep_factory->product->create(
[
'average_rating' => 2
'average_rating' => 2,
]
);

$product_2 = $this->ep_factory->product->create(
[
'average_rating' => 1
'average_rating' => 1,
]
);

Expand Down Expand Up @@ -824,13 +824,13 @@ public function testProductQueryOrderByPopularityQueryString() {

$product_1 = $this->ep_factory->product->create(
[
'total_sales' => 2
'total_sales' => 2,
]
);

$product_2 = $this->ep_factory->product->create(
[
'total_sales' => 1
'total_sales' => 1,
]
);

Expand All @@ -839,9 +839,12 @@ public function testProductQueryOrderByPopularityQueryString() {
parse_str( 'orderby=popularity', $_GET );

// mock the query as post type archive
add_action( 'parse_query', function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
} );
add_action(
'parse_query',
function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
}
);

$args = array(
'post_type' => 'product',
Expand Down Expand Up @@ -895,13 +898,13 @@ public function testProductQueryOrderByPriceQueryString() {

$product_1 = $this->ep_factory->product->create(
array(
'regular_price' => 2
'regular_price' => 2,
)
);

$product_2 = $this->ep_factory->product->create(
array(
'regular_price' => 1
'regular_price' => 1,
)
);

Expand All @@ -910,9 +913,12 @@ public function testProductQueryOrderByPriceQueryString() {
parse_str( 'orderby=price', $_GET );

// mock the query as post type archive
add_action( 'parse_query', function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
} );
add_action(
'parse_query',
function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
}
);

$args = array(
'post_type' => 'product',
Expand Down Expand Up @@ -966,13 +972,13 @@ public function testProductQueryOrderByPriceDesc() {

$product_1 = $this->ep_factory->product->create(
[
'regular_price' => 2
'regular_price' => 2,
]
);

$product_2 = $this->ep_factory->product->create(
[
'regular_price' => 1
'regular_price' => 1,
]
);

Expand All @@ -981,9 +987,12 @@ public function testProductQueryOrderByPriceDesc() {
parse_str( 'orderby=price-desc', $_GET );

// mock the query as post type archive
add_action( 'parse_query', function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
} );
add_action(
'parse_query',
function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
}
);

$args = array(
'post_type' => 'product',
Expand Down Expand Up @@ -1052,9 +1061,12 @@ public function testProductQueryOrderByRating() {
parse_str( 'orderby=rating', $_GET );

// mock the query as post type archive
add_action( 'parse_query', function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
} );
add_action(
'parse_query',
function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
}
);

$args = array(
'post_type' => 'product',
Expand Down Expand Up @@ -1123,9 +1135,12 @@ public function testProductQueryOrderBySkuQueryString() {
parse_str( 'orderby=sku', $_GET );

// mock the query as post type archive
add_action( 'parse_query', function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
} );
add_action(
'parse_query',
function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
}
);

$args = array(
'post_type' => 'product',
Expand Down Expand Up @@ -1203,9 +1218,12 @@ public function testProductQueryOrderByTitleQueryString() {
$wp_the_query = $query;

// mock the query as post type archive
add_action( 'parse_query', function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
} );
add_action(
'parse_query',
function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
}
);

add_filter(
'ep_post_formatted_args',
Expand Down Expand Up @@ -1253,9 +1271,12 @@ public function testProductQueryOrderByDefault() {
ElasticPress\Elasticsearch::factory()->refresh_indices();

// mock the query as post type archive
add_action( 'parse_query', function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
} );
add_action(
'parse_query',
function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
}
);

parse_str( 'orderby=default', $_GET );

Expand Down Expand Up @@ -1392,18 +1413,23 @@ public function testProductListInAdminUseElasticSearch() {
$typenow = 'product';
$wc_list_table = new \WC_Admin_List_Table_Products();

add_filter( 'ep_post_filters', function( $filters, $args, $query ) {
$expected_result = array(
'terms' => array(
'post_type.raw' => array(
'product',
add_filter(
'ep_post_filters',
function( $filters, $args, $query ) {
$expected_result = array(
'terms' => array(
'post_type.raw' => array(
'product',
),
),
),
);
);

$this->assertEquals( $expected_result, $filters['post_type'] );
return $filters;
}, 10, 3 );
$this->assertEquals( $expected_result, $filters['post_type'] );
return $filters;
},
10,
3
);

parse_str( 'post_type=product&s=product', $_GET );

Expand Down Expand Up @@ -1600,28 +1626,31 @@ public function testAttributesFilterUseES() {

$this->ep_factory->product->create_variation_product(
[
'name' => 'Cap'
'name' => 'Cap',
]
);

$this->ep_factory->product->create(
[
'name' => 'Shoes'
'name' => 'Shoes',
]
);

$this->ep_factory->product->create(
[
'name' => 'T-Shirt'
'name' => 'T-Shirt',
]
);

ElasticPress\Elasticsearch::factory()->refresh_indices();

// mock the query as post type archive
add_action( 'parse_query', function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
} );
add_action(
'parse_query',
function( \WP_Query $query ) : void {
$query->is_post_type_archive = true;
}
);

parse_str( 'filter_colour=blue', $_GET );

Expand All @@ -1638,6 +1667,51 @@ public function testAttributesFilterUseES() {

$this->assertTrue( $wp_the_query->elasticsearch_success );
$this->assertEquals( 1, count( $query ) );
$this->assertEquals( 'Cap', $query[0]->post_title );
$this->assertEquals( 'Cap', $query[0]->post_title );
}

/**
* Tests that get_posts() uses Elasticsearch when ep_integrate is true.
*
* @since 4.5.0
*/
public function testGetPosts() {
ElasticPress\Features::factory()->activate_feature( 'woocommerce' );
ElasticPress\Features::factory()->setup_features();

$this->ep_factory->product->create();

ElasticPress\Elasticsearch::factory()->refresh_indices();

$posts = get_posts(
[
'post_type' => 'product',
'ep_integrate' => true,
]
);

$this->assertTrue( $posts[0]->elasticsearch );
}

/**
* Tests that get_posts() does not use Elasticsearch when ep_integrate is not set.
*
* @since 4.5.0
*/
public function testGetPostQueryDoesNotUseElasticSearchByDefault() {
ElasticPress\Features::factory()->activate_feature( 'woocommerce' );
ElasticPress\Features::factory()->setup_features();

$this->ep_factory->product->create();

ElasticPress\Elasticsearch::factory()->refresh_indices();

$posts = get_posts(
[
'post_type' => 'product',
]
);

$this->assertObjectNotHasAttribute( 'elasticsearch', $posts[0] );
}
}