Add test for backfill from COT to wp_post and wp_postmeta.
This commit is contained in:
parent
c6d198723f
commit
22f81c9693
|
@ -208,8 +208,8 @@ class OrderHelper {
|
|||
$order->get_data_store()->set_download_permissions_granted( $order, true );
|
||||
$order->get_data_store()->set_recorded_sales( $order, true );
|
||||
$order->set_cart_hash( '1234' );
|
||||
$order->update_meta_data( '_new_order_email_sent', 'true' );
|
||||
$order->update_meta_data( '_order_stock_reduced', 'true' );
|
||||
$order->get_data_store()->set_email_sent( $order, true );
|
||||
$order->get_data_store()->stock_reduced( $order, true );
|
||||
$order->set_date_paid( time() );
|
||||
$order->set_date_completed( time() );
|
||||
$order->calculate_shipping();
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Automattic\WooCommerce\Database\Migrations\CustomOrderTable\PostsToOrdersMigrationController;
|
||||
use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer;
|
||||
use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
|
||||
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper;
|
||||
|
||||
|
@ -72,4 +73,67 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
|
|||
$this->assertEquals( $post_order_data, $cot_order->get_data() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test whether backfill_post_record works as expected.
|
||||
*/
|
||||
public function test_backfill_post_record() {
|
||||
$post_order_id = OrderHelper::create_complex_wp_post_order();
|
||||
$this->migrator->process_migration_for_ids( array( $post_order_id ) );
|
||||
|
||||
$post_data = get_post( $post_order_id, ARRAY_A );
|
||||
$post_meta_data = get_post_meta( $post_order_id );
|
||||
// TODO: Remove `_recorded_sales` from exempted keys after https://github.com/woocommerce/woocommerce/issues/32843.
|
||||
$exempted_keys = array( 'post_modified', 'post_modified_gmt', '_recorded_sales' );
|
||||
$convert_to_float_keys = array( '_cart_discount_tax', '_order_shipping', '_order_shipping_tax', '_order_tax' );
|
||||
$exempted_keys = array_flip( array_merge( $exempted_keys, $convert_to_float_keys ) );
|
||||
|
||||
$post_data_float = array_intersect_key( $post_data, array_flip( $convert_to_float_keys ) );
|
||||
$post_meta_data_float = array_intersect_key( $post_meta_data, array_flip( $convert_to_float_keys ) );
|
||||
$post_data = array_diff_key( $post_data, $exempted_keys );
|
||||
$post_meta_data = array_diff_key( $post_meta_data, $exempted_keys );
|
||||
|
||||
// Let's update post data.
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => $post_order_id,
|
||||
'post_status' => 'migration_pending',
|
||||
'post_type' => DataSynchronizer::PLACEHOLDER_ORDER_POST_TYPE,
|
||||
'ping_status' => 'closed',
|
||||
'post_parent' => 0,
|
||||
'menu_order' => 0,
|
||||
'post_date' => '',
|
||||
'post_date_gmt' => '',
|
||||
)
|
||||
);
|
||||
$this->delete_all_meta_for_post( $post_order_id );
|
||||
|
||||
$this->assertEquals( 'migration_pending', get_post_status( $post_order_id ) ); // assert post was updated.
|
||||
|
||||
$cot_order = new WC_Order();
|
||||
$cot_order->set_id( $post_order_id );
|
||||
$this->sut->read( $cot_order );
|
||||
$this->sut->backfill_post_record( $cot_order );
|
||||
|
||||
$this->assertEquals( $post_data, array_diff_key( get_post( $post_order_id, ARRAY_A ), $exempted_keys ) );
|
||||
$this->assertEquals( $post_meta_data, array_diff_key( get_post_meta( $post_order_id ), $exempted_keys ) );
|
||||
|
||||
foreach ( $post_data_float as $float_key => $value ) {
|
||||
$this->assertEquals( (float) get_post( $post_order_id, ARRAY_A )[ $float_key ], (float) $value, "Value for $float_key does not match." );
|
||||
}
|
||||
|
||||
foreach ( $post_meta_data_float as $float_key => $value ) {
|
||||
$this->assertEquals( (float) get_post_meta( $post_order_id )[ $float_key ], (float) $value, "Value for $float_key does not match." );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to delete all meta for post.
|
||||
*
|
||||
* @param int $post_id Post ID to delete data for.
|
||||
*/
|
||||
private function delete_all_meta_for_post( $post_id ) {
|
||||
global $wpdb;
|
||||
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d", $post_id ) );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue