1
0
mirror of synced 2024-11-28 08:16:07 +03:00
This commit is contained in:
Akolzin Dmitry 2019-02-28 14:45:39 +03:00
parent 54bfa6ceeb
commit 6bb711bc52
12 changed files with 2105 additions and 125 deletions

4
.env.dist Normal file
View File

@ -0,0 +1,4 @@
DB_BITRIX_HOST=""
DB_BITRIX_LOGIN=""
DB_BITRIX_PASS=""
DB_BITRIX_NAME=""

4
.gitignore vendored
View File

@ -9,3 +9,7 @@
/.idea/*
.travis.yml
/vendor/
.env
.phpunit.result.cache

5
Makefile Normal file
View File

@ -0,0 +1,5 @@
test: prepare_module
@php -d short_open_tag=On vendor/bin/phpunit -c phpunit.xml.dist
prepare_module:
@composer pre-module-install

21
composer.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "retailcrm/bitrix-module",
"scripts": {
"pre-module-install": "cp -R intaro.retailcrm $BITRIX_PATH/bitrix/modules"
},
"description": "Integration module for Bitrix & RetailCRM",
"type": "bitrix-module",
"authors": [
{
"name": "RetailDriver LLC",
"email": "integration@retailcrm.ru"
}
],
"require": {
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^7",
"vlucas/phpdotenv": "^3.3"
}
}

1591
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,17 +1,25 @@
<?php
/**
* Class RetailCrmCollector
*/
class RetailCrmCollector
{
public static $MODULE_ID = 'intaro.retailcrm';
public static $CRM_COLL_KEY = 'coll_key';
public static $CRM_COLL = 'collector';
/**
* Add Daemon Collector script
*
* @return bool
*/
public static function add()
{
$keys = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_COLL_KEY, 0));
$collector = COption::GetOptionString(self::$MODULE_ID, self::$CRM_COLL, 0);
if ($collector === 'Y' && !empty($keys[SITE_ID]) && ADMIN_SECTION !== true) {
global $APPLICATION;
global $USER;
$params = array();
@ -24,7 +32,9 @@ class RetailCrmCollector
_rc('create', '" . $keys[SITE_ID] . "', " . json_encode((object) $params) . ");
_rc('send', 'pageView');
</script>";
$APPLICATION->AddHeadString($str, true);
\Bitrix\Main\Page\Asset::getInstance()->addString($str, true);
return true;
} else {
return false;
}

View File

@ -1,9 +1,7 @@
<?php
/**
* RCrmEvent
* Class RetailCrmEvent
*/
use \Bitrix\Main\Event;
class RetailCrmEvent
{
protected static $MODULE_ID = 'intaro.retailcrm';
@ -23,29 +21,28 @@ class RetailCrmEvent
protected static $CRM_SITES_LIST = 'sites_list';
/**
* OnAfterUserUpdate
* @param $arFields
*
* @param mixed $arFields - User arFields
*
* @return mixed
* @return bool
* @throws InvalidArgumentException
*/
function OnAfterUserUpdate($arFields)
{
if (isset($GLOBALS['RETAIL_CRM_HISTORY']) && $GLOBALS['RETAIL_CRM_HISTORY']) {
return;
return false;
}
if (!$arFields['RESULT']) {
return;
return false;
}
$api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0);
$api_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0);
$api = new RetailCrm\ApiClient($api_host, $api_key);
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
$api = new RetailCrm\ApiClient($api_host, $api_key);
$resultOrder = RetailCrmUser::customerEdit($arFields, $api, $optionsSitesList);
if (!$resultOrder) {
RCrmActions::eventLog('RetailCrmEvent::OnAfterUserUpdate', 'RetailCrmUser::customerEdit', 'error update customer');
}
@ -84,31 +81,36 @@ class RetailCrmEvent
}
/**
* orderSave
*
* @param object $event - Order object
* @param $event
*
* @return bool
* @throws InvalidArgumentException
*/
function orderSave($event)
{
if ($GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] !== false && $GLOBALS['RETAIL_CRM_HISTORY'] !== true && $GLOBALS['RETAILCRM_ORDER_DELETE'] !== true) {
if ($GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] === false
&& $GLOBALS['RETAIL_CRM_HISTORY'] === true
&& $GLOBALS['RETAILCRM_ORDER_DELETE'] === true
) {
return false;
}
if (!CModule::IncludeModule('iblock')) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'iblock', 'module not found');
return true;
return false;
}
if (!CModule::IncludeModule("sale")) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'sale', 'module not found');
return true;
return false;
}
if (!CModule::IncludeModule("catalog")) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'catalog', 'module not found');
return true;
return false;
}
//exists getParameter("ENTITY")
@ -119,7 +121,7 @@ class RetailCrmEvent
} else {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'events', 'event error');
return true;
return false;
}
$arOrder = RetailCrmOrder::orderObjToArr($obOrder);
@ -159,7 +161,7 @@ class RetailCrmEvent
if (array_key_exists($arOrder['LID'], $optionsSitesList) && $optionsSitesList[$arOrder['LID']] !== null) {
$site = $optionsSitesList[$arOrder['LID']];
} else {
return;
return false;
}
} elseif (!$optionsSitesList) {
$site = null;
@ -182,7 +184,7 @@ class RetailCrmEvent
if (!$resultUser) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'RetailCrmUser::customerSend', 'error during creating customer');
return true;
return false;
}
}
@ -191,19 +193,18 @@ class RetailCrmEvent
if (!$resultOrder) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'RetailCrmOrder::orderSend', 'error during creating order');
return true;
}
return true;
return false;
}
return true;
}
/**
* paymentSave
* @param \Bitrix\Sale\Payment $event
*
* @param \Bitrix\Sale\Payment $event - Payment object
* @throws InvalidArgumentException
*
* @return bool
*/
function paymentSave($event)
{
@ -216,7 +217,7 @@ class RetailCrmEvent
|| $apiVersion != 'v5'
|| $order->isNew()
) {
return;
return false;
}
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
@ -237,7 +238,7 @@ class RetailCrmEvent
if (array_key_exists($arPayment['LID'], $optionsSitesList) && $optionsSitesList[$arPayment['LID']] !== null) {
$site = $optionsSitesList[$arPayment['LID']];
} else {
return;
return false;
}
} elseif (!$optionsSitesList) {
$site = null;
@ -288,7 +289,7 @@ class RetailCrmEvent
}
} else {
RCrmActions::eventLog('RetailCrmEvent::paymentSave', 'payments', 'OrderID = ' . $arPayment['ID'] . '. Payment not found.');
return;
return false;
}
if (!array_key_exists($arPayment['ID'], $paymentsExternalIds)) {
@ -299,12 +300,14 @@ class RetailCrmEvent
RCrmActions::apiMethod($api, 'ordersPaymentDelete', __METHOD__, $paymentsExternalIds[$arPayment['ID']]['id']);
RCrmActions::apiMethod($api, 'ordersPaymentCreate', __METHOD__, $paymentToCrm, $site);
}
return true;
}
/**
* paymentDelete
* @param \Bitrix\Sale\Payment $event
*
* @param object $event - Payment object
* @throws InvalidArgumentException
*/
function paymentDelete($event)
{

19
phpunit.xml.dist Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="tests/phpunit/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
verbose="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="true"
>
<testsuites>
<testsuite name="Retailcrm Bitrix module Test Suite">
<directory suffix=".php">tests/phpunit</directory>
</testsuite>
</testsuites>
</phpunit>

20
tests/bin/before_script.sh Executable file
View File

@ -0,0 +1,20 @@
#!/usr/bin/env bash
if [ -d $TRAVIS_BUILD_DIR ]; then
BITRIX_PATH="$TRAVIS_BUILD_DIR/bitrix"
fi
download() {
wget http://download.retailcrm.pro/download/modules/bitrix/bitrix.tar.gz
gunzip bitrix.tar.gz
rm bitrix.tar.gz
}
create_db() {
mysqladmin create $DB_BITRIX_NAME --user="$DB_BITRIX_LOGIN" --password="$DB_BITRIX_PASS"
mysql -user="$DB_BITRIX_LOGIN" -password="$DB_BITRIX_PASS" $DB_BITRIX_NAME < $BITRIX_PATH/dump.sql
}
download
create_db

View File

@ -0,0 +1,31 @@
<?php
/**
* PHP version 7.0
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
$dotenv = Dotenv\Dotenv::create(__DIR__ . '/../../');
$dotenv->load();
//if (getenv('TRAVIS_BUILD_DIR')) {
// $_SERVER['DOCUMENT_ROOT'] = getenv('TRAVIS_BUILD_DIR') . '/bitrix';
//} else {
$_SERVER['DOCUMENT_ROOT'] = getenv('BITRIX_PATH');
//}
define('NOT_CHECK_PERMISSIONS', true);
define('NO_AGENT_CHECK', true);
require($_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/prolog_before.php');
global $USER;
$USER->Authorize(1);
if (!CModule::IncludeModule('intaro.retailcrm')) {
RegisterModule('intaro.retailcrm');
}

View File

@ -0,0 +1,32 @@
<?php
class RetailCrmCollectorTest extends \PHPUnit\Framework\TestCase
{
const TEST_KEY = 'RC-XXXXXXXXXX-X';
public function setUp()
{
parent::setUp();
COption::SetOptionString(
RetailCrmCollector::$MODULE_ID,
RetailCrmCollector::$CRM_COLL_KEY,
serialize([SITE_ID => self::TEST_KEY])
);
COption::SetOptionString(
RetailCrmCollector::$MODULE_ID,
RetailCrmCollector::$CRM_COLL,
'Y'
);
}
public function testAdd()
{
RetailCrmCollector::add();
$strings = \Bitrix\Main\Page\Asset::getInstance()->getStrings();
$this->assertContains(self::TEST_KEY, $strings);
$this->assertContains('customerId', $strings);
}
}

View File

@ -0,0 +1,240 @@
<?php
/**
* Class RetailCrmEventTest
*/
class RetailCrmEventTest extends PHPUnit\Framework\TestCase
{
/**
* @var RetailCrmEvent
*/
private $retailcrmEvent;
public function setUp()
{
parent::setUp();
COption::SetOptionString('intaro.retailcrm', 'api_version', 'v5');
CModule::IncludeModule('intaro.retailcrm');
$this->retailcrmEvent = new RetailCrmEvent();
}
/**
* @param $history
* @param $emptyData
*
* @throws InvalidArgumentException
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @dataProvider userUpdateDataProvider
*/
public function testOnAfterUserUpdate($history, $emptyData)
{
$arFields = [
'ID' => 1
];
if ($history === true) {
$GLOBALS['RETAIL_CRM_HISTORY'] = $history;
}
if ($emptyData === true) {
$arFields['RESULT'] = [];
}
$result = $this->retailcrmEvent->OnAfterUserUpdate($arFields);
$this->assertEquals(false, $result);
}
/**
* @param $history
* @param $v5
* @param $new
*
* @throws InvalidArgumentException
* @throws \PHPUnit\Framework\Exception
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \PHPUnit\Framework\MockObject\RuntimeException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @dataProvider paymentSaveDataProvider
*/
public function testPaymentSave($history, $v5, $new)
{
$event = $this->createMock(\Bitrix\Sale\Payment::class);
$order = $this->createMock(\Bitrix\Sale\Order::class);
$order->expects($this->any())
->method('isNew')
->willReturn($new);
$paymentCollection = $this->createMock(\Bitrix\Sale\PaymentCollection::class);
$paymentCollection->expects($this->any())
->method('getOrder')
->willReturn($order);
$event->method('getCollection')->willReturn($paymentCollection);
if ($history === true) {
$GLOBALS['RETAIL_CRM_HISTORY'] = true;
}
if ($v5 === false) {
COption::SetOptionString('intaro.retailcrm', 'api_version', 'v4');
}
$result = $this->retailcrmEvent->paymentSave($event);
$this->assertEquals(false, $result);
}
/**
* @param $history
* @param $v5
*
* @throws InvalidArgumentException
* @throws \PHPUnit\Framework\Exception
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @dataProvider paymentDeleteDataProvider
*/
public function testPaymentDelete($history, $v5)
{
$event = $this->createMock(\Bitrix\Sale\Payment::class);
if ($history === true) {
$GLOBALS['RETAIL_CRM_HISTORY'] = true;
}
if ($v5 === false) {
COption::SetOptionString('intaro.retailcrm', 'api_version', 'v4');
}
$result = $this->retailcrmEvent->paymentDelete($event);
$this->assertEquals(false, $result);
}
/**
*
* @throws InvalidArgumentException
* @throws \PHPUnit\Framework\Exception
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testOrderSave()
{
$GLOBALS['RETAIL_CRM_HISTORY'] = true;
$GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] = false;
$GLOBALS['RETAILCRM_ORDER_DELETE'] = true;
$event = $this->createMock(\Bitrix\Main\Event::class);
$result = $this->retailcrmEvent->orderSave($event);
$this->assertEquals(false, $result);
}
/**
* @throws InvalidArgumentException
* @throws \PHPUnit\Framework\Exception
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testOrderDelete()
{
$event = $this->createMock(\Bitrix\Main\Event::class);
$result = $this->retailcrmEvent->OrderDelete($event);
$this->assertEquals(null, $result);
}
/**
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testOnUpdateOrder()
{
$arFields = [];
$result = $this->retailcrmEvent->OnUpdateOrder(1, $arFields);
$this->assertEquals(null, $result);
}
/**
* @return array
*/
public function userUpdateDataProvider()
{
return [
[
'history' => true,
'emptyData' => false
],
[
'history' => false,
'emptyData' => true
]
];
}
/**
* @return array
*/
public function paymentSaveDataProvider()
{
return [
[
'history' => true,
'v5' => true,
'new' => false
],
[
'history' => false,
'v5' => false,
'new' => false
],
[
'history' => false,
'v5' => true,
'new' => true
]
];
}
/**
* @return array
*/
public function paymentDeleteDataProvider()
{
return [
[
'history' => true,
'v5' => true
],
[
'history' => false,
'v5' => false
],
[
'history' => false,
'v5' => true
]
];
}
public function tearDown()
{
parent::tearDown();
$GLOBALS['RETAIL_CRM_HISTORY'] = false;
$GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] = true;
$GLOBALS['RETAILCRM_ORDER_DELETE'] = false;
}
}