diff --git a/.github/workflows/woo.yml b/.github/workflows/woo.yml index 01c88c5..864898a 100644 --- a/.github/workflows/woo.yml +++ b/.github/workflows/woo.yml @@ -464,6 +464,7 @@ jobs: php --version composer --version phpunit --version + - name: Install Woocommerce env: WP_VERSION: ${{ matrix.wp }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 25dcaac..5a3f1d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 2021-12-15 4.4.0 +* Migrating to PHP 5.6. We tested the module, improved performance, security and test coverage. +* Add validate countryIso. Fix bug with duplicate customer address +* Fix bugs in history +* Fix PHP warning and deprecated +* Add documentation for module + ## 2021-08-30 4.3.8 * Updated logic work address * Added transfer of the client's comment to the WC order diff --git a/VERSION b/VERSION index 738a7f0..64b5ae3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.3.8 \ No newline at end of file +4.4.0 \ No newline at end of file diff --git a/src/include/abstracts/class-wc-retailcrm-abstract-builder.php b/src/include/abstracts/class-wc-retailcrm-abstract-builder.php index dc2ac80..e7e2165 100644 --- a/src/include/abstracts/class-wc-retailcrm-abstract-builder.php +++ b/src/include/abstracts/class-wc-retailcrm-abstract-builder.php @@ -21,6 +21,8 @@ abstract class WC_Retailcrm_Abstract_Builder implements WC_Retailcrm_Builder_Int /** * @return array|mixed + * + * @codeCoverageIgnore */ public function getData() { diff --git a/src/include/abstracts/class-wc-retailcrm-abstracts-data.php b/src/include/abstracts/class-wc-retailcrm-abstracts-data.php index e968dc4..16698bc 100644 --- a/src/include/abstracts/class-wc-retailcrm-abstracts-data.php +++ b/src/include/abstracts/class-wc-retailcrm-abstracts-data.php @@ -29,6 +29,9 @@ abstract class WC_Retailcrm_Abstracts_Data */ abstract public function build($data); + /** + * @codeCoverageIgnore + */ protected function set_data_field($field, $value) { if (isset($this->data[$field]) && \gettype($value) !== \gettype($this->data[$field])) { @@ -45,8 +48,10 @@ abstract class WC_Retailcrm_Abstracts_Data */ protected function set_data_fields($fields) { - foreach ($fields as $field => $value) { - $this->set_data_field($field, $value); + if (!empty($fields)) { + foreach ($fields as $field => $value) { + $this->set_data_field($field, $value); + } } } diff --git a/src/include/abstracts/class-wc-retailcrm-abstracts-settings.php b/src/include/abstracts/class-wc-retailcrm-abstracts-settings.php index 81ba5d2..6e341e5 100644 --- a/src/include/abstracts/class-wc-retailcrm-abstracts-settings.php +++ b/src/include/abstracts/class-wc-retailcrm-abstracts-settings.php @@ -39,6 +39,9 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration } + /** + * @codeCoverageIgnore + */ public function ajax_generate_icml() { $ajax_url = admin_url('admin-ajax.php'); @@ -59,6 +62,9 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration } + /** + * @codeCoverageIgnore + */ public function ajax_selected_order() { $ajax_url = admin_url('admin-ajax.php'); @@ -140,7 +146,7 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration 'placeholder' => __( 'Insert the Online consultant code here', 'retailcrm' ) ); - $this->form_fields[] = array( + $this->form_fields['catalog_options'] = array( 'title' => __( 'Catalog settings', 'retailcrm' ), 'type' => 'heading', 'description' => '', @@ -521,7 +527,7 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration /** * Debug information */ - $this->form_fields[] = array( + $this->form_fields['debug-info'] = array( 'title' => __('Debug information', 'retailcrm'), 'type' => 'heading', 'class' => 'debug_info_options' @@ -549,6 +555,8 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration * @param array $data * * @return string + * + * @codeCoverageIgnore */ public function generate_button_html($key, $data) { @@ -590,6 +598,8 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration * @param array $data * * @return string + * + * @codeCoverageIgnore */ public function generate_heading_html($key, $data) { @@ -616,8 +626,10 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration /** * Returns the original value for the online_consultant field (ignores woocommerce validation) + * * @param $key * @param $value + * * @return string */ public function validate_online_assistant_field($key, $value) @@ -637,6 +649,8 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration * @param string $key * @param string $value * + * @codeCoverageIgnore + * * @return string */ public function validate_api_url_field($key, $value) @@ -664,6 +678,8 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration * @param string $key * @param string $value * + * @codeCoverageIgnore + * * @return string */ public function validate_api_key_field($key, $value) @@ -696,6 +712,8 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration * @param string $key * @param string $value * + * @codeCoverageIgnore + * * @return string */ public function validate_whatsapp_number_field($key, $value) @@ -717,6 +735,8 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration /** * Scritp show|hide block settings + * + * @codeCoverageIgnore */ function show_blocks() { @@ -744,6 +764,8 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration /** * Add button in admin + * + * @codeCoverageIgnore */ function add_retailcrm_button() { global $wp_admin_bar; @@ -777,4 +799,4 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration ) ); } -} \ No newline at end of file +} diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index ce00542..90fcef1 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -18,21 +18,9 @@ if (!class_exists('WC_Retailcrm_Base')) { */ class WC_Retailcrm_Base extends WC_Retailcrm_Abstracts_Settings { - /** @var string */ - protected $api_url; - - /** @var string */ - protected $api_key; - /** @var \WC_Retailcrm_Proxy|WC_Retailcrm_Client_V4|WC_Retailcrm_Client_V5|bool */ protected $apiClient; - /** @var mixed */ - protected $order_item; - - /** @var mixed */ - protected $order_address; - /** @var \WC_Retailcrm_Customers */ protected $customers; @@ -118,7 +106,6 @@ if (!class_exists('WC_Retailcrm_Base')) { add_action('retailcrm_deactivate', array($this, 'deactivate')); } - /** * Init settings fields */ @@ -128,7 +115,6 @@ if (!class_exists('WC_Retailcrm_Base')) { $this->init_settings(); } - /** * @param $settings * @@ -167,7 +153,11 @@ if (!class_exists('WC_Retailcrm_Base')) { return $settings; } - + /** + * Generate ICML + * + * @codeCoverageIgnore Check in another tests + */ public function generate_icml() { /* @@ -212,9 +202,10 @@ if (!class_exists('WC_Retailcrm_Base')) { $retailCrmIcml->generate(); } - /** * Get history + * + * @codeCoverageIgnore Check in another tests */ public function retailcrm_history_get() { @@ -222,18 +213,20 @@ if (!class_exists('WC_Retailcrm_Base')) { $retailcrm_history->getHistory(); } - /** * @param int $order_id + * + * @codeCoverageIgnore Check in another tests */ public function retailcrm_process_order($order_id) { $this->orders->orderCreate($order_id); } - /** * Load stock from retailCRM + * + * @codeCoverageIgnore Check in another tests */ public function load_stocks() { @@ -241,10 +234,11 @@ if (!class_exists('WC_Retailcrm_Base')) { $inventories->updateQuantity(); } - /** * Upload selected orders * + * @codeCoverageIgnore Check in another tests + * * @return void */ public function order_upload() @@ -252,9 +246,10 @@ if (!class_exists('WC_Retailcrm_Base')) { $this->uploader->uploadSelectedOrders(); } - /** * Upload archive customers and order to retailCRM + * + * @codeCoverageIgnore Check in another tests */ public function upload_to_crm() { @@ -268,12 +263,12 @@ if (!class_exists('WC_Retailcrm_Base')) { } } - /** * Create customer in retailCRM * * @param int $customer_id * + * @codeCoverageIgnore There is a task for analysis * @return void * @throws \Exception */ @@ -330,9 +325,11 @@ if (!class_exists('WC_Retailcrm_Base')) { } } - /** * Edit customer in retailCRM + * + * @codeCoverageIgnore Check in another tests + * * @param int $customer_id */ public function update_customer($customer_id) @@ -348,10 +345,11 @@ if (!class_exists('WC_Retailcrm_Base')) { $this->customers->updateCustomer($customer_id); } - /** * Create order in retailCRM from admin panel * + * @codeCoverageIgnore Check in another tests + * * @param int $order_id */ public function create_order($order_id) @@ -361,10 +359,11 @@ if (!class_exists('WC_Retailcrm_Base')) { } } - /** * Edit order in retailCRM * + * @codeCoverageIgnore Check in another tests + * * @param int $order_id * * @throws \Exception @@ -378,7 +377,6 @@ if (!class_exists('WC_Retailcrm_Base')) { $this->orders->updateOrder($order_id); } - /** * Init google analytics code */ @@ -392,9 +390,10 @@ if (!class_exists('WC_Retailcrm_Base')) { } } - /** * Google analytics send code + * + * @codeCoverageIgnore Check in another tests */ public function send_analytics() { @@ -406,9 +405,10 @@ if (!class_exists('WC_Retailcrm_Base')) { } } - /** * Daemon collector + * + * @codeCoverageIgnore Check in another tests */ public function initialize_daemon_collector() { @@ -420,7 +420,6 @@ if (!class_exists('WC_Retailcrm_Base')) { } } - /** * Initialize online consultant */ @@ -431,10 +430,11 @@ if (!class_exists('WC_Retailcrm_Base')) { } } - /** * In this method we include files in admin WP * + * @codeCoverageIgnore + * * @return void */ public function include_files_for_admin() @@ -443,10 +443,11 @@ if (!class_exists('WC_Retailcrm_Base')) { $this->include_js_scripts_for_admin(); } - /** * In this method we include CSS file * + * @codeCoverageIgnore + * * @return void */ private function include_css_files_for_admin() @@ -462,10 +463,11 @@ if (!class_exists('WC_Retailcrm_Base')) { wp_enqueue_style('retailcrm-debug-info-style'); } - /** * In this method we include JS scripts. * + * @codeCoverageIgnore + * * @return void */ private function include_js_scripts_for_admin() @@ -479,9 +481,12 @@ if (!class_exists('WC_Retailcrm_Base')) { wp_enqueue_script('retailcrm-cron-info', $path . 'retailcrm-export.js', '', '', true); } - /** * Include style for WhatsApp icon + * + * @codeCoverageIgnore + * + * @return void */ public function include_whatsapp_icon_style() { @@ -489,7 +494,6 @@ if (!class_exists('WC_Retailcrm_Base')) { wp_enqueue_style('whatsapp_icon_style'); } - /** * Initialize WhatsApp */ @@ -514,7 +518,6 @@ if (!class_exists('WC_Retailcrm_Base')) { } } - /** * Return count upload data */ @@ -530,7 +533,6 @@ if (!class_exists('WC_Retailcrm_Base')) { wp_die(); } - /** * Return time work next cron */ @@ -562,7 +564,6 @@ if (!class_exists('WC_Retailcrm_Base')) { wp_die(); } - /** * Get retailcrm api client * @@ -581,7 +582,6 @@ if (!class_exists('WC_Retailcrm_Base')) { return false; } - /** * Deactivate module in marketplace retailCRM * diff --git a/src/include/class-wc-retailcrm-customers.php b/src/include/class-wc-retailcrm-customers.php index e6c4170..8e02727 100644 --- a/src/include/class-wc-retailcrm-customers.php +++ b/src/include/class-wc-retailcrm-customers.php @@ -51,19 +51,13 @@ if (!class_exists('WC_Retailcrm_Customers')) : } /** - * setCustomerAddress + * Return corporate customer * - * @param $address - * - * @return $this + * @return array */ - public function setCustomerAddress($address) + public function getCorporateCustomer() { - if ($address instanceof WC_Retailcrm_Customer_Address) { - $this->customer_address = $address; - } - - return $this; + return $this->customerCorporate; } /** @@ -201,11 +195,13 @@ if (!class_exists('WC_Retailcrm_Customers')) : } /** - * Create new address in corporate customer (if needed) + * Create new address in corporate customer (if needed). * * @param int $corporateId * @param \WC_Customer $customer * @param \WC_Order|null $order + * + * @return bool */ public function fillCorporateAddress($corporateId, $customer, $order = null) { @@ -213,7 +209,6 @@ if (!class_exists('WC_Retailcrm_Customers')) : $builder = new WC_Retailcrm_Customer_Corporate_Address(); $newAddress = $builder ->setIsMain(false) - ->setExplicitIsMain(false) ->build($customer, $order) ->get_data(); @@ -225,7 +220,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : 'id' ); - if ($addresses && $addresses->isSuccessful() && $addresses->offsetExists('addresses')) { + if (!empty($addresses['addresses']) && $addresses->isSuccessful()) { foreach ($addresses['addresses'] as $address) { foreach ($newAddress as $field => $value) { if (isset($address[$field]) && $address[$field] != $value) { @@ -249,6 +244,8 @@ if (!class_exists('WC_Retailcrm_Customers')) : $this->retailcrm->getSingleSiteForKey() ); } + + return $found; } /** @@ -261,7 +258,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : */ protected function fillCorporateCustomer($response) { - if (!$response->isSuccessful() || $response->isSuccessful() && !$response->offsetExists('id')) { + if ((empty($response) || !$response->isSuccessful()) && !$response->offsetExists('id')) { return null; } diff --git a/src/include/class-wc-retailcrm-ga.php b/src/include/class-wc-retailcrm-ga.php index fe2be39..d364b65 100644 --- a/src/include/class-wc-retailcrm-ga.php +++ b/src/include/class-wc-retailcrm-ga.php @@ -69,7 +69,7 @@ if (!class_exists('WC_Retailcrm_Google_Analytics')) { $order_id = wc_get_order_id_by_order_key($_GET['key']); $order = wc_get_order($order_id); - if (!$order) { + if (is_object($order) === false) { return $js; } diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 72ee2e6..597b4d8 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -35,9 +35,6 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : /** @var string */ protected $bindField = 'externalId'; - /** @var WC_Retailcrm_Order_Item */ - protected $orderItem; - /** * WC_Retailcrm_History constructor. * @@ -49,7 +46,8 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : { $this->retailcrmSettings = get_option(WC_Retailcrm_Base::$option_key); - if (isset($this->retailcrmSettings['bind_by_sku']) + if ( + isset($this->retailcrmSettings['bind_by_sku']) && $this->retailcrmSettings['bind_by_sku'] == WC_Retailcrm_Base::YES ) { $this->bindField = 'xmlId'; @@ -78,23 +76,29 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : $ordersSinceId = get_option('retailcrm_orders_history_since_id'); $customersSinceId = get_option('retailcrm_customers_history_since_id'); + // @codeCoverageIgnoreStart + // TODO: There is a task to analyze the work of getting history by date if (!$ordersSinceId && isset($this->retailcrmSettings['history_orders'])) { $this->startDateOrders = new DateTime($this->retailcrmSettings['history_orders']); } + if (!$customersSinceId && isset($this->retailcrmSettings['history_customers'])) { $this->startDateCustomers = new DateTime($this->retailcrmSettings['history_customers']); } + // @codeCoverageIgnoreEnd try { $this->customersHistory($this->startDateCustomers->format('Y-m-d H:i:s'), $customersSinceId); $this->ordersHistory($this->startDateOrders->format('Y-m-d H:i:s'), $ordersSinceId); + // @codeCoverageIgnoreStart } catch (\Exception $exception) { WC_Retailcrm_Logger::add( sprintf("[%s] - %s", $exception->getMessage(), 'Exception in file - ' . $exception->getFile() . ' on line ' . $exception->getLine()) ); } + // @codeCoverageIgnoreEnd } /** @@ -132,6 +136,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : WC_Retailcrm_Logger::debug(__METHOD__, array('Assembled customers history:', $customers)); foreach ($customers as $crmCustomer) { + // Only update customers, if customer not exist in WP - skip this customer ! if (!isset($crmCustomer['externalId'])) { continue; } @@ -139,6 +144,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : try { $builder->reset(); + // @codeCoverageIgnoreStart if (!$builder->loadExternalId($crmCustomer['externalId'])) { WC_Retailcrm_Logger::addCaller(__METHOD__, sprintf( 'Customer with id=%s is not found in the DB, skipping...', @@ -146,6 +152,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : )); continue; } + // @codeCoverageIgnoreEnd $wcCustomer = $builder ->setData($crmCustomer) @@ -157,6 +164,8 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : } WC_Retailcrm_Logger::debug(__METHOD__, array('Updated WC_Customer:', $wcCustomer)); + + // @codeCoverageIgnoreStart } catch (\Exception $exception) { WC_Retailcrm_Logger::error(sprintf( 'Error while trying to process history: %s', @@ -169,6 +178,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : )); WC_Retailcrm_Logger::error($exception->getTraceAsString()); } + // @codeCoverageIgnoreEnd } update_option('retailcrm_customers_history_since_id', $lastChange['id']); @@ -206,6 +216,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : if (!empty($history)) { $lastChange = end($history); $historyAssembly = WC_Retailcrm_History_Assembler::assemblyOrder($history); + WC_Retailcrm_Logger::debug(__METHOD__, array('Assembled orders history:', $historyAssembly)); WC_Retailcrm_Plugin::$history_run = true; @@ -226,8 +237,10 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : $wcOrder = wc_get_order($wcOrderId); if ($wcOrder instanceof WC_Order) { - $this->update_total($wcOrder); + $wcOrder->calculate_totals(); } + + // @codeCoverageIgnoreStart } catch (Exception $exception) { WC_Retailcrm_Logger::add( sprintf("[%s] - %s", $exception->getMessage(), @@ -236,6 +249,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : continue; } + // @codeCoverageIgnoreEnd } update_option('retailcrm_orders_history_since_id', $lastChange['id']); @@ -306,18 +320,6 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : return true; } - /** - * Calculate totals in order - * - * @param WC_Order $order - * - * @return void - */ - protected function update_total($order) - { - $order->calculate_totals(); - } - /** * Update order in WC * @@ -366,13 +368,17 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : } if (isset($order['contact']['address'])) { + var_dump('test'); $billingAddress = $order['contact']['address']; - $wcOrder->set_billing_state(self::arrayValue($billingAddress, 'region', '')); - $wcOrder->set_billing_postcode(self::arrayValue($billingAddress, 'index', '')); - $wcOrder->set_billing_country(self::arrayValue($billingAddress, 'country', '')); - $wcOrder->set_billing_city(self::arrayValue($billingAddress, 'city', '')); - $wcOrder->set_billing_address_1(self::arrayValue($billingAddress, 'text', '')); + // @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_postcode(self::arrayValue($billingAddress, 'index')); + $wcOrder->set_billing_country(self::arrayValue($billingAddress, 'country')); + $wcOrder->set_billing_city(self::arrayValue($billingAddress, 'city')); + $wcOrder->set_billing_address_1(self::arrayValue($billingAddress, 'text')); + // @codeCoverageIgnoreEnd } } @@ -885,6 +891,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : if (!empty($crmOrder) && isset($crmOrder['items'][$item['id']])) { $woocommerceId = self::getItemWoocommerceId($crmOrder['items'][$item['id']]); } else { + // @codeCoverageIgnoreStart WC_Retailcrm_Logger::add( sprintf( "Order externalId=`%s`: item doesn't have woocomerceId, skipping... (item id=`%s`)", @@ -893,6 +900,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : ) ); continue; + // @codeCoverageIgnoreEnd } } else { $woocommerceId = self::getItemWoocommerceId($order['items'][$item['id']]); diff --git a/src/include/class-wc-retailcrm-inventories.php b/src/include/class-wc-retailcrm-inventories.php index de725a9..645e383 100644 --- a/src/include/class-wc-retailcrm-inventories.php +++ b/src/include/class-wc-retailcrm-inventories.php @@ -44,11 +44,11 @@ if (!class_exists('WC_Retailcrm_Inventories')) : * * @return mixed */ - public function load_stocks() + protected function load_stocks() { $success = array(); - if (!$this->retailcrm) { + if (!$this->retailcrm instanceof WC_Retailcrm_Proxy) { return null; } @@ -56,17 +56,17 @@ if (!class_exists('WC_Retailcrm_Inventories')) : $variationProducts = array(); do { - /** @var WC_Retailcrm_Response $result */ - $result = $this->retailcrm->storeInventories(array(), $page, 250); + /** @var WC_Retailcrm_Response $response */ + $response = $this->retailcrm->storeInventories(array(), $page, 250); - if (!$result->isSuccessful()) { + if (empty($response) || !$response->isSuccessful()) { return null; } - $totalPageCount = $result['pagination']['totalPageCount']; + $totalPageCount = $response['pagination']['totalPageCount']; $page++; - foreach ($result['offers'] as $offer) { + foreach ($response['offers'] as $offer) { if (isset($offer[$this->bind_field])) { $product = retailcrm_get_wc_product($offer[$this->bind_field], $this->retailcrm_settings); @@ -96,7 +96,7 @@ if (!class_exists('WC_Retailcrm_Inventories')) : if (is_object($variationProduct)) { $variationProduct->set_manage_stock(true); - $variationProduct->set_stock($quantity); + $variationProduct->set_stock_quantity($quantity); $success[] = $variationProduct->save(); } } @@ -121,3 +121,4 @@ if (!class_exists('WC_Retailcrm_Inventories')) : } } endif; + diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 4ec1eb5..1348bde 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -84,7 +84,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : try { $response = $this->retailcrm->ordersCreate($this->order); - if ($response instanceof WC_Retailcrm_Response) { + if ($response instanceof WC_Retailcrm_Proxy) { if ($response->isSuccessful()) { return $wcOrder; } @@ -138,6 +138,8 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : } } + // @codeCoverageIgnoreStart + // TODO: There is a task for analysis if ($update && $customerWasChanged) { $firstName = $wcOrder->get_shipping_first_name(); $lastName = $wcOrder->get_shipping_last_name(); @@ -145,6 +147,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $this->order['firstName'] = $firstName; $this->order['lastName'] = $lastName; } + // @codeCoverageIgnoreEnd return true; } @@ -203,12 +206,27 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : ); $this->order['customer']['id'] = $corporateId; } else { - $this->customers->fillCorporateAddress( + // Testing of this method occurs in customers tests. + // @codeCoverageIgnoreStart + $addressFound = $this->customers->fillCorporateAddress( $crmCorporate['id'], new WC_Customer($wcCustomerId), $wcOrder ); + + // If address not found create new address. + if (!$addressFound) { + WC_Retailcrm_Logger::add( + sprintf( + '[%d] => %s', + $this->order['customer']['id'], + 'Notification: Create new address for corporate customer' + ) + ); + } + $this->order['customer']['id'] = $crmCorporate['id']; + // @codeCoverageIgnoreEnd } $companiesResponse = $this->retailcrm->customersCorporateCompanies( @@ -511,7 +529,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : } $customerWasChanged = self::isCorporateOrder($wcOrder) != self::isCorporateCrmOrder($crmOrder); - $synchronizableUserData = self::isCorporateCrmOrder($crmOrder) + $synchronizableUserData = (self::isCorporateCrmOrder($crmOrder) && isset($crmOrder['contact'])) ? $crmOrder['contact'] : $crmOrder['customer']; if (!$customerWasChanged) { @@ -523,11 +541,13 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : } } - if (isset($synchronizableUserData['externalId']) + if ( + isset($synchronizableUserData['externalId']) && $synchronizableUserData['externalId'] != $wcOrder->get_customer_id() ) { $customerWasChanged = true; - } elseif (isset($synchronizableUserData['email']) + } elseif ( + isset($synchronizableUserData['email']) && $synchronizableUserData['email'] != $wcOrder->get_billing_email() ) { $customerWasChanged = true; diff --git a/src/include/class-wc-retailcrm-plugin.php b/src/include/class-wc-retailcrm-plugin.php index 908fe54..6c6268b 100644 --- a/src/include/class-wc-retailcrm-plugin.php +++ b/src/include/class-wc-retailcrm-plugin.php @@ -1,16 +1,17 @@ file = $file; add_filter('cron_schedules', array($this, 'filter_cron_schedules'), 10, 1); } - public function filter_cron_schedules($schedules) { + public function filter_cron_schedules($schedules) + { return array_merge( $schedules, array( @@ -44,16 +52,22 @@ class WC_Retailcrm_Plugin { ); } - public function register_activation_hook() { + public function register_activation_hook() + { register_activation_hook($this->file, array($this, 'activate')); } - public function register_deactivation_hook() { + public function register_deactivation_hook() + { register_deactivation_hook($this->file, array($this, 'deactivate')); } - public function activate() { - if (!class_exists( 'WC_Integration' ) ) { + /** + * @codeCoverageIgnore + */ + public function activate() + { + if (!class_exists('WC_Integration')) { add_action('admin_notices', array(new WC_Integration_Retailcrm(), 'woocommerce_missing_notice')); return; @@ -71,7 +85,8 @@ class WC_Retailcrm_Plugin { $retailcrm_icml->generate(); } - public function deactivate() { + public function deactivate() + { do_action('retailcrm_deactivate'); if (wp_next_scheduled('retailcrm_icml')) { @@ -96,9 +111,10 @@ class WC_Retailcrm_Plugin { * * @return boolean */ - public static function integration_module($api_client, $client_id, $active = true) { + public static function integration_module($api_client, $client_id, $active = true) + { - if (!$api_client) { + if (!$api_client instanceof WC_Retailcrm_Proxy) { return false; } @@ -116,15 +132,7 @@ class WC_Retailcrm_Plugin { $response = $api_client->integrationModulesEdit($configuration); - if (!$response) { - return false; - } - - if (!empty($response) && $response->isSuccessful()) { - return true; - } - - return false; + return !empty($response) && $response->isSuccessful(); } /** @@ -147,7 +155,8 @@ class WC_Retailcrm_Plugin { ? self::clearArray($node) : $node; - if ($result[$index] === '' + if ( + $result[$index] === '' || $result[$index] === null || (is_array($result[$index]) && count($result[$index]) < 1) ) { @@ -158,22 +167,6 @@ class WC_Retailcrm_Plugin { return $result; } - /** - * Returns WC_Customer by id. Returns null if there's no such customer. - * - * @param int $id - * - * @return \WC_Customer|null - */ - public static function getWcCustomerById($id) - { - try { - return new WC_Customer($id); - } catch (\Exception $exception) { - return null; - } - } - /** * Check running history * diff --git a/src/include/class-wc-retailcrm-uploader.php b/src/include/class-wc-retailcrm-uploader.php index 95d86c6..cdf25ca 100644 --- a/src/include/class-wc-retailcrm-uploader.php +++ b/src/include/class-wc-retailcrm-uploader.php @@ -37,7 +37,6 @@ if (class_exists('WC_Retailcrm_Uploader') === false) { */ private $customers; - /** * WC_Retailcrm_Uploader constructor. * @@ -52,7 +51,6 @@ if (class_exists('WC_Retailcrm_Uploader') === false) { $this->customers = $customers; } - /** * Uploads selected order in CRM * @@ -63,16 +61,15 @@ if (class_exists('WC_Retailcrm_Uploader') === false) { { $response = filter_input(INPUT_GET, 'order_ids_retailcrm'); - if (empty($response) === false) { + if (false === empty($response)) { $ids = array_unique(explode(',', $response)); - if (empty($ids) === false) { + if (false === empty($ids)) { $this->uploadArchiveOrders(0, $ids); } } } - /** * Uploads archive order in CRM * @@ -95,8 +92,10 @@ if (class_exists('WC_Retailcrm_Uploader') === false) { $orderId = $dataOrder->ID; $errorMessage = $this->orders->orderCreate($orderId); - if (is_string($errorMessage) === true) { - $errorMessage = empty($errorMessage) === true ? 'Order exist. External id: ' . $orderId : $errorMessage; + if (true === is_string($errorMessage)) { + $errorMessage = empty($errorMessage) === true + ? 'Order exist. External id: ' . $orderId + : $errorMessage; $uploadErrors[$orderId] = $errorMessage; } } @@ -106,7 +105,6 @@ if (class_exists('WC_Retailcrm_Uploader') === false) { return array(); } - /** * Uploads archive customer in CRM * @@ -123,7 +121,7 @@ if (class_exists('WC_Retailcrm_Uploader') === false) { $users = $this->getCmsUsers($page); - if (empty($users) === false) { + if (false === empty($users)) { $dataCustomers = array(); foreach ($users as $user) { @@ -142,7 +140,6 @@ if (class_exists('WC_Retailcrm_Uploader') === false) { return $dataCustomers; } - /** * Return orders ids * @@ -164,7 +161,6 @@ if (class_exists('WC_Retailcrm_Uploader') === false) { ); } - /** * Return count orders * @@ -176,7 +172,7 @@ if (class_exists('WC_Retailcrm_Uploader') === false) { $result = $wpdb->get_results("SELECT COUNT(ID) as `count` FROM $wpdb->posts WHERE post_type = 'shop_order'"); - return empty($result[0]->count) === false ? $result[0]->count : 0; + return empty($result[0]->count) === false ? (int) $result[0]->count : 0; } @@ -217,6 +213,8 @@ if (class_exists('WC_Retailcrm_Uploader') === false) { * @param array $errors Id order - key and message error - value. * * @return void + * + * @codeCoverageIgnore */ private function logOrdersUploadErrors($errors) { diff --git a/src/include/components/class-wc-retailcrm-customer-switcher.php b/src/include/components/class-wc-retailcrm-customer-switcher.php index 03c2297..e1f39c5 100644 --- a/src/include/components/class-wc-retailcrm-customer-switcher.php +++ b/src/include/components/class-wc-retailcrm-customer-switcher.php @@ -110,7 +110,7 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface ); if (isset($newCustomer['externalId'])) { - $wcCustomer = WC_Retailcrm_Plugin::getWcCustomerById($newCustomer['externalId']); + $wcCustomer = new WC_Customer($newCustomer['externalId']); if (!empty($wcCustomer)) { $wcOrder->set_customer_id($wcCustomer->get_id()); diff --git a/src/include/components/class-wc-retailcrm-logger.php b/src/include/components/class-wc-retailcrm-logger.php index cf9e798..4f20630 100644 --- a/src/include/components/class-wc-retailcrm-logger.php +++ b/src/include/components/class-wc-retailcrm-logger.php @@ -10,6 +10,8 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')): /** * Class WC_Retailcrm_Logger + * + * @codeCoverageIgnore */ class WC_Retailcrm_Logger { diff --git a/src/include/customer/class-wc-retailcrm-customer-corporate-address.php b/src/include/customer/class-wc-retailcrm-customer-corporate-address.php index a94f042..5482a82 100644 --- a/src/include/customer/class-wc-retailcrm-customer-corporate-address.php +++ b/src/include/customer/class-wc-retailcrm-customer-corporate-address.php @@ -17,9 +17,6 @@ class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Add /** @var bool $isMain */ protected $isMain = true; - /** @var bool $explicitIsMain */ - protected $explicitIsMain; - /** * @param bool $isMain * @@ -31,17 +28,6 @@ class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Add return $this; } - /** - * @param bool $explicitIsMain - * - * @return WC_Retailcrm_Customer_Corporate_Address - */ - public function setExplicitIsMain($explicitIsMain) - { - $this->explicitIsMain = $explicitIsMain; - return $this; - } - /** * @param WC_Customer $customer * @param \WC_Order|null $order @@ -53,17 +39,13 @@ class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Add $address = $this->getCustomerAddress($customer, $order); if (!empty($address)) { - if ($this->isMain) { - $address['isMain'] = true; - } elseif ($this->explicitIsMain) { - $address['isMain'] = false; - } + $address['isMain'] = $this->isMain; $corporateCustomerAddress = apply_filters( 'retailcrm_process_customer_corporate_address', WC_Retailcrm_Plugin::clearArray(array_merge( $address, - array('isMain' => $address['isMain']) + array('isMain' => $this->isMain) )), $customer ); @@ -73,7 +55,6 @@ class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Add WC_Retailcrm_Logger::add('Error Corporate Customer address is empty'); } - return $this; } } diff --git a/src/include/customer/woocommerce/class-wc-retailcrm-wc-customer-builder.php b/src/include/customer/woocommerce/class-wc-retailcrm-wc-customer-builder.php index 7eddc4e..6d21071 100644 --- a/src/include/customer/woocommerce/class-wc-retailcrm-wc-customer-builder.php +++ b/src/include/customer/woocommerce/class-wc-retailcrm-wc-customer-builder.php @@ -235,3 +235,4 @@ class WC_Retailcrm_WC_Customer_Builder extends WC_Retailcrm_Abstract_Builder return true; } } + diff --git a/src/include/functions.php b/src/include/functions.php index 4127c60..a958091 100644 --- a/src/include/functions.php +++ b/src/include/functions.php @@ -1,10 +1,13 @@ get_shipping_methods(); @@ -70,7 +75,8 @@ function get_wc_shipping_methods() { return apply_filters('retailcrm_shipping_list', WC_Retailcrm_Plugin::clearArray($result)); } -function retailcrm_get_delivery_service($method_id, $instance_id) { +function retailcrm_get_delivery_service($method_id, $instance_id) +{ $shippings_by_zone = get_wc_shipping_methods_by_zones(true); $method = explode(':', $method_id); $method_id = $method[0]; @@ -89,8 +95,10 @@ function retailcrm_get_delivery_service($method_id, $instance_id) { * * @return false|WC_Product|null */ -function retailcrm_get_wc_product($id, $settings) { - if (isset($settings['bind_by_sku']) +function retailcrm_get_wc_product($id, $settings) +{ + if ( + isset($settings['bind_by_sku']) && $settings['bind_by_sku'] == WC_Retailcrm_Base::YES ) { $id = wc_get_product_id_by_sku($id); @@ -104,8 +112,8 @@ function retailcrm_get_wc_product($id, $settings) { * * @return bool */ -function retailcrm_is_debug() { - +function retailcrm_is_debug() +{ $options = get_option(WC_Retailcrm_Base::$option_key); if (isset($options['debug_mode']) === true && $options['debug_mode'] === WC_Retailcrm_Base::YES) { @@ -122,10 +130,10 @@ function is_wplogin() { $ABSPATH_MY = str_replace(array('\\','/'), DIRECTORY_SEPARATOR, ABSPATH); - return ((in_array($ABSPATH_MY.'wp-login.php', get_included_files()) - || in_array($ABSPATH_MY.'wp-register.php', get_included_files())) + return ( + (in_array($ABSPATH_MY . 'wp-login.php', get_included_files()) + || in_array($ABSPATH_MY . 'wp-register.php', get_included_files())) || (isset($_GLOBALS['pagenow']) && $GLOBALS['pagenow'] === 'wp-login.php') - || $_SERVER['PHP_SELF']== '/wp-login.php' + || $_SERVER['PHP_SELF'] == '/wp-login.php' ); } - 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 7364b8e..2bb7abb 100644 --- a/src/include/models/class-wc-retailcrm-customer-switcher-state.php +++ b/src/include/models/class-wc-retailcrm-customer-switcher-state.php @@ -176,3 +176,4 @@ class WC_Retailcrm_Customer_Switcher_State } } } + diff --git a/src/include/order/class-wc-retailcrm-order.php b/src/include/order/class-wc-retailcrm-order.php index 8af5530..4773aaa 100644 --- a/src/include/order/class-wc-retailcrm-order.php +++ b/src/include/order/class-wc-retailcrm-order.php @@ -14,8 +14,6 @@ class WC_Retailcrm_Order extends WC_Retailcrm_Abstracts_Data /** @var bool */ public $is_new = true; - protected $filter_name = 'order'; - protected $data = array( 'externalId' => 0, 'status' => '', @@ -74,7 +72,7 @@ class WC_Retailcrm_Order extends WC_Retailcrm_Abstracts_Data 'countryIso' => $order->get_shipping_country() ); - if ($data['countryIso'] == '--') { + if ($data['countryIso'] == '--' || empty($data['countryIso'])) { $countries = new WC_Countries(); $data['countryIso'] = $countries->get_base_country(); } @@ -89,19 +87,6 @@ class WC_Retailcrm_Order extends WC_Retailcrm_Abstracts_Data return $this; } - /** - * @param WC_Order $order - */ - protected function set_payment_data($order) - { - if ($order->get_payment_method() && isset($this->settings[$order->get_payment_method()])) { - $this->set_data_field('paymentType', $this->settings[$order->get_payment_method()]); - } - - if ($order->is_paid()) { - $this->set_data_field('paymentStatus', 'paid'); - } - } /** * @param WC_Order $order diff --git a/src/readme.txt b/src/readme.txt index 31dbaf3..3591402 100644 --- a/src/readme.txt +++ b/src/readme.txt @@ -2,10 +2,10 @@ Contributors: Simla.com Donate link: https://www.simla.com Tags: Интеграция, Simla.com, simla -Requires PHP: 5.3 +Requires PHP: 5.6 Requires at least: 5.3 Tested up to: 5.8 -Stable tag: 4.3.8 +Stable tag: 4.4.0 License: GPLv1 or later License URI: http://www.gnu.org/licenses/gpl-1.0.html @@ -82,6 +82,12 @@ Asegúrate de tener una clave API específica para cada tienda. Las siguientes i == Changelog == += 4.4.0 = +* Migrating to PHP 5.6. We tested the module, improved performance, security and test coverage. +* Add validate countryIso. Fix bug with duplicate customer address +* Fix bugs in history +* Fix PHP warning and deprecated +* Add documentation for module = 4.3.8 = * Updated logic work address diff --git a/src/retailcrm.php b/src/retailcrm.php index 4103372..5e31abb 100644 --- a/src/retailcrm.php +++ b/src/retailcrm.php @@ -5,10 +5,10 @@ * Description: Integration plugin for WooCommerce & Simla.com * Author: RetailDriver LLC * Author URI: http://retailcrm.pro/ - * Version: 4.3.8 + * Version: 4.4.0 * Tested up to: 5.8 - * WC requires at least: 3.0 - * WC tested up to: 5.4 + * WC requires at least: 5.4 + * WC tested up to: 5.8 * Text Domain: retailcrm */ @@ -20,6 +20,8 @@ if (!class_exists( 'WC_Integration_Retailcrm')) : /** * Class WC_Integration_Retailcrm + * + * @codeCoverageIgnore */ class WC_Integration_Retailcrm { const WOOCOMMERCE_SLUG = 'woocommerce'; @@ -125,7 +127,6 @@ if (!class_exists( 'WC_Integration_Retailcrm')) : require_once(self::checkCustomFile('include/class-wc-retailcrm-base.php')); require_once(self::checkCustomFile('include/class-wc-retailcrm-uploader.php')); require_once(self::checkCustomFile('include/functions.php')); - } /** diff --git a/src/uninstall.php b/src/uninstall.php index 8ca4d3e..74ab04c 100644 --- a/src/uninstall.php +++ b/src/uninstall.php @@ -16,11 +16,12 @@ * * @link https://wordpress.org/plugins/woo-retailcrm/ * - * @version 4.3.8 + * @version 4.4.0 * * @package RetailCRM */ +// @codeCoverageIgnoreStart if (!defined('ABSPATH')) { exit; // Exit if accessed directly } @@ -45,3 +46,4 @@ $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'retailcrm_client_i // Clear any cached data that has been removed wp_cache_flush(); +// @codeCoverageIgnoreEnd diff --git a/tests/abstracts/test-wc-retailcrm-abstract-builder.php b/tests/abstracts/test-wc-retailcrm-abstract-builder.php new file mode 100644 index 0000000..1f31662 --- /dev/null +++ b/tests/abstracts/test-wc-retailcrm-abstract-builder.php @@ -0,0 +1,52 @@ +base = new WC_Retailcrm_Base(); + + parent::setUp(); + } + + /** + * @param $checkout + * + * @dataProvider dataProviderAssistant + */ + public function test_validate_online_assistant_field($checkout) + { + $_POST['woocommerce_integration-retailcrm_online_assistant'] = $checkout; + + $onlineAssistant = $this->base->validate_online_assistant_field('', ''); + + $this->assertInternalType('string', $onlineAssistant); + + if (is_string($checkout)) { + $this->assertEquals('jscode', $onlineAssistant); + } else { + $this->assertEquals('', $onlineAssistant); + } + } + + + public function dataProviderAssistant() + { + return array( + array( + 'checkout' => 'js\code' + ), + array( + 'checkout' => null + ), + array( + 'checkout' => array() + ) + ); + } +} diff --git a/tests/customer/test-wc-retailcrm-customer-corporate-address.php b/tests/customer/test-wc-retailcrm-customer-corporate-address.php index 9fe51ab..6bd475a 100644 --- a/tests/customer/test-wc-retailcrm-customer-corporate-address.php +++ b/tests/customer/test-wc-retailcrm-customer-corporate-address.php @@ -32,7 +32,6 @@ class WC_Retailcrm_Customer_Corporate_Address_Test extends WC_Retailcrm_Test_Cas $customer_address = new WC_Retailcrm_Customer_Corporate_Address(); $data = $customer_address ->setIsMain(true) - ->setExplicitIsMain(false) ->build($this->customer) ->get_data(); @@ -69,7 +68,6 @@ class WC_Retailcrm_Customer_Corporate_Address_Test extends WC_Retailcrm_Test_Cas $customer_address = new WC_Retailcrm_Customer_Corporate_Address(); $data = $customer_address ->setIsMain(false) - ->setExplicitIsMain(true) ->build($this->customer) ->get_data(); @@ -93,7 +91,6 @@ class WC_Retailcrm_Customer_Corporate_Address_Test extends WC_Retailcrm_Test_Cas $customer_address = new WC_Retailcrm_Customer_Corporate_Address(); $data = $customer_address ->setIsMain(false) - ->setExplicitIsMain(true) ->build(null) ->get_data(); diff --git a/tests/customer/woocommerce/test-wc-retailcrm-wc-customer-builder.php b/tests/customer/woocommerce/test-wc-retailcrm-wc-customer-builder.php index 55bc258..d27c7a6 100644 --- a/tests/customer/woocommerce/test-wc-retailcrm-wc-customer-builder.php +++ b/tests/customer/woocommerce/test-wc-retailcrm-wc-customer-builder.php @@ -11,13 +11,29 @@ class WC_Retailcrm_WC_Customer_Builder_Test extends WC_Retailcrm_Test_Case_Helper { + protected $customer; + + public function setUp() + { + $this->customer = new WC_Customer(); + + $this->customer->set_first_name('Tester First Name'); + $this->customer->set_last_name('Tester Last Name'); + $this->customer->set_email(uniqid(md5(date('Y-m-d H:i:s'))) . '@mail.com'); + $this->customer->set_password('password'); + $this->customer->set_billing_phone('89000000000'); + $this->customer->set_date_created(date('Y-m-d H:i:s')); + $this->customer->save(); + } + /** * @expectedException \RuntimeException */ public function test_empty() { $builder = new WC_Retailcrm_WC_Customer_Builder(); - $builder->build(); + + $this->assertEmpty($builder->build()->getResult()); } /** @@ -26,7 +42,8 @@ class WC_Retailcrm_WC_Customer_Builder_Test extends WC_Retailcrm_Test_Case_Helpe public function test_empty_array() { $builder = new WC_Retailcrm_WC_Customer_Builder(); - $builder->setData(array())->build(); + + $this->assertEmpty($builder->setData(array())->build()->getResult()); } /** @@ -35,13 +52,14 @@ class WC_Retailcrm_WC_Customer_Builder_Test extends WC_Retailcrm_Test_Case_Helpe public function test_not_array() { $builder = new WC_Retailcrm_WC_Customer_Builder(); - $builder->setData(new stdClass())->build(); + + $this->assertEquals('test', $builder->setData('test')->build()->getResult()); } /** * @dataProvider customerData * - * @param array $customerData + * @param $customerData */ public function test_build($customerData) { @@ -87,6 +105,133 @@ class WC_Retailcrm_WC_Customer_Builder_Test extends WC_Retailcrm_Test_Case_Helpe } } + public function test_set_field() + { + $builder = new WC_Retailcrm_WC_Customer_Builder(); + $customerData = $this->customerData()[1]['customer']; + $wcCustomer = $builder + ->setFirstName($customerData['firstName']) + ->setLastName($customerData['lastName']) + ->setEmail($customerData['email']) + ->setExternalId($customerData['externalId']) + ->setPhones($customerData['phones']) + ->setAddress($customerData['address']) + ->build() + ->getResult(); + + $this->assertInstanceOf('\WC_Customer', $wcCustomer); + $this->assertEquals($customerData['firstName'], $wcCustomer->get_first_name()); + $this->assertEquals($customerData['lastName'], $wcCustomer->get_last_name()); + $this->assertEquals($customerData['email'], $wcCustomer->get_billing_email()); + $this->assertEquals($customerData['phones'][0]['number'], $wcCustomer->get_billing_phone()); + + $address = $customerData['address']; + + $this->assertEquals($address['region'], $wcCustomer->get_billing_state()); + $this->assertEquals($address['index'], $wcCustomer->get_billing_postcode()); + $this->assertEquals($address['country'], $wcCustomer->get_billing_country()); + $this->assertEquals($address['city'], $wcCustomer->get_billing_city()); + } + + public function test_phone_string() + { + $builder = new WC_Retailcrm_WC_Customer_Builder(); + + $customerData = $this->customerData()[1]['customer']; + $customerData['phones'] = '123454567'; + + $wcCustomer = $builder->setData($customerData)->build()->getResult(); + + $this->assertInstanceOf('\WC_Customer', $wcCustomer); + $this->assertEquals('123454567', $wcCustomer->get_billing_phone()); + } + + public function test_set_phone_string() + { + $builder = new WC_Retailcrm_WC_Customer_Builder(); + $customerData = $this->customerData()[1]['customer']; + $wcCustomer = $builder->setData($customerData)->setPhones('123456789')->build()->getResult(); + + $this->assertInstanceOf('\WC_Customer', $wcCustomer); + $this->assertEquals($customerData['phones'][0]['number'], $wcCustomer->get_billing_phone()); + } + + public function test_set_address_empty() + { + $builder = new WC_Retailcrm_WC_Customer_Builder(); + $customerData = $this->customerData()[1]['customer']; + + $wcCustomer = $builder->setData($customerData)->setAddress(array())->build()->getResult(); + + $this->assertInstanceOf('\WC_Customer', $wcCustomer); + + $this->assertEmpty($wcCustomer->get_billing_state()); + $this->assertEmpty($wcCustomer->get_billing_postcode()); + $this->assertEmpty($wcCustomer->get_billing_country()); + $this->assertEmpty($wcCustomer->get_billing_city()); + } + + public function test_set_wc_customer() + { + $builder = new WC_Retailcrm_WC_Customer_Builder(); + $wcCustomer = $builder->setWcCustomer($this->customer)->getResult(); + + $this->assertInstanceOf('\WC_Customer', $wcCustomer); + $this->assertEquals($this->customer->get_id(), $wcCustomer->get_id()); + $this->assertEquals($this->customer->get_first_name(), $wcCustomer->get_first_name()); + $this->assertEquals($this->customer->get_last_name(), $wcCustomer->get_last_name()); + $this->assertEquals($this->customer->get_billing_phone(), $wcCustomer->get_billing_phone()); + $this->assertEquals($this->customer->get_email(), $wcCustomer->get_email()); + } + + public function test_set_not_wc_customer() + { + $builder = new WC_Retailcrm_WC_Customer_Builder(); + $wcCustomer = $builder->setWcCustomer(null)->getResult(); + + $this->assertInstanceOf('\WC_Customer', $wcCustomer); + $this->assertEquals(null, $wcCustomer->get_id()); + $this->assertEquals(null, $wcCustomer->get_first_name()); + $this->assertEquals(null, $wcCustomer->get_last_name()); + $this->assertEquals(null, $wcCustomer->get_billing_phone()); + $this->assertEquals(null, $wcCustomer->get_email()); + } + + public function test_load_wc_customer_by_id() + { + $builder = new WC_Retailcrm_WC_Customer_Builder(); + $isValidExternalId = $builder->loadExternalId($this->customer->get_id()); + $wcCustomer = $builder->getResult(); + + $this->assertInstanceOf('\WC_Customer', $wcCustomer); + $this->assertEquals(true, $isValidExternalId); + $this->assertEquals($this->customer->get_id(), $wcCustomer->get_id()); + $this->assertEquals($this->customer->get_first_name(), $wcCustomer->get_first_name()); + $this->assertEquals($this->customer->get_last_name(), $wcCustomer->get_last_name()); + $this->assertEquals($this->customer->get_billing_phone(), $wcCustomer->get_billing_phone()); + $this->assertEquals($this->customer->get_email(), $wcCustomer->get_email()); + } + + public function test_load_wc_customer_by_not_valid_id() + { + $builder = new WC_Retailcrm_WC_Customer_Builder(); + + $builder->loadExternalId(null); + + $wcCustomer = $builder->getResult(); + + $this->assertInstanceOf('\WC_Customer', $wcCustomer); + + $this->assertEquals(null, $wcCustomer->get_id()); + $this->assertEquals(null, $wcCustomer->get_first_name()); + $this->assertEquals(null, $wcCustomer->get_last_name()); + $this->assertEquals(null, $wcCustomer->get_billing_phone()); + $this->assertEquals(null, $wcCustomer->get_email()); + } + + /** + * @return array + */ public function customerData() { return array( @@ -121,8 +266,12 @@ class WC_Retailcrm_WC_Customer_Builder_Test extends WC_Retailcrm_Test_Case_Helpe 'personalDiscount' => 0, 'cumulativeDiscount' => 0, 'address' => array( - 'id' => 3132, - 'text' => 'ул. Пушкина дом Колотушкина', + 'id' => 3132, + 'text' => 'street_test', + 'region' => 'region_test', + 'index' => '112233', + 'country' => 'country_test', + 'city' => 'city_test' ), 'segments' => array(), 'firstName' => 'tester001', @@ -135,3 +284,4 @@ class WC_Retailcrm_WC_Customer_Builder_Test extends WC_Retailcrm_Test_Case_Helpe ); } } + diff --git a/tests/datasets/data-base-retailcrm.php b/tests/datasets/data-base-retailcrm.php new file mode 100644 index 0000000..1f9149a --- /dev/null +++ b/tests/datasets/data-base-retailcrm.php @@ -0,0 +1,82 @@ + true, + 'statuses' => array( + array( + 'name' => 'status1', + 'code' => 'status1', + 'active' => true + ), + array( + 'name' => 'status2', + 'code' => 'status2', + 'active' => false + ) + ) + ); + } + + public static function getResponsePaymentTypes() + { + return array( + 'success' => true, + 'paymentTypes' => array( + array( + 'name' => 'payment1', + 'code' => 'payment1', + 'active' => true + ), + array( + 'name' => 'payment2', + 'code' => 'payment2', + 'active' => false + ) + ) + ); + } + + public static function getResponseDeliveryTypes() + { + return array( + 'success' => true, + 'deliveryTypes' => array( + array( + 'name' => 'delivery1', + 'code' => 'delivery1', + 'active' => true + ), + array( + 'name' => 'delivery2', + 'code' => 'delivery2', + 'active' => false + ) + ) + ); + } + + public static function getResponseOrderMethods() + { + return array( + 'success' => true, + 'orderMethods' => array( + array( + 'name' => 'orderMethod1', + 'code' => 'orderMethod1', + 'active' => true + ), + array( + 'name' => 'orderMethod2', + 'code' => 'orderMethod2', + 'active' => false + ) + ) + ); + } +} \ No newline at end of file diff --git a/tests/datasets/data-customers-retailcrm.php b/tests/datasets/data-customers-retailcrm.php new file mode 100644 index 0000000..7b66170 --- /dev/null +++ b/tests/datasets/data-customers-retailcrm.php @@ -0,0 +1,22 @@ + true, + 'addresses' => array ( + 'id' => 3503, + 'index' => 144566, + 'countryIso' => 'ES', + 'region' => 'Region', + 'city' => 'City', + 'text' => 'street Test 777', + ) + ); + } +} + diff --git a/tests/datasets/data-history-retailcrm.php b/tests/datasets/data-history-retailcrm.php new file mode 100644 index 0000000..f55644e --- /dev/null +++ b/tests/datasets/data-history-retailcrm.php @@ -0,0 +1,2168 @@ + true, + 'history' => array(), + 'pagination' => array( + 'limit' => 100, + 'totalCount' => 0, + 'currentPage' => 1, + 'totalPageCount' => 0 + ) + ); + } + + public static function get_history_data_new_customer() + { + return array( + 'success' => true, + 'history' => array( + array ( + 'id' => 18009, + 'createdAt' => '2021-12-03 13:22:45', + 'created' => true, + 'source' => 'user', + 'user' => array('id' => 11), + 'field' => 'id', + 'oldValue' => null, + 'newValue' => 3758, + 'customer' => array ( + 'type' => 'customer', + 'id' => 3758, + 'isContact' => false, + 'createdAt' => '2021-12-03 13:22:45', + 'vip' => false, + 'bad' => false, + 'site' => 'woocomerce', + 'marginSumm' => 0, + 'totalSumm' => 0, + 'averageSumm' => 0, + 'ordersCount' => 0, + 'customFields' => array(), + 'personalDiscount' => 0, + 'cumulativeDiscount' => 0, + 'address' => array ( + 'id' => 3503, + 'index' => 123456, + 'countryIso' => 'ES', + 'region' => 'Region', + 'city' => 'City', + 'text' => 'street Test 777', + ), + 'segments' => array(), + 'firstName' => 'Test_Name', + 'lastName' => 'Test', + 'email' => 'mail_test@mail.es', + 'phones' => array('0' => array('number' => '+79184563200')), + 'birthday' => '2021-10-01' + ) + ) + ), + 'pagination' => array( + 'limit' => 100, + 'totalCount' => 1, + 'currentPage' => 1, + 'totalPageCount' => 1 + ) + ); + } + + public static function get_history_data_new_order($product_create_id) + { + return array( + 'success' => true, + 'history' => array( + array( + 'id' => 1, + 'createdAt' => '2018-01-01 00:00:00', + 'created' => true, + 'source' => 'user', + 'user' => array( + 'id' => 1 + ), + 'field' => 'status', + 'oldValue' => null, + 'newValue' => array( + 'code' => 'status1' + ), + 'order' => array( + 'slug' => 1, + 'id' => 1, + 'number' => '1C', + 'orderType' => 'eshop-individual', + 'orderMethod' => 'phone', + 'countryIso' => 'RU', + 'createdAt' => '2018-01-01 00:00:00', + 'statusUpdatedAt' => '2018-01-01 00:00:00', + 'summ' => 100, + 'totalSumm' => 100, + 'prepaySum' => 0, + 'purchaseSumm' => 50, + 'markDatetime' => '2018-01-01 00:00:00', + 'firstName' => 'Test', + 'lastName' => 'Test', + 'phone' => '80000000000', + 'call' => false, + 'expired' => false, + 'customer' => array( + 'type' => 'customer', + 'segments' => array(), + 'id' => 1, + 'firstName' => 'Test', + 'lastName' => 'Test', + 'email' => 'email@test.ru', + 'phones' => array( + array( + 'number' => '111111111111111' + ), + array( + 'number' => '+7111111111' + ) + ), + 'address' => array( + 'index' => '111111', + 'countryIso' => 'RU', + 'region' => 'Test region', + 'city' => 'Test', + 'text' => 'Test text address' + ), + 'createdAt' => '2018-01-01 00:00:00', + 'managerId' => 1, + 'vip' => false, + 'bad' => false, + 'site' => 'test-com', + 'contragent' => array( + 'contragentType' => 'individual' + ), + 'personalDiscount' => 0, + 'cumulativeDiscount' => 0, + 'marginSumm' => 58654, + 'totalSumm' => 61549, + 'averageSumm' => 15387.25, + 'ordersCount' => 4, + 'costSumm' => 101, + 'customFields' => array( + 'custom' => 'test' + ) + ), + 'contragent' => array(), + 'delivery' => array( + 'cost' => 0, + 'netCost' => 0, + 'address' => array( + 'index' => '111111', + 'countryIso' => 'RU', + 'region' => 'Test region', + 'city' => 'Test', + 'text' => 'Test text address' + ) + ), + 'site' => 'test-com', + 'status' => 'status1', + 'items' => array( + array( + 'id' => 160, + 'initialPrice' => 100, + 'discountTotal' => 5, + 'createdAt' => '2018-01-01 00:00:00', + 'quantity' => 1, + 'status' => 'new', + 'externalIds' => array( + array( + 'code' => 'woocomerce', + 'value' => '160_' . $product_create_id + ) + ), + 'initialPrice' => 15, + 'discountTotal' => 1, + 'offer' => array( + 'id' => 1, + 'externalId' => $product_create_id, + 'xmlId' => '1', + 'name' => 'Test name', + 'vatRate' => 'none' + ), + 'properties' => array(), + 'purchasePrice' => 50 + ) + ), + 'paymentType' => 'payment4', + 'payments' => array( + array( + 'id' => 1, + 'type' => 'payment4', + 'amount' => 100, + ) + ), + 'fromApi' => false, + 'length' => 0, + 'width' => 0, + 'height' => 0, + 'shipmentStore' => 'main', + 'shipped' => false, + 'customFields' => array(), + 'uploadedToExternalStoreSystem' => false + ) + ) + ), + 'pagination' => array( + 'limit' => 100, + 'totalCount' => 1, + 'currentPage' => 1, + 'totalPageCount' => 1 + ) + ); + } + + public static function get_history_data_new_order_deleted_items($product_create_id, $product_delete_id) + { + return array( + 'success' => true, + 'history' => array( + array( + 'id' => 1, + 'createdAt' => '2018-01-01 00:00:00', + 'created' => true, + 'source' => 'user', + 'user' => array( + 'id' => 1 + ), + 'field' => 'status', + 'oldValue' => null, + 'newValue' => array( + 'code' => 'status1' + ), + 'order' => array( + 'slug' => 3, + 'id' => 3, + 'number' => '3C', + 'orderType' => 'eshop-individual', + 'orderMethod' => 'phone', + 'countryIso' => 'RU', + 'createdAt' => '2018-01-01 00:00:00', + 'statusUpdatedAt' => '2018-01-01 00:00:00', + 'summ' => 100, + 'totalSumm' => 100, + 'prepaySum' => 0, + 'purchaseSumm' => 50, + 'markDatetime' => '2018-01-01 00:00:00', + 'firstName' => 'Test', + 'lastName' => 'Test', + 'phone' => '80000000000', + 'call' => false, + 'expired' => false, + 'customer' => array( + 'type' => 'customer', + 'segments' => array(), + 'id' => 1, + 'firstName' => 'Test', + 'lastName' => 'Test', + 'email' => 'email@test.ru', + 'phones' => array( + array( + 'number' => '111111111111111' + ), + array( + 'number' => '+7111111111' + ) + ), + 'address' => array( + 'index' => '111111', + 'countryIso' => 'RU', + 'region' => 'Test region', + 'city' => 'Test', + 'text' => 'Test text address' + ), + 'createdAt' => '2018-01-01 00:00:00', + 'managerId' => 1, + 'vip' => false, + 'bad' => false, + 'site' => 'test-com', + 'contragent' => array( + 'contragentType' => 'individual' + ), + 'personalDiscount' => 0, + 'cumulativeDiscount' => 0, + 'marginSumm' => 58654, + 'totalSumm' => 61549, + 'averageSumm' => 15387.25, + 'ordersCount' => 4, + 'costSumm' => 101, + 'customFields' => array( + 'custom' => 'test' + ) + ), + 'contragent' => array(), + 'delivery' => array( + 'cost' => 0, + 'netCost' => 0, + 'address' => array( + 'index' => '111111', + 'countryIso' => 'RU', + 'region' => 'Test region', + 'city' => 'Test', + 'text' => 'Test text address' + ) + ), + 'site' => 'test-com', + 'status' => 'status1', + 'items' => array( + array( + 'id' => 160, + 'initialPrice' => 15, + 'discountTotal' => 1, + 'createdAt' => '2018-01-01 00:00:00', + 'quantity' => 1, + 'status' => 'new', + 'externalIds' => array( + array( + 'code' => 'woocomerce', + 'value' => '160_' . $product_create_id + ) + ), + 'offer' => array( + 'id' => 1, + 'externalId' => $product_create_id, + 'xmlId' => '1', + 'name' => 'Test name', + 'vatRate' => 'none' + ), + 'properties' => array(), + 'purchasePrice' => 10 + ), + array( + 'id' => 161, + 'initialPrice' => 100, + 'discountTotal' => 5, + 'createdAt' => '2018-01-01 00:00:00', + 'quantity' => 1, + 'status' => 'new', + 'externalIds' => array( + array( + 'code' => 'woocomerce', + 'value' => '161_' . $product_delete_id + ) + ), + 'offer' => array( + 'id' => 2, + 'externalId' => $product_delete_id, + 'xmlId' => '2', + 'name' => 'Test name 2', + 'vatRate' => 'none' + ), + 'properties' => array(), + 'purchasePrice' => 50 + ) + ), + 'paymentType' => 'payment4', + 'payments' => array( + array( + 'id' => 1, + 'type' => 'payment4', + 'amount' => 100, + ) + ), + 'fromApi' => false, + 'length' => 0, + 'width' => 0, + 'height' => 0, + 'shipmentStore' => 'main', + 'shipped' => false, + 'customFields' => array(), + 'uploadedToExternalStoreSystem' => false + ) + ), + array( + 'id' => 2, + 'createdAt' => '2018-01-01 00:01:00', + 'source' => 'api', + 'field' => 'order_product', + 'oldValue' => array( + 'id' => 161, + 'offer' => array( + 'id' => 2, + 'externalId' => $product_delete_id + ) + ), + 'newValue' => null, + 'order' => array( + 'id' => 3, + 'site' => 'test-com', + 'status' => 'status1' + ), + 'item' => array( + 'id' => 161, + 'initialPrice' => 100, + 'discountTotal' => 5, + 'createdAt' => '2018-01-01 00:00:00', + 'quantity' => 1, + 'status' => 'new', + 'externalIds' => array( + array( + 'code' => 'woocomerce', + 'value' => '161_' . $product_delete_id + ) + ), + 'offer' => array( + 'id' => 2, + 'externalId' => $product_delete_id, + 'xmlId' => '2', + 'name' => 'Test name 2', + 'vatRate' => 'none' + ), + 'properties' => array(), + 'purchasePrice' => 50 + ) + ) + ), + 'pagination' => array( + 'limit' => 100, + 'totalCount' => 2, + 'currentPage' => 1, + 'totalPageCount' => 1 + ) + ); + } + + public static function get_history_data_product_add($product_add_id, $order_id) + { + return array( + 'success' => true, + 'history' => array( + array( + 'id' => 2, + 'createdAt' => '2018-01-01 00:00:01', + 'source' => 'user', + 'user' => array( + 'id' => 1 + ), + 'field' => 'order_product', + 'oldValue' => null, + 'newValue' => array( + 'id' => 2, + 'offer' => array( + 'id' => 2, + 'externalId' => $product_add_id, + 'xmlId' => 'xmlId' + ) + ), + 'order' => array( + 'id' => 2, + 'externalId' => $order_id, + 'site' => 'test-com', + 'status' => 'status1' + ), + 'item' => array( + 'id' => 2, + 'initialPrice' => 999, + 'createdAt' => '2018-01-01 00:02:00', + 'quantity' => 2, + 'status' => 'status1', + 'externalIds' => array( + array( + 'code' => 'woocomerce', + 'value' => '160_' . $product_add_id + ) + ), + 'offer' => array( + 'id' => 2, + 'externalId' => $product_add_id, + 'xmlId' => 'xmlId', + 'name' => 'Test name 2' + ), + 'properties' => array(), + 'purchasePrice' => 500 + ) + ) + ), + 'pagination' => array( + 'limit' => 100, + 'totalCount' => 1, + 'currentPage' => 1, + 'totalPageCount' => 1 + ) + ); + } + + public static function get_history_data_update($order_id) + { + $history = array( + 'success' => true, + 'history' => array( + array( + 'id' => 3, + 'createdAt' => '2018-01-01 00:03:00', + 'source' => 'user', + 'user' => array( + 'id' => 1 + ), + 'field' => 'status', + 'oldValue' => array( + 'code' => 'new' + ), + 'newValue' => array( + 'code' => 'status4' + ), + 'order' => array( + 'id' => 2, + 'externalId' => $order_id, + 'managerId' => 6, + 'site' => 'test-com', + 'status' => 'status4' + ) + ), + array( + 'id' => 4, + 'createdAt' => '2018-01-01 00:03:00', + 'source' => 'user', + 'user' => array( + 'id' => 1 + ), + 'field' => 'managerComment', + 'oldValue' => array( + 'code' => '' + ), + 'newValue' => array( + 'code' => 'managerComment' + ), + 'order' => array( + 'id' => 2, + 'externalId' => $order_id, + 'managerId' => 6, + 'site' => 'test-com', + 'status' => 'status4', + 'managerComment' => 'managerComment' + ) + ), + array( + 'id' => 5, + 'createdAt' => '2018-01-01 00:03:00', + 'source' => 'user', + 'user' => array( + 'id' => 1 + ), + 'field' => 'customerComment', + 'oldValue' => array( + 'code' => '' + ), + 'newValue' => array( + 'code' => 'customerComment' + ), + 'order' => array( + 'id' => 2, + 'externalId' => $order_id, + 'managerId' => 6, + 'site' => 'test-com', + 'status' => 'status4', + 'managerComment' => 'managerComment', + 'customerComment' => 'customerComment' + ) + ), + array( + 'id' => 6, + 'createdAt' => '2018-01-01 00:03:00', + 'source' => 'user', + 'user' => array( + 'id' => 1 + ), + 'field' => 'phone', + 'oldValue' => array( + 'code' => '' + ), + 'newValue' => array( + 'code' => '12345678' + ), + 'order' => array( + 'id' => 2, + 'externalId' => $order_id, + 'managerId' => 6, + 'site' => 'test-com', + 'status' => 'status4', + 'managerComment' => 'managerComment', + 'customerComment' => 'customerComment', + 'phone' => '12345678' + ) + ), + array( + 'id' => 7, + 'createdAt' => '2018-01-01 00:03:00', + 'source' => 'user', + 'user' => array( + 'id' => 1 + ), + 'field' => 'email', + 'oldValue' => array( + 'code' => '' + ), + 'newValue' => array( + 'code' => 'tester001@example.com' + ), + 'order' => array( + 'id' => 2, + 'externalId' => $order_id, + 'managerId' => 6, + 'site' => 'test-com', + 'status' => 'status4', + 'managerComment' => 'managerComment', + 'customerComment' => 'customerComment', + 'phone' => '12345678', + 'email' => 'tester001@example.com' + ) + ) + ), + 'pagination' => array( + 'limit' => 100, + 'totalCount' => 1, + 'currentPage' => 1, + 'totalPageCount' => 1 + ) + ); + + $payment_v5 = array( + 'id' => 4, + 'createdAt' => '2018-01-01 00:03:00', + 'source' => 'user', + 'user' => array( + 'id' => 1 + ), + 'field' => 'payments', + 'oldValue' => null, + 'newValue' => array( + 'code' => 'payment2' + ), + 'order' => array( + 'id' => 2, + 'externalId' => $order_id, + 'managerId' => 6, + 'site' => 'test-com', + 'status' => 'status4' + ), + 'payment' => array( + 'id' => 1, + 'type' => 'payment2', + 'amount' => 100 + ) + ); + + array_push($history['history'], $payment_v5); + + return $history; + } + + public static 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, + 'discountTotal' => 5, + 'createdAt' => '2018-01-01 00:00:00', + 'quantity' => 1, + 'status' => 'new', + 'externalIds' => array( + array( + 'code' => 'woocomerce', + 'value' => '160_' . $productId + ) + ), + 'initialPrice' => 15, + 'discountTotal' => 1, + '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, + ) + ); + } + + public static 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, + ), + ); + } + + public static 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, + ), + ); + } + + public static 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, + ), + ); + } + + public static 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, + ), + ); + } + + public static 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, + ), + ); + } + + public static 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, + ), + ); + } + + public static 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', + ), + ), + ); + } + + public static 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', + ), + ), + ); + } + + public static 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' => 5, + '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['order']['company'] = $company; + } + + return $order; + } + + public static 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', + ), + ), + ); + } + + public static 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', + ), + ); + } + + public static 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', + ), + ), + ); + } + + public static 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', + ), + ); + } + + public static 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/datasets/data-inventories-retailcrm.php b/tests/datasets/data-inventories-retailcrm.php new file mode 100644 index 0000000..2345ec8 --- /dev/null +++ b/tests/datasets/data-inventories-retailcrm.php @@ -0,0 +1,25 @@ + true, + 'pagination' => array( + 'limit' => 250, + 'totalCount' => 1, + 'currentPage' => 1, + 'totalPageCount' => 1 + ), + 'offers' => array( + array( + 'id' => 1, + 'xmlId' => 'xmlId', + 'quantity' => 10 + ) + ) + ); + } +} \ No newline at end of file diff --git a/tests/helpers/class-wc-retailcrm-test-case-helper.php b/tests/helpers/class-wc-retailcrm-test-case-helper.php index 53a51a1..e48539f 100644 --- a/tests/helpers/class-wc-retailcrm-test-case-helper.php +++ b/tests/helpers/class-wc-retailcrm-test-case-helper.php @@ -12,14 +12,15 @@ class WC_Retailcrm_Test_Case_Helper extends WC_Unit_Test_Case { $options = array( 'api_url' => 'https://example.retailcrm.ru', - 'api_key' => 'dhsHJGYdjkHHJKJSGjhasjhgajsgJGHsg', - 'api_version' => 'v5', + 'api_key' => 'test_key', + 'corporate_enabled' => 'yes', + 'online_assistant' => 'code', 'p_draft' => 'no', 'p_pending' => 'no', 'p_private' => 'no', 'p_publish' => 'no', - 'send_payment_amount' => 'yes', - 'order_methods' => '', + 'catalog_options' => 'no', + 'order_methods' => array(0 => 'phone'), 'flat_rate_shipping' => 'delivery', 'free_shipping' => 'delivery2', 'local_pickup' => 'delivery3', @@ -35,11 +36,23 @@ class WC_Retailcrm_Test_Case_Helper extends WC_Unit_Test_Case 'cancelled' => 'status5', 'refunded' => 'status6', 'failed' => 'status7', - 'sync' => 'no', + 'sync' => 'yes', 'ua' => 'yes', 'ua_code' => 'UA-XXXXXXX-XX', 'ua_custom' => '1', - 'upload-button' => '' + 'daemon_collector' => 'yes', + 'upload-button' => '', + 'whatsapp_active' => 'yes', + 'whatsapp_location_icon' => 'yes', + 'whatsapp_number' => '+79184567234', + 'icml' => 'yes', + 'single_order' => '123', + 'history' => 'yes', + 'deactivate_update_order' => 'no', + 'bind_by_sku' => 'no', + 'update_number' => 'yes', + 'debug_mode' => 'yes', + 'debug-info' => '' ); update_option(WC_Retailcrm_Base::$option_key, $options); @@ -91,4 +104,19 @@ class WC_Retailcrm_Test_Case_Helper extends WC_Unit_Test_Case { return get_option(WC_Retailcrm_Base::$option_key); } + + + /** + * @param $mock + * @param $method + * @param $response + * + * @return void + */ + protected function setMockResponse($mock, $method, $response) + { + $mock->expects($this->any()) + ->method($method) + ->willReturn($response); + } } diff --git a/tests/models/test-wc-retailcrm-customer-switcher-result.php b/tests/models/test-wc-retailcrm-customer-switcher-result.php index 5ded62d..252fdd8 100644 --- a/tests/models/test-wc-retailcrm-customer-switcher-result.php +++ b/tests/models/test-wc-retailcrm-customer-switcher-result.php @@ -11,6 +11,21 @@ class WC_Retailcrm_Customer_Switcher_Result_Test extends WC_Retailcrm_Test_Case_Helper { + protected $customer; + + public function setUp() + { + $this->customer = new WC_Customer(); + + $this->customer->set_first_name('Tester'); + $this->customer->set_last_name('Tester'); + $this->customer->set_email( uniqid(md5(date('Y-m-d H:i:s'))) . '@mail.com'); + $this->customer->set_password('password'); + $this->customer->set_billing_phone('89000000000'); + $this->customer->set_date_created(date('Y-m-d H:i:s')); + $this->customer->save(); + } + /** * @expectedException \InvalidArgumentException */ @@ -37,7 +52,8 @@ class WC_Retailcrm_Customer_Switcher_Result_Test extends WC_Retailcrm_Test_Case_ public function test_valid() { - $result = new WC_Retailcrm_Customer_Switcher_Result(new WC_Customer(), new WC_Order()); + $result = new WC_Retailcrm_Customer_Switcher_Result($this->customer, new WC_Order()); + $this->assertInstanceOf('\WC_Customer', $result->getWcCustomer()); $this->assertInstanceOf('\WC_Order', $result->getWcOrder()); } @@ -45,7 +61,18 @@ class WC_Retailcrm_Customer_Switcher_Result_Test extends WC_Retailcrm_Test_Case_ 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', $result->getWcOrder()); + + $this->assertEmpty($result->getWcCustomer()); + $this->assertInstanceOf('\WC_Order', $result->getWcOrder()); + } + + + public function test_save() + { + $switcher = new WC_Retailcrm_Customer_Switcher_Result($this->customer, new WC_Order()); + + $switcher->save(); + $this->assertInstanceOf('\WC_Customer', $switcher->getWcCustomer()); } } + diff --git a/tests/customer/woocommerce/test-wc-retailcrm-customer-switcher-state.php b/tests/models/test-wc-retailcrm-customer-switcher-state.php similarity index 92% rename from tests/customer/woocommerce/test-wc-retailcrm-customer-switcher-state.php rename to tests/models/test-wc-retailcrm-customer-switcher-state.php index d4fb5ba..b1808c3 100644 --- a/tests/customer/woocommerce/test-wc-retailcrm-customer-switcher-state.php +++ b/tests/models/test-wc-retailcrm-customer-switcher-state.php @@ -14,6 +14,7 @@ class WC_Retailcrm_Customer_Switcher_State_Test extends WC_Retailcrm_Test_Case_H public function test_feasible() { $state = new WC_Retailcrm_Customer_Switcher_State(); + $this->assertFalse($state->feasible()); $state->setNewCustomer(array()) @@ -35,7 +36,7 @@ class WC_Retailcrm_Customer_Switcher_State_Test extends WC_Retailcrm_Test_Case_H $state->setNewCustomer(array()) ->setNewContact(array()) - ->setNewCompany(array('name' => 'test')); + ->setNewCompany(array('name' => 'test', 'address' => 'address_test')); $this->assertTrue($state->feasible()); $state->setNewCustomer(array()) @@ -50,6 +51,7 @@ class WC_Retailcrm_Customer_Switcher_State_Test extends WC_Retailcrm_Test_Case_H public function test_validate_empty() { $state = new WC_Retailcrm_Customer_Switcher_State(); + $state->validate(); } @@ -59,8 +61,8 @@ class WC_Retailcrm_Customer_Switcher_State_Test extends WC_Retailcrm_Test_Case_H public function test_validate_order() { $state = new WC_Retailcrm_Customer_Switcher_State(); - $state->setWcOrder(new WC_Order()) - ->validate(); + + $state->setWcOrder(new WC_Order())->validate(); } /** @@ -69,6 +71,7 @@ class WC_Retailcrm_Customer_Switcher_State_Test extends WC_Retailcrm_Test_Case_H public function test_validate_customer_and_contact_set() { $state = new WC_Retailcrm_Customer_Switcher_State(); + $state->setWcOrder(new WC_Order()) ->setNewCustomer(array('id' => 1)) ->setNewContact(array('id' => 1)) @@ -76,13 +79,15 @@ class WC_Retailcrm_Customer_Switcher_State_Test extends WC_Retailcrm_Test_Case_H } /** - * @@doesNotPerformAssertions + * @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/order/test-wc-retailcrm-order-item.php b/tests/order/test-wc-retailcrm-order-item.php index 5c6a523..b9406bb 100644 --- a/tests/order/test-wc-retailcrm-order-item.php +++ b/tests/order/test-wc-retailcrm-order-item.php @@ -35,4 +35,16 @@ class WC_Retailcrm_Order_Item_Test extends WC_Retailcrm_Test_Case_Helper $this->assertArrayHasKey('offer', $data); } } + + public function test_bind_by_sku() + { + $order_item = new WC_Retailcrm_Order_Item(['bind_by_sku' => 'yes']); + + foreach ($this->order->get_items() as $item) { + $data = $order_item->build($item)->get_data(); + + $this->assertArrayHasKey('offer', $data); + $this->assertArrayHasKey('xmlId', $data['offer']); + } + } } diff --git a/tests/order/test-wc-retailcrm-order-payment.php b/tests/order/test-wc-retailcrm-order-payment.php index 5c3f848..a0b0a74 100644 --- a/tests/order/test-wc-retailcrm-order-payment.php +++ b/tests/order/test-wc-retailcrm-order-payment.php @@ -44,6 +44,62 @@ class WC_Retailcrm_Order_Payment_Test extends WC_Retailcrm_Test_Case_Helper $this->assertArrayHasKey('order', $data); } + /** + * @param mixed $externalId + * + * @dataProvider dataProvider + */ + public function test_build_payment_type_not_exist($externalId) + { + $order_payment = new WC_Retailcrm_Order_Payment('test'); + $data = $order_payment->build($this->order, $externalId)->get_data(); + + $this->assertEmpty($data); + } + + + /** + * @param mixed $externalId + * + * @dataProvider dataProvider + */ + public function test_not_new_payment($externalId) + { + $settings = $this->getOptions(); + $order_payment = new WC_Retailcrm_Order_Payment($settings); + $order_payment->is_new = false; + + $data = $order_payment->build($this->order, $externalId)->get_data(); + + $this->assertEmpty($data); + } + + + /** + * @param mixed $externalId + * + * @dataProvider dataProvider + */ + public function test_order_paid($externalId) + { + $settings = $this->getOptions(); + $order_payment = new WC_Retailcrm_Order_Payment($settings); + + $this->order->update_status('completed'); + + $data = $order_payment->build($this->order, $externalId)->get_data(); + + $this->assertNotEmpty($data); + + if (!empty($externalId)) { + $this->assertArrayHasKey('externalId', $data); + } + + $this->assertArrayHasKey('type', $data); + $this->assertArrayHasKey('order', $data); + } + + /** * @param mixed $externalId * diff --git a/tests/order/test-wc-retailcrm-order.php b/tests/order/test-wc-retailcrm-order.php new file mode 100644 index 0000000..a38ca72 --- /dev/null +++ b/tests/order/test-wc-retailcrm-order.php @@ -0,0 +1,60 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +class WC_Retailcrm_Order_Test extends WC_Retailcrm_Test_Case_Helper { + /** @var WC_Order */ + protected $order; + + public function setUp() + { + parent::setUp(); + + $this->order = WC_Helper_Order::create_order(); + } + + public function test_reset_data() + { + $buildOrder = new WC_Retailcrm_Order($this->getOptions()); + $data = $buildOrder->build($this->order)->get_data(); + + $this->assertNotEmpty($data); + + $buildOrder->reset_data(); + + $this->assertEmpty(array_filter($buildOrder->get_data())); + } + + public function test_empty_shipping_data() + { + $buildOrder = new WC_Retailcrm_Order($this->getOptions()); + $data = $buildOrder->build($this->order)->get_data(); + + $this->assertNotEmpty($data); + $this->assertArrayHasKey('firstName', $data); + $this->assertArrayHasKey('lastName', $data); + $this->assertEquals($this->order->get_billing_first_name(), $data['firstName']); + $this->assertEquals($this->order->get_billing_last_name(), $data['lastName']); + } + + public function test_empty_country_iso() + { + $buildOrder = new WC_Retailcrm_Order($this->getOptions()); + + $this->order->set_shipping_country(''); + + $data = $buildOrder->build($this->order)->get_data(); + + $this->assertNotEmpty($data); + $this->assertArrayHasKey('countryIso', $data); + $this->assertNotEquals('', $data['countryIso']); + } +} diff --git a/tests/test-wc-retailcrm-base.php b/tests/test-wc-retailcrm-base.php index ab02383..35a63ea 100644 --- a/tests/test-wc-retailcrm-base.php +++ b/tests/test-wc-retailcrm-base.php @@ -1,5 +1,7 @@ apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') - ->disableOriginalConstructor() - ->setMethods(array( - 'orderMethodsList', - 'deliveryTypesList', - 'paymentTypesList', - 'statusesList' - )) - ->getMock(); + ->disableOriginalConstructor() + ->setMethods( + array( + 'orderMethodsList', + 'deliveryTypesList', + 'paymentTypesList', + 'statusesList' + ) + ) + ->getMock(); $this->setMockOrderMethods(); $this->setMockDeliveryTypes(); @@ -30,25 +34,25 @@ class WC_Retailcrm_Base_Test extends WC_Retailcrm_Test_Case_Helper $_GET['page'] = 'wc-settings'; $_GET['tab'] = 'integration'; - $this->setOptions('v5'); - $this->unit = new \WC_Retailcrm_Base($this->apiMock); + $this->dataOptions = $this->setOptions(); + $this->baseRetailcrm = new \WC_Retailcrm_Base($this->apiMock); } public function test_retailcrm_form_fields() { - $this->assertInternalType('array', $this->unit->form_fields); - $this->assertArrayHasKey('api_url', $this->unit->form_fields); - $this->assertArrayHasKey('api_key', $this->unit->form_fields); + $this->assertInternalType('array', $this->baseRetailcrm->form_fields); + $this->assertArrayHasKey('api_url', $this->baseRetailcrm->form_fields); + $this->assertArrayHasKey('api_key', $this->baseRetailcrm->form_fields); foreach (get_post_statuses() as $key => $status) { - $this->assertArrayHasKey('p_' . $key, $this->unit->form_fields); + $this->assertArrayHasKey('p_' . $key, $this->baseRetailcrm->form_fields); } - $this->assertArrayHasKey('order_methods', $this->unit->form_fields); + $this->assertArrayHasKey('order_methods', $this->baseRetailcrm->form_fields); foreach (get_wc_shipping_methods() as $code => $value) { if (isset($value['enabled']) && $value['enabled'] == 'yes') { - $this->assertArrayHasKey($code, $this->unit->form_fields); + $this->assertArrayHasKey($code, $this->baseRetailcrm->form_fields); } } @@ -56,165 +60,276 @@ class WC_Retailcrm_Base_Test extends WC_Retailcrm_Test_Case_Helper foreach ($wc_payment->get_available_payment_gateways() as $payment) { if (isset($payment->enabled) && $payment->enabled == 'yes') { - $this->assertArrayHasKey($payment->id, $this->unit->form_fields); + $this->assertArrayHasKey($payment->id, $this->baseRetailcrm->form_fields); } } - foreach (wc_get_order_statuses() as $idx => $name ) { + foreach (wc_get_order_statuses() as $idx => $name) { $uid = str_replace('wc-', '', $idx); - $this->assertArrayHasKey($uid, $this->unit->form_fields); + $this->assertArrayHasKey($uid, $this->baseRetailcrm->form_fields); + } + + $this->assertArrayHasKey('corporate_enabled', $this->baseRetailcrm->form_fields); + $this->assertArrayHasKey('online_assistant', $this->baseRetailcrm->form_fields); + $this->assertArrayHasKey('catalog_options', $this->baseRetailcrm->form_fields); + + $this->assertArrayHasKey('order_methods', $this->baseRetailcrm->form_fields); + $this->assertInternalType('array', $this->baseRetailcrm->form_fields['order_methods']); + + $this->assertArrayHasKey('whatsapp_active', $this->baseRetailcrm->form_fields); + $this->assertArrayHasKey('whatsapp_location_icon', $this->baseRetailcrm->form_fields); + $this->assertArrayHasKey('whatsapp_number', $this->baseRetailcrm->form_fields); + $this->assertArrayHasKey('icml', $this->baseRetailcrm->form_fields); + $this->assertArrayHasKey('single_order', $this->baseRetailcrm->form_fields); + $this->assertArrayHasKey('history', $this->baseRetailcrm->form_fields); + $this->assertArrayHasKey('deactivate_update_order', $this->baseRetailcrm->form_fields); + $this->assertArrayHasKey('bind_by_sku', $this->baseRetailcrm->form_fields); + $this->assertArrayHasKey('update_number', $this->baseRetailcrm->form_fields); + $this->assertArrayHasKey('debug_mode', $this->baseRetailcrm->form_fields); + $this->assertArrayHasKey('debug-info', $this->baseRetailcrm->form_fields); + } + + public function test_retailcrm_form_fields_value() + { + foreach ($this->getOptions() as $key => $value) { + if (is_array($value) === false) { + $this->assertEquals($this->dataOptions[$key], $value); + } else { + $this->assertEquals($this->dataOptions[$key][0], $value[0]); + } } } - private function getResponseOrderMethods() + public function test_initialize_online_assistant() { - return array( - 'success' => true, - 'orderMethods' => array( - array( - 'name' => 'orderMethod1', - 'code' => 'orderMethod1', - 'active' => true - ), - array( - 'name' => 'orderMethod2', - 'code' => 'orderMethod2', - 'active' => false - ) - ) - ); + ob_start(); + $this->baseRetailcrm->initialize_online_assistant($this->dataOptions); + + $this->assertEquals($this->dataOptions['online_assistant'], ob_get_contents()); + ob_end_clean(); } - private function getResponseDeliveryTypes() + public function test_option_cron_enabled() { - return array( - 'success' => true, - 'deliveryTypes' => array( - array( - 'name' => 'delivery1', - 'code' => 'delivery1', - 'active' => true - ), - array( - 'name' => 'delivery2', - 'code' => 'delivery2', - 'active' => true - ) - ) - ); + $this->baseRetailcrm->api_sanitized($this->getOptions()); + + $history = date('H:i:s d-m-Y', wp_next_scheduled('retailcrm_history')); + $icml = date('H:i:s d-m-Y', wp_next_scheduled('retailcrm_icml')); + $inventories = date('H:i:s d-m-Y', wp_next_scheduled('retailcrm_inventories')); + + $this->assertInternalType('string', $history); + $this->assertInternalType('string', $icml); + $this->assertInternalType('string', $inventories); } - private function getResponsePaymentTypes() + public function test_option_cron_disabled() { - return array( - 'success' => true, - 'paymentTypes' => array( - array( - 'name' => 'payment1', - 'code' => 'payment1', - 'active' => true - ), - array( - 'name' => 'payment2', - 'code' => 'payment2', - 'active' => true - ) + $settings = $this->baseRetailcrm->api_sanitized( + array( + 'api_url' => 'https://example.retailcrm.ru', + 'api_key' => 'test_key', + 'corporate_enabled' => 'yes', + 'sync' => 'no', + 'icml' => 'no', + 'history' => 'no', ) ); + + $history = date('H:i:s d-m-Y', wp_next_scheduled('retailcrm_history')); + $icml = date('H:i:s d-m-Y', wp_next_scheduled('retailcrm_icml')); + $inventories = date('H:i:s d-m-Y', wp_next_scheduled('retailcrm_inventories')); + + $this->assertEquals('00:00:00 01-01-1970', $history); + $this->assertEquals('00:00:00 01-01-1970', $icml); + $this->assertEquals('00:00:00 01-01-1970', $inventories); + + $this->assertInternalType('array', $settings); + $this->assertArrayHasKey('api_url', $settings); + $this->assertEquals('https://example.retailcrm.ru', $settings['api_url']); + $this->assertArrayHasKey('api_key', $settings); + $this->assertEquals('test_key', $settings['api_key']); + $this->assertArrayHasKey('corporate_enabled', $settings); + $this->assertEquals('yes', $settings['corporate_enabled']); + $this->assertArrayHasKey('sync', $settings); + $this->assertEquals('no', $settings['sync']); + $this->assertArrayHasKey('icml', $settings); + $this->assertEquals('no', $settings['icml']); + $this->assertArrayHasKey('history', $settings); + $this->assertEquals('no', $settings['history']); } - private function getResponseStatuses() + + public function test_get_cron_info() { - return array( - 'success' => true, - 'statuses' => array( - array( - 'name' => 'status1', - 'code' => 'status1', - 'active' => true - ), - array( - 'name' => 'status2', - 'code' => 'status2', - 'active' => true - ) - ) + ob_start(); + $this->baseRetailcrm->get_cron_info(); + + $cronInfo = $this->getJsonData(ob_get_contents()); + + $this->assertInternalType('array', $cronInfo); + $this->assertArrayHasKey('history', $cronInfo); + $this->assertArrayHasKey('icml', $cronInfo); + $this->assertArrayHasKey('inventories', $cronInfo); + $this->assertInternalType('string', $cronInfo['history']); + $this->assertInternalType('string', $cronInfo['icml']); + $this->assertInternalType('string', $cronInfo['inventories']); + $this->assertNotEquals('This option is disabled', $cronInfo['history']); + $this->assertNotEquals('This option is disabled', $cronInfo['icml']); + $this->assertNotEquals('This option is disabled', $cronInfo['inventories']); + ob_end_clean(); + } + + public function test_get_cron_info_off() + { + $this->baseRetailcrm->settings = array('sync' => 'no', 'icml' => 'no', 'history' => 'no'); + + ob_start(); + $this->baseRetailcrm->get_cron_info(); + + $cronInfo = $this->getJsonData(ob_get_contents()); + + $this->assertInternalType('array', $cronInfo); + $this->assertArrayHasKey('history', $cronInfo); + $this->assertArrayHasKey('icml', $cronInfo); + $this->assertArrayHasKey('inventories', $cronInfo); + $this->assertInternalType('string', $cronInfo['history']); + $this->assertInternalType('string', $cronInfo['icml']); + $this->assertInternalType('string', $cronInfo['inventories']); + $this->assertEquals('This option is disabled', $cronInfo['history']); + $this->assertEquals('This option is disabled', $cronInfo['icml']); + $this->assertEquals('This option is disabled', $cronInfo['inventories']); + ob_end_clean(); + } + + public function test_count_upload_data() + { + ob_start(); + $this->baseRetailcrm->count_upload_data(); + + $uploadInfo = $this->getJsonData(ob_get_contents()); + + $this->assertInternalType('array', $uploadInfo); + $this->assertArrayHasKey('count_orders', $uploadInfo); + $this->assertArrayHasKey('count_users', $uploadInfo); + $this->assertInternalType('integer', $uploadInfo['count_orders']); + $this->assertInternalType('integer', $uploadInfo['count_users']); + ob_end_clean(); + } + + public function test_initialize_whatsapp() + { + ob_start(); + $this->baseRetailcrm->initialize_whatsapp(); + + $js = ob_get_contents(); + + $this->assertNotEquals('', $js); + $this->assertContains('79184567234', $js); + ob_end_clean(); + } + + public function test_initialize_whatsapp_off() + { + $this->baseRetailcrm->settings = array( + 'whatsapp_active' => 'no', + 'whatsapp_location_icon' => 'no', + 'whatsapp_number' => '', ); + + ob_start(); + $this->baseRetailcrm->initialize_whatsapp(); + + $this->assertEquals('', ob_get_contents()); + ob_end_clean(); + } + + public function test_initialize_daemon_collector_off() + { + $this->baseRetailcrm->settings = array('daemon_collector' => 'no', 'daemon_collector_key' => ''); + + ob_start(); + $this->baseRetailcrm->initialize_daemon_collector(); + + $this->assertEquals('', ob_get_contents()); + ob_end_clean(); + } + + public function test_initialize_analytics() + { + ob_start(); + $this->baseRetailcrm->initialize_analytics(); + + $js = ob_get_contents(); + + $this->assertNotEquals('', $js); + $this->assertContains('UA-XXXXXXX-XX', $js); + ob_end_clean(); + } + + public function test_initialize_analytics_off() + { + $this->baseRetailcrm->settings = array('ua' => '', 'ua_code' => '', 'ua_custom' => ''); + + ob_start(); + $this->baseRetailcrm->initialize_analytics(); + + $this->assertEquals('', ob_get_contents()); + ob_end_clean(); + } + + + private function getJsonData($text) + { + preg_match('/{.*}/', $text, $matches); + + return json_decode($matches[0], true); } private function setMockOrderMethods() { $this->responseMockOrderMethods = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); + ->disableOriginalConstructor() + ->setMethods(array('isSuccessful')) + ->getMock(); + $this->setMockResponse($this->responseMockOrderMethods, 'isSuccessful', true); - $this->responseMockOrderMethods->expects($this->any()) - ->method('isSuccessful') - ->willReturn(true); - - $this->responseMockOrderMethods->setResponse($this->getResponseOrderMethods()); - $this->apiMock->expects($this->any()) - ->method('orderMethodsList') - ->willReturn($this->responseMockOrderMethods); + $this->responseMockOrderMethods->setResponse(DataBaseRetailCrm::getResponseOrderMethods()); + $this->setMockResponse($this->apiMock, 'orderMethodsList', $this->responseMockOrderMethods); } private function setMockDeliveryTypes() { $this->responseMockDeliveryTypes = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); + ->disableOriginalConstructor() + ->setMethods(array('isSuccessful')) + ->getMock(); + $this->setMockResponse($this->responseMockDeliveryTypes, 'isSuccessful', true); - $this->responseMockDeliveryTypes->expects($this->any()) - ->method('isSuccessful') - ->willReturn(true); - - $this->responseMockDeliveryTypes->setResponse($this->getResponseDeliveryTypes()); - $this->apiMock->expects($this->any()) - ->method('deliveryTypesList') - ->willReturn($this->responseMockDeliveryTypes); + $this->responseMockDeliveryTypes->setResponse(DataBaseRetailCrm::getResponseDeliveryTypes()); + $this->setMockResponse($this->apiMock, 'deliveryTypesList', $this->responseMockDeliveryTypes); } private function setMockPaymentTypes() { $this->responseMockPaymentTypes = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); + ->disableOriginalConstructor() + ->setMethods(array('isSuccessful')) + ->getMock(); + $this->setMockResponse($this->responseMockPaymentTypes, 'isSuccessful', true); - $this->responseMockPaymentTypes->expects($this->any()) - ->method('isSuccessful') - ->willReturn(true); - - $this->responseMockPaymentTypes->setResponse($this->getResponsePaymentTypes()); - $this->apiMock->expects($this->any()) - ->method('paymentTypesList') - ->willReturn($this->responseMockPaymentTypes); + $this->responseMockPaymentTypes->setResponse(DataBaseRetailCrm::getResponsePaymentTypes()); + $this->setMockResponse($this->apiMock, 'paymentTypesList', $this->responseMockPaymentTypes); } private function setMockStatuses() { $this->responseMockStatuses = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); + ->disableOriginalConstructor() + ->setMethods(array('isSuccessful')) + ->getMock(); + $this->setMockResponse($this->responseMockStatuses, 'isSuccessful', true); - $this->responseMockStatuses->expects($this->any()) - ->method('isSuccessful') - ->willReturn(true); - - $this->responseMockStatuses->setResponse($this->getResponseStatuses()); - $this->apiMock->expects($this->any()) - ->method('statusesList') - ->willReturn($this->responseMockStatuses); + $this->responseMockStatuses->setResponse(DataBaseRetailCrm::getResponseStatuses()); + $this->setMockResponse($this->apiMock, 'statusesList', $this->responseMockStatuses); } } diff --git a/tests/test-wc-retailcrm-customers.php b/tests/test-wc-retailcrm-customers.php index 724450a..2b3d7b9 100644 --- a/tests/test-wc-retailcrm-customers.php +++ b/tests/test-wc-retailcrm-customers.php @@ -1,5 +1,7 @@ responseMock = $this->getMockBuilder('\WC_Retailcrm_Response') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); + $this->responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') + ->disableOriginalConstructor() + ->setMethods(array( + 'isSuccessful', + 'offsetExists' + )) + ->getMock(); + + $this->responseMock->setResponse(array('id' => 1)); $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') - ->disableOriginalConstructor() - ->setMethods(array( - 'ordersGet', - 'ordersCreate', - 'ordersEdit', - 'customersGet', - 'customersCreate', - 'customersEdit' - )) - ->getMock(); + ->disableOriginalConstructor() + ->setMethods(array( + 'ordersGet', + 'ordersCreate', + 'ordersEdit', + 'customersGet', + 'customersCreate', + 'customersEdit', + 'getCorporateEnabled', + 'customersCorporateCreate', + 'customersCorporateAddressesCreate', + 'customersCorporateCompaniesCreate', + 'getSingleSiteForKey', + 'customersCorporateAddresses' + )) + ->getMock(); - $this->responseMock->expects($this->any()) - ->method('isSuccessful') - ->willReturn(true); - - $this->apiMock->expects($this->any()) - ->method('customersCreate') - ->willReturn($this->responseMock); + $this->setMockResponse($this->responseMock, 'isSuccessful', true); + $this->setMockResponse($this->responseMock, 'offsetExists', true); + $this->setMockResponse($this->apiMock, 'getCorporateEnabled', true); + $this->setMockResponse($this->apiMock, 'getSingleSiteForKey', 'test'); + $this->setMockResponse($this->apiMock, 'customersCreate', $this->responseMock); + $this->setMockResponse($this->apiMock, 'customersCorporateCreate', $this->responseMock); + $this->setMockResponse($this->apiMock, 'customersCorporateAddressesCreate', $this->responseMock); + $this->setMockResponse($this->apiMock, 'customersCorporateCompaniesCreate', $this->responseMock); + $this->setMockResponse($this->apiMock, 'customersCorporateCreate', true); $this->customer = new WC_Customer(); $this->customer->set_first_name('Tester'); @@ -42,69 +55,89 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper $this->customer->set_billing_email($this->customer->get_email()); $this->customer->set_password('password'); $this->customer->set_billing_phone('89000000000'); + $this->customer->set_billing_company('test_company'); + $this->customer->set_billing_state('test_state'); + $this->customer->set_billing_postcode('123456'); + $this->customer->set_billing_city('test_city'); + $this->customer->set_billing_address_1('test_address_line'); $this->customer->set_date_created(date('Y-m-d H:i:s')); $this->customer->save(); } /** * @param retailcrm + * * @dataProvider dataProviderApiClient */ public function test_wc_customer_get($retailcrm) { $wc_customer = new WC_Customer($this->customer->get_id()); - $retailcrm_customer = $this->getRetailcrmCustomer($retailcrm); - $this->assertEquals($wc_customer, $retailcrm_customer->wcCustomerGet($this->customer->get_id())); - } + $retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm); + $this->assertEquals($wc_customer, $retailcrmCustomer->wcCustomerGet($this->customer->get_id())); + } /** * @param $retailcrm + * * @dataProvider dataProviderApiClient */ public function test_create_customer($retailcrm) { - $retailcrm_customer = $this->getRetailcrmCustomer($retailcrm); - $id = $retailcrm_customer->createCustomer($this->customer->get_id()); - $customer_send = $retailcrm_customer->getCustomer(); + $retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm); + $id = $retailcrmCustomer->createCustomer($this->customer->get_id()); + $customer = $retailcrmCustomer->getCustomer(); if ($retailcrm) { - $this->assertArrayHasKey('firstName', $customer_send); - $this->assertArrayHasKey('createdAt', $customer_send); - $this->assertArrayHasKey('email', $customer_send); - $this->assertNotEmpty($customer_send['externalId']); - $this->assertNotEmpty($customer_send['createdAt']); - $this->assertNotEmpty($customer_send['firstName']); - $this->assertNotEmpty($customer_send['email']); + $this->assertArrayHasKey('firstName', $customer); + $this->assertArrayHasKey('createdAt', $customer); + $this->assertArrayHasKey('email', $customer); + $this->assertNotEmpty($customer['externalId']); + $this->assertNotEmpty($customer['createdAt']); + $this->assertNotEmpty($customer['firstName']); + $this->assertNotEmpty($customer['email']); + $this->assertEquals($customer['firstName'], $this->customer->get_first_name()); + $this->assertEquals($customer['email'], $this->customer->get_email()); } else { $this->assertEquals(null, $id); - $this->assertEquals(array(), $customer_send); + $this->assertEquals(array(), $customer); } } + public function test_create_customer_empty_data() + { + $retailcrmCustomer = $this->getRetailcrmCustomer($this->apiMock); + $id = $retailcrmCustomer->createCustomer(null); + $customer = $retailcrmCustomer->getCustomer(); + + $this->assertEquals(null, $id); + $this->assertEquals(array(), $customer); + } + /** * @param $retailcrm + * * @dataProvider dataProviderApiClient */ public function test_update_customer($retailcrm) { - $retailcrm_customer = $this->getRetailcrmCustomer($retailcrm); - $customer = $retailcrm_customer->updateCustomer($this->customer->get_id()); - $customer_send = $retailcrm_customer->getCustomer(); + $retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm); + $wcCustomer = $retailcrmCustomer->updateCustomer($this->customer->get_id()); + $customer = $retailcrmCustomer->getCustomer(); if ($retailcrm) { - $this->assertArrayHasKey('externalId', $customer_send); - $this->assertArrayHasKey('firstName', $customer_send); - $this->assertArrayHasKey('createdAt', $customer_send); - $this->assertArrayHasKey('email', $customer_send); - $this->assertNotEmpty($customer_send['externalId']); - $this->assertNotEmpty($customer_send['createdAt']); - $this->assertNotEmpty($customer_send['firstName']); - $this->assertNotEmpty($customer_send['email']); - $this->assertInstanceOf('WC_Customer', $customer); + $this->assertInstanceOf('WC_Customer', $wcCustomer); + $this->assertArrayHasKey('externalId', $customer); + $this->assertArrayHasKey('firstName', $customer); + $this->assertArrayHasKey('createdAt', $customer); + $this->assertArrayHasKey('email', $customer); + $this->assertNotEmpty($customer['externalId']); + $this->assertNotEmpty($customer['createdAt']); + $this->assertNotEmpty($customer['firstName']); + $this->assertNotEmpty($customer['email']); } else { - $this->assertEquals(null, $customer); - $this->assertEquals(array(), $customer_send); + $this->assertEquals(null, $wcCustomer); + $this->assertEquals(array(), $customer); } } @@ -112,17 +145,101 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper * @param $retailcrm * * @dataProvider dataProviderApiClient - * @throws \Exception */ - public function test_create_customer_empty_response($retailcrm) + public function test_update_customer_by_id($retailcrm) { - $this->responseMock = null; - $this->apiMock = null; + $retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm); + $wcCustomer = $retailcrmCustomer->updateCustomerById($this->customer->get_id(), '12345'); + $customer = $retailcrmCustomer->getCustomer(); - $retailcrm_customer = $this->getRetailcrmCustomer($retailcrm); - $id = $retailcrm_customer->createCustomer($this->customer->get_id()); + if ($retailcrm) { + $this->assertInstanceOf('WC_Customer', $wcCustomer); + $this->assertArrayHasKey('externalId', $customer); + $this->assertArrayHasKey('firstName', $customer); + $this->assertArrayHasKey('createdAt', $customer); + $this->assertArrayHasKey('email', $customer); + $this->assertNotEmpty($customer['externalId']); + $this->assertNotEmpty($customer['createdAt']); + $this->assertNotEmpty($customer['firstName']); + $this->assertNotEmpty($customer['email']); + } else { + $this->assertEquals(null, $wcCustomer); + $this->assertEquals(array(), $customer); + } + } + + /** + * @param $retailcrm + * + * @dataProvider dataProviderApiClient + */ + public function test_is_corparate_enabled($retailcrm) + { + $retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm); + $isCorporate = $retailcrmCustomer->isCorporateEnabled(); + + if ($retailcrm) { + $this->assertEquals(true, $isCorporate); + } else { + $this->assertEquals(false, $isCorporate); + } + } + + /** + * @param $retailcrm + * + * @dataProvider dataProviderApiClient + */ + public function test_create_customer_corporate($retailcrm) + { + $retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm); + $id = $retailcrmCustomer->createCorporateCustomerForOrder(777, $this->customer->get_id(), new WC_Order()); + $customer = $retailcrmCustomer->getCorporateCustomer(); + + if ($retailcrm) { + $this->assertArrayHasKey('customerContacts', $customer); + + foreach ($customer['customerContacts'] as $customerCorporate) { + $this->assertArrayHasKey('isMain', $customerCorporate); + $this->assertArrayHasKey('customer', $customerCorporate); + $this->assertEquals($customerCorporate['isMain'], true); + $this->assertEquals($customerCorporate['customer']['id'], 777); + } + } else { + $this->assertEquals(null, $id); + $this->assertEquals(array(), $customer); + } + } + + public function test_create_customer_corporate_empty_data() + { + $retailcrmCustomer = $this->getRetailcrmCustomer($this->apiMock); + $id = $retailcrmCustomer->createCorporateCustomerForOrder(777, null, new WC_Order()); + $customer = $retailcrmCustomer->getCorporateCustomer(); $this->assertEquals(null, $id); + $this->assertEquals(array(), $customer); + } + + public function test_fill_corporate_address() + { + $retailcrmCustomer = $this->getRetailcrmCustomer($this->apiMock); + + // Mock response for get customer address + $responseCustomerAddress = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') + ->disableOriginalConstructor() + ->setMethods(array('isSuccessful')) + ->getMock(); + + $this->setMockResponse($responseCustomerAddress, 'isSuccessful', true); + $responseCustomerAddress->setResponse(DataCustomersRetailCrm::getCustomerAddress()); + + //Set responseCustomerAddress mock for apiMock + $this->setMockResponse($this->apiMock, 'customersCorporateAddresses', $responseCustomerAddress); + + $addressFound = $retailcrmCustomer->fillCorporateAddress($this->customer->get_id(), $this->customer); + + $this->assertEquals(true, $addressFound); } public function dataProviderApiClient() diff --git a/tests/test-wc-retailcrm-ga.php b/tests/test-wc-retailcrm-ga.php index af1c424..c37a8f7 100644 --- a/tests/test-wc-retailcrm-ga.php +++ b/tests/test-wc-retailcrm-ga.php @@ -27,12 +27,15 @@ class WC_Retailcrm_Google_Analytics_Test extends WC_Retailcrm_Test_Case_Helper /** * @param $checkout + * * @dataProvider dataProvider */ public function test_send_analytics($checkout) { - if ($checkout) { + if ($checkout === true) { $_GET['key'] = $this->orderKey; + } elseif (is_null($checkout)) { + $_GET['key'] = ''; } $js = $this->ga->send_analytics(); @@ -53,6 +56,9 @@ class WC_Retailcrm_Google_Analytics_Test extends WC_Retailcrm_Test_Case_Helper array( 'checkout' => false ), + array( + 'checkout' => null + ), array( 'checkout' => true ) diff --git a/tests/test-wc-retailcrm-history.php b/tests/test-wc-retailcrm-history.php index 7f25640..68d4847 100644 --- a/tests/test-wc-retailcrm-history.php +++ b/tests/test-wc-retailcrm-history.php @@ -1,8 +1,10 @@ regenerateMocks(); @@ -23,23 +22,18 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper public function test_history_order_create() { $product = WC_Helper_Product::create_simple_product(); - $order = $this->get_history_data_new_order($product->get_id()); + $order = DataHistoryRetailCrm::get_history_data_new_order($product->get_id()); - $this->mockHistory( - true, - true, - $this->empty_history(), - $order - ); + $this->mockHistory(true, DataHistoryRetailCrm::empty_history(), $order); - $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); + $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->fail('$order_added is null - no orders were added after receiving history'); } $order_added_items = $order_added->get_items(); @@ -47,7 +41,8 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $shipping_address = $order_added->get_address('shipping'); $billing_address = $order_added->get_address('billing'); $options = get_option(\WC_Retailcrm_Base::$option_key); - $this->assertEquals(self::STATUS_1, $options[$order_added->get_status()]); + + $this->assertEquals('status1', $options[$order_added->get_status()]); if (is_object($order_added_item)) { $this->assertEquals($product->get_id(), $order_added_item->get_product()->get_id()); @@ -87,15 +82,13 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $product = WC_Helper_Product::create_simple_product(); $product_deleted = WC_Helper_Product::create_simple_product(); - $order = $this->get_history_data_new_order_deleted_items($product->get_id(), $product_deleted->get_id()); - - $this->mockHistory( - true, - true, - $this->empty_history(), - $order + $order = DataHistoryRetailCrm::get_history_data_new_order_deleted_items( + $product->get_id(), + $product_deleted->get_id() ); + $this->mockHistory(true, DataHistoryRetailCrm::empty_history(), $order); + $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); $retailcrm_history->getHistory(); @@ -113,7 +106,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $shipping_address = $order_added->get_address('shipping'); $billing_address = $order_added->get_address('billing'); $options = get_option(\WC_Retailcrm_Base::$option_key); - $this->assertEquals(self::STATUS_1, $options[$order_added->get_status()]); + $this->assertEquals('status1', $options[$order_added->get_status()]); if (is_object($order_added_item)) { $this->assertEquals($product->get_id(), $order_added_item->get_product()->get_id()); @@ -155,9 +148,8 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $this->mockHistory( true, - true, - $this->empty_history(), - $this->get_history_data_product_add($product->get_id(), $order->get_id()) + DataHistoryRetailCrm::empty_history(), + DataHistoryRetailCrm::get_history_data_product_add($product->get_id(), $order->get_id()) ); $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); @@ -178,9 +170,8 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $this->mockHistory( true, - true, - $this->empty_history(), - $this->get_history_data_update($order->get_id()) + DataHistoryRetailCrm::empty_history(), + DataHistoryRetailCrm::get_history_data_update($order->get_id()) ); $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); @@ -189,8 +180,82 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $order_updated = wc_get_order($order->get_id()); $options = get_option(\WC_Retailcrm_Base::$option_key); - $this->assertEquals(self::STATUS_2, $options[$order_updated->get_status()]); + $this->assertEquals('status4', $options[$order_updated->get_status()]); $this->assertEquals('payment2', $options[$order_updated->get_payment_method()]); + $this->assertEquals('customerComment', $order_updated->get_customer_note()); + $this->assertEquals(12345678, $order_updated->get_billing_phone()); + $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())); + + foreach ($notes as $note) { + if ($note->content === 'managerComment') { + $managerComment = $note->content; + } + } + + $this->assertEquals('managerComment', $managerComment); + } + + public function test_history_order_update_empty_order() + { + $order = WC_Helper_Order::create_order(0); + $order->set_status(''); + + $this->mockHistory( + true, + DataHistoryRetailCrm::empty_history(), + DataHistoryRetailCrm::get_history_data_update($order->get_id()) + ); + + $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); + $retailcrm_history->getHistory(); + + $order_updated = wc_get_order($order->get_id()); + + $this->assertNotEquals('status4', $order_updated->get_status()); + } + + + public function test_history_customer_create() + { + $this->mockHistory( + true, + DataHistoryRetailCrm::get_history_data_new_customer(), + DataHistoryRetailCrm::empty_history() + ); + + $retailcrm_history = new WC_Retailcrm_History($this->apiMock); + $retailcrm_history->getHistory(); + + $sinceId = get_option('retailcrm_orders_history_since_id'); + + $this->assertEquals(0, $sinceId); + } + + public function test_history_customer_update() + { + $customerId = wc_create_new_customer('mail_test@mail.es', 'test'); + $customer = DataHistoryRetailCrm::get_history_data_new_customer(); + + $customer['history'][0]['customer']['externalId'] = $customerId; + + $this->mockHistory( + true, + $customer, + DataHistoryRetailCrm::empty_history() + ); + + $retailcrm_history = new WC_Retailcrm_History($this->apiMock); + $retailcrm_history->getHistory(); + + $wcCustomer = new WC_Customer($customerId); + + $this->assertNotEmpty($wcCustomer); + $this->assertEquals('Test_Name', $wcCustomer->get_first_name()); + $this->assertEquals('City', $wcCustomer->get_billing_city()); + $this->assertEquals(123456, $wcCustomer->get_billing_postcode()); } public function test_history_switch_customer_tests() @@ -225,9 +290,8 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $this->mockHistory( true, - true, - $this->empty_history(), - $this->get_history_order_for_client_replace($product->get_id()) + DataHistoryRetailCrm::empty_history(), + DataHistoryRetailCrm::get_history_order_for_client_replace($product->get_id()) ); $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); @@ -255,14 +319,13 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper { $this->mockHistory( true, - true, - $this->empty_history(), - $this->get_history_change_to_another_individual($order_id) + DataHistoryRetailCrm::empty_history(), + DataHistoryRetailCrm::get_history_change_to_another_individual($order_id) ); $this->ordersGetMock( true, - $this->get_order_with_customer_and_contact($this->get_new_individual_for_order()) + DataHistoryRetailCrm::get_order_with_customer_and_contact(DataHistoryRetailCrm::get_new_individual_for_order()) ); $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); @@ -317,16 +380,15 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper { $this->mockHistory( true, - true, - $this->empty_history(), - $this->get_history_change_to_corporate($order_id) + DataHistoryRetailCrm::empty_history(), + DataHistoryRetailCrm::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(), + DataHistoryRetailCrm::get_order_with_customer_and_contact( + DataHistoryRetailCrm::get_new_corporate_for_order(), + DataHistoryRetailCrm::get_new_contact_for_order(), array('name' => 'Компания1'), 'legal-entity' ) @@ -381,16 +443,15 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper { $this->mockHistory( true, - true, - $this->empty_history(), - $this->get_history_change_to_another_corporate($order_id) + DataHistoryRetailCrm::empty_history(), + DataHistoryRetailCrm::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(), + DataHistoryRetailCrm::get_order_with_customer_and_contact( + DataHistoryRetailCrm::get_another_corporate_for_order(), + DataHistoryRetailCrm::get_another_contact_for_order(), array('name' => 'TestCompany3428769'), 'legal-entity' ) @@ -446,16 +507,15 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper { $this->mockHistory( true, - true, - $this->empty_history(), - $this->get_history_change_only_company($order_id) + DataHistoryRetailCrm::empty_history(), + DataHistoryRetailCrm::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(), + DataHistoryRetailCrm::get_order_with_customer_and_contact( + DataHistoryRetailCrm::get_another_corporate_for_order(), + DataHistoryRetailCrm::get_another_contact_for_order(), array('name' => 'TestCompany017089465'), 'legal-entity' ) @@ -511,16 +571,15 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper { $this->mockHistory( true, - true, - $this->empty_history(), - $this->get_history_change_only_contact($order_id) + DataHistoryRetailCrm::empty_history(), + DataHistoryRetailCrm::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(), + DataHistoryRetailCrm::get_order_with_customer_and_contact( + DataHistoryRetailCrm::get_another_corporate_for_order(), + DataHistoryRetailCrm::get_contact_when_only_contact_changed(), array('name' => 'TestCompany017089465'), 'legal-entity' ) @@ -576,14 +635,13 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper { $this->mockHistory( true, - true, - $this->empty_history(), - $this->get_history_change_from_corporate_to_individual($order_id) + DataHistoryRetailCrm::empty_history(), + DataHistoryRetailCrm::get_history_change_from_corporate_to_individual($order_id) ); $this->ordersGetMock( true, - $this->get_order_with_customer_and_contact($this->get_initial_regular_customer()) + DataHistoryRetailCrm::get_order_with_customer_and_contact(DataHistoryRetailCrm::get_initial_regular_customer()) ); $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); @@ -641,15 +699,13 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper private function ordersGetMock($isSuccessful, $response) { $mock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); + ->disableOriginalConstructor() + ->setMethods(array('isSuccessful')) + ->getMock(); $mock->expects($this->any()) - ->method('isSuccessful') - ->willReturn($isSuccessful); + ->method('isSuccessful') + ->willReturn($isSuccessful); $mock->setResponse($response); $this->apiMock->expects($this->any())->method('ordersGet')->willReturn($mock); @@ -658,2074 +714,57 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper /** * Mocks customers and orders history responses with provided data * - * @param bool $isSuccessfulCustomers - * @param bool $isSuccessfulOrders - * @param array $customersHistoryResponse + * @param $isSuccessfulResponse + * @param array $customerHistoryResponse * @param array $orderHistoryResponse */ - private function mockHistory( - $isSuccessfulCustomers, - $isSuccessfulOrders, - $customersHistoryResponse, - $orderHistoryResponse - ) { + private function mockHistory($isSuccessfulResponse, $customerHistoryResponse, $orderHistoryResponse) + { $this->setOptions(); if (!add_option('retailcrm_orders_history_since_id', 0)) { - update_option('retailcrm_orders_history_since_id', 0); + update_option('retailcrm_orders_history_since_id', 1); } if (!add_option('retailcrm_customers_history_since_id', 0)) { - update_option('retailcrm_customers_history_since_id', 0); + update_option('retailcrm_customers_history_since_id', 1); } $this->customersHistoryResponse->expects($this->any()) - ->method('isSuccessful') - ->willReturn($isSuccessfulCustomers); + ->method('isSuccessful') + ->willReturn($isSuccessfulResponse); $this->ordersHistoryResponse->expects($this->any()) - ->method('isSuccessful') - ->willReturn($isSuccessfulOrders); + ->method('isSuccessful') + ->willReturn($isSuccessfulResponse); - $this->customersHistoryResponse->setResponse($customersHistoryResponse); + $this->customersHistoryResponse->setResponse($customerHistoryResponse); $this->ordersHistoryResponse->setResponse($orderHistoryResponse); - $this->apiMock->expects($this->any())->method('customersHistory')->willReturn($this->customersHistoryResponse); - $this->apiMock->expects($this->any())->method('ordersHistory')->willReturn($this->ordersHistoryResponse); + $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') - ->disableOriginalConstructor() - ->setMethods(array( - 'ordersHistory', - 'customersHistory', - 'ordersGet' - )) - ->getMock(); + ->disableOriginalConstructor() + ->setMethods(array('ordersHistory', 'customersHistory', 'ordersGet')) + ->getMock(); $this->customersHistoryResponse = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); + ->disableOriginalConstructor() + ->setMethods(array('isSuccessful')) + ->getMock(); $this->ordersHistoryResponse = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); - } - - private function empty_history() - { - return array( - 'success' => true, - 'history' => array(), - "pagination" => array( - "limit" => 100, - "totalCount" => 0, - "currentPage" => 1, - "totalPageCount" => 0 - ) - ); - } - - private function get_history_data_new_order($product_create_id) - { - return array( - 'success' => true, - 'history' => array( - array( - 'id' => 1, - 'createdAt' => '2018-01-01 00:00:00', - 'created' => true, - 'source' => 'user', - 'user' => array( - 'id' => 1 - ), - 'field' => 'status', - 'oldValue' => null, - 'newValue' => array( - 'code' => self::STATUS_1 - ), - 'order' => array( - 'slug' => 1, - 'id' => 1, - 'number' => '1C', - 'orderType' => 'eshop-individual', - 'orderMethod' => 'phone', - 'countryIso' => 'RU', - 'createdAt' => '2018-01-01 00:00:00', - 'statusUpdatedAt' => '2018-01-01 00:00:00', - 'summ' => 100, - 'totalSumm' => 100, - 'prepaySum' => 0, - 'purchaseSumm' => 50, - 'markDatetime' => '2018-01-01 00:00:00', - 'firstName' => 'Test', - 'lastName' => 'Test', - 'phone' => '80000000000', - 'call' => false, - 'expired' => false, - 'customer' => array( - 'type' => 'customer', - 'segments' => array(), - 'id' => 1, - 'firstName' => 'Test', - 'lastName' => 'Test', - 'email' => 'email@test.ru', - 'phones' => array( - array( - 'number' => '111111111111111' - ), - array( - 'number' => '+7111111111' - ) - ), - 'address' => array( - 'index' => '111111', - 'countryIso' => 'RU', - 'region' => 'Test region', - 'city' => 'Test', - 'text' => 'Test text address' - ), - 'createdAt' => '2018-01-01 00:00:00', - 'managerId' => 1, - 'vip' => false, - 'bad' => false, - 'site' => 'test-com', - 'contragent' => array( - 'contragentType' => 'individual' - ), - 'personalDiscount' => 0, - 'cumulativeDiscount' => 0, - 'marginSumm' => 58654, - 'totalSumm' => 61549, - 'averageSumm' => 15387.25, - 'ordersCount' => 4, - 'costSumm' => 101, - 'customFields' => array( - 'custom' => 'test' - ) - ), - 'contragent' => array(), - 'delivery' => array( - 'cost' => 0, - 'netCost' => 0, - 'address' => array( - 'index' => '111111', - 'countryIso' => 'RU', - 'region' => 'Test region', - 'city' => 'Test', - 'text' => 'Test text address' - ) - ), - 'site' => 'test-com', - 'status' => self::STATUS_1, - 'items' => array( - array( - 'id' => 160, - 'initialPrice' => 100, - 'discountTotal' => 5, - 'createdAt' => '2018-01-01 00:00:00', - 'quantity' => 1, - 'status' => 'new', - 'externalIds' =>array( - array( - 'code' =>'woocomerce', - 'value' =>"160_".$product_create_id - ) - ), - 'initialPrice' => 15, - 'discountTotal' => 1, - 'offer' => array( - 'id' => 1, - 'externalId' => $product_create_id, - 'xmlId' => '1', - 'name' => 'Test name', - 'vatRate' => 'none' - ), - 'properties' => array(), - 'purchasePrice' => 50 - ) - ), - 'paymentType' => 'payment4', - 'payments' => array( - array( - 'id'=> 1, - 'type'=> 'payment4', - 'amount'=> 100, - ) - ), - 'fromApi' => false, - 'length' => 0, - 'width' => 0, - 'height' => 0, - 'shipmentStore' => 'main', - 'shipped' => false, - 'customFields' => array(), - 'uploadedToExternalStoreSystem' => false - ) - ) - ), - "pagination" => array( - "limit" => 100, - "totalCount" => 1, - "currentPage" => 1, - "totalPageCount" => 1 - ) - ); - } - - private function get_history_data_new_order_deleted_items($product_create_id, $product_delete_id) - { - return array( - 'success' => true, - 'history' => array( - array( - 'id' => 1, - 'createdAt' => '2018-01-01 00:00:00', - 'created' => true, - 'source' => 'user', - 'user' => array( - 'id' => 1 - ), - 'field' => 'status', - 'oldValue' => null, - 'newValue' => array( - 'code' => self::STATUS_1 - ), - 'order' => array( - 'slug' => 3, - 'id' => 3, - 'number' => '3C', - 'orderType' => 'eshop-individual', - 'orderMethod' => 'phone', - 'countryIso' => 'RU', - 'createdAt' => '2018-01-01 00:00:00', - 'statusUpdatedAt' => '2018-01-01 00:00:00', - 'summ' => 100, - 'totalSumm' => 100, - 'prepaySum' => 0, - 'purchaseSumm' => 50, - 'markDatetime' => '2018-01-01 00:00:00', - 'firstName' => 'Test', - 'lastName' => 'Test', - 'phone' => '80000000000', - 'call' => false, - 'expired' => false, - 'customer' => array( - 'type' => 'customer', - 'segments' => array(), - 'id' => 1, - 'firstName' => 'Test', - 'lastName' => 'Test', - 'email' => 'email@test.ru', - 'phones' => array( - array( - 'number' => '111111111111111' - ), - array( - 'number' => '+7111111111' - ) - ), - 'address' => array( - 'index' => '111111', - 'countryIso' => 'RU', - 'region' => 'Test region', - 'city' => 'Test', - 'text' => 'Test text address' - ), - 'createdAt' => '2018-01-01 00:00:00', - 'managerId' => 1, - 'vip' => false, - 'bad' => false, - 'site' => 'test-com', - 'contragent' => array( - 'contragentType' => 'individual' - ), - 'personalDiscount' => 0, - 'cumulativeDiscount' => 0, - 'marginSumm' => 58654, - 'totalSumm' => 61549, - 'averageSumm' => 15387.25, - 'ordersCount' => 4, - 'costSumm' => 101, - 'customFields' => array( - 'custom' => 'test' - ) - ), - 'contragent' => array(), - 'delivery' => array( - 'cost' => 0, - 'netCost' => 0, - 'address' => array( - 'index' => '111111', - 'countryIso' => 'RU', - 'region' => 'Test region', - 'city' => 'Test', - 'text' => 'Test text address' - ) - ), - 'site' => 'test-com', - 'status' => self::STATUS_1, - 'items' => array( - array( - 'id' => 160, - 'initialPrice' => 15, - 'discountTotal' => 1, - 'createdAt' => '2018-01-01 00:00:00', - 'quantity' => 1, - 'status' => 'new', - 'externalIds' => array( - array( - 'code' => 'woocomerce', - 'value' => "160_" . $product_create_id - ) - ), - 'offer' => array( - 'id' => 1, - 'externalId' => $product_create_id, - 'xmlId' => '1', - 'name' => 'Test name', - 'vatRate' => 'none' - ), - 'properties' => array(), - 'purchasePrice' => 10 - ), - array( - 'id' => 161, - 'initialPrice' => 100, - 'discountTotal' => 5, - 'createdAt' => '2018-01-01 00:00:00', - 'quantity' => 1, - 'status' => 'new', - 'externalIds' => array( - array( - 'code' => 'woocomerce', - 'value' => "161_" . $product_delete_id - ) - ), - 'offer' => array( - 'id' => 2, - 'externalId' => $product_delete_id, - 'xmlId' => '2', - 'name' => 'Test name 2', - 'vatRate' => 'none' - ), - 'properties' => array(), - 'purchasePrice' => 50 - ) - ), - 'paymentType' => 'payment4', - 'payments' => array( - array( - 'id' => 1, - 'type' => 'payment4', - 'amount' => 100, - ) - ), - 'fromApi' => false, - 'length' => 0, - 'width' => 0, - 'height' => 0, - 'shipmentStore' => 'main', - 'shipped' => false, - 'customFields' => array(), - 'uploadedToExternalStoreSystem' => false - ) - ), - array( - 'id' => 2, - 'createdAt' => '2018-01-01 00:01:00', - 'source' => 'api', - 'field' => 'order_product', - 'oldValue' => array( - 'id' => 161, - 'offer' => array( - 'id' => 2, - 'externalId' => $product_delete_id - ) - ), - 'newValue' => null, - 'order' => array( - 'id' => 3, - 'site' => 'test-com', - 'status' => self::STATUS_1 - ), - 'item' => array( - 'id' => 161, - 'initialPrice' => 100, - 'discountTotal' => 5, - 'createdAt' => '2018-01-01 00:00:00', - 'quantity' => 1, - 'status' => 'new', - 'externalIds' => array( - array( - 'code' => 'woocomerce', - 'value' => "161_" . $product_delete_id - ) - ), - 'offer' => array( - 'id' => 2, - 'externalId' => $product_delete_id, - 'xmlId' => '2', - 'name' => 'Test name 2', - 'vatRate' => 'none' - ), - 'properties' => array(), - 'purchasePrice' => 50 - ) - ) - ), - 'pagination' => array( - 'limit' => 100, - 'totalCount' => 2, - 'currentPage' => 1, - 'totalPageCount' => 1 - ) - ); - } - - private function get_history_data_product_add($product_add_id, $order_id) - { - return array( - 'success' => true, - 'history' => array( - array( - 'id' => 2, - 'createdAt' => '2018-01-01 00:00:01', - 'source'=> 'user', - 'user' => array( - 'id'=> 1 - ), - 'field' => 'order_product', - 'oldValue' => null, - 'newValue' => array( - 'id' => 2, - 'offer' => array( - 'id'=> 2, - 'externalId' => $product_add_id, - 'xmlId' => 'xmlId' - ) - ), - 'order' => array( - 'id' => 2, - 'externalId' => $order_id, - 'site' => 'test-com', - 'status' => self::STATUS_1 - ), - 'item' => array( - 'id' => 2, - 'initialPrice' => 999, - 'createdAt' => '2018-01-01 00:02:00', - 'quantity' => 2, - 'status' => self::STATUS_1, - 'externalIds' =>array( - array( - 'code' =>'woocomerce', - 'value' =>"160_".$product_add_id - ) - ), - 'offer' => array( - 'id' => 2, - 'externalId' => $product_add_id, - 'xmlId' => 'xmlId', - 'name' => 'Test name 2' - ), - 'properties' => array(), - 'purchasePrice' => 500 - ) - ) - ), - "pagination" => array( - "limit" => 100, - "totalCount" => 1, - "currentPage" => 1, - "totalPageCount" => 1 - ) - ); - } - - private function get_history_data_update($order_id) - { - $history = array( - 'success' => true, - 'history' => array( - array( - 'id' => 3, - 'createdAt' => '2018-01-01 00:03:00', - 'source' => 'user', - 'user' => array( - 'id' => 1 - ), - 'field' => 'status', - 'oldValue' => array( - 'code' => 'new' - ), - 'newValue' => array( - 'code' => self::STATUS_2 - ), - 'order' => array( - 'id' => 2, - 'externalId' => $order_id, - 'managerId' => 6, - 'site' => 'test-com', - 'status' => self::STATUS_2 - ) - ) - ), - "pagination" => array( - "limit" => 100, - "totalCount" => 1, - "currentPage" => 1, - "totalPageCount" => 1 - ) - ); - - $payment_v5 = array( - 'id' => 4, - 'createdAt' => '2018-01-01 00:03:00', - 'source' => 'user', - 'user' => array( - 'id' => 1 - ), - 'field' => 'payments', - 'oldValue' => null, - 'newValue' => array( - 'code' => 'payment2' - ), - 'order' => array( - 'id' => 2, - 'externalId' => $order_id, - 'managerId' => 6, - 'site' => 'test-com', - 'status' => self::STATUS_2 - ), - 'payment' => array( - 'id' => 1, - 'type' => 'payment2', - "amount" => 100 - ) - ); - - array_push($history['history'], $payment_v5); - - 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, - 'discountTotal' => 5, - 'createdAt' => '2018-01-01 00:00:00', - 'quantity' => 1, - 'status' => 'new', - 'externalIds' =>array( - array( - 'code' =>'woocomerce', - 'value' =>"160_".$productId - ) - ), - 'initialPrice' => 15, - 'discountTotal' => 1, - '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' => 5, - '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['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(), - ); + ->disableOriginalConstructor() + ->setMethods(array('isSuccessful')) + ->getMock(); } } diff --git a/tests/test-wc-retailcrm-inventories.php b/tests/test-wc-retailcrm-inventories.php index 908d799..9f491e3 100644 --- a/tests/test-wc-retailcrm-inventories.php +++ b/tests/test-wc-retailcrm-inventories.php @@ -1,26 +1,25 @@ responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); + ->disableOriginalConstructor() + ->setMethods(array('isSuccessful')) + ->getMock(); + + $this->setMockResponse($this->responseMock, 'isSuccessful', true); $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') - ->disableOriginalConstructor() - ->setMethods(array( - 'storeInventories' - )) - ->getMock(); + ->disableOriginalConstructor() + ->setMethods(array('storeInventories')) + ->getMock(); parent::setUp(); } @@ -31,92 +30,103 @@ class WC_Retailcrm_Inventories_Test extends WC_Retailcrm_Test_Case_Helper * * @dataProvider dataProviderLoadStocks */ - public function test_load_stocks($retailcrm, $response) + public function test_load_stocks_simple_product($retailcrm, $response) { $offer = WC_Helper_Product::create_simple_product(); $offer->save(); - if ($response['success'] == true) { + if (null !== $response) { $response['offers'][0]['externalId'] = $offer->get_id(); - $this->responseMock->expects($this->any()) - ->method('isSuccessful') - ->willReturn(true); - } elseif ($response['success'] == false) { - $this->responseMock->expects($this->any()) - ->method('isSuccessful') - ->willReturn(false); } $this->responseMock->setResponse($response); if ($retailcrm) { - $retailcrm->expects($this->any()) - ->method('storeInventories') - ->willReturn($this->responseMock); + $this->setMockResponse($retailcrm, 'storeInventories', $this->responseMock); } $retailcrm_inventories = new WC_Retailcrm_Inventories($retailcrm); - $result = $retailcrm_inventories->load_stocks(); + $result = $retailcrm_inventories->updateQuantity(); - if ($retailcrm && $response['success'] == true) { - $product = new WC_Product_Simple($result[0]); + $this->checkProductData($retailcrm, $response, $result, 'simple'); + } + + /** + * @param $retailcrm + * @param $response + * + * @dataProvider dataProviderLoadStocks + */ + public function test_load_stocks_variation_product($retailcrm, $response) + { + $offer = WC_Helper_Product::create_variation_product(); + $offer->save(); + + $childrens = $offer->get_children(); + + if (null !== $response) { + $response['offers'][0]['externalId'] = $childrens[0]; + } + + $this->responseMock->setResponse($response); + + if ($retailcrm) { + $this->setMockResponse($retailcrm, 'storeInventories', $this->responseMock); + } + + $retailcrm_inventories = new WC_Retailcrm_Inventories($retailcrm); + $result = $retailcrm_inventories->updateQuantity(); + + $this->checkProductData($retailcrm, $response, $result, 'variation'); + } + + public function test_sync_off() + { + $options = $this->getOptions(); + $options['sync'] = 'no'; + + update_option(WC_Retailcrm_Base::$option_key, $options); + + $retailcrm_inventories = new WC_Retailcrm_Inventories($this->apiMock); + $result = $retailcrm_inventories->updateQuantity(); + + $this->assertEquals(false, $result); + } + + private function checkProductData($retailcrm, $response, $result, $entity) + { + if ($retailcrm && null !== $response) { + $product = wc_get_product($result[0]); $this->assertInstanceOf('WC_Product', $product); + $this->assertEquals($entity, $product->get_type()); $this->assertEquals(10, $product->get_stock_quantity()); - $this->assertContains($product->get_id(), $result); + $this->assertEquals($result[0], $product->get_id()); $this->assertInternalType('array', $result); + } elseif (null === $response) { + $this->assertEquals(false, $result); } else { $this->assertEquals(null, $result); } } - private function getResponseData() - { - return array( - 'true' => array( - 'success' => true, - 'pagination' => array( - 'limit' => 250, - 'totalCount' => 1, - 'currentPage' => 1, - 'totalPageCount' => 1 - ), - 'offers' => array( - array( - 'id' => 1, - 'xmlId' => 'xmlId', - 'quantity' => 10 - ) - ) - ), - 'false' => array( - 'success' => false, - 'errorMsg' => 'Forbidden' - ) - ); - } - public function dataProviderLoadStocks() { $this->setUp(); - $response = $this->getResponseData(); + $response = DataInventoriesRetailCrm::getResponseData(); return array( array( 'retailcrm' => $this->apiMock, - 'response' => $response['true'] + 'response' => $response ), array( 'retailcrm' => false, - 'response' => $response['true'] + 'response' => $response ), array( 'retailcrm' => $this->apiMock, - 'response' => $response['false'] - ), - array( - 'retailcrm' => false, - 'response' => $response['false'] + 'response' => null ) ); } diff --git a/tests/test-wc-retailcrm-orders.php b/tests/test-wc-retailcrm-orders.php index b68a2e8..e6fc37c 100644 --- a/tests/test-wc-retailcrm-orders.php +++ b/tests/test-wc-retailcrm-orders.php @@ -1,6 +1,6 @@ apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') - ->disableOriginalConstructor() - ->setMethods(array( - 'ordersGet', - 'ordersCreate', - 'ordersEdit', - 'customersGet', - 'customersCreate', - 'ordersPaymentCreate', - 'ordersPaymentDelete', - 'customersList' - )) - ->getMock(); + ->disableOriginalConstructor() + ->setMethods( + array( + 'ordersGet', + 'ordersCreate', + 'ordersEdit', + 'customersGet', + 'customersCreate', + 'ordersPaymentCreate', + 'ordersPaymentDelete', + 'customersList', + 'getCorporateEnabled', + 'customersCorporateCompanies', + 'customersCorporateList', + 'customersCorporateCreate', + 'getSingleSiteForKey', + 'customersCorporateAddressesCreate', + 'customersCorporateCompaniesCreate' + ) + ) + ->getMock(); + + $this->options = $this->setOptions(); parent::setUp(); } - /** * @param $retailcrm * @dataProvider dataProviderRetailcrm @@ -33,73 +43,136 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper public function test_order_create($retailcrm) { if ($retailcrm) { - $responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); + $responseMock = $this->createResponseMock(); + $responseMockCustomers = $this->createResponseMock(); - $responseMockCustomers = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); $responseMockCustomers->setResponse( - array('success' => true, + array( + 'success' => true, 'customers' => array( array('externalId' => 1) ) ) ); - $retailcrm->expects($this->any()) - ->method('customersCreate') - ->willReturn($responseMock); - $retailcrm->expects($this->any()) - ->method('customersList') - ->willReturn($responseMockCustomers); + $this->setMockResponse($retailcrm, 'ordersCreate', $responseMock); + $this->setMockResponse($retailcrm, 'customersCreate', $responseMock); + $this->setMockResponse($retailcrm, 'customersList', $responseMockCustomers); } $this->createTestOrder(); - $this->options = $this->setOptions(); - $retailcrm_orders = $this->getRetailcrmOrders($retailcrm); - $order = $retailcrm_orders->orderCreate($this->order->get_id()); - $order_send = $retailcrm_orders->getOrder(); + + $retailcrmOrders = $this->getRetailcrmOrders($retailcrm); + $order = $retailcrmOrders->orderCreate($this->order->get_id()); + $orderData = $retailcrmOrders->getOrder(); if ($retailcrm) { $this->assertInstanceOf('WC_Order', $order); - $this->assertInternalType('array', $order_send); - $this->assertArrayHasKey('status', $order_send); - $this->assertArrayHasKey('externalId', $order_send); - $this->assertArrayHasKey('firstName', $order_send); - $this->assertArrayHasKey('lastName', $order_send); - $this->assertArrayHasKey('email', $order_send); - $this->assertArrayHasKey('delivery', $order_send); - $this->assertArrayHasKey('code', $order_send['delivery']); - $this->assertArrayHasKey('address', $order_send['delivery']); - $this->assertArrayHasKey('index', $order_send['delivery']['address']); - $this->assertArrayHasKey('city', $order_send['delivery']['address']); - $this->assertEquals($this->order->get_id(), $order_send['externalId']); - $this->assertEquals('status1', $order_send['status']); - $this->assertEquals('Jeroen', $order_send['firstName']); - $this->assertEquals('Sormani', $order_send['lastName']); - $this->assertEquals('admin@example.org', $order_send['email']); - $this->assertEquals('US', $order_send['countryIso']); - if (mb_strlen($order_send['delivery']['address']['index']) === 6) { - $this->assertEquals('123456', $order_send['delivery']['address']['index']); + $this->assertInternalType('array', $orderData); + $this->assertArrayHasKey('status', $orderData); + $this->assertArrayHasKey('externalId', $orderData); + $this->assertArrayHasKey('firstName', $orderData); + $this->assertArrayHasKey('lastName', $orderData); + $this->assertArrayHasKey('email', $orderData); + $this->assertArrayHasKey('delivery', $orderData); + $this->assertArrayHasKey('code', $orderData['delivery']); + $this->assertArrayHasKey('address', $orderData['delivery']); + $this->assertArrayHasKey('index', $orderData['delivery']['address']); + $this->assertArrayHasKey('city', $orderData['delivery']['address']); + $this->assertEquals($this->order->get_id(), $orderData['externalId']); + $this->assertEquals('status1', $orderData['status']); + $this->assertEquals('Jeroen', $orderData['firstName']); + $this->assertEquals('Sormani', $orderData['lastName']); + $this->assertEquals('admin@example.org', $orderData['email']); + $this->assertEquals('US', $orderData['countryIso']); + if (mb_strlen($orderData['delivery']['address']['index']) === 6) { + $this->assertEquals('123456', $orderData['delivery']['address']['index']); } else { - $this->assertEquals('12345', $order_send['delivery']['address']['index']); + $this->assertEquals('12345', $orderData['delivery']['address']['index']); } - $this->assertEquals('WooCity', $order_send['delivery']['address']['city']); - $this->assertEquals('delivery', $order_send['delivery']['code']); + $this->assertEquals('WooCity', $orderData['delivery']['address']['city']); + $this->assertEquals('delivery', $orderData['delivery']['code']); - $this->assertArrayHasKey('payments', $order_send); - $this->assertInternalType('array', $order_send['payments']); - $this->assertArrayHasKey('type', $order_send['payments'][0]); - $this->assertArrayHasKey('externalId', $order_send['payments'][0]); - $this->assertEquals('payment1', $order_send['payments'][0]['type']); + $this->assertArrayHasKey('payments', $orderData); + $this->assertInternalType('array', $orderData['payments']); + $this->assertArrayHasKey('type', $orderData['payments'][0]); + $this->assertArrayHasKey('externalId', $orderData['payments'][0]); + $this->assertEquals('payment1', $orderData['payments'][0]['type']); + } else { + $this->assertEquals(null, $order); + } + } + + /** + * @param $retailcrm + * @dataProvider dataProviderRetailcrm + */ + public function test_order_create_with_corporate_customer($retailcrm) + { + if ($retailcrm) { + $responseMock = $this->createResponseMock(); + + // Mock response for search customer + $responseMockSearch = $this->createResponseMock(); + $this->setMockResponse($responseMockSearch, 'isSuccessful', true); + + $responseMockSearch->setResponse( + array( + 'success' => true, + 'customersCorporate' => array() + ) + ); + + // Mock response for create customer corporate, his addresses and companies + $responseMockCustomerCorporate = $this->createResponseMock(); + $this->setMockResponse($responseMockCustomerCorporate, 'isSuccessful', true); + + $responseMockCustomerCorporate->setResponse( + array( + 'success' => true, + 'id' => 1 + ) + ); + + // Mock response for get companies + $responseMockCompany = $this->createResponseMock(); + $this->setMockResponse($responseMockCompany, 'isSuccessful', true); + + $responseMockCompany->setResponse( + array( + 'success' => true, + 'companies' => array( + array('name' => 'WooCompany', 'id' => 777) + ) + ) + ); + + $this->setMockResponse($retailcrm, 'ordersCreate', $responseMock); + $this->setMockResponse($retailcrm, 'getSingleSiteForKey', 'woo'); + $this->setMockResponse($retailcrm, 'customersCorporateCreate', $responseMockCustomerCorporate); + $this->setMockResponse($retailcrm, 'customersCorporateAddressesCreate', $responseMockCustomerCorporate); + $this->setMockResponse($retailcrm, 'customersCorporateCompaniesCreate', $responseMockCustomerCorporate); + $this->setMockResponse($retailcrm, 'customersCorporateList', $responseMockSearch); + $this->setMockResponse($retailcrm, 'getCorporateEnabled', true); + $this->setMockResponse($retailcrm, 'customersCorporateCompanies', $responseMockCompany); + } + + $this->createTestOrder(); + + $retailcrmOrders = $this->getRetailcrmOrders($retailcrm); + $order = $retailcrmOrders->orderCreate($this->order->get_id()); + $orderData = $retailcrmOrders->getOrder(); + + if ($retailcrm) { + $this->assertInstanceOf('WC_Order', $order); + $this->assertArrayHasKey('customer', $orderData); + $this->assertArrayHasKey('id', $orderData['customer']); + $this->assertEquals(1, $orderData['customer']['id']); + $this->assertArrayHasKey('company', $orderData); + $this->assertArrayHasKey('id', $orderData['company']); + $this->assertArrayHasKey('name', $orderData['company']); + $this->assertEquals(777, $orderData['company']['id']); + $this->assertEquals($this->order->get_billing_company(), $orderData['company']['name']); } else { $this->assertEquals(null, $order); } @@ -114,72 +187,57 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper public function test_update_order($isSuccessful, $retailcrm) { $this->createTestOrder(); - $this->options = $this->setOptions(); if ($retailcrm) { - $responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); + $responseMock = $this->createResponseMock(); - $responseMock->expects($this->any()) - ->method('isSuccessful') - ->willReturn($isSuccessful); - - $retailcrm->expects($this->any()) - ->method('ordersEdit') - ->willReturn($responseMock); - - $retailcrm->expects($this->any()) - ->method('ordersPaymentDelete') - ->willReturn($responseMock); + $this->setMockResponse($responseMock, 'isSuccessful', $isSuccessful); + $this->setMockResponse($retailcrm, 'ordersEdit', $responseMock); + $this->setMockResponse($retailcrm, 'ordersPaymentDelete', $responseMock); $response = $this->getResponseData($this->order->get_id()); $responseMock->setResponse($response); - $retailcrm->expects($this->any()) - ->method('ordersGet') - ->willReturn($responseMock); + $this->setMockResponse($retailcrm, 'ordersGet', $responseMock); } - $retailcrm_orders = $this->getRetailcrmOrders($retailcrm); - $order = $retailcrm_orders->updateOrder($this->order->get_id()); - $order_send = $retailcrm_orders->getOrder(); + $retailcrmOrders = $this->getRetailcrmOrders($retailcrm); + $order = $retailcrmOrders->updateOrder($this->order->get_id()); + $orderData = $retailcrmOrders->getOrder(); if ($retailcrm) { $this->assertInstanceOf('WC_Order', $order); - $this->assertInternalType('array', $order_send); - $this->assertArrayHasKey('status', $order_send); - $this->assertArrayHasKey('externalId', $order_send); - $this->assertArrayHasKey('firstName', $order_send); - $this->assertArrayHasKey('lastName', $order_send); - $this->assertArrayHasKey('email', $order_send); - $this->assertArrayHasKey('delivery', $order_send); - $this->assertArrayHasKey('code', $order_send['delivery']); - $this->assertArrayHasKey('address', $order_send['delivery']); - $this->assertArrayHasKey('index', $order_send['delivery']['address']); - $this->assertArrayHasKey('city', $order_send['delivery']['address']); - $this->assertEquals($this->order->get_id(), $order_send['externalId']); - $this->assertEquals('status1', $order_send['status']); - $this->assertEquals('Jeroen', $order_send['firstName']); - $this->assertEquals('Sormani', $order_send['lastName']); - $this->assertEquals('admin@example.org', $order_send['email']); - $this->assertEquals('US', $order_send['countryIso']); - $this->assertEquals(0, $order_send['discountManualAmount']); - $this->assertEquals(0, $order_send['discountManualPercent']); + $this->assertInternalType('array', $orderData); + $this->assertArrayHasKey('status', $orderData); + $this->assertArrayHasKey('externalId', $orderData); + $this->assertArrayHasKey('firstName', $orderData); + $this->assertArrayHasKey('lastName', $orderData); + $this->assertArrayHasKey('email', $orderData); + $this->assertArrayHasKey('delivery', $orderData); + $this->assertArrayHasKey('code', $orderData['delivery']); + $this->assertArrayHasKey('address', $orderData['delivery']); + $this->assertArrayHasKey('index', $orderData['delivery']['address']); + $this->assertArrayHasKey('city', $orderData['delivery']['address']); + $this->assertEquals($this->order->get_id(), $orderData['externalId']); + $this->assertEquals('status1', $orderData['status']); + $this->assertEquals('Jeroen', $orderData['firstName']); + $this->assertEquals('Sormani', $orderData['lastName']); + $this->assertEquals('admin@example.org', $orderData['email']); + $this->assertEquals('US', $orderData['countryIso']); + $this->assertEquals(0, $orderData['discountManualAmount']); + $this->assertEquals(0, $orderData['discountManualPercent']); - if (mb_strlen($order_send['delivery']['address']['index']) === 6) { - $this->assertEquals('123456', $order_send['delivery']['address']['index']); + if (mb_strlen($orderData['delivery']['address']['index']) === 6) { + $this->assertEquals('123456', $orderData['delivery']['address']['index']); } else { - $this->assertEquals('12345', $order_send['delivery']['address']['index']); + $this->assertEquals('12345', $orderData['delivery']['address']['index']); } - $this->assertEquals('WooCity', $order_send['delivery']['address']['city']); - $this->assertEquals('delivery', $order_send['delivery']['code']); + $this->assertEquals('WooCity', $orderData['delivery']['address']['city']); + $this->assertEquals('delivery', $orderData['delivery']['code']); + + $payment = $retailcrmOrders->getPayment(); - $payment = $retailcrm_orders->getPayment(); $this->assertInternalType('array', $payment); if (!empty($payment)) { @@ -196,6 +254,138 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper } } + public function test_is_corporate_order() + { + $this->createTestOrder(); + $this->order->set_billing_company('Test'); + + $this->assertEquals(true, WC_Retailcrm_Orders::isCorporateOrder($this->order)); + + //Check not corporate order + $this->order->set_billing_company(''); + + $this->assertEquals(false, WC_Retailcrm_Orders::isCorporateOrder($this->order)); + } + + public function test_is_corporate_crm_order() + { + $this->assertEquals( + true, + WC_Retailcrm_Orders::isCorporateCrmOrder( + array( + 'customer' => array( + 'type' => 'customer_corporate' + ) + ) + ) + ); + + //Check not corporate order + $this->assertEquals( + false, + WC_Retailcrm_Orders::isCorporateCrmOrder( + array( + 'customer' => array( + 'type' => 'customer' + ) + ) + ) + ); + } + + public function test_is_order_customer_was_changed() + { + $this->createTestOrder(); + + // First case + $this->order->set_billing_company('Test'); + + $this->assertEquals( + true, + WC_Retailcrm_Orders::isOrderCustomerWasChanged( + $this->order, + array( + 'customer' => array( + 'type' => 'customer' + ) + ) + ) + ); + + // Second case + $this->assertEquals( + true, + WC_Retailcrm_Orders::isOrderCustomerWasChanged( + $this->order, + array( + 'customer' => array( + 'type' => 'customer_corporate' + ), + 'company' => array( + 'name' => 'Test1' + ) + ) + ) + ); + + // Third case + $this->order->set_customer_id(1); + + $this->assertEquals( + true, + WC_Retailcrm_Orders::isOrderCustomerWasChanged( + $this->order, + array( + 'customer' => array( + 'type' => 'customer_corporate', + 'externalId' => 2 + ), + 'company' => array( + 'name' => 'Test' + ) + ) + ) + ); + + // Fourth case + $this->order->set_billing_email('test@mail.es'); + + $this->assertEquals( + true, + WC_Retailcrm_Orders::isOrderCustomerWasChanged( + $this->order, + array( + 'customer' => array( + 'type' => 'customer_corporate', + 'externalId' => 1, + 'email' => 'test1@mail.es' + ), + 'company' => array( + 'name' => 'Test' + ) + ) + ) + ); + + // Customer not changed + $this->assertEquals( + false, + WC_Retailcrm_Orders::isOrderCustomerWasChanged( + $this->order, + array( + 'customer' => array( + 'type' => 'customer_corporate', + 'externalId' => 1, + 'email' => 'test@mail.es' + ), + 'company' => array( + 'name' => 'Test' + ) + ) + ) + ); + } + public function dataProviderUpdateOrder() { $this->setUp(); @@ -275,12 +465,27 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $retailcrm, $this->getOptions(), new WC_Retailcrm_Order_Item($this->getOptions()), - new WC_Retailcrm_Order_Address, + new WC_Retailcrm_Order_Address(), new WC_Retailcrm_Customers( - $retailcrm, $this->getOptions(), new WC_Retailcrm_Customer_Address + $retailcrm, + $this->getOptions(), + new WC_Retailcrm_Customer_Address() ), new WC_Retailcrm_Order($this->getOptions()), new WC_Retailcrm_Order_Payment($this->getOptions()) ); } + + + /** + * @return mixed + */ + private function createResponseMock() + { + return $this->getMockBuilder('\WC_Retailcrm_Response_Helper') + ->disableOriginalConstructor() + ->setMethods(array('isSuccessful')) + ->getMock(); + } } + diff --git a/tests/test-wc-retailcrm-plugin.php b/tests/test-wc-retailcrm-plugin.php index 07ddf1e..8bced9e 100644 --- a/tests/test-wc-retailcrm-plugin.php +++ b/tests/test-wc-retailcrm-plugin.php @@ -1,50 +1,58 @@ responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); + ->disableOriginalConstructor() + ->setMethods(array('isSuccessful')) + ->getMock(); $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') - ->disableOriginalConstructor() - ->setMethods(array( - 'marketplaceSettingsEdit' - )) - ->getMock(); + ->disableOriginalConstructor() + ->setMethods(array('integrationModulesEdit')) + ->getMock(); + + $this->plugin = WC_Retailcrm_Plugin::getInstance($this->path); parent::setUp(); } /** * @param $retailcrm - * @param $response + * @param $responseStatus * * @dataProvider dataProviderIntegrationModule */ - public function test_integration_module($retailcrm, $response) + public function test_integration_module($retailcrm, $responseStatus) { + $this->setMockResponse($this->responseMock, 'isSuccessful', $responseStatus); + + $this->responseMock->setResponse(array('success' => $responseStatus)); + + if ($retailcrm) { + $this->setMockResponse($retailcrm, 'integrationModulesEdit', $this->responseMock); + } + $client_id = uniqid(); $result = WC_Retailcrm_Plugin::integration_module($retailcrm, $client_id); - if (!$retailcrm || $response['success'] == false) { + if (!$retailcrm || !$responseStatus) { $this->assertEquals(false, $result); } else { $this->assertEquals(true, $result); - } + } } public function test_filter_cron_schedules() { - $plugin = WC_Retailcrm_Plugin::getInstance(dirname(__DIR__ . '/../src/retailcrm.php')); - $schedules = $plugin->filter_cron_schedules(array()); + $schedules = $this->plugin->filter_cron_schedules(array()); $this->assertNotEmpty($schedules['five_minutes']); $this->assertEquals(300, $schedules['five_minutes']['interval']); @@ -54,77 +62,68 @@ class WC_Retailcrm_Plugin_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals(900, $schedules['fiveteen_minutes']['interval']); } - private function getResponseData() + public function test_deactivate() { - return array( - "true" => array( - "success" => true - ), - "false" => array( - "success" => false, - "errorMsg" => "Forbidden" - ) - ); + wp_schedule_event(time(), 'three_hours', 'retailcrm_icml'); + wp_schedule_event(time(), 'five_minutes', 'retailcrm_history'); + wp_schedule_event(time(), 'fiveteen_minutes', 'retailcrm_inventories'); + + $this->plugin->deactivate(); + + $this->assertEquals(false, wp_next_scheduled('retailcrm_icml')); + $this->assertEquals(false, wp_next_scheduled('retailcrm_history')); + $this->assertEquals(false, wp_next_scheduled('retailcrm_inventories')); + } + + public function test_register_deactivation_and_activation_hook() + { + global $wp_filter; + + $this->plugin->register_activation_hook(); + $this->plugin->register_deactivation_hook(); + + $actions = array(); + + foreach (array_keys($wp_filter) as $key) { + if (false !== strpos($key, 'retailcrm')) { + if (false !== strpos($key, 'deactivate_')) { + $actions['deactivate'] = $key; + } + + if (false !== strpos($key, 'activate_')) { + $actions['activate'] = $key; + } + } + } + + $this->assertArrayHasKey('deactivate', $actions); + $this->assertNotEmpty($actions['deactivate']); + $this->assertArrayHasKey('activate', $actions); + $this->assertNotEmpty($actions['activate']); } public function dataProviderIntegrationModule() { $this->setUp(); - $responseData = $this->getResponseData(); return array( array( - 'retailcrm' => $this->getApiMock($responseData['true']), - 'response' => $responseData['true'] + 'retailcrm' => $this->apiMock, + 'responseStatus' => true ), array( 'retailcrm' => false, - 'response' => $responseData['true'] + 'responseStatus' => true ), array( - 'retailcrm' => $this->getApiMock($responseData['false']), - 'response' => $responseData['false'] + 'retailcrm' => $this->apiMock, + 'responseStatus' => false ), array( 'retailcrm' => false, - 'response' => $responseData['false'] - ) + 'responseStatus' => false + ), ); } - - private function getApiMock($response) - { - $responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') - ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) - ->getMock(); - - if ($response['success'] == true) { - $responseMock->expects($this->any()) - ->method('isSuccessful') - ->willReturn(true); - } elseif ($response['success'] == false) { - $responseMock->expects($this->any()) - ->method('isSuccessful') - ->willReturn(false); - } - - $responseMock->setResponse($response); - - $apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') - ->disableOriginalConstructor() - ->setMethods(array( - 'integrationModulesEdit' - )) - ->getMock(); - - $apiMock->expects($this->any()) - ->method('integrationModulesEdit') - ->willReturn($responseMock); - - return $apiMock; - } } diff --git a/tests/test-wc-retailcrm-uploader.php b/tests/test-wc-retailcrm-uploader.php index b2e24be..782d3d1 100644 --- a/tests/test-wc-retailcrm-uploader.php +++ b/tests/test-wc-retailcrm-uploader.php @@ -5,14 +5,13 @@ class WC_Retailcrm_Uploader_Test extends WC_Retailcrm_Test_Case_Helper protected $apiMock; protected $responseMock; protected $customer; + private $order; public function setUp() { $this->responseMock = $this->getMockBuilder('\WC_Retailcrm_Response') ->disableOriginalConstructor() - ->setMethods(array( - 'isSuccessful' - )) + ->setMethods(array('isSuccessful')) ->getMock(); $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') @@ -23,17 +22,21 @@ class WC_Retailcrm_Uploader_Test extends WC_Retailcrm_Test_Case_Helper 'uploadArchiveCustomers', 'uploadArchiveOrders', 'getCountUsers', - 'getCountOrders' + 'getCountOrders', + 'customersGet', + 'customersList', + 'ordersCreate' )) ->getMock(); - $this->responseMock->expects($this->any()) - ->method('isSuccessful') - ->willReturn(true); - $this->apiMock->expects($this->any()) - ->method('customersCreate') - ->willReturn($this->responseMock); + $this->setMockResponse($this->responseMock, 'isSuccessful', true); + $this->setMockResponse( + $this->apiMock, + 'customersList', + array('success' => true, 'customers' => array(array('externalId' => 1))) + ); + $this->setMockResponse($this->apiMock, 'customersCreate', $this->responseMock); $this->customer = new WC_Customer(); $this->customer->set_first_name('Tester'); @@ -44,8 +47,9 @@ class WC_Retailcrm_Uploader_Test extends WC_Retailcrm_Test_Case_Helper $this->customer->set_billing_phone('89000000000'); $this->customer->set_date_created(date('Y-m-d H:i:s')); $this->customer->save(); - } + $this->order = WC_Helper_Order::create_order(); + } /** * @param retailcrm @@ -65,7 +69,6 @@ class WC_Retailcrm_Uploader_Test extends WC_Retailcrm_Test_Case_Helper } } - /** * @param $retailcrm * @dataProvider dataProviderApiClient @@ -82,23 +85,21 @@ class WC_Retailcrm_Uploader_Test extends WC_Retailcrm_Test_Case_Helper } } - - /** - * @param retailcrm - * @dataProvider dataProviderApiClient - */ - public function test_get_count_orders_upload($retailcrm) + public function test_get_count_orders_upload() { - $retailcrm_uploader = $this->getRetailcrmUploader($retailcrm); - $data = $retailcrm_uploader->getCountOrders(); + $retailcrm_uploader = $this->getRetailcrmUploader($this->apiMock); + $count_orders = $retailcrm_uploader->getCountOrders(); - if ($retailcrm) { - $this->assertInternalType('int', $data); - } else { - $this->assertEquals(null, $data); - } + $this->assertInternalType('int', $count_orders); } + public function test_get_count_users_upload() + { + $retailcrm_uploader = $this->getRetailcrmUploader($this->apiMock); + $count_users = $retailcrm_uploader->getCountUsers(); + + $this->assertInternalType('int', $count_users); + } public function dataProviderApiClient() { @@ -131,10 +132,8 @@ class WC_Retailcrm_Uploader_Test extends WC_Retailcrm_Test_Case_Helper $retailcrm, $this->getOptions(), new WC_Retailcrm_Order_Item($this->getOptions()), - new WC_Retailcrm_Order_Address, - new WC_Retailcrm_Customers( - $retailcrm, $this->getOptions(), new WC_Retailcrm_Customer_Address - ), + new WC_Retailcrm_Order_Address(), + new WC_Retailcrm_Customers($retailcrm, $this->getOptions(), new WC_Retailcrm_Customer_Address()), new WC_Retailcrm_Order($this->getOptions()), new WC_Retailcrm_Order_Payment($this->getOptions()) ); @@ -142,4 +141,3 @@ class WC_Retailcrm_Uploader_Test extends WC_Retailcrm_Test_Case_Helper return new WC_Retailcrm_Uploader($retailcrm, $order, $customer); } } -