From 84c837cc75df80e1cfcb4fcf38655c38af2195dd Mon Sep 17 00:00:00 2001 From: m-muxfeld-diw <143803170+m-muxfeld-diw@users.noreply.github.com> Date: Mon, 16 Sep 2024 12:34:50 +0200 Subject: [PATCH] pishps314 cypress (#836) * PISHPS-314: Added refund credit notes * PISHPS-314: cypress test; fixed bug for refunds with no line items --- .../Api/Order/RefundControllerBase.php | 11 ++++++--- .../Refund/Exceptions/CreditNoteException.php | 6 +++-- .../Refund/RefundCreditNoteService.php | 10 ++++++-- .../e2e/storefront/refund/refund.cy.js | 24 ++++++++++++++++++- 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/Controller/Api/Order/RefundControllerBase.php b/src/Controller/Api/Order/RefundControllerBase.php index 2c10ee1c7..c8a8c430b 100644 --- a/src/Controller/Api/Order/RefundControllerBase.php +++ b/src/Controller/Api/Order/RefundControllerBase.php @@ -191,7 +191,7 @@ public function refundOrderID(RequestDataBag $data, Context $context): JsonRespo $context ); - if ($response->getStatusCode() === 200 && $response->getContent() !== false) { + if ($response->getStatusCode() === 200 && $response->getContent() !== false && count($items) > 0) { $refundId = json_decode($response->getContent(), true)['refundId']; try { $this->creditNoteService->addCreditNoteToOrder($orderId, $refundId, $items, $context); @@ -220,8 +220,13 @@ public function cancel(RequestDataBag $data, Context $context): JsonResponse try { $this->creditNoteService->cancelCreditNoteToOrder($orderId, $refundId, $context); } catch (CreditNoteException $exception) { - $this->logger->error($exception->getMessage(), ['code' => $exception->getCode(),]); - return $this->buildErrorResponse($exception->getMessage()); + if ($exception->getCode() === CreditNoteException::CODE_REMOVING_CREDIT_NOTE_LINE_ITEMS) { + $this->logger->error($exception->getMessage(), ['code' => $exception->getCode(),]); + return $this->buildErrorResponse($exception->getMessage()); + } + if ($exception->getCode() === CreditNoteException::CODE_WARNING_LEVEL) { + $this->logger->warning($exception->getMessage(), ['code' => $exception->getCode(),]); + } } } diff --git a/src/Service/Refund/Exceptions/CreditNoteException.php b/src/Service/Refund/Exceptions/CreditNoteException.php index 5f67bc90f..a53fe1546 100644 --- a/src/Service/Refund/Exceptions/CreditNoteException.php +++ b/src/Service/Refund/Exceptions/CreditNoteException.php @@ -7,6 +7,8 @@ class CreditNoteException extends \Exception { public const CODE_ADDING_CREDIT_NOTE_LINE_ITEMS = 1; public const CODE_REMOVING_CREDIT_NOTE_LINE_ITEMS = 2; + public const CODE_WARNING_LEVEL = 3; + final private function __construct(string $message, int $code) { parent::__construct($message, $code); @@ -17,8 +19,8 @@ public static function forAddingLineItems(string $message): CreditNoteException return new self($message, self::CODE_ADDING_CREDIT_NOTE_LINE_ITEMS); } - public static function forRemovingLineItems(string $message): CreditNoteException + public static function forRemovingLineItems(string $message, int $code = self::CODE_REMOVING_CREDIT_NOTE_LINE_ITEMS): CreditNoteException { - return new self($message, self::CODE_REMOVING_CREDIT_NOTE_LINE_ITEMS); + return new self($message, $code); } } diff --git a/src/Service/Refund/RefundCreditNoteService.php b/src/Service/Refund/RefundCreditNoteService.php index 6ec46ec4f..d0ebe62ca 100644 --- a/src/Service/Refund/RefundCreditNoteService.php +++ b/src/Service/Refund/RefundCreditNoteService.php @@ -150,7 +150,10 @@ public function cancelCreditNoteToOrder(string $orderId, string $refundId, Conte $lineItems = $order->getLineItems(); if ($lineItems === null) { - throw CreditNoteException::forRemovingLineItems(sprintf('No line items found for order. OrderID: %s RefundID: %s', $orderId, $refundId)); + throw CreditNoteException::forRemovingLineItems( + sprintf('No line items found for order. OrderID: %s RefundID: %s', $orderId, $refundId), + CreditNoteException::CODE_WARNING_LEVEL + ); } $ids = []; @@ -170,7 +173,10 @@ public function cancelCreditNoteToOrder(string $orderId, string $refundId, Conte } if (empty($ids)) { - throw CreditNoteException::forRemovingLineItems(sprintf('No credit note line items found for order. OrderID: %s RefundID: %s', $orderId, $refundId)); + throw CreditNoteException::forRemovingLineItems( + sprintf('No credit note line items found for order. OrderID: %s RefundID: %s', $orderId, $refundId), + CreditNoteException::CODE_WARNING_LEVEL + ); } $this->orderLineItemRepository->delete($ids, $context); diff --git a/tests/Cypress/cypress/e2e/storefront/refund/refund.cy.js b/tests/Cypress/cypress/e2e/storefront/refund/refund.cy.js index f787f0940..dbcf1b6c2 100644 --- a/tests/Cypress/cypress/e2e/storefront/refund/refund.cy.js +++ b/tests/Cypress/cypress/e2e/storefront/refund/refund.cy.js @@ -73,7 +73,13 @@ context("Order Refunds", () => { // now start the partial refund refundManager.fullRefund(REFUND_DESCRIPTION, REFUND_INTERNAL_DESCRIPTION); - // verify that our refund now exists + cy.wait(5000); // wait for the page to reload + + adminOrders.openRefundManager(); + + cy.wait(1500); + + // // verify that our refund now exists repoRefundManager.getFirstRefundStatusLabel().contains('Pending'); repoRefundManager.getFirstRefundPublicDescriptionLabel().contains(REFUND_DESCRIPTION); @@ -108,6 +114,10 @@ context("Order Refunds", () => { // now start the partial refund refundManager.partialAmountRefund(2, REFUND_DESCRIPTION); + cy.wait(5000); // wait for the page to reload + + adminOrders.openRefundManager(); + // verify that our refund now exists repoRefundManager.getFirstRefundStatusLabel().contains('Pending'); repoRefundManager.getFirstRefundPublicDescriptionLabel().contains(REFUND_DESCRIPTION); @@ -144,6 +154,10 @@ context("Order Refunds", () => { // now start the partial refund with a custom amount refundManager.partialAmountRefund(2, REFUND_DESCRIPTION); + cy.wait(5000); // wait for the page to reload + + adminOrders.openRefundManager(); + // ------------------------------------------------------------------------------- repoRefundManager.getFirstRefundStatusLabel().contains('Pending'); @@ -188,6 +202,10 @@ context("Order Refunds", () => { // now start the full refund refundManager.fullRefund(REFUND_DESCRIPTION, ''); + cy.wait(5000); // wait for the page to reload + + adminOrders.openRefundManager(); + // verify that our refund now exists repoRefundManager.getFirstRefundStatusLabel().contains('Pending'); repoRefundManager.getFirstRefundPublicDescriptionLabel().contains(REFUND_DESCRIPTION); @@ -204,6 +222,10 @@ context("Order Refunds", () => { // now start another full refund refundManager.fullRefund(REFUND_DESCRIPTION, ''); + cy.wait(5000); // wait for the page to reload + + adminOrders.openRefundManager(); + cy.contains(CANCELED_REFUND_STATUS_LABEL).should('not.exist'); // second cancel should clear the history