From f47af54f1a29bc82c53a476d68ed637050ea65c7 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Mon, 3 Sep 2018 15:21:40 +0300 Subject: [PATCH 1/4] Send to CRM activate/deactivate information in marketplace --- src/Model/Config/Backend/ApiVersion.php | 31 +++- src/Model/Service/IntegrationModule.php | 106 ++++++++++++++ src/Setup/Uninstall.php | 25 ++++ .../Model/Service/IntegrationModuleTest.php | 137 ++++++++++++++++++ 4 files changed, 298 insertions(+), 1 deletion(-) create mode 100644 src/Model/Service/IntegrationModule.php create mode 100644 src/Setup/Uninstall.php create mode 100644 src/Test/Unit/Model/Service/IntegrationModuleTest.php diff --git a/src/Model/Config/Backend/ApiVersion.php b/src/Model/Config/Backend/ApiVersion.php index ce7356a..f8fa2f7 100644 --- a/src/Model/Config/Backend/ApiVersion.php +++ b/src/Model/Config/Backend/ApiVersion.php @@ -7,6 +7,9 @@ use Retailcrm\Retailcrm\Helper\Proxy as ApiClient; class ApiVersion extends \Magento\Framework\App\Config\Value { private $api; + private $request; + private $helper; + private $integrationModule; /** * ApiVersion constructor. @@ -16,6 +19,9 @@ class ApiVersion extends \Magento\Framework\App\Config\Value * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection + * @param \Magento\Framework\App\Request\Http $request + * @param \Retailcrm\Retailcrm\Helper\Data $helper + * @param \Retailcrm\Retailcrm\Model\Service\IntegrationModule $integrationModule * @param ApiClient $api * @param array $data */ @@ -24,12 +30,19 @@ class ApiVersion extends \Magento\Framework\App\Config\Value \Magento\Framework\Registry $registry, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, - ApiClient $api, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, + \Magento\Framework\App\Request\Http $request, + \Retailcrm\Retailcrm\Helper\Data $helper, + \Retailcrm\Retailcrm\Model\Service\IntegrationModule $integrationModule, + ApiClient $api, array $data = [] ) { $this->api = $api; + $this->request = $request; + $this->helper = $helper; + $this->integrationModule = $integrationModule; + parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); } @@ -79,6 +92,8 @@ class ApiVersion extends \Magento\Framework\App\Config\Value if (isset($availableVersions)) { if (in_array($apiVersions[$apiVersion], $availableVersions)) { $this->setValue($this->getValue()); + + $this->sendModuleConfiguration($api); } else { throw new \Magento\Framework\Exception\ValidatorException( __('The selected API version is unavailable') @@ -87,6 +102,20 @@ class ApiVersion extends \Magento\Framework\App\Config\Value } } + /** + * @param $api + */ + private function sendModuleConfiguration($api) + { + $clientId = $this->helper->getGeneralSettings('client_id_in_crm'); + + if (!$clientId) { + $this->integrationModule->setApiVersion($api->getVersion()); + $this->integrationModule->setAccountUrl($this->request->getUriString()); + $this->integrationModule->sendConfiguration($api); + } + } + /** * @param array $data */ diff --git a/src/Model/Service/IntegrationModule.php b/src/Model/Service/IntegrationModule.php new file mode 100644 index 0000000..1233c0f --- /dev/null +++ b/src/Model/Service/IntegrationModule.php @@ -0,0 +1,106 @@ +resourceConfig = $resourceConfig; + } + + /** + * @param $accountUrl + */ + public function setAccountUrl($accountUrl) + { + $this->accountUrl = $accountUrl; + } + + /** + * @param $apiVersion + */ + public function setApiVersion($apiVersion) + { + $this->apiVersion = $apiVersion; + } + + /** + * @return array + */ + public function getConfiguration() + { + return $this->configuration; + } + + /** + * @param $active + * + * @return void + */ + private function setConfiguration($active) + { + if ($this->apiVersion == 'v4') { + $this->configuration = [ + 'name' => self::NAME, + 'code' => self::CODE, + 'logo' => self::LOGO, + 'configurationUrl' => $this->accountUrl, + 'active' => $active + ]; + } else { + $clientId = hash('md5', date('Y-m-d H:i:s')); + + $this->configuration = [ + 'clientId' => $clientId, + 'code' => self::CODE, + 'integrationCode' => self::CODE, + 'active' => $active, + 'name' => self::NAME, + 'logo' => self::LOGO, + 'accountUrl' => $this->accountUrl + ]; + } + } + + /** + * @param \Retailcrm\Retailcrm\Helper\Proxy $apiClient + * @param boolean $active + * + * @return boolean + */ + public function sendConfiguration($apiClient, $active = true) + { + $this->setConfiguration($active); + + if ($this->apiVersion == 'v4') { + $response = $apiClient->marketplaceSettingsEdit(Helper::filterRecursive($this->configuration)); + } else { + $response = $apiClient->integrationModulesEdit(Helper::filterRecursive($this->configuration)); + } + + if (!$response) { + return false; + } + + if ($response->isSuccessful() && isset($clientId)) { + $this->resourceConfig->saveConfig(Helper::XML_PATH_RETAILCRM . 'general/client_id_in_crm', $clientId); + + return true; + } + + return false; + } +} diff --git a/src/Setup/Uninstall.php b/src/Setup/Uninstall.php new file mode 100644 index 0000000..7cf13c9 --- /dev/null +++ b/src/Setup/Uninstall.php @@ -0,0 +1,25 @@ +apiClient = $apiClient; + $this->integrationModule = $integrationModule; + } + + public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context) + { + $this->integrationModule->sendConfiguration($this->apiClient, $this->apiClient->getVersion(), false); + } +} diff --git a/src/Test/Unit/Model/Service/IntegrationModuleTest.php b/src/Test/Unit/Model/Service/IntegrationModuleTest.php new file mode 100644 index 0000000..1f2cca7 --- /dev/null +++ b/src/Test/Unit/Model/Service/IntegrationModuleTest.php @@ -0,0 +1,137 @@ +mockResourceConfig = $this->getMockBuilder(\Magento\Config\Model\ResourceModel\Config::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->mockApiClient = $this->getMockBuilder(\Retailcrm\Retailcrm\Helper\Proxy::class) + ->disableOriginalConstructor() + ->setMethods([ + 'marketplaceSettingsEdit', + 'integrationModulesEdit' + ]) + ->getMock(); + + $this->unit = new \Retailcrm\Retailcrm\Model\Service\IntegrationModule($this->mockResourceConfig); + } + + /** + * @param $active + * @param $apiVersion + * @param $isSuccessful + * + * @dataProvider dataProvider + */ + public function testSendConfiguration($active, $apiVersion, $isSuccessful) + { + $response = $this->getMockBuilder(\RetailCrm\Response\ApiResponse::class) + ->disableOriginalConstructor() + ->getMock(); + $response->expects($this->any())->method('isSuccessful')->willReturn($isSuccessful); + + if ($apiVersion == 'v4') { + $this->mockApiClient->expects($this->any())->method('marketplaceSettingsEdit') + ->willReturn($response); + } else { + $this->mockApiClient->expects($this->any())->method('integrationModulesEdit') + ->willReturn($response); + } + + $this->unit->setAccountUrl(self::ACCOUNT_URL); + $this->unit->setApiVersion($apiVersion); + $this->unit->sendConfiguration($this->mockApiClient, $active); + $configuration = $this->unit->getConfiguration(); + + $this->assertNotEmpty($configuration); + $this->assertArrayHasKey('name', $configuration); + $this->assertEquals( + \Retailcrm\Retailcrm\Model\Service\IntegrationModule::NAME, + $configuration['name'] + ); + $this->assertArrayHasKey('logo', $configuration); + $this->assertEquals( + \Retailcrm\Retailcrm\Model\Service\IntegrationModule::LOGO, + $configuration['logo'] + ); + $this->assertArrayHasKey('code', $configuration); + $this->assertEquals( + \Retailcrm\Retailcrm\Model\Service\IntegrationModule::CODE, + $configuration['code'] + ); + $this->assertArrayHasKey('active', $configuration); + $this->assertEquals($active, $configuration['active']); + + if ($apiVersion == 'v4') { + $this->assertArrayHasKey('configurationUrl', $configuration); + $this->assertEquals(self::ACCOUNT_URL, $configuration['configurationUrl']); + } else { + $this->assertArrayHasKey('accountUrl', $configuration); + $this->assertEquals(self::ACCOUNT_URL, $configuration['accountUrl']); + $this->assertArrayHasKey('integrationCode', $configuration); + $this->assertEquals( + \Retailcrm\Retailcrm\Model\Service\IntegrationModule::CODE, + $configuration['integrationCode'] + ); + $this->assertArrayHasKey('clientId', $configuration); + $this->assertNotEmpty($configuration['clientId']); + } + } + + public function dataProvider() + { + return [ + [ + 'active' => true, + 'api_version' => 'v4', + 'is_successful' => true + ], + [ + 'active' => false, + 'api_version' => 'v4', + 'is_successful' => true + ], + [ + 'active' => true, + 'api_version' => 'v4', + 'is_successful' => false + ], + [ + 'active' => false, + 'api_version' => 'v4', + 'is_successful' => false + ], + [ + 'active' => true, + 'api_version' => 'v5', + 'is_successful' => true + ], + [ + 'active' => false, + 'api_version' => 'v5', + 'is_successful' => true + ], + [ + 'active' => true, + 'api_version' => 'v5', + 'is_successful' => false + ], + [ + 'active' => false, + 'api_version' => 'v5', + 'is_successful' => false + ] + ]; + } +} From 15da35916a7f4bd03478f7db445b5d7f20074312 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Thu, 25 Oct 2018 12:35:03 +0300 Subject: [PATCH 2/4] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BD=D0=B5=D1=81=D0=BA=D0=BE=D0=BB=D1=8C=D0=BA=D0=B8?= =?UTF-8?q?=D1=85=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D0=B5=D0=B9=20=D0=B2=20?= =?UTF-8?q?=D0=BC=D0=B0=D1=80=D0=BA=D0=B5=D1=82=D0=BF=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Model/Config/Backend/ApiVersion.php | 14 ++------- src/Model/Service/IntegrationModule.php | 39 ++++++++++++++++++------- src/Setup/Uninstall.php | 3 +- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/Model/Config/Backend/ApiVersion.php b/src/Model/Config/Backend/ApiVersion.php index f8fa2f7..a55388f 100644 --- a/src/Model/Config/Backend/ApiVersion.php +++ b/src/Model/Config/Backend/ApiVersion.php @@ -8,7 +8,6 @@ class ApiVersion extends \Magento\Framework\App\Config\Value { private $api; private $request; - private $helper; private $integrationModule; /** @@ -20,7 +19,6 @@ class ApiVersion extends \Magento\Framework\App\Config\Value * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection * @param \Magento\Framework\App\Request\Http $request - * @param \Retailcrm\Retailcrm\Helper\Data $helper * @param \Retailcrm\Retailcrm\Model\Service\IntegrationModule $integrationModule * @param ApiClient $api * @param array $data @@ -33,14 +31,12 @@ class ApiVersion extends \Magento\Framework\App\Config\Value \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, \Magento\Framework\App\Request\Http $request, - \Retailcrm\Retailcrm\Helper\Data $helper, \Retailcrm\Retailcrm\Model\Service\IntegrationModule $integrationModule, ApiClient $api, array $data = [] ) { $this->api = $api; $this->request = $request; - $this->helper = $helper; $this->integrationModule = $integrationModule; parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); @@ -107,13 +103,9 @@ class ApiVersion extends \Magento\Framework\App\Config\Value */ private function sendModuleConfiguration($api) { - $clientId = $this->helper->getGeneralSettings('client_id_in_crm'); - - if (!$clientId) { - $this->integrationModule->setApiVersion($api->getVersion()); - $this->integrationModule->setAccountUrl($this->request->getUriString()); - $this->integrationModule->sendConfiguration($api); - } + $this->integrationModule->setApiVersion($api->getVersion()); + $this->integrationModule->setAccountUrl($this->request->getUriString()); + $this->integrationModule->sendConfiguration($api); } /** diff --git a/src/Model/Service/IntegrationModule.php b/src/Model/Service/IntegrationModule.php index 1233c0f..0d877ab 100644 --- a/src/Model/Service/IntegrationModule.php +++ b/src/Model/Service/IntegrationModule.php @@ -7,18 +7,28 @@ use Retailcrm\Retailcrm\Helper\Data as Helper; class IntegrationModule { const LOGO = 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5b846b1fef57e-magento.svg'; - const CODE = 'magento'; + const INTEGRATION_CODE = 'magento'; const NAME = 'Magento 2'; private $accountUrl = null; - private $resourceConfig; private $apiVersion = 'v5'; private $configuration = []; + private $resourceConfig; + private $clientId; + private $helper; + /** + * IntegrationModule constructor. + * + * @param \Magento\Config\Model\ResourceModel\Config $resourceConfig + * @param \Retailcrm\Retailcrm\Helper\Data $helper + */ public function __construct( - \Magento\Config\Model\ResourceModel\Config $resourceConfig + \Magento\Config\Model\ResourceModel\Config $resourceConfig, + Helper $helper ) { $this->resourceConfig = $resourceConfig; + $this->helper = $helper; } /** @@ -52,21 +62,25 @@ class IntegrationModule */ private function setConfiguration($active) { + $this->clientId = $this->helper->getGeneralSettings('client_id_in_crm'); + + if (!$this->clientId) { + $this->clientId = uniqid(); + } + if ($this->apiVersion == 'v4') { $this->configuration = [ 'name' => self::NAME, - 'code' => self::CODE, + 'code' => self::INTEGRATION_CODE . '-' . $this->clientId, 'logo' => self::LOGO, 'configurationUrl' => $this->accountUrl, 'active' => $active ]; } else { - $clientId = hash('md5', date('Y-m-d H:i:s')); - $this->configuration = [ - 'clientId' => $clientId, - 'code' => self::CODE, - 'integrationCode' => self::CODE, + 'clientId' => $this->clientId, + 'code' => self::INTEGRATION_CODE . '-' . $this->clientId, + 'integrationCode' => self::INTEGRATION_CODE, 'active' => $active, 'name' => self::NAME, 'logo' => self::LOGO, @@ -95,8 +109,11 @@ class IntegrationModule return false; } - if ($response->isSuccessful() && isset($clientId)) { - $this->resourceConfig->saveConfig(Helper::XML_PATH_RETAILCRM . 'general/client_id_in_crm', $clientId); + if ($response->isSuccessful() && $active == true) { + $this->resourceConfig->saveConfig( + Helper::XML_PATH_RETAILCRM . 'general/client_id_in_crm', + $this->clientId + ); return true; } diff --git a/src/Setup/Uninstall.php b/src/Setup/Uninstall.php index 7cf13c9..b444255 100644 --- a/src/Setup/Uninstall.php +++ b/src/Setup/Uninstall.php @@ -20,6 +20,7 @@ class Uninstall implements \Magento\Framework\Setup\UninstallInterface public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context) { - $this->integrationModule->sendConfiguration($this->apiClient, $this->apiClient->getVersion(), false); + $this->integrationModule->setApiVersion($this->apiClient->getVersion()); + $this->integrationModule->sendConfiguration($this->apiClient, false); } } From ef12474dfc7adbe9959b6727ece87e0b01296c40 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Thu, 25 Oct 2018 12:38:17 +0300 Subject: [PATCH 3/4] Update changelog --- CHACNGELOG.md | 3 +++ VERSION | 2 +- src/composer.json | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHACNGELOG.md b/CHACNGELOG.md index 01d13c8..b19a513 100644 --- a/CHACNGELOG.md +++ b/CHACNGELOG.md @@ -1,3 +1,6 @@ +## 2018-10-25 v.2.3.1 +* Добавлена активация модуля в маркетплейсе retailCRM + ## 2018-08-21 v.2.3.0 * Добавлены консольные команды для выгрузки архива клиентов и заказов * Обработка данных для формирования структуры перед отправкой вынесена в сервисы diff --git a/VERSION b/VERSION index cc6612c..a625450 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.3.0 \ No newline at end of file +2.3.1 \ No newline at end of file diff --git a/src/composer.json b/src/composer.json index 718bfa7..87a3451 100644 --- a/src/composer.json +++ b/src/composer.json @@ -5,7 +5,7 @@ "retailcrm/api-client-php": "~5.0" }, "type": "magento2-module", - "version": "2.3.0", + "version": "2.3.1", "license": [ "OSL-3.0", "AFL-3.0" @@ -13,7 +13,7 @@ "authors": [ { "name": "RetailDriver LLC", - "email": "gorokh@retailcrm.ru", + "email": "integration@retailcrm.ru", "homepage": "https://www.retailcrm.ru", "role": "Developer" } From 5ecedd12f0cc4bacb0ba7165c86b8381c5a3d2d6 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Thu, 25 Oct 2018 12:55:32 +0300 Subject: [PATCH 4/4] Unit test update --- .../Model/Service/IntegrationModuleTest.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Test/Unit/Model/Service/IntegrationModuleTest.php b/src/Test/Unit/Model/Service/IntegrationModuleTest.php index 1f2cca7..7679e05 100644 --- a/src/Test/Unit/Model/Service/IntegrationModuleTest.php +++ b/src/Test/Unit/Model/Service/IntegrationModuleTest.php @@ -6,12 +6,17 @@ class IntegrationModuleTest extends \PHPUnit\Framework\TestCase { private $mockResourceConfig; private $mockApiClient; + private $mockData; private $unit; const ACCOUNT_URL = 'test'; public function setUp() { + $this->mockData = $this->getMockBuilder(\Retailcrm\Retailcrm\Helper\Data::class) + ->disableOriginalConstructor() + ->getMock(); + $this->mockResourceConfig = $this->getMockBuilder(\Magento\Config\Model\ResourceModel\Config::class) ->disableOriginalConstructor() ->getMock(); @@ -24,7 +29,10 @@ class IntegrationModuleTest extends \PHPUnit\Framework\TestCase ]) ->getMock(); - $this->unit = new \Retailcrm\Retailcrm\Model\Service\IntegrationModule($this->mockResourceConfig); + $this->unit = new \Retailcrm\Retailcrm\Model\Service\IntegrationModule( + $this->mockResourceConfig, + $this->mockData + ); } /** @@ -66,8 +74,8 @@ class IntegrationModuleTest extends \PHPUnit\Framework\TestCase $configuration['logo'] ); $this->assertArrayHasKey('code', $configuration); - $this->assertEquals( - \Retailcrm\Retailcrm\Model\Service\IntegrationModule::CODE, + $this->assertContains( + \Retailcrm\Retailcrm\Model\Service\IntegrationModule::INTEGRATION_CODE, $configuration['code'] ); $this->assertArrayHasKey('active', $configuration); @@ -80,8 +88,8 @@ class IntegrationModuleTest extends \PHPUnit\Framework\TestCase $this->assertArrayHasKey('accountUrl', $configuration); $this->assertEquals(self::ACCOUNT_URL, $configuration['accountUrl']); $this->assertArrayHasKey('integrationCode', $configuration); - $this->assertEquals( - \Retailcrm\Retailcrm\Model\Service\IntegrationModule::CODE, + $this->assertContains( + \Retailcrm\Retailcrm\Model\Service\IntegrationModule::INTEGRATION_CODE, $configuration['integrationCode'] ); $this->assertArrayHasKey('clientId', $configuration);