prestashop-module/tests/RetailcrmTest.php

434 lines
13 KiB
PHP
Raw Permalink Normal View History

2018-05-28 17:09:31 +03:00
<?php
/**
* 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();
$this->retailcrmModule = new RetailCRM();
$this->retailcrmModule->api = $this->getApiMock(
[
'customersCreate',
'customersEdit',
'customersGet',
'ordersCreate',
'ordersEdit',
'ordersGet',
'ordersPaymentEdit',
'ordersPaymentCreate',
]
);
2018-05-28 17:09:31 +03:00
}
private function mockMethodsForOrderUpload($crmId, $cmsId, $reference)
{
$this->apiClientMock->expects($this->any())->method('ordersCreate')->willReturn(new RetailcrmApiResponse(
200,
json_encode([
'success' => true,
'id' => $crmId,
'order' => [
'externalId' => $cmsId,
'id' => $crmId,
'number' => $reference,
],
])
));
$this->apiClientMock->expects($this->any())->method('ordersEdit')->willReturn(new RetailcrmApiResponse(
200,
2021-11-03 16:19:39 +07:00
json_encode([
'success' => true,
'id' => $crmId,
2021-11-03 16:19:39 +07:00
'order' => [
'externalId' => $cmsId,
'id' => $crmId,
'number' => $reference,
2021-11-03 16:19:39 +07: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' => [],
])
));
}
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);
RetailcrmExport::$api = $this->retailcrmModule->api;
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, false);
RetailcrmExport::uploadOrders([1]);
$firstUpdOrder = new Order(1);
$this->assertEquals($reference, $firstUpdOrder->reference);
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, true);
RetailcrmExport::uploadOrders([1]);
$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];
$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->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];
$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' => [],
])
));
$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';
$this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn(new RetailcrmApiResponse(
200,
2021-11-03 16:19:39 +07:00
json_encode([
'success' => true,
'order' => [
'number' => $updReference,
],
2021-11-03 16:19:39 +07:00
])
));
$this->mockMethodsForOrderUpload(1, 1, $updReference);
2021-10-20 12:18:08 +03:00
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, false);
$this->assertTrue($this->retailcrmModule->hookActionOrderEdited($params));
$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));
$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
*/
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());
$status = new stdClass();
$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
$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
}
$this->mockMethodsForOrderUpload(1, 1, $updReference);
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);
$this->assertTrue($this->retailcrmModule->hookActionOrderStatusPostUpdate($params));
$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
$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;
$this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn($ordersGet);
2018-05-28 17:09:31 +03:00
$result = $this->retailcrmModule->hookActionPaymentCCAdd($params);
$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
}
}