diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 59b7c89..b3bbd1c 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -402,9 +402,9 @@ if (!class_exists('WC_Retailcrm_History')) : // @codeCoverageIgnoreStart // TODO: There is a task to analyze the work set billing address in WC order - $wcOrder->set_billing_state(self::arrayValue($billingAddress, 'region')); + $wcOrder->set_billing_country(self::arrayValue($billingAddress, 'countryIso')); $wcOrder->set_billing_postcode(self::arrayValue($billingAddress, 'index')); - $wcOrder->set_billing_country(self::arrayValue($billingAddress, 'country')); + $wcOrder->set_billing_state(self::arrayValue($billingAddress, 'region')); $wcOrder->set_billing_city(self::arrayValue($billingAddress, 'city')); $wcOrder->set_billing_address_1(self::arrayValue($billingAddress, 'text')); // @codeCoverageIgnoreEnd @@ -491,21 +491,11 @@ if (!class_exists('WC_Retailcrm_History')) : if (isset($order['delivery']['address'])) { $shippingAddress = $order['delivery']['address']; - if (isset($shippingAddress['region'])) { - $wcOrder->set_shipping_state($shippingAddress['region']); - } - - if (isset($shippingAddress['city'])) { - $wcOrder->set_shipping_city($shippingAddress['city']); - } - - if (isset($shippingAddress['street'])) { - $wcOrder->set_shipping_address_1($shippingAddress['street']); - } - - if (isset($shippingAddress['building'])) { - $wcOrder->set_shipping_address_2($shippingAddress['building']); - } + $wcOrder->set_shipping_country(self::arrayValue($shippingAddress, 'countryIso', '')); + $wcOrder->set_shipping_postcode(self::arrayValue($shippingAddress, 'index', '')); + $wcOrder->set_shipping_state(self::arrayValue($shippingAddress, 'region', '')); + $wcOrder->set_shipping_city(self::arrayValue($shippingAddress, 'city', '')); + $wcOrder->set_shipping_address_1(self::arrayValue($shippingAddress, 'text', '')); } } @@ -721,31 +711,31 @@ if (!class_exists('WC_Retailcrm_History')) : $companyName = $customer['mainCompany']['name']; } - $addressShipping = array( - 'first_name' => isset($order['firstName']) ? $order['firstName'] : '', - 'last_name' => isset($order['lastName']) ? $order['lastName'] : '', + $addressShipping = [ + 'first_name' => $order['firstName'] ?? '', + 'last_name' => $order['lastName'] ?? '', 'company' => '', - 'address_1' => isset($order['delivery']['address']['text']) ? $order['delivery']['address']['text'] : '', + 'address_1' => $order['delivery']['address']['text'] ?? '', 'address_2' => '', - 'city' => isset($order['delivery']['address']['city']) ? $order['delivery']['address']['city'] : '', - 'state' => isset($order['delivery']['address']['region']) ? $order['delivery']['address']['region'] : '', - 'postcode' => isset($order['delivery']['address']['index']) ? $order['delivery']['address']['index'] : '', - 'country' => isset($order['delivery']['address']['countryIso']) ? $order['delivery']['address']['countryIso'] : '' - ); + 'city' => $order['delivery']['address']['city'] ?? '', + 'state' => $order['delivery']['address']['region'] ?? '', + 'postcode' => $order['delivery']['address']['index'] ?? '', + 'country' => $order['delivery']['address']['countryIso'] ?? '' + ]; - $addressBilling = array( - 'first_name' => isset($contactOrCustomer['firstName']) ? $contactOrCustomer['firstName'] : '', - 'last_name' => isset($contactOrCustomer['lastName']) ? $contactOrCustomer['lastName'] : '', + $addressBilling = [ + 'first_name' => $contactOrCustomer['firstName'] ?? '', + 'last_name' => $contactOrCustomer['lastName'] ?? '', 'company' => $companyName, - 'email' => isset($contactOrCustomer['email']) ? $contactOrCustomer['email'] : '', - 'phone' => isset($contactOrCustomer['phones'][0]['number']) ? $contactOrCustomer['phones'][0]['number'] : '', - 'address_1' => isset($billingAddress['text']) ? $billingAddress['text'] : '', + 'email' => $contactOrCustomer['email'] ?? '', + 'phone' => $contactOrCustomer['phones'][0]['number'] ?? '', + 'address_1' => $billingAddress['text'] ?? '', 'address_2' => '', - 'city' => isset($billingAddress['city']) ? $billingAddress['city'] : '', - 'state' => isset($billingAddress['region']) ? $billingAddress['region'] : '', - 'postcode' => isset($billingAddress['index']) ? $billingAddress['index'] : '', - 'country' => isset($billingAddress['countryIso']) ? $billingAddress['countryIso'] : '' - ); + 'city' => $billingAddress['city'] ?? '', + 'state' => $billingAddress['region'] ?? '', + 'postcode' => $billingAddress['index'] ?? '', + 'country' => $billingAddress['countryIso'] ?? '' + ]; if (isset($order['payments']) && $order['payments']) { $payment = WC_Payment_Gateways::instance(); diff --git a/tests/datasets/data-history-retailcrm.php b/tests/datasets/data-history-retailcrm.php index dfa9e29..6e2b4c3 100644 --- a/tests/datasets/data-history-retailcrm.php +++ b/tests/datasets/data-history-retailcrm.php @@ -55,7 +55,6 @@ class DataHistoryRetailCrm 'totalSumm' => 0, 'averageSumm' => 0, 'ordersCount' => 0, - 'customFields' => [], 'personalDiscount' => 0, 'cumulativeDiscount' => 0, 'address' => [ @@ -64,11 +63,12 @@ class DataHistoryRetailCrm 'countryIso' => 'ES', 'region' => 'Region', 'city' => 'City', - 'text' => 'street Test 777', + 'text' => 'Street', ], + 'customFields' => ['woo_customer_test' => 'test_customer'], 'segments' => [], 'firstName' => 'Test_Name', - 'lastName' => 'Test', + 'lastName' => 'Test_LastName', 'email' => 'mail_test@mail.es', 'phones' => [ '0' => [ 'number' => '+79184563200' ] ], 'birthday' => '2021-10-01' @@ -116,8 +116,8 @@ class DataHistoryRetailCrm 'prepaySum' => 0, 'purchaseSumm' => 50, 'markDatetime' => '2018-01-01 00:00:00', - 'firstName' => 'Test', - 'lastName' => 'Test', + 'firstName' => 'Test_Name', + 'lastName' => 'Test_LastName', 'phone' => '80000000000', 'call' => false, 'expired' => false, @@ -125,8 +125,8 @@ class DataHistoryRetailCrm 'type' => 'customer', 'segments' => [], 'id' => 1, - 'firstName' => 'Test', - 'lastName' => 'Test', + 'firstName' => 'Test_Name', + 'lastName' => 'Test_LastName', 'email' => 'email@test.ru', 'phones' => [ [ @@ -137,11 +137,11 @@ class DataHistoryRetailCrm ] ], 'address' => [ - 'index' => '111111', - 'countryIso' => 'RU', - 'region' => 'Test region', - 'city' => 'Test', - 'text' => 'Test text address' + 'index' => 123456, + 'countryIso' => 'ES', + 'region' => 'Region', + 'city' => 'City', + 'text' => 'Street' ], 'createdAt' => '2018-01-01 00:00:00', 'managerId' => 1, @@ -158,20 +158,18 @@ class DataHistoryRetailCrm 'averageSumm' => 15387.25, 'ordersCount' => 4, 'costSumm' => 101, - 'customFields' => [ - 'custom' => 'test' - ] + 'customFields' => ['woo_order_test' => 'test_order'], ], 'contragent' => [], 'delivery' => [ 'cost' => 0, 'netCost' => 0, 'address' => [ - 'index' => '111111', - 'countryIso' => 'RU', - 'region' => 'Test region', - 'city' => 'Test', - 'text' => 'Test text address' + 'index' => 123456, + 'countryIso' => 'ES', + 'region' => 'Region', + 'city' => 'City', + 'text' => 'Street' ] ], 'site' => 'test-com', @@ -217,7 +215,6 @@ class DataHistoryRetailCrm 'height' => 0, 'shipmentStore' => 'main', 'shipped' => false, - 'customFields' => [], 'uploadedToExternalStoreSystem' => false ] ] @@ -263,8 +260,8 @@ class DataHistoryRetailCrm 'prepaySum' => 0, 'purchaseSumm' => 50, 'markDatetime' => '2018-01-01 00:00:00', - 'firstName' => 'Test', - 'lastName' => 'Test', + 'firstName' => 'Test_Name', + 'lastName' => 'Test_LastName', 'phone' => '80000000000', 'call' => false, 'expired' => false, @@ -272,8 +269,8 @@ class DataHistoryRetailCrm 'type' => 'customer', 'segments' => [], 'id' => 1, - 'firstName' => 'Test', - 'lastName' => 'Test', + 'firstName' => 'Test_Name', + 'lastName' => 'Test_LastName', 'email' => 'email@test.ru', 'phones' => [ [ @@ -284,11 +281,11 @@ class DataHistoryRetailCrm ] ], 'address' => [ - 'index' => '111111', - 'countryIso' => 'RU', - 'region' => 'Test region', - 'city' => 'Test', - 'text' => 'Test text address' + 'index' => 123456, + 'countryIso' => 'ES', + 'region' => 'Region', + 'city' => 'City', + 'text' => 'Street' ], 'createdAt' => '2018-01-01 00:00:00', 'managerId' => 1, @@ -305,20 +302,18 @@ class DataHistoryRetailCrm 'averageSumm' => 15387.25, 'ordersCount' => 4, 'costSumm' => 101, - 'customFields' => [ - 'custom' => 'test' - ] + 'customFields' => ['woo_order_test' => 'test_order'], ], 'contragent' => [], 'delivery' => [ 'cost' => 0, 'netCost' => 0, 'address' => [ - 'index' => '111111', - 'countryIso' => 'RU', - 'region' => 'Test region', - 'city' => 'Test', - 'text' => 'Test text address' + 'index' => 123456, + 'countryIso' => 'ES', + 'region' => 'Region', + 'city' => 'City', + 'text' => 'Street' ] ], 'site' => 'test-com', @@ -385,7 +380,6 @@ class DataHistoryRetailCrm 'height' => 0, 'shipmentStore' => 'main', 'shipped' => false, - 'customFields' => [], 'uploadedToExternalStoreSystem' => false ] ], @@ -712,7 +706,6 @@ class DataHistoryRetailCrm 'averageSumm' => 9412, 'ordersCount' => 1, 'costSumm' => 0, - 'customFields' => [], 'personalDiscount' => 0, 'cumulativeDiscount' => 0, 'address' => [ @@ -745,7 +738,6 @@ class DataHistoryRetailCrm 'averageSumm' => 9412, 'ordersCount' => 1, 'costSumm' => 0, - 'customFields' => [], 'personalDiscount' => 0, 'cumulativeDiscount' => 0, 'address' => [ diff --git a/tests/helpers/class-wc-retailcrm-test-case-helper.php b/tests/helpers/class-wc-retailcrm-test-case-helper.php index 0ebe160..fdf63bf 100644 --- a/tests/helpers/class-wc-retailcrm-test-case-helper.php +++ b/tests/helpers/class-wc-retailcrm-test-case-helper.php @@ -21,12 +21,12 @@ class WC_Retailcrm_Test_Case_Helper extends WC_Unit_Test_Case 'api_url' => 'https://example.retailcrm.ru', 'api_key' => 'test_key', 'corporate_enabled' => 'yes', - 'online_assistant' => 'code', - 'p_draft' => 'no', + 'online_assistant' => 'code', + 'p_draft' => 'no', 'p_pending' => 'no', 'p_private' => 'no', 'p_publish' => 'no', - 'order_methods' => [ 0 => 'phone' ], + 'order_methods' => [0 => 'phone'], 'flat_rate_shipping' => 'delivery', 'free_shipping' => 'delivery2', 'local_pickup' => 'delivery3', @@ -51,14 +51,16 @@ class WC_Retailcrm_Test_Case_Helper extends WC_Unit_Test_Case 'whatsapp_active' => 'yes', 'whatsapp_location_icon' => 'yes', 'whatsapp_number' => '+79184567234', - 'icml' => 'yes', - 'single_order' => '123', - 'history' => 'yes', + 'icml' => 'yes', + 'single_order' => '123', + 'history' => 'yes', 'deactivate_update_order' => 'no', - 'bind_by_sku' => 'no', + 'bind_by_sku' => 'no', 'update_number' => 'yes', - 'debug_mode' => 'yes', - 'debug-info' => '' + 'debug_mode' => 'yes', + 'debug-info' => '', + 'order-meta-data-retailcrm' => json_encode(['woo_order' => 'woo_order_test']), + 'customer-meta-data-retailcrm' => json_encode(['woo_customer' => 'woo_customer_test']), ]; update_option(WC_Retailcrm_Base::$option_key, $options); diff --git a/tests/test-wc-retailcrm-history.php b/tests/test-wc-retailcrm-history.php index 1d1c2d2..043f041 100644 --- a/tests/test-wc-retailcrm-history.php +++ b/tests/test-wc-retailcrm-history.php @@ -40,7 +40,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $retailcrm_history = new WC_Retailcrm_History($this->apiMock); $retailcrm_history->getHistory(); - $orders = wc_get_orders(array('numberposts' => -1)); + $orders = wc_get_orders(['numberposts' => -1]); $wcOrder = end($orders); if (!$wcOrder) { @@ -58,9 +58,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper if (is_object($order_added_item)) { $this->assertEquals($product->get_id(), $order_added_item->get_product()->get_id()); } - $this->assertNotEmpty($wcOrder->get_date_created()); - $this->assertEquals("2018-01-01 00:00:00", $order['history'][0]['createdAt']); $this->assertNotEmpty($shipping_address['first_name']); $this->assertNotEmpty($shipping_address['last_name']); $this->assertNotEmpty($shipping_address['postcode']); @@ -68,6 +66,15 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $this->assertNotEmpty($shipping_address['country']); $this->assertNotEmpty($shipping_address['state']); + $this->assertEquals('Test_Name', $shipping_address['first_name']); + $this->assertEquals('Test_LastName', $shipping_address['last_name']); + $this->assertEquals('City', $shipping_address['city']); + $this->assertEquals('Region', $shipping_address['state']); + $this->assertEquals('ES', $shipping_address['country']); + $this->assertEquals(123456, $shipping_address['postcode']); + $this->assertEquals('Street', $shipping_address['address_1']); + + if (isset($billing_address['phone'])) { $this->assertNotEmpty($billing_address['phone']); } @@ -83,6 +90,14 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $this->assertNotEmpty($billing_address['country']); $this->assertNotEmpty($billing_address['state']); + $this->assertEquals('Test_Name', $billing_address['first_name']); + $this->assertEquals('Test_LastName', $billing_address['last_name']); + $this->assertEquals('City', $billing_address['city']); + $this->assertEquals('Region', $billing_address['state']); + $this->assertEquals('ES', $billing_address['country']); + $this->assertEquals(123456, $billing_address['postcode']); + $this->assertEquals('Street', $billing_address['address_1']); + if ($wcOrder->get_payment_method()) { $this->assertEquals('payment4', $options[$wcOrder->get_payment_method()]); } @@ -144,7 +159,6 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper } $this->assertNotEmpty($order_added->get_date_created()); - $this->assertEquals("2018-01-01 00:00:00", $order['history'][0]['createdAt']); $this->assertNotEmpty($shipping_address['first_name']); $this->assertNotEmpty($shipping_address['last_name']); $this->assertNotEmpty($shipping_address['postcode']); @@ -223,7 +237,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals('tester001@example.com', $order_updated->get_billing_email()); //Check order note - $notes = wc_get_order_notes(array('limit' => 100, 'order_id' => $order->get_id())); + $notes = wc_get_order_notes(['limit' => 100, 'order_id' => $order->get_id()]); foreach ($notes as $note) { if ($note->content === 'managerComment') { @@ -287,6 +301,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals('Test_Name', $wcCustomer->get_first_name()); $this->assertEquals('City', $wcCustomer->get_billing_city()); $this->assertEquals(123456, $wcCustomer->get_billing_postcode()); + $this->assertEquals('test_customer', $wcCustomer->get_meta('woo_customer')); } public function test_history_switch_customer_tests() @@ -731,7 +746,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper { $mock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') ->disableOriginalConstructor() - ->setMethods(array('isSuccessful')) + ->setMethods(['isSuccessful']) ->getMock(); $mock->expects($this->any()) diff --git a/tests/test-wc-retailcrm-orders.php b/tests/test-wc-retailcrm-orders.php index 298daaf..08c47d1 100644 --- a/tests/test-wc-retailcrm-orders.php +++ b/tests/test-wc-retailcrm-orders.php @@ -110,6 +110,7 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $this->assertArrayHasKey('type', $orderData['payments'][0]); $this->assertArrayHasKey('externalId', $orderData['payments'][0]); $this->assertEquals('payment1', $orderData['payments'][0]['type']); + $this->assertEquals($orderData['customFields']['woo_order_test'], 'test_custom_fields'); } else { $this->assertEquals(null, $order); } @@ -185,6 +186,7 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $this->assertArrayHasKey('name', $orderData['company']); $this->assertEquals(777, $orderData['company']['id']); $this->assertEquals($this->order->get_billing_company(), $orderData['company']['name']); + $this->assertEquals($orderData['customFields']['woo_order_test'], 'test_custom_fields'); } else { $this->assertEquals(null, $order); } @@ -239,7 +241,8 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals('US', $orderData['countryIso']); $this->assertEquals(0, $orderData['discountManualAmount']); $this->assertEquals(0, $orderData['discountManualPercent']); - + $this->assertEquals($orderData['customFields']['woo_order_test'], 'test_custom_fields'); + if (mb_strlen($orderData['delivery']['address']['index']) === 6) { $this->assertEquals('123456', $orderData['delivery']['address']['index']); } else { @@ -502,6 +505,8 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper } $this->order->save(); + + update_post_meta($this->order->get_id(), 'woo_order', 'test_custom_fields'); } private function getResponseData($externalId)