Skip to content
This repository was archived by the owner on Jun 15, 2022. It is now read-only.

Commit 99463f6

Browse files
Merge pull request #104 from liquidweb/fix/billing-email-validation
Don't let existing, invalid emails break migrations
2 parents 4d0c36c + e88effd commit 99463f6

File tree

5 files changed

+53
-7
lines changed

5 files changed

+53
-7
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ matrix:
5151
allow_failures:
5252
- name: Code Coverage
5353
php: 7.2
54-
env: WP_VERSION=latest WP_MULTISITE=0 WC_VERSION=latest RUN_CODE_COVERAGE=1
54+
env: WP_VERSION=latest WC_VERSION=latest RUN_CODE_COVERAGE=1
5555
- name: Bleeding Edge
5656
php: 7.3
5757
env: WP_VERSION=trunk WC_VERSION=latest

includes/class-woocommerce-custom-orders-table.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ public static function populate_order_from_post_meta( $order ) {
160160
case 'shipping_index':
161161
break;
162162

163+
/*
164+
* Migration isn't the time to validate (and potentially throw exceptions);
165+
* if it was accepted into WooCommerce core, let it persist.
166+
*
167+
* If we're unable to set an email address due to $order->set_billing_email(),
168+
* try to circumvent the check by using reflection to call the protected
169+
* $order->set_address_prop() method.
170+
*/
171+
case 'billing_email':
172+
try {
173+
$order->set_billing_email( $meta );
174+
} catch ( WC_Data_Exception $e ) {
175+
$method = new ReflectionMethod( $order, 'set_address_prop' );
176+
$method->setAccessible( true );
177+
$method->invoke( $order, 'email', 'billing', $meta );
178+
}
179+
break;
180+
163181
case 'prices_include_tax':
164182
$order->set_prices_include_tax( 'yes' === $meta );
165183
break;

tests/test-cli.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ public function test_migrate_handles_exceptions() {
204204
$order_ids = $this->generate_orders( 3 );
205205
$this->toggle_use_custom_table( true );
206206

207-
// Break the billing email on the first item.
208-
update_post_meta( $order_ids[0], '_billing_email', 'this is not an email address' );
207+
// Set a duplicate order key for the middle order.
208+
update_post_meta( $order_ids[1], '_order_key', get_post_meta( $order_ids[0], '_order_key' ) );
209209

210210
$this->cli->migrate();
211211

@@ -215,7 +215,7 @@ public function test_migrate_handles_exceptions() {
215215
'Expected to only see two orders in the custom table.'
216216
);
217217

218-
$this->assertContains( $order_ids[0], $this->get_skipped_ids() );
218+
$this->assertContains( $order_ids[1], $this->get_skipped_ids() );
219219
}
220220

221221
public function test_migrate_with_duplicate_ids() {

tests/test-core.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ public function test_order_row_exists() {
1515
$this->assertFalse( wc_custom_order_table()->row_exists( $order->get_id() + 1 ) );
1616
}
1717

18+
/**
19+
* @ticket https://github.com/liquidweb/woocommerce-custom-orders-table/issues/98
20+
*/
21+
public function test_populate_order_from_post_meta_handles_invalid_billing_emails() {
22+
$this->toggle_use_custom_table( false );
23+
$order = WC_Helper_Order::create_order();
24+
update_post_meta( $order->get_id(), '_billing_email', 'this is an invalid email address' );
25+
$this->toggle_use_custom_table( true );
26+
27+
WooCommerce_Custom_Orders_Table::populate_order_from_post_meta( $order );
28+
29+
$this->assertSame(
30+
'this is an invalid email address',
31+
$order->get_billing_email(),
32+
'Don\'t let an invalid email address cause a migration failure.'
33+
);
34+
}
35+
1836
public function test_migrate_to_post_meta() {
1937
$order = WC_Helper_Order::create_order();
2038
$row = $this->get_order_row( $order->get_id() );

tests/test-order-data-store.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,23 @@ public function test_populate_from_meta_handles_wc_data_exceptions() {
287287
$order = WC_Helper_Order::create_order();
288288
$this->toggle_use_custom_table( true );
289289

290-
// Give an invalid billing email.
291-
update_post_meta( $order->get_id(), '_billing_email', 'this is not an email address' );
292-
293290
// Refresh the instance.
294291
$order = wc_get_order( $order->get_id() );
295292

293+
add_action( 'woocommerce_order_object_updated_props', array( $this, 'throw_wc_data_exception' ) );
294+
296295
$this->assertInstanceOf( 'WP_Error', $order->get_data_store()->populate_from_meta( $order ) );
296+
297+
remove_action( 'woocommerce_order_object_updated_props', array( $this, 'throw_wc_data_exception' ) );
298+
}
299+
300+
/**
301+
* Hooked method that simply throws a WC_Data_Exception exception.
302+
*
303+
* @throws WC_Data_Exception
304+
*/
305+
public function throw_wc_data_exception() {
306+
throw new WC_Data_Exception( 'test-data-exception', 'A sample WC_Data_Exception' );
297307
}
298308

299309
/**

0 commit comments

Comments
 (0)