diff --git a/src/include/abstracts/class-wc-retailcrm-abstracts-settings.php b/src/include/abstracts/class-wc-retailcrm-abstracts-settings.php index a221a20..47403c5 100644 --- a/src/include/abstracts/class-wc-retailcrm-abstracts-settings.php +++ b/src/include/abstracts/class-wc-retailcrm-abstracts-settings.php @@ -122,11 +122,6 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration ) ); - $api_version_list = array( - 'v4' => 'v4', - 'v5' => 'v5' - ); - $this->form_fields[] = array( 'title' => __( 'API settings', 'retailcrm' ), 'type' => 'title', @@ -134,16 +129,6 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration 'id' => 'api_options' ); - $this->form_fields['api_version'] = array( - 'title' => __( 'API version', 'retailcrm' ), - 'description' => __( 'Select API version', 'retailcrm' ), - 'css' => 'min-width:50px;', - 'class' => 'select', - 'type' => 'select', - 'options' => $api_version_list, - 'desc_tip' => true, - ); - $this->form_fields['send_payment_amount'] = array( 'title' => __( 'Transferring the payment amount', 'retailcrm' ), 'label' => ' ', @@ -573,6 +558,7 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration } /** +<<<<<<< HEAD * Validate API version * * @param string $key @@ -609,6 +595,8 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration } /** +======= +>>>>>>> Dropped v4, fixes for several bugs, tests. * Validate API url * * @param string $key @@ -622,7 +610,6 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration $api = new WC_Retailcrm_Proxy( $value, $post[$this->plugin_id . $this->id . '_api_key'], - null, $this->get_option('corporate_enabled', 'no') === 'yes' ); @@ -650,7 +637,6 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration $api = new WC_Retailcrm_Proxy( $post[$this->plugin_id . $this->id . '_api_url'], $value, - null, $this->get_option('corporate_enabled', 'no') === 'yes' ); diff --git a/src/include/api/class-wc-retailcrm-proxy.php b/src/include/api/class-wc-retailcrm-proxy.php index 9256055..c80511a 100644 --- a/src/include/api/class-wc-retailcrm-proxy.php +++ b/src/include/api/class-wc-retailcrm-proxy.php @@ -17,29 +17,15 @@ if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) : protected $retailcrm; protected $corporateEnabled; - public function __construct($api_url, $api_key, $api_vers = null, $corporateEnabled = false) + public function __construct($api_url, $api_key, $corporateEnabled = false) { $this->corporateEnabled = $corporateEnabled; - if ( ! class_exists( 'WC_Retailcrm_Client_V4' ) ) { - include_once( __DIR__ . '/class-wc-retailcrm-client-v4.php' ); - } - if ( ! class_exists( 'WC_Retailcrm_Client_V5' ) ) { include_once( __DIR__ . '/class-wc-retailcrm-client-v5.php' ); } - switch ($api_vers) { - case 'v4': - $this->retailcrm = new WC_Retailcrm_Client_V4($api_url, $api_key, $api_vers); - break; - case 'v5': - $this->retailcrm = new WC_Retailcrm_Client_V5($api_url, $api_key, $api_vers); - break; - case null: - $this->retailcrm = new WC_Retailcrm_Client_V4($api_url, $api_key, $api_vers); - break; - } + $this->retailcrm = new WC_Retailcrm_Client_V5($api_url, $api_key, 'v5'); } /** diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index e55bd4f..f593605 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -416,7 +416,6 @@ if (!class_exists('WC_Retailcrm_Base')) { return new WC_Retailcrm_Proxy( $this->get_option('api_url'), $this->get_option('api_key'), - $this->get_option('api_version'), $this->get_option('corporate_enabled', 'no') === 'yes' ); } @@ -433,9 +432,8 @@ if (!class_exists('WC_Retailcrm_Base')) { { $api_client = $this->getApiClient(); $clientId = get_option('retailcrm_client_id'); - $api_version = $this->get_option('api_version'); - WC_Retailcrm_Plugin::integration_module($api_client, $clientId, $api_version, false); + WC_Retailcrm_Plugin::integration_module($api_client, $clientId, false); delete_option('retailcrm_active_in_crm'); } @@ -452,15 +450,14 @@ if (!class_exists('WC_Retailcrm_Base')) { $client_id = uniqid(); } - if ($settings['api_url'] && $settings['api_key'] && $settings['api_version']) { + if ($settings['api_url'] && $settings['api_key']) { $api_client = new WC_Retailcrm_Proxy( $settings['api_url'], $settings['api_key'], - $settings['api_version'], $settings['corporate_enabled'] === 'yes' ); - $result = WC_Retailcrm_Plugin::integration_module($api_client, $client_id, $settings['api_version']); + $result = WC_Retailcrm_Plugin::integration_module($api_client, $client_id); if ($result) { update_option('retailcrm_active_in_crm', true); diff --git a/src/include/class-wc-retailcrm-customers.php b/src/include/class-wc-retailcrm-customers.php index 4caa04c..8f5f2e8 100644 --- a/src/include/class-wc-retailcrm-customers.php +++ b/src/include/class-wc-retailcrm-customers.php @@ -408,13 +408,12 @@ if (!class_exists('WC_Retailcrm_Customers')) : $corpAddress = new WC_Retailcrm_Customer_Corporate_Address(); $address = $orderAddress - ->setFallbackToBilling(true) ->setWCAddressType(WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_BILLING) ->build($order) ->get_data(); $shippingAddress = $corpAddress - ->setWCAddressType(WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_SHIPPING) + ->setWCAddressType(WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_BILLING) ->setFallbackToBilling(true) ->setIsMain(true) ->build($customer, $order) @@ -540,8 +539,6 @@ if (!class_exists('WC_Retailcrm_Customers')) : $dataCorporateCustomers = $search['customersCorporate']; $customer = reset($dataCorporateCustomers); } - } elseif (isset($search['customerCorporate'])) { - $customer = $search['customerCorporate']; } else { $customer = false; } diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 6de59a5..6bbd40a 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -687,29 +687,18 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : 'city' => isset($address['city']) ? $address['city'] : '', 'state' => isset($address['region']) ? $address['region'] : '', 'postcode' => isset($address['index']) ? $address['index'] : '', - 'country' => $address['countryIso'] + 'country' => isset($address['countryIso']) ? $address['countryIso'] : '' ); - if ($this->retailcrm_settings['api_version'] == 'v5') { - if (isset($order['payments']) && $order['payments']) { - $payment = WC_Payment_Gateways::instance(); + if (isset($order['payments']) && $order['payments']) { + $payment = WC_Payment_Gateways::instance(); - if (count($order['payments']) == 1) { - $payment_types = $payment->payment_gateways(); - $payments = $order['payments']; - $paymentType = end($payments); - if (isset($options[$paymentType['type']]) && isset($payment_types[$options[$paymentType['type']]])) { - $wc_order->set_payment_method($payment_types[$options[$paymentType['type']]]); - } - } - } - } else { - if (isset($order['paymentType']) && $order['paymentType']) { - $payment = WC_Payment_Gateways::instance(); + if (count($order['payments']) == 1) { $payment_types = $payment->payment_gateways(); - - if (isset($options[$order['paymentType']]) && isset($payment_types[$options[$order['paymentType']]])) { - $wc_order->set_payment_method($payment_types[$options[$order['paymentType']]]); + $payments = $order['payments']; + $paymentType = end($payments); + if (isset($options[$paymentType['type']]) && isset($payment_types[$options[$paymentType['type']]])) { + $wc_order->set_payment_method($payment_types[$options[$paymentType['type']]]); } } } @@ -977,13 +966,13 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : return; } - $this->prepareChangeToIndividual( - self::arrayValue($crmOrder, 'contact', array()), - $data, - true - ); + if (self::isOrderCorporate($crmOrder)) { + $this->prepareChangeToIndividual( + self::arrayValue($crmOrder, 'contact', array()), + $data, + true + ); - if (isset($order['customer']) && $order['customer']['id'] == $order['contact']['id']) { $data->setNewCustomer(array()); } } @@ -992,24 +981,30 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : $data->setNewCompany($order['company']); } - try { - $result = $switcher->setData($data) - ->build() - ->getResult(); + if ($data->feasible()) { + try { + $result = $switcher->setData($data) + ->build() + ->getResult(); - $result->save(); - } catch (\Exception $exception) { - WC_Retailcrm_Logger::addCaller( - __METHOD__, - sprintf( + $result->save(); + } catch (\Exception $exception) { + $errorMessage = sprintf( 'Error switching order externalId=%s to customer id=%s (new company: id=%s %s). Reason: %s', $order['externalId'], $newCustomerId, isset($order['company']) ? $order['company']['id'] : '', isset($order['company']) ? $order['company']['name'] : '', $exception->getMessage() - ) - ); + ); + WC_Retailcrm_Logger::addCaller(__METHOD__, $errorMessage); + WC_Retailcrm_Logger::debug(__METHOD__, sprintf( + '%s%s%s', + $errorMessage, + PHP_EOL, + $exception->getTraceAsString() + )); + } } } @@ -1142,7 +1137,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : return $def; } - return $arr[$key]; + return isset($arr[$key]) ? $arr[$key] : $def; } } diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index a99110e..67b3c1c 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -369,13 +369,13 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $order = wc_get_order($order_id); $this->processOrder($order, true); - if ($this->retailcrm_settings['api_version'] == 'v4') { - $this->order['paymentType'] = $this->retailcrm_settings[$order->get_payment_method()]; - } - $response = $this->retailcrm->ordersEdit($this->order); +<<<<<<< HEAD if ((!empty($response) && $response->isSuccessful()) && $this->retailcrm_settings['api_version'] == 'v5') { +======= + if ($response->isSuccessful()) { +>>>>>>> Dropped v4, fixes for several bugs, tests. $this->payment = $this->orderUpdatePaymentType($order); } @@ -500,7 +500,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $order_data['items'] = $order_items; - if ($this->retailcrm_settings['api_version'] == 'v5' && !$update) { + if (!$update) { $this->order_payment->is_new = true; $order_data['payments'][] = $this->order_payment->build($order)->get_data(); } diff --git a/src/include/class-wc-retailcrm-plugin.php b/src/include/class-wc-retailcrm-plugin.php index 5bb3232..ca10d9a 100644 --- a/src/include/class-wc-retailcrm-plugin.php +++ b/src/include/class-wc-retailcrm-plugin.php @@ -96,7 +96,7 @@ class WC_Retailcrm_Plugin { * * @return boolean */ - public static function integration_module($api_client, $client_id, $api_version, $active = true) { + public static function integration_module($api_client, $client_id, $active = true) { if (!$api_client) { return false; @@ -109,18 +109,12 @@ class WC_Retailcrm_Plugin { 'active' => $active, ); - if ($api_version == 'v4') { - $configuration['configurationUrl'] = get_site_url(); + $configuration['integrationCode'] = self::INTEGRATION_CODE; + $configuration['baseUrl'] = get_site_url(); + $configuration['clientId'] = $client_id; + $configuration['accountUrl'] = get_site_url(); - $response = $api_client->marketplaceSettingsEdit($configuration); - } else { - $configuration['integrationCode'] = self::INTEGRATION_CODE; - $configuration['baseUrl'] = get_site_url(); - $configuration['clientId'] = $client_id; - $configuration['accountUrl'] = get_site_url(); - - $response = $api_client->integrationModulesEdit($configuration); - } + $response = $api_client->integrationModulesEdit($configuration); if (!$response) { return false; diff --git a/src/include/components/class-wc-retailcrm-customer-switcher.php b/src/include/components/class-wc-retailcrm-customer-switcher.php index ab7692c..d88c76d 100644 --- a/src/include/components/class-wc-retailcrm-customer-switcher.php +++ b/src/include/components/class-wc-retailcrm-customer-switcher.php @@ -51,6 +51,7 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface ) ); $this->processChangeToRegular($this->data->getWcOrder(), $newCustomer); + $this->data->getWcOrder()->set_billing_company(''); } else { if (!empty($newContact)) { WC_Retailcrm_Logger::debug( @@ -138,37 +139,14 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface $wcOrder->{'set_' . $field}($value); } - if (isset($newCustomer['address'])) { - $address = $newCustomer['address']; + $address = self::arrayValue($newCustomer, 'address', array()); + $wcOrder->set_billing_state(self::arrayValue($address, 'region', '--')); + $wcOrder->set_billing_postcode(self::arrayValue($address, 'index', '--')); + $wcOrder->set_billing_country(self::arrayValue($address, 'country', '--')); + $wcOrder->set_billing_city(self::arrayValue($address, 'city', '--')); + $wcOrder->set_billing_address_1(self::arrayValue($address, 'text', '--')); + $wcOrder->set_billing_phone(self::singleCustomerPhone($newCustomer)); - if (isset($address['region'])) { - $wcOrder->set_billing_state($address['region']); - } - - if (isset($address['index'])) { - $wcOrder->set_billing_postcode($address['index']); - } - - if (isset($address['country'])) { - $wcOrder->set_billing_country($address['country']); - } - - if (isset($address['city'])) { - $wcOrder->set_billing_city($address['city']); - } - - if (isset($address['text'])) { - $wcOrder->set_billing_address_1($address['text']); - } - } - - $customerPhone = self::singleCustomerPhone($newCustomer); - - if (!empty($customerPhone)) { - $wcOrder->set_billing_phone($customerPhone); - } - - $wcOrder->set_billing_company(''); $this->result = new WC_Retailcrm_Customer_Switcher_Result($wcCustomer, $wcOrder); } @@ -246,7 +224,7 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface return $def; } - return $arr[$key]; + return isset($arr[$key]) ? $arr[$key] : $def; } /** @@ -259,18 +237,18 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface private static function singleCustomerPhone($customerData) { if (!array_key_exists('phones', $customerData)) { - return null; + return ''; } if (empty($customerData['phones']) || !is_array($customerData['phones'])) { - return null; + return ''; } $phones = $customerData['phones']; $phone = reset($phones); if (!isset($phone['number'])) { - return null; + return ''; } return (string) $phone['number']; diff --git a/src/include/components/class-wc-retailcrm-history-assembler.php b/src/include/components/class-wc-retailcrm-history-assembler.php index f7f47a1..0b5ace7 100644 --- a/src/include/components/class-wc-retailcrm-history-assembler.php +++ b/src/include/components/class-wc-retailcrm-history-assembler.php @@ -271,10 +271,10 @@ class WC_Retailcrm_History_Assembler private static function getMappingValues($groupFilter = array()) { $fields = array(); - $mappingFile = implode( + $mappingFile = realpath(implode( DIRECTORY_SEPARATOR, - array(WP_CONTENT_DIR, 'plugins', 'woo-retailcrm', 'config', 'objects.xml') - ); + array(__DIR__, '..', '..', 'config', 'objects.xml') + )); if (file_exists($mappingFile)) { $objects = simplexml_load_file($mappingFile); diff --git a/src/include/components/class-wc-retailcrm-logger.php b/src/include/components/class-wc-retailcrm-logger.php index c9ae532..cf9e798 100644 --- a/src/include/components/class-wc-retailcrm-logger.php +++ b/src/include/components/class-wc-retailcrm-logger.php @@ -91,14 +91,14 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')): * This log will work only if debug mode is enabled (see retailcrm_is_debug() for details). * Caller should be specified, or message will be ignored at all. * - * @param string $method - * @param array $messages + * @param string $method + * @param array|string $messages */ public static function debug($method, $messages) { if (retailcrm_is_debug()) { if (!empty($method) && !empty($messages)) { - $result = substr( + $result = is_array($messages) ? substr( array_reduce( $messages, function ($carry, $item) { @@ -107,7 +107,7 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')): } ), 1 - ); + ) : $messages; self::getInstance()->add( self::HANDLE . '_debug', diff --git a/src/include/models/class-wc-retailcrm-customer-switcher-result.php b/src/include/models/class-wc-retailcrm-customer-switcher-result.php index d6b193c..301e43d 100644 --- a/src/include/models/class-wc-retailcrm-customer-switcher-result.php +++ b/src/include/models/class-wc-retailcrm-customer-switcher-result.php @@ -64,11 +64,13 @@ class WC_Retailcrm_Customer_Switcher_Result ) ); - if (!empty($this->wcCustomer)) { + if (!empty($this->wcCustomer) && $this->wcCustomer->get_id()) { $this->wcCustomer->save(); } - $this->wcOrder->save(); + if (!empty($this->wcOrder) && $this->wcOrder->get_id()) { + $this->wcOrder->save(); + } return $this; } diff --git a/src/include/models/class-wc-retailcrm-customer-switcher-state.php b/src/include/models/class-wc-retailcrm-customer-switcher-state.php index d75412a..765bf3c 100644 --- a/src/include/models/class-wc-retailcrm-customer-switcher-state.php +++ b/src/include/models/class-wc-retailcrm-customer-switcher-state.php @@ -108,6 +108,17 @@ class WC_Retailcrm_Customer_Switcher_State return $this; } + /** + * Returns true if current state may be processable (e.g. when customer or related data was changed). + * It doesn't guarantee state validity. + * + * @return bool + */ + public function feasible() + { + return !(empty($this->newCustomer) && empty($this->newContact) && empty($this->newCompanyName)); + } + /** * Throws an exception if state is not valid * diff --git a/src/include/order/class-wc-retailcrm-order-item.php b/src/include/order/class-wc-retailcrm-order-item.php index cb70cca..2cbb170 100644 --- a/src/include/order/class-wc-retailcrm-order-item.php +++ b/src/include/order/class-wc-retailcrm-order-item.php @@ -65,12 +65,7 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data $this->set_data_fields($data); $this->set_offer($item); - - if ($this->settings['api_version'] == 'v5') { - $this->set_data_field('discountManualAmount', (float) round($discount_price, 2)); - } elseif ($this->settings['api_version'] == 'v4') { - $this->set_data_field('discount', (float) round($discount_price, 2)); - } + $this->set_data_field('discountManualAmount', (float) round($discount_price, 2)); return $this; } diff --git a/src/include/order/class-wc-retailcrm-order.php b/src/include/order/class-wc-retailcrm-order.php index 760f880..6b85fcc 100644 --- a/src/include/order/class-wc-retailcrm-order.php +++ b/src/include/order/class-wc-retailcrm-order.php @@ -67,10 +67,6 @@ class WC_Retailcrm_Order extends WC_Retailcrm_Abstracts_Data $this->set_data_fields($data); $this->set_number($order); - if ($this->settings['api_version'] != 'v5') { - $this->set_payment_data($order); - } - if (isset($this->settings[$order->get_status()])) { $this->set_data_field('status', $this->settings[$order->get_status()]); } diff --git a/tests/customer/woocommerce/test-wc-retailcrm-customer-switcher-state.php b/tests/customer/woocommerce/test-wc-retailcrm-customer-switcher-state.php new file mode 100644 index 0000000..43a1919 --- /dev/null +++ b/tests/customer/woocommerce/test-wc-retailcrm-customer-switcher-state.php @@ -0,0 +1,82 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +class WC_Retailcrm_Customer_Switcher_State_Test extends WC_Retailcrm_Test_Case_Helper +{ + public function test_feasible() + { + $state = new WC_Retailcrm_Customer_Switcher_State(); + $this->assertFalse($state->feasible()); + + $state->setNewCustomer(array()) + ->setNewContact(array()) + ->setNewCompanyName(''); + $this->assertFalse($state->feasible()); + + $state->setNewCustomer(array('id' => 1)); + $this->assertTrue($state->feasible()); + + $state->setNewCustomer(array()) + ->setNewContact(array('id' => 1)); + $this->assertTrue($state->feasible()); + + $state->setNewCustomer(array()) + ->setNewContact(array()) + ->setNewCompanyName('test'); + $this->assertTrue($state->feasible()); + + $state->setNewCustomer(array()) + ->setNewContact(array()) + ->setNewCompany(array('name' => 'test')); + $this->assertTrue($state->feasible()); + + $state->setNewCustomer(array()) + ->setNewContact(array()) + ->setNewCompanyName(''); + $this->assertFalse($state->feasible()); + } + + public function test_validate_empty() + { + $this->expectException(InvalidArgumentException::class); + $state = new WC_Retailcrm_Customer_Switcher_State(); + $state->validate(); + } + + public function test_validate_order() + { + $this->expectException(InvalidArgumentException::class); + $state = new WC_Retailcrm_Customer_Switcher_State(); + $state->setWcOrder(new WC_Order()) + ->validate(); + } + + public function test_validate_customer_and_contact_set() + { + $this->expectException(InvalidArgumentException::class); + $state = new WC_Retailcrm_Customer_Switcher_State(); + $state->setWcOrder(new WC_Order()) + ->setNewCustomer(array('id' => 1)) + ->setNewContact(array('id' => 1)) + ->validate(); + } + + /** + * @@doesNotPerformAssertions + */ + public function test_validate_ok() + { + $state = new WC_Retailcrm_Customer_Switcher_State(); + $state->setWcOrder(new WC_Order()) + ->setNewCustomer(array('id' => 1)) + ->validate(); + } +} diff --git a/tests/customer/woocommerce/test-wc-retailcrm-wc-customer-builder.php b/tests/customer/woocommerce/test-wc-retailcrm-wc-customer-builder.php new file mode 100644 index 0000000..b2a9d74 --- /dev/null +++ b/tests/customer/woocommerce/test-wc-retailcrm-wc-customer-builder.php @@ -0,0 +1,131 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +class WC_Retailcrm_WC_Customer_Builder_Test extends WC_Retailcrm_Test_Case_Helper +{ + public function test_empty() + { + $this->expectException(RuntimeException::class); + $builder = new WC_Retailcrm_WC_Customer_Builder(); + $builder->build(); + } + + public function test_empty_array() + { + $this->expectException(RuntimeException::class); + $builder = new WC_Retailcrm_WC_Customer_Builder(); + $builder->setData(array())->build(); + } + + public function test_not_array() + { + $this->expectException(RuntimeException::class); + $builder = new WC_Retailcrm_WC_Customer_Builder(); + $builder->setData(new stdClass())->build(); + } + + /** + * @dataProvider customerData + * + * @param array $customerData + */ + public function test_build($customerData) + { + $builder = new WC_Retailcrm_WC_Customer_Builder(); + $wcCustomer = $builder->setData($customerData)->build()->getResult(); + + $this->assertInstanceOf(WC_Customer::class, $wcCustomer); + + if (isset($customerData['firstName'])) { + $this->assertEquals($customerData['firstName'], $wcCustomer->get_first_name()); + } + + if (isset($customerData['lastName'])) { + $this->assertEquals($customerData['lastName'], $wcCustomer->get_last_name()); + } + + if (isset($customerData['email'])) { + $this->assertEquals($customerData['email'], $wcCustomer->get_billing_email()); + } + + if (isset($customerData['phones']) && count($customerData['phones']) > 0) { + $this->assertEquals($customerData['phones'][0]['number'], $wcCustomer->get_billing_phone()); + } + + if (isset($customerData['address']) && !empty($customerData['address'])) { + $address = $customerData['address']; + + if (isset($address['region'])) { + $this->assertEquals($address['region'], $wcCustomer->get_billing_state()); + } + + if (isset($address['index'])) { + $this->assertEquals($address['index'], $wcCustomer->get_billing_postcode()); + } + + if (isset($address['country'])) { + $this->assertEquals($address['country'], $wcCustomer->get_billing_country()); + } + + if (isset($address['city'])) { + $this->assertEquals($address['city'], $wcCustomer->get_billing_city()); + } + } + } + + public function customerData() + { + return array( + array( + 'customer' => array( + 'type' => 'customer', + 'id' => 4228, + 'externalId' => '2', + ) + ), + array( + 'customer' => array( + 'type' => 'customer', + 'id' => 4228, + 'externalId' => '2', + 'isContact' => false, + 'createdAt' => '2020-06-01 15:31:46', + 'managerId' => 27, + 'vip' => false, + 'bad' => false, + 'site' => 'bitrix-test', + 'contragent' => array( + 'contragentType' => 'individual', + ), + 'tags' => array(), + 'marginSumm' => 9412, + 'totalSumm' => 9412, + 'averageSumm' => 9412, + 'ordersCount' => 1, + 'costSumm' => 0, + 'customFields' => array(), + 'personalDiscount' => 0, + 'cumulativeDiscount' => 0, + 'address' => array( + 'id' => 3132, + 'text' => 'ул. Пушкина дом Колотушкина', + ), + 'segments' => array(), + 'firstName' => 'tester001', + 'lastName' => 'tester001', + 'email' => 'tester001@example.com', + 'emailMarketingUnsubscribedAt' => '2020-06-01 15:34:23', + 'phones' => array(array('number' => '2354708915097')) + ) + ) + ); + } +} diff --git a/tests/helpers/class-wc-retailcrm-test-case-helper.php b/tests/helpers/class-wc-retailcrm-test-case-helper.php index 69dd9d8..881707f 100644 --- a/tests/helpers/class-wc-retailcrm-test-case-helper.php +++ b/tests/helpers/class-wc-retailcrm-test-case-helper.php @@ -6,11 +6,9 @@ class WC_Retailcrm_Test_Case_Helper extends WC_Unit_Test_Case { /** - * @param string $apiVersion - * * @return array */ - protected function setOptions($apiVersion = 'v5') + protected function setOptions() { $options = array( 'api_url' => 'https://example.retailcrm.ru', diff --git a/tests/models/test-wc-retailcrm-customer-switcher-result.php b/tests/models/test-wc-retailcrm-customer-switcher-result.php new file mode 100644 index 0000000..eb446b6 --- /dev/null +++ b/tests/models/test-wc-retailcrm-customer-switcher-result.php @@ -0,0 +1,45 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +class WC_Retailcrm_Customer_Switcher_Result_Test extends WC_Retailcrm_Test_Case_Helper +{ + public function test_invalid_both() + { + $this->expectException(InvalidArgumentException::class); + new WC_Retailcrm_Customer_Switcher_Result(new stdClass(), new stdClass()); + } + + public function test_invalid_customer() + { + $this->expectException(InvalidArgumentException::class); + new WC_Retailcrm_Customer_Switcher_Result(new stdClass(), new WC_Order()); + } + + public function test_invalid_order() + { + $this->expectException(InvalidArgumentException::class); + new WC_Retailcrm_Customer_Switcher_Result(new WC_Customer(), new stdClass()); + } + + public function test_valid() + { + $result = new WC_Retailcrm_Customer_Switcher_Result(new WC_Customer(), new WC_Order()); + $this->assertInstanceOf(WC_Customer::class, $result->getWcCustomer()); + $this->assertInstanceOf(WC_Order::class, $result->getWcOrder()); + } + + public function test_valid_no_customer() + { + $result = new WC_Retailcrm_Customer_Switcher_Result(null, new WC_Order()); + $this->assertEmpty($result->getWcCustomer()) +; $this->assertInstanceOf(WC_Order::class, $result->getWcOrder()); + } +} diff --git a/tests/test-wc-retailcrm-base.php b/tests/test-wc-retailcrm-base.php index b28840a..a5efca4 100644 --- a/tests/test-wc-retailcrm-base.php +++ b/tests/test-wc-retailcrm-base.php @@ -45,7 +45,6 @@ class WC_Retailcrm_Base_Test extends WC_Retailcrm_Test_Case_Helper $this->assertInternalType('array', $this->unit->form_fields); $this->assertArrayHasKey('api_url', $this->unit->form_fields); $this->assertArrayHasKey('api_key', $this->unit->form_fields); - $this->assertArrayHasKey('api_version', $this->unit->form_fields); foreach (get_post_statuses() as $key => $status) { $this->assertArrayHasKey('p_' . $key, $this->unit->form_fields); diff --git a/tests/test-wc-retailcrm-history.php b/tests/test-wc-retailcrm-history.php index be57a2a..d46176a 100644 --- a/tests/test-wc-retailcrm-history.php +++ b/tests/test-wc-retailcrm-history.php @@ -2,7 +2,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper { - /** @var WC_Retailcrm_Proxy */ + /** @var \PHPUnit\Framework\MockObject\MockObject|\WC_Retailcrm_Proxy */ protected $apiMock; /** @var WC_Retailcrm_Response_Helper */ @@ -16,62 +16,21 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper public function setUp() { - $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') - ->disableOriginalConstructor() - ->setMethods(array( - 'ordersHistory', - 'customersHistory' - )) - ->getMock(); - - $this->customersHistoryResponse = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); - - $this->ordersHistoryResponse = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); - + $this->regenerateMocks(); parent::setUp(); } public function test_history_order_create() { - $this->setOptions(); - - if (!add_option('retailcrm_orders_history_since_id', 0)) { - update_option('retailcrm_orders_history_since_id', 0); - } - - if (!add_option('retailcrm_customers_history_since_id', 0)) { - update_option('retailcrm_customers_history_since_id', 0); - } - - $this->customersHistoryResponse->expects($this->any()) - ->method('isSuccessful') - ->willReturn(true); - - $this->customersHistoryResponse->setResponse($this->empty_history()); - - $this->ordersHistoryResponse->expects($this->any()) - ->method('isSuccessful') - ->willReturn(true); - $product = WC_Helper_Product::create_simple_product(); - $this->ordersHistoryResponse->setResponse( + $this->mockHistory( + true, + true, + $this->empty_history(), $this->get_history_data_new_order($product->get_id()) ); - $this->apiMock->expects($this->any())->method('customersHistory')->willReturn($this->customersHistoryResponse); - $this->apiMock->expects($this->any())->method('ordersHistory')->willReturn($this->ordersHistoryResponse); - $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); $retailcrm_history->getHistory(); @@ -122,28 +81,16 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper public function test_history_order_add_product() { - $this->setOptions(); - - $this->customersHistoryResponse->expects($this->any()) - ->method('isSuccessful') - ->willReturn(true); - - $this->customersHistoryResponse->setResponse($this->empty_history()); - - $this->ordersHistoryResponse->expects($this->any()) - ->method('isSuccessful') - ->willReturn(true); - $product = WC_Helper_Product::create_simple_product(); $order = WC_Helper_Order::create_order(0); - $this->ordersHistoryResponse->setResponse( + $this->mockHistory( + true, + true, + $this->empty_history(), $this->get_history_data_product_add($product->get_id(), $order->get_id()) ); - $this->apiMock->expects($this->any())->method('customersHistory')->willReturn($this->customersHistoryResponse); - $this->apiMock->expects($this->any())->method('ordersHistory')->willReturn($this->ordersHistoryResponse); - $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); $retailcrm_history->getHistory(); @@ -158,27 +105,15 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper public function test_history_order_update() { - $this->setOptions(); - - $this->customersHistoryResponse->expects($this->any()) - ->method('isSuccessful') - ->willReturn(true); - - $this->customersHistoryResponse->setResponse($this->empty_history()); - - $this->ordersHistoryResponse->expects($this->any()) - ->method('isSuccessful') - ->willReturn(true); - $order = WC_Helper_Order::create_order(0); - $this->ordersHistoryResponse->setResponse( + $this->mockHistory( + true, + true, + $this->empty_history(), $this->get_history_data_update($order->get_id()) ); - $this->apiMock->expects($this->any())->method('customersHistory')->willReturn($this->customersHistoryResponse); - $this->apiMock->expects($this->any())->method('ordersHistory')->willReturn($this->ordersHistoryResponse); - $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); $retailcrm_history->getHistory(); @@ -189,6 +124,525 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals('payment2', $options[$order_updated->get_payment_method()]); } + public function test_history_switch_customer_tests() + { + $this->regenerateMocks(); + $order_id = $this->history_order_create_for_changing_customer(); + $this->assertNotEmpty($order_id); + + $this->regenerateMocks(); + $this->history_order_switch_customer($order_id); + + $this->regenerateMocks(); + $this->history_order_switch_customer_to_corporate($order_id); + + $this->regenerateMocks(); + $this->history_order_switch_customer_to_another_corporate($order_id); + + $this->regenerateMocks(); + $this->history_order_switch_only_company($order_id); + + $this->regenerateMocks(); + $this->history_order_switch_only_contact($order_id); + + $this->regenerateMocks(); + $this->history_order_switch_back_to_individual($order_id); + } + + public function history_order_create_for_changing_customer() + { + $product = WC_Helper_Product::create_simple_product(); + + $this->mockHistory( + true, + true, + $this->empty_history(), + $this->get_history_order_for_client_replace($product->get_id()) + ); + + $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); + $retailcrm_history->getHistory(); + + $orders = wc_get_orders(array('numberposts' => -1)); + $order_added = end($orders); + + if (!$order_added) { + $this->fail('$order_added is null - no orders were added after receiving history'); + } + + $this->assertEquals('tester001@example.com', $order_added->get_billing_email()); + $this->assertNotEmpty($order_added->get_id()); + + return $order_added->get_id(); + } + + /** + * @param int $order_id + * + * @throws \Exception + */ + public function history_order_switch_customer($order_id) + { + $this->mockHistory( + true, + true, + $this->empty_history(), + $this->get_history_change_to_another_individual($order_id) + ); + + $this->ordersGetMock( + true, + $this->get_order_with_customer_and_contact($this->get_new_individual_for_order()) + ); + + $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); + $retailcrm_history->getHistory(); + + try { + $order = new WC_Order($order_id); + } catch (\Exception $exception) { + $post = get_post($order_id); + + if (!$post instanceof WP_Post) { + $this->fail(sprintf('Cannot find order with id=%d', $order_id)); + } + + if (!in_array($post->post_type, wc_get_order_types())) { + $this->fail(sprintf( + 'Invalid order post type `%s`. Should be one of these: %s', + $post->post_type, + implode(', ', wc_get_order_types()) + )); + } else { + $this->fail(sprintf( + 'Cannot determine what\'s wrong with order id=%d. Message from WooCommerce: %s', + $order_id, + $exception->getMessage() + )); + } + + return; + } + + $this->assertEquals('tester002', $order->get_billing_first_name()); + $this->assertEquals('tester002', $order->get_billing_last_name()); + $this->assertEquals('ewtrhibehb126879@example.com', $order->get_billing_email()); + $this->assertEquals('с. Верхненазаровское', $order->get_billing_city()); + $this->assertEquals('34000', $order->get_billing_postcode()); + $this->assertEquals('Адыгея Республика', $order->get_billing_state()); + $this->assertEquals('34687453268933', $order->get_billing_phone()); + $this->assertEmpty($order->get_billing_company()); + $this->assertEmpty($order->get_customer_id()); + } + + /** + * @param int $order_id + * + * @throws \Exception + */ + public function history_order_switch_customer_to_corporate($order_id) + { + $this->mockHistory( + true, + true, + $this->empty_history(), + $this->get_history_change_to_corporate($order_id) + ); + + $this->ordersGetMock( + true, + $this->get_order_with_customer_and_contact( + $this->get_new_corporate_for_order(), + $this->get_new_contact_for_order(), + array('name' => 'Компания1'), + 'legal-entity' + ) + ); + + $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); + $retailcrm_history->getHistory(); + + try { + $order = new WC_Order($order_id); + } catch (\Exception $exception) { + $post = get_post($order_id); + + if (!$post instanceof WP_Post) { + $this->fail(sprintf('Cannot find order with id=%d', $order_id)); + } + + if (!in_array($post->post_type, wc_get_order_types())) { + $this->fail(sprintf( + 'Invalid order post type `%s`. Should be one of these: %s', + $post->post_type, + implode(', ', wc_get_order_types()) + )); + } else { + $this->fail(sprintf( + 'Cannot determine what\'s wrong with order id=%d. Message from WooCommerce: %s', + $order_id, + $exception->getMessage() + )); + } + + return; + } + + $this->assertEquals('psycho913', $order->get_billing_first_name()); + $this->assertEquals('psycho913', $order->get_billing_last_name()); + $this->assertEquals('psycho913@example.com', $order->get_billing_email()); + $this->assertEquals('Валдгейм', $order->get_billing_city()); + $this->assertEquals('344091', $order->get_billing_postcode()); + $this->assertEquals('Еврейская Автономная область', $order->get_billing_state()); + $this->assertEquals('Компания1', $order->get_billing_company()); + $this->assertEquals('9135487458709', $order->get_billing_phone()); + $this->assertEmpty($order->get_customer_id()); + } + + /** + * @param int $order_id + * + * @throws \Exception + */ + public function history_order_switch_customer_to_another_corporate($order_id) + { + $this->mockHistory( + true, + true, + $this->empty_history(), + $this->get_history_change_to_another_corporate($order_id) + ); + + $this->ordersGetMock( + true, + $this->get_order_with_customer_and_contact( + $this->get_another_corporate_for_order(), + $this->get_another_contact_for_order(), + array('name' => 'TestCompany3428769'), + 'legal-entity' + ) + ); + + $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); + $retailcrm_history->getHistory(); + + try { + $order = new WC_Order($order_id); + } catch (\Exception $exception) { + $post = get_post($order_id); + + if (!$post instanceof WP_Post) { + $this->fail(sprintf('Cannot find order with id=%d', $order_id)); + } + + if (!in_array($post->post_type, wc_get_order_types())) { + $this->fail(sprintf( + 'Invalid order post type `%s`. Should be one of these: %s', + $post->post_type, + implode(', ', wc_get_order_types()) + )); + } else { + $this->fail(sprintf( + 'Cannot determine what\'s wrong with order id=%d. Message from WooCommerce: %s', + $order_id, + $exception->getMessage() + )); + } + + return; + } + + $this->assertEquals('Tester4867', $order->get_billing_first_name()); + $this->assertEquals('Tester4867', $order->get_billing_last_name()); + $this->assertEquals('tester4867@example.com', $order->get_billing_email()); + $this->assertEquals('TestCompany3428769', $order->get_billing_company()); + $this->assertEquals('--', $order->get_billing_city()); + $this->assertEquals('--', $order->get_billing_postcode()); + $this->assertEquals('--', $order->get_billing_state()); + $this->assertEquals('--', $order->get_billing_country()); + $this->assertEquals('', $order->get_billing_phone()); + $this->assertEmpty($order->get_customer_id()); + } + + /** + * @param int $order_id + * + * @throws \Exception + */ + public function history_order_switch_only_company($order_id) + { + $this->mockHistory( + true, + true, + $this->empty_history(), + $this->get_history_change_only_company($order_id) + ); + + $this->ordersGetMock( + true, + $this->get_order_with_customer_and_contact( + $this->get_another_corporate_for_order(), + $this->get_another_contact_for_order(), + array('name' => 'TestCompany017089465'), + 'legal-entity' + ) + ); + + $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); + $retailcrm_history->getHistory(); + + try { + $order = new WC_Order($order_id); + } catch (\Exception $exception) { + $post = get_post($order_id); + + if (!$post instanceof WP_Post) { + $this->fail(sprintf('Cannot find order with id=%d', $order_id)); + } + + if (!in_array($post->post_type, wc_get_order_types())) { + $this->fail(sprintf( + 'Invalid order post type `%s`. Should be one of these: %s', + $post->post_type, + implode(', ', wc_get_order_types()) + )); + } else { + $this->fail(sprintf( + 'Cannot determine what\'s wrong with order id=%d. Message from WooCommerce: %s', + $order_id, + $exception->getMessage() + )); + } + + return; + } + + $this->assertEquals('Tester4867', $order->get_billing_first_name()); + $this->assertEquals('Tester4867', $order->get_billing_last_name()); + $this->assertEquals('tester4867@example.com', $order->get_billing_email()); + $this->assertEquals('TestCompany017089465', $order->get_billing_company()); + $this->assertEquals('--', $order->get_billing_city()); + $this->assertEquals('--', $order->get_billing_postcode()); + $this->assertEquals('--', $order->get_billing_state()); + $this->assertEquals('--', $order->get_billing_country()); + $this->assertEquals('', $order->get_billing_phone()); + $this->assertEmpty($order->get_customer_id()); + } + + /** + * @param int $order_id + * + * @throws \Exception + */ + public function history_order_switch_only_contact($order_id) + { + $this->mockHistory( + true, + true, + $this->empty_history(), + $this->get_history_change_only_contact($order_id) + ); + + $this->ordersGetMock( + true, + $this->get_order_with_customer_and_contact( + $this->get_another_corporate_for_order(), + $this->get_contact_when_only_contact_changed(), + array('name' => 'TestCompany017089465'), + 'legal-entity' + ) + ); + + $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); + $retailcrm_history->getHistory(); + + try { + $order = new WC_Order($order_id); + } catch (\Exception $exception) { + $post = get_post($order_id); + + if (!$post instanceof WP_Post) { + $this->fail(sprintf('Cannot find order with id=%d', $order_id)); + } + + if (!in_array($post->post_type, wc_get_order_types())) { + $this->fail(sprintf( + 'Invalid order post type `%s`. Should be one of these: %s', + $post->post_type, + implode(', ', wc_get_order_types()) + )); + } else { + $this->fail(sprintf( + 'Cannot determine what\'s wrong with order id=%d. Message from WooCommerce: %s', + $order_id, + $exception->getMessage() + )); + } + + return; + } + + $this->assertEquals('Tester2890', $order->get_billing_first_name()); + $this->assertEquals('Tester2890', $order->get_billing_last_name()); + $this->assertEquals('tester2890@example.com', $order->get_billing_email()); + $this->assertEquals('TestCompany017089465', $order->get_billing_company()); + $this->assertEquals('--', $order->get_billing_city()); + $this->assertEquals('--', $order->get_billing_postcode()); + $this->assertEquals('--', $order->get_billing_state()); + $this->assertEquals('--', $order->get_billing_country()); + $this->assertEquals('32418790888', $order->get_billing_phone()); + $this->assertEmpty($order->get_customer_id()); + } + + /** + * @param int $order_id + * + * @throws \Exception + */ + public function history_order_switch_back_to_individual($order_id) + { + $this->mockHistory( + true, + true, + $this->empty_history(), + $this->get_history_change_from_corporate_to_individual($order_id) + ); + + $this->ordersGetMock( + true, + $this->get_order_with_customer_and_contact($this->get_initial_regular_customer()) + ); + + $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); + $retailcrm_history->getHistory(); + + try { + $order = new WC_Order($order_id); + } catch (\Exception $exception) { + $post = get_post($order_id); + + if (!$post instanceof WP_Post) { + $this->fail(sprintf('Cannot find order with id=%d', $order_id)); + } + + if (!in_array($post->post_type, wc_get_order_types())) { + $this->fail(sprintf( + 'Invalid order post type `%s`. Should be one of these: %s', + $post->post_type, + implode(', ', wc_get_order_types()) + )); + } else { + $this->fail(sprintf( + 'Cannot determine what\'s wrong with order id=%d. Message from WooCommerce: %s', + $order_id, + $exception->getMessage() + )); + } + + return; + } + + $this->assertEquals('tester001', $order->get_billing_first_name()); + $this->assertEquals('tester001', $order->get_billing_last_name()); + $this->assertEquals('tester001@example.com', $order->get_billing_email()); + $this->assertEquals('--', $order->get_billing_city()); + $this->assertEquals('--', $order->get_billing_postcode()); + $this->assertEquals('--', $order->get_billing_state()); + $this->assertEquals('--', $order->get_billing_country()); + $this->assertEquals('2354708915097', $order->get_billing_phone()); + $this->assertEquals('ул. Пушкина дом Колотушкина', $order->get_billing_address_1()); + $this->assertEmpty($order->get_billing_company()); + $this->assertEmpty($order->get_customer_id()); + } + + /** + * Mock ordersGet response. + * + * @param bool $isSuccessful + * @param array $response + */ + private function ordersGetMock($isSuccessful, $response) + { + $mock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') + ->disableOriginalConstructor() + ->setMethods(array( + 'isSuccessful' + )) + ->getMock(); + + $mock->expects($this->any()) + ->method('isSuccessful') + ->willReturn($isSuccessful); + + $mock->setResponse($response); + $this->apiMock->expects($this->any())->method('ordersGet')->willReturn($mock); + } + + /** + * Mocks customers and orders history responses with provided data + * + * @param bool $isSuccessfulCustomers + * @param bool $isSuccessfulOrders + * @param array $customersHistoryResponse + * @param array $orderHistoryResponse + */ + private function mockHistory( + $isSuccessfulCustomers, + $isSuccessfulOrders, + $customersHistoryResponse, + $orderHistoryResponse + ) { + $this->setOptions(); + + if (!add_option('retailcrm_orders_history_since_id', 0)) { + update_option('retailcrm_orders_history_since_id', 0); + } + + if (!add_option('retailcrm_customers_history_since_id', 0)) { + update_option('retailcrm_customers_history_since_id', 0); + } + + $this->customersHistoryResponse->expects($this->any()) + ->method('isSuccessful') + ->willReturn($isSuccessfulCustomers); + + $this->ordersHistoryResponse->expects($this->any()) + ->method('isSuccessful') + ->willReturn($isSuccessfulOrders); + + $this->customersHistoryResponse->setResponse($customersHistoryResponse); + $this->ordersHistoryResponse->setResponse($orderHistoryResponse); + + $this->apiMock->expects($this->any())->method('customersHistory')->willReturn($this->customersHistoryResponse); + $this->apiMock->expects($this->any())->method('ordersHistory')->willReturn($this->ordersHistoryResponse); + } + + private function regenerateMocks() + { + $this->apiMock = $this->getMockBuilder(WC_Retailcrm_Proxy::class) + ->disableOriginalConstructor() + ->setMethods(array( + 'ordersHistory', + 'customersHistory', + 'ordersGet' + )) + ->getMock(); + + $this->customersHistoryResponse = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') + ->disableOriginalConstructor() + ->setMethods(array( + 'isSuccessful' + )) + ->getMock(); + + $this->ordersHistoryResponse = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') + ->disableOriginalConstructor() + ->setMethods(array( + 'isSuccessful' + )) + ->getMock(); + } + private function empty_history() { return array( @@ -473,4 +927,1512 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper return $history; } + + private function get_history_order_for_client_replace($productId) + { + return array( + 'success' => true, + 'generatedAt' => '2020-06-04 15:05:39', + 'history' => array(array( + 'id' => 25011, + 'createdAt' => '2020-06-04 15:05:10', + 'created' => true, + 'source' => 'user', + 'field' => 'status', + 'oldValue' => NULL, + 'newValue' => array ('code' => 'new'), + 'order' => array ( + 'slug' => 5868, + 'id' => 5868, + 'number' => '5868C', + 'orderType' => 'test', + 'orderMethod' => 'phone', + 'countryIso' => 'RU', + 'createdAt' => '2020-06-04 15:05:10', + 'statusUpdatedAt' => '2020-06-04 15:05:10', + 'summ' => 16, + 'totalSumm' => 16, + 'prepaySum' => 0, + 'purchaseSumm' => 0, + 'markDatetime' => '2020-06-04 15:05:10', + 'lastName' => 'tester001', + 'firstName' => 'tester001', + 'phone' => '2354708915097', + 'email' => 'tester001@example.com', + 'call' => false, + 'expired' => false, + 'managerId' => 27, + 'customer' => array( + 'type' => 'customer', + 'id' => 4228, + 'externalId' => '2', + 'isContact' => false, + 'createdAt' => '2020-06-01 15:31:46', + 'managerId' => 27, + 'vip' => false, + 'bad' => false, + 'site' => 'bitrix-test', + 'contragent' => array( + 'contragentType' => 'individual', + ), + 'tags' => array(), + 'marginSumm' => 9412, + 'totalSumm' => 9412, + 'averageSumm' => 9412, + 'ordersCount' => 1, + 'costSumm' => 0, + 'customFields' => array(), + 'personalDiscount' => 0, + 'cumulativeDiscount' => 0, + 'address' => array( + 'id' => 3132, + 'text' => 'ул. Пушкина дом Колотушкина', + ), + 'segments' => array(), + 'firstName' => 'tester001', + 'lastName' => 'tester001', + 'email' => 'tester001@example.com', + 'emailMarketingUnsubscribedAt' => '2020-06-01 15:34:23', + 'phones' => array(array('number' => '2354708915097')) + ), + 'contact' => array( + 'type' => 'customer', + 'id' => 4228, + 'externalId' => '2', + 'isContact' => false, + 'createdAt' => '2020-06-01 15:31:46', + 'managerId' => 27, + 'vip' => false, + 'bad' => false, + 'site' => 'bitrix-test', + 'contragent' => array( + 'contragentType' => 'individual', + ), + 'tags' => array(), + 'marginSumm' => 9412, + 'totalSumm' => 9412, + 'averageSumm' => 9412, + 'ordersCount' => 1, + 'costSumm' => 0, + 'customFields' => array(), + 'personalDiscount' => 0, + 'cumulativeDiscount' => 0, + 'address' => array( + 'id' => 3132, + 'text' => 'ул. Пушкина дом Колотушкина', + ), + 'segments' => array(), + 'firstName' => 'tester001', + 'lastName' => 'tester001', + 'email' => 'tester001@example.com', + 'emailMarketingUnsubscribedAt' => '2020-06-01 15:34:23', + 'phones' => array(array('number' => '2354708915097')) + ), + 'contragent' => array( + 'contragentType' => 'individual', + ), + 'delivery' => array( + 'cost' => 0, + 'netCost' => 0, + 'address' => array( + 'id' => 5864, + 'countryIso' => 'RU', + 'text' => 'ул. Пушкина дом Колотушкина', + ), + ), + 'site' => 'woocommerce', + 'status' => 'new', + 'items' => array( + array( + 'id' => 160, + 'initialPrice' => 100, + 'createdAt' => '2018-01-01 00:00:00', + 'quantity' => 1, + 'status' => 'new', + 'externalIds' =>array( + array( + 'code' =>'woocomerce', + 'value' =>"160_".$productId + ) + ), + 'offer' => array( + 'id' => 1, + 'externalId' => $productId, + 'xmlId' => '1', + 'name' => 'Test name', + 'vatRate' => 'none' + ), + 'properties' => array(), + 'purchasePrice' => 50 + ) + ), + 'fromApi' => false, + 'length' => 0, + 'width' => 0, + 'height' => 0, + 'shipmentStore' => 'main', + 'shipped' => false, + 'customFields' => array() + ) + )), + 'pagination' => array( + 'limit' => 100, + 'totalCount' => 1, + 'currentPage' => 1, + 'totalPageCount' => 1, + ) + ); + } + + private function get_history_change_to_another_individual($orderExternalId) + { + return array( + 'success' => true, + 'generatedAt' => '2020-06-05 12:29:14', + 'history' => array( + array( + 'id' => 25398, + 'createdAt' => '2020-06-05 12:29:08', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'contact', + 'oldValue' => array( + 'id' => 4228, + 'externalId' => '2', + 'site' => 'bitrix-test', + ), + 'newValue' => array( + 'id' => 4231, + 'site' => 'bitrix-test', + ), + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25399, + 'createdAt' => '2020-06-05 12:29:08', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'customer', + 'oldValue' => array( + 'id' => 4228, + 'externalId' => '2', + 'site' => 'bitrix-test', + ), + 'newValue' => array( + 'id' => 4231, + 'site' => 'bitrix-test', + ), + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25400, + 'createdAt' => '2020-06-05 12:29:08', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'email', + 'oldValue' => 'tester001@example.com', + 'newValue' => 'ewtrhibehb126879@example.com', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25401, + 'createdAt' => '2020-06-05 12:29:08', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'first_name', + 'oldValue' => 'tester001', + 'newValue' => 'tester002', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25402, + 'createdAt' => '2020-06-05 12:29:08', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'last_name', + 'oldValue' => 'tester001', + 'newValue' => 'tester002', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25403, + 'createdAt' => '2020-06-05 12:29:08', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'patronymic', + 'oldValue' => null, + 'newValue' => 'tester002', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25404, + 'createdAt' => '2020-06-05 12:29:08', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'phone', + 'oldValue' => '2354708915097', + 'newValue' => '34687453268933', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + ), + 'pagination' => array( + 'limit' => 100, + 'totalCount' => 7, + 'currentPage' => 1, + 'totalPageCount' => 1, + ), + ); + } + + private function get_history_change_to_corporate($orderExternalId) + { + return array( + 'success' => true, + 'generatedAt' => '2020-06-05 15:24:19', + 'history' => array( + array( + 'id' => 25744, + 'createdAt' => '2020-06-05 15:24:12', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'delivery_address.city', + 'oldValue' => 'с. Верхненазаровское', + 'newValue' => 'Валдгейм', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25745, + 'createdAt' => '2020-06-05 15:24:12', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'delivery_address.index', + 'oldValue' => '34000', + 'newValue' => '344091', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25746, + 'createdAt' => '2020-06-05 15:24:12', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'delivery_address.region', + 'oldValue' => 'Адыгея Республика', + 'newValue' => 'Еврейская Автономная область', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25747, + 'createdAt' => '2020-06-05 15:24:12', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'contragent.contragent_type', + 'oldValue' => 'individual', + 'newValue' => 'legal-entity', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25748, + 'createdAt' => '2020-06-05 15:24:12', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'contragent.legal_address', + 'oldValue' => null, + 'newValue' => '344090 * Москва упцупуцйпуц йцавафыафыафыафы', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25749, + 'createdAt' => '2020-06-05 15:24:12', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'company', + 'oldValue' => null, + 'newValue' => array( + 'id' => 591, + 'name' => 'Компания1', + ), + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25750, + 'createdAt' => '2020-06-05 15:24:12', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'contact', + 'oldValue' => array( + 'id' => 4231, + 'site' => 'bitrix-test', + ), + 'newValue' => array( + 'id' => 4219, + 'externalId' => '4', + 'site' => 'woocommerce', + ), + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25751, + 'createdAt' => '2020-06-05 15:24:12', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'customer', + 'oldValue' => array( + 'id' => 4231, + 'site' => 'bitrix-test', + ), + 'newValue' => array( + 'id' => 4220, + 'site' => 'woocommerce', + ), + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25752, + 'createdAt' => '2020-06-05 15:24:12', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'email', + 'oldValue' => 'ewtrhibehb126879@example.com', + 'newValue' => 'psycho913@example.com', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25753, + 'createdAt' => '2020-06-05 15:24:12', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'first_name', + 'oldValue' => 'tester002', + 'newValue' => 'psycho913', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25754, + 'createdAt' => '2020-06-05 15:24:12', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'last_name', + 'oldValue' => 'tester002', + 'newValue' => 'psycho913', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25755, + 'createdAt' => '2020-06-05 15:24:12', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'patronymic', + 'oldValue' => 'tester002', + 'newValue' => null, + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25756, + 'createdAt' => '2020-06-05 15:24:12', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'phone', + 'oldValue' => '34687453268933', + 'newValue' => '9135487458709', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + ), + 'pagination' => array( + 'limit' => 100, + 'totalCount' => 13, + 'currentPage' => 1, + 'totalPageCount' => 1, + ), + ); + } + + private function get_history_change_to_another_corporate($orderExternalId) + { + return array( + 'success' => true, + 'generatedAt' => '2020-06-05 16:37:53', + 'history' => array( + array( + 'id' => 25979, + 'createdAt' => '2020-06-05 16:37:46', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'contragent.legal_address', + 'oldValue' => '344090 * Москва упцупуцйпуц йцавафыафыафыафы', + 'newValue' => null, + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25980, + 'createdAt' => '2020-06-05 16:37:46', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'company', + 'oldValue' => array( + 'id' => 591, + 'name' => 'Компания1', + ), + 'newValue' => array( + 'id' => 621, + 'name' => 'TestCompany3428769', + ), + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25981, + 'createdAt' => '2020-06-05 16:37:46', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'contact', + 'oldValue' => array( + 'id' => 4219, + 'externalId' => '4', + 'site' => 'woocommerce', + ), + 'newValue' => array( + 'id' => 4304, + 'site' => 'woocommerce', + ), + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25982, + 'createdAt' => '2020-06-05 16:37:46', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'customer', + 'oldValue' => array( + 'id' => 4220, + 'site' => 'woocommerce', + ), + 'newValue' => array( + 'id' => 4303, + 'site' => 'woocommerce', + ), + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25983, + 'createdAt' => '2020-06-05 16:37:46', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'email', + 'oldValue' => 'psycho913@example.com', + 'newValue' => 'tester4867@example.com', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25984, + 'createdAt' => '2020-06-05 16:37:46', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'first_name', + 'oldValue' => 'psycho913', + 'newValue' => 'Tester4867', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25985, + 'createdAt' => '2020-06-05 16:37:46', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'last_name', + 'oldValue' => 'psycho913', + 'newValue' => 'Tester4867', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25986, + 'createdAt' => '2020-06-05 16:37:46', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'patronymic', + 'oldValue' => null, + 'newValue' => 'Tester4867', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25987, + 'createdAt' => '2020-06-05 16:37:46', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'phone', + 'oldValue' => '9135487458709', + 'newValue' => null, + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + ), + 'pagination' => array( + 'limit' => 100, + 'totalCount' => 9, + 'currentPage' => 1, + 'totalPageCount' => 1, + ), + ); + } + + private function get_history_change_only_company($orderExternalId) + { + return array( + 'success' => true, + 'generatedAt' => '2020-06-05 17:13:23', + 'history' => array( + array( + 'id' => 25988, + 'createdAt' => '2020-06-05 17:13:17', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'company', + 'oldValue' => array( + 'id' => 621, + 'name' => 'TestCompany3428769', + ), + 'newValue' => array( + 'id' => 622, + 'name' => 'TestCompany017089465', + ), + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + ), + 'pagination' => array( + 'limit' => 100, + 'totalCount' => 1, + 'currentPage' => 1, + 'totalPageCount' => 1, + ), + ); + } + + private function get_history_change_only_contact($orderExternalId) + { + return array( + 'success' => true, + 'generatedAt' => '2020-06-05 17:36:28', + 'history' => array( + array( + 'id' => 25989, + 'createdAt' => '2020-06-05 17:36:20', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'contact', + 'oldValue' => array( + 'id' => 4304, + 'site' => 'woocommerce', + ), + 'newValue' => array( + 'id' => 4305, + 'site' => 'woocommerce', + ), + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25990, + 'createdAt' => '2020-06-05 17:36:20', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'email', + 'oldValue' => 'tester4867@example.com', + 'newValue' => 'tester2890@example.com', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25991, + 'createdAt' => '2020-06-05 17:36:20', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'first_name', + 'oldValue' => 'Tester4867', + 'newValue' => 'Tester2890', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25992, + 'createdAt' => '2020-06-05 17:36:20', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'last_name', + 'oldValue' => 'Tester4867', + 'newValue' => 'Tester2890', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25993, + 'createdAt' => '2020-06-05 17:36:20', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'patronymic', + 'oldValue' => 'Tester4867', + 'newValue' => 'Tester2890', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25994, + 'createdAt' => '2020-06-05 17:36:20', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'phone', + 'oldValue' => null, + 'newValue' => '32418790888', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + ), + 'pagination' => array( + 'limit' => 100, + 'totalCount' => 6, + 'currentPage' => 1, + 'totalPageCount' => 1, + ), + ); + } + + private function get_history_change_from_corporate_to_individual($orderExternalId) + { + return array( + 'success' => true, + 'generatedAt' => '2020-06-05 17:47:05', + 'history' => array( + array( + 'id' => 25995, + 'createdAt' => '2020-06-05 17:46:58', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'contragent.contragent_type', + 'oldValue' => 'legal-entity', + 'newValue' => 'individual', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25996, + 'createdAt' => '2020-06-05 17:46:58', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'contact', + 'oldValue' => array( + 'id' => 4305, + 'site' => 'woocommerce', + ), + 'newValue' => array( + 'id' => 4228, + 'externalId' => '2', + 'site' => 'bitrix-test', + ), + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25997, + 'createdAt' => '2020-06-05 17:46:58', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'customer', + 'oldValue' => array( + 'id' => 4303, + 'site' => 'woocommerce', + ), + 'newValue' => array( + 'id' => 4228, + 'externalId' => '2', + 'site' => 'bitrix-test', + ), + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25998, + 'createdAt' => '2020-06-05 17:46:58', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'email', + 'oldValue' => 'tester2890@example.com', + 'newValue' => 'tester001@example.com', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 25999, + 'createdAt' => '2020-06-05 17:46:58', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'first_name', + 'oldValue' => 'Tester2890', + 'newValue' => 'tester001', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 26000, + 'createdAt' => '2020-06-05 17:46:58', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'last_name', + 'oldValue' => 'Tester2890', + 'newValue' => 'tester001', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 26001, + 'createdAt' => '2020-06-05 17:46:58', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'patronymic', + 'oldValue' => 'Tester2890', + 'newValue' => null, + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + array( + 'id' => 26002, + 'createdAt' => '2020-06-05 17:46:58', + 'source' => 'user', + 'user' => array( + 'id' => 27, + ), + 'field' => 'phone', + 'oldValue' => '32418790888', + 'newValue' => '2354708915097', + 'order' => array( + 'id' => 5868, + 'externalId' => $orderExternalId, + 'managerId' => 27, + 'site' => 'woocommerce', + 'status' => 'new', + ), + ), + ), + 'pagination' => array( + 'limit' => 100, + 'totalCount' => 8, + 'currentPage' => 1, + 'totalPageCount' => 1, + ), + ); + } + + private function get_initial_regular_customer() + { + return array( + 'type' => 'customer', + 'id' => 4228, + 'externalId' => '2', + 'isContact' => false, + 'createdAt' => '2020-06-01 15:31:46', + 'managerId' => 27, + 'vip' => false, + 'bad' => false, + 'site' => 'bitrix-test', + 'contragent' => array( + 'contragentType' => 'individual', + ), + 'tags' => array(), + 'marginSumm' => 9428, + 'totalSumm' => 9428, + 'averageSumm' => 4714, + 'ordersCount' => 2, + 'costSumm' => 0, + 'personalDiscount' => 0, + 'address' => array( + 'id' => 3132, + 'text' => 'ул. Пушкина дом Колотушкина', + ), + 'firstName' => 'tester001', + 'lastName' => 'tester001', + 'email' => 'tester001@example.com', + 'emailMarketingUnsubscribedAt' => '2020-06-01 15:34:23', + 'phones' => array( + array( + 'number' => '2354708915097', + ), + ), + ); + } + + private function get_contact_when_only_contact_changed() + { + return array( + 'type' => 'customer', + 'id' => 4305, + 'isContact' => true, + 'createdAt' => '2020-06-05 17:11:53', + 'managerId' => 27, + 'vip' => false, + 'bad' => false, + 'site' => 'woocommerce', + 'tags' => array(), + 'marginSumm' => 0, + 'totalSumm' => 0, + 'averageSumm' => 0, + 'ordersCount' => 0, + 'costSumm' => 0, + 'customFields' => array( + 'galkatrue' => true, + ), + 'personalDiscount' => 0, + 'segments' => array(), + 'firstName' => 'Tester2890', + 'lastName' => 'Tester2890', + 'patronymic' => 'Tester2890', + 'email' => 'tester2890@example.com', + 'phones' => array( + array( + 'number' => '32418790888', + ), + ), + ); + } + + private function get_order_with_customer_and_contact( + $customer, + $contact = array(), + $company = array(), + $contragentType = 'individual' + ) { + $order = array( + 'success' => true, + 'order' => array( + 'slug' => 5868, + 'id' => 5868, + 'number' => '5868C', + 'externalId' => '85', + 'customer' => $customer, + 'contact' => empty($contact) ? $customer : $contact, + 'orderType' => 'test', + 'orderMethod' => 'phone', + 'countryIso' => 'RU', + 'createdAt' => '2020-06-04 15:05:10', + 'statusUpdatedAt' => '2020-06-04 15:05:10', + 'summ' => 16, + 'totalSumm' => 16, + 'prepaySum' => 0, + 'purchaseSumm' => 0, + 'markDatetime' => '2020-06-04 15:05:10', + 'lastName' => 'tester002', + 'firstName' => 'tester002', + 'patronymic' => 'tester002', + 'phone' => '34687453268933', + 'email' => 'ewtrhibehb126879@example.com', + 'call' => false, + 'expired' => false, + 'managerId' => 27, + 'contragent' => array( + 'contragentType' => $contragentType, + ), + 'delivery' => array( + 'cost' => 0, + 'netCost' => 0, + 'address' => array( + 'index' => '34000', + 'countryIso' => 'RU', + 'region' => 'Адыгея Республика', + 'regionId' => 26, + 'city' => 'с. Верхненазаровское', + 'cityId' => 240863, + 'street' => 'ул. Зеленая', + 'streetId' => 962815, + 'building' => '22', + 'text' => 'ул. Зеленая, д. 22', + ), + ), + 'site' => 'woocommerce', + 'status' => 'new', + 'items' => array( + array( + 'markingCodes' => array(), + 'id' => 8955, + 'externalIds' => array( + array( + 'code' => 'woocomerce', + 'value' => '23_31', + ), + ), + 'priceType' => array( + 'code' => 'base', + ), + 'initialPrice' => 16, + 'discountTotal' => 0, + 'vatRate' => 'none', + 'createdAt' => '2020-06-04 14:54:54', + 'quantity' => 1, + 'status' => 'new', + 'offer' => array( + 'displayName' => 'Cap', + 'id' => 67424, + 'externalId' => '23', + 'name' => 'Cap', + 'vatRate' => 'none', + 'unit' => array( + 'code' => 'pc', + 'name' => 'Штука', + 'sym' => 'шт.', + ), + ), + 'properties' => array(), + 'purchasePrice' => 0, + ), + ), + 'payments' => array(), + 'fromApi' => false, + 'length' => 0, + 'width' => 0, + 'height' => 0, + 'shipmentStore' => 'main', + 'shipped' => false, + 'customFields' => array( + 'galka' => false, + 'test_number' => 0, + 'otpravit_dozakaz' => false, + ), + ), + ); + + if (!empty($company)) { + $order['company'] = $company; + } + + return $order; + } + + private function get_new_individual_for_order() + { + return array( + 'type' => 'customer', + 'id' => 4231, + 'isContact' => false, + 'createdAt' => '2020-06-01 15:50:33', + 'managerId' => 27, + 'vip' => false, + 'bad' => false, + 'site' => 'bitrix-test', + 'contragent' => array( + 'contragentType' => 'individual', + ), + 'tags' => array(), + 'marginSumm' => 2144, + 'totalSumm' => 2144, + 'averageSumm' => 1072, + 'ordersCount' => 2, + 'costSumm' => 0, + 'customFields' => array( + 'galkatrue' => true, + ), + 'personalDiscount' => 0, + 'address' => array( + 'id' => 3135, + 'index' => '34000', + 'countryIso' => 'RU', + 'region' => 'Адыгея Республика', + 'regionId' => 26, + 'city' => 'с. Верхненазаровское', + 'cityId' => 240863, + 'street' => 'ул. Зеленая', + 'streetId' => 962815, + 'building' => '22', + 'text' => 'ул. Зеленая, д. 22', + ), + 'firstName' => 'tester002', + 'lastName' => 'tester002', + 'patronymic' => 'tester002', + 'email' => 'ewtrhibehb126879@example.com', + 'phones' => array( + array( + 'number' => '34687453268933', + ), + ), + ); + } + + private function get_new_corporate_for_order() + { + return array( + 'type' => 'customer_corporate', + 'id' => 4220, + 'nickName' => 'Компания1', + 'mainAddress' => array( + 'id' => 3131, + 'name' => 'Компания2', + ), + 'createdAt' => '2020-05-27 15:20:33', + 'managerId' => 27, + 'vip' => false, + 'bad' => false, + 'site' => 'woocommerce', + 'tags' => array(), + 'marginSumm' => 604, + 'totalSumm' => 604, + 'averageSumm' => 604, + 'ordersCount' => 1, + 'costSumm' => 0, + 'customFields' => array( + 'galkatrue' => true, + ), + 'personalDiscount' => 0, + 'mainCustomerContact' => array( + 'id' => 711, + 'customer' => array( + 'id' => 4219, + 'externalId' => '4', + ), + 'companies' => array(), + ), + 'mainCompany' => array( + 'id' => 591, + 'name' => 'Компания1', + ), + ); + } + + private function get_new_contact_for_order() + { + return array( + 'type' => 'customer', + 'id' => 4219, + 'externalId' => '4', + 'isContact' => false, + 'createdAt' => '2020-05-27 12:09:00', + 'managerId' => 27, + 'vip' => false, + 'bad' => false, + 'site' => 'woocommerce', + 'contragent' => array( + 'contragentType' => 'individual', + ), + 'tags' => array(), + 'marginSumm' => 0, + 'totalSumm' => 0, + 'averageSumm' => 0, + 'ordersCount' => 0, + 'costSumm' => 0, + 'customFields' => array( + 'galkatrue' => true, + ), + 'personalDiscount' => 0, + 'address' => array( + 'id' => 3130, + 'index' => '344091', + 'countryIso' => 'RU', + 'region' => 'Еврейская Автономная область', + 'regionId' => 47, + 'city' => 'Валдгейм', + 'text' => 'упцупуцйпуц, йцавафыафыафыафы', + ), + 'firstName' => 'psycho913', + 'lastName' => 'psycho913', + 'email' => 'psycho913@example.com', + 'phones' => array( + array( + 'number' => '9135487458709', + ), + ), + ); + } + + private function get_another_corporate_for_order() + { + return array( + 'type' => 'customer_corporate', + 'id' => 4303, + 'nickName' => 'Another Test Legal Entity', + 'mainAddress' => array( + 'id' => 3177, + 'name' => 'Test Address', + ), + 'createdAt' => '2020-06-05 16:34:05', + 'managerId' => 27, + 'vip' => false, + 'bad' => false, + 'site' => 'woocommerce', + 'tags' => array(), + 'marginSumm' => 0, + 'totalSumm' => 0, + 'averageSumm' => 0, + 'ordersCount' => 0, + 'customFields' => array( + 'galkatrue' => true, + ), + 'personalDiscount' => 0, + 'mainCustomerContact' => array( + 'id' => 748, + 'customer' => array( + 'id' => 4304, + ), + 'companies' => array( + array( + 'id' => 110, + 'company' => array( + 'id' => 621, + 'name' => 'TestCompany3428769', + ), + ), + ), + ), + 'mainCompany' => array( + 'id' => 621, + 'name' => 'TestCompany3428769', + ), + ); + } + + private function get_another_contact_for_order() + { + return array( + 'type' => 'customer', + 'id' => 4304, + 'isContact' => true, + 'createdAt' => '2020-06-05 16:34:27', + 'vip' => false, + 'bad' => false, + 'site' => 'woocommerce', + 'tags' => array(), + 'marginSumm' => 0, + 'totalSumm' => 0, + 'averageSumm' => 0, + 'ordersCount' => 0, + 'personalDiscount' => 0, + 'segments' => array(), + 'firstName' => 'Tester4867', + 'lastName' => 'Tester4867', + 'patronymic' => 'Tester4867', + 'sex' => 'male', + 'email' => 'tester4867@example.com', + 'phones' => array(), + ); + } } diff --git a/tests/test-wc-retailcrm-orders.php b/tests/test-wc-retailcrm-orders.php index 8366976..28e4f53 100644 --- a/tests/test-wc-retailcrm-orders.php +++ b/tests/test-wc-retailcrm-orders.php @@ -28,12 +28,11 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper /** * @param $retailcrm - * @param $apiVersion * @dataProvider dataProviderRetailcrm */ - public function test_order_upload($retailcrm, $apiVersion) + public function test_order_upload($retailcrm) { - $this->options = $this->setOptions($apiVersion); + $this->options = $this->setOptions(); $retailcrm_orders = $this->getRetailcrmOrders($retailcrm); $upload_orders = $retailcrm_orders->ordersUpload(); @@ -46,10 +45,9 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper /** * @param $retailcrm - * @param $apiVersion * @dataProvider dataProviderRetailcrm */ - public function test_order_create($retailcrm, $apiVersion) + public function test_order_create($retailcrm) { if ($retailcrm) { $responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') @@ -82,7 +80,7 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper } $this->createTestOrder(); - $this->options = $this->setOptions($apiVersion); + $this->options = $this->setOptions(); $retailcrm_orders = $this->getRetailcrmOrders($retailcrm); $order = $retailcrm_orders->orderCreate($this->order->get_id()); $order_send = $retailcrm_orders->getOrder(); @@ -127,15 +125,15 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper /** * @param $isSuccessful * @param $retailcrm - * @param $apiVersion + * @dataProvider dataProviderUpdateOrder */ - public function test_update_order($isSuccessful, $retailcrm, $apiVersion) + public function test_update_order($isSuccessful, $retailcrm) { $this->createTestOrder(); - $this->options = $this->setOptions($apiVersion); + $this->options = $this->setOptions(); - if ($retailcrm && $apiVersion == 'v5') { + if ($retailcrm) { $responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') ->disableOriginalConstructor() ->setMethods(array( @@ -222,23 +220,19 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper return array( array( 'is_successful' => true, - 'retailcrm' => $this->apiMock, - 'api_version' => 'v5' + 'retailcrm' => $this->apiMock ), array( 'is_successful' => true, - 'retailcrm' => false, - 'api_version' => 'v5' + 'retailcrm' => false ), array( 'is_successful' => false, - 'retailcrm' => false, - 'api_version' => 'v5' + 'retailcrm' => false ), array( 'is_successful' => false, - 'retailcrm' => $this->apiMock, - 'api_version' => 'v5' + 'retailcrm' => $this->apiMock ) ); } @@ -249,12 +243,10 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper return array( array( - 'retailcrm' => $this->apiMock, - 'api_version' => 'v5' + 'retailcrm' => $this->apiMock ), array( - 'retailcrm' => false, - 'api_version' => 'v5' + 'retailcrm' => false ) ); } diff --git a/tests/test-wc-retailcrm-plugin.php b/tests/test-wc-retailcrm-plugin.php index f858c87..9c6fd04 100644 --- a/tests/test-wc-retailcrm-plugin.php +++ b/tests/test-wc-retailcrm-plugin.php @@ -26,14 +26,13 @@ class WC_Retailcrm_Plugin_Test extends WC_Retailcrm_Test_Case_Helper /** * @param $retailcrm * @param $response - * @param $apiVersion * * @dataProvider dataProviderIntegrationModule */ - public function test_integration_module($retailcrm,$response, $apiVersion) + public function test_integration_module($retailcrm, $response) { $client_id = uniqid(); - $result = WC_Retailcrm_Plugin::integration_module($retailcrm, $client_id, $apiVersion); + $result = WC_Retailcrm_Plugin::integration_module($retailcrm, $client_id); if (!$retailcrm || $response['success'] == false) { $this->assertEquals(false, $result); @@ -58,22 +57,12 @@ class WC_Retailcrm_Plugin_Test extends WC_Retailcrm_Test_Case_Helper private function getResponseData() { return array( - 'v4' => array( - "true" => array( - "success" => true - ), - "false" => array( - "success" => false - ) + "true" => array( + "success" => true ), - 'v5' => array( - "true" => array( - "success" => true - ), - "false" => array( - "success" => false, - "errorMsg" => "Forbidden" - ) + "false" => array( + "success" => false, + "errorMsg" => "Forbidden" ) ); } @@ -84,61 +73,25 @@ class WC_Retailcrm_Plugin_Test extends WC_Retailcrm_Test_Case_Helper return array( array( - 'retailcrm' => $this->getApiMock( - 'v4', - $this->getResponseData['v4']['true'] - ), - 'response' => $this->getResponseData['v4']['true'], - 'apiVersion' => 'v4' + 'retailcrm' => $this->getApiMock($this->getResponseData()['true']), + 'response' => $this->getResponseData()['true'] ), array( 'retailcrm' => false, - 'response' => $this->getResponseData['v4']['true'], - 'apiVersion' => 'v4' + 'response' => $this->getResponseData()['true'] ), array( - 'retailcrm' => $this->getApiMock( - 'v4', - $this->getResponseData['v4']['false'] - ), - 'response' => $this->getResponseData['v4']['false'], - 'apiVersion' => 'v4' + 'retailcrm' => $this->getApiMock($this->getResponseData()['false']), + 'response' => $this->getResponseData()['false'] ), array( 'retailcrm' => false, - 'response' => $this->getResponseData['v4']['false'], - 'apiVersion' => 'v4' - ), - array( - 'retailcrm' => $this->getApiMock( - 'v5', - $this->getResponseData['v5']['true'] - ), - 'response' => $this->getResponseData['v5']['true'], - 'apiVersion' => 'v5' - ), - array( - 'retailcrm' => false, - 'response' => $this->getResponseData['v5']['true'], - 'apiVersion' => 'v5' - ), - array( - 'retailcrm' => $this->getApiMock( - 'v5', - $this->getResponseData['v5']['false'] - ), - 'response' => $this->getResponseData['v5']['false'], - 'apiVersion' => 'v5' - ), - array( - 'retailcrm' => false, - 'response' => $this->getResponseData['v5']['false'], - 'apiVersion' => 'v5' + 'response' => $this->getResponseData()['false'] ) ); } - private function getApiMock($apiVersion, $response) + private function getApiMock($response) { $responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') ->disableOriginalConstructor() @@ -159,29 +112,16 @@ class WC_Retailcrm_Plugin_Test extends WC_Retailcrm_Test_Case_Helper $responseMock->setResponse($response); - if ($apiVersion == 'v4') { - $apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') - ->disableOriginalConstructor() - ->setMethods(array( - 'marketplaceSettingsEdit' - )) - ->getMock(); - - $apiMock->expects($this->any()) - ->method('marketplaceSettingsEdit') - ->willReturn($responseMock); - } else { - $apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') + $apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') ->disableOriginalConstructor() ->setMethods(array( 'integrationModulesEdit' )) ->getMock(); - $apiMock->expects($this->any()) - ->method('integrationModulesEdit') - ->willReturn($responseMock); - } + $apiMock->expects($this->any()) + ->method('integrationModulesEdit') + ->willReturn($responseMock); return $apiMock; }