From 97a9e8b27cab36f19c76165fae86c840a151de16 Mon Sep 17 00:00:00 2001 From: Evgeniy-Goroh Date: Mon, 18 May 2020 12:19:01 +0300 Subject: [PATCH] Save config dinamic fields * save config dinamic fields * update test --- .gitignore | 3 +- .travis.yml | 218 ++++++++++++++++-- Makefile | 4 + bin/install.sh | 18 +- bin/script.sh | 9 - .../System/Config/Form/Fieldset/Payment.php | 2 +- .../Config/Form/Fieldset/PaymentList.php | 95 ++++++++ .../System/Config/Form/Fieldset/Shipping.php | 8 +- .../Config/Form/Fieldset/ShippingList.php | 95 ++++++++ .../System/Config/Form/Fieldset/Site.php | 8 +- .../System/Config/Form/Fieldset/Sites.php | 8 +- .../System/Config/Form/Fieldset/Status.php | 8 +- .../Config/Form/Fieldset/StatusList.php | 95 ++++++++ src/Helper/Data.php | 59 +++++ src/Model/Config/Backend/PaymentCms.php | 71 ++++++ src/Model/Config/Backend/PaymentCrm.php | 75 ++++++ src/Model/Config/Backend/ShippingCms.php | 72 ++++++ src/Model/Config/Backend/ShippingCrm.php | 74 ++++++ src/Model/Config/Backend/StatusCms.php | 71 ++++++ src/Model/Config/Backend/StatusCrm.php | 75 ++++++ src/Model/History/Exchange.php | 14 +- src/Model/Observer/OrderUpdate.php | 4 +- src/Model/Service/IntegrationModule.php | 9 +- src/Model/Service/Order.php | 16 +- src/Test/Helpers/FieldsetTest.php | 6 +- src/Test/TestCase.php | 58 +++++ .../Block/Frontend/DaemonCollectorTest.php | 4 +- src/Test/Unit/Model/Observer/CustomerTest.php | 4 +- .../Unit/Model/Observer/OrderCreateTest.php | 4 +- .../Unit/Model/Observer/OrderUpdateTest.php | 4 +- src/Test/Unit/Model/Service/CustomerTest.php | 4 +- .../Model/Service/IntegrationModuleTest.php | 4 +- .../Model/Service/InventoriesUploadTest.php | 15 +- src/Test/Unit/Model/Service/OrderTest.php | 14 +- src/etc/adminhtml/system.xml | 26 ++- 35 files changed, 1169 insertions(+), 85 deletions(-) delete mode 100644 bin/script.sh create mode 100644 src/Block/Adminhtml/System/Config/Form/Fieldset/PaymentList.php create mode 100644 src/Block/Adminhtml/System/Config/Form/Fieldset/ShippingList.php create mode 100644 src/Block/Adminhtml/System/Config/Form/Fieldset/StatusList.php create mode 100644 src/Model/Config/Backend/PaymentCms.php create mode 100644 src/Model/Config/Backend/PaymentCrm.php create mode 100644 src/Model/Config/Backend/ShippingCms.php create mode 100644 src/Model/Config/Backend/ShippingCrm.php create mode 100644 src/Model/Config/Backend/StatusCms.php create mode 100644 src/Model/Config/Backend/StatusCrm.php create mode 100644 src/Test/TestCase.php diff --git a/.gitignore b/.gitignore index 14bc68c..4831ed4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/nbproject/private/ \ No newline at end of file +/nbproject/private/ +.idea diff --git a/.travis.yml b/.travis.yml index 2e243df..f1f6753 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,8 @@ +os: linux +dist: trusty language: php -sudo: false +if: tag IS blank env: global: @@ -13,34 +15,210 @@ env: - ADMIN_USER=admin - ADMIN_PASS=admin123 -matrix: +stages: + - test + +jobs: include: + - php: 7.0 + env: BRANCH=2.0.2 - - php: 7.0 - env: BRANCH=2.2.5 - - php: 7.1 - env: BRANCH=2.2.5 + - php: 7.0 + env: BRANCH=2.0.4 - - php: 7.0 - env: BRANCH=2.2.6 - - php: 7.1 - env: BRANCH=2.2.6 + - php: 7.0 + env: BRANCH=2.0.5 - - php: 7.0 - env: BRANCH=2.2.7 - - php: 7.1 - env: BRANCH=2.2.7 + - php: 7.0 + env: BRANCH=2.0.6 - - php: 7.0 - env: BRANCH=2.2.8 - - php: 7.1 - env: BRANCH=2.2.8 + - php: 7.0 + env: BRANCH=2.0.7 + + - php: 7.0 + env: BRANCH=2.0.8 + + - php: 7.0 + env: BRANCH=2.0.9 + + - php: 7.0 + env: BRANCH=2.0.10 + + - php: 7.0 + env: BRANCH=2.0.11 + + - php: 7.0 + env: BRANCH=2.0.12 + + - php: 7.0 + env: BRANCH=2.0.13 + + - php: 7.0 + env: BRANCH=2.0.14 + + - php: 7.0 + env: BRANCH=2.0.15 + + - php: 7.0 + env: BRANCH=2.0.16 + + - php: 7.0 + env: BRANCH=2.0.17 + + - php: 7.0 + env: BRANCH=2.0.18 + + - php: 7.0 + env: BRANCH=2.1.0 + + - php: 7.0 + env: BRANCH=2.1.1 + + - php: 7.0 + env: BRANCH=2.1.2 + + - php: 7.0 + env: BRANCH=2.1.3 + + - php: 7.0 + env: BRANCH=2.1.4 + + - php: 7.0 + env: BRANCH=2.1.5 + + - php: 7.0 + env: BRANCH=2.1.6 + + - php: 7.0 + env: BRANCH=2.1.7 + + - php: 7.0 + env: BRANCH=2.1.8 + + - php: 7.0 + env: BRANCH=2.1.9 + + - php: 7.0 + env: BRANCH=2.1.10 + + - php: 7.0 + env: BRANCH=2.1.11 + + - php: 7.0 + env: BRANCH=2.1.12 + + - php: 7.0 + env: BRANCH=2.1.13 + + - php: 7.0 + env: BRANCH=2.1.14 + + - php: 7.0 + env: BRANCH=2.1.15 + + - php: 7.0 + env: BRANCH=2.1.16 + - php: 7.1 + env: BRANCH=2.1.16 + + - php: 7.0 + env: BRANCH=2.1.17 + - php: 7.1 + env: BRANCH=2.1.17 + + - php: 7.0 + env: BRANCH=2.1.18 + - php: 7.1 + env: BRANCH=2.1.18 + + - php: 7.0 + env: BRANCH=2.2.0 + - php: 7.1 + env: BRANCH=2.2.0 + + - php: 7.0 + env: BRANCH=2.2.1 + - php: 7.1 + env: BRANCH=2.2.1 + + - php: 7.0 + env: BRANCH=2.2.2 + - php: 7.1 + env: BRANCH=2.2.2 + + - php: 7.0 + env: BRANCH=2.2.3 + - php: 7.1 + env: BRANCH=2.2.3 + + - php: 7.0 + env: BRANCH=2.2.4 + - php: 7.1 + env: BRANCH=2.2.4 + + - php: 7.0 + env: BRANCH=2.2.5 + - php: 7.1 + env: BRANCH=2.2.5 + + - php: 7.0 + env: BRANCH=2.2.6 + - php: 7.1 + env: BRANCH=2.2.6 + + - php: 7.0 + env: BRANCH=2.2.7 + - php: 7.1 + env: BRANCH=2.2.7 + + - php: 7.0 + env: BRANCH=2.2.8 + - php: 7.1 + env: BRANCH=2.2.8 + + - php: 7.0 + env: BRANCH=2.2.9 + - php: 7.1 + env: BRANCH=2.2.9 + + - php: 7.0 + env: BRANCH=2.2.10 + - php: 7.1 + env: BRANCH=2.2.10 + - php: 7.2 + env: BRANCH=2.2.10 + + - php: 7.1 + env: BRANCH=2.3.0 + - php: 7.2 + env: BRANCH=2.3.0 + + - php: 7.1 + env: BRANCH=2.3.1 + - php: 7.2 + env: BRANCH=2.3.1 + + - php: 7.1 + env: BRANCH=2.3.2 + - php: 7.2 + env: BRANCH=2.3.2 + + - php: 7.1 + env: BRANCH=2.3.3 + - php: 7.2 + env: BRANCH=2.3.3 + - php: 7.3 + env: BRANCH=2.3.3 + + - php: 7.2 + env: BRANCH=2.3.4 + - php: 7.3 + env: BRANCH=2.3.4 before_script: - bash bin/install.sh -script: - - bash bin/script.sh +script: make ci # deploy: # skip_cleanup: true diff --git a/Makefile b/Makefile index 957ab84..c4acbc2 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ FILE = $(TRAVIS_BUILD_DIR)/VERSION VERSION = `cat $(FILE)` ARCHIVE_NAME = '/tmp/retailcrm-retailcrm-'$(VERSION)'.zip' +MAGE_ROOT = $(TRAVIS_BUILD_DIR)/../magento2 all: build_archive send_to_ftp delete_archive @@ -12,3 +13,6 @@ send_to_ftp: delete_archive: rm -f $(ARCHIVE_NAME) + +ci: + php $(MAGE_ROOT)/vendor/phpunit/phpunit/phpunit -c $(MAGE_ROOT)/dev/tests/unit/phpunit.xml.dist $(MAGE_ROOT)/app/code/Retailcrm/Retailcrm/Test/Unit diff --git a/bin/install.sh b/bin/install.sh index 46abbb8..a17938c 100644 --- a/bin/install.sh +++ b/bin/install.sh @@ -15,7 +15,23 @@ magento_clone() { git clone https://github.com/magento/magento2 cd magento2 git checkout $BRANCH - composer install + + touch auth.json + echo ' + { + "http-basic": { + "repo.magento.com": { + "username": "", + "password": "" + } + } + } + ' > auth.json + + sudo sed -e "s??$PUBLIC_KEY?g" --in-place auth.json + sudo sed -e "s??$PRIVATE_KEY?g" --in-place auth.json + + composer install --no-interaction --prefer-dist composer require retailcrm/api-client-php } diff --git a/bin/script.sh b/bin/script.sh deleted file mode 100644 index 1e5ebe2..0000000 --- a/bin/script.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash - -if [ -z $TRAVIS_BUILD_DIR ]; then - exit 0; -fi - -MAGE_ROOT=$TRAVIS_BUILD_DIR/../magento2 -cd $MAGE_ROOT -php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Retailcrm/Retailcrm/Test/Unit diff --git a/src/Block/Adminhtml/System/Config/Form/Fieldset/Payment.php b/src/Block/Adminhtml/System/Config/Form/Fieldset/Payment.php index 25435a7..da3fa18 100644 --- a/src/Block/Adminhtml/System/Config/Form/Fieldset/Payment.php +++ b/src/Block/Adminhtml/System/Config/Form/Fieldset/Payment.php @@ -116,7 +116,7 @@ class Payment extends \Magento\Config\Block\System\Config\Form\Fieldset return $defaultValues; } - if ($response->isSuccessful()) { + if (isset($response['paymentTypes']) && $response->isSuccessful()) { $paymentsTypes = $response['paymentTypes']; } else { return $defaultValues; diff --git a/src/Block/Adminhtml/System/Config/Form/Fieldset/PaymentList.php b/src/Block/Adminhtml/System/Config/Form/Fieldset/PaymentList.php new file mode 100644 index 0000000..fb77324 --- /dev/null +++ b/src/Block/Adminhtml/System/Config/Form/Fieldset/PaymentList.php @@ -0,0 +1,95 @@ +_paymentCms) { + $this->_paymentCms = $this->getLayout()->createBlock( + '\Retailcrm\Retailcrm\Model\Config\Backend\PaymentCms', + '', + ['data' => ['is_render_to_js_template' => true]] + ); + } + + return $this->_paymentCms; + } + + /** + * @return \Magento\Framework\View\Element\BlockInterface + * @throws \Magento\Framework\Exception\LocalizedException + */ + protected function _getPaymentCrmRenderer() + { + if (!$this->_paymentCrm) { + $this->_paymentCrm = $this->getLayout()->createBlock( + '\Retailcrm\Retailcrm\Model\Config\Backend\PaymentCrm', + '', + ['data' => ['is_render_to_js_template' => true]] + ); + } + + return $this->_paymentCrm; + } + + /** + * @throws \Magento\Framework\Exception\LocalizedException + */ + protected function _prepareToRender() + { + $this->addColumn( + 'payment_cms', + [ + 'label' => __('CMS'), + 'renderer' => $this->_getPaymentCmsRenderer() + ] + ); + + $this->addColumn( + 'payment_crm', + [ + 'label' => __('CRM'), + 'renderer' => $this->_getPaymentCrmRenderer() + ] + ); + + $this->_addAfter = false; + $this->_addButtonLabel = __('Add'); + } + + /** + * + * @param \Magento\Framework\DataObject $row + * @throws \Magento\Framework\Exception\LocalizedException + * @return void + */ + protected function _prepareArrayRow(\Magento\Framework\DataObject $row) + { + $options = []; + $customAttribute = $row->getData('payment_cms'); + $key = 'option_' . $this->_getPaymentCmsRenderer()->calcOptionHash($customAttribute); + $options[$key] = 'selected="selected"'; + + $customAttribute = $row->getData('payment_crm'); + $key = 'option_' . $this->_getPaymentCrmRenderer()->calcOptionHash($customAttribute); + $options[$key] = 'selected="selected"'; + + $row->setData('option_extra_attrs', $options); + } +} diff --git a/src/Block/Adminhtml/System/Config/Form/Fieldset/Shipping.php b/src/Block/Adminhtml/System/Config/Form/Fieldset/Shipping.php index 8ba7999..5ee32d9 100644 --- a/src/Block/Adminhtml/System/Config/Form/Fieldset/Shipping.php +++ b/src/Block/Adminhtml/System/Config/Form/Fieldset/Shipping.php @@ -117,13 +117,17 @@ class Shipping extends \Magento\Config\Block\System\Config\Form\Fieldset $values = []; - $response = $this->client->deliveryTypesList(); + try { + $response = $this->client->deliveryTypesList(); + } catch (\Exception $exception) { + return $defaultValues; + } if ($response === false) { return $defaultValues; } - if ($response->isSuccessful()) { + if (isset($response['deliveryTypes']) && $response->isSuccessful()) { $deliveryTypes = $response['deliveryTypes']; } else { return $defaultValues; diff --git a/src/Block/Adminhtml/System/Config/Form/Fieldset/ShippingList.php b/src/Block/Adminhtml/System/Config/Form/Fieldset/ShippingList.php new file mode 100644 index 0000000..d4475b4 --- /dev/null +++ b/src/Block/Adminhtml/System/Config/Form/Fieldset/ShippingList.php @@ -0,0 +1,95 @@ +_shippingCms) { + $this->_shippingCms = $this->getLayout()->createBlock( + '\Retailcrm\Retailcrm\Model\Config\Backend\ShippingCms', + '', + ['data' => ['is_render_to_js_template' => true]] + ); + } + + return $this->_shippingCms; + } + + /** + * @return \Magento\Framework\View\Element\BlockInterface + * @throws \Magento\Framework\Exception\LocalizedException + */ + protected function _getShippingCrmRenderer() + { + if (!$this->_shippingCrm) { + $this->_shippingCrm = $this->getLayout()->createBlock( + '\Retailcrm\Retailcrm\Model\Config\Backend\ShippingCrm', + '', + ['data' => ['is_render_to_js_template' => true]] + ); + } + + return $this->_shippingCrm; + } + + /** + * @throws \Magento\Framework\Exception\LocalizedException + */ + protected function _prepareToRender() + { + $this->addColumn( + 'shipping_cms', + [ + 'label' => __('CMS'), + 'renderer' => $this->_getShippingCmsRenderer() + ] + ); + + $this->addColumn( + 'shipping_crm', + [ + 'label' => __('CRM'), + 'renderer' => $this->_getShippingCrmRenderer() + ] + ); + + $this->_addAfter = false; + $this->_addButtonLabel = __('Add'); + } + + /** + * + * @param \Magento\Framework\DataObject $row + * @throws \Magento\Framework\Exception\LocalizedException + * @return void + */ + protected function _prepareArrayRow(\Magento\Framework\DataObject $row) + { + $options = []; + $customAttribute = $row->getData('shipping_cms'); + $key = 'option_' . $this->_getShippingCmsRenderer()->calcOptionHash($customAttribute); + $options[$key] = 'selected="selected"'; + + $customAttribute = $row->getData('shipping_crm'); + $key = 'option_' . $this->_getShippingCrmRenderer()->calcOptionHash($customAttribute); + $options[$key] = 'selected="selected"'; + + $row->setData('option_extra_attrs', $options); + } +} diff --git a/src/Block/Adminhtml/System/Config/Form/Fieldset/Site.php b/src/Block/Adminhtml/System/Config/Form/Fieldset/Site.php index 10e9872..a6c50ea 100644 --- a/src/Block/Adminhtml/System/Config/Form/Fieldset/Site.php +++ b/src/Block/Adminhtml/System/Config/Form/Fieldset/Site.php @@ -109,13 +109,17 @@ class Site extends \Magento\Config\Block\System\Config\Form\Fieldset $values = []; - $response = $this->client->sitesList(); + try { + $response = $this->client->sitesList(); + } catch (\Exception $exception) { + return $defaultValues; + } if ($response === false) { return $defaultValues; } - if ($response->isSuccessful()) { + if (isset($response['sites']) && $response->isSuccessful()) { $sites = $response['sites']; } else { return $defaultValues; diff --git a/src/Block/Adminhtml/System/Config/Form/Fieldset/Sites.php b/src/Block/Adminhtml/System/Config/Form/Fieldset/Sites.php index dfefd48..cd0a128 100644 --- a/src/Block/Adminhtml/System/Config/Form/Fieldset/Sites.php +++ b/src/Block/Adminhtml/System/Config/Form/Fieldset/Sites.php @@ -115,13 +115,17 @@ class Sites extends \Magento\Config\Block\System\Config\Form\Fieldset $values = []; - $response = $this->client->sitesList(); + try { + $response = $this->client->sitesList(); + } catch (\Exception $exception) { + return $defaultValues; + } if ($response === false) { return $defaultValues; } - if ($response->isSuccessful()) { + if (isset($response['sites']) && $response->isSuccessful()) { $sites = $response['sites']; } else { return $defaultValues; diff --git a/src/Block/Adminhtml/System/Config/Form/Fieldset/Status.php b/src/Block/Adminhtml/System/Config/Form/Fieldset/Status.php index f141ee2..c87213a 100644 --- a/src/Block/Adminhtml/System/Config/Form/Fieldset/Status.php +++ b/src/Block/Adminhtml/System/Config/Form/Fieldset/Status.php @@ -111,13 +111,17 @@ class Status extends \Magento\Config\Block\System\Config\Form\Fieldset $values = []; - $response = $this->client->statusesList(); + try { + $response = $this->client->statusesList(); + } catch (\Exception $exception) { + return $defaultValues; + } if ($response === false) { return $defaultValues; } - if ($response->isSuccessful()) { + if (isset($response['statuses']) && $response->isSuccessful()) { $statuses = $response['statuses']; } else { return $defaultValues; diff --git a/src/Block/Adminhtml/System/Config/Form/Fieldset/StatusList.php b/src/Block/Adminhtml/System/Config/Form/Fieldset/StatusList.php new file mode 100644 index 0000000..53ea867 --- /dev/null +++ b/src/Block/Adminhtml/System/Config/Form/Fieldset/StatusList.php @@ -0,0 +1,95 @@ +_statusCms) { + $this->_statusCms = $this->getLayout()->createBlock( + '\Retailcrm\Retailcrm\Model\Config\Backend\StatusCms', + '', + ['data' => ['is_render_to_js_template' => true]] + ); + } + + return $this->_statusCms; + } + + /** + * @return \Magento\Framework\View\Element\BlockInterface + * @throws \Magento\Framework\Exception\LocalizedException + */ + protected function _getStatusCrmRenderer() + { + if (!$this->_statusCrm) { + $this->_statusCrm = $this->getLayout()->createBlock( + '\Retailcrm\Retailcrm\Model\Config\Backend\StatusCrm', + '', + ['data' => ['is_render_to_js_template' => true]] + ); + } + + return $this->_statusCrm; + } + + /** + * @throws \Magento\Framework\Exception\LocalizedException + */ + protected function _prepareToRender() + { + $this->addColumn( + 'status_cms', + [ + 'label' => __('CMS'), + 'renderer' => $this->_getStatusCmsRenderer() + ] + ); + + $this->addColumn( + 'status_crm', + [ + 'label' => __('CRM'), + 'renderer' => $this->_getStatusCrmRenderer() + ] + ); + + $this->_addAfter = false; + $this->_addButtonLabel = __('Add'); + } + + /** + * + * @param \Magento\Framework\DataObject $row + * @throws \Magento\Framework\Exception\LocalizedException + * @return void + */ + protected function _prepareArrayRow(\Magento\Framework\DataObject $row) + { + $options = []; + $customAttribute = $row->getData('status_cms'); + $key = 'option_' . $this->_getStatusCmsRenderer()->calcOptionHash($customAttribute); + $options[$key] = 'selected="selected"'; + + $customAttribute = $row->getData('status_crm'); + $key = 'option_' . $this->_getStatusCrmRenderer()->calcOptionHash($customAttribute); + $options[$key] = 'selected="selected"'; + + $row->setData('option_extra_attrs', $options); + } +} diff --git a/src/Helper/Data.php b/src/Helper/Data.php index dc805fc..747979b 100644 --- a/src/Helper/Data.php +++ b/src/Helper/Data.php @@ -206,4 +206,63 @@ class Data extends AbstractHelper return $haystack; } + + /** + * @return mixed + */ + public function getConfigPayments() + { + $json = $this->scopeConfig->getValue('retailcrm/paymentList/paymentList'); + $List = $this->getConfigJsonUnserialize($json); + foreach ($List as $code => $el) { + $payments[$el['payment_cms']] = $el['payment_crm']; + } + + return $payments; + } + + /** + * @return mixed + */ + public function getCongigShipping() + { + $json = $this->scopeConfig->getValue('retailcrm/shippingList/shippingList'); + $shippingList = $this->getConfigJsonUnserialize($json); + foreach ($shippingList as $code => $el) { + $shippings[$el['shipping_cms']] = $el['shipping_crm']; + } + + return $shippings; + } + + /** + * @return mixed + */ + public function getCongigStatus() + { + $json = $this->scopeConfig->getValue('retailcrm/statusList/statusList'); + $List = $this->getConfigJsonUnserialize($json); + foreach ($List as $code => $el) { + $statusList[$el['status_cms']] = $el['status_crm']; + } + + return $statusList; + } + + /** + * @param $json + * + * @return mixed + */ + public function getConfigJsonUnserialize($json) + { + if (class_exists(\Magento\Framework\Serialize\Serializer\Json::class)) { + $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); + $serializer = $objectManager->create(\Magento\Framework\Serialize\Serializer\Json::class); + return $serializer->unserialize($json); + } else { + return json_decode($json); + } + } + } diff --git a/src/Model/Config/Backend/PaymentCms.php b/src/Model/Config/Backend/PaymentCms.php new file mode 100644 index 0000000..ae2bd41 --- /dev/null +++ b/src/Model/Config/Backend/PaymentCms.php @@ -0,0 +1,71 @@ +paymentConfig = $paymentConfig; + $this->logger = $logger; + } + + /** + * @param string $value + * @return Magently\Tutorial\Block\Adminhtml\Form\Field\Activation + */ + public function setInputName($value) + { + return $this->setName($value); + } + + /** + * Parse to html. + * + * @return mixed + */ + public function _toHtml() + { + if (!$this->getOptions()) { + $paymentMethods = array(); + + try { + $paymentMethods = $this->paymentConfig->getActiveMethods(); + } catch (\Exception $exception) { + $this->logger->writeRow($exception->getMessage()); + } + + $this->addOption( 'null', "not selected"); + if ($paymentMethods) { + foreach ($paymentMethods as $code => $payment) { + $this->addOption($payment->getCode(), $payment->getTitle()); + } + } + } + + return parent::_toHtml(); + } +} diff --git a/src/Model/Config/Backend/PaymentCrm.php b/src/Model/Config/Backend/PaymentCrm.php new file mode 100644 index 0000000..106d6c3 --- /dev/null +++ b/src/Model/Config/Backend/PaymentCrm.php @@ -0,0 +1,75 @@ +client = $client; + $this->logger = $logger; + } + + /** + * @param string $value + * @return Magently\Tutorial\Block\Adminhtml\Form\Field\Activation + */ + public function setInputName($value) + { + return $this->setName($value); + } + + /** + * Parse to html. + * + * @return mixed + */ + public function _toHtml() + { + if (!$this->getOptions()) { + $paymentsTypes = array(); + + try { + $response = $this->client->paymentTypesList(); + } catch (\Exception $exception) { + $this->logger->writeRow($exception->getMessage()); + } + + if (isset($response) && $response->isSuccessful()) { + $paymentsTypes = $response['paymentTypes']; + } + + $this->addOption( 'null', "not selected"); + if ($paymentsTypes) { + foreach ($paymentsTypes as $paymentsType) { + $this->addOption($paymentsType['code'], $paymentsType['name']); + } + } + + } + + return parent::_toHtml(); + } +} diff --git a/src/Model/Config/Backend/ShippingCms.php b/src/Model/Config/Backend/ShippingCms.php new file mode 100644 index 0000000..7228767 --- /dev/null +++ b/src/Model/Config/Backend/ShippingCms.php @@ -0,0 +1,72 @@ +shippingConfig = $shippingConfig; + $this->logger = $logger; + } + + /** + * @param string $value + * @return Magently\Tutorial\Block\Adminhtml\Form\Field\Activation + */ + public function setInputName($value) + { + return $this->setName($value); + } + + /** + * Parse to html. + * + * @return mixed + */ + public function _toHtml() + { + if (!$this->getOptions()) { + $deliveryMethods = array(); + + try { + $deliveryMethods = $this->shippingConfig->getActiveCarriers(); + } catch (\Exception $exception) { + $this->logger->writeRow($exception->getMessage()); + } + + $this->addOption( 'null', "not selected"); + if ($deliveryMethods) { + foreach ($deliveryMethods as $code => $delivery) { + $this->addOption($delivery->getCarrierCode(), $delivery->getConfigData('title')); + } + } + + } + + return parent::_toHtml(); + } +} diff --git a/src/Model/Config/Backend/ShippingCrm.php b/src/Model/Config/Backend/ShippingCrm.php new file mode 100644 index 0000000..f06cf23 --- /dev/null +++ b/src/Model/Config/Backend/ShippingCrm.php @@ -0,0 +1,74 @@ +client = $client; + $this->logger = $logger; + } + + /** + * @param string $value + * @return Magently\Tutorial\Block\Adminhtml\Form\Field\Activation + */ + public function setInputName($value) + { + return $this->setName($value); + } + + /** + * Parse to html. + * + * @return mixed + */ + public function _toHtml() + { + if (!$this->getOptions()) { + $deliveryTypes = array(); + + try { + $response = $this->client->deliveryTypesList(); + } catch (\Exception $exception) { + $this->logger->writeRow($exception->getMessage()); + } + + if (isset($response) && $response->isSuccessful()) { + $deliveryTypes = $response['deliveryTypes']; + } + + $this->addOption( 'null', "not selected"); + if ($deliveryTypes) { + foreach ($deliveryTypes as $deliveryType) { + $this->addOption($deliveryType['code'], $deliveryType['name']); + } + } + } + + return parent::_toHtml(); + } +} diff --git a/src/Model/Config/Backend/StatusCms.php b/src/Model/Config/Backend/StatusCms.php new file mode 100644 index 0000000..39f0ade --- /dev/null +++ b/src/Model/Config/Backend/StatusCms.php @@ -0,0 +1,71 @@ +statusCollection = $statusCollection; + $this->logger = $logger; + } + + /** + * @param string $value + * @return Magently\Tutorial\Block\Adminhtml\Form\Field\Activation + */ + public function setInputName($value) + { + return $this->setName($value); + } + + /** + * Parse to html. + * + * @return mixed + */ + public function _toHtml() + { + if (!$this->getOptions()) { + $statuses = array(); + + try { + $statuses = $this->statusCollection->toOptionArray(); + } catch (\Exception $exception) { + $this->logger->writeRow($exception->getMessage()); + } + + $this->addOption( 'null', "not selected"); + if ($statuses) { + foreach ($statuses as $code => $status) { + $this->addOption( $status['value'], $status['label']); + } + } + } + + return parent::_toHtml(); + } +} diff --git a/src/Model/Config/Backend/StatusCrm.php b/src/Model/Config/Backend/StatusCrm.php new file mode 100644 index 0000000..3f6a5b1 --- /dev/null +++ b/src/Model/Config/Backend/StatusCrm.php @@ -0,0 +1,75 @@ +client = $client; + $this->logger = $logger; + } + + /** + * @param string $value + * @return Magently\Tutorial\Block\Adminhtml\Form\Field\Activation + */ + public function setInputName($value) + { + return $this->setName($value); + } + + /** + * Parse to html. + * + * @return mixed + */ + public function _toHtml() + { + if (!$this->getOptions()) { + $statuses = array(); + + try { + $response = $this->client->statusesList(); + } catch (\Exception $exception) { + $this->logger->writeRow($exception->getMessage()); + } + + if (isset($response) && $response->isSuccessful()) { + $statuses = $response['statuses']; + } + + $this->addOption( 'null', "not selected"); + if ($statuses) { + foreach ($statuses as $status) { + $this->addOption($status['code'], $status['name']); + } + } + + } + + return parent::_toHtml(); + } +} diff --git a/src/Model/History/Exchange.php b/src/Model/History/Exchange.php index b6f821d..326a360 100644 --- a/src/Model/History/Exchange.php +++ b/src/Model/History/Exchange.php @@ -161,8 +161,8 @@ class Exchange { $this->logger->writeDump($order, 'doCreate'); - $payments = $this->config->getValue('retailcrm/retailcrm_payment'); - $shippings = $this->config->getValue('retailcrm/retailcrm_shipping'); + $payments = $this->helper->getConfigPayments(); + $shippings = $this->helper->getCongigShipping(); $sites = $this->helper->getMappingSites(); if ($sites && in_array($order['site'], $sites)) { @@ -242,7 +242,7 @@ class Exchange //add items in quote foreach ($ditems as $id =>$item) { $product = $productRepository->getById($id,false, $store->getId(), false); - $product->setPrice($item['price_item']); + $product->setPrice($item['initialPrice']); $quote->addProduct( $product, (int)$item['quantity'] @@ -349,8 +349,8 @@ class Exchange $order = $response['order']; } - $payments = $this->config->getValue('retailcrm/retailcrm_payment'); - $shippings = $this->config->getValue('retailcrm/retailcrm_shipping'); + $payments = $this->helper->getConfigPayments(); + $shippings = $this->helper->getCongigShipping(); $region = $this->regionFactory->create(); $sites = $this->helper->getMappingSites(); @@ -411,7 +411,7 @@ class Exchange //add items in quote foreach ($ditems as $id => $item) { $product = $productRepository->getById($id,false, $store->getId(), false); - $product->setPrice($item['price_item']); + $product->setPrice($item['initialPrice']); $quote->addProduct( $product, (int)$item['quantity'] @@ -520,7 +520,7 @@ class Exchange { $this->logger->writeDump($order, 'doUpdate'); - $Status = $this->config->getValue('retailcrm/retailcrm_status'); + $Status = $this->helper->getCongigStatus(); $Status = array_flip(array_filter($Status)); $magentoOrder = $this->order->load($order['externalId']); diff --git a/src/Model/Observer/OrderUpdate.php b/src/Model/Observer/OrderUpdate.php index 0f65184..ed5768e 100644 --- a/src/Model/Observer/OrderUpdate.php +++ b/src/Model/Observer/OrderUpdate.php @@ -53,9 +53,11 @@ class OrderUpdate implements \Magento\Framework\Event\ObserverInterface $order = $observer->getEvent()->getOrder(); if ($order) { + $statusList = $this->helper->getCongigStatus(); + $this->order = [ 'externalId' => $order->getId(), - 'status' => $this->config->getValue('retailcrm/retailcrm_status/' . $order->getStatus()) + 'status' => $statusList[$order->getStatus()] ]; if ($order->getBaseTotalDue() == 0) { diff --git a/src/Model/Service/IntegrationModule.php b/src/Model/Service/IntegrationModule.php index 0d877ab..0a79225 100644 --- a/src/Model/Service/IntegrationModule.php +++ b/src/Model/Service/IntegrationModule.php @@ -3,6 +3,7 @@ namespace Retailcrm\Retailcrm\Model\Service; use Retailcrm\Retailcrm\Helper\Data as Helper; +use Magento\Framework\App\Config\ScopeConfigInterface; class IntegrationModule { @@ -110,9 +111,15 @@ class IntegrationModule } if ($response->isSuccessful() && $active == true) { + + $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT; + $scopeId = 0; + $this->resourceConfig->saveConfig( Helper::XML_PATH_RETAILCRM . 'general/client_id_in_crm', - $this->clientId + $this->clientId, + $scope, + $scopeId ); return true; diff --git a/src/Model/Service/Order.php b/src/Model/Service/Order.php index 5ed2bb9..f8940b7 100644 --- a/src/Model/Service/Order.php +++ b/src/Model/Service/Order.php @@ -37,6 +37,10 @@ class Order implements \Retailcrm\Retailcrm\Api\OrderManagerInterface $shippingAddress = $order->getShippingAddress(); $shipping = $this->getShippingCode($order->getShippingMethod()); + $shipList = $this->helper->getCongigShipping(); + $statusList = $this->helper->getCongigStatus(); + $paymentList = $this->helper->getConfigPayments(); + $preparedOrder = [ 'externalId' => $order->getId(), 'number' => $order->getRealOrderId(), @@ -46,10 +50,10 @@ class Order implements \Retailcrm\Retailcrm\Api\OrderManagerInterface 'patronymic' => $shippingAddress->getMiddlename(), 'email' => $shippingAddress->getEmail(), 'phone' => $shippingAddress->getTelephone(), - 'status' => $this->config->getValue('retailcrm/retailcrm_status/' . $order->getStatus()), + 'status' => $statusList[$order->getStatus()], 'items' => $products, 'delivery' => [ - 'code' => $this->config->getValue('retailcrm/retailcrm_shipping/' . $shipping), + 'code' => $shipList[$shipping], 'cost' => $order->getShippingAmount(), 'address' => [ 'index' => $shippingAddress->getData('postcode'), @@ -80,15 +84,11 @@ class Order implements \Retailcrm\Retailcrm\Api\OrderManagerInterface if ($this->helper->getGeneralSettings('api_version') == 'v4') { $preparedOrder['discount'] = abs($order->getDiscountAmount()); - $preparedOrder['paymentType'] = $this->config->getValue( - 'retailcrm/retailcrm_payment/' . $order->getPayment()->getMethodInstance()->getCode() - ); + $preparedOrder['paymentType'] = $paymentList[$order->getPayment()->getMethodInstance()->getCode()]; } elseif ($this->helper->getGeneralSettings('api_version') == 'v5') { $preparedOrder['discountManualAmount'] = abs($order->getDiscountAmount()); $payment = [ - 'type' => $this->config->getValue( - 'retailcrm/retailcrm_payment/' . $order->getPayment()->getMethodInstance()->getCode() - ), + 'type'=> $paymentList[$order->getPayment()->getMethodInstance()->getCode()], 'externalId' => $codeShop.$order->getId(), 'order' => [ 'externalId' => $order->getId(), diff --git a/src/Test/Helpers/FieldsetTest.php b/src/Test/Helpers/FieldsetTest.php index 4b9138c..3e32f75 100644 --- a/src/Test/Helpers/FieldsetTest.php +++ b/src/Test/Helpers/FieldsetTest.php @@ -2,7 +2,9 @@ namespace Retailcrm\Retailcrm\Test\Helpers; -class FieldsetTest extends \PHPUnit\Framework\TestCase +use Retailcrm\Retailcrm\Test\TestCase; + +class FieldsetTest extends TestCase { protected $elementMock; protected $authSessionMock; @@ -100,7 +102,7 @@ class FieldsetTest extends \PHPUnit\Framework\TestCase \Magento\Config\Block\System\Config\Form::class, ['getElements', 'getRequest'] ); - $this->form->expects($this->any())->method('getElements')->willReturn($elementCollection); + //$this->form->expects($this->any())->method('getElements')->willReturn($elementCollection); $this->form->expects($this->any())->method('getRequest')->willReturn($this->requestMock); } } diff --git a/src/Test/TestCase.php b/src/Test/TestCase.php new file mode 100644 index 0000000..d1e5be2 --- /dev/null +++ b/src/Test/TestCase.php @@ -0,0 +1,58 @@ +getMockBuilder($originalClassName) + ->disableOriginalConstructor() + ->disableOriginalClone() + ->disableArgumentCloning() + ->getMock(); + } + } + + protected function createPartialMock($originalClassName, $methods) + { + if (method_exists(\PHPUnit\Framework\TestCase::class, 'createPartialMock')) { + return parent::createMock($originalClassName); + } else { + return $this->getMockBuilder($originalClassName) + ->disableOriginalConstructor() + ->disableOriginalClone() + ->disableArgumentCloning() +// ->disallowMockingUnknownTypes() + ->setMethods(empty($methods) ? null : $methods) + ->getMock(); + } + } + } +} else { + + abstract class TestCase extends \PHPUnit\Framework\TestCase + { + public function createMock($originalClassName) + { + if (method_exists(\PHPUnit\Framework\TestCase::class, 'createMock')) { + return parent::createMock($originalClassName); + } else { + return $this->getMockBuilder($originalClassName) + ->disableOriginalConstructor() + ->disableOriginalClone() + ->disableArgumentCloning() + ->getMock(); + } + } + } +} + + diff --git a/src/Test/Unit/Block/Frontend/DaemonCollectorTest.php b/src/Test/Unit/Block/Frontend/DaemonCollectorTest.php index fb7e5f3..0c43b2c 100644 --- a/src/Test/Unit/Block/Frontend/DaemonCollectorTest.php +++ b/src/Test/Unit/Block/Frontend/DaemonCollectorTest.php @@ -2,7 +2,9 @@ namespace Retailcrm\Retailcrm\Test\Unit\Block\Frontend; -class DaemonCollectorTest extends \PHPUnit\Framework\TestCase +use Retailcrm\Retailcrm\Test\TestCase; + +class DaemonCollectorTest extends TestCase { private $unit; private $customer; diff --git a/src/Test/Unit/Model/Observer/CustomerTest.php b/src/Test/Unit/Model/Observer/CustomerTest.php index 50f7d12..769b101 100644 --- a/src/Test/Unit/Model/Observer/CustomerTest.php +++ b/src/Test/Unit/Model/Observer/CustomerTest.php @@ -2,7 +2,9 @@ namespace Retailcrm\Retailcrm\Test\Unit\Model\Observer; -class CustomerTest extends \PHPUnit\Framework\TestCase +use Retailcrm\Retailcrm\Test\TestCase; + +class CustomerTest extends TestCase { private $mockApi; private $mockResponse; diff --git a/src/Test/Unit/Model/Observer/OrderCreateTest.php b/src/Test/Unit/Model/Observer/OrderCreateTest.php index c734619..675fa34 100644 --- a/src/Test/Unit/Model/Observer/OrderCreateTest.php +++ b/src/Test/Unit/Model/Observer/OrderCreateTest.php @@ -2,10 +2,12 @@ namespace Retailcrm\Retailcrm\Test\Unit\Observer; +use Retailcrm\Retailcrm\Test\TestCase; + /** * Order create observer test class */ -class OrderCreateTest extends \PHPUnit\Framework\TestCase +class OrderCreateTest extends TestCase { private $unit; private $mockEvent; diff --git a/src/Test/Unit/Model/Observer/OrderUpdateTest.php b/src/Test/Unit/Model/Observer/OrderUpdateTest.php index 6ece21b..edfcc17 100644 --- a/src/Test/Unit/Model/Observer/OrderUpdateTest.php +++ b/src/Test/Unit/Model/Observer/OrderUpdateTest.php @@ -2,7 +2,9 @@ namespace Retailcrm\Retailcrm\Test\Unit\Observer; -class OrderUpdateTest extends \PHPUnit\Framework\TestCase +use Retailcrm\Retailcrm\Test\TestCase; + +class OrderUpdateTest extends TestCase { private $unit; private $objectManager; diff --git a/src/Test/Unit/Model/Service/CustomerTest.php b/src/Test/Unit/Model/Service/CustomerTest.php index 3a2126d..89fb636 100644 --- a/src/Test/Unit/Model/Service/CustomerTest.php +++ b/src/Test/Unit/Model/Service/CustomerTest.php @@ -2,7 +2,9 @@ namespace Retailcrm\Retailcrm\Test\Unit\Model\Service; -class CustomerTest extends \PHPUnit\Framework\TestCase +use Retailcrm\Retailcrm\Test\TestCase; + +class CustomerTest extends TestCase { private $mockData; private $mockCustomer; diff --git a/src/Test/Unit/Model/Service/IntegrationModuleTest.php b/src/Test/Unit/Model/Service/IntegrationModuleTest.php index 7679e05..ba87bed 100644 --- a/src/Test/Unit/Model/Service/IntegrationModuleTest.php +++ b/src/Test/Unit/Model/Service/IntegrationModuleTest.php @@ -2,7 +2,9 @@ namespace Retailcrm\Retailcrm\Test\Unit\Model\Service; -class IntegrationModuleTest extends \PHPUnit\Framework\TestCase +use Retailcrm\Retailcrm\Test\TestCase; + +class IntegrationModuleTest extends TestCase { private $mockResourceConfig; private $mockApiClient; diff --git a/src/Test/Unit/Model/Service/InventoriesUploadTest.php b/src/Test/Unit/Model/Service/InventoriesUploadTest.php index edbe880..9dfac27 100644 --- a/src/Test/Unit/Model/Service/InventoriesUploadTest.php +++ b/src/Test/Unit/Model/Service/InventoriesUploadTest.php @@ -2,7 +2,9 @@ namespace Retailcrm\Retailcrm\Test\Unit\Model\Service; -class InventoriesUploadTest extends \PHPUnit\Framework\TestCase +use Retailcrm\Retailcrm\Test\TestCase; + +class InventoriesUploadTest extends TestCase { private $mockApi; private $mockProductRepository; @@ -18,12 +20,12 @@ class InventoriesUploadTest extends \PHPUnit\Framework\TestCase 'isConfigured' ]) ->getMock(); - + $this->mockProductRepository = $this->getMockBuilder(\Magento\Catalog\Api\ProductRepositoryInterface::class) ->disableOriginalConstructor() ->setMethods(['getById']) ->getMockForAbstractClass(); - + $this->mockResponse = $this->getMockBuilder(\RetailCrm\Response\ApiResponse::class) ->disableOriginalConstructor() ->setMethods(['isSuccessful']) @@ -47,6 +49,7 @@ class InventoriesUploadTest extends \PHPUnit\Framework\TestCase { if ($response != false) { $responseInventories = new \RetailCrm\Response\ApiResponse(200, json_encode($response)); + $responseInventories->asJsonResponse($response); $this->mockResponse->expects($this->any()) ->method('isSuccessful') @@ -88,9 +91,9 @@ class InventoriesUploadTest extends \PHPUnit\Framework\TestCase } public function dataProviderLoadStocks() - { + { $response = $this->getResponseData(); - + return array( array( 'response' => $response['true'] @@ -100,7 +103,7 @@ class InventoriesUploadTest extends \PHPUnit\Framework\TestCase ) ); } - + private function getApiInventories() { return array( diff --git a/src/Test/Unit/Model/Service/OrderTest.php b/src/Test/Unit/Model/Service/OrderTest.php index 2d5f171..5fec64c 100644 --- a/src/Test/Unit/Model/Service/OrderTest.php +++ b/src/Test/Unit/Model/Service/OrderTest.php @@ -2,7 +2,9 @@ namespace Retailcrm\Retailcrm\Test\Unit\Model\Service; -class OrderTest extends \PHPUnit\Framework\TestCase +use Retailcrm\Retailcrm\Test\TestCase; + +class OrderTest extends TestCase { private $mockProductRepository; private $mockHelper; @@ -50,11 +52,12 @@ class OrderTest extends \PHPUnit\Framework\TestCase $this->mockHelper->expects($this->any())->method('getGeneralSettings') ->with('api_version')->willReturn($apiVersion); + $this->mockHelper->expects($this->any())->method('getConfigPayments')->willReturn(['checkmo'=>'test']); + $this->mockHelper->expects($this->any())->method('getCongigStatus')->willReturn(['processing'=>'test']); + $this->mockHelper->expects($this->any())->method('getCongigShipping')->willReturn(['flatrate'=>'test']); + $this->mockConfig->expects($this->any())->method('getValue') ->with($this->logicalOr( - $this->equalTo('retailcrm/retailcrm_status/processing'), - $this->equalTo('retailcrm/retailcrm_payment/checkmo'), - $this->equalTo('retailcrm/retailcrm_shipping/flatrate'), $this->equalTo('retailcrm/retailcrm_site/default') ))->will($this->returnCallback([$this, 'getCallbackDataConfig'])); @@ -166,9 +169,6 @@ class OrderTest extends \PHPUnit\Framework\TestCase public function getCallbackDataConfig($key) { $data = [ - 'retailcrm/retailcrm_status/processing' => 'new', - 'retailcrm/retailcrm_payment/checkmo' => 'test', - 'retailcrm/retailcrm_shipping/flatrate' => 'test', 'retailcrm/retailcrm_site/default' => 'test' ]; diff --git a/src/etc/adminhtml/system.xml b/src/etc/adminhtml/system.xml index 3130bb6..56617e1 100644 --- a/src/etc/adminhtml/system.xml +++ b/src/etc/adminhtml/system.xml @@ -34,17 +34,29 @@ Retailcrm\Retailcrm\Model\Setting\Attribute - + - Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\Shipping + + + Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\ShippingList + Magento\Config\Model\Config\Backend\Serialized\ArraySerialized + - + - Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\Payment + + + Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\PaymentList + Magento\Config\Model\Config\Backend\Serialized\ArraySerialized + - - - Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\Status + + + + + Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\StatusList + Magento\Config\Model\Config\Backend\Serialized\ArraySerialized +