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

WooCommerce code split #3502

Merged
merged 12 commits into from
Jul 19, 2023
Prev Previous commit
Next Next commit
More tests fix
  • Loading branch information
felipeelia committed Jun 21, 2023
commit 57e6958a825f2b6e14b563ccd6b48fad51b6f4aa
3 changes: 3 additions & 0 deletions tests/php/features/WooCommerce/TestWooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ public function testSearchOnAllFrontEnd() {
*
* @since 4.2.0
* @group woocommerce
* @expectedDeprecated ElasticPress\Feature\WooCommerce\WooCommerce::add_variations_skus_meta
*/
public function testAddVariationsSkusMeta() {
ElasticPress\Features::factory()->activate_feature( 'woocommerce' );
Expand Down Expand Up @@ -369,6 +370,7 @@ public function testAddVariationsSkusMeta() {
*
* @since 4.2.0
* @group woocommerce
* @expectedDeprecated ElasticPress\Feature\WooCommerce\WooCommerce::translate_args_admin_products_list
*/
public function testTranslateArgsAdminProductsList() {
ElasticPress\Features::factory()->activate_feature( 'protected_content' );
Expand Down Expand Up @@ -411,6 +413,7 @@ public function testTranslateArgsAdminProductsList() {
*
* @since 4.2.0
* @group woocommerce
* @expectedDeprecated ElasticPress\Feature\WooCommerce\WooCommerce::translate_args_admin_products_list
*/
public function testEPWoocommerceAdminProductsListSearchFields() {
ElasticPress\Features::factory()->activate_feature( 'protected_content' );
Expand Down
89 changes: 89 additions & 0 deletions tests/php/features/WooCommerce/TestWooCommerceOrders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Test woocommerce orders class
*
* @since 4.7.0
* @package elasticpress
*/

namespace ElasticPressTest;

use ElasticPress;

/**
* WC orders test class
*/
class TestWooCommerceOrders extends BaseTestCase {

/**
* Test the translate_args_admin_products_list method
*
* @group woocommerce
*/
public function testTranslateArgsAdminProductsList() {
ElasticPress\Features::factory()->activate_feature( 'protected_content' );
ElasticPress\Features::factory()->activate_feature( 'woocommerce' );
ElasticPress\Features::factory()->setup_features();

parse_str( 'post_type=product&s=product&product_type=downloadable&stock_status=instock', $_GET );

$query_args = [
'ep_integrate' => true,
];

$woocommerce_feature = ElasticPress\Features::factory()->get_registered_feature( 'woocommerce' );
add_action( 'pre_get_posts', [ $woocommerce_feature->orders, 'translate_args_admin_products_list' ] );

$query = new \WP_Query( $query_args );

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( $query->query_vars['s'], 'product' );
$this->assertEquals( $query->query_vars['meta_query'][0]['key'], '_downloadable' );
$this->assertEquals( $query->query_vars['meta_query'][0]['value'], 'yes' );
$this->assertEquals( $query->query_vars['meta_query'][1]['key'], '_stock_status' );
$this->assertEquals( $query->query_vars['meta_query'][1]['value'], 'instock' );
$this->assertEquals(
$query->query_vars['search_fields'],
[
'post_title',
'post_content',
'post_excerpt',
'meta' => [
'_sku',
'_variations_skus',
],
]
);
}

/**
* Test the ep_woocommerce_admin_products_list_search_fields filter
*
* @group woocommerce
*/
public function testEPWoocommerceAdminProductsListSearchFields() {
ElasticPress\Features::factory()->activate_feature( 'protected_content' );
ElasticPress\Features::factory()->activate_feature( 'woocommerce' );
ElasticPress\Features::factory()->setup_features();

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

$query_args = [
'ep_integrate' => true,
];

$woocommerce_feature = ElasticPress\Features::factory()->get_registered_feature( 'woocommerce' );
add_action( 'pre_get_posts', [ $woocommerce_feature->orders, 'translate_args_admin_products_list' ] );

$search_fields_function = function () {
return [ 'post_title', 'post_content' ];
};
add_filter( 'ep_woocommerce_admin_products_list_search_fields', $search_fields_function );

$query = new \WP_Query( $query_args );
$this->assertEquals(
$query->query_vars['search_fields'],
[ 'post_title', 'post_content' ]
);
}
}
53 changes: 53 additions & 0 deletions tests/php/features/WooCommerce/TestWooCommerceProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Test woocommerce products class
*
* @since 4.7.0
* @package elasticpress
*/

namespace ElasticPressTest;

use ElasticPress;

/**
* WC products test class
*/
class TestWooCommerceProduct extends BaseTestCase {
/**
* Test the addition of variations skus to product meta
*
* @group woocommerce
*/
public function testAddVariationsSkusMeta() {
ElasticPress\Features::factory()->activate_feature( 'woocommerce' );
ElasticPress\Features::factory()->setup_features();

$this->assertTrue( class_exists( '\WC_Product_Variable' ) );
$this->assertTrue( class_exists( '\WC_Product_Variation' ) );

$main_product = new \WC_Product_Variable();
$main_product->set_sku( 'main-product_sku' );
$main_product_id = $main_product->save();

$variation_1 = new \WC_Product_Variation();
$variation_1->set_parent_id( $main_product_id );
$variation_1->set_sku( 'child-sku-1' );
$variation_1->save();

$variation_2 = new \WC_Product_Variation();
$variation_2->set_parent_id( $main_product_id );
$variation_2->set_sku( 'child-sku-2' );
$variation_2->save();

$main_product_as_post = get_post( $main_product_id );
$product_meta_to_index = ElasticPress\Features::factory()
->get_registered_feature( 'woocommerce' )
->products
->add_variations_skus_meta( [], $main_product_as_post );

$this->assertArrayHasKey( '_variations_skus', $product_meta_to_index );
$this->assertContains( 'child-sku-1', $product_meta_to_index['_variations_skus'] );
$this->assertContains( 'child-sku-2', $product_meta_to_index['_variations_skus'] );
}
}