diff --git a/.travis.yml b/.travis.yml index 1c45f63..e45ba8e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,3 +30,12 @@ before_script: script: - cd ../magento2 - php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Retailcrm/Retailcrm/Test/Unit/ + +deploy: + skip_cleanup: true + provider: script + script: make + on: + php: 7.2 + branch: master + condition: "$DEPLOY = true" \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b7bd457 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +FILE = $(TRAVIS_BUILD_DIR)/VERSION +VERSION = `cat $(FILE)` +ARCHIVE_NAME = '/tmp/retailcrm-retailcrm-'$(VERSION)'.zip' + +all: build_archive send_to_ftp delete_archive + +build_archive: + cd src; zip -r $(ARCHIVE_NAME) ./* + +send_to_ftp: + curl -T $(ARCHIVE_NAME) -u $(FTP_USER):$(FTP_PASSWORD) ftp://$(FTP_HOST)/public_html/ + +delete_archive: + rm -f $(ARCHIVE_NAME) diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..afaf360 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +1.0.0 \ No newline at end of file diff --git a/src/Block/Adminhtml/System/Config/Form/Field/Attributes.php b/src/Block/Adminhtml/System/Config/Form/Field/Attributes.php deleted file mode 100644 index 2ac2448..0000000 --- a/src/Block/Adminhtml/System/Config/Form/Field/Attributes.php +++ /dev/null @@ -1,63 +0,0 @@ -getValues(); - $html = ''; - $html .= ''; - $html .= '
    '; - - $selected = explode(',', $element->getValue()); - - foreach ($selected as $value) { - if ($key = array_search($value, array_column($values, 'value'))) { - $html .= '
  • '; - $html .= isset($values[$key]['label'])?$values[$key]['label']:'n/a'; - $html .= '
  • '; - $values[$key]['selected'] = true; - } - } - - $html .= '
'; - $html .= '
    '; - - if ($values) { - foreach ($values as $option) { - if (!isset($option['selected'])) { - $html .= '
  • '; - $html .= isset($option['label'])?$option['label']:'n/a'; - $html .= '
  • '; - } - } - } - - $html .= '
'; - $html .= '
' . $element->getElementHtml() . '
'; - $html .= ''; - - return $html; - } -} diff --git a/src/Block/Adminhtml/System/Config/Form/Field/ListMode.php b/src/Block/Adminhtml/System/Config/Form/Field/ListMode.php deleted file mode 100644 index 203121e..0000000 --- a/src/Block/Adminhtml/System/Config/Form/Field/ListMode.php +++ /dev/null @@ -1,16 +0,0 @@ - 'grid', 'label' => __('Grid Only')], - ['value' => 'list', 'label' => __('List Only')], - ['value' => 'grid-list', 'label' => __('Grid (default) / List')], - ['value' => 'list-grid', 'label' => __('List (default) / Grid')] - ]; - } -} diff --git a/src/Block/Adminhtml/System/Config/Form/Field/Payment.php b/src/Block/Adminhtml/System/Config/Form/Field/Payment.php deleted file mode 100644 index 9448ebf..0000000 --- a/src/Block/Adminhtml/System/Config/Form/Field/Payment.php +++ /dev/null @@ -1,78 +0,0 @@ -systemStore = $systemStore; - $this->formFactory = $formFactory; - $this->config = $config; - $this->paymentConfig = $paymentConfig; - $this->client = $client; - } - - public function render(AbstractElement $element) - { - $html = ''; - $htmlError = '
Please check your API Url & API Key
'; - - if ($this->client->isConfigured()) { - $activePaymentMethods = $this->paymentConfig->getActiveMethods(); - $response = $this->client->paymentTypesList(); - - if ($response === false) { - return $htmlError; - } - - if ($response->isSuccessful()) { - $paymentTypes = $response['paymentTypes']; - } else { - return $htmlError; - } - - foreach ($activePaymentMethods as $code => $payment) { - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= '
' . $payment->getTitle() . ''; - $html .= ''; - $html .= '
'; - } - - return $html; - } else { - return $htmlError; - } - } -} diff --git a/src/Block/Adminhtml/System/Config/Form/Field/Shipping.php b/src/Block/Adminhtml/System/Config/Form/Field/Shipping.php deleted file mode 100644 index 81a74bd..0000000 --- a/src/Block/Adminhtml/System/Config/Form/Field/Shipping.php +++ /dev/null @@ -1,78 +0,0 @@ -systemStore = $systemStore; - $this->formFactory = $formFactory; - $this->config = $config; - $this->shippingConfig = $shippingConfig; - $this->client = $client; - } - - public function render(AbstractElement $element) - { - $html = ''; - $htmlError = '
Please check your API Url & API Key
'; - - if ($this->client->isConfigured()) { - $deliveryMethods = $this->shippingConfig->getActiveCarriers(); - $response = $this->client->deliveryTypesList(); - - if ($response === false) { - return $htmlError; - } - - if ($response->isSuccessful()) { - $deliveryTypes = $response['deliveryTypes']; - } else { - return $htmlError; - } - - foreach ($deliveryMethods as $code => $delivery) { - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= '
' . $delivery->getConfigData('title') . ''; - $html .= ''; - $html .= '
'; - } - - return $html; - } else { - return $htmlError; - } - } -} diff --git a/src/Block/Adminhtml/System/Config/Form/Field/Status.php b/src/Block/Adminhtml/System/Config/Form/Field/Status.php deleted file mode 100644 index d5c28e2..0000000 --- a/src/Block/Adminhtml/System/Config/Form/Field/Status.php +++ /dev/null @@ -1,82 +0,0 @@ -systemStore = $systemStore; - $this->formFactory = $formFactory; - $this->config = $config; - $this->statusCollection = $statusCollection; - $this->client = $client; - } - - public function render(AbstractElement $element) - { - $html = ''; - $htmlError = '
Please check your API Url & API Key
'; - - if ($this->client->isConfigured()) { - $statuses = $this->statusCollection->toOptionArray(); - $response = $this->client->statusesList(); - - if ($response === false) { - return $htmlError; - } - - if ($response->isSuccessful()) { - $statusTypes = $response['statuses']; - } else { - return $htmlError; - } - - foreach ($statuses as $k => $status) { - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= '
' . $status['label'] . ''; - $html .= ''; - $html .= '
'; - } - - return $html; - } else { - return $htmlError; - } - } -} diff --git a/src/Block/Adminhtml/System/Config/Form/Fieldset/Payment.php b/src/Block/Adminhtml/System/Config/Form/Fieldset/Payment.php new file mode 100644 index 0000000..014bf89 --- /dev/null +++ b/src/Block/Adminhtml/System/Config/Form/Fieldset/Payment.php @@ -0,0 +1,164 @@ +paymentConfig = $paymentConfig; + $this->client = $client; + + parent::__construct($context, $authSession, $jsHelper, $data); + } + + /** + * Get field renderer + * + * @return \Magento\Config\Block\System\Config\Form\Field + */ + protected function _getFieldRenderer() + { + if (empty($this->_fieldRenderer)) { + $this->_fieldRenderer = $this->getLayout()->getBlockSingleton( + \Magento\Config\Block\System\Config\Form\Field::class + ); + } + + return $this->_fieldRenderer; + } + + /** + * Get dummy element + * + * @return \Magento\Framework\DataObject + */ + protected function _getDummyElement() + { + if (empty($this->_dummyElement)) { + $this->_dummyElement = new \Magento\Framework\DataObject(['showInDefault' => 1, 'showInWebsite' => 1]); + } + + return $this->_dummyElement; + } + + public function render(AbstractElement $element) + { + $html = ''; + $htmlError = '
Please check your API Url & API Key
'; + $html .= $this->_getHeaderHtml($element); + + if ($this->client->isConfigured()) { + $paymentMethods = $this->paymentConfig->getActiveMethods(); + + foreach ($paymentMethods as $code => $payment) { + $html .= $this->_getFieldHtml($element, $payment); + } + } else { + $html .= $htmlError; + } + + $html .= $this->_getFooterHtml($element); + + return $html; + } + + /** + * Get options values + * + * @return array + */ + private function getValues() + { + $defaultValues = [ + [ + 'value' => '', + 'label' => '' + ] + ]; + + $values = []; + + $response = $this->client->paymentTypesList(); + + if ($response === false) { + return $defaultValues; + } + + if ($response->isSuccessful()) { + $paymentsTypes = $response['paymentTypes']; + } else { + return $defaultValues; + } + + foreach ($paymentsTypes as $paymentType) { + $values[] = [ + 'label' => $paymentType['name'], + 'value' => $paymentType['code'] + ]; + } + + return $values; + } + + /** + * Get field html + * + * @param \Magento\Framework\Data\Form\Element\AbstractElement $fieldset + * @param \Magento\Payment\Model\Method\AbstractMethod $payment + * + * @return string + */ + protected function _getFieldHtml($fieldset, $payment) + { + $configData = $this->getConfigData(); + $path = 'retailcrm/' . $fieldset->getId() . '/' . $payment->getCode(); + + $data = isset($configData[$path]) ? $configData[$path] : []; + + $e = $this->_getDummyElement(); + + $field = $fieldset->addField( + $payment->getCode(), + 'select', + [ + 'name' => 'groups[' . $fieldset->getId() . '][fields][' . $payment->getCode() . '][value]', + 'label' => $payment->getTitle(), + 'value' => isset($data) ? $data : '', + 'values' => $this->getValues(), + 'inherit' => true, + 'can_use_default_value' => $this->getForm()->canUseDefaultValue($e), + 'can_use_website_value' => $this->getForm()->canUseWebsiteValue($e) + ] + )->setRenderer( + $this->_getFieldRenderer() + ); + + return $field->toHtml(); + } +} diff --git a/src/Block/Adminhtml/System/Config/Form/Fieldset/Shipping.php b/src/Block/Adminhtml/System/Config/Form/Fieldset/Shipping.php new file mode 100644 index 0000000..1def2f3 --- /dev/null +++ b/src/Block/Adminhtml/System/Config/Form/Fieldset/Shipping.php @@ -0,0 +1,170 @@ +shippingConfig = $shippingConfig; + $this->client = $client; + + parent::__construct($context, $authSession, $jsHelper, $data); + } + + /** + * Get field renderer + * + * @return \Magento\Config\Block\System\Config\Form\Field + */ + protected function _getFieldRenderer() + { + if (empty($this->_fieldRenderer)) { + $this->_fieldRenderer = $this->getLayout()->getBlockSingleton( + \Magento\Config\Block\System\Config\Form\Field::class + ); + } + + return $this->_fieldRenderer; + } + + /** + * Get dummy element + * + * @return \Magento\Framework\DataObject + */ + protected function _getDummyElement() + { + if (empty($this->_dummyElement)) { + $this->_dummyElement = new \Magento\Framework\DataObject(['showInDefault' => 1, 'showInWebsite' => 1]); + } + + return $this->_dummyElement; + } + + /** + * Render element + * + * @param AbstractElement $element + * @return string + */ + public function render(AbstractElement $element) + { + $html = ''; + $htmlError = '
Please check your API Url & API Key
'; + $html .= $this->_getHeaderHtml($element); + + if ($this->client->isConfigured()) { + $deliveryMethods = $this->shippingConfig->getActiveCarriers(); + + foreach ($deliveryMethods as $code => $delivery) { + $html .= $this->_getFieldHtml($element, $delivery); + } + } else { + $html .= $htmlError; + } + + $html .= $this->_getFooterHtml($element); + + return $html; + } + + /** + * Get options values + * + * @return array + */ + private function getValues() + { + $defaultValues = [ + [ + 'value' => '', + 'label' => '' + ] + ]; + + $values = []; + + $response = $this->client->deliveryTypesList(); + + if ($response === false) { + return $defaultValues; + } + + if ($response->isSuccessful()) { + $deliveryTypes = $response['deliveryTypes']; + } else { + return $defaultValues; + } + + foreach ($deliveryTypes as $deliveryType) { + $values[] = [ + 'label' => $deliveryType['name'], + 'value' => $deliveryType['code'] + ]; + } + + return $values; + } + + /** + * Get field html + * + * @param \Magento\Framework\Data\Form\Element\AbstractElement $fieldset + * @param \Magento\Shipping\Model\Carrier\AbstractCarrier $shipping + * + * @return string + */ + protected function _getFieldHtml($fieldset, $shipping) + { + $configData = $this->getConfigData(); + $path = 'retailcrm/' . $fieldset->getId() . '/' . $shipping->getCarrierCode(); + + $data = isset($configData[$path]) ? $configData[$path] : []; + + $e = $this->_getDummyElement(); + + $field = $fieldset->addField( + $shipping->getCarrierCode(), + 'select', + [ + 'name' => 'groups[' . $fieldset->getId() . '][fields][' . $shipping->getCarrierCode() . '][value]', + 'label' => $shipping->getConfigData('title'), + 'value' => isset($data) ? $data : '', + 'values' => $this->getValues(), + 'inherit' => true, + 'can_use_default_value' => $this->getForm()->canUseDefaultValue($e), + 'can_use_website_value' => $this->getForm()->canUseWebsiteValue($e) + ] + )->setRenderer( + $this->_getFieldRenderer() + ); + + return $field->toHtml(); + } +} diff --git a/src/Block/Adminhtml/System/Config/Form/Fieldset/Site.php b/src/Block/Adminhtml/System/Config/Form/Fieldset/Site.php new file mode 100644 index 0000000..e27af0b --- /dev/null +++ b/src/Block/Adminhtml/System/Config/Form/Fieldset/Site.php @@ -0,0 +1,161 @@ +client = $client; + + parent::__construct($context, $authSession, $jsHelper, $data); + } + + /** + * Get field renderer + * + * @return \Magento\Config\Block\System\Config\Form\Field + */ + protected function _getFieldRenderer() + { + if (empty($this->_fieldRenderer)) { + $this->_fieldRenderer = $this->getLayout()->getBlockSingleton( + \Magento\Config\Block\System\Config\Form\Field::class + ); + } + + return $this->_fieldRenderer; + } + + /** + * Get dummy element + * + * @return \Magento\Framework\DataObject + */ + protected function _getDummyElement() + { + if (empty($this->_dummyElement)) { + $this->_dummyElement = new \Magento\Framework\DataObject(['showInDefault' => 1, 'showInWebsite' => 0]); + } + + return $this->_dummyElement; + } + + /** + * Render element + * + * @param AbstractElement $element + * @return string + */ + public function render(AbstractElement $element) + { + $html = ''; + $htmlError = '
Please check your API Url & API Key
'; + $html .= $this->_getHeaderHtml($element); + + if ($this->client->isConfigured()) { + $html .= $this->_getFieldHtml($element); + } else { + $html .= $htmlError; + } + + $html .= $this->_getFooterHtml($element); + + return $html; + } + + /** + * Get options values + * + * @return array + */ + private function getValues() + { + $defaultValues = [ + [ + 'value' => '', + 'label' => '' + ] + ]; + + $values = []; + + $response = $this->client->sitesList(); + + if ($response === false) { + return $defaultValues; + } + + if ($response->isSuccessful()) { + $sites = $response['sites']; + } else { + return $defaultValues; + } + + foreach ($sites as $site) { + $values[] = [ + 'label' => $site['name'], + 'value' => $site['code'] + ]; + } + + return $values; + } + + /** + * Get field html + * + * @param \Magento\Framework\Data\Form\Element\AbstractElement $fieldset + * @param \Magento\Shipping\Model\Carrier\AbstractCarrier $shipping + * + * @return string + */ + protected function _getFieldHtml($fieldset) + { + $configData = $this->getConfigData(); + $path = 'retailcrm/' . $fieldset->getId() . '/default'; + $data = isset($configData[$path]) ? $configData[$path] : []; + $e = $this->_getDummyElement(); + + $field = $fieldset->addField( + 'default', + 'select', + [ + 'name' => 'groups[' . $fieldset->getId() . '][fields][default][value]', + 'label' => 'Default site', + 'value' => isset($data) ? $data : '', + 'values' => $this->getValues(), + 'inherit' => isset($data['inherit']) ? $data['inherit'] : '', + 'can_use_default_value' => $this->getForm()->canUseDefaultValue($e), + 'can_use_website_value' => $this->getForm()->canUseWebsiteValue($e) + ] + )->setRenderer( + $this->_getFieldRenderer() + ); + + return $field->toHtml(); + } +} diff --git a/src/Block/Adminhtml/System/Config/Form/Fieldset/Sites.php b/src/Block/Adminhtml/System/Config/Form/Fieldset/Sites.php new file mode 100644 index 0000000..0d13cfe --- /dev/null +++ b/src/Block/Adminhtml/System/Config/Form/Fieldset/Sites.php @@ -0,0 +1,168 @@ +storeManager = $storeManager; + $this->client = $client; + + parent::__construct($context, $authSession, $jsHelper, $data); + } + + /** + * Get field renderer + * + * @return \Magento\Config\Block\System\Config\Form\Field + */ + protected function _getFieldRenderer() + { + if (empty($this->_fieldRenderer)) { + $this->_fieldRenderer = $this->getLayout()->getBlockSingleton( + \Magento\Config\Block\System\Config\Form\Field::class + ); + } + + return $this->_fieldRenderer; + } + + /** + * Get dummy element + * + * @return \Magento\Framework\DataObject + */ + protected function _getDummyElement() + { + if (empty($this->_dummyElement)) { + $this->_dummyElement = new \Magento\Framework\DataObject(['showInDefault' => 1, 'showInWebsite' => 0]); + } + + return $this->_dummyElement; + } + + /** + * Render element + * + * @param AbstractElement $element + * @return string + */ + public function render(AbstractElement $element) + { + $html = ''; + $htmlError = '
Please check your API Url & API Key
'; + $html .= $this->_getHeaderHtml($element); + + if ($this->client->isConfigured()) { + $website = $this->storeManager->getWebsite($this->getRequest()->getParam('website', 0)); + + foreach ($website->getStores() as $store) { + $html .= $this->_getFieldHtml($element, $store); + } + } else { + $html .= $htmlError; + } + + $html .= $this->_getFooterHtml($element); + + return $html; + } + + /** + * Get options values + * + * @return array + */ + private function getValues() + { + $defaultValues = [ + [ + 'value' => '', + 'label' => '' + ] + ]; + + $values = []; + + $response = $this->client->sitesList(); + + if ($response === false) { + return $defaultValues; + } + + if ($response->isSuccessful()) { + $sites = $response['sites']; + } else { + return $defaultValues; + } + + foreach ($sites as $site) { + $values[] = [ + 'label' => $site['name'], + 'value' => $site['code'] + ]; + } + + return $values; + } + + /** + * Get field html + * + * @param \Magento\Framework\Data\Form\Element\AbstractElement $fieldset + * @param \Magento\Shipping\Model\Carrier\AbstractCarrier $shipping + * + * @return string + */ + protected function _getFieldHtml($fieldset, $store) + { + $configData = $this->getConfigData(); + $path = 'retailcrm/' . $fieldset->getId() . '/' . $store->getCode(); + $data = isset($configData[$path]) ? $configData[$path] : []; + $e = $this->_getDummyElement(); + + $field = $fieldset->addField( + $store->getCode(), + 'select', + [ + 'name' => 'groups[' . $fieldset->getId() . '][fields][' . $store->getCode() . '][value]', + 'label' => $store->getName(), + 'value' => isset($data) ? $data : '', + 'values' => $this->getValues(), + 'inherit' => isset($data['inherit']) ? $data['inherit'] : '', + 'can_use_default_value' => $this->getForm()->canUseDefaultValue($e), + 'can_use_website_value' => $this->getForm()->canUseWebsiteValue($e) + ] + )->setRenderer( + $this->_getFieldRenderer() + ); + + return $field->toHtml(); + } +} diff --git a/src/Block/Adminhtml/System/Config/Form/Fieldset/Status.php b/src/Block/Adminhtml/System/Config/Form/Fieldset/Status.php new file mode 100644 index 0000000..7a4a669 --- /dev/null +++ b/src/Block/Adminhtml/System/Config/Form/Fieldset/Status.php @@ -0,0 +1,164 @@ +statusCollection = $statusCollection; + $this->client = $client; + + parent::__construct($context, $authSession, $jsHelper, $data); + } + + /** + * Get field renderer + * + * @return \Magento\Config\Block\System\Config\Form\Field + */ + protected function _getFieldRenderer() + { + if (empty($this->_fieldRenderer)) { + $this->_fieldRenderer = $this->getLayout()->getBlockSingleton( + \Magento\Config\Block\System\Config\Form\Field::class + ); + } + + return $this->_fieldRenderer; + } + + /** + * Get dummy element + * + * @return \Magento\Framework\DataObject + */ + protected function _getDummyElement() + { + if (empty($this->_dummyElement)) { + $this->_dummyElement = new \Magento\Framework\DataObject(['showInDefault' => 1, 'showInWebsite' => 1]); + } + + return $this->_dummyElement; + } + + public function render(AbstractElement $element) + { + $html = ''; + $htmlError = '
Please check your API Url & API Key
'; + $html .= $this->_getHeaderHtml($element); + + if ($this->client->isConfigured()) { + $statuses = $this->statusCollection->toOptionArray();; + + foreach ($statuses as $code => $status) { + $html .= $this->_getFieldHtml($element, $status); + } + } else { + $html .= $htmlError; + } + + $html .= $this->_getFooterHtml($element); + + return $html; + } + + /** + * Get options values + * + * @return array + */ + private function getValues() + { + $defaultValues = [ + [ + 'value' => '', + 'label' => '' + ] + ]; + + $values = []; + + $response = $this->client->statusesList(); + + if ($response === false) { + return $defaultValues; + } + + if ($response->isSuccessful()) { + $statuses = $response['statuses']; + } else { + return $defaultValues; + } + + foreach ($statuses as $status) { + $values[] = [ + 'label' => $status['name'], + 'value' => $status['code'] + ]; + } + + return $values; + } + + /** + * Get field html + * + * @param \Magento\Framework\Data\Form\Element\AbstractElement $fieldset + * @param array $status + * + * @return string + */ + protected function _getFieldHtml($fieldset, $status) + { + $configData = $this->getConfigData(); + $path = 'retailcrm/' . $fieldset->getId() . '/' . $status['value']; + + $data = isset($configData[$path]) ? $configData[$path] : []; + + $e = $this->_getDummyElement(); + + $field = $fieldset->addField( + $status['value'], + 'select', + [ + 'name' => 'groups[' . $fieldset->getId() . '][fields][' . $status['value'] . '][value]', + 'label' => $status['label'], + 'value' => isset($data) ? $data : '', + 'values' => $this->getValues(), + 'inherit' => true, + 'can_use_default_value' => $this->getForm()->canUseDefaultValue($e), + 'can_use_website_value' => $this->getForm()->canUseWebsiteValue($e) + ] + )->setRenderer( + $this->_getFieldRenderer() + ); + + return $field->toHtml(); + } +} diff --git a/src/Helper/Data.php b/src/Helper/Data.php index 745dc34..fbbdb1d 100644 --- a/src/Helper/Data.php +++ b/src/Helper/Data.php @@ -4,23 +4,21 @@ namespace Retailcrm\Retailcrm\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Store\Model\StoreManagerInterface; -use Magento\Framework\ObjectManagerInterface; use Magento\Framework\App\Helper\Context; use Magento\Store\Model\ScopeInterface; class Data extends AbstractHelper { private $storeManager; - private $objectManager; const XML_PATH_RETAILCRM = 'retailcrm/'; + const XML_PATH_DEFAULT_SITE = 'retailcrm_site/default'; + const XML_PATH_SITES = 'retailcrm_sites/'; public function __construct( Context $context, - ObjectManagerInterface $objectManager, StoreManagerInterface $storeManager ) { - $this->objectManager = $objectManager; $this->storeManager = $storeManager; parent::__construct($context); } @@ -38,7 +36,58 @@ class Data extends AbstractHelper { return $this->getConfigValue(self::XML_PATH_RETAILCRM . $code, $storeId); } - + + /** + * Get site code + * + * @param $store + * + * @return mixed|null + */ + public function getSite($store) + { + if (is_int($store)) { + $store = $this->storeManager->getStore($store); + } + + $websitesConfig = $this->scopeConfig->getValue( + self::XML_PATH_RETAILCRM . self::XML_PATH_SITES . $store->getCode(), + ScopeInterface::SCOPE_WEBSITES + ); + + if (!$websitesConfig) { + $defaultSite = $this->scopeConfig->getValue(self::XML_PATH_RETAILCRM . self::XML_PATH_DEFAULT_SITE); + + if (!$defaultSite) { + return null; + } + + return $defaultSite; + } + + return $websitesConfig; + } + + public function getMappingSites() + { + $sites = []; + + $websites = $this->storeManager->getWebsites(); + + foreach ($websites as $website) { + foreach ($website->getStores() as $store) { + $site = $this->scopeConfig->getValue( + self::XML_PATH_RETAILCRM . self::XML_PATH_SITES . $store->getCode(), + ScopeInterface::SCOPE_WEBSITES, + $website->getId() + ); + $sites[$site] = $store->getId(); + } + } + + return $sites; + } + /** * Recursive array filter * diff --git a/src/Helper/Proxy.php b/src/Helper/Proxy.php index 1ea3ceb..726bd86 100644 --- a/src/Helper/Proxy.php +++ b/src/Helper/Proxy.php @@ -54,8 +54,9 @@ class Proxy if (!$response->isSuccessful()) { $this->logger->writeRow( sprintf( - "[HTTP status %s] %s", + "[HTTP status %s] %s - %s", $response->getStatusCode(), + $method, $response->getErrorMsg() ) ); @@ -135,6 +136,32 @@ class Proxy return $this->apiClient->getVersion(); } + /** + * Set site code + * + * @param $site + */ + public function setSite($site) + { + if ($this->isConfigured()) { + $this->apiClient->request->setSite($site); + } + } + + /** + * Get site code + * + * @return null | mixed + */ + public function getSite() + { + if ($this->isConfigured()) { + return $this->apiClient->request->getSite(); + } + + return null; + } + /** * Get error text message * diff --git a/src/Model/History/Exchange.php b/src/Model/History/Exchange.php index eea1a7f..c32d416 100644 --- a/src/Model/History/Exchange.php +++ b/src/Model/History/Exchange.php @@ -167,13 +167,19 @@ class Exchange { $this->logger->writeDump($order, 'doCreate'); - $payments = $this->config->getValue('retailcrm/Payment'); - $shippings = $this->config->getValue('retailcrm/Shipping'); + $payments = $this->config->getValue('retailcrm/retailcrm_payment'); + $shippings = $this->config->getValue('retailcrm/retailcrm_shipping'); + $sites = $this->helper->getMappingSites(); + + if ($sites) { + $store = $this->storeManager->getStore($sites[$order['site']]); + $websiteId = $store->getWebsiteId(); + } else { + $store = $this->storeManager->getStore(); + $websiteId = $this->storeManager->getStore()->getWebsiteId(); + } $region = $this->regionFactory->create(); - $store = $this->storeManager->getStore(); - $websiteId = $this->storeManager->getStore()->getWebsiteId(); - $customer = $this->customerFactory->create(); $customer->setWebsiteId($websiteId); @@ -324,18 +330,25 @@ class Exchange $order = $response['order']; } - $payments = $this->config->getValue('retailcrm/Payment'); - $shippings = $this->config->getValue('retailcrm/Shipping'); + $payments = $this->config->getValue('retailcrm/retailcrm_payment'); + $shippings = $this->config->getValue('retailcrm/retailcrm_shipping'); $region = $this->regionFactory->create(); - $store = $this->storeManager->getStore(); - $websiteId = $this->storeManager->getStore()->getWebsiteId(); + $sites = $this->helper->getMappingSites(); + + if ($sites) { + $store = $this->storeManager->getStore($sites[$order['site']]); + $websiteId = $store->getWebsiteId(); + } else { + $store = $this->storeManager->getStore(); + $websiteId = $this->storeManager->getStore()->getWebsiteId(); + } $customer = $this->customerFactory->create(); $customer->setWebsiteId($websiteId); if (isset($order['customer']['externalId'])) { - $customer->load($order['customer']['externalId']); // load customet by external id + $customer->load($order['customer']['externalId']); // load customer by external id } //Create object of quote @@ -345,7 +358,7 @@ class Exchange $quote->setStore($store); $quote->setCurrency(); - // if you have allready buyer id then you can load customer directly + // if you have all ready buyer id then you can load customer directly if ($customer->getId()) { $customer = $this->customerRepository->getById($customer->getId()); $quote->assignCustomer($customer); //Assign quote to customer @@ -462,7 +475,7 @@ class Exchange { $this->logger->writeDump($order, 'doUpdate'); - $Status = $this->config->getValue('retailcrm/Status'); + $Status = $this->config->getValue('retailcrm/retailcrm_status'); $Status = array_flip(array_filter($Status)); $magentoOrder = $this->order->load($order['externalId']); @@ -662,11 +675,8 @@ class Exchange public function getAllShippingMethodsCode($mcode) { $activeCarriers = $this->shipconfig->getActiveCarriers(); - $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; foreach ($activeCarriers as $carrierCode => $carrierModel) { - $options = []; - if ($carrierMethods = $carrierModel->getAllowedMethods()) { foreach ($carrierMethods as $methodCode => $method) { $code = $carrierCode . '_'. $methodCode; diff --git a/src/Model/Observer/Customer.php b/src/Model/Observer/Customer.php index 8e4465c..3432dfa 100644 --- a/src/Model/Observer/Customer.php +++ b/src/Model/Observer/Customer.php @@ -3,18 +3,22 @@ namespace Retailcrm\Retailcrm\Model\Observer; use Retailcrm\Retailcrm\Helper\Proxy as ApiClient; +use Retailcrm\Retailcrm\Helper\Data as Helper; class Customer implements \Magento\Framework\Event\ObserverInterface { private $api; private $registry; private $customer; + private $helper; public function __construct( \Magento\Framework\Registry $registry, + Helper $helper, ApiClient $api ) { $this->api = $api; + $this->helper = $helper; $this->registry = $registry; $this->customer = []; } @@ -24,7 +28,7 @@ class Customer implements \Magento\Framework\Event\ObserverInterface if ($this->registry->registry('RETAILCRM_HISTORY') === true || !$this->api->isConfigured() ) { - return; + return false; } $data = $observer->getEvent()->getCustomer(); @@ -41,12 +45,15 @@ class Customer implements \Magento\Framework\Event\ObserverInterface $response = $this->api->customersEdit($this->customer); if ($response === false) { - return; + return false; } if (!$response->isSuccessful() && $response->errorMsg == $this->api->getErrorText('errorNotFound')) { + $this->api->setSite($this->helper->getSite($data->getStore())); $this->api->customersCreate($this->customer); } + + return $this; } /** diff --git a/src/Model/Observer/OrderCreate.php b/src/Model/Observer/OrderCreate.php index 5f1350f..39404ce 100644 --- a/src/Model/Observer/OrderCreate.php +++ b/src/Model/Observer/OrderCreate.php @@ -4,12 +4,15 @@ namespace Retailcrm\Retailcrm\Model\Observer; use Magento\Framework\Event\Observer; use Retailcrm\Retailcrm\Helper\Proxy as ApiClient; +use RetailCrm\Retailcrm\Helper\Data as Helper; class OrderCreate implements \Magento\Framework\Event\ObserverInterface { - private $api; - private $config; - private $logger; + protected $api; + protected $config; + protected $logger; + protected $helper; + private $registry; private $product; private $order; @@ -18,20 +21,25 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface * Constructor * * @param \Magento\Framework\App\Config\ScopeConfigInterface $config - * @param \Retailcrm\Retailcrm\Model\Logger\Logger $logger * @param \Magento\Framework\Registry $registry + * @param \Retailcrm\Retailcrm\Model\Logger\Logger $logger + * @param \Magento\Catalog\Model\ProductRepository $product + * @param Helper $helper + * @param ApiClient $api */ public function __construct( \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\Registry $registry, \Retailcrm\Retailcrm\Model\Logger\Logger $logger, \Magento\Catalog\Model\ProductRepository $product, + Helper $helper, ApiClient $api ) { $this->logger = $logger; $this->config = $config; $this->registry = $registry; $this->product = $product; + $this->helper = $helper; $this->api = $api; $this->order = []; } @@ -41,20 +49,22 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface * * @param Observer $observer * - * @return null + * @return mixed */ public function execute(Observer $observer) { if ($this->registry->registry('RETAILCRM_HISTORY') === true || !$this->api->isConfigured() ) { - return; + return false; } $order = $observer->getEvent()->getOrder(); + $this->api->setSite($this->helper->getSite($order->getStore())); + if ($this->existsInCrm($order->getId()) === true) { - return; + return false; } $addressObj = $order->getBillingAddress(); @@ -62,7 +72,6 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface $shippingCode = $this->getShippingCode($order->getShippingMethod()); $this->order = [ - 'site' => $order->getStore()->getCode(), 'externalId' => $order->getId(), 'number' => $order->getRealOrderId(), 'createdAt' => $order->getCreatedAt(), @@ -77,10 +86,10 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface : $addressObj->getMiddlename(), 'email' => $order->getCustomerEmail(), 'phone' => $addressObj->getTelephone(), - 'status' => $this->config->getValue('retailcrm/Status/' . $order->getStatus()), + 'status' => $this->config->getValue('retailcrm/retailcrm_status/' . $order->getStatus()), 'items' => $this->getOrderItems($order), 'delivery' => [ - 'code' => $this->config->getValue('retailcrm/Shipping/' . $shippingCode), + 'code' => $this->config->getValue('retailcrm/retailcrm_shipping/' . $shippingCode), 'cost' => $order->getShippingAmount(), 'address' => [ 'index' => $addressObj->getData('postcode'), @@ -108,7 +117,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface if ($this->api->getVersion() == 'v4') { $this->order['paymentType'] = $this->config->getValue( - 'retailcrm/Payment/' . $order->getPayment()->getMethodInstance()->getCode() + 'retailcrm/retailcrm_payment/' . $order->getPayment()->getMethodInstance()->getCode() ); $this->order['discount'] = abs($order->getDiscountAmount()); } elseif ($this->api->getVersion() == 'v5') { @@ -116,7 +125,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface $payment = [ 'type' => $this->config->getValue( - 'retailcrm/Payment/' . $order->getPayment()->getMethodInstance()->getCode() + 'retailcrm/retailcrm_payment/' . $order->getPayment()->getMethodInstance()->getCode() ), 'externalId' => $order->getId(), 'order' => [ @@ -148,11 +157,13 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface $addressObj ); - \Retailcrm\Retailcrm\Helper\Data::filterRecursive($this->order); + Helper::filterRecursive($this->order); $this->logger->writeDump($this->order, 'CreateOrder'); $this->api->ordersCreate($this->order); + + return $this; } /** @@ -253,15 +264,18 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface * Check exists order or customer in CRM * * @param int $id + * @param string $method + * @param string $by + * @param string $site * * @return boolean */ - private function existsInCrm($id, $method = 'ordersGet', $by = 'externalId') + private function existsInCrm($id, $method = 'ordersGet', $by = 'externalId', $site = null) { - $response = $this->api->{$method}($id, $by); + $response = $this->api->{$method}($id, $by, $site); if ($response === false) { - return; + return false; } if (!$response->isSuccessful() && $response->errorMsg == $this->api->getErrorText('errorNotFound')) { diff --git a/src/Model/Observer/OrderUpdate.php b/src/Model/Observer/OrderUpdate.php index 760ea34..d324d09 100644 --- a/src/Model/Observer/OrderUpdate.php +++ b/src/Model/Observer/OrderUpdate.php @@ -4,6 +4,7 @@ namespace Retailcrm\Retailcrm\Model\Observer; use Magento\Framework\Event\Observer; use Retailcrm\Retailcrm\Helper\Proxy as ApiClient; +use RetailCrm\Retailcrm\Helper\Data as Helper; class OrderUpdate implements \Magento\Framework\Event\ObserverInterface { @@ -11,21 +12,25 @@ class OrderUpdate implements \Magento\Framework\Event\ObserverInterface private $config; private $registry; private $order; + private $helper; /** * Constructor * * @param \Magento\Framework\App\Config\ScopeConfigInterface $config * @param \Magento\Framework\Registry $registry + * @param Helper $helper * @param ApiClient $api */ public function __construct( \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\Registry $registry, + Helper $helper, ApiClient $api ) { $this->config = $config; $this->registry = $registry; + $this->helper = $helper; $this->api = $api; $this->order = []; } @@ -35,14 +40,14 @@ class OrderUpdate implements \Magento\Framework\Event\ObserverInterface * * @param Observer $observer * - * @return void + * @return mixed */ public function execute(Observer $observer) { if ($this->registry->registry('RETAILCRM_HISTORY') === true || !$this->api->isConfigured() ) { - return; + return false; } $order = $observer->getEvent()->getOrder(); @@ -50,7 +55,7 @@ class OrderUpdate implements \Magento\Framework\Event\ObserverInterface if ($order) { $this->order = [ 'externalId' => $order->getId(), - 'status' => $this->config->getValue('retailcrm/Status/' . $order->getStatus()) + 'status' => $this->config->getValue('retailcrm/retailcrm_status/' . $order->getStatus()) ]; if ($order->getBaseTotalDue() == 0) { @@ -66,9 +71,12 @@ class OrderUpdate implements \Magento\Framework\Event\ObserverInterface } } - \Retailcrm\Retailcrm\Helper\Data::filterRecursive($this->order); + Helper::filterRecursive($this->order); + $this->api->setSite($this->helper->getSite($order->getStore())); $this->api->ordersEdit($this->order); } + + return $this; } /** diff --git a/src/Model/Order/OrderNumber.php b/src/Model/Order/OrderNumber.php index 7864a64..e8b1c67 100644 --- a/src/Model/Order/OrderNumber.php +++ b/src/Model/Order/OrderNumber.php @@ -2,38 +2,25 @@ namespace Retailcrm\Retailcrm\Model\Order; -use Retailcrm\Retailcrm\Model\Observer\OrderCreate; +use RetailCrm\Retailcrm\Helper\Data as Helper; use Retailcrm\Retailcrm\Helper\Proxy as ApiClient; +use Retailcrm\Retailcrm\Model\Observer\OrderCreate; class OrderNumber extends OrderCreate { - private $orderRepository; - private $searchCriteriaBuilder; - private $config; - private $filterBuilder; - private $order; - private $api; - private $logger; - private $productRepository; + private $salesOrder; public function __construct( - \Magento\Sales\Model\OrderRepository $orderRepository, - \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, \Magento\Framework\App\Config\ScopeConfigInterface $config, - \Magento\Framework\Api\FilterBuilder $filterBuilder, - \Magento\Sales\Api\Data\OrderInterface $order, + \Magento\Framework\Registry $registry, \Retailcrm\Retailcrm\Model\Logger\Logger $logger, - \Magento\Catalog\Model\ProductRepository $productRepository, - ApiClient $api + \Magento\Catalog\Model\ProductRepository $product, + Helper $helper, + ApiClient $api, + \Magento\Sales\Api\Data\OrderInterface $salesOrder ) { - $this->orderRepository = $orderRepository; - $this->searchCriteriaBuilder = $searchCriteriaBuilder; - $this->config = $config; - $this->filterBuilder = $filterBuilder; - $this->order = $order; - $this->logger = $logger; - $this->productRepository = $productRepository; - $this->api = $api; + $this->salesOrder = $salesOrder; + parent::__construct($config, $registry, $logger, $product, $helper, $api); } public function exportOrderNumber() @@ -43,26 +30,28 @@ class OrderNumber extends OrderCreate $orders = []; foreach ($ordersId as $id) { - $orders[] = $this->prepareOrder($id); + $magentoOrder = $this->salesOrder->load($id); + $orders[$magentoOrder->getStore()->getId()][] = $this->prepareOrder($magentoOrder); } - $chunked = array_chunk($orders, 50); - unset($orders); + foreach ($orders as $storeId => $ordersStore) { + $chunked = array_chunk($ordersStore, 50); + unset($ordersStore); - foreach ($chunked as $chunk) { - $this->api->ordersUpload($chunk); - time_nanosleep(0, 250000000); + foreach ($chunked as $chunk) { + $this->api->setSite($this->helper->getSite($storeId)); + $this->api->ordersUpload($chunk); + time_nanosleep(0, 250000000); + } + + unset($chunked); } - unset($chunked); - return true; } - public function prepareOrder($id) + public function prepareOrder($magentoOrder) { - $magentoOrder = $this->order->load($id); - $items = []; $addressObj = $magentoOrder->getBillingAddress(); @@ -95,7 +84,6 @@ class OrderNumber extends OrderCreate $ship = $this->getShippingCode($magentoOrder->getShippingMethod()); $preparedOrder = [ - 'site' => $magentoOrder->getStore()->getCode(), 'externalId' => $magentoOrder->getRealOrderId(), 'number' => $magentoOrder->getRealOrderId(), 'createdAt' => date('Y-m-d H:i:s'), @@ -152,6 +140,6 @@ class OrderNumber extends OrderCreate $this->logger->writeDump($preparedOrder, 'OrderNumber'); - return \Retailcrm\Retailcrm\Helper\Data::filterRecursive($preparedOrder); + return Helper::filterRecursive($preparedOrder); } } diff --git a/src/README.md b/src/README.md index 100b938..8280653 100644 --- a/src/README.md +++ b/src/README.md @@ -1 +1 @@ -README \ No newline at end of file +Magento 2 module for interaction with retailCRM \ No newline at end of file diff --git a/src/Test/Helpers/FieldsetTest.php b/src/Test/Helpers/FieldsetTest.php new file mode 100644 index 0000000..e139d96 --- /dev/null +++ b/src/Test/Helpers/FieldsetTest.php @@ -0,0 +1,101 @@ +objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $factoryMock = $this->createMock(\Magento\Framework\Data\Form\Element\Factory::class); + $collectionFactoryMock = $this->createMock(\Magento\Framework\Data\Form\Element\CollectionFactory::class); + $escaperMock = $this->createMock(\Magento\Framework\Escaper::class); + $formElementMock = $this->createMock(\Magento\Framework\Data\Form\Element\Select::class); + $factoryMock->expects($this->any())->method('create')->willReturn($formElementMock); + + $formElementMock->expects($this->any())->method('setRenderer')->willReturn($formElementMock); + $elementCollection = $this->objectManager->getObject(\Magento\Framework\Data\Form\Element\Collection::class); + + // element mock + $this->elementMock = $this->getMockBuilder(\Magento\Framework\Data\Form\Element\AbstractElement::class) + ->setMethods([ + 'getId', + 'getHtmlId', + 'getName', + 'getElements', + 'getLegend', + 'getComment', + 'getIsNested', + 'getExpanded', + 'getForm', + 'addField' + ]) + ->setConstructorArgs([$factoryMock, $collectionFactoryMock, $escaperMock]) + ->getMockForAbstractClass(); + $this->elementMock->expects($this->any()) + ->method('getId') + ->willReturn($this->testElementId); + $this->elementMock->expects($this->any()) + ->method('getHtmlId') + ->willReturn($this->testElementId); + $this->elementMock->expects($this->any()) + ->method('addField') + ->willReturn($formElementMock); + $this->elementMock->expects($this->any()) + ->method('getElements') + ->willReturn($elementCollection); + + $this->authSessionMock = $this->getMockBuilder(\Magento\Backend\Model\Auth\Session::class) + ->setMethods(['getUser']) + ->disableOriginalConstructor() + ->getMock(); + + $this->userMock = $this->getMockBuilder(\Magento\User\Model\User::class) + ->setMethods(['getExtra']) + ->disableOriginalConstructor() + ->getMock(); + + $this->authSessionMock->expects($this->any()) + ->method('getUser') + ->willReturn($this->userMock); + + $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class) + ->disableOriginalConstructor() + ->getMock(); + $this->requestMock->expects($this->any()) + ->method('getParam') + ->willReturn('Test Param'); + + $factoryCollection = $this->createMock(\Magento\Framework\Data\Form\Element\CollectionFactory::class); + $elementCollection = $this->createMock(\Magento\Framework\Data\Form\Element\Collection::class); + $factoryCollection->expects($this->any())->method('create')->willReturn($elementCollection); + $rendererMock = $this->createMock(\Magento\Framework\Data\Form\Element\Renderer\RendererInterface::class); + + $this->urlModelMock = $this->createMock(\Magento\Backend\Model\Url::class); + $this->layoutMock = $this->createMock(\Magento\Framework\View\Layout::class); + $this->groupMock = $this->createMock(\Magento\Config\Model\Config\Structure\Element\Group::class); + $this->groupMock->expects($this->any())->method('getFieldsetCss')->will($this->returnValue($this->testFieldSetCss)); + $this->context = $this->createMock(\Magento\Backend\Block\Context::class); + $this->context->expects($this->any())->method('getRequest')->willReturn($this->requestMock); + $this->context->expects($this->any())->method('getUrlBuilder')->willReturn($this->urlModelMock); + $this->layoutMock->expects($this->any())->method('getBlockSingleton')->willReturn($rendererMock); + $this->helperMock = $this->createMock(\Magento\Framework\View\Helper\Js::class); + $this->form = $this->createPartialMock(\Magento\Config\Block\System\Config\Form::class, ['getElements', 'getRequest']); + $this->form->expects($this->any())->method('getElements')->willReturn($elementCollection); + $this->form->expects($this->any())->method('getRequest')->willReturn($this->requestMock); + } +} diff --git a/src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/PaymentTest.php b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Fieldset/PaymentTest.php similarity index 56% rename from src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/PaymentTest.php rename to src/Test/Unit/Block/Adminhtml/System/Config/Form/Fieldset/PaymentTest.php index 95c631d..8edf18f 100644 --- a/src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/PaymentTest.php +++ b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Fieldset/PaymentTest.php @@ -1,17 +1,9 @@ objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - } - /** * @param boolean $isSuccessful * @param boolean $isConfigured @@ -19,15 +11,6 @@ class PaymentTest extends \PHPUnit\Framework\TestCase */ public function testRender($isSuccessful, $isConfigured) { - // element mock - $elementMock = $this->getMockBuilder(\Magento\Framework\Data\Form\Element\AbstractElement::class) - ->disableOriginalConstructor() - ->setMethods(['getId']) - ->getMock(); - $elementMock->expects($this->any()) - ->method('getId') - ->willReturn($this->testElementId); - // response $response = $this->objectManager->getObject( \RetailCrm\Response\ApiResponse::class, @@ -62,26 +45,35 @@ class PaymentTest extends \PHPUnit\Framework\TestCase ->method('getActiveMethods') ->willReturn($this->getTestActiveMethods()); + $data = [ + 'authSession' => $this->authSessionMock, + 'jsHelper' => $this->helperMock, + 'data' => ['group' => $this->groupMock], + 'client' => $client, + 'paymentConfig' => $paymentConfig, + 'context' => $this->context + ]; + $payment = $this->objectManager->getObject( - \Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Field\Payment::class, - [ - 'client' => $client, - 'paymentConfig' => $paymentConfig - ] + \Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\Payment::class, + $data ); - $html = $payment->render($elementMock); + $payment->setForm($this->form); + $payment->setLayout($this->layoutMock); - if (!$isConfigured || !$isSuccessful) { - $this->assertEquals($html, $this->getHtml(true)); - } + $html = $payment->render($this->elementMock); - if ($isConfigured && $isSuccessful) { - $this->assertEquals($html, $this->getHtml(false)); + $this->assertContains($this->testElementId, $html); + $this->assertContains($this->testFieldSetCss, $html); + + if (!$isConfigured) { + $expected = '
Please check your API Url & API Key
'; + $this->assertContains($expected, $html); } } - private function getTestActiveMethods() + protected function getTestActiveMethods() { $payment = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class) ->disableOriginalConstructor() @@ -107,35 +99,6 @@ class PaymentTest extends \PHPUnit\Framework\TestCase ]; } - private function getHtml($error) - { - $html = ''; - $paymentTypes = $this->getTestResponse(); - - foreach ($this->getTestActiveMethods() as $code => $paymentType) { - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= '
' . $paymentType->getTitle() . ''; - $html .= ''; - $html .= '
'; - } - - if ($error) { - return '
Please check your API Url & API Key
'; - } - - return $html; - } - public function dataProvider() { return [ diff --git a/src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/ShippingTest.php b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Fieldset/ShippingTest.php similarity index 56% rename from src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/ShippingTest.php rename to src/Test/Unit/Block/Adminhtml/System/Config/Form/Fieldset/ShippingTest.php index ac53639..a1511c8 100644 --- a/src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/ShippingTest.php +++ b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Fieldset/ShippingTest.php @@ -1,17 +1,9 @@ objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - } - /** * @param $isSuccessful * @param $isConfigured @@ -19,15 +11,6 @@ class ShippingTest extends \PHPUnit\Framework\TestCase */ public function testRender($isSuccessful, $isConfigured) { - // element mock - $elementMock = $this->getMockBuilder(\Magento\Framework\Data\Form\Element\AbstractElement::class) - ->disableOriginalConstructor() - ->setMethods(['getId']) - ->getMock(); - $elementMock->expects($this->any()) - ->method('getId') - ->willReturn($this->testElementId); - // response $response = $this->objectManager->getObject( \RetailCrm\Response\ApiResponse::class, @@ -62,26 +45,35 @@ class ShippingTest extends \PHPUnit\Framework\TestCase ->method('getActiveCarriers') ->willReturn($this->getTestActiveCarriers()); + $data = [ + 'authSession' => $this->authSessionMock, + 'jsHelper' => $this->helperMock, + 'data' => ['group' => $this->groupMock], + 'client' => $client, + 'shippingConfig' => $shippingConfig, + 'context' => $this->context + ]; + $shipping = $this->objectManager->getObject( - \Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Field\Shipping::class, - [ - 'client' => $client, - 'shippingConfig' => $shippingConfig - ] + \Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\Shipping::class, + $data ); - $html = $shipping->render($elementMock); + $shipping->setForm($this->form); + $shipping->setLayout($this->layoutMock); - if (!$isConfigured || !$isSuccessful) { - $this->assertEquals($html, $this->getHtml(true)); - } + $html = $shipping->render($this->elementMock); - if ($isConfigured && $isSuccessful) { - $this->assertEquals($html, $this->getHtml(false)); + $this->assertContains($this->testElementId, $html); + $this->assertContains($this->testFieldSetCss, $html); + + if (!$isConfigured) { + $expected = '
Please check your API Url & API Key
'; + $this->assertContains($expected, $html); } } - private function getTestActiveCarriers() + protected function getTestActiveCarriers() { $shipping = $this->getMockBuilder(\Magento\Shipping\Model\Carrier\AbstractCarrierInterface::class) ->disableOriginalConstructor() @@ -108,35 +100,6 @@ class ShippingTest extends \PHPUnit\Framework\TestCase ]; } - private function getHtml($error) - { - $html = ''; - $deliveryTypes = $this->getTestResponse(); - - foreach ($this->getTestActiveCarriers() as $code => $deliveryType) { - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= '
' . $deliveryType->getConfigData('title') . ''; - $html .= ''; - $html .= '
'; - } - - if ($error) { - return '
Please check your API Url & API Key
'; - } - - return $html; - } - public function dataProvider() { return [ diff --git a/src/Test/Unit/Block/Adminhtml/System/Config/Form/Fieldset/SiteTest.php b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Fieldset/SiteTest.php new file mode 100644 index 0000000..82e5f4e --- /dev/null +++ b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Fieldset/SiteTest.php @@ -0,0 +1,101 @@ +objectManager->getObject( + \RetailCrm\Response\ApiResponse::class, + [ + 'statusCode' => $isSuccessful ? 200 : 404, + 'responseBody' => json_encode($this->getTestResponse()) + ] + ); + + // api client mock + $client = $this->getMockBuilder(\Retailcrm\Retailcrm\Helper\Proxy::class) + ->disableOriginalConstructor() + ->setMethods( + [ + 'isConfigured', + 'sitesList' + ] + ) + ->getMock(); + $client->expects($this->any()) + ->method('isConfigured') + ->willReturn($isConfigured); + $client->expects($this->any()) + ->method('sitesList') + ->willReturn($response); + + $data = [ + 'authSession' => $this->authSessionMock, + 'jsHelper' => $this->helperMock, + 'data' => ['group' => $this->groupMock], + 'client' => $client, + 'context' => $this->context + ]; + + $site = $this->objectManager->getObject( + \Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\Site::class, + $data + ); + + $site->setForm($this->form); + $site->setLayout($this->layoutMock); + + $html = $site->render($this->elementMock); + + $this->assertContains($this->testElementId, $html); + $this->assertContains($this->testFieldSetCss, $html); + + if (!$isConfigured) { + $expected = '
Please check your API Url & API Key
'; + $this->assertContains($expected, $html); + } + } + + private function getTestResponse() + { + return [ + 'success' => true, + 'sites' => [ + [ + 'code' => 'payment', + 'name' => 'Test site' + ] + ] + ]; + } + + public function dataProvider() + { + return [ + [ + 'is_successful' => true, + 'is_configured' => true + ], + [ + 'is_successful' => false, + 'is_configured' => false + ], + [ + 'is_successful' => true, + 'is_configured' => false + ], + [ + 'is_successful' => false, + 'is_configured' => true + ] + ]; + } +} diff --git a/src/Test/Unit/Block/Adminhtml/System/Config/Form/Fieldset/SitesTest.php b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Fieldset/SitesTest.php new file mode 100644 index 0000000..7e81c88 --- /dev/null +++ b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Fieldset/SitesTest.php @@ -0,0 +1,130 @@ +objectManager->getObject( + \RetailCrm\Response\ApiResponse::class, + [ + 'statusCode' => $isSuccessful ? 200 : 404, + 'responseBody' => json_encode($this->getTestResponse()) + ] + ); + + // api client mock + $client = $this->getMockBuilder(\Retailcrm\Retailcrm\Helper\Proxy::class) + ->disableOriginalConstructor() + ->setMethods( + [ + 'isConfigured', + 'sitesList' + ] + ) + ->getMock(); + $client->expects($this->any()) + ->method('isConfigured') + ->willReturn($isConfigured); + $client->expects($this->any()) + ->method('sitesList') + ->willReturn($response); + + $websiteMock = $this->createMock(\Magento\Store\Model\Website::class); + $websiteMock->expects($this->any())->method('getStores')->willReturn($this->getTestStores()); + + // payment config mock + $storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + $storeManager->expects($this->any()) + ->method('getWebsite') + ->willReturn($websiteMock); + + $data = [ + 'authSession' => $this->authSessionMock, + 'jsHelper' => $this->helperMock, + 'data' => ['group' => $this->groupMock], + 'client' => $client, + 'storeManager' => $storeManager, + 'context' => $this->context + ]; + + $sites = $this->objectManager->getObject( + \Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\Sites::class, + $data + ); + + $sites->setForm($this->form); + $sites->setLayout($this->layoutMock); + + $html = $sites->render($this->elementMock); + + $this->assertContains($this->testElementId, $html); + $this->assertContains($this->testFieldSetCss, $html); + + if (!$isConfigured) { + $expected = '
Please check your API Url & API Key
'; + $this->assertContains($expected, $html); + } + } + + protected function getTestStores() + { + $store = $this->getMockBuilder(\Magento\Store\Model\Store::class) + ->disableOriginalConstructor() + ->getMock(); + + $store->expects($this->any()) + ->method('getName') + ->willReturn('Test Store'); + + $store->expects($this->any()) + ->method('getCode') + ->willReturn('test_store_code'); + + return ['test_site' => $store]; + } + + private function getTestResponse() + { + return [ + 'success' => true, + 'sites' => [ + [ + 'code' => 'payment', + 'name' => 'Test site' + ] + ] + ]; + } + + public function dataProvider() + { + return [ + [ + 'is_successful' => true, + 'is_configured' => true + ], + [ + 'is_successful' => false, + 'is_configured' => false + ], + [ + 'is_successful' => true, + 'is_configured' => false + ], + [ + 'is_successful' => false, + 'is_configured' => true + ] + ]; + } +} diff --git a/src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/StatusTest.php b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Fieldset/StatusTest.php similarity index 54% rename from src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/StatusTest.php rename to src/Test/Unit/Block/Adminhtml/System/Config/Form/Fieldset/StatusTest.php index 56d091b..16da0ff 100644 --- a/src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/StatusTest.php +++ b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Fieldset/StatusTest.php @@ -1,17 +1,9 @@ objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - } - /** * @param boolean $isSuccessful * @param boolean $isConfigured @@ -19,15 +11,6 @@ class StatusTest extends \PHPUnit\Framework\TestCase */ public function testRender($isSuccessful, $isConfigured) { - // element mock - $elementMock = $this->getMockBuilder(\Magento\Framework\Data\Form\Element\AbstractElement::class) - ->disableOriginalConstructor() - ->setMethods(['getId']) - ->getMock(); - $elementMock->expects($this->any()) - ->method('getId') - ->willReturn($this->testElementId); - // response $response = $this->objectManager->getObject( \RetailCrm\Response\ApiResponse::class, @@ -62,26 +45,35 @@ class StatusTest extends \PHPUnit\Framework\TestCase ->method('toOptionArray') ->willReturn($this->getTestStatuses()); + $data = [ + 'authSession' => $this->authSessionMock, + 'jsHelper' => $this->helperMock, + 'data' => ['group' => $this->groupMock], + 'client' => $client, + 'statusCollection' => $statusCollection, + 'context' => $this->context + ]; + $status = $this->objectManager->getObject( - \Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Field\Status::class, - [ - 'client' => $client, - 'statusCollection' => $statusCollection - ] + \Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\Status::class, + $data ); - $html = $status->render($elementMock); + $status->setForm($this->form); + $status->setLayout($this->layoutMock); - if (!$isConfigured || !$isSuccessful) { - $this->assertEquals($html, $this->getHtml(true)); - } + $html = $status->render($this->elementMock); - if ($isConfigured && $isSuccessful) { - $this->assertEquals($html, $this->getHtml(false)); + $this->assertContains($this->testElementId, $html); + $this->assertContains($this->testFieldSetCss, $html); + + if (!$isConfigured) { + $expected = '
Please check your API Url & API Key
'; + $this->assertContains($expected, $html); } } - private function getTestStatuses() + protected function getTestStatuses() { $status = [ 'label' => 'Test Status', @@ -104,37 +96,6 @@ class StatusTest extends \PHPUnit\Framework\TestCase ]; } - private function getHtml($error) - { - $html = ''; - $statuses = $this->getTestResponse(); - - foreach ($this->getTestStatuses() as $code => $status) { - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= '
' . $status['label'] . ''; - $html .= ''; - $html .= '
'; - } - - if ($error) { - return '
Please check your API Url & API Key
'; - } - - return $html; - } - public function dataProvider() { return [ diff --git a/src/Test/Unit/Model/Observer/CustomerTest.php b/src/Test/Unit/Model/Observer/CustomerTest.php index 952fd4c..e1bd331 100644 --- a/src/Test/Unit/Model/Observer/CustomerTest.php +++ b/src/Test/Unit/Model/Observer/CustomerTest.php @@ -11,6 +11,7 @@ class CustomerTest extends \PHPUnit\Framework\TestCase private $mockEvent; private $mockCustomer; private $unit; + private $helper; public function setUp() { @@ -19,7 +20,8 @@ class CustomerTest extends \PHPUnit\Framework\TestCase ->setMethods([ 'customersEdit', 'customersCreate', - 'isConfigured' + 'isConfigured', + 'setSite' ]) ->getMock(); @@ -48,12 +50,16 @@ class CustomerTest extends \PHPUnit\Framework\TestCase 'getEmail', 'getFirstname', 'getMiddlename', - 'getLastname' + 'getLastname', + 'getStore' ]) ->getMock(); + $this->helper = $this->createMock(\Retailcrm\Retailcrm\Helper\Data::class); + $this->unit = new \Retailcrm\Retailcrm\Model\Observer\Customer( $this->registry, + $this->helper, $this->mockApi ); } @@ -110,6 +116,12 @@ class CustomerTest extends \PHPUnit\Framework\TestCase ->method('getLastname') ->willReturn($testData['lastname']); + $store = $this->createMock(\Magento\Store\Model\Store::class); + + $this->mockCustomer->expects($this->any()) + ->method('getStore') + ->willReturn($store); + // mock Event $this->mockEvent->expects($this->any()) ->method('getCustomer') @@ -120,7 +132,7 @@ class CustomerTest extends \PHPUnit\Framework\TestCase ->method('getEvent') ->willReturn($this->mockEvent); - $this->unit->execute($this->mockObserver); + $customerObserver = $this->unit->execute($this->mockObserver); if ($isConfigured) { $this->assertNotEmpty($this->unit->getCustomer()); @@ -130,6 +142,7 @@ class CustomerTest extends \PHPUnit\Framework\TestCase $this->assertArrayHasKey('lastName', $this->unit->getCustomer()); $this->assertArrayHasKey('patronymic', $this->unit->getCustomer()); $this->assertArrayHasKey('createdAt', $this->unit->getCustomer()); + $this->assertInstanceOf(\RetailCrm\Retailcrm\Model\Observer\Customer::class, $customerObserver); } else { $this->assertEmpty($this->unit->getCustomer()); } diff --git a/src/Test/Unit/Model/Observer/OrderCreateTest.php b/src/Test/Unit/Model/Observer/OrderCreateTest.php index fa38252..143528e 100644 --- a/src/Test/Unit/Model/Observer/OrderCreateTest.php +++ b/src/Test/Unit/Model/Observer/OrderCreateTest.php @@ -33,7 +33,8 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase 'customersCreate', 'customersList', 'getVersion', - 'isConfigured' + 'isConfigured', + 'setSite' ]) ->getMock(); @@ -119,11 +120,14 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase ->disableOriginalConstructor() ->getMock(); + $helper = $this->createMock(\Retailcrm\Retailcrm\Helper\Data::class); + $this->unit = new \Retailcrm\Retailcrm\Model\Observer\OrderCreate( $this->config, $this->registry, $this->logger, $product, + $helper, $this->mockApi ); } @@ -304,7 +308,7 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase ->method('getEvent') ->willReturn($this->mockEvent); - $this->unit->execute($this->mockObserver); + $orderCreateObserver = $this->unit->execute($this->mockObserver); if ($isConfigured && !$isSuccessful) { $this->assertNotEmpty($this->unit->getOrder()); @@ -325,6 +329,8 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase } else { $this->assertArrayHasKey('paymentType', $this->unit->getOrder()); } + + $this->assertInstanceOf(\Retailcrm\Retailcrm\Model\Observer\OrderCreate::class, $orderCreateObserver); } elseif (!$isConfigured || $isSuccessful) { $this->assertEmpty($this->unit->getOrder()); } diff --git a/src/Test/Unit/Model/Observer/OrderUpdateTest.php b/src/Test/Unit/Model/Observer/OrderUpdateTest.php index e0779ad..6ece21b 100644 --- a/src/Test/Unit/Model/Observer/OrderUpdateTest.php +++ b/src/Test/Unit/Model/Observer/OrderUpdateTest.php @@ -22,7 +22,8 @@ class OrderUpdateTest extends \PHPUnit\Framework\TestCase 'ordersEdit', 'ordersPaymentsEdit', 'getVersion', - 'isConfigured' + 'isConfigured', + 'setSite' ]) ->getMock(); @@ -38,12 +39,14 @@ class OrderUpdateTest extends \PHPUnit\Framework\TestCase $this->objectManager = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class) ->getMockForAbstractClass(); - $this->mockOrder = $this->getMockBuilder(\Magento\Sales\Order::class) + $this->mockOrder = $this->getMockBuilder(\Magento\Sales\Model\Order::class) + ->disableOriginalConstructor() ->setMethods([ 'getId', 'getPayment', 'getBaseTotalDue', - 'getStatus' + 'getStatus', + 'getStore' ]) ->getMock(); @@ -59,9 +62,12 @@ class OrderUpdateTest extends \PHPUnit\Framework\TestCase ->disableOriginalConstructor() ->getMock(); + $helper = $this->createMock(\Retailcrm\Retailcrm\Helper\Data::class); + $this->unit = new \Retailcrm\Retailcrm\Model\Observer\OrderUpdate( $this->config, $this->registry, + $helper, $this->mockApi ); } @@ -101,6 +107,12 @@ class OrderUpdateTest extends \PHPUnit\Framework\TestCase ->method('getPayment') ->willReturn($this->mockPayment); + $store = $this->createMock(\Magento\Store\Model\Store::class); + + $this->mockOrder->expects($this->any()) + ->method('getStore') + ->willReturn($store); + // mock Api $this->mockApi->expects($this->any()) ->method('getVersion') @@ -120,12 +132,16 @@ class OrderUpdateTest extends \PHPUnit\Framework\TestCase ->method('getEvent') ->willReturn($this->mockEvent); - $this->unit->execute($this->mockObserver); + $updateOrderObserver = $this->unit->execute($this->mockObserver); if ($isConfigured) { $this->assertNotEmpty($this->unit->getOrder()); $this->assertArrayHasKey('externalId', $this->unit->getOrder()); $this->assertArrayHasKey('status', $this->unit->getOrder()); + $this->assertInstanceOf( + \Retailcrm\Retailcrm\Model\Observer\OrderUpdate::class, + $updateOrderObserver + ); } else { $this->assertEmpty($this->unit->getOrder()); } diff --git a/src/etc/adminhtml/system.xml b/src/etc/adminhtml/system.xml index 8f27a94..b1868a6 100644 --- a/src/etc/adminhtml/system.xml +++ b/src/etc/adminhtml/system.xml @@ -35,43 +35,26 @@ Attributes to export into icml - Retailcrm\Retailcrm\Model\Setting\Attribute - + Retailcrm\Retailcrm\Model\Setting\Attribute - + - - - - Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Field\Shipping - + Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\Shipping - + - - - Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Field\Payment - This text will display on the frontend. - - + Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\Payment - + - - - Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Field\Status - This text will display on the frontend. - + Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\Status - + @@ -81,7 +64,16 @@ Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Button - + + + + Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\Site + + + + + Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\Sites + diff --git a/src/etc/config.xml b/src/etc/config.xml index c35267a..3889d33 100644 --- a/src/etc/config.xml +++ b/src/etc/config.xml @@ -12,4 +12,4 @@ - \ No newline at end of file +