From 624c8a1579ff90b7a52b9004024ffbb3dfd60a73 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Wed, 31 Jul 2024 23:12:30 +0200 Subject: [PATCH 01/14] feature: send data Tracking for Alma Order --- alma/alma.php | 11 +- .../hook/ActionObjectUpdateAfter.php | 109 ++++++++ alma/lib/Factories/CarrierFactory.php | 41 +-- alma/lib/Factories/OrderFactory.php | 17 ++ alma/lib/Helpers/HookHelper.php | 1 + .../hook/ActionObjectUpdateAfterTest.php | 235 ++++++++++++++++++ 6 files changed, 377 insertions(+), 37 deletions(-) create mode 100644 alma/controllers/hook/ActionObjectUpdateAfter.php create mode 100644 alma/lib/Factories/OrderFactory.php create mode 100644 alma/tests/Unit/controllers/hook/ActionObjectUpdateAfterTest.php diff --git a/alma/alma.php b/alma/alma.php index 7678ac6d..ad14bc6a 100644 --- a/alma/alma.php +++ b/alma/alma.php @@ -313,7 +313,7 @@ public function registerHooks() private function updateCarriersWithAlma() { $id_module = $this->id; - $id_shop = (int) $this->context->shop->id; + $id_shop = (int)$this->context->shop->id; $id_lang = $this->context->language->id; $carriers = Carrier::getCarriers($id_lang, false, false, false, null, Carrier::ALL_CARRIERS); $values = null; @@ -754,4 +754,13 @@ public function hookActionGetProductPropertiesBefore($params) { return $this->runHookController('actionGetProductPropertiesBefore', $params); } + + public function hookActionObjectUpdateAfter($params) + { + $orderFactory = new Alma\PrestaShop\Factories\OrderFactory(); + $clientHelper = new Alma\PrestaShop\Helpers\ClientHelper(); + $carrierFactory = new Alma\PrestaShop\Factories\CarrierFactory(); + $actionObjectUpdateAfter = new Alma\PrestaShop\Controllers\Hook\ActionObjectUpdateAfter($orderFactory, $clientHelper, $carrierFactory); + $actionObjectUpdateAfter->run($params); + } } diff --git a/alma/controllers/hook/ActionObjectUpdateAfter.php b/alma/controllers/hook/ActionObjectUpdateAfter.php new file mode 100644 index 00000000..424eebb2 --- /dev/null +++ b/alma/controllers/hook/ActionObjectUpdateAfter.php @@ -0,0 +1,109 @@ +orderFactory = $orderFactory; + $this->clientHelper = $clientHelper; + $this->carrierFactory = $carrierFactory; + } + + /** + * @param $params + * @return void + */ + public function run($params) + { + Logger::instance()->info('[Alma] - Start Run'); + if ( + !isset($params['object']) || + !($params['object'] instanceof \OrderCarrierCore) + ) { + return; + } + Logger::instance()->info('[Alma] - it s OrderCarrierCore object'); + + /** @var \OrderCarrier $orderCarrier */ + $orderCarrier = $params['object']; + $idOrder = $orderCarrier->id_order; + Logger::instance()->info('[Alma] - id order ' . $idOrder); + + /** @var \OrderCore $order */ + try { + $order = $this->orderFactory->create($idOrder); + } catch (\PrestaShopException $e) { + Logger::instance()->info('[Alma] - PrestaShopException - Impossible to get Order with id :' . $idOrder); + return; + } + if ($order->module != ConstantsHelper::ALMA_MODULE_NAME || empty($order->getOrderPayments())) { + Logger::instance()->info('[Alma] - To Remove - Order is not Alma or payments are empty'); + Logger::instance()->info('[Alma] - order module' . $order->module); + return; + } + + foreach ($order->getOrderPayments() as $orderPayment) { + /** @var \OrderPayment $orderPayment */ + if (isset($orderPayment->transaction_id)) { + $almaPaymentExternalId = $orderPayment->transaction_id; + break; + } + } + if (!isset($almaPaymentExternalId)) { + Logger::instance()->info('[Alma] - To Remove - No Alma Payment External Id'); + return; + } + + try { + $almaClient = $this->clientHelper->getAlmaClient(); + } catch (ClientException $e) { + Logger::instance()->error('[Alma] - ClientException - ' . $e->getMessage()); + return; + } + + try { + $almaPayment = $almaClient->payments->fetch($almaPaymentExternalId); + + $orderExternalId = null; + foreach ($almaPayment->orders as $almaOrder) { + if ($order->reference === $almaOrder->getMerchantReference()) { + $orderExternalId = $almaOrder->getExternalId(); + } + } + if (!isset($orderExternalId)) { + $almaOrder = $almaClient->payments->addOrder($almaPaymentExternalId, [ + 'merchant_reference' => $order->reference + ] + ); + $orderExternalId = $almaOrder->getExternalId(); + } + $carrier = $this->carrierFactory->create((int) $orderCarrier->id_carrier); + $almaClient->orders->addTracking($orderExternalId, $carrier->name, $orderCarrier->tracking_number, $carrier->url); + } catch (AlmaException $e) { + Logger::instance()->error('[Alma] - AlmaException ' . $e->getMessage()); + } + } +} \ No newline at end of file diff --git a/alma/lib/Factories/CarrierFactory.php b/alma/lib/Factories/CarrierFactory.php index c9066ed3..59dad998 100644 --- a/alma/lib/Factories/CarrierFactory.php +++ b/alma/lib/Factories/CarrierFactory.php @@ -1,46 +1,15 @@ - * @copyright 2018-2024 Alma SAS - * @license https://opensource.org/licenses/MIT The MIT License - */ - namespace Alma\PrestaShop\Factories; -if (!defined('_PS_VERSION_')) { - exit; -} - -/** - * Class CarrierFactory. - */ class CarrierFactory { + /** - * @param int|null $id Carrier ID - * @param int|null $id_lang Language ID - * + * @param int $id * @return \Carrier */ - public function create($id = null, $id_lang = null) + public function create($id) { - return new \Carrier($id, $id_lang); + return new \Carrier($id); } -} +} \ No newline at end of file diff --git a/alma/lib/Factories/OrderFactory.php b/alma/lib/Factories/OrderFactory.php new file mode 100644 index 00000000..c89181af --- /dev/null +++ b/alma/lib/Factories/OrderFactory.php @@ -0,0 +1,17 @@ + 'all', 'actionAdminControllerInitBefore' => 'all', + 'actionObjectUpdateAfter' => 'all', 'header' => 'all', 'displayHeader' => 'all', 'displayBackOfficeHeader' => 'all', diff --git a/alma/tests/Unit/controllers/hook/ActionObjectUpdateAfterTest.php b/alma/tests/Unit/controllers/hook/ActionObjectUpdateAfterTest.php new file mode 100644 index 00000000..44d35760 --- /dev/null +++ b/alma/tests/Unit/controllers/hook/ActionObjectUpdateAfterTest.php @@ -0,0 +1,235 @@ +orderFactory = $this->createMock(OrderFactory::class); + $this->carrierFactory = $this->createMock(CarrierFactory::class); + $carrier = $this->createMock(\Carrier::class); + $carrier->name = 'MyCarrierName'; + $carrier->url = 'myurl'; + $this->carrierFactory->method('create')->willReturn($carrier); + $client = $this->createMock(Client::class); + $this->paymentEndpoint = $this->createMock(Payments::class); + $this->orderEndpoint = $this->createMock(OrdersEndpoint::class); + $client->orders = $this->orderEndpoint; + $client->payments = $this->paymentEndpoint; + + $this->clientHelper = $this->createMock(ClientHelper::class); + $this->clientHelper->method('getAlmaClient')->willReturn($client); + + $this->ActionObjectUpdateAfter = new ActionObjectUpdateAfter($this->orderFactory, $this->clientHelper, $this->carrierFactory); + } + + /** + * @dataProvider badObjectProvider + * @return void + */ + public function testRunWithoutOrderCarrierObjectReturn($params) + { + $this->orderFactory->expects($this->never())->method('create'); + $this->assertNull($this->ActionObjectUpdateAfter->run($params)); + } + + public function testRunWithNonAlmaOrderReturn() + { + $this->orderFactory + ->expects($this->once()) + ->method('create') + ->willReturn(new \Order()); + + $this->assertNull($this->ActionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject())); + } + + public function testRunWithAlmaOrderWithoutPaymentsReturn() + { + $almaOrder = new \Order(); + $almaOrder->module = ConstantsHelper::ALMA_MODULE_NAME; + $this->orderFactory + ->expects($this->once()) + ->method('create') + ->willReturn($almaOrder); + + $this->assertNull($this->ActionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject())); + } + + public function testRunWithAlmaOrderWithPaymentsWithoutAlmaExternalIdReturn() + { + $almaOrder = $this->createMock(\Order::class); + $almaOrder->method('getOrderPayments')->willReturn([new \OrderPayment(), new \OrderPayment()]); + $almaOrder->module = ConstantsHelper::ALMA_MODULE_NAME; + $this->orderFactory + ->expects($this->once()) + ->method('create') + ->willReturn($almaOrder); + $this->orderEndpoint->expects($this->never())->method('addTracking'); + $this->assertNull($this->ActionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject())); + } + + public function testRunWithAlmaPaymentNotFound() + { + $almaOrder = $this->createMock(\Order::class); + $almaPayment = new \OrderPayment(); + $almaPayment->transaction_id = 'payment_123456'; + $almaOrder->method('getOrderPayments')->willReturn([new \OrderPayment(), $almaPayment]); + $almaOrder->module = ConstantsHelper::ALMA_MODULE_NAME; + $this->orderFactory + ->expects($this->once()) + ->method('create') + ->willReturn($almaOrder); + + $this->paymentEndpoint->method('fetch')->willThrowException(new AlmaException('Payment not found')); + + $this->orderEndpoint->expects($this->never())->method('addTracking'); + $this->assertNull($this->ActionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject())); + } + + public function testRunWithAlmaPaymentWithoutOrderCreateOrderAndSendShipping() + { + $prestashopOrder = $this->createMock(\Order::class); + $prestashopOrder->reference = 'AZERTY'; + $almaPayment = new \OrderPayment(); + $almaPayment->transaction_id = 'payment_123456'; + $prestashopOrder->method('getOrderPayments')->willReturn([new \OrderPayment(), $almaPayment]); + $prestashopOrder->module = ConstantsHelper::ALMA_MODULE_NAME; + $this->orderFactory + ->expects($this->once()) + ->method('create') + ->willReturn($prestashopOrder); + + $this->paymentEndpoint->method('fetch')->willReturn($this->almaPaymentWithoutOrders()); + + $almaOrder = $this->createMock(Order::class); + $almaOrder->method('getExternalId')->willReturn('order_123'); + $this->paymentEndpoint + ->expects($this->once()) + ->method('addOrder') + ->with('payment_123456', ['merchant_reference' => 'AZERTY']) + ->willReturn($almaOrder); + $this->orderEndpoint + ->expects($this->once()) + ->method('addTracking') + ->with('order_123'); + $this->assertNull($this->ActionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject())); + } + + public function testRunWithAlmaPaymentWithOrderWithBadMerchantReferenceCreateOrderAndSendShipping() + { + $prestashopOrder = $this->createMock(\Order::class); + $prestashopOrder->reference = 'AZERTY'; + $almaPayment = new \OrderPayment(); + $almaPayment->transaction_id = 'payment_123456'; + $prestashopOrder->method('getOrderPayments')->willReturn([new \OrderPayment(), $almaPayment]); + $prestashopOrder->module = ConstantsHelper::ALMA_MODULE_NAME; + $this->orderFactory + ->expects($this->once()) + ->method('create') + ->willReturn($prestashopOrder); + + $almaOrder = $this->createMock(Order::class); + $almaOrder->method('getExternalId')->willReturn('order_123'); + $almaOrder->method('getMerchantReference')->willReturn('YTREZA'); + $almaPayment = $this->almaPaymentWithoutOrders(); + $almaPayment->orders = [$almaOrder]; + $this->paymentEndpoint->method('fetch')->willReturn($almaPayment); + + $this->paymentEndpoint + ->expects($this->once()) + ->method('addOrder') + ->with('payment_123456', ['merchant_reference' => 'AZERTY']) + ->willReturn($almaOrder); + $this->orderEndpoint + ->expects($this->once()) + ->method('addTracking') + ->with('order_123'); + $this->assertNull($this->ActionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject())); + } + + public function testRunWithAlmaPaymentWithOrdersWithMerchantReferenceNoCreateOrderAndSendShipping() + { + $prestashopOrder = $this->createMock(\Order::class); + $prestashopOrder->reference = 'AZERTY'; + $almaPayment = new \OrderPayment(); + $almaPayment->transaction_id = 'payment_123456'; + $prestashopOrder->method('getOrderPayments')->willReturn([new \OrderPayment(), $almaPayment]); + $prestashopOrder->module = ConstantsHelper::ALMA_MODULE_NAME; + $this->orderFactory + ->expects($this->once()) + ->method('create') + ->willReturn($prestashopOrder); + + $almaOrder = $this->createMock(Order::class); + $almaOrder->method('getExternalId')->willReturn('order_123'); + $almaOrder->method('getMerchantReference')->willReturn('YTREZA'); + + $almaOrder2 = $this->createMock(Order::class); + $almaOrder2->method('getExternalId')->willReturn('order_321'); + $almaOrder2->method('getMerchantReference')->willReturn('AZERTY'); + + $almaPayment = $this->almaPaymentWithoutOrders(); + $almaPayment->orders = [$almaOrder,$almaOrder2]; + $this->paymentEndpoint->method('fetch')->willReturn($almaPayment); + + $this->paymentEndpoint + ->expects($this->never()) + ->method('addOrder'); + $this->orderEndpoint + ->expects($this->once()) + ->method('addTracking') + ->with('order_321', 'MyCarrierName', 'track_1232', 'myurl'); + $this->assertNull($this->ActionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject())); + } + + /** + * @return array + */ + public function badObjectProvider() + { + return [ + 'Null object' => [[]], + 'StdClass' => [['object' => new \stdClass()]], + 'Order' => [['object' => new \Order()]], + ]; + } + + private function paramsWithOrderCarrierObject() + { + $orderCarrier = $this->createMock(\OrderCarrier::class); + $orderCarrier->tracking_number = 'track_1232'; + return [ + 'object' => $orderCarrier, + ]; + } + + private function almaPaymentWithoutOrders() + { + $almaPayment = $this->createMock(Payment::class); + $almaPayment->orders = []; + return $almaPayment; + } +} \ No newline at end of file From 10a282ffd327896dda28f7b9caec8dbc1f039c58 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Wed, 31 Jul 2024 23:20:30 +0200 Subject: [PATCH 02/14] fix: fix factories --- alma/lib/Factories/CarrierFactory.php | 41 +++++++++++++++++++++++---- alma/lib/Factories/OrderFactory.php | 4 +-- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/alma/lib/Factories/CarrierFactory.php b/alma/lib/Factories/CarrierFactory.php index 59dad998..c9066ed3 100644 --- a/alma/lib/Factories/CarrierFactory.php +++ b/alma/lib/Factories/CarrierFactory.php @@ -1,15 +1,46 @@ + * @copyright 2018-2024 Alma SAS + * @license https://opensource.org/licenses/MIT The MIT License + */ + namespace Alma\PrestaShop\Factories; +if (!defined('_PS_VERSION_')) { + exit; +} + +/** + * Class CarrierFactory. + */ class CarrierFactory { - /** - * @param int $id + * @param int|null $id Carrier ID + * @param int|null $id_lang Language ID + * * @return \Carrier */ - public function create($id) + public function create($id = null, $id_lang = null) { - return new \Carrier($id); + return new \Carrier($id, $id_lang); } -} \ No newline at end of file +} diff --git a/alma/lib/Factories/OrderFactory.php b/alma/lib/Factories/OrderFactory.php index c89181af..1121f3ff 100644 --- a/alma/lib/Factories/OrderFactory.php +++ b/alma/lib/Factories/OrderFactory.php @@ -10,8 +10,8 @@ class OrderFactory * @throws \PrestaShopDatabaseException * @throws \PrestaShopException */ - public function create($id) + public function create($id = null, $id_lang = null) { - return new \Order($id); + return new \Order($id, $id_lang); } } \ No newline at end of file From 5e73e9d8d61414556533239e3dd57564a5422fc6 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Wed, 31 Jul 2024 23:26:55 +0200 Subject: [PATCH 03/14] fix: remove logs --- alma/controllers/hook/ActionObjectUpdateAfter.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/alma/controllers/hook/ActionObjectUpdateAfter.php b/alma/controllers/hook/ActionObjectUpdateAfter.php index 424eebb2..6f154801 100644 --- a/alma/controllers/hook/ActionObjectUpdateAfter.php +++ b/alma/controllers/hook/ActionObjectUpdateAfter.php @@ -38,30 +38,25 @@ public function __construct($orderFactory, $clientHelper, $carrierFactory) */ public function run($params) { - Logger::instance()->info('[Alma] - Start Run'); if ( !isset($params['object']) || !($params['object'] instanceof \OrderCarrierCore) ) { return; } - Logger::instance()->info('[Alma] - it s OrderCarrierCore object'); /** @var \OrderCarrier $orderCarrier */ $orderCarrier = $params['object']; $idOrder = $orderCarrier->id_order; - Logger::instance()->info('[Alma] - id order ' . $idOrder); /** @var \OrderCore $order */ try { $order = $this->orderFactory->create($idOrder); } catch (\PrestaShopException $e) { - Logger::instance()->info('[Alma] - PrestaShopException - Impossible to get Order with id :' . $idOrder); + Logger::instance()->error('[Alma] - PrestaShopException - Impossible to get Order with id :' . $idOrder); return; } if ($order->module != ConstantsHelper::ALMA_MODULE_NAME || empty($order->getOrderPayments())) { - Logger::instance()->info('[Alma] - To Remove - Order is not Alma or payments are empty'); - Logger::instance()->info('[Alma] - order module' . $order->module); return; } @@ -73,7 +68,7 @@ public function run($params) } } if (!isset($almaPaymentExternalId)) { - Logger::instance()->info('[Alma] - To Remove - No Alma Payment External Id'); + Logger::instance()->error('[Alma] - No Alma Payment External Id in order ' . $order->reference); return; } From 6a62b45b2a6bc5657d3794f9d2da14cd4af069ca Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Wed, 31 Jul 2024 23:42:23 +0200 Subject: [PATCH 04/14] fix: add check ps_version --- alma/controllers/hook/ActionObjectUpdateAfter.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/alma/controllers/hook/ActionObjectUpdateAfter.php b/alma/controllers/hook/ActionObjectUpdateAfter.php index 6f154801..a6f98216 100644 --- a/alma/controllers/hook/ActionObjectUpdateAfter.php +++ b/alma/controllers/hook/ActionObjectUpdateAfter.php @@ -10,6 +10,9 @@ use Alma\PrestaShop\Helpers\ConstantsHelper; use Alma\PrestaShop\Logger; +if (!defined('_PS_VERSION_')) { + exit; +} class ActionObjectUpdateAfter { /** From d99fc00073019430b7cdae775da3efac3b262667 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Fri, 2 Aug 2024 10:50:25 +0200 Subject: [PATCH 05/14] fix: test directory name --- .../hook => Controllers/Hook}/ActionObjectUpdateAfterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename alma/tests/Unit/{controllers/hook => Controllers/Hook}/ActionObjectUpdateAfterTest.php (99%) diff --git a/alma/tests/Unit/controllers/hook/ActionObjectUpdateAfterTest.php b/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php similarity index 99% rename from alma/tests/Unit/controllers/hook/ActionObjectUpdateAfterTest.php rename to alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php index 44d35760..25efc0ba 100644 --- a/alma/tests/Unit/controllers/hook/ActionObjectUpdateAfterTest.php +++ b/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php @@ -1,6 +1,6 @@ Date: Fri, 2 Aug 2024 11:32:50 +0200 Subject: [PATCH 06/14] fix: review [BEN] test + lint --- alma/alma.php | 2 +- alma/composer.json | 2 +- .../hook/ActionObjectUpdateAfter.php | 10 +- alma/lib/Factories/OrderFactory.php | 6 +- ...ntrollerSetVariablesHookControllerTest.php | 143 ------------------ ...ductPropertiesBeforeHookControllerTest.php | 135 ----------------- .../Hook/ActionObjectUpdateAfterTest.php | 28 ++-- 7 files changed, 28 insertions(+), 298 deletions(-) delete mode 100644 alma/tests/Unit/Controllers/Hook/ActionFrontControllerSetVariablesHookControllerTest.php delete mode 100644 alma/tests/Unit/Controllers/Hook/ActionGetProductPropertiesBeforeHookControllerTest.php diff --git a/alma/alma.php b/alma/alma.php index ad14bc6a..89a58225 100644 --- a/alma/alma.php +++ b/alma/alma.php @@ -313,7 +313,7 @@ public function registerHooks() private function updateCarriersWithAlma() { $id_module = $this->id; - $id_shop = (int)$this->context->shop->id; + $id_shop = (int) $this->context->shop->id; $id_lang = $this->context->language->id; $carriers = Carrier::getCarriers($id_lang, false, false, false, null, Carrier::ALL_CARRIERS); $values = null; diff --git a/alma/composer.json b/alma/composer.json index 2b2dc676..94bf843a 100644 --- a/alma/composer.json +++ b/alma/composer.json @@ -11,7 +11,7 @@ }, "require": { "php": "^5.6 || ~7.0 || ~7.1 || ~7.2 || ~7.3 || ~7.4 || ~8.0 || ~8.1", - "alma/alma-php-client": "^2.0.7", + "alma/alma-php-client": "^2.1.0", "ext-json": "*", "ext-openssl": "*", "prestashop/prestashop-accounts-installer": "^v1.0.4", diff --git a/alma/controllers/hook/ActionObjectUpdateAfter.php b/alma/controllers/hook/ActionObjectUpdateAfter.php index a6f98216..9f61002a 100644 --- a/alma/controllers/hook/ActionObjectUpdateAfter.php +++ b/alma/controllers/hook/ActionObjectUpdateAfter.php @@ -37,6 +37,7 @@ public function __construct($orderFactory, $clientHelper, $carrierFactory) /** * @param $params + * * @return void */ public function run($params) @@ -52,11 +53,12 @@ public function run($params) $orderCarrier = $params['object']; $idOrder = $orderCarrier->id_order; - /** @var \OrderCore $order */ + /* @var \OrderCore $order */ try { $order = $this->orderFactory->create($idOrder); } catch (\PrestaShopException $e) { Logger::instance()->error('[Alma] - PrestaShopException - Impossible to get Order with id :' . $idOrder); + return; } if ($order->module != ConstantsHelper::ALMA_MODULE_NAME || empty($order->getOrderPayments())) { @@ -72,6 +74,7 @@ public function run($params) } if (!isset($almaPaymentExternalId)) { Logger::instance()->error('[Alma] - No Alma Payment External Id in order ' . $order->reference); + return; } @@ -79,6 +82,7 @@ public function run($params) $almaClient = $this->clientHelper->getAlmaClient(); } catch (ClientException $e) { Logger::instance()->error('[Alma] - ClientException - ' . $e->getMessage()); + return; } @@ -93,7 +97,7 @@ public function run($params) } if (!isset($orderExternalId)) { $almaOrder = $almaClient->payments->addOrder($almaPaymentExternalId, [ - 'merchant_reference' => $order->reference + 'merchant_reference' => $order->reference, ] ); $orderExternalId = $almaOrder->getExternalId(); @@ -104,4 +108,4 @@ public function run($params) Logger::instance()->error('[Alma] - AlmaException ' . $e->getMessage()); } } -} \ No newline at end of file +} diff --git a/alma/lib/Factories/OrderFactory.php b/alma/lib/Factories/OrderFactory.php index 1121f3ff..6c164e26 100644 --- a/alma/lib/Factories/OrderFactory.php +++ b/alma/lib/Factories/OrderFactory.php @@ -1,12 +1,14 @@ - * @copyright 2018-2024 Alma SAS - * @license https://opensource.org/licenses/MIT The MIT License - */ - -namespace Alma\PrestaShop\Tests\Unit\Controllers\Hook; - -use Alma\PrestaShop\Controllers\Hook\ActionFrontControllerSetVariablesHookController; -use Alma\PrestaShop\Helpers\ConstantsHelper; -use Alma\PrestaShop\Repositories\ProductRepository; -use PHPUnit\Framework\TestCase; - -class ActionFrontControllerSetVariablesHookControllerTest extends TestCase -{ - /** - * @var ProductRepository - */ - protected $productRepository; - /** - * @var \Module - */ - protected $module; - - public function setUp() - { - $this->productRepository = $this->createMock(ProductRepository::class); - $this->module = $this->createMock(\Module::class); - $this->insuranceProductId = '22'; - } - - /** - * @return void - */ - public function tearDown() - { - $this->productRepository = null; - $this->module = null; - $this->insuranceProductId = null; - } - - /** - * @dataProvider paramsDataProvider - * - * @return void - */ - public function testRunWithWrongParams($params) - { - $controller = new ActionFrontControllerSetVariablesHookController($this->module); - $this->assertFalse($controller->run($params)); - } - - /** - * @return void - */ - public function testRunWithCorrectParamsAndNoInsuranceProduct() - { - $idProduct = '12'; - $this->productRepository->expects($this->once()) - ->method('getProductIdByReference') - ->with(ConstantsHelper::ALMA_INSURANCE_PRODUCT_REFERENCE) - ->willReturn($this->insuranceProductId); - $params = [ - 'templateVars' => [ - 'page' => [ - 'page_name' => 'product', - 'body_classes' => [ - "product-id-${idProduct}", - ], - ], - ], - ]; - $controller = new ActionFrontControllerSetVariablesHookController($this->module, $this->productRepository); - $this->assertFalse($controller->run($params)); - } - - /** - * @return void - */ - public function testRunWithCorrectParamsAndInsuranceProduct() - { - $idProduct = '22'; - $this->productRepository->expects($this->once()) - ->method('getProductIdByReference') - ->with(ConstantsHelper::ALMA_INSURANCE_PRODUCT_REFERENCE) - ->willReturn($this->insuranceProductId); - $params = [ - 'templateVars' => [ - 'page' => [ - 'page_name' => 'product', - 'body_classes' => [ - "product-id-${idProduct}" => true, - ], - ], - ], - ]; - $controller = new ActionFrontControllerSetVariablesHookController($this->module, $this->productRepository); - $this->assertTrue($controller->run($params)); - } - - /** - * @return array - */ - public function paramsDataProvider() - { - return [ - 'params without page key' => [ - [ - 'templateVars' => [ - ], - ], - ], - 'params without configuration key' => [ - [ - 'templateVars' => [ - 'page' => [ - 'page_name' => 'toto', - ], - ], - ], - ], - ]; - } -} diff --git a/alma/tests/Unit/Controllers/Hook/ActionGetProductPropertiesBeforeHookControllerTest.php b/alma/tests/Unit/Controllers/Hook/ActionGetProductPropertiesBeforeHookControllerTest.php deleted file mode 100644 index 75944c21..00000000 --- a/alma/tests/Unit/Controllers/Hook/ActionGetProductPropertiesBeforeHookControllerTest.php +++ /dev/null @@ -1,135 +0,0 @@ - - * @copyright 2018-2024 Alma SAS - * @license https://opensource.org/licenses/MIT The MIT License - */ - -namespace Alma\PrestaShop\Tests\Unit\Controllers\Hook; - -use Alma\PrestaShop\Controllers\Hook\ActionGetProductPropertiesBeforeHookController; -use Alma\PrestaShop\Helpers\ConstantsHelper; -use PHPUnit\Framework\TestCase; - -class ActionGetProductPropertiesBeforeHookControllerTest extends TestCase -{ - /** - * @var \Context - */ - protected $context; - /** - * @var \Module - */ - protected $module; - - /** - * @return void - */ - public function setUp() - { - $this->context = $this->createMock(\Context::class); - $this->context->smarty = $this->createMock(\Smarty::class); - $this->module = $this->createMock(\Module::class); - } - - /** - * @return void - */ - public function tearDown() - { - $this->context->smarty = null; - $this->module = null; - } - - /** - * @dataProvider paramsDataProvider - * - * @return void - */ - public function testRunWithWrongParams($params) - { - $this->context->smarty->expects($this->never()) - ->method('assign'); - $controller = new ActionGetProductPropertiesBeforeHookController($this->module); - $controller->run($params); - } - - /** - * @return void - */ - public function testRunWithNotInsuranceProduct() - { - $this->context->smarty->expects($this->never()) - ->method('assign'); - $params = [ - 'product' => [ - 'reference' => 'reference-product', - ], - 'context' => $this->context, - ]; - - $controller = new ActionGetProductPropertiesBeforeHookController($this->module); - $controller->run($params); - } - - /** - * @return void - */ - public function testRunWithCorrectParams() - { - $this->context->smarty->expects($this->once()) - ->method('assign') - ->with([ - 'configuration' => [ - 'is_catalog' => true, - 'display_taxes_label' => null, - ], - ]); - - $params = [ - 'product' => [ - 'reference' => ConstantsHelper::ALMA_INSURANCE_PRODUCT_REFERENCE, - ], - 'context' => $this->context, - ]; - - $controller = new ActionGetProductPropertiesBeforeHookController($this->module); - $controller->run($params); - } - - /** - * @return array - */ - public function paramsDataProvider() - { - return [ - 'params without product' => [ - [ - 'context' => $this->context, - ], - ], - 'params with product and no context' => [ - [ - 'product' => ['title' => 'product', 'reference' => '123456'], - ], - ], - ]; - } -} diff --git a/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php b/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php index 25efc0ba..2bce5f89 100644 --- a/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php +++ b/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php @@ -17,7 +17,7 @@ class ActionObjectUpdateAfterTest extends TestCase { - protected $ActionObjectUpdateAfter; + protected $actionObjectUpdateAfter; protected $orderFactory; protected $carrierFactory; @@ -25,7 +25,6 @@ class ActionObjectUpdateAfterTest extends TestCase protected $paymentEndpoint; protected $orderEndpoint; - public function setUp() { $this->orderFactory = $this->createMock(OrderFactory::class); @@ -43,17 +42,18 @@ public function setUp() $this->clientHelper = $this->createMock(ClientHelper::class); $this->clientHelper->method('getAlmaClient')->willReturn($client); - $this->ActionObjectUpdateAfter = new ActionObjectUpdateAfter($this->orderFactory, $this->clientHelper, $this->carrierFactory); + $this->actionObjectUpdateAfter = new ActionObjectUpdateAfter($this->orderFactory, $this->clientHelper, $this->carrierFactory); } /** * @dataProvider badObjectProvider + * * @return void */ public function testRunWithoutOrderCarrierObjectReturn($params) { $this->orderFactory->expects($this->never())->method('create'); - $this->assertNull($this->ActionObjectUpdateAfter->run($params)); + $this->actionObjectUpdateAfter->run($params); } public function testRunWithNonAlmaOrderReturn() @@ -63,7 +63,7 @@ public function testRunWithNonAlmaOrderReturn() ->method('create') ->willReturn(new \Order()); - $this->assertNull($this->ActionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject())); + $this->actionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject()); } public function testRunWithAlmaOrderWithoutPaymentsReturn() @@ -75,7 +75,7 @@ public function testRunWithAlmaOrderWithoutPaymentsReturn() ->method('create') ->willReturn($almaOrder); - $this->assertNull($this->ActionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject())); + $this->actionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject()); } public function testRunWithAlmaOrderWithPaymentsWithoutAlmaExternalIdReturn() @@ -88,7 +88,7 @@ public function testRunWithAlmaOrderWithPaymentsWithoutAlmaExternalIdReturn() ->method('create') ->willReturn($almaOrder); $this->orderEndpoint->expects($this->never())->method('addTracking'); - $this->assertNull($this->ActionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject())); + $this->actionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject()); } public function testRunWithAlmaPaymentNotFound() @@ -106,7 +106,7 @@ public function testRunWithAlmaPaymentNotFound() $this->paymentEndpoint->method('fetch')->willThrowException(new AlmaException('Payment not found')); $this->orderEndpoint->expects($this->never())->method('addTracking'); - $this->assertNull($this->ActionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject())); + $this->actionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject()); } public function testRunWithAlmaPaymentWithoutOrderCreateOrderAndSendShipping() @@ -135,7 +135,7 @@ public function testRunWithAlmaPaymentWithoutOrderCreateOrderAndSendShipping() ->expects($this->once()) ->method('addTracking') ->with('order_123'); - $this->assertNull($this->ActionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject())); + $this->actionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject()); } public function testRunWithAlmaPaymentWithOrderWithBadMerchantReferenceCreateOrderAndSendShipping() @@ -167,7 +167,7 @@ public function testRunWithAlmaPaymentWithOrderWithBadMerchantReferenceCreateOrd ->expects($this->once()) ->method('addTracking') ->with('order_123'); - $this->assertNull($this->ActionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject())); + $this->actionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject()); } public function testRunWithAlmaPaymentWithOrdersWithMerchantReferenceNoCreateOrderAndSendShipping() @@ -192,7 +192,7 @@ public function testRunWithAlmaPaymentWithOrdersWithMerchantReferenceNoCreateOrd $almaOrder2->method('getMerchantReference')->willReturn('AZERTY'); $almaPayment = $this->almaPaymentWithoutOrders(); - $almaPayment->orders = [$almaOrder,$almaOrder2]; + $almaPayment->orders = [$almaOrder, $almaOrder2]; $this->paymentEndpoint->method('fetch')->willReturn($almaPayment); $this->paymentEndpoint @@ -202,7 +202,7 @@ public function testRunWithAlmaPaymentWithOrdersWithMerchantReferenceNoCreateOrd ->expects($this->once()) ->method('addTracking') ->with('order_321', 'MyCarrierName', 'track_1232', 'myurl'); - $this->assertNull($this->ActionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject())); + $this->actionObjectUpdateAfter->run($this->paramsWithOrderCarrierObject()); } /** @@ -221,6 +221,7 @@ private function paramsWithOrderCarrierObject() { $orderCarrier = $this->createMock(\OrderCarrier::class); $orderCarrier->tracking_number = 'track_1232'; + return [ 'object' => $orderCarrier, ]; @@ -230,6 +231,7 @@ private function almaPaymentWithoutOrders() { $almaPayment = $this->createMock(Payment::class); $almaPayment->orders = []; + return $almaPayment; } -} \ No newline at end of file +} From 8446fc0b972088d87d74a3c905d7d2a1000d21c6 Mon Sep 17 00:00:00 2001 From: Benjamin Freoua Date: Fri, 2 Aug 2024 11:44:59 +0200 Subject: [PATCH 07/14] fix: recover files removed for test coverage --- ...ntrollerSetVariablesHookControllerTest.php | 143 ++++++++++++++++++ ...ductPropertiesBeforeHookControllerTest.php | 135 +++++++++++++++++ 2 files changed, 278 insertions(+) create mode 100644 alma/tests/Unit/Controllers/Hook/ActionFrontControllerSetVariablesHookControllerTest.php create mode 100644 alma/tests/Unit/Controllers/Hook/ActionGetProductPropertiesBeforeHookControllerTest.php diff --git a/alma/tests/Unit/Controllers/Hook/ActionFrontControllerSetVariablesHookControllerTest.php b/alma/tests/Unit/Controllers/Hook/ActionFrontControllerSetVariablesHookControllerTest.php new file mode 100644 index 00000000..fa313c9c --- /dev/null +++ b/alma/tests/Unit/Controllers/Hook/ActionFrontControllerSetVariablesHookControllerTest.php @@ -0,0 +1,143 @@ + + * @copyright 2018-2024 Alma SAS + * @license https://opensource.org/licenses/MIT The MIT License + */ + +namespace Alma\PrestaShop\Tests\Unit\Controllers\Hook; + +use Alma\PrestaShop\Controllers\Hook\ActionFrontControllerSetVariablesHookController; +use Alma\PrestaShop\Helpers\ConstantsHelper; +use Alma\PrestaShop\Repositories\ProductRepository; +use PHPUnit\Framework\TestCase; + +class ActionFrontControllerSetVariablesHookControllerTest extends TestCase +{ + /** + * @var ProductRepository + */ + protected $productRepository; + /** + * @var \Module + */ + protected $module; + + public function setUp() + { + $this->productRepository = $this->createMock(ProductRepository::class); + $this->module = $this->createMock(\Module::class); + $this->insuranceProductId = '22'; + } + + /** + * @return void + */ + public function tearDown() + { + $this->productRepository = null; + $this->module = null; + $this->insuranceProductId = null; + } + + /** + * @dataProvider paramsDataProvider + * + * @return void + */ + public function testRunWithWrongParams($params) + { + $controller = new ActionFrontControllerSetVariablesHookController($this->module); + $this->assertFalse($controller->run($params)); + } + + /** + * @return void + */ + public function testRunWithCorrectParamsAndNoInsuranceProduct() + { + $idProduct = '12'; + $this->productRepository->expects($this->once()) + ->method('getProductIdByReference') + ->with(ConstantsHelper::ALMA_INSURANCE_PRODUCT_REFERENCE) + ->willReturn($this->insuranceProductId); + $params = [ + 'templateVars' => [ + 'page' => [ + 'page_name' => 'product', + 'body_classes' => [ + "product-id-${idProduct}", + ], + ], + ], + ]; + $controller = new ActionFrontControllerSetVariablesHookController($this->module, $this->productRepository); + $this->assertFalse($controller->run($params)); + } + + /** + * @return void + */ + public function testRunWithCorrectParamsAndInsuranceProduct() + { + $idProduct = '22'; + $this->productRepository->expects($this->once()) + ->method('getProductIdByReference') + ->with(ConstantsHelper::ALMA_INSURANCE_PRODUCT_REFERENCE) + ->willReturn($this->insuranceProductId); + $params = [ + 'templateVars' => [ + 'page' => [ + 'page_name' => 'product', + 'body_classes' => [ + "product-id-${idProduct}" => true, + ], + ], + ], + ]; + $controller = new ActionFrontControllerSetVariablesHookController($this->module, $this->productRepository); + $this->assertTrue($controller->run($params)); + } + + /** + * @return array + */ + public function paramsDataProvider() + { + return [ + 'params without page key' => [ + [ + 'templateVars' => [ + ], + ], + ], + 'params without configuration key' => [ + [ + 'templateVars' => [ + 'page' => [ + 'page_name' => 'toto', + ], + ], + ], + ], + ]; + } +} diff --git a/alma/tests/Unit/Controllers/Hook/ActionGetProductPropertiesBeforeHookControllerTest.php b/alma/tests/Unit/Controllers/Hook/ActionGetProductPropertiesBeforeHookControllerTest.php new file mode 100644 index 00000000..75944c21 --- /dev/null +++ b/alma/tests/Unit/Controllers/Hook/ActionGetProductPropertiesBeforeHookControllerTest.php @@ -0,0 +1,135 @@ + + * @copyright 2018-2024 Alma SAS + * @license https://opensource.org/licenses/MIT The MIT License + */ + +namespace Alma\PrestaShop\Tests\Unit\Controllers\Hook; + +use Alma\PrestaShop\Controllers\Hook\ActionGetProductPropertiesBeforeHookController; +use Alma\PrestaShop\Helpers\ConstantsHelper; +use PHPUnit\Framework\TestCase; + +class ActionGetProductPropertiesBeforeHookControllerTest extends TestCase +{ + /** + * @var \Context + */ + protected $context; + /** + * @var \Module + */ + protected $module; + + /** + * @return void + */ + public function setUp() + { + $this->context = $this->createMock(\Context::class); + $this->context->smarty = $this->createMock(\Smarty::class); + $this->module = $this->createMock(\Module::class); + } + + /** + * @return void + */ + public function tearDown() + { + $this->context->smarty = null; + $this->module = null; + } + + /** + * @dataProvider paramsDataProvider + * + * @return void + */ + public function testRunWithWrongParams($params) + { + $this->context->smarty->expects($this->never()) + ->method('assign'); + $controller = new ActionGetProductPropertiesBeforeHookController($this->module); + $controller->run($params); + } + + /** + * @return void + */ + public function testRunWithNotInsuranceProduct() + { + $this->context->smarty->expects($this->never()) + ->method('assign'); + $params = [ + 'product' => [ + 'reference' => 'reference-product', + ], + 'context' => $this->context, + ]; + + $controller = new ActionGetProductPropertiesBeforeHookController($this->module); + $controller->run($params); + } + + /** + * @return void + */ + public function testRunWithCorrectParams() + { + $this->context->smarty->expects($this->once()) + ->method('assign') + ->with([ + 'configuration' => [ + 'is_catalog' => true, + 'display_taxes_label' => null, + ], + ]); + + $params = [ + 'product' => [ + 'reference' => ConstantsHelper::ALMA_INSURANCE_PRODUCT_REFERENCE, + ], + 'context' => $this->context, + ]; + + $controller = new ActionGetProductPropertiesBeforeHookController($this->module); + $controller->run($params); + } + + /** + * @return array + */ + public function paramsDataProvider() + { + return [ + 'params without product' => [ + [ + 'context' => $this->context, + ], + ], + 'params with product and no context' => [ + [ + 'product' => ['title' => 'product', 'reference' => '123456'], + ], + ], + ]; + } +} From 6a6387faf8e4ace60d486923871c8c17e8899ebe Mon Sep 17 00:00:00 2001 From: "alma-renovate-bot[bot]" <163289924+alma-renovate-bot[bot]@users.noreply.github.com> Date: Mon, 5 Aug 2024 10:12:06 +0000 Subject: [PATCH 08/14] chore(deps): update pre-commit hook returntocorp/semgrep to v1.83.0 --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f76ad9ce..8fd0c21d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -39,7 +39,7 @@ repos: stages: [commit] - repo: https://github.com/returntocorp/semgrep - rev: v1.81.0 + rev: v1.83.0 hooks: - id: semgrep args: From ef095d0575324875c83d47f20862fd64ab435b7d Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Tue, 6 Aug 2024 11:51:58 +0200 Subject: [PATCH 09/14] fix: no send data if no tracking number --- alma/controllers/hook/ActionObjectUpdateAfter.php | 8 +++++--- .../Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/alma/controllers/hook/ActionObjectUpdateAfter.php b/alma/controllers/hook/ActionObjectUpdateAfter.php index 9f61002a..15c892b7 100644 --- a/alma/controllers/hook/ActionObjectUpdateAfter.php +++ b/alma/controllers/hook/ActionObjectUpdateAfter.php @@ -13,6 +13,7 @@ if (!defined('_PS_VERSION_')) { exit; } + class ActionObjectUpdateAfter { /** @@ -52,7 +53,9 @@ public function run($params) /** @var \OrderCarrier $orderCarrier */ $orderCarrier = $params['object']; $idOrder = $orderCarrier->id_order; - + if ($orderCarrier->tracking_number === '') { + return; + } /* @var \OrderCore $order */ try { $order = $this->orderFactory->create($idOrder); @@ -77,7 +80,6 @@ public function run($params) return; } - try { $almaClient = $this->clientHelper->getAlmaClient(); } catch (ClientException $e) { @@ -102,7 +104,7 @@ public function run($params) ); $orderExternalId = $almaOrder->getExternalId(); } - $carrier = $this->carrierFactory->create((int) $orderCarrier->id_carrier); + $carrier = $this->carrierFactory->create((int)$orderCarrier->id_carrier); $almaClient->orders->addTracking($orderExternalId, $carrier->name, $orderCarrier->tracking_number, $carrier->url); } catch (AlmaException $e) { Logger::instance()->error('[Alma] - AlmaException ' . $e->getMessage()); diff --git a/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php b/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php index 2bce5f89..708b3373 100644 --- a/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php +++ b/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php @@ -45,6 +45,9 @@ public function setUp() $this->actionObjectUpdateAfter = new ActionObjectUpdateAfter($this->orderFactory, $this->clientHelper, $this->carrierFactory); } + public function testInstanceOfObject(){ + $this->assertInstanceOf(ActionObjectUpdateAfter::class, $this->actionObjectUpdateAfter); + } /** * @dataProvider badObjectProvider * @@ -210,10 +213,13 @@ public function testRunWithAlmaPaymentWithOrdersWithMerchantReferenceNoCreateOrd */ public function badObjectProvider() { + $orderCarrier = $this->createMock(\OrderCarrier::class); + $orderCarrier->tracking_number = ''; return [ 'Null object' => [[]], 'StdClass' => [['object' => new \stdClass()]], 'Order' => [['object' => new \Order()]], + 'track without tracking number' => [['object' => $orderCarrier]], ]; } From 445f2fb595a8fdd267b015eefb441b08ab5dbc51 Mon Sep 17 00:00:00 2001 From: Benjamin Freoua Date: Tue, 6 Aug 2024 12:00:19 +0200 Subject: [PATCH 10/14] fix: lint --- alma/controllers/hook/ActionObjectUpdateAfter.php | 2 +- .../Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/alma/controllers/hook/ActionObjectUpdateAfter.php b/alma/controllers/hook/ActionObjectUpdateAfter.php index 15c892b7..aa03039e 100644 --- a/alma/controllers/hook/ActionObjectUpdateAfter.php +++ b/alma/controllers/hook/ActionObjectUpdateAfter.php @@ -104,7 +104,7 @@ public function run($params) ); $orderExternalId = $almaOrder->getExternalId(); } - $carrier = $this->carrierFactory->create((int)$orderCarrier->id_carrier); + $carrier = $this->carrierFactory->create((int) $orderCarrier->id_carrier); $almaClient->orders->addTracking($orderExternalId, $carrier->name, $orderCarrier->tracking_number, $carrier->url); } catch (AlmaException $e) { Logger::instance()->error('[Alma] - AlmaException ' . $e->getMessage()); diff --git a/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php b/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php index 708b3373..fac85df5 100644 --- a/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php +++ b/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php @@ -45,9 +45,11 @@ public function setUp() $this->actionObjectUpdateAfter = new ActionObjectUpdateAfter($this->orderFactory, $this->clientHelper, $this->carrierFactory); } - public function testInstanceOfObject(){ + public function testInstanceOfObject() + { $this->assertInstanceOf(ActionObjectUpdateAfter::class, $this->actionObjectUpdateAfter); } + /** * @dataProvider badObjectProvider * @@ -215,6 +217,7 @@ public function badObjectProvider() { $orderCarrier = $this->createMock(\OrderCarrier::class); $orderCarrier->tracking_number = ''; + return [ 'Null object' => [[]], 'StdClass' => [['object' => new \stdClass()]], From 02f8194b979d77524cb519979ddfdee37b068f83 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Tue, 6 Aug 2024 23:43:52 +0200 Subject: [PATCH 11/14] refactor: refactor run method --- .../hook/ActionObjectUpdateAfter.php | 122 ++++++++++++++---- .../AlmaActionObjectUpdateException.php | 11 ++ .../Hook/ActionObjectUpdateAfterTest.php | 2 +- 3 files changed, 108 insertions(+), 27 deletions(-) create mode 100644 alma/exceptions/AlmaActionObjectUpdateException.php diff --git a/alma/controllers/hook/ActionObjectUpdateAfter.php b/alma/controllers/hook/ActionObjectUpdateAfter.php index 15c892b7..e90a9201 100644 --- a/alma/controllers/hook/ActionObjectUpdateAfter.php +++ b/alma/controllers/hook/ActionObjectUpdateAfter.php @@ -2,7 +2,9 @@ namespace Alma\PrestaShop\Controllers\Hook; +use Alma\API\Client; use Alma\API\Exceptions\AlmaException; +use Alma\PrestaShop\Exceptions\AlmaActionObjectUpdateException; use Alma\PrestaShop\Exceptions\ClientException; use Alma\PrestaShop\Factories\CarrierFactory; use Alma\PrestaShop\Factories\OrderFactory; @@ -20,10 +22,12 @@ class ActionObjectUpdateAfter * @var OrderFactory */ private $orderFactory; + /** * @var ClientHelper */ private $clientHelper; + /** * @var CarrierFactory */ @@ -37,57 +41,121 @@ public function __construct($orderFactory, $clientHelper, $carrierFactory) } /** + * Send tracking information to Alma only for Alma Orders + * * @param $params * * @return void */ public function run($params) + { + try { + $orderCarrier = $this->checkParamsContainValidOrderCarrierObject($params); + $carrier = $this->carrierFactory->create($orderCarrier->id_carrier); + $order = $this->getAlmaOrderFromOrderCarrierOrderId($orderCarrier->id_order); + $almaPaymentExternalId = $this->getAlmaPaymentExternalId($order); + $almaClient = $this->getAlmaClient(); + $orderExternalId = $this->getOrCreateAlmaOrderExternalId($almaClient, $order, $almaPaymentExternalId); + $almaClient->orders->addTracking($orderExternalId, $carrier->name, $orderCarrier->tracking_number, $carrier->url); + } catch (AlmaActionObjectUpdateException $e) { + return; + } catch (AlmaException $e) { + Logger::instance()->error('[Alma] - Add tracking error: ' . $e->getMessage()); + } + } + + /** + * Check if params Object is set and is an OrderCarrier + * + * @param $params + * @return \OrderCarrierCore $orderCarrier + * @throws AlmaActionObjectUpdateException + */ + private function checkParamsContainValidOrderCarrierObject($params) { if ( !isset($params['object']) || - !($params['object'] instanceof \OrderCarrierCore) + !($params['object'] instanceof \OrderCarrierCore) || + $params['object']->tracking_number === '' ) { - return; + throw new AlmaActionObjectUpdateException('Object is not an OrderCarrier'); } + return $params['object']; + } - /** @var \OrderCarrier $orderCarrier */ - $orderCarrier = $params['object']; - $idOrder = $orderCarrier->id_order; - if ($orderCarrier->tracking_number === '') { - return; - } - /* @var \OrderCore $order */ + /** + * Get Alma Order from OrderCarrier OrderId + * Throw Exception for no Alma Order + * + * @param $orderId + * @return \Order + * @throws AlmaActionObjectUpdateException + */ + private function getAlmaOrderFromOrderCarrierOrderId($orderId) + { try { - $order = $this->orderFactory->create($idOrder); + $order = $this->orderFactory->create($orderId); } catch (\PrestaShopException $e) { - Logger::instance()->error('[Alma] - PrestaShopException - Impossible to get Order with id :' . $idOrder); - - return; + Logger::instance()->error('[Alma] - PrestaShopException - Impossible to get Order with id :' . $orderId); + throw new AlmaActionObjectUpdateException('Impossible to get Order'); } - if ($order->module != ConstantsHelper::ALMA_MODULE_NAME || empty($order->getOrderPayments())) { - return; + if ($order->module != ConstantsHelper::ALMA_MODULE_NAME) { + throw new AlmaActionObjectUpdateException('Order is not an Alma Order'); } + return $order; + } + + /** + * Get Alma Payment External Id from Order + * Throw Exception if no Payment or no Alma Payment External Id in Order + * + * @param \Order $order + * @return string + * @throws AlmaActionObjectUpdateException + */ + private function getAlmaPaymentExternalId($order) + { + if (empty($order->getOrderPayments())) { + throw new AlmaActionObjectUpdateException('Order is not an Alma Order'); + } foreach ($order->getOrderPayments() as $orderPayment) { /** @var \OrderPayment $orderPayment */ if (isset($orderPayment->transaction_id)) { - $almaPaymentExternalId = $orderPayment->transaction_id; - break; + return $orderPayment->transaction_id; } } - if (!isset($almaPaymentExternalId)) { - Logger::instance()->error('[Alma] - No Alma Payment External Id in order ' . $order->reference); + Logger::instance()->error('[Alma] - No Alma Payment External Id in order ' . $order->reference); + throw new AlmaActionObjectUpdateException('No Alma Payment External Id'); + } - return; - } + /** + * Get Alma Client or throw Exception + * + * @return Client + * @throws AlmaActionObjectUpdateException + */ + private function getAlmaClient() + { try { - $almaClient = $this->clientHelper->getAlmaClient(); + return $this->clientHelper->getAlmaClient(); } catch (ClientException $e) { Logger::instance()->error('[Alma] - ClientException - ' . $e->getMessage()); - - return; + throw new AlmaActionObjectUpdateException('Impossible to get Alma Client'); } + } + /** + * Get or Create Alma Order External Id + * + * @param $almaClient + * @param $order + * @param $almaPaymentExternalId + * @return mixed|null + * @throws AlmaActionObjectUpdateException + */ + private function getOrCreateAlmaOrderExternalId($almaClient, $order, $almaPaymentExternalId) + { try { $almaPayment = $almaClient->payments->fetch($almaPaymentExternalId); @@ -104,10 +172,12 @@ public function run($params) ); $orderExternalId = $almaOrder->getExternalId(); } - $carrier = $this->carrierFactory->create((int)$orderCarrier->id_carrier); - $almaClient->orders->addTracking($orderExternalId, $carrier->name, $orderCarrier->tracking_number, $carrier->url); + return $orderExternalId; } catch (AlmaException $e) { Logger::instance()->error('[Alma] - AlmaException ' . $e->getMessage()); + throw new AlmaActionObjectUpdateException('Impossible to get or create Alma Order'); } } + + } diff --git a/alma/exceptions/AlmaActionObjectUpdateException.php b/alma/exceptions/AlmaActionObjectUpdateException.php new file mode 100644 index 00000000..cba8fdef --- /dev/null +++ b/alma/exceptions/AlmaActionObjectUpdateException.php @@ -0,0 +1,11 @@ +orderFactory = $this->createMock(OrderFactory::class); $this->carrierFactory = $this->createMock(CarrierFactory::class); From 1054394204dcee84437ab078b56d8e7128d4ff1c Mon Sep 17 00:00:00 2001 From: Benjamin Freoua Date: Wed, 7 Aug 2024 10:34:08 +0200 Subject: [PATCH 12/14] fix: lint --- alma/controllers/hook/ActionObjectUpdateAfter.php | 15 ++++++++++++--- .../Hook/ActionObjectUpdateAfterTest.php | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/alma/controllers/hook/ActionObjectUpdateAfter.php b/alma/controllers/hook/ActionObjectUpdateAfter.php index e90a9201..910aee62 100644 --- a/alma/controllers/hook/ActionObjectUpdateAfter.php +++ b/alma/controllers/hook/ActionObjectUpdateAfter.php @@ -68,7 +68,9 @@ public function run($params) * Check if params Object is set and is an OrderCarrier * * @param $params + * * @return \OrderCarrierCore $orderCarrier + * * @throws AlmaActionObjectUpdateException */ private function checkParamsContainValidOrderCarrierObject($params) @@ -80,6 +82,7 @@ private function checkParamsContainValidOrderCarrierObject($params) ) { throw new AlmaActionObjectUpdateException('Object is not an OrderCarrier'); } + return $params['object']; } @@ -88,7 +91,9 @@ private function checkParamsContainValidOrderCarrierObject($params) * Throw Exception for no Alma Order * * @param $orderId + * * @return \Order + * * @throws AlmaActionObjectUpdateException */ private function getAlmaOrderFromOrderCarrierOrderId($orderId) @@ -102,6 +107,7 @@ private function getAlmaOrderFromOrderCarrierOrderId($orderId) if ($order->module != ConstantsHelper::ALMA_MODULE_NAME) { throw new AlmaActionObjectUpdateException('Order is not an Alma Order'); } + return $order; } @@ -110,12 +116,13 @@ private function getAlmaOrderFromOrderCarrierOrderId($orderId) * Throw Exception if no Payment or no Alma Payment External Id in Order * * @param \Order $order + * * @return string + * * @throws AlmaActionObjectUpdateException */ private function getAlmaPaymentExternalId($order) { - if (empty($order->getOrderPayments())) { throw new AlmaActionObjectUpdateException('Order is not an Alma Order'); } @@ -133,6 +140,7 @@ private function getAlmaPaymentExternalId($order) * Get Alma Client or throw Exception * * @return Client + * * @throws AlmaActionObjectUpdateException */ private function getAlmaClient() @@ -151,7 +159,9 @@ private function getAlmaClient() * @param $almaClient * @param $order * @param $almaPaymentExternalId + * * @return mixed|null + * * @throws AlmaActionObjectUpdateException */ private function getOrCreateAlmaOrderExternalId($almaClient, $order, $almaPaymentExternalId) @@ -172,12 +182,11 @@ private function getOrCreateAlmaOrderExternalId($almaClient, $order, $almaPaymen ); $orderExternalId = $almaOrder->getExternalId(); } + return $orderExternalId; } catch (AlmaException $e) { Logger::instance()->error('[Alma] - AlmaException ' . $e->getMessage()); throw new AlmaActionObjectUpdateException('Impossible to get or create Alma Order'); } } - - } diff --git a/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php b/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php index 5e321130..fac85df5 100644 --- a/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php +++ b/alma/tests/Unit/Controllers/Hook/ActionObjectUpdateAfterTest.php @@ -25,7 +25,7 @@ class ActionObjectUpdateAfterTest extends TestCase protected $paymentEndpoint; protected $orderEndpoint; - public function setUp(): void + public function setUp() { $this->orderFactory = $this->createMock(OrderFactory::class); $this->carrierFactory = $this->createMock(CarrierFactory::class); From 3ffedb13a3a0a37ba4efcdb3439edc80e3db01b3 Mon Sep 17 00:00:00 2001 From: Benjamin-Freoua-Alma <89775252+Benjamin-Freoua-Alma@users.noreply.github.com> Date: Mon, 12 Aug 2024 10:05:51 +0000 Subject: [PATCH 13/14] chore: update version --- CHANGELOG.md | 12 ++++++++++++ alma/alma.php | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b4527097..c7ed1664 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## v4.3.0 - 2024-08-12 + +### Changes + +### 🚀 New Features + +- feat: Shipment info analysis (#545) + +#### Contributors + +@Benjamin-Freoua-Alma, @Francois-Gomis, @alma-renovate-bot and @github-actions + ## v4.2.0 - 2024-08-06 ### Changes diff --git a/alma/alma.php b/alma/alma.php index 498514a7..34ccaa4b 100644 --- a/alma/alma.php +++ b/alma/alma.php @@ -30,7 +30,7 @@ class Alma extends PaymentModule { - const VERSION = '4.2.0'; + const VERSION = '4.3.0'; public $_path; public $local_path; @@ -80,7 +80,7 @@ public function __construct() { $this->name = 'alma'; $this->tab = 'payments_gateways'; - $this->version = '4.2.0'; + $this->version = '4.3.0'; $this->author = 'Alma'; $this->need_instance = false; $this->bootstrap = true; From 19a172cb62fbba6857444807d68e53bba71eb94e Mon Sep 17 00:00:00 2001 From: Benjamin Freoua Date: Mon, 12 Aug 2024 12:10:05 +0200 Subject: [PATCH 14/14] chore: update translation --- alma/translations/es.php | 2 +- alma/translations/nl.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/alma/translations/es.php b/alma/translations/es.php index 81e68fb3..e278355b 100644 --- a/alma/translations/es.php +++ b/alma/translations/es.php @@ -191,7 +191,7 @@ $_MODULE['<{alma}prestashop>getcontenthookcontroller_5f265dfcb9ab68a1258ec25393ffb886'] = 'Gestor de módulos'; $_MODULE['<{alma}prestashop>getcontenthookcontroller_c6e24f81cb99e08c3d56b191c00aea28'] = 'Gestor de módulos - Lista'; $_MODULE['<{alma}prestashop>frontheaderhookcontroller_0bbf1fb0708055cf522541b4151894c4'] = 'Para gestionar tus compras con Assurance, ve a la página de pago.'; -$_MODULE['<{alma}prestashop>frontheaderhookcontroller_8f1e80580ceb2bb7a54a7288ea169b30'] = 'El seguro Alma sólo puede añadirse a su cesta si está asociado a un producto susceptible de seguro. Se ofrecerá en la página del producto en cuestión.'; +$_MODULE['<{alma}prestashop>frontheaderhookcontroller_8f1e80580ceb2bb7a54a7288ea169b30'] = 'El seguro Alma sólo puede añadirse a tu cesta si está asociado a un producto susceptible de seguro. Se ofrecerá en la página del producto en cuestión.'; $_MODULE['<{alma}prestashop>displaypaymenthookcontroller_afd8ac1c65e1519d6a890e5f98558a52'] = '%1$s entonces %2$d x %3$s'; $_MODULE['<{alma}prestashop>displaypaymenthookcontroller_bc69b42c4bd339154f9025f3299ad146'] = '%1$s hoy después %2$d x %3$s'; $_MODULE['<{alma}prestashop>displaypaymenthookcontroller_9088921432b295dfe6f02863b2dc0ff8'] = '0 € hoy después %1$s en %2$s'; diff --git a/alma/translations/nl.php b/alma/translations/nl.php index 4af67c5b..083f537a 100644 --- a/alma/translations/nl.php +++ b/alma/translations/nl.php @@ -191,7 +191,7 @@ $_MODULE['<{alma}prestashop>getcontenthookcontroller_5f265dfcb9ab68a1258ec25393ffb886'] = 'Module Manager'; $_MODULE['<{alma}prestashop>getcontenthookcontroller_c6e24f81cb99e08c3d56b191c00aea28'] = 'Module Manager - Lijst'; $_MODULE['<{alma}prestashop>frontheaderhookcontroller_0bbf1fb0708055cf522541b4151894c4'] = 'Ga naar de afrekenpagina om je aankopen met Assurance te beheren.'; -$_MODULE['<{alma}prestashop>frontheaderhookcontroller_8f1e80580ceb2bb7a54a7288ea169b30'] = 'Een Alma-verzekering kan alleen aan uw winkelwagen worden toegevoegd als deze gekoppeld is aan een product dat in aanmerking komt voor een verzekering. De verzekering wordt aangeboden op de betreffende productpagina.'; +$_MODULE['<{alma}prestashop>frontheaderhookcontroller_8f1e80580ceb2bb7a54a7288ea169b30'] = 'Een Alma-verzekering kan alleen aan de winkelwagen worden toegevoegd als deze gekoppeld is aan een product dat in aanmerking komt voor een verzekering. De verzekering wordt aangeboden op de betreffende productpagina.'; $_MODULE['<{alma}prestashop>displaypaymenthookcontroller_afd8ac1c65e1519d6a890e5f98558a52'] = '%1$s dan %2$d x %3$s'; $_MODULE['<{alma}prestashop>displaypaymenthookcontroller_bc69b42c4bd339154f9025f3299ad146'] = '%1$s vandaag dan %2$d x %3$s'; $_MODULE['<{alma}prestashop>displaypaymenthookcontroller_9088921432b295dfe6f02863b2dc0ff8'] = '€0 vandaag dan %1$s op %2$s';