From f4eca5a7b0d63120b533b05435d89d3ca72491eb Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Fri, 18 Jan 2019 11:45:51 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B4=D0=B0=D1=87=D0=B8=20=D0=B4=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D1=85=20=D0=BF=D0=BE=20=D0=B7=D0=B0=D0=BA=D0=B0=D0=B7?= =?UTF-8?q?=D0=B0=D0=BC=20=D0=B8=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82?= =?UTF-8?q?=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Model/Observer/OrderCreate.php | 37 +++---- src/Model/Service/Customer.php | 44 ++++++++- src/Model/Service/Order.php | 15 ++- .../Unit/Model/Observer/OrderCreateTest.php | 97 +++++++++++++++++++ src/Test/Unit/Model/Service/OrderTest.php | 24 ++--- 5 files changed, 176 insertions(+), 41 deletions(-) diff --git a/src/Model/Observer/OrderCreate.php b/src/Model/Observer/OrderCreate.php index 3aa543e..7e5adea 100644 --- a/src/Model/Observer/OrderCreate.php +++ b/src/Model/Observer/OrderCreate.php @@ -15,6 +15,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface private $registry; private $order; private $serviceOrder; + private $serviceCustomer; /** * Constructor @@ -22,6 +23,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface * @param \Magento\Framework\Registry $registry * @param \Retailcrm\Retailcrm\Model\Logger\Logger $logger * @param \Retailcrm\Retailcrm\Model\Service\Order $serviceOrder + * @param \Retailcrm\Retailcrm\Model\Service\Customer $serviceCustomer * @param Helper $helper * @param ApiClient $api */ @@ -29,12 +31,14 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface \Magento\Framework\Registry $registry, \Retailcrm\Retailcrm\Model\Logger\Logger $logger, \Retailcrm\Retailcrm\Model\Service\Order $serviceOrder, + \Retailcrm\Retailcrm\Model\Service\Customer $serviceCustomer, Helper $helper, ApiClient $api ) { $this->logger = $logger; $this->registry = $registry; $this->serviceOrder = $serviceOrder; + $this->serviceCustomer = $serviceCustomer; $this->helper = $helper; $this->api = $api; $this->order = []; @@ -55,6 +59,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface return false; } + /** @var \Magento\Sales\Model\Order $order */ $order = $observer->getEvent()->getOrder(); $this->api->setSite($this->helper->getSite($order->getStore())); @@ -62,13 +67,8 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface return false; } - $billingAddress = $order->getBillingAddress(); $this->order = $this->serviceOrder->process($order); - - $this->setCustomer( - $order, - $billingAddress - ); + $this->setCustomer($order); Helper::filterRecursive($this->order); @@ -80,16 +80,22 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface } /** - * @param $order - * @param $billingAddress + * @param \Magento\Sales\Model\Order $order + * @param \Magento\Sales\Model\Order\Address $billingAddress */ - private function setCustomer($order, $billingAddress) + private function setCustomer($order) { if ($order->getCustomerIsGuest() == 1) { $customer = $this->getCustomerByEmail($order->getCustomerEmail()); if ($customer !== false) { $this->order['customer']['id'] = $customer['id']; + } else { + $newCustomer = $this->serviceCustomer->prepareCustomerFromOrder($order); + $response = $this->api->customersCreate($newCustomer); + if ($response && isset($response['id'])) { + $this->order['customer']['id'] = $response['id']; + } } } @@ -97,18 +103,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface if ($this->existsInCrm($order->getCustomerId(), 'customersGet')) { $this->order['customer']['externalId'] = $order->getCustomerId(); } else { - $preparedCustomer = [ - 'externalId' => $order->getCustomerId(), - 'firstName' => $order->getCustomerFirstname(), - 'lastName' => $order->getCustomerLastname(), - 'email' => $order->getCustomerEmail() - ]; - - if ($billingAddress->getTelephone()) { - $preparedCustomer['phones'][] = [ - 'number' => $billingAddress->getTelephone() - ]; - } + $preparedCustomer = $this->serviceCustomer->process($order->getCustomer()); if ($this->api->customersCreate($preparedCustomer)) { $this->order['customer']['externalId'] = $order->getCustomerId(); diff --git a/src/Model/Service/Customer.php b/src/Model/Service/Customer.php index bce18f8..1ba449c 100644 --- a/src/Model/Service/Customer.php +++ b/src/Model/Service/Customer.php @@ -25,7 +25,6 @@ class Customer implements CustomerManagerInterface * @param \Magento\Customer\Model\Customer $customer * * @return array $preparedCustomer - * @throws \Magento\Framework\Exception\LocalizedException */ public function process(\Magento\Customer\Model\Customer $customer) { @@ -60,6 +59,49 @@ class Customer implements CustomerManagerInterface return $preparedCustomer; } + /** + * @param \Magento\Sales\Model\Order $order + * + * @return array + */ + public function prepareCustomerFromOrder(\Magento\Sales\Model\Order $order) + { + $billing = $order->getBillingAddress(); + + $preparedCustomer = [ + 'externalId' => uniqid(), + 'email' => $billing->getEmail(), + 'firstName' => $billing->getFirstname(), + 'patronymic' => $billing->getMiddlename(), + 'lastName' => $billing->getLastname(), + 'createdAt' => $order->getCreatedAt(), + 'address' => [ + 'countryIso' => $billing->getCountryId(), + 'index' => $billing->getPostcode(), + 'region' => $billing->getRegion(), + 'city' => $billing->getCity(), + 'street' => $billing->getStreet(), + 'text' => sprintf( + '%s %s %s %s', + $billing->getPostcode(), + $billing->getRegion(), + $billing->getCity(), + $billing->getStreet() + ) + ] + ]; + + if ($billing->getTelephone()) { + $preparedCustomer['phones'] = [ + [ + 'number' => $billing->getTelephone() + ] + ]; + } + + return $preparedCustomer; + } + private function getAddress(\Magento\Customer\Model\Customer $customer) { $billingAddress = $customer->getDefaultBillingAddress(); diff --git a/src/Model/Service/Order.php b/src/Model/Service/Order.php index a130aac..5b34420 100644 --- a/src/Model/Service/Order.php +++ b/src/Model/Service/Order.php @@ -34,7 +34,6 @@ class Order implements \Retailcrm\Retailcrm\Api\OrderManagerInterface { $items = $order->getAllItems(); $products = $this->addProducts($items); - $billingAddress = $order->getBillingAddress(); $shippingAddress = $order->getShippingAddress(); $shipping = $this->getShippingCode($order->getShippingMethod()); @@ -42,11 +41,11 @@ class Order implements \Retailcrm\Retailcrm\Api\OrderManagerInterface 'externalId' => $order->getId(), 'number' => $order->getRealOrderId(), 'createdAt' => $order->getCreatedAt(), - 'lastName' => $order->getCustomerLastname(), - 'firstName' => $order->getCustomerFirstname(), - 'patronymic' => $order->getCustomerMiddlename(), - 'email' => $order->getCustomerEmail(), - 'phone' => $billingAddress->getTelephone(), + 'lastName' => $shippingAddress->getLastname(), + 'firstName' => $shippingAddress->getFirstname(), + 'patronymic' => $shippingAddress->getMiddlename(), + 'email' => $shippingAddress->getEmail(), + 'phone' => $shippingAddress->getTelephone(), 'status' => $this->config->getValue('retailcrm/retailcrm_status/' . $order->getStatus()), 'items' => $products, 'delivery' => [ @@ -73,8 +72,8 @@ class Order implements \Retailcrm\Retailcrm\Api\OrderManagerInterface ] ]; - if ($billingAddress->getData('country_id')) { - $preparedOrder['countryIso'] = $billingAddress->getData('country_id'); + if ($shippingAddress->getData('country_id')) { + $preparedOrder['countryIso'] = $shippingAddress->getData('country_id'); } if ($this->helper->getGeneralSettings('api_version') == 'v4') { diff --git a/src/Test/Unit/Model/Observer/OrderCreateTest.php b/src/Test/Unit/Model/Observer/OrderCreateTest.php index 2899799..c734619 100644 --- a/src/Test/Unit/Model/Observer/OrderCreateTest.php +++ b/src/Test/Unit/Model/Observer/OrderCreateTest.php @@ -20,6 +20,8 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase private $mockLogger; private $mockServiceOrder; private $mockHelper; + private $mockServiceCustomer; + private $mockCustomer; public function setUp() { @@ -56,6 +58,12 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase $this->mockOrder = $this->getMockBuilder(\Magento\Sales\Model\Order::class) ->disableOriginalConstructor() + ->setMethods([ + 'getCustomer', + 'getId', + 'getBillingAddress', + 'getStore' + ]) ->getMock(); $this->mockStore = $this->getMockBuilder(\Magento\Store\Model\Store::class) @@ -81,10 +89,62 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase ->disableOriginalConstructor() ->getMock(); + $this->mockServiceCustomer = $this->getMockBuilder(\Retailcrm\Retailcrm\Model\Service\Customer::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->mockServiceCustomer->expects($this->any())->method('process')->willReturn($this->getCustomerTestData()); + $this->mockServiceCustomer + ->expects($this->any()) + ->method('prepareCustomerFromOrder') + ->willReturn( + $this->getCustomerTestData() + ); + + $this->mockCustomer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class) + ->disableOriginalConstructor() + ->setMethods([ + 'getId', + 'getEmail', + 'getFirstname', + 'getMiddlename', + 'getLastname', + 'getStore', + 'getGender', + 'getDob', + 'getDefaultBillingAddress', + 'getAddressesCollection' + ]) + ->getMock(); + + $testData = $this->getAfterSaveCustomerTestData(); + + // mock Customer + $this->mockCustomer->expects($this->any()) + ->method('getId') + ->willReturn($testData['id']); + + $this->mockCustomer->expects($this->any()) + ->method('getEmail') + ->willReturn($testData['email']); + + $this->mockCustomer->expects($this->any()) + ->method('getFirstname') + ->willReturn($testData['firstname']); + + $this->mockCustomer->expects($this->any()) + ->method('getMiddlename') + ->willReturn($testData['middlename']); + + $this->mockCustomer->expects($this->any()) + ->method('getLastname') + ->willReturn($testData['lastname']); + $this->unit = new \Retailcrm\Retailcrm\Model\Observer\OrderCreate( $this->mockRegistry, $this->mockLogger, $this->mockServiceOrder, + $this->mockServiceCustomer, $this->mockHelper, $this->mockApi ); @@ -177,6 +237,10 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase ->method('getStore') ->willReturn($this->mockStore); + $this->mockOrder->expects($this->any()) + ->method('getCustomer') + ->willReturn($this->mockCustomer); + // mock Event $this->mockEvent->expects($this->any()) ->method('getOrder') @@ -379,4 +443,37 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase ] ]; } + + /** + * Get test customer data + * + * @return array + */ + private function getAfterSaveCustomerTestData() + { + return [ + 'id' => 1, + 'email' => 'test@mail.com', + 'firstname' => 'TestFirstname', + 'lastname' => 'Testlastname', + 'middlename' => 'Testmiddlename', + 'birthday' => '1990-01-01', + 'gender' => 1 + ]; + } + + /** + * @return array + */ + private function getCustomerTestData() + { + return [ + 'externalId' => 1, + 'email' => 'test@mail.com', + 'firstName' => 'TestFirstname', + 'lastName' => 'Testlastname', + 'patronymic' => 'Testmiddlename', + 'createdAt' => \date('Y-m-d H:i:s') + ]; + } } diff --git a/src/Test/Unit/Model/Service/OrderTest.php b/src/Test/Unit/Model/Service/OrderTest.php index e568740..321232d 100644 --- a/src/Test/Unit/Model/Service/OrderTest.php +++ b/src/Test/Unit/Model/Service/OrderTest.php @@ -90,7 +90,14 @@ class OrderTest extends \PHPUnit\Framework\TestCase $mockShippingAddress = $this->getMockBuilder(\Magento\Sales\Model\Order\Address::class) ->disableOriginalConstructor() - ->setMethods(['getData']) + ->setMethods([ + 'getData', + 'getTelephone', + 'getFirstname', + 'getLastname', + 'getMiddlename', + 'getEmail' + ]) ->getMock(); $mockShippingAddress->expects($this->any()) ->method('getData') @@ -103,11 +110,11 @@ class OrderTest extends \PHPUnit\Framework\TestCase )) ->will($this->returnCallback([$this, 'getCallbackDataAddress'])); - $mockBillingAddress = $this->getMockBuilder(\Magento\Sales\Model\Order\Address::class) - ->disableOriginalConstructor() - ->setMethods(['getTelephone']) - ->getMock(); - $mockBillingAddress->expects($this->any())->method('getTelephone')->willReturn('89000000000'); + $mockShippingAddress->expects($this->any())->method('getTelephone')->willReturn('89000000000'); + $mockShippingAddress->expects($this->any())->method('getFirstname')->willReturn('Test'); + $mockShippingAddress->expects($this->any())->method('getLastname')->willReturn('Test'); + $mockShippingAddress->expects($this->any())->method('getMiddlename')->willReturn('Test'); + $mockShippingAddress->expects($this->any())->method('getEmail')->willReturn('test@mail.com'); $mockPaymentMethod = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class) ->disableOriginalConstructor() @@ -121,16 +128,11 @@ class OrderTest extends \PHPUnit\Framework\TestCase $mockPayment->expects($this->any())->method('getMethodInstance')->willReturn($mockPaymentMethod); $this->mockOrder->expects($this->any())->method('getAllItems')->willReturn([$mockItem]); - $this->mockOrder->expects($this->any())->method('getBillingAddress')->willReturn($mockBillingAddress); $this->mockOrder->expects($this->any())->method('getShippingAddress')->willReturn($mockShippingAddress); $this->mockOrder->expects($this->any())->method('getShippingMethod')->willReturn('flatrate_flatrate'); $this->mockOrder->expects($this->any())->method('getId')->willReturn(1); $this->mockOrder->expects($this->any())->method('getRealOrderId')->willReturn('000000001'); $this->mockOrder->expects($this->any())->method('getCreatedAt')->willReturn(date('Y-m-d H:i:s')); - $this->mockOrder->expects($this->any())->method('getCustomerLastname')->willReturn('Test'); - $this->mockOrder->expects($this->any())->method('getCustomerFirstname')->willReturn(date('Test')); - $this->mockOrder->expects($this->any())->method('getCustomerMiddlename')->willReturn(date('Test')); - $this->mockOrder->expects($this->any())->method('getCustomerEmail')->willReturn('test@mail.com'); $this->mockOrder->expects($this->any())->method('getStatus')->willReturn('processing'); $this->mockOrder->expects($this->any())->method('getShippingAmount')->willReturn(100); $this->mockOrder->expects($this->any())->method('getDiscountAmount')->willReturn(0); From 2c8a87c582f70d8e91b2c026a85742a6b0c545e6 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Tue, 22 Jan 2019 10:00:52 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9=D0=BA?= =?UTF-8?q?=D0=B8=20Daemon=20Collector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Block/Frontend/DaemonCollector.php | 89 +++++++++++++++++++ src/Helper/Data.php | 72 +++++++++++++++ src/Model/Setting/DaemonCollector.php | 14 +++ .../Block/Frontend/DaemonCollectorTest.php | 75 ++++++++++++++++ src/etc/adminhtml/system.xml | 10 +++ src/etc/crontab.xml | 2 - src/i18n/es_ES.csv | 4 +- src/i18n/ru_RU.csv | 4 +- src/view/frontend/layout/default.xml | 8 ++ .../layout/retailcrm_index_display.xml | 5 ++ .../frontend/templates/daemon_collector.phtml | 1 + 11 files changed, 280 insertions(+), 4 deletions(-) create mode 100644 src/Block/Frontend/DaemonCollector.php create mode 100644 src/Model/Setting/DaemonCollector.php create mode 100644 src/Test/Unit/Block/Frontend/DaemonCollectorTest.php create mode 100644 src/view/frontend/layout/default.xml create mode 100644 src/view/frontend/templates/daemon_collector.phtml diff --git a/src/Block/Frontend/DaemonCollector.php b/src/Block/Frontend/DaemonCollector.php new file mode 100644 index 0000000..27f45d5 --- /dev/null +++ b/src/Block/Frontend/DaemonCollector.php @@ -0,0 +1,89 @@ + + (function(_,r,e,t,a,i,l){_['retailCRMObject']=a;_[a]=_[a]||function(){(_[a].q=_[a].q||[]).push(arguments)};_[a].l=1*new Date();l=r.getElementsByTagName(e)[0];i=r.createElement(e);i.async=!0;i.src=t;l.parentNode.insertBefore(i,l)})(window,document,'script','https://collector.retailcrm.pro/w.js','_rc'); + {{ code }} + _rc('send', 'pageView'); + +EOT; + + /** + * DaemonCollector constructor. + * + * @param \Magento\Framework\View\Element\Template\Context $context + * @param \Magento\Customer\Model\Session $customerSession + * @param \Magento\Store\Model\StoreManagerInterface $storeManager + * @param \Magento\Store\Api\StoreResolverInterface $storeResolver + * @param \Retailcrm\Retailcrm\Helper\Data $helper + * @param array $data + */ + public function __construct( + \Magento\Framework\View\Element\Template\Context $context, + \Magento\Customer\Model\Session $customerSession, + \Magento\Store\Model\StoreManagerInterface $storeManager, + \Magento\Store\Api\StoreResolverInterface $storeResolver, + \Retailcrm\Retailcrm\Helper\Data $helper, + array $data = [] + ) { + parent::__construct($context, $data); + $this->customer = $customerSession->getCustomer(); + $this->storeManager = $storeManager; + $this->storeResolver = $storeResolver; + $this->helper = $helper; + } + + /** + * @return string + */ + public function getJs() + { + return $this->js; + } + + /** + * @return $this + */ + public function buildScript() + { + $params = array(); + + if ($this->customer->getId()) { + $params['customerId'] = $this->customer->getId(); + } + + try { + $siteKey = $this->helper->getSiteKey( + $this->storeManager->getStore( + $this->storeResolver->getCurrentStoreId() + )->getWebsiteId() + ); + } catch (\Magento\Framework\Exception\NoSuchEntityException $entityException) { + return $this; + } + + if ($siteKey) { + $this->js = preg_replace( + '/{{ code }}/', + sprintf( + "\t_rc('create', '%s', %s);\n", + $siteKey, + json_encode((object) $params) + ), + $this->template + ); + } + + return $this; + } +} diff --git a/src/Helper/Data.php b/src/Helper/Data.php index bfe613b..91792c5 100644 --- a/src/Helper/Data.php +++ b/src/Helper/Data.php @@ -2,6 +2,7 @@ namespace Retailcrm\Retailcrm\Helper; +use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Store\Model\StoreManagerInterface; use Magento\Framework\App\Helper\Context; @@ -14,6 +15,7 @@ class Data extends AbstractHelper const XML_PATH_RETAILCRM = 'retailcrm/'; const XML_PATH_DEFAULT_SITE = 'retailcrm_site/default'; const XML_PATH_SITES = 'retailcrm_sites/'; + const XML_PATH_DAEMON_COLLECTOR = 'daemon_collector/'; public function __construct( Context $context, @@ -45,6 +47,7 @@ class Data extends AbstractHelper * @param $store * * @return mixed|null + * @throws \Exception */ public function getSite($store) { @@ -70,6 +73,9 @@ class Data extends AbstractHelper return $websitesConfig; } + /** + * @return array + */ public function getMappingSites() { $sites = []; @@ -90,6 +96,72 @@ class Data extends AbstractHelper return $sites; } + /** + * @param $website + * + * @return array|bool + */ + public function getDaemonCollector($website) + { + $forWebsite = $this->scopeConfig->getValue( + self::XML_PATH_RETAILCRM . self::XML_PATH_DAEMON_COLLECTOR . 'active', + ScopeInterface::SCOPE_WEBSITES, + $website + ); + + if ($forWebsite) { + return [ + 'website' => $website, + 'active' => $forWebsite + ]; + } + + $forDefault = $this->scopeConfig->getValue( + self::XML_PATH_RETAILCRM . self::XML_PATH_DAEMON_COLLECTOR . 'active', + ScopeConfigInterface::SCOPE_TYPE_DEFAULT + ); + + if ($forDefault) { + return [ + 'website' => 0, + 'active' => $forDefault + ]; + } + + return false; + } + + /** + * @param $website + * + * @return bool|mixed + */ + public function getSiteKey($website) + { + $daemonCollector = $this->getDaemonCollector($website); + + if ($daemonCollector === false) { + return false; + } + + if ($daemonCollector['active']) { + if ($daemonCollector['website'] > 0) { + return $this->scopeConfig->getValue( + self::XML_PATH_RETAILCRM . self::XML_PATH_DAEMON_COLLECTOR . 'key', + ScopeInterface::SCOPE_WEBSITES, + $website + ); + } else { + return $this->scopeConfig->getValue( + self::XML_PATH_RETAILCRM . self::XML_PATH_DAEMON_COLLECTOR . 'key', + ScopeConfigInterface::SCOPE_TYPE_DEFAULT + ); + } + } + + return false; + } + /** * Recursive array filter * diff --git a/src/Model/Setting/DaemonCollector.php b/src/Model/Setting/DaemonCollector.php new file mode 100644 index 0000000..fc838ca --- /dev/null +++ b/src/Model/Setting/DaemonCollector.php @@ -0,0 +1,14 @@ + true, 'label' => __('Yes')], + ['value' => false, 'label' => __('No')] + ]; + } +} diff --git a/src/Test/Unit/Block/Frontend/DaemonCollectorTest.php b/src/Test/Unit/Block/Frontend/DaemonCollectorTest.php new file mode 100644 index 0000000..46aa1ba --- /dev/null +++ b/src/Test/Unit/Block/Frontend/DaemonCollectorTest.php @@ -0,0 +1,75 @@ +getObject(\Magento\Framework\View\Element\Template\Context::class); + $customerSession = $this->createMock(\Magento\Customer\Model\Session::class); + $storeManager = $this->createMock(\Magento\Store\Model\StoreManager::class); + $storeResolver = $this->createMock(\Magento\Store\Model\StoreResolver::class); + $helper = $this->createMock(\Retailcrm\Retailcrm\Helper\Data::class); + $this->customer = $this->createMock(\Magento\Customer\Model\Customer::class); + $store = $this->createMock(\Magento\Store\Model\Store::class); + + $customerSession->expects($this->any()) + ->method('getCustomer') + ->willReturn($this->customer); + $storeResolver->expects($this->any()) + ->method('getCurrentStoreId') + ->willReturn(1); + $store->expects($this->any()) + ->method('getWebsiteId') + ->willReturn(1); + $storeManager->expects($this->any()) + ->method('getStore') + ->willReturn($store); + $helper->expects($this->any()) + ->method('getSiteKey') + ->willReturn(self::SITE_KEY); + + $this->unit = new \Retailcrm\Retailcrm\Block\Frontend\DaemonCollector( + $context, + $customerSession, + $storeManager, + $storeResolver, + $helper + ); + } + + public function testGetJSWithCustomer() + { + $this->customer->expects($this->any()) + ->method('getId') + ->willReturn(1); + + $js = $this->unit->buildScript()->getJs(); + + $this->assertContains('', $js); + $this->assertContains('_rc(\'send\', \'pageView\');', $js); + $this->assertContains(self::SITE_KEY, $js); + $this->assertContains('customerId', $js); + } + + public function testGetJSWithoutCustomer() + { + $this->customer->expects($this->any()) + ->method('getId') + ->willReturn(null); + + $js = $this->unit->buildScript()->getJs(); + + $this->assertContains('', $js); + $this->assertContains('_rc(\'send\', \'pageView\');', $js); + $this->assertContains(self::SITE_KEY, $js); + $this->assertNotContains('customerId', $js); + } +} diff --git a/src/etc/adminhtml/system.xml b/src/etc/adminhtml/system.xml index 6e48678..8fa6dac 100644 --- a/src/etc/adminhtml/system.xml +++ b/src/etc/adminhtml/system.xml @@ -64,6 +64,16 @@ Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\Sites + + + + + Retailcrm\Retailcrm\Model\Setting\DaemonCollector + + + + + diff --git a/src/etc/crontab.xml b/src/etc/crontab.xml index fe8235d..44e38d3 100644 --- a/src/etc/crontab.xml +++ b/src/etc/crontab.xml @@ -4,10 +4,8 @@ * */4 * * * - */5 * * * * - \ No newline at end of file diff --git a/src/i18n/es_ES.csv b/src/i18n/es_ES.csv index 57780a2..0dc474f 100644 --- a/src/i18n/es_ES.csv +++ b/src/i18n/es_ES.csv @@ -21,4 +21,6 @@ "Incorrect URL of retailCRM or API key","La dirección del retailCRM o la llave API son incorrectos" "The selected API version is unavailable","La versión de la API seleccionada no está disponible" "Send","Enviar" -"Default site","Tienda por defecto" \ No newline at end of file +"Default site","Tienda por defecto" +"Activate","Active" +"Site key","Clave de la página web" \ No newline at end of file diff --git a/src/i18n/ru_RU.csv b/src/i18n/ru_RU.csv index 4dffff2..35339e3 100644 --- a/src/i18n/ru_RU.csv +++ b/src/i18n/ru_RU.csv @@ -21,4 +21,6 @@ "Incorrect URL of retailCRM or API key","Некорректный адрес retailCRM или API ключ" "The selected API version is unavailable","Выбранная версия API недоступна" "Send","Выгрузить" -"Default site","Сайт по умолчанию" \ No newline at end of file +"Default site","Сайт по умолчанию" +"Activate","Активировать" +"Site key","Ключ сайта" \ No newline at end of file diff --git a/src/view/frontend/layout/default.xml b/src/view/frontend/layout/default.xml new file mode 100644 index 0000000..faf05af --- /dev/null +++ b/src/view/frontend/layout/default.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/view/frontend/layout/retailcrm_index_display.xml b/src/view/frontend/layout/retailcrm_index_display.xml index 368d1f7..7e82d05 100644 --- a/src/view/frontend/layout/retailcrm_index_display.xml +++ b/src/view/frontend/layout/retailcrm_index_display.xml @@ -3,4 +3,9 @@ + + + + + \ No newline at end of file diff --git a/src/view/frontend/templates/daemon_collector.phtml b/src/view/frontend/templates/daemon_collector.phtml new file mode 100644 index 0000000..acb8ec6 --- /dev/null +++ b/src/view/frontend/templates/daemon_collector.phtml @@ -0,0 +1 @@ +buildScript()->getJs() ?> \ No newline at end of file