Support values written in exponential notation for HPOS migrations. (#38561)

This commit is contained in:
Ron Rennick 2023-06-02 13:21:33 -03:00 committed by GitHub
commit a4bb76a573
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 3 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Support values written in exponential notation for HPOS migrations.

View File

@ -571,7 +571,7 @@ WHERE
private function validate_data( $value, string $type ) { private function validate_data( $value, string $type ) {
switch ( $type ) { switch ( $type ) {
case 'decimal': case 'decimal':
$value = wc_format_decimal( $value, false, true ); $value = wc_format_decimal( floatval( $value ), false, true );
break; break;
case 'int': case 'int':
$value = (int) $value; $value = (int) $value;
@ -843,8 +843,8 @@ WHERE $where_clause
if ( '' === $row[ $alias ] || null === $row[ $alias ] ) { if ( '' === $row[ $alias ] || null === $row[ $alias ] ) {
$row[ $alias ] = 0; // $wpdb->prepare forces empty values to 0. $row[ $alias ] = 0; // $wpdb->prepare forces empty values to 0.
} }
$row[ $alias ] = wc_format_decimal( $row[ $alias ], false, true ); $row[ $alias ] = wc_format_decimal( floatval( $row[ $alias ] ), false, true );
$row[ $destination_alias ] = wc_format_decimal( $row[ $destination_alias ], false, true ); $row[ $destination_alias ] = wc_format_decimal( floatval( $row[ $destination_alias ] ), false, true );
} }
if ( 'bool' === $schema['type'] ) { if ( 'bool' === $schema['type'] ) {
$row[ $alias ] = wc_string_to_bool( $row[ $alias ] ); $row[ $alias ] = wc_string_to_bool( $row[ $alias ] );

View File

@ -822,4 +822,31 @@ WHERE order_id = {$order_id} AND meta_key = 'non_unique_key_1' AND meta_value in
// phpcs:ignore -- Hardcoded value. // phpcs:ignore -- Hardcoded value.
$wpdb->query( "SET sql_mode = '$sql_mode' " ); $wpdb->query( "SET sql_mode = '$sql_mode' " );
} }
/**
* @testDox Test that values in exponential notation are migrated properly.
*/
public function test_migration_with_numbers_in_exponential_notation() {
global $wpdb;
$order = OrderHelper::create_order();
update_post_meta( $order->get_id(), '_order_tax', '7.1054273576E-15' ); // 0
update_post_meta( $order->get_id(), '_order_total', '12E-2' ); // 0.12
update_post_meta( $order->get_id(), '_cart_discount_tax', '1237E-2' ); // 12.37
$this->sut->migrate_order( $order->get_id() );
$errors = $this->sut->verify_migrated_orders( array( $order->get_id() ) );
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r -- Intentional for informative debug message.
$this->assertEmpty( $errors, 'Errors found in migrated data: ' . print_r( $errors, true ) );
$order_tax = $wpdb->get_var( $wpdb->prepare( "SELECT tax_amount FROM {$wpdb->prefix}wc_orders WHERE id = %d", $order->get_id() ) );
$this->assertEquals( 0, $order_tax );
$order_total = $wpdb->get_var( $wpdb->prepare( "SELECT total_amount FROM {$wpdb->prefix}wc_orders WHERE id = %d", $order->get_id() ) );
$this->assertEquals( 0.12, $order_total );
$cart_discount_tax = $wpdb->get_var( $wpdb->prepare( "SELECT discount_tax_amount FROM {$wpdb->prefix}wc_order_operational_data WHERE order_id = %d", $order->get_id() ) );
$this->assertEquals( 12.37, $cart_discount_tax );
}
} }