From 6701bc03bbe8482978e44cb2a79c6605442f3550 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Thu, 27 Sep 2018 16:46:04 +0300 Subject: [PATCH] Tests --- .travis.yml | 2 +- RoboFile.php | 4 +- VERSION | 2 +- composer.json | 3 + phpunit.xml.dist | 7 +- .../controller/extension/module/retailcrm.php | 12 +- .../model/extension/retailcrm/customer.php | 137 -------- .../admin/model/extension/retailcrm/order.php | 8 +- .../model/extension/retailcrm/prices.php | 20 +- .../extension/analytics/daemon_collector.php | 10 +- .../model/extension/retailcrm/customer.php | 104 ------ src/upload/system/library/retailcrm/base.php | 151 ++++----- src/upload/system/library/retailcrm/order.php | 94 ++++++ .../system/library/retailcrm/retailcrm.php | 1 + tests/admin/ControllerRetailcrmAdminTest.php | 33 +- tests/admin/ModelRetailcrmOrderAdminTest.php | 185 +---------- tests/admin/ModelRetailcrmPricesAdminTest.php | 28 +- .../admin/ModelRetailcrmProductsAdminTest.php | 14 + .../ControllerRetailcrmApiCatalogTest.php | 13 +- .../ModelRetailcrmCustomerCatalogTest.php | 62 ---- .../ModelRetailcrmOrderCatalogTest.php | 191 +---------- .../ModelRetailcrmProductsCatalogTest.php | 14 + .../CustomerRetailcrmLibraryTest.php} | 38 +-- tests/library/OrderRetailcrmLibraryTest.php | 306 ++++++++++++++++++ tests/opencart_sample_data.sql | 3 +- tests/test-config.php | 70 ++++ 26 files changed, 685 insertions(+), 827 deletions(-) delete mode 100644 src/upload/admin/model/extension/retailcrm/customer.php delete mode 100644 src/upload/catalog/model/extension/retailcrm/customer.php create mode 100644 tests/admin/ModelRetailcrmProductsAdminTest.php delete mode 100644 tests/catalog/ModelRetailcrmCustomerCatalogTest.php create mode 100644 tests/catalog/ModelRetailcrmProductsCatalogTest.php rename tests/{admin/ModelRetailcrmCustomerAdminTest.php => library/CustomerRetailcrmLibraryTest.php} (51%) create mode 100644 tests/library/OrderRetailcrmLibraryTest.php create mode 100644 tests/test-config.php diff --git a/.travis.yml b/.travis.yml index c98c779..0c9dcb0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,4 +39,4 @@ deploy: on: php: 7.1 branch: master - condition: "$DEPLOY = true" \ No newline at end of file + condition: "$DEPLOY = false" \ No newline at end of file diff --git a/RoboFile.php b/RoboFile.php index 861f1b1..d8b74cf 100644 --- a/RoboFile.php +++ b/RoboFile.php @@ -73,10 +73,12 @@ class RoboFile extends \Robo\Tasks $this->taskDeleteDir('www')->run(); $this->taskFileSystemStack() ->mirror('vendor/opencart/opencart/upload', 'www') - ->copy('vendor/beyondit/opencart-test-suite/src/upload/system/config/test-config.php', 'www/system/config/test-config.php') + ->copy('tests/test-config.php', 'www/system/config/test-config.php') ->copy('vendor/beyondit/opencart-test-suite/src/upload/system/library/session/test.php', 'www/system/library/session/test.php') ->copy('vendor/beyondit/opencart-test-suite/src/upload/admin/controller/startup/test_startup.php','www/admin/controller/startup/test_startup.php') ->chmod('www', 0777, 0000, true) + ->rename('www/config-dist.php', 'www/config.php') + ->rename('www/admin/config-dist.php', 'www/admin/config.php') ->run(); // Create new database, drop if exists already diff --git a/VERSION b/VERSION index 50e47c8..826cf5d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.1.1 \ No newline at end of file +3.1.0-alpha1 \ No newline at end of file diff --git a/composer.json b/composer.json index a83a6af..9387249 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ "email": "integration@retailcrm.ru" } ], + "version": "3.1.0-alpha1", "require-dev": { "opencart/opencart" : "3.1.0.0_a1", "vlucas/phpdotenv": "~1.1.0", @@ -25,9 +26,11 @@ "opencart-dir" : "www" }, "scripts" : { + "test-library": "bin/phpunit --testsuite library-tests --colors=always", "test-admin": "bin/phpunit --testsuite admin-tests --colors=always", "test-catalog": "bin/phpunit --testsuite catalog-tests --colors=always", "test": [ + "@test-library", "@test-admin", "@test-catalog" ], diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 90272e8..2f79810 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -6,7 +6,7 @@ convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" - processIsolation="false" + processIsolation="true" stopOnFailure="false" syntaxCheck="false"> @@ -16,8 +16,13 @@ ./tests/admin/ + + ./tests/library/ + + + \ No newline at end of file diff --git a/src/upload/admin/controller/extension/module/retailcrm.php b/src/upload/admin/controller/extension/module/retailcrm.php index f64d151..e72bcd3 100644 --- a/src/upload/admin/controller/extension/module/retailcrm.php +++ b/src/upload/admin/controller/extension/module/retailcrm.php @@ -540,10 +540,18 @@ class ControllerExtensionModuleRetailcrm extends Controller { if (file_exists(DIR_APPLICATION . 'model/extension/retailcrm/custom/prices.php')) { $this->load->model('extension/retailcrm/custom/prices'); - $this->model_extension_retailcrm_custom_prices->uploadPrices($products, $this->retailcrm->getApiClient()); + $this->model_extension_retailcrm_custom_prices->uploadPrices( + $products, + $this->retailcrm->getApiClient(), + $this->retailcrm + ); } else { $this->load->model('extension/retailcrm/prices'); - $this->model_extension_retailcrm_prices->uploadPrices($products, $this->retailcrm->getApiClient()); + $this->model_extension_retailcrm_prices->uploadPrices( + $products, + $this->retailcrm->getApiClient(), + $this->retailcrm + ); } } diff --git a/src/upload/admin/model/extension/retailcrm/customer.php b/src/upload/admin/model/extension/retailcrm/customer.php deleted file mode 100644 index 1702e9a..0000000 --- a/src/upload/admin/model/extension/retailcrm/customer.php +++ /dev/null @@ -1,137 +0,0 @@ -load->model('setting/setting'); - $this->load->library('retailcrm/retailcrm'); - - $this->moduleTitle = $this->retailcrm->getModuleTitle(); - $this->settings = $this->model_setting_setting->getSetting($this->moduleTitle); - } - - /** - * Upload customers - * - * @param array $customers - * @param \RetailcrmProxy $retailcrmApiClient - * - * @return mixed - */ - public function uploadToCrm($customers, $retailcrmApiClient) - { - if ($retailcrmApiClient === false || empty($customers)) { - return false; - } - - $customersToCrm = array(); - - foreach($customers as $customer) { - $customersToCrm[] = $this->process($customer); - } - - $chunkedCustomers = array_chunk($customersToCrm, 50); - - foreach($chunkedCustomers as $customersPart) { - $retailcrmApiClient->customersUpload($customersPart); - } - - return $chunkedCustomers; - } - - /** - * Edit customer - * - * @param array $customer - * @param \RetailcrmProxy $retailcrmApiClient - * - * @return mixed - */ - public function changeInCrm($customer, $retailcrmApiClient) - { - if ($retailcrmApiClient === false || empty($customer)) { - return false; - } - - $customerToCrm = $this->process($customer); - - $retailcrmApiClient->customersEdit($customerToCrm); - - return $customerToCrm; - } - - /** - * Create customer - * - * @param array $customer - * @param \RetailcrmProxy $retailcrmApiClient - * - * @return mixed - */ - public function sendToCrm($customer, $retailcrmApiClient) - { - if ($retailcrmApiClient === false || empty($customer)) { - return false; - } - - $customerToCrm = $this->process($customer); - - $retailcrmApiClient->customersCreate($customerToCrm); - - return $customerToCrm; - } - - /** - * Process customer - * - * @param array $customer - * - * @return array $customerToCrm - */ - private function process($customer) - { - $customerToCrm = array( - 'externalId' => $customer['customer_id'], - 'firstName' => $customer['firstname'], - 'lastName' => $customer['lastname'], - 'email' => $customer['email'], - 'phones' => array( - array( - 'number' => $customer['telephone'] - ) - ), - 'createdAt' => $customer['date_added'] - ); - - if (isset($customer['address'])) { - $customerToCrm['address'] = array( - 'index' => $customer['address']['postcode'], - 'countryIso' => $customer['address']['iso_code_2'], - 'region' => $customer['address']['zone'], - 'city' => $customer['address']['city'], - 'text' => $customer['address']['address_1'] . ' ' . $customer['address']['address_2'] - ); - } - - if (isset($this->settings[$this->moduleTitle . '_custom_field']) && $customer['custom_field']) { - $customFields = json_decode($customer['custom_field']); - - foreach ($customFields as $key => $value) { - if (isset($this->settings[$this->moduleTitle . '_custom_field']['c_' . $key])) { - $customFieldsToCrm[$this->settings[$this->moduleTitle . '_custom_field']['c_' . $key]] = $value; - } - } - - if (isset($customFieldsToCrm)) { - $customerToCrm['customFields'] = $customFieldsToCrm; - } - } - - return $customerToCrm; - } -} diff --git a/src/upload/admin/model/extension/retailcrm/order.php b/src/upload/admin/model/extension/retailcrm/order.php index d065c66..7b1135f 100644 --- a/src/upload/admin/model/extension/retailcrm/order.php +++ b/src/upload/admin/model/extension/retailcrm/order.php @@ -98,13 +98,7 @@ class ModelExtensionRetailcrmOrder extends Model */ protected function totalTitles() { - if (version_compare(VERSION, '3.0', '<')) { - $title = ''; - } else { - $title = 'total_'; - } - - return $title; + return 'total_'; } /** diff --git a/src/upload/admin/model/extension/retailcrm/prices.php b/src/upload/admin/model/extension/retailcrm/prices.php index 4d56a35..c784184 100644 --- a/src/upload/admin/model/extension/retailcrm/prices.php +++ b/src/upload/admin/model/extension/retailcrm/prices.php @@ -3,7 +3,6 @@ class ModelExtensionRetailcrmPrices extends Model { protected $settings; - protected $moduleTitle; private $options; private $optionValues; @@ -13,14 +12,16 @@ class ModelExtensionRetailcrmPrices extends Model * * @param array $products * @param \RetailcrmProxy $retailcrm_api_client + * @param \Retailcrm\Retailcrm $retailcrm + * * @return mixed bool | array */ - public function uploadPrices($products, $retailcrm_api_client) + public function uploadPrices($products, $retailcrm_api_client, $retailcrm) { $this->load->model('catalog/option'); $this->load->model('setting/setting'); - $prices = $this->getPrices($products, $retailcrm_api_client); + $prices = $this->getPrices($products, $retailcrm_api_client, $retailcrm); if ($retailcrm_api_client === false || !$prices) { return false; @@ -39,18 +40,19 @@ class ModelExtensionRetailcrmPrices extends Model * Get prices * * @param array $products - * + * @param \RetailcrmProxy $retailcrm_api_client + * @param \Retailcrm\Retailcrm $retailcrm * @return mixed */ - protected function getPrices($products, $retailcrm_api_client) + protected function getPrices($products, $retailcrm_api_client, $retailcrm) { $settings = $this->model_setting_setting->getSetting(\retailcrm\Retailcrm::MODULE); $prices = array(); $site = $this->getSite($retailcrm_api_client); - if (!isset($settings[$this->moduleTitle . '_special']) - || $settings[$this->moduleTitle . '_apiversion'] == 'v3' + if (!isset($settings[\Retailcrm\Retailcrm::MODULE . '_special']) + || $settings[\Retailcrm\Retailcrm::MODULE . '_apiversion'] == 'v3' ) { return false; } @@ -70,7 +72,7 @@ class ModelExtensionRetailcrmPrices extends Model } } - $offers = $this->retailcrm->getOffers($product); + $offers = $retailcrm->getOffers($product); foreach ($offers as $optionsString => $optionsValues) { $optionsString = explode('_', $optionsString); @@ -105,7 +107,7 @@ class ModelExtensionRetailcrmPrices extends Model 'site' => $site, 'prices' => array( array( - 'code' => $settings[$this->moduleTitle . '_special'], + 'code' => $settings[\Retailcrm\Retailcrm::MODULE . '_special'], 'price' => $productPrice + $optionsValues['price'] ) ) diff --git a/src/upload/catalog/controller/extension/analytics/daemon_collector.php b/src/upload/catalog/controller/extension/analytics/daemon_collector.php index 170cfd9..80e6767 100644 --- a/src/upload/catalog/controller/extension/analytics/daemon_collector.php +++ b/src/upload/catalog/controller/extension/analytics/daemon_collector.php @@ -2,15 +2,13 @@ class ControllerExtensionAnalyticsDaemonCollector extends Controller { public function index() { $this->load->model('setting/setting'); - $this->load->library('retailcrm/retailcrm'); - $moduleTitle = $this->retailcrm->getModuleTitle(); - $settings = $this->model_setting_setting->getSetting($moduleTitle); - $setting = $settings[$moduleTitle . '_collector']; + $settings = $this->model_setting_setting->getSetting(\Retailcrm\Retailcrm::MODULE); + $setting = $settings[\Retailcrm\Retailcrm::MODULE . '_collector']; $siteCode = isset($setting['site_key']) ? $setting['site_key'] : ''; if ($this->customer->isLogged()) $customerId = $this->customer->getID(); - + $customer = isset($customerId) ? "'customerId': '" . $customerId . "'" : ""; $labelPromo = !empty($setting['label_promo']) ? $setting['label_promo'] : null; $labelSend = !empty($setting['label_send']) ? $setting['label_send'] : null; @@ -54,7 +52,7 @@ class ControllerExtensionAnalyticsDaemonCollector extends Controller { 'period': " . $settings[$moduleTitle . '_collector']['period'] . ", " . $customForm . " });"; - } + } } elseif ($labelPromo != null || $labelSend != null) { $captureForm = "_rc('require', 'capture-form', { " . $customForm . " diff --git a/src/upload/catalog/model/extension/retailcrm/customer.php b/src/upload/catalog/model/extension/retailcrm/customer.php deleted file mode 100644 index fd586e0..0000000 --- a/src/upload/catalog/model/extension/retailcrm/customer.php +++ /dev/null @@ -1,104 +0,0 @@ -load->model('setting/setting'); - $this->load->library('retailcrm/retailcrm'); - - $this->moduleTitle = $this->retailcrm->getModuleTitle(); - $this->settings = $this->model_setting_setting->getSetting($this->moduleTitle); - } - - /** - * Create customer - * - * @param array $customer - * - * @return mixed - */ - public function sendToCrm($customer, $retailcrmApiClient) - { - if (empty($customer) || $retailcrmApiClient === false) { - return false; - } - - $customerToCrm = $this->process($customer); - - $retailcrmApiClient->customersCreate($customerToCrm); - - return $customerToCrm; - } - - /** - * Edit customer - * - * @param array $customer - * - * @return mixed - */ - public function changeInCrm($customer, $retailcrmApiClient) - { - if (empty($customer) || $retailcrmApiClient === false) { - return false; - } - - $customerToCrm = $this->process($customer); - - $retailcrmApiClient->customersEdit($customerToCrm); - - return $customerToCrm; - } - - /** - * Process customer - * - * @param array $customer - * - * @return array $customerToCrm - */ - private function process($customer) { - $customerToCrm = array( - 'externalId' => $customer['customer_id'], - 'firstName' => $customer['firstname'], - 'lastName' => $customer['lastname'], - 'email' => $customer['email'], - 'phones' => array( - array( - 'number' => $customer['telephone'] - ) - ), - 'createdAt' => $customer['date_added'] - ); - - if (isset($customer['address'])) { - $customerToCrm['address'] = array( - 'index' => $customer['address']['postcode'], - 'countryIso' => $customer['address']['iso_code_2'], - 'region' => $customer['address']['zone'], - 'city' => $customer['address']['city'], - 'text' => $customer['address']['address_1'] . ' ' . $customer['address']['address_2'] - ); - } - - if (isset($this->settings[$this->moduleTitle . '_custom_field']) && $customer['custom_field']) { - $customFields = json_decode($customer['custom_field']); - - foreach ($customFields as $key => $value) { - if (isset($this->settings[$this->moduleTitle . '_custom_field']['c_' . $key])) { - $customFieldsToCrm[$this->settings[$this->moduleTitle . '_custom_field']['c_' . $key]] = $value; - } - } - - if (isset($customFieldsToCrm)) { - $customerToCrm['customFields'] = $customFieldsToCrm; - } - } - - return $customerToCrm; - } -} diff --git a/src/upload/system/library/retailcrm/base.php b/src/upload/system/library/retailcrm/base.php index 623334f..de0cadd 100644 --- a/src/upload/system/library/retailcrm/base.php +++ b/src/upload/system/library/retailcrm/base.php @@ -7,6 +7,24 @@ abstract class Base protected $registry; protected $data = array(); + /** + * Send to crm + * + * @param $retailcrm_api_client + * + * @return mixed + */ + abstract public function create($retailcrm_api_client); + + /** + * Edit in crm + * + * @param $retailcrm_api_client + * + * @return mixed + */ + abstract public function edit($retailcrm_api_client); + /** * Base constructor. * @@ -26,6 +44,13 @@ abstract class Base return $this->registry->get($name); } + /** + * @return array + */ + public function getData() { + return $this->data; + } + /** * @param $field * @param $value @@ -61,6 +86,60 @@ abstract class Base } } + /** + * Prepare data array + * + * @param array $data + * + * @return void + */ + public function prepare($data) + { + unset($data); + $this->data = Retailcrm::filterRecursive($this->data); + } + + /** + * Upload to CRM + * + * @param $retailcrm_api_client + * @param array $data + * @param string $method + * + * @return boolean + */ + public function upload($retailcrm_api_client, $data = array(), $method = 'orders') + { + if (!$data) { + return false; + } + + $upload = array(); + $countOrders = count($data); + $countIterations = (int) ($countOrders / 50); + + foreach ($data as $key => $entity) { + $this->prepare($entity); + $upload[] = $this->data; + $this->resetData(); + + if ($countIterations > 0) { + unset($data[$key]); + } + + if (($countIterations == 0 && count($data) == count($upload)) + || count($upload) == 50 + ) { + /** @var \RetailcrmApiClient5 $retailcrm_api_client */ + $retailcrm_api_client->{$method . 'Upload'}($upload); + $upload = array(); + $countIterations--; + } + } + + return true; + } + /** * @param array $custom_fields * @param array $setting @@ -111,78 +190,6 @@ abstract class Base return $result; } - /** - * Prepare data array - * - * @param array $data - * - * @return void - */ - public function prepare($data) - { - unset($data); - Retailcrm::filterRecursive($this->data); - } - - /** - * Send to crm - * - * @param $retailcrm_api_client - * - * @return mixed - */ - abstract public function create($retailcrm_api_client); - - /** - * Edit in crm - * - * @param $retailcrm_api_client - * - * @return mixed - */ - abstract public function edit($retailcrm_api_client); - - /** - * Upload to CRM - * - * @param $retailcrm_api_client - * @param array $data - * @param string $method - * - * @return boolean - */ - public function upload($retailcrm_api_client, $data = array(), $method = 'orders') - { - if (!$data) { - return false; - } - - $upload = array(); - $countOrders = count($data); - $countIterations = (int) ($countOrders / 50); - - foreach ($data as $key => $entity) { - $this->prepare($entity); - $upload[] = $this->data; - $this->resetData(); - - if ($countIterations > 0) { - unset($data[$key]); - } - - if (($countIterations == 0 && count($data) == count($upload)) - || count($upload) == 50 - ) { - /** @var \RetailcrmApiClient5 $retailcrm_api_client */ - $retailcrm_api_client->{$method . 'Upload'}($upload); - $upload = array(); - $countIterations--; - } - } - - return true; - } - /** * Reset data on default */ diff --git a/src/upload/system/library/retailcrm/order.php b/src/upload/system/library/retailcrm/order.php index d295174..8f4dab5 100644 --- a/src/upload/system/library/retailcrm/order.php +++ b/src/upload/system/library/retailcrm/order.php @@ -7,6 +7,7 @@ class Order extends Base protected $registry; protected $data = array( 'number' => 0, + 'createdAt' => null, 'countryIso' => null, 'externalId' => 0, 'customer' => array(), @@ -17,6 +18,7 @@ class Order extends Base 'email' => null, 'phone' => null, 'discountManualAmount' => 0, + 'customerComment' => null, 'items' => array(), 'delivery' => array(), 'customFields' => array(), @@ -141,6 +143,15 @@ class Order extends Base $this->setDataArray($custom_fields, 'customFields'); } } + + if ($order['customer_id']) { + $this->setDataArray( + array( + 'externalId' => $order['customer_id'] + ), + 'customer' + ); + } } parent::prepare($order); @@ -156,6 +167,19 @@ class Order extends Base return false; } + if (!$this->data['customer']) { + $customer = $this->searchCustomer($this->data['phone'], $this->data['email'], $retailcrm_api_client); + + if ($customer) { + $this->setDataArray( + array( + 'id' => $customer['id'] + ), + 'customer' + ); + } + } + $response = $retailcrm_api_client->ordersCreate($this->data); return $response; @@ -171,11 +195,81 @@ class Order extends Base return false; } + $order_payment = reset($this->data['payments']); + unset($this->data['payments']); + $response = $retailcrm_api_client->ordersEdit($this->data); + if ($response->isSuccessful()) { + $this->updatePayment($order_payment, $this->data['externalId'], $retailcrm_api_client); + } + return $response; } + /** + * Update payment in CRM + * + * @param array $order_payment + * @param int $order_id + * @param \RetailcrmProxy $retailcrm_api_client + * + * @return boolean + */ + private function updatePayment($order_payment, $order_id, $retailcrm_api_client) { + $response_order = $retailcrm_api_client->ordersGet($order_id); + + if (!$response_order->isSuccessful()) { + return false; + } + + $order_info = $response_order['order']; + + foreach ($order_info['payments'] as $payment_data) { + if (isset($payment_data['externalId']) && $payment_data['externalId'] == $order_id) { + $payment = $payment_data; + } + } + + if (isset($payment) && $payment['type'] != $order_payment['type']) { + $response = $retailcrm_api_client->ordersPaymentDelete($payment['id']); + if ($response->isSuccessful()) { + $retailcrm_api_client->ordersPaymentCreate($order_payment); + } + } elseif (isset($payment) && $payment['type'] == $order_payment['type']) { + $retailcrm_api_client->ordersPaymentEdit($order_payment); + } + + return true; + } + + /** + * @param $phone + * @param $email + * @param $retailcrm_api_client + * + * @return array|mixed + */ + private function searchCustomer($phone, $email, $retailcrm_api_client) { + $customer = array(); + $response = $retailcrm_api_client->customersList( + array( + 'name' => $phone, + 'email' => $email + ), + 1, + 100 + ); + + if ($response->isSuccessful() && isset($response['customers'])) { + $customers = $response['customers']; + if ($customers) { + $customer = end($customers); + } + } + return $customer; + } + /** * @param string $shipping_code * @param array $deliveries diff --git a/src/upload/system/library/retailcrm/retailcrm.php b/src/upload/system/library/retailcrm/retailcrm.php index 33772c9..8edc9a0 100644 --- a/src/upload/system/library/retailcrm/retailcrm.php +++ b/src/upload/system/library/retailcrm/retailcrm.php @@ -168,6 +168,7 @@ class Retailcrm { $haystack[$key] = trim($value); } } + return $haystack; } } diff --git a/tests/admin/ControllerRetailcrmAdminTest.php b/tests/admin/ControllerRetailcrmAdminTest.php index 6be635d..88121ab 100644 --- a/tests/admin/ControllerRetailcrmAdminTest.php +++ b/tests/admin/ControllerRetailcrmAdminTest.php @@ -1,15 +1,19 @@ db->query("SELECT permission from ".DB_PREFIX."user_group WHERE name = 'Administrator'"); + parent::setUp(); + + $query = $this->db->query("SELECT permission from " . DB_PREFIX . "user_group WHERE name = 'Administrator'"); $permissions = json_decode($query->row['permission'],true); - if (!in_array('extension/module/retailcrm',$permissions['access'])) { + if (!in_array('extension/module/retailcrm', $permissions['access'])) { $permissions['access'][] = 'extension/module/retailcrm'; $this->db->query("UPDATE ".DB_PREFIX."user_group SET permission='".$this->db->escape(json_encode($permissions))."' WHERE name = 'Administrator'"); } @@ -22,36 +26,15 @@ class ControllerRetailcrmAdminTest extends OpenCartTest public function testIndex() { $this->login('admin', 'admin'); - $response = $this->dispatchAction('extension/module/retailcrm'); + $this->assertRegExp('/Connection settings/', $response->getOutput()); } public function testIcml() { $this->login('admin', 'admin'); - - $response = $this->dispatchAction('extension/module/retailcrm/icml'); - - $this->assertRegExp('/Connection settings/', $response->getOutput()); + $this->dispatchAction('extension/module/retailcrm/icml'); $this->assertFileExists(DIR_SYSTEM . '../' . 'retailcrm.xml'); } - - public function testInstallCollector() - { - $this->login('admin', 'admin'); - - $response = $this->dispatchAction('extension/module/retailcrm/install_collector'); - - $this->assertRegExp('/Connection settings/', $response->getOutput()); - } - - public function testUnnstallCollector() - { - $this->login('admin', 'admin'); - - $response = $this->dispatchAction('extension/module/retailcrm/uninstall_collector'); - - $this->assertRegExp('/Connection settings/', $response->getOutput()); - } } diff --git a/tests/admin/ModelRetailcrmOrderAdminTest.php b/tests/admin/ModelRetailcrmOrderAdminTest.php index fa2ffaf..edec29c 100644 --- a/tests/admin/ModelRetailcrmOrderAdminTest.php +++ b/tests/admin/ModelRetailcrmOrderAdminTest.php @@ -1,187 +1,12 @@ orderModel = $this->loadModel('extension/retailcrm/order'); - - $this->apiClientMock = $this->getMockBuilder(\RetailcrmProxy::class) - ->disableOriginalConstructor() - ->setMethods(array( - 'ordersUpload', - 'customersList', - 'ordersCreate', - 'ordersPaymentCreate', - 'customersCreate' - )) - ->getMock(); - - $this->settingModel = $this->loadModel('setting/setting'); - $this->retailcrm = new \retailcrm\retailcrm(self::$registry); - - $this->settingModel->editSetting( - $this->retailcrm->getModuleTitle(), - array( - $this->retailcrm->getModuleTitle() . '_apiversion' => 'v5', - $this->retailcrm->getModuleTitle() . '_order_number' => 1, - $this->retailcrm->getModuleTitle() . '_status' => array( - 1 => 'new' - ), - $this->retailcrm->getModuleTitle() . '_delivery' => array( - 'flat.flat' => 'flat' - ), - $this->retailcrm->getModuleTitle() . '_payment' => array( - 'cod' => 'cod' - ) - ) - ); - } - - public function testUploadToCrm() - { - $saleOrderModel = $this->loadModel('sale/order'); - $orders = $saleOrderModel->getOrders(); - $fullOrders = array(); - - foreach ($orders as $order) { - $fullOrder = $saleOrderModel->getOrder($order['order_id']); - - $fullOrder['order_total'] = $saleOrderModel->getOrderTotals($order['order_id']); - $fullOrder['products'] = $saleOrderModel->getOrderProducts($order['order_id']); - - foreach($fullOrder['products'] as $key => $product) { - $fullOrder['products'][$key]['option'] = $saleOrderModel->getOrderOptions($product['order_id'], $product['order_product_id']); - } - - $fullOrders[] = $fullOrder; - } - - $chunkedOrders = $this->orderModel->uploadToCrm($fullOrders, $this->apiClientMock); - - $order = $chunkedOrders[0][0]; - - $this->assertInternalType('array', $chunkedOrders); - $this->assertInternalType('array', $chunkedOrders[0]); - $this->assertNotEmpty($chunkedOrders[0]); - $this->assertArrayHasKey('externalId', $order); - $this->assertArrayHasKey('number', $order); - $this->assertArrayHasKey('firstName', $order); - $this->assertArrayHasKey('lastName', $order); - $this->assertArrayHasKey('email', $order); - $this->assertArrayHasKey('phone', $order); - $this->assertArrayHasKey('createdAt', $order); - $this->assertArrayHasKey('delivery', $order); - $this->assertArrayHasKey('status', $order); - $this->assertArrayHasKey('items', $order); - $this->assertArrayHasKey('payments', $order); - $this->assertNotEmpty($order['payments']); - } - - public function testUploadWithCustomerTest() - { - $saleOrderModel = $this->loadModel('sale/order'); - $order = $saleOrderModel->getOrder(self::ORDER_WITH_CUST_ID); - - $order['totals'] = $saleOrderModel->getOrderTotals($order['order_id']); - $order['products'] = $saleOrderModel->getOrderProducts($order['order_id']); - - foreach($order['products'] as $key => $product) { - $order['products'][$key]['option'] = $saleOrderModel->getOrderOptions($product['order_id'], $product['order_product_id']); - } - - $response = new \RetailcrmApiResponse( - 201, - json_encode( - array( - 'success' => true, - 'id' => 1 - ) - ) - ); - - $this->apiClientMock->expects($this->any())->method('ordersCreate')->willReturn($response); - $orderSend = $this->orderModel->uploadOrder($order, $this->apiClientMock); - - $this->assertArrayHasKey('status', $orderSend); - $this->assertEquals('new', $orderSend['status']); - $this->assertArrayHasKey('externalId', $orderSend); - $this->assertArrayHasKey('number', $orderSend); - $this->assertArrayHasKey('firstName', $orderSend); - $this->assertEquals('Test', $orderSend['firstName']); - $this->assertArrayHasKey('lastName', $orderSend); - $this->assertEquals('Test', $orderSend['lastName']); - $this->assertArrayHasKey('email', $orderSend); - $this->assertEquals('test@mail.ru', $orderSend['email']); - $this->assertArrayHasKey('phone', $orderSend); - $this->assertEquals('+7 (000) 000-00-00', $orderSend['phone']); - $this->assertArrayHasKey('createdAt', $orderSend); - $this->assertArrayHasKey('delivery', $orderSend); - $this->assertInternalType('array', $orderSend['delivery']); - $this->assertEquals('flat', $orderSend['delivery']['code']); - $this->assertEquals('Test', $orderSend['delivery']['address']['city']); - $this->assertEquals('Rostov-na-Donu', $orderSend['delivery']['address']['region']); - $this->assertEquals('111111', $orderSend['delivery']['address']['index']); - $this->assertArrayHasKey('items', $orderSend); - $this->assertArrayHasKey('customerComment', $orderSend); - $this->assertArrayHasKey('customer', $orderSend); - $this->assertArrayHasKey('externalId', $orderSend['customer']); - $this->assertEquals(self::CUSTOMER_ID, $orderSend['customer']['externalId']); - $this->assertArrayHasKey('payments', $orderSend); - $this->assertEquals('cod', $orderSend['payments'][0]['type']); - $this->assertNotEmpty($orderSend['payments']); - } - - public function testUploadWithoutCustomerTest() - { - $saleOrderModel = $this->loadModel('sale/order'); - $order = $saleOrderModel->getOrder(self::ORDER_ID); - - $order['totals'] = $saleOrderModel->getOrderTotals($order['order_id']); - $order['products'] = $saleOrderModel->getOrderProducts($order['order_id']); - - foreach($order['products'] as $key => $product) { - $order['products'][$key]['option'] = $saleOrderModel->getOrderOptions($product['order_id'], $product['order_product_id']); - } - - $response = new \RetailcrmApiResponse( - 201, - json_encode( - array( - 'success' => true, - 'id' => 1 - ) - ) - ); - - $this->apiClientMock->expects($this->any())->method('ordersCreate')->willReturn($response); - $orderSend = $this->orderModel->uploadOrder($order, $this->apiClientMock); - - $this->assertArrayHasKey('status', $orderSend); - $this->assertArrayHasKey('externalId', $orderSend); - $this->assertArrayHasKey('number', $orderSend); - $this->assertArrayHasKey('firstName', $orderSend); - $this->assertArrayHasKey('lastName', $orderSend); - $this->assertArrayHasKey('email', $orderSend); - $this->assertArrayHasKey('phone', $orderSend); - $this->assertArrayHasKey('createdAt', $orderSend); - $this->assertArrayHasKey('delivery', $orderSend); - $this->assertArrayHasKey('items', $orderSend); - $this->assertContains('#', $orderSend['items'][0]['offer']['externalId']); - $this->assertArrayHasKey('payments', $orderSend); - $this->assertArrayHasKey('customerComment', $orderSend); - $this->assertArrayNotHasKey('customer', $orderSend); - $this->assertNotEmpty($orderSend['payments']); + $this->assertTrue(true); + // todo test } } diff --git a/tests/admin/ModelRetailcrmPricesAdminTest.php b/tests/admin/ModelRetailcrmPricesAdminTest.php index a9b4b6d..073c539 100644 --- a/tests/admin/ModelRetailcrmPricesAdminTest.php +++ b/tests/admin/ModelRetailcrmPricesAdminTest.php @@ -1,5 +1,7 @@ getMock(); $this->settingModel = $this->loadModel('setting/setting'); - $this->retailcrm = new \retailcrm\retailcrm(self::$registry); + $this->retailcrm = new \Retailcrm\Retailcrm(self::$registry); $this->settingModel->editSetting( - $this->retailcrm->getModuleTitle(), + \Retailcrm\Retailcrm::MODULE, array( - $this->retailcrm->getModuleTitle() . '_apiversion' => 'v5', - $this->retailcrm->getModuleTitle() . '_special' => 'special' + \Retailcrm\Retailcrm::MODULE . '_apiversion' => 'v5', + \Retailcrm\Retailcrm::MODULE . '_special' => 'special' ) ); } public function testUploadPrices() { + $response = new \RetailcrmApiResponse(200, json_encode($this->getSites())); + $this->apiClientMock->expects($this->any())->method('sitesList')->willReturn($response); + $productModel = $this->loadModel('catalog/product'); $products = $productModel->getProducts(); - $prices = $this->pricesModel->uploadPrices($products, $this->apiClientMock); + $prices = $this->pricesModel->uploadPrices($products, $this->apiClientMock, $this->retailcrm); $price = $prices[0][0]; $this->assertInternalType('array', $prices); @@ -45,7 +50,20 @@ class ModelRetailcrmPricesAdminTest extends OpenCartTest $this->assertInternalType('array', $price); $this->assertArrayHasKey('externalId', $price); $this->assertArrayHasKey('site', $price); + $this->assertEquals('test_site', $price['site']); $this->assertArrayHasKey('prices', $price); $this->assertInternalType('array', $price['prices']); } + + private function getSites() + { + return array( + 'success' => true, + 'sites' => array( + array( + 'code' => 'test_site' + ) + ) + ); + } } \ No newline at end of file diff --git a/tests/admin/ModelRetailcrmProductsAdminTest.php b/tests/admin/ModelRetailcrmProductsAdminTest.php new file mode 100644 index 0000000..ee5b523 --- /dev/null +++ b/tests/admin/ModelRetailcrmProductsAdminTest.php @@ -0,0 +1,14 @@ +loadModel('extension/retailcrm/products'); + $options = $model->getProductOptions(42); + + $this->assertNotEmpty($options); + } +} diff --git a/tests/catalog/ControllerRetailcrmApiCatalogTest.php b/tests/catalog/ControllerRetailcrmApiCatalogTest.php index 798e8af..b617301 100644 --- a/tests/catalog/ControllerRetailcrmApiCatalogTest.php +++ b/tests/catalog/ControllerRetailcrmApiCatalogTest.php @@ -1,9 +1,12 @@ db->query("SELECT * FROM `" . DB_PREFIX . "api` WHERE api_id = 1"); $api = $query->row; $this->apiKey = $api['key']; - $this->retailcrm = new \retailcrm\retailcrm(self::$registry); + $this->username = $api['username']; + $this->retailcrm = new \Retailcrm\Retailcrm(self::$registry); $this->setSetting( - $this->retailcrm->getModuleTitle(), + \Retailcrm\Retailcrm::MODULE, array( - $this->retailcrm->getModuleTitle() . '_country' => array(1), + \Retailcrm\Retailcrm::MODULE . '_country' => array(1), ) ); @@ -36,6 +40,8 @@ class ControllerRetailcrmApiCatalogTest extends OpenCartTest $this->assertEquals('Not found api key', $data->error); $this->request->get['key'] = $this->apiKey; + $this->request->get['username'] = $this->username; + $response = $this->dispatchAction('api/retailcrm/getDeliveryTypes'); $data = json_decode($response->getOutput()); @@ -50,6 +56,7 @@ class ControllerRetailcrmApiCatalogTest extends OpenCartTest $this->assertEquals('Not found api key', $data->error); $this->request->get['key'] = $this->apiKey; + $this->request->get['username'] = $this->username; $response = $this->dispatchAction('api/retailcrm/addOrderHistory'); $data = json_decode($response->getOutput()); diff --git a/tests/catalog/ModelRetailcrmCustomerCatalogTest.php b/tests/catalog/ModelRetailcrmCustomerCatalogTest.php deleted file mode 100644 index 9101a0a..0000000 --- a/tests/catalog/ModelRetailcrmCustomerCatalogTest.php +++ /dev/null @@ -1,62 +0,0 @@ -customerModel = $this->loadModel('extension/retailcrm/customer'); - - $this->apiClientMock = $this->getMockBuilder(\RetailcrmProxy::class) - ->disableOriginalConstructor() - ->setMethods(array( - 'customersCreate', - 'customersEdit' - )) - ->getMock(); - } - - public function testSendToCrm() - { - $customerModel = $this->loadModel('account/customer'); - $customer = $customerModel->getCustomer(self::CUSTOMER_ID); - - $customerSend = $this->customerModel->sendToCrm($customer, $this->apiClientMock); - - $this->assertArrayHasKey('externalId', $customerSend); - $this->assertEquals(self::CUSTOMER_ID, $customerSend['externalId']); - $this->assertArrayHasKey('firstName', $customerSend); - $this->assertEquals('Test', $customerSend['firstName']); - $this->assertArrayHasKey('lastName', $customerSend); - $this->assertEquals('Test', $customerSend['lastName']); - $this->assertArrayHasKey('email', $customerSend); - $this->assertEquals('test@mail.ru', $customerSend['email']); - $this->assertArrayHasKey('phones', $customerSend); - $this->assertEquals('+7 (000) 000-00-00', $customerSend['phones'][0]['number']); - } - - public function testChangeInCrm() - { - $customerModel = $this->loadModel('account/customer'); - $customer = $customerModel->getCustomer(self::CUSTOMER_ID); - - $customerSend = $this->customerModel->changeInCrm($customer, $this->apiClientMock); - - $this->assertArrayHasKey('externalId', $customerSend); - $this->assertEquals(self::CUSTOMER_ID, $customerSend['externalId']); - $this->assertArrayHasKey('firstName', $customerSend); - $this->assertEquals('Test', $customerSend['firstName']); - $this->assertArrayHasKey('lastName', $customerSend); - $this->assertEquals('Test', $customerSend['lastName']); - $this->assertArrayHasKey('email', $customerSend); - $this->assertEquals('test@mail.ru', $customerSend['email']); - $this->assertArrayHasKey('phones', $customerSend); - $this->assertEquals('+7 (000) 000-00-00', $customerSend['phones'][0]['number']); - } -} \ No newline at end of file diff --git a/tests/catalog/ModelRetailcrmOrderCatalogTest.php b/tests/catalog/ModelRetailcrmOrderCatalogTest.php index 4f199e3..bf45735 100644 --- a/tests/catalog/ModelRetailcrmOrderCatalogTest.php +++ b/tests/catalog/ModelRetailcrmOrderCatalogTest.php @@ -1,192 +1,15 @@ loadModel('extension/retailcrm/order'); + $status_id = $model->getOrderStatusId(1); - $this->orderModel = $this->loadModel('extension/retailcrm/order'); - - $this->apiClientMock = $this->getMockBuilder(\RetailcrmProxy::class) - ->disableOriginalConstructor() - ->setMethods(array( - 'ordersCreate', - 'ordersEdit', - 'ordersGet', - 'ordersPaymentEdit', - 'customersList' - )) - ->getMock(); - - $this->retailcrm = new \retailcrm\retailcrm(self::$registry); - - $this->setSetting( - $this->retailcrm->getModuleTitle(), - array( - $this->retailcrm->getModuleTitle() . '_apiversion' => 'v5', - $this->retailcrm->getModuleTitle() . '_order_number' => 1, - $this->retailcrm->getModuleTitle() . '_status' => array( - 1 => 'new' - ), - $this->retailcrm->getModuleTitle() . '_delivery' => array( - 'flat.flat' => 'flat' - ), - $this->retailcrm->getModuleTitle() . '_payment' => array( - 'cod' => 'cod' - ) - ) - ); - } - - public function testCreateOrderWithCustomer() - { - $orderCheckoutModel = $this->loadModel('checkout/order'); - $orderAccountModel = $this->loadModel('account/order'); - $order_id = self::ORDER_WITH_CUST_ID; - $order = $orderCheckoutModel->getOrder($order_id); - $order['products'] = $orderAccountModel->getOrderProducts($order_id); - $order['totals'] = $orderAccountModel->getOrderTotals($order_id); - - foreach ($order['products'] as $key => $product) { - $productOptions = $orderAccountModel->getOrderOptions($order_id, $product['order_product_id']); - - if (!empty($productOptions)) { - $order['products'][$key]['option'] = $productOptions; - } - } - - $orderProcess = $this->orderModel->processOrder($order); - $orderSend = $this->orderModel->sendToCrm($orderProcess, $this->apiClientMock); - - $this->assertArrayHasKey('status', $orderSend); - $this->assertEquals('new', $orderSend['status']); - $this->assertArrayHasKey('externalId', $orderSend); - $this->assertArrayHasKey('number', $orderSend); - $this->assertArrayHasKey('firstName', $orderSend); - $this->assertEquals('Test', $orderSend['firstName']); - $this->assertArrayHasKey('lastName', $orderSend); - $this->assertEquals('Test', $orderSend['lastName']); - $this->assertArrayHasKey('email', $orderSend); - $this->assertEquals('test@mail.ru', $orderSend['email']); - $this->assertArrayHasKey('phone', $orderSend); - $this->assertEquals('+7 (000) 000-00-00', $orderSend['phone']); - $this->assertArrayHasKey('createdAt', $orderSend); - $this->assertArrayHasKey('delivery', $orderSend); - $this->assertInternalType('array', $orderSend['delivery']); - $this->assertEquals('flat', $orderSend['delivery']['code']); - $this->assertEquals('Test', $orderSend['delivery']['address']['city']); - $this->assertEquals('Rostov-na-Donu', $orderSend['delivery']['address']['region']); - $this->assertEquals('111111', $orderSend['delivery']['address']['index']); - $this->assertArrayHasKey('items', $orderSend); - $this->assertArrayHasKey('customerComment', $orderSend); - $this->assertArrayHasKey('customer', $orderSend); - $this->assertArrayHasKey('externalId', $orderSend['customer']); - $this->assertEquals(self::CUSTOMER_ID, $orderSend['customer']['externalId']); - $this->assertArrayHasKey('payments', $orderSend); - $this->assertEquals('cod', $orderSend['payments'][0]['type']); - $this->assertNotEmpty($orderSend['payments']); - } - - public function testEditOrderWithCustomer() - { - $orderCheckoutModel = $this->loadModel('checkout/order'); - $orderAccountModel = $this->loadModel('account/order'); - $order_id = self::ORDER_WITH_CUST_ID; - $order = $orderCheckoutModel->getOrder($order_id); - $order['products'] = $orderAccountModel->getOrderProducts($order_id); - $order['totals'] = $orderAccountModel->getOrderTotals($order_id); - - foreach ($order['products'] as $key => $product) { - $productOptions = $orderAccountModel->getOrderOptions($order_id, $product['order_product_id']); - - if (!empty($productOptions)) { - $order['products'][$key]['option'] = $productOptions; - } - } - - $orderEditResponse = new \RetailcrmApiResponse( - 200, - json_encode( - array( - 'success' => true, - 'id' => 1 - ) - ) - ); - - $ordersGetResponse = new \RetailcrmApiResponse( - 200, - json_encode( - array( - 'success' => true, - 'order' => $this->getOrder($order_id) - ) - ) - ); - - $this->apiClientMock->expects($this->any())->method('ordersEdit')->willReturn($orderEditResponse); - $this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn($ordersGetResponse); - $orderProcess = $this->orderModel->processOrder($order); - $orderSend = $this->orderModel->sendToCrm($orderProcess, $this->apiClientMock, false); - - $this->assertArrayHasKey('status', $orderSend); - $this->assertEquals('new', $orderSend['status']); - $this->assertArrayHasKey('externalId', $orderSend); - $this->assertArrayHasKey('number', $orderSend); - $this->assertArrayHasKey('firstName', $orderSend); - $this->assertEquals('Test', $orderSend['firstName']); - $this->assertArrayHasKey('lastName', $orderSend); - $this->assertEquals('Test', $orderSend['lastName']); - $this->assertArrayHasKey('email', $orderSend); - $this->assertEquals('test@mail.ru', $orderSend['email']); - $this->assertArrayHasKey('phone', $orderSend); - $this->assertEquals('+7 (000) 000-00-00', $orderSend['phone']); - $this->assertArrayHasKey('createdAt', $orderSend); - $this->assertArrayHasKey('delivery', $orderSend); - $this->assertInternalType('array', $orderSend['delivery']); - $this->assertEquals('flat', $orderSend['delivery']['code']); - $this->assertEquals('Test', $orderSend['delivery']['address']['city']); - $this->assertEquals('Rostov-na-Donu', $orderSend['delivery']['address']['region']); - $this->assertEquals('111111', $orderSend['delivery']['address']['index']); - $this->assertArrayHasKey('items', $orderSend); - $this->assertArrayHasKey('customerComment', $orderSend); - } - - protected function setSetting($code, $data, $store_id = 0) { - $this->db->query("DELETE FROM `" . DB_PREFIX . "setting` WHERE store_id = '" . (int)$store_id . "' AND `code` = '" . $this->db->escape($code) . "'"); - - foreach ($data as $key => $value) { - if (substr($key, 0, strlen($code)) == $code) { - if (!is_array($value)) { - $this->db->query("INSERT INTO " . DB_PREFIX . "setting SET store_id = '" . (int)$store_id . "', `code` = '" . $this->db->escape($code) . "', `key` = '" . $this->db->escape($key) . "', `value` = '" . $this->db->escape($value) . "'"); - } else { - $this->db->query("INSERT INTO " . DB_PREFIX . "setting SET store_id = '" . (int)$store_id . "', `code` = '" . $this->db->escape($code) . "', `key` = '" . $this->db->escape($key) . "', `value` = '" . $this->db->escape(json_encode($value, true)) . "', serialized = '1'"); - } - } - } - } - - private function getOrder($id) - { - return array( - 'payments' => array( - array( - 'id' => 1, - 'status' => 'not-paid', - 'type' => 'cod', - 'externalId' => $id, - 'amount' => '100' - ) - ) - ); + $this->assertNotEmpty($status_id); + $this->assertNotNull($status_id); } } diff --git a/tests/catalog/ModelRetailcrmProductsCatalogTest.php b/tests/catalog/ModelRetailcrmProductsCatalogTest.php new file mode 100644 index 0000000..e8f2903 --- /dev/null +++ b/tests/catalog/ModelRetailcrmProductsCatalogTest.php @@ -0,0 +1,14 @@ +loadModel('extension/retailcrm/products'); + $options = $model->getProductOptions(42); + + $this->assertNotEmpty($options); + } +} \ No newline at end of file diff --git a/tests/admin/ModelRetailcrmCustomerAdminTest.php b/tests/library/CustomerRetailcrmLibraryTest.php similarity index 51% rename from tests/admin/ModelRetailcrmCustomerAdminTest.php rename to tests/library/CustomerRetailcrmLibraryTest.php index 80bf169..1cd062d 100644 --- a/tests/admin/ModelRetailcrmCustomerAdminTest.php +++ b/tests/library/CustomerRetailcrmLibraryTest.php @@ -1,8 +1,10 @@ customerModel = $this->loadModel('extension/retailcrm/customer'); - $this->apiClientMock = $this->getMockBuilder(\RetailcrmProxy::class) ->disableOriginalConstructor() ->setMethods(array( - 'customersUpload', + 'customersCreate', 'customersEdit' )) ->getMock(); + + $retailcrm = new \Retailcrm\Retailcrm(self::$registry); + $this->customer = $retailcrm->createObject(\Retailcrm\Customer::class); } - public function testUploadToCrm() + public function testPrepareCustomer() { - $customerModel = $this->loadModel('customer/customer'); - $customers = $customerModel->getCustomers(); - - $customersSend = $this->customerModel->uploadToCrm($customers, $this->apiClientMock); - $customer = $customersSend[0][0]; - - $this->assertInternalType('array', $customersSend); - $this->assertInternalType('array', $customersSend[0]); - $this->assertArrayHasKey('externalId', $customer); - $this->assertArrayHasKey('firstName', $customer); - $this->assertArrayHasKey('lastName', $customer); - $this->assertArrayHasKey('email', $customer); - } - - public function testChangeInCrm() - { - $customerModel = $this->loadModel('customer/customer'); + $customerModel = $this->loadModel('account/customer'); $customer = $customerModel->getCustomer(self::CUSTOMER_ID); - $customerSend = $this->customerModel->changeInCrm($customer, $this->apiClientMock); + $this->customer->prepare($customer); + $customerSend = $this->customer->getData(); $this->assertArrayHasKey('externalId', $customerSend); $this->assertEquals(self::CUSTOMER_ID, $customerSend['externalId']); @@ -56,4 +44,4 @@ class ModelRetailcrmCustomerAdminTest extends OpenCartTest $this->assertArrayHasKey('phones', $customerSend); $this->assertEquals('+7 (000) 000-00-00', $customerSend['phones'][0]['number']); } -} +} \ No newline at end of file diff --git a/tests/library/OrderRetailcrmLibraryTest.php b/tests/library/OrderRetailcrmLibraryTest.php new file mode 100644 index 0000000..6050696 --- /dev/null +++ b/tests/library/OrderRetailcrmLibraryTest.php @@ -0,0 +1,306 @@ +apiClientMock = $this->getMockBuilder(\RetailcrmProxy::class) + ->disableOriginalConstructor() + ->setMethods(array( + 'ordersCreate', + 'ordersEdit', + 'ordersGet', + 'ordersPaymentEdit', + 'customersList' + )) + ->getMock(); + + $this->retailcrm = new \Retailcrm\Retailcrm(self::$registry); + + $this->order = $this->retailcrm->createObject(\Retailcrm\Order::class); + + $this->setSetting( + \Retailcrm\Retailcrm::MODULE, + array( + \Retailcrm\Retailcrm::MODULE . '_apiversion' => 'v5', + \Retailcrm\Retailcrm::MODULE . '_order_number' => 1, + \Retailcrm\Retailcrm::MODULE . '_status' => array( + 1 => 'new' + ), + \Retailcrm\Retailcrm::MODULE . '_delivery' => array( + 'flat.flat' => 'flat' + ), + \Retailcrm\Retailcrm::MODULE . '_payment' => array( + 'cod' => 'cod' + ) + ) + ); + } + + public function testPrepareCreateOrderWithCustomer() + { + $orderCheckoutModel = $this->loadModel('checkout/order'); + $orderAccountModel = $this->loadModel('account/order'); + $order_id = self::ORDER_WITH_CUST_ID; + $order = $orderCheckoutModel->getOrder($order_id); + $order['products'] = $orderAccountModel->getOrderProducts($order_id); + $order['totals'] = $orderAccountModel->getOrderTotals($order_id); + + foreach ($order['products'] as $key => $product) { + $productOptions = $orderAccountModel->getOrderOptions($order_id, $product['order_product_id']); + + if (!empty($productOptions)) { + $order['products'][$key]['option'] = $productOptions; + } + } + + $this->order->prepare($order); + $orderPrepare = $this->order->getData(); + + $this->assertArrayHasKey('status', $orderPrepare); + $this->assertEquals('new', $orderPrepare['status']); + $this->assertArrayHasKey('externalId', $orderPrepare); + $this->assertArrayHasKey('number', $orderPrepare); + $this->assertArrayHasKey('firstName', $orderPrepare); + $this->assertEquals('Test', $orderPrepare['firstName']); + $this->assertArrayHasKey('lastName', $orderPrepare); + $this->assertEquals('Test', $orderPrepare['lastName']); + $this->assertArrayHasKey('email', $orderPrepare); + $this->assertEquals('test@mail.ru', $orderPrepare['email']); + $this->assertArrayHasKey('phone', $orderPrepare); + $this->assertEquals('+7 (000) 000-00-00', $orderPrepare['phone']); + $this->assertArrayHasKey('createdAt', $orderPrepare); + $this->assertArrayHasKey('delivery', $orderPrepare); + $this->assertInternalType('array', $orderPrepare['delivery']); + $this->assertEquals('flat', $orderPrepare['delivery']['code']); + $this->assertEquals('Test', $orderPrepare['delivery']['address']['city']); + $this->assertEquals('Rostov-na-Donu', $orderPrepare['delivery']['address']['region']); + $this->assertEquals('111111', $orderPrepare['delivery']['address']['index']); + $this->assertArrayHasKey('items', $orderPrepare); + $this->assertArrayHasKey('customer', $orderPrepare); + $this->assertArrayHasKey('externalId', $orderPrepare['customer']); + $this->assertEquals(self::CUSTOMER_ID, $orderPrepare['customer']['externalId']); + $this->assertArrayHasKey('payments', $orderPrepare); + $this->assertEquals('cod', $orderPrepare['payments'][0]['type']); + $this->assertNotEmpty($orderPrepare['payments']); + } + + public function testPrepareEditOrderWithCustomer() + { + $orderCheckoutModel = $this->loadModel('checkout/order'); + $orderAccountModel = $this->loadModel('account/order'); + $order_id = self::ORDER_WITH_CUST_ID; + $order = $orderCheckoutModel->getOrder($order_id); + $order['products'] = $orderAccountModel->getOrderProducts($order_id); + $order['totals'] = $orderAccountModel->getOrderTotals($order_id); + + foreach ($order['products'] as $key => $product) { + $productOptions = $orderAccountModel->getOrderOptions($order_id, $product['order_product_id']); + + if (!empty($productOptions)) { + $order['products'][$key]['option'] = $productOptions; + } + } + + $orderEditResponse = new \RetailcrmApiResponse( + 200, + json_encode( + array( + 'success' => true, + 'id' => 1 + ) + ) + ); + + $ordersGetResponse = new \RetailcrmApiResponse( + 200, + json_encode( + array( + 'success' => true, + 'order' => $this->getOrder($order_id) + ) + ) + ); + + $this->apiClientMock->expects($this->any())->method('ordersEdit')->willReturn($orderEditResponse); + $this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn($ordersGetResponse); + $this->order->prepare($order); + $orderPrepare = $this->order->getData(); + + $this->assertArrayHasKey('status', $orderPrepare); + $this->assertEquals('new', $orderPrepare['status']); + $this->assertArrayHasKey('externalId', $orderPrepare); + $this->assertArrayHasKey('number', $orderPrepare); + $this->assertArrayHasKey('firstName', $orderPrepare); + $this->assertEquals('Test', $orderPrepare['firstName']); + $this->assertArrayHasKey('lastName', $orderPrepare); + $this->assertEquals('Test', $orderPrepare['lastName']); + $this->assertArrayHasKey('email', $orderPrepare); + $this->assertEquals('test@mail.ru', $orderPrepare['email']); + $this->assertArrayHasKey('phone', $orderPrepare); + $this->assertEquals('+7 (000) 000-00-00', $orderPrepare['phone']); + $this->assertArrayHasKey('createdAt', $orderPrepare); + $this->assertArrayHasKey('delivery', $orderPrepare); + $this->assertInternalType('array', $orderPrepare['delivery']); + $this->assertEquals('flat', $orderPrepare['delivery']['code']); + $this->assertEquals('Test', $orderPrepare['delivery']['address']['city']); + $this->assertEquals('Rostov-na-Donu', $orderPrepare['delivery']['address']['region']); + $this->assertEquals('111111', $orderPrepare['delivery']['address']['index']); + $this->assertArrayHasKey('items', $orderPrepare); + $this->assertArrayHasKey('customer', $orderPrepare); + $this->assertArrayHasKey('externalId', $orderPrepare['customer']); + $this->assertEquals(self::CUSTOMER_ID, $orderPrepare['customer']['externalId']); + } + + public function testPrepareCreateOrderWithoutCustomer() + { + $orderCheckoutModel = $this->loadModel('checkout/order'); + $orderAccountModel = $this->loadModel('account/order'); + $order_id = self::ORDER_ID; + $order = $orderCheckoutModel->getOrder($order_id); + $order['products'] = $orderAccountModel->getOrderProducts($order_id); + $order['totals'] = $orderAccountModel->getOrderTotals($order_id); + + foreach ($order['products'] as $key => $product) { + $productOptions = $orderAccountModel->getOrderOptions($order_id, $product['order_product_id']); + + if (!empty($productOptions)) { + $order['products'][$key]['option'] = $productOptions; + } + } + + $this->order->prepare($order); + $orderPrepare = $this->order->getData(); + + $this->assertArrayHasKey('status', $orderPrepare); + $this->assertEquals('new', $orderPrepare['status']); + $this->assertArrayHasKey('externalId', $orderPrepare); + $this->assertArrayHasKey('number', $orderPrepare); + $this->assertArrayHasKey('firstName', $orderPrepare); + $this->assertEquals('Test', $orderPrepare['firstName']); + $this->assertArrayHasKey('lastName', $orderPrepare); + $this->assertEquals('Test', $orderPrepare['lastName']); + $this->assertArrayHasKey('email', $orderPrepare); + $this->assertEquals('test@mail.ru', $orderPrepare['email']); + $this->assertArrayHasKey('phone', $orderPrepare); + $this->assertEquals('+7 (000) 000-00-00', $orderPrepare['phone']); + $this->assertArrayHasKey('createdAt', $orderPrepare); + $this->assertArrayHasKey('delivery', $orderPrepare); + $this->assertInternalType('array', $orderPrepare['delivery']); + $this->assertEquals('flat', $orderPrepare['delivery']['code']); + $this->assertEquals('Test', $orderPrepare['delivery']['address']['city']); + $this->assertEquals('Rostov-na-Donu', $orderPrepare['delivery']['address']['region']); + $this->assertEquals('111111', $orderPrepare['delivery']['address']['index']); + $this->assertArrayHasKey('items', $orderPrepare); + $this->assertArrayNotHasKey('customer', $orderPrepare); + $this->assertArrayHasKey('payments', $orderPrepare); + $this->assertEquals('cod', $orderPrepare['payments'][0]['type']); + $this->assertNotEmpty($orderPrepare['payments']); + } + + public function testPrepareEditOrderWithoutCustomer() + { + $orderCheckoutModel = $this->loadModel('checkout/order'); + $orderAccountModel = $this->loadModel('account/order'); + $order_id = self::ORDER_ID; + $order = $orderCheckoutModel->getOrder($order_id); + $order['products'] = $orderAccountModel->getOrderProducts($order_id); + $order['totals'] = $orderAccountModel->getOrderTotals($order_id); + + foreach ($order['products'] as $key => $product) { + $productOptions = $orderAccountModel->getOrderOptions($order_id, $product['order_product_id']); + + if (!empty($productOptions)) { + $order['products'][$key]['option'] = $productOptions; + } + } + + $orderEditResponse = new \RetailcrmApiResponse( + 200, + json_encode( + array( + 'success' => true, + 'id' => 1 + ) + ) + ); + + $ordersGetResponse = new \RetailcrmApiResponse( + 200, + json_encode( + array( + 'success' => true, + 'order' => $this->getOrder($order_id) + ) + ) + ); + + $this->apiClientMock->expects($this->any())->method('ordersEdit')->willReturn($orderEditResponse); + $this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn($ordersGetResponse); + $this->order->prepare($order); + $orderPrepare = $this->order->getData(); + + $this->assertArrayHasKey('status', $orderPrepare); + $this->assertEquals('new', $orderPrepare['status']); + $this->assertArrayHasKey('externalId', $orderPrepare); + $this->assertArrayHasKey('number', $orderPrepare); + $this->assertArrayHasKey('firstName', $orderPrepare); + $this->assertEquals('Test', $orderPrepare['firstName']); + $this->assertArrayHasKey('lastName', $orderPrepare); + $this->assertEquals('Test', $orderPrepare['lastName']); + $this->assertArrayHasKey('email', $orderPrepare); + $this->assertEquals('test@mail.ru', $orderPrepare['email']); + $this->assertArrayHasKey('phone', $orderPrepare); + $this->assertEquals('+7 (000) 000-00-00', $orderPrepare['phone']); + $this->assertArrayHasKey('createdAt', $orderPrepare); + $this->assertArrayHasKey('delivery', $orderPrepare); + $this->assertInternalType('array', $orderPrepare['delivery']); + $this->assertEquals('flat', $orderPrepare['delivery']['code']); + $this->assertEquals('Test', $orderPrepare['delivery']['address']['city']); + $this->assertEquals('Rostov-na-Donu', $orderPrepare['delivery']['address']['region']); + $this->assertEquals('111111', $orderPrepare['delivery']['address']['index']); + $this->assertArrayHasKey('items', $orderPrepare); + $this->assertArrayNotHasKey('customer', $orderPrepare); + } + + protected function setSetting($code, $data, $store_id = 0) { + $this->db->query("DELETE FROM `" . DB_PREFIX . "setting` WHERE store_id = '" . (int)$store_id . "' AND `code` = '" . $this->db->escape($code) . "'"); + + foreach ($data as $key => $value) { + if (substr($key, 0, strlen($code)) == $code) { + if (!is_array($value)) { + $this->db->query("INSERT INTO " . DB_PREFIX . "setting SET store_id = '" . (int)$store_id . "', `code` = '" . $this->db->escape($code) . "', `key` = '" . $this->db->escape($key) . "', `value` = '" . $this->db->escape($value) . "'"); + } else { + $this->db->query("INSERT INTO " . DB_PREFIX . "setting SET store_id = '" . (int)$store_id . "', `code` = '" . $this->db->escape($code) . "', `key` = '" . $this->db->escape($key) . "', `value` = '" . $this->db->escape(json_encode($value, true)) . "', serialized = '1'"); + } + } + } + } + + private function getOrder($id) + { + return array( + 'payments' => array( + array( + 'id' => 1, + 'status' => 'not-paid', + 'type' => 'cod', + 'externalId' => $id, + 'amount' => '100' + ) + ) + ); + } +} \ No newline at end of file diff --git a/tests/opencart_sample_data.sql b/tests/opencart_sample_data.sql index cb4812d..20718b6 100644 --- a/tests/opencart_sample_data.sql +++ b/tests/opencart_sample_data.sql @@ -1,5 +1,5 @@ TRUNCATE TABLE `oc_customer`; -INSERT INTO `oc_customer` (`customer_id`, `customer_group_id`, `store_id`, `language_id`, `firstname`, `lastname`, `email`, `telephone`, `fax`, `password`, `salt`, `cart`, `wishlist`, `newsletter`, `address_id`, `custom_field`, `ip`, `status`, `approved`, `safe`, `token`, `code`, `date_added`) VALUES ('1', '1', '0', '1', 'Test', 'Test', 'test@mail.ru', '+7 (000) 000-00-00', '', 'ed3798da75d6cdd695e99e87a60d587a10aa95ff', '51TalnrgH', '', '', '0', '1', '', '172.21.0.1', '1', '1', '0', '', '', '2018-06-07 13:50:08'); +INSERT INTO `oc_customer` (`customer_id`, `customer_group_id`, `store_id`, `language_id`, `firstname`, `lastname`, `email`, `telephone`, `fax`, `password`, `salt`, `cart`, `wishlist`, `newsletter`, `address_id`, `custom_field`, `ip`, `status`, `safe`, `token`, `code`, `date_added`) VALUES ('1', '1', '0', '1', 'Test', 'Test', 'test@mail.ru', '+7 (000) 000-00-00', '', 'ed3798da75d6cdd695e99e87a60d587a10aa95ff', '51TalnrgH', '', '', '0', '1', '', '172.21.0.1', '1', '0', '', '', '2018-06-07 13:50:08'); TRUNCATE TABLE `oc_customer_activity`; TRUNCATE TABLE `oc_customer_group`; @@ -23,7 +23,6 @@ TRUNCATE TABLE `oc_order`; INSERT INTO `oc_order` (`order_id`, `invoice_no`, `invoice_prefix`, `store_id`, `store_name`, `store_url`, `customer_id`, `customer_group_id`, `firstname`, `lastname`, `email`, `telephone`, `fax`, `custom_field`, `payment_firstname`, `payment_lastname`, `payment_company`, `payment_address_1`, `payment_address_2`, `payment_city`, `payment_postcode`, `payment_country`, `payment_country_id`, `payment_zone`, `payment_zone_id`, `payment_address_format`, `payment_custom_field`, `payment_method`, `payment_code`, `shipping_firstname`, `shipping_lastname`, `shipping_company`, `shipping_address_1`, `shipping_address_2`, `shipping_city`, `shipping_postcode`, `shipping_country`, `shipping_country_id`, `shipping_zone`, `shipping_zone_id`, `shipping_address_format`, `shipping_custom_field`, `shipping_method`, `shipping_code`, `comment`, `total`, `order_status_id`, `affiliate_id`, `commission`, `marketing_id`, `tracking`, `language_id`, `currency_id`, `currency_code`, `currency_value`, `ip`, `forwarded_ip`, `user_agent`, `accept_language`, `date_added`, `date_modified`) VALUES ('1', '0', 'INV-2016-00', '0', 'Opencart', 'http://localhost:8000/', '1', '1', 'Test', 'Test', 'test@mail.ru', '+7 (000) 000-00-00', '', '', 'Test', 'Test', '', 'Address', 'Address 2', 'Test', '111111', 'Russian Federation', '176', 'Rostov-na-Donu', '99', '', '[]', 'Cash on delivery', 'cod', 'Test', 'Test', '', 'Address', 'Address 2', 'Test', '111111', 'Russian Federation', '176', 'Rostov-na-Donu', '99', '', '[]', 'Flat Rate', 'flat.flat', '', '106.0000', '1', '0', '0.0000', '0', '', '1', '1', 'USD', '1.00000000', '172.21.0.1', '', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36', 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7', '2018-06-07 13:51:10', '2018-06-07 13:51:23'); INSERT INTO `oc_order` (`order_id`, `invoice_no`, `invoice_prefix`, `store_id`, `store_name`, `store_url`, `customer_id`, `customer_group_id`, `firstname`, `lastname`, `email`, `telephone`, `fax`, `custom_field`, `payment_firstname`, `payment_lastname`, `payment_company`, `payment_address_1`, `payment_address_2`, `payment_city`, `payment_postcode`, `payment_country`, `payment_country_id`, `payment_zone`, `payment_zone_id`, `payment_address_format`, `payment_custom_field`, `payment_method`, `payment_code`, `shipping_firstname`, `shipping_lastname`, `shipping_company`, `shipping_address_1`, `shipping_address_2`, `shipping_city`, `shipping_postcode`, `shipping_country`, `shipping_country_id`, `shipping_zone`, `shipping_zone_id`, `shipping_address_format`, `shipping_custom_field`, `shipping_method`, `shipping_code`, `comment`, `total`, `order_status_id`, `affiliate_id`, `commission`, `marketing_id`, `tracking`, `language_id`, `currency_id`, `currency_code`, `currency_value`, `ip`, `forwarded_ip`, `user_agent`, `accept_language`, `date_added`, `date_modified`) VALUES ('2', '0', 'INV-2016-00', '0', 'Opencart', 'http://localhost:8000/', '0', '1', 'Test', 'Test', 'test@mail.ru', '+7 (000) 000-00-00', '', '[]', 'Test', 'Test', '', 'Address', 'Address 2', 'Test', '111111', 'Russian Federation', '176', 'Rostov-na-Donu', '99', '', '[]', 'Cash on delivery', 'cod', 'Test', 'Test', '', 'Address', 'Address 2', 'Test', '111111', 'Russian Federation', '176', 'Rostov-na-Donu', '99', '', '[]', 'Flat Rate', 'flat.flat', '', '85.0000', '1', '0', '0.0000', '0', '', '1', '1', 'USD', '1.00000000', '172.21.0.1', '', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36', 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7', '2018-06-07 13:53:50', '2018-06-07 13:54:00'); -TRUNCATE TABLE `oc_order_custom_field`; TRUNCATE TABLE `oc_order_history`; INSERT INTO `oc_order_history` (`order_history_id`, `order_id`, `order_status_id`, `notify`, `comment`, `date_added`) VALUES ('19', '2', '1', '0', '', '2018-06-07 13:54:00'); INSERT INTO `oc_order_history` (`order_history_id`, `order_id`, `order_status_id`, `notify`, `comment`, `date_added`) VALUES ('18', '1', '1', '0', '', '2018-06-07 13:51:23'); diff --git a/tests/test-config.php b/tests/test-config.php new file mode 100644 index 0000000..77b4059 --- /dev/null +++ b/tests/test-config.php @@ -0,0 +1,70 @@ + array( + 'event/language/before' + ), + 'controller/*/after' => array( + 'event/language/after' + ), + 'view/*/before' => array( + 500 => 'event/theme/override', + 998 => 'event/language', + 1000 => 'event/theme' + ), + 'language/*/after' => array( + 'event/translation' + ), + //'view/*/before' => array( + // 1000 => 'event/debug/before' + //), + 'controller/*/after' => array( + 'event/debug/after' + ) +); + +if (defined('HTTP_CATALOG')) { // is defined iff in catalog + + // Actions + $_['action_pre_action'] = array( + 'startup/startup', + 'startup/error', + 'startup/event', + 'startup/sass', + 'startup/login', + 'startup/permission' + ); +} else { // admin + // Actions + $_['action_pre_action'] = array( + 'startup/startup', + 'startup/error', + 'startup/event', + 'startup/maintenance', + 'startup/seo_url' + ); +} + +// Test Settings +$_['session_engine'] = 'test'; +$_['session_autostart'] = false; + +$_['template_engine'] = 'twig'; +$_['template_directory'] = ''; +$_['template_cache'] = false;