Convert null and '' to 0 before verification. (#33453)

During migration $wpdb->prepare would force null and empty values to be zero for %f placeholder. This was causing verification logic to fail, which is being addressed in this commit.

The alternative was to insert null values without running them via $wpdb->prepare, but that seemed less safer than converting to zero because it would have to done manually since $wpdb->prepare wouldn't support it.
This commit is contained in:
Vedanshu Jain 2022-06-20 13:13:10 +05:30 committed by GitHub
parent 71456382c4
commit 87ccf73da7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 1 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Convert null and '' to 0 before verification to mimic $wpdb->prepare.

View File

@ -563,7 +563,7 @@ WHERE
private function validate_data( $value, string $type ) {
switch ( $type ) {
case 'decimal':
$value = (float) $value;
$value = wc_format_decimal( $value, false, true );
break;
case 'int':
$value = (int) $value;
@ -778,6 +778,9 @@ WHERE $where_clause
*/
private function pre_process_row( $row, $schema, $alias, $destination_alias ) {
if ( in_array( $schema['type'], array( 'int', 'decimal' ), true ) ) {
if ( '' === $row[ $alias ] || null === $row[ $alias ] ) {
$row[ $alias ] = 0; // $wpdb->prepare forces empty values to 0.
}
$row[ $alias ] = wc_format_decimal( $row[ $alias ], false, true );
$row[ $destination_alias ] = wc_format_decimal( $row[ $destination_alias ], false, true );
}

View File

@ -726,4 +726,22 @@ WHERE order_id = {$order_id} AND meta_key = 'non_unique_key_1' AND meta_value in
return $wpdb_mock;
}
/**
* Test that orders are migrated and verified without errors.
*/
public function test_verify_migrated_orders() {
$order = wc_get_order( OrderHelper::create_complex_wp_post_order() );
$this->clear_all_orders();
// Additional test to assert null values are converted properly.
delete_post_meta( $order->get_id(), '_cart_discount_tax' );
$this->assertEquals( '', get_post_meta( $order->get_id(), '_cart_discount_tax', true ) );
$this->sut->migrate_order( $order->get_id() );
$errors = $this->sut->verify_migrated_orders( array( $order->get_id() ) );
$this->assertEmpty( $errors );
}
}