From 6328ad01a5756b332364e550ce32657231dcf62b Mon Sep 17 00:00:00 2001 From: Michael Pretty Date: Thu, 4 Apr 2024 14:19:22 -0400 Subject: [PATCH] Fix tests currently failing against HPOS environment setup (#46242) * Fix tests currently failing against HPOS environment setup * Add changefile(s) from automation for the following project(s): woocommerce * fix lint warning for print_r usage --------- Co-authored-by: github-actions --- .../woocommerce/changelog/46242-fix-broken-hpos-tests | 4 ++++ .../woocommerce/tests/legacy/unit-tests/crud/meta.php | 10 ++++++---- .../order/class-wc-tests-order-functions.php | 3 +++ .../unit-tests/payment-tokens/payment-tokens.php | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 plugins/woocommerce/changelog/46242-fix-broken-hpos-tests diff --git a/plugins/woocommerce/changelog/46242-fix-broken-hpos-tests b/plugins/woocommerce/changelog/46242-fix-broken-hpos-tests new file mode 100644 index 00000000000..6097a592759 --- /dev/null +++ b/plugins/woocommerce/changelog/46242-fix-broken-hpos-tests @@ -0,0 +1,4 @@ +Significance: patch +Type: dev + +Fix tests that were failing against HPOS environment setup. \ No newline at end of file diff --git a/plugins/woocommerce/tests/legacy/unit-tests/crud/meta.php b/plugins/woocommerce/tests/legacy/unit-tests/crud/meta.php index 00c34fd5eac..579ecfad851 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/crud/meta.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/crud/meta.php @@ -91,6 +91,8 @@ class WC_Tests_CRUD_Meta_Data extends WC_Unit_Test_Case { $order->delete_meta_data( 'random_other' ); $order->delete_meta_data( 'random_other_pre_crud' ); $order->save(); + // Track the number of meta items we originally have for future assertions. + $original_meta_count = count( $order->get_meta_data() ); // Now add one piece of meta $order->add_meta_data( 'random', (string) rand( 0, 0xffffff ) ); @@ -105,13 +107,13 @@ class WC_Tests_CRUD_Meta_Data extends WC_Unit_Test_Case { // Get a new instance of the same order. It should have both 'random' and 'random_other' set on it $new_order = wc_get_order( $this->order_id ); - // The original $order should have 1 item of meta - random. - $this->assertCount( 1, $order->get_meta_data() ); + // The original $order should have $original_meta_count + 1 item of meta - random. + $this->assertCount( $original_meta_count + 1, $order->get_meta_data() ); $this->assertTrue( in_array( 'random', wp_list_pluck( $order->get_meta_data(), 'key' ) ) ); - $expected_count = OrderUtil::custom_orders_table_usage_is_enabled() ? 2 : 3; + $expected_new_meta = OrderUtil::custom_orders_table_usage_is_enabled() ? 2 : 3; // The new $order should have 3 items (or 2 in case of HPOS since direct post updates are not read) of meta since it's freshly loaded. - $this->assertCount( $expected_count, $new_order->get_meta_data() ); + $this->assertCount( $expected_new_meta + $original_meta_count, $new_order->get_meta_data() ); $this->assertTrue( in_array( 'random', wp_list_pluck( $new_order->get_meta_data(), 'key' ) ) ); $this->assertTrue( in_array( 'random_other', wp_list_pluck( $new_order->get_meta_data(), 'key' ) ) ); if ( ! OrderUtil::custom_orders_table_usage_is_enabled() ) { diff --git a/plugins/woocommerce/tests/legacy/unit-tests/order/class-wc-tests-order-functions.php b/plugins/woocommerce/tests/legacy/unit-tests/order/class-wc-tests-order-functions.php index b79ef79f688..17587756056 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/order/class-wc-tests-order-functions.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/order/class-wc-tests-order-functions.php @@ -222,6 +222,9 @@ class WC_Tests_Order_Functions extends WC_Unit_Test_Case { * @since 2.6 */ public function test_wc_order_get_payment_tokens() { + if ( \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) { + $this->markTestSkipped( 'Test only works against Post Meta' ); + } $order = WC_Helper_Order::create_order(); $this->assertEmpty( $order->get_payment_tokens() ); diff --git a/plugins/woocommerce/tests/legacy/unit-tests/payment-tokens/payment-tokens.php b/plugins/woocommerce/tests/legacy/unit-tests/payment-tokens/payment-tokens.php index ab7c75cf4ef..101237fdf7b 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/payment-tokens/payment-tokens.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/payment-tokens/payment-tokens.php @@ -29,7 +29,7 @@ class WC_Tests_Payment_Tokens extends WC_Unit_Test_Case { $this->assertEmpty( WC_Payment_Tokens::get_order_tokens( $order->get_id() ) ); $token = WC_Helper_Payment_Token::create_cc_token(); - update_post_meta( $order->get_id(), '_payment_tokens', array( $token->get_id() ) ); + $order->add_payment_token( $token ); $this->assertCount( 1, WC_Payment_Tokens::get_order_tokens( $order->get_id() ) );