From 9aa5293eac2bafc9d9cc2c0e497f8004e10418fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB?= Date: Tue, 9 Jun 2020 11:20:52 +0300 Subject: [PATCH] delete all data before testing customer switching --- .../class-wc-retailcrm-test-case-helper.php | 37 +++++++++++++++++++ tests/test-wc-retailcrm-history.php | 12 +----- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/tests/helpers/class-wc-retailcrm-test-case-helper.php b/tests/helpers/class-wc-retailcrm-test-case-helper.php index 881707f..53a51a1 100644 --- a/tests/helpers/class-wc-retailcrm-test-case-helper.php +++ b/tests/helpers/class-wc-retailcrm-test-case-helper.php @@ -47,6 +47,43 @@ class WC_Retailcrm_Test_Case_Helper extends WC_Unit_Test_Case return $options; } + /** + * Removes all data from the DB. + */ + protected function deleteAllData() + { + if (function_exists('_delete_all_data')) { + _delete_all_data(); + } else { + global $wpdb; + + foreach ( array( + $wpdb->posts, + $wpdb->postmeta, + $wpdb->comments, + $wpdb->commentmeta, + $wpdb->term_relationships, + $wpdb->termmeta, + ) as $table ) { + //phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared + $wpdb->query( "DELETE FROM {$table}" ); + } + + foreach ( array( + $wpdb->terms, + $wpdb->term_taxonomy, + ) as $table ) { + //phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared + $wpdb->query( "DELETE FROM {$table} WHERE term_id != 1" ); + } + + $wpdb->query( "UPDATE {$wpdb->term_taxonomy} SET count = 0" ); + + $wpdb->query( "DELETE FROM {$wpdb->users} WHERE ID != 1" ); + $wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE user_id != 1" ); + } + } + /** * @return array */ diff --git a/tests/test-wc-retailcrm-history.php b/tests/test-wc-retailcrm-history.php index 85effb9..6014d93 100644 --- a/tests/test-wc-retailcrm-history.php +++ b/tests/test-wc-retailcrm-history.php @@ -126,6 +126,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper public function test_history_switch_customer_tests() { + $this->deleteAllData(); $this->regenerateMocks(); $order_id = $this->history_order_create_for_changing_customer(); $this->assertNotEmpty($order_id); @@ -296,16 +297,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals('Еврейская Автономная область', $order->get_billing_state()); $this->assertEquals('Компания1', $order->get_billing_company()); $this->assertEquals('9135487458709', $order->get_billing_phone()); - - $orderUser = $order->get_user(); - - if ($orderUser instanceof WP_User) { - WC_Retailcrm_Logger::debug(__METHOD__, get_user_meta($orderUser->ID)); - $this->assertEquals('psycho913', $orderUser->get('_billing_first_name')); - $this->assertEquals('psycho913', $orderUser->get('_billing_last_name')); - } else { - $this->assertEmpty($order->get_customer_id()); - } + $this->assertEmpty($order->get_customer_id()); } /**