2018-05-28 17:09:31 +03:00
|
|
|
<?php
|
2021-12-06 14:37:43 +03:00
|
|
|
/**
|
|
|
|
* MIT License
|
|
|
|
*
|
|
|
|
* Copyright (c) 2021 DIGITAL RETAIL TECHNOLOGIES SL
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*
|
|
|
|
* DISCLAIMER
|
|
|
|
*
|
|
|
|
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
|
|
|
* versions in the future. If you wish to customize PrestaShop for your
|
|
|
|
* needs please refer to http://www.prestashop.com for more information.
|
|
|
|
*
|
|
|
|
* @author DIGITAL RETAIL TECHNOLOGIES SL <mail@simlachat.com>
|
|
|
|
* @copyright 2021 DIGITAL RETAIL TECHNOLOGIES SL
|
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License
|
|
|
|
*
|
|
|
|
* Don't forget to prefix your containers with your own identifier
|
|
|
|
* to avoid any conflicts with others containers.
|
|
|
|
*/
|
2018-05-28 17:09:31 +03:00
|
|
|
|
|
|
|
class RetailCRMTest extends RetailcrmTestCase
|
|
|
|
{
|
|
|
|
private $retailcrmModule;
|
|
|
|
|
2021-11-03 16:19:39 +07:00
|
|
|
protected function setUp()
|
2018-05-28 17:09:31 +03:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->setConfig();
|
|
|
|
|
2020-04-23 15:41:11 +03:00
|
|
|
$this->retailcrmModule = new RetailCRM();
|
2021-11-15 15:14:16 +03:00
|
|
|
$this->retailcrmModule->api = $this->getApiMock(
|
|
|
|
[
|
|
|
|
'customersCreate',
|
|
|
|
'customersEdit',
|
|
|
|
'customersGet',
|
|
|
|
'ordersCreate',
|
|
|
|
'ordersEdit',
|
|
|
|
'ordersGet',
|
|
|
|
'ordersPaymentEdit',
|
|
|
|
'ordersPaymentCreate',
|
|
|
|
]
|
|
|
|
);
|
2018-05-28 17:09:31 +03:00
|
|
|
}
|
|
|
|
|
2021-12-06 14:37:43 +03:00
|
|
|
private function mockMethodsForOrderUpload($crmId, $cmsId, $reference)
|
2021-07-30 14:30:51 +03:00
|
|
|
{
|
2021-11-15 15:14:16 +03:00
|
|
|
$this->apiClientMock->expects($this->any())->method('ordersCreate')->willReturn(new RetailcrmApiResponse(
|
|
|
|
200,
|
|
|
|
json_encode([
|
|
|
|
'success' => true,
|
2021-12-06 14:37:43 +03:00
|
|
|
'id' => $crmId,
|
2021-11-15 15:14:16 +03:00
|
|
|
'order' => [
|
2021-12-06 14:37:43 +03:00
|
|
|
'externalId' => $cmsId,
|
|
|
|
'id' => $crmId,
|
|
|
|
'number' => $reference,
|
2021-11-15 15:14:16 +03:00
|
|
|
],
|
|
|
|
])
|
|
|
|
));
|
|
|
|
$this->apiClientMock->expects($this->any())->method('ordersEdit')->willReturn(new RetailcrmApiResponse(
|
2021-07-30 14:30:51 +03:00
|
|
|
200,
|
2021-11-03 16:19:39 +07:00
|
|
|
json_encode([
|
2021-07-30 14:30:51 +03:00
|
|
|
'success' => true,
|
2021-12-06 14:37:43 +03:00
|
|
|
'id' => $crmId,
|
2021-11-03 16:19:39 +07:00
|
|
|
'order' => [
|
2021-12-06 14:37:43 +03:00
|
|
|
'externalId' => $cmsId,
|
|
|
|
'id' => $crmId,
|
|
|
|
'number' => $reference,
|
2021-11-03 16:19:39 +07:00
|
|
|
],
|
|
|
|
])
|
2021-07-30 14:30:51 +03:00
|
|
|
));
|
2021-11-15 15:14:16 +03:00
|
|
|
$this->apiClientMock->expects($this->any())->method('customersGet')->willReturn(new RetailcrmApiResponse(
|
|
|
|
200,
|
|
|
|
json_encode([
|
|
|
|
'success' => true,
|
|
|
|
'customer' => [],
|
|
|
|
])
|
|
|
|
));
|
|
|
|
$this->apiClientMock->expects($this->any())->method('customersCreate')->willReturn(new RetailcrmApiResponse(
|
|
|
|
200,
|
|
|
|
json_encode([
|
|
|
|
'success' => true,
|
|
|
|
'customer' => [],
|
|
|
|
])
|
|
|
|
));
|
|
|
|
$this->apiClientMock->expects($this->any())->method('ordersPaymentCreate')->willReturn(new RetailcrmApiResponse(
|
|
|
|
200,
|
|
|
|
json_encode([
|
|
|
|
'success' => true,
|
|
|
|
'payment' => [],
|
|
|
|
])
|
|
|
|
));
|
2021-12-06 14:37:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUploadOrders()
|
|
|
|
{
|
|
|
|
Configuration::updateValue(RetailCRM::API_URL, 'https://test.test');
|
|
|
|
Configuration::updateValue(RetailCRM::API_KEY, 'test_key');
|
|
|
|
|
|
|
|
$order = new Order(1);
|
|
|
|
$reference = $order->reference;
|
|
|
|
$updReference = 'test';
|
|
|
|
|
|
|
|
$this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn(new RetailcrmApiResponse(
|
|
|
|
200,
|
|
|
|
json_encode([
|
|
|
|
'success' => true,
|
|
|
|
'order' => [],
|
|
|
|
])
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->mockMethodsForOrderUpload(1, 1, $updReference);
|
2021-07-30 14:30:51 +03:00
|
|
|
|
2022-05-30 18:20:21 +03:00
|
|
|
RetailcrmExport::$api = $this->retailcrmModule->api;
|
|
|
|
|
2021-07-30 14:30:51 +03:00
|
|
|
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, false);
|
2022-05-30 18:20:21 +03:00
|
|
|
RetailcrmExport::uploadOrders([1]);
|
2021-07-30 14:30:51 +03:00
|
|
|
$firstUpdOrder = new Order(1);
|
|
|
|
|
|
|
|
$this->assertEquals($reference, $firstUpdOrder->reference);
|
|
|
|
|
|
|
|
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, true);
|
2022-05-30 18:20:21 +03:00
|
|
|
RetailcrmExport::uploadOrders([1]);
|
2021-07-30 14:30:51 +03:00
|
|
|
$secondUpdOrder = new Order(1);
|
|
|
|
|
|
|
|
$this->assertEquals($updReference, $secondUpdOrder->reference);
|
|
|
|
}
|
|
|
|
|
2018-05-28 17:09:31 +03:00
|
|
|
public function testHookActionCustomerAccountAdd()
|
|
|
|
{
|
|
|
|
$newCustomer = new Customer(1);
|
2021-11-03 16:19:39 +07:00
|
|
|
$params = ['newCustomer' => $newCustomer];
|
2019-06-18 11:54:05 +03:00
|
|
|
|
2021-11-15 15:14:16 +03:00
|
|
|
$this->apiClientMock->expects($this->any())->method('customersGet')->willReturn(new RetailcrmApiResponse(
|
|
|
|
200,
|
|
|
|
json_encode([
|
|
|
|
'success' => true,
|
|
|
|
'customer' => [],
|
|
|
|
])
|
|
|
|
));
|
|
|
|
$this->apiClientMock->expects($this->any())->method('customersCreate')->willReturn(new RetailcrmApiResponse(
|
|
|
|
200,
|
|
|
|
json_encode([
|
|
|
|
'success' => true,
|
|
|
|
'customer' => [],
|
|
|
|
])
|
|
|
|
));
|
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
$this->assertTrue($this->retailcrmModule->hookActionCustomerAccountAdd($params));
|
2018-05-28 17:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testHookActionCustomerAccountUpdate()
|
|
|
|
{
|
|
|
|
$customer = new Customer(1);
|
2021-11-03 16:19:39 +07:00
|
|
|
$params = ['customer' => $customer];
|
2019-06-18 11:54:05 +03:00
|
|
|
|
2021-11-15 15:14:16 +03:00
|
|
|
$this->apiClientMock->expects($this->any())->method('customersGet')->willReturn(new RetailcrmApiResponse(
|
|
|
|
200,
|
|
|
|
json_encode([
|
|
|
|
'success' => true,
|
|
|
|
'customer' => [
|
|
|
|
'phones' => [],
|
|
|
|
],
|
|
|
|
])
|
|
|
|
));
|
|
|
|
$this->apiClientMock->expects($this->any())->method('customersEdit')->willReturn(new RetailcrmApiResponse(
|
|
|
|
200,
|
|
|
|
json_encode([
|
|
|
|
'success' => true,
|
|
|
|
'customer' => [],
|
|
|
|
])
|
|
|
|
));
|
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
$this->assertTrue($this->retailcrmModule->hookActionCustomerAccountUpdate($params));
|
2018-05-28 17:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testHookActionOrderEdited()
|
|
|
|
{
|
|
|
|
$order = new Order(1);
|
|
|
|
$customer = new Customer($order->id_customer);
|
2021-11-03 16:19:39 +07:00
|
|
|
$params = ['order' => $order, 'customer' => $customer];
|
2021-10-20 12:18:08 +03:00
|
|
|
$reference = $order->reference;
|
|
|
|
$updReference = 'test';
|
|
|
|
|
2021-11-15 15:14:16 +03:00
|
|
|
$this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn(new RetailcrmApiResponse(
|
2020-04-23 15:41:11 +03:00
|
|
|
200,
|
2021-11-03 16:19:39 +07:00
|
|
|
json_encode([
|
2020-04-23 15:41:11 +03:00
|
|
|
'success' => true,
|
2021-11-15 15:14:16 +03:00
|
|
|
'order' => [
|
|
|
|
'number' => $updReference,
|
|
|
|
],
|
2021-11-03 16:19:39 +07:00
|
|
|
])
|
2020-04-23 15:41:11 +03:00
|
|
|
));
|
2021-12-06 14:37:43 +03:00
|
|
|
|
|
|
|
$this->mockMethodsForOrderUpload(1, 1, $updReference);
|
2021-10-20 12:18:08 +03:00
|
|
|
|
|
|
|
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, false);
|
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
$this->assertTrue($this->retailcrmModule->hookActionOrderEdited($params));
|
2021-11-15 15:14:16 +03:00
|
|
|
$order = new Order(1);
|
2021-10-20 12:18:08 +03:00
|
|
|
$this->assertEquals($reference, $order->reference);
|
|
|
|
|
|
|
|
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, true);
|
|
|
|
|
|
|
|
$this->assertTrue($this->retailcrmModule->hookActionOrderEdited($params));
|
2021-11-15 15:14:16 +03:00
|
|
|
$order = new Order(1);
|
2021-10-20 12:18:08 +03:00
|
|
|
$this->assertEquals($updReference, $order->reference);
|
2018-05-28 17:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $newOrder
|
2022-09-09 12:47:47 +03:00
|
|
|
*
|
2018-05-28 17:09:31 +03:00
|
|
|
* @dataProvider dataProvider
|
|
|
|
*/
|
2020-04-16 15:26:03 +03:00
|
|
|
public function testHookActionOrderStatusPostUpdate($newOrder)
|
2018-05-28 17:09:31 +03:00
|
|
|
{
|
|
|
|
$order = new Order(1);
|
|
|
|
$customer = new Customer($order->id_customer);
|
|
|
|
$cart = $this->createMock('Cart');
|
|
|
|
$cart->expects($this->any())->method('getProducts')->willReturn($this->getProducts());
|
|
|
|
$cart->expects($this->any())->method('getAddressCollection')->willReturn($this->getAddressCollection());
|
2022-03-04 12:40:08 +03:00
|
|
|
$status = new stdClass();
|
2021-07-30 14:30:51 +03:00
|
|
|
$reference = $order->reference;
|
|
|
|
$updReference = 'test';
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2021-11-03 16:19:39 +07:00
|
|
|
if (false === $newOrder) {
|
2018-10-05 17:50:20 +03:00
|
|
|
$status->id = 11;
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2021-11-03 16:19:39 +07:00
|
|
|
$params = [
|
2018-05-28 17:09:31 +03:00
|
|
|
'newOrderStatus' => $status,
|
2021-11-03 16:19:39 +07:00
|
|
|
'id_order' => $order->id,
|
|
|
|
];
|
2018-05-28 17:09:31 +03:00
|
|
|
} else {
|
|
|
|
$status->id = 'new';
|
|
|
|
|
2021-11-03 16:19:39 +07:00
|
|
|
$params = [
|
2018-05-28 17:09:31 +03:00
|
|
|
'orderStatus' => $status,
|
|
|
|
'customer' => $customer,
|
|
|
|
'order' => $order,
|
|
|
|
'cart' => $cart,
|
2021-11-03 16:19:39 +07:00
|
|
|
];
|
2021-10-20 12:18:08 +03:00
|
|
|
|
2021-11-15 15:14:16 +03:00
|
|
|
$this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn(new RetailcrmApiResponse(
|
2021-10-20 12:18:08 +03:00
|
|
|
200,
|
2021-11-03 16:19:39 +07:00
|
|
|
json_encode([
|
2021-10-20 12:18:08 +03:00
|
|
|
'success' => true,
|
2021-11-03 16:19:39 +07:00
|
|
|
'order' => [],
|
|
|
|
])
|
2021-10-20 12:18:08 +03:00
|
|
|
));
|
2018-05-28 17:09:31 +03:00
|
|
|
}
|
|
|
|
|
2021-12-06 14:37:43 +03:00
|
|
|
$this->mockMethodsForOrderUpload(1, 1, $updReference);
|
2021-07-30 14:30:51 +03:00
|
|
|
|
|
|
|
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, false);
|
|
|
|
|
|
|
|
$this->assertTrue($this->retailcrmModule->hookActionOrderStatusPostUpdate($params));
|
|
|
|
$this->assertEquals($reference, $order->reference);
|
|
|
|
|
|
|
|
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, true);
|
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
$this->assertTrue($this->retailcrmModule->hookActionOrderStatusPostUpdate($params));
|
2021-07-30 14:30:51 +03:00
|
|
|
$this->assertEquals($updReference, $order->reference);
|
2018-05-28 17:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $ordersGet
|
2022-09-09 12:47:47 +03:00
|
|
|
*
|
2018-05-28 17:09:31 +03:00
|
|
|
* @dataProvider ordersGetDataProvider
|
|
|
|
*/
|
|
|
|
public function testHookActionPaymentCCAdd($ordersGet)
|
|
|
|
{
|
|
|
|
$order = new Order(1);
|
|
|
|
|
|
|
|
$orderPayment = RetailcrmTestHelper::createOrderPayment($order->reference);
|
|
|
|
$cart = new Cart($order->id_cart);
|
|
|
|
|
2021-11-03 16:19:39 +07:00
|
|
|
$params = [
|
2018-05-28 17:09:31 +03:00
|
|
|
'paymentCC' => $orderPayment,
|
2021-11-03 16:19:39 +07:00
|
|
|
'cart' => $cart,
|
|
|
|
];
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2021-11-15 15:14:16 +03:00
|
|
|
$this->apiClientMock->expects($this->any())->method('ordersPaymentCreate')->willReturn(new RetailcrmApiResponse(
|
|
|
|
200,
|
|
|
|
json_encode([
|
|
|
|
'success' => true,
|
|
|
|
'payment' => [],
|
|
|
|
])
|
|
|
|
));
|
|
|
|
$this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn(new RetailcrmApiResponse(
|
|
|
|
200,
|
|
|
|
json_encode([
|
|
|
|
'success' => true,
|
|
|
|
'order' => [],
|
|
|
|
])
|
|
|
|
));
|
|
|
|
|
2018-05-28 17:09:31 +03:00
|
|
|
$referenceMock = $this->createMock('RetailcrmReferences');
|
|
|
|
$referenceMock->expects($this->once())->method('getSystemPaymentModules')->willReturn($this->getSystemPaymentModules());
|
|
|
|
$this->retailcrmModule->reference = $referenceMock;
|
2021-11-15 15:14:16 +03:00
|
|
|
$this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn($ordersGet);
|
2018-05-28 17:09:31 +03:00
|
|
|
|
|
|
|
$result = $this->retailcrmModule->hookActionPaymentCCAdd($params);
|
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
$this->assertInternalType('bool', $result);
|
|
|
|
$this->assertTrue($result);
|
2018-05-28 17:09:31 +03:00
|
|
|
|
|
|
|
RetailcrmTestHelper::deleteOrderPayment($orderPayment->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function dataProvider()
|
|
|
|
{
|
2021-11-03 16:19:39 +07:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
'newOrder' => true,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'newOrder' => false,
|
|
|
|
],
|
|
|
|
];
|
2018-05-28 17:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function ordersGetDataProvider()
|
|
|
|
{
|
2021-11-03 16:19:39 +07:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
'ordersGet' => [
|
2018-05-28 17:09:31 +03:00
|
|
|
'success' => true,
|
2021-11-03 16:19:39 +07:00
|
|
|
'order' => [
|
|
|
|
'payments' => [
|
|
|
|
[
|
|
|
|
'type' => 'bankwire',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'totalSumm' => 1500,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'ordersGet' => [
|
2018-05-28 17:09:31 +03:00
|
|
|
'success' => true,
|
2021-11-03 16:19:39 +07:00
|
|
|
'order' => [
|
|
|
|
'payments' => [
|
|
|
|
[
|
|
|
|
'type' => 'cheque',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'totalSumm' => 1500,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
2018-05-28 17:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getProducts()
|
|
|
|
{
|
2021-11-03 16:19:39 +07:00
|
|
|
return [
|
|
|
|
[
|
2018-05-28 17:09:31 +03:00
|
|
|
'id_product_attribute' => 1,
|
|
|
|
'id_product' => 1,
|
|
|
|
'attributes' => '',
|
|
|
|
'rate' => 1,
|
|
|
|
'price' => 100,
|
|
|
|
'name' => 'Test product 1',
|
2021-11-03 16:19:39 +07:00
|
|
|
'quantity' => 2,
|
|
|
|
],
|
|
|
|
[
|
2018-05-28 17:09:31 +03:00
|
|
|
'id_product_attribute' => 1,
|
|
|
|
'id_product' => 2,
|
|
|
|
'attributes' => '',
|
|
|
|
'rate' => 1,
|
|
|
|
'price' => 100,
|
|
|
|
'name' => 'Test product 2',
|
2021-11-03 16:19:39 +07:00
|
|
|
'quantity' => 1,
|
|
|
|
],
|
|
|
|
];
|
2018-05-28 17:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getAddressCollection()
|
|
|
|
{
|
|
|
|
$address = new Address(1);
|
|
|
|
|
2021-11-03 16:19:39 +07:00
|
|
|
return [$address];
|
2018-05-28 17:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getSystemPaymentModules()
|
|
|
|
{
|
2021-11-03 16:19:39 +07:00
|
|
|
return [
|
|
|
|
[
|
2018-05-28 17:09:31 +03:00
|
|
|
'id' => '3',
|
|
|
|
'code' => 'bankwire',
|
|
|
|
'name' => 'Bank wire',
|
2021-11-03 16:19:39 +07:00
|
|
|
],
|
|
|
|
[
|
2018-05-28 17:09:31 +03:00
|
|
|
'id' => '30',
|
|
|
|
'code' => 'cheque',
|
|
|
|
'name' => 'Payment by check',
|
2021-11-03 16:19:39 +07:00
|
|
|
],
|
|
|
|
];
|
2018-05-28 17:09:31 +03:00
|
|
|
}
|
|
|
|
}
|