1
0
mirror of synced 2025-02-21 01:13:13 +03:00

Update old tests, add new tests

This commit is contained in:
Dima Uryvskiy 2021-12-20 11:41:41 +03:00 committed by GitHub
parent b0577a18fb
commit 6132172da5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
45 changed files with 4019 additions and 2805 deletions

View File

@ -464,6 +464,7 @@ jobs:
php --version
composer --version
phpunit --version
- name: Install Woocommerce
env:
WP_VERSION: ${{ matrix.wp }}

View File

@ -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

View File

@ -1 +1 @@
4.3.8
4.4.0

View File

@ -21,6 +21,8 @@ abstract class WC_Retailcrm_Abstract_Builder implements WC_Retailcrm_Builder_Int
/**
* @return array|mixed
*
* @codeCoverageIgnore
*/
public function getData()
{

View File

@ -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);
}
}
}

View File

@ -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
)
);
}
}
}

View File

@ -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
*

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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']]);

View File

@ -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;

View File

@ -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;

View File

@ -1,16 +1,17 @@
<?php
class WC_Retailcrm_Plugin {
class WC_Retailcrm_Plugin
{
public $file;
public static $history_run = false;
private static $instance = null;
const MARKETPLACE_LOGO = 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5b69ce4bda663-woocommercesvg2.svg';
const INTEGRATION_CODE = 'woocommerce';
public static function getInstance($file) {
public static function getInstance($file)
{
if (self::$instance === null) {
self::$instance = new self($file);
}
@ -18,13 +19,20 @@ class WC_Retailcrm_Plugin {
return self::$instance;
}
private function __construct($file) {
/**
* @param $file
*
* @codeCoverageIgnore
*/
private function __construct($file)
{
$this->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
*

View File

@ -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)
{

View File

@ -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());

View File

@ -10,6 +10,8 @@
if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')):
/**
* Class WC_Retailcrm_Logger
*
* @codeCoverageIgnore
*/
class WC_Retailcrm_Logger
{

View File

@ -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;
}
}

View File

@ -235,3 +235,4 @@ class WC_Retailcrm_WC_Customer_Builder extends WC_Retailcrm_Abstract_Builder
return true;
}
}

View File

@ -1,10 +1,13 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
if (! defined('ABSPATH')) {
exit; // Exit if accessed directly
}
function get_wc_shipping_methods_by_zones($enhanced = false) {
// @codeCoverageIgnoreStart
// TODO: There is a task to analyze the work
function get_wc_shipping_methods_by_zones($enhanced = false)
{
$result = array();
$shippingZones = WC_Shipping_Zones::get_zones();
@ -51,8 +54,10 @@ function get_wc_shipping_methods_by_zones($enhanced = false) {
return $result;
}
// @codeCoverageIgnoreEnd
function get_wc_shipping_methods() {
function get_wc_shipping_methods()
{
$wc_shipping = WC_Shipping::instance();
$shipping_methods = $wc_shipping->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'
);
}

View File

@ -176,3 +176,4 @@ class WC_Retailcrm_Customer_Switcher_State
}
}
}

View File

@ -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

View File

@ -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

View File

@ -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'));
}
/**

View File

@ -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

View File

@ -0,0 +1,52 @@
<?php
class WC_Retailcrm_Abstracts_Settings_Test extends WC_Retailcrm_Test_Case_Helper
{
/**
* @var WC_Retailcrm_Base
*/
protected $base;
public function setUp()
{
$this->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()
)
);
}
}

View File

@ -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();

View File

@ -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
);
}
}

View File

@ -0,0 +1,82 @@
<?php
namespace datasets;
class DataBaseRetailCrm
{
public static function getResponseStatuses()
{
return array(
'success' => 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
)
)
);
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace datasets;
class DataCustomersRetailCrm
{
public static function getCustomerAddress()
{
return array(
'success' => true,
'addresses' => array (
'id' => 3503,
'index' => 144566,
'countryIso' => 'ES',
'region' => 'Region',
'city' => 'City',
'text' => 'street Test 777',
)
);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,25 @@
<?php
namespace datasets;
class DataInventoriesRetailCrm {
public static function getResponseData()
{
return array(
'success' => true,
'pagination' => array(
'limit' => 250,
'totalCount' => 1,
'currentPage' => 1,
'totalPageCount' => 1
),
'offers' => array(
array(
'id' => 1,
'xmlId' => 'xmlId',
'quantity' => 10
)
)
);
}
}

View File

@ -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);
}
}

View File

@ -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());
}
}

View File

@ -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();
}
}

View File

@ -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']);
}
}
}

View File

@ -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
*

View File

@ -0,0 +1,60 @@
<?php
/**
* PHP version 5.3
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @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']);
}
}

View File

@ -1,5 +1,7 @@
<?php
use datasets\DataBaseRetailCrm;
class WC_Retailcrm_Base_Test extends WC_Retailcrm_Test_Case_Helper
{
protected $apiMock;
@ -7,20 +9,22 @@ class WC_Retailcrm_Base_Test extends WC_Retailcrm_Test_Case_Helper
protected $responseMockDeliveryTypes;
protected $responseMockPaymentTypes;
protected $responseMockStatuses;
private $unit;
protected $dataOptions;
private $baseRetailcrm;
public function setUp()
{
$this->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);
}
}

View File

@ -1,5 +1,7 @@
<?php
use datasets\DataCustomersRetailCrm;
class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper
{
protected $apiMock;
@ -8,32 +10,43 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper
public function setUp()
{
$this->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()

View File

@ -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
)

File diff suppressed because it is too large Load Diff

View File

@ -1,26 +1,25 @@
<?php
use datasets\DataInventoriesRetailCrm;
class WC_Retailcrm_Inventories_Test extends WC_Retailcrm_Test_Case_Helper
{
protected $apiMock;
protected $responseMock;
protected $offer;
public function setUp()
{
$this->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
)
);
}

View File

@ -1,6 +1,6 @@
<?php
class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper
class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper
{
protected $apiMock;
protected $order;
@ -9,23 +9,33 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper
public function setUp()
{
$this->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();
}
}

View File

@ -1,50 +1,58 @@
<?php
class WC_Retailcrm_Plugin_Test extends WC_Retailcrm_Test_Case_Helper
{
protected $apiMock;
protected $responseMock;
protected $plugin;
private $path = __DIR__ . '/src/retailcrm.php';
public function setUp()
{
$this->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;
}
}

View File

@ -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);
}
}