|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Checkout\Test\Integration; |
| 9 | + |
| 10 | +use Magento\Checkout\Helper\Data; |
| 11 | +use Magento\Framework\ObjectManagerInterface; |
| 12 | +use Magento\Quote\Model\QuoteFactory; |
| 13 | +use Magento\Quote\Model\QuoteManagement; |
| 14 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 15 | +use Magento\Sales\Model\Order; |
| 16 | +use Magento\TestFramework\Helper\Bootstrap; |
| 17 | +use Magento\TestFramework\Mail\Template\TransportBuilderMock; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Integration test for sending the "payment failed" email on virtual product order failure. |
| 22 | + * |
| 23 | + * @magentoAppIsolation enabled |
| 24 | + * @magentoDbIsolation enabled |
| 25 | + * @magentoDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php |
| 26 | + */ |
| 27 | +class VirtualProductFailedPaymentEmailTest extends TestCase |
| 28 | +{ |
| 29 | + /** @var ObjectManagerInterface */ |
| 30 | + private ObjectManagerInterface $objectManager; |
| 31 | + |
| 32 | + /** @var QuoteManagement */ |
| 33 | + private QuoteManagement $quoteManagement; |
| 34 | + |
| 35 | + /** @var QuoteFactory */ |
| 36 | + private QuoteFactory $quoteFactory; |
| 37 | + |
| 38 | + /** @var Data */ |
| 39 | + private Data $checkoutHelper; |
| 40 | + |
| 41 | + /** @var OrderRepositoryInterface */ |
| 42 | + private OrderRepositoryInterface $orderRepository; |
| 43 | + |
| 44 | + protected function setUp(): void |
| 45 | + { |
| 46 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 47 | + $this->quoteManagement = $this->objectManager->get(QuoteManagement::class); |
| 48 | + $this->quoteFactory = $this->objectManager->get(QuoteFactory::class); |
| 49 | + $this->checkoutHelper = $this->objectManager->get(Data::class); |
| 50 | + $this->orderRepository = $this->objectManager->get(OrderRepositoryInterface::class); |
| 51 | + } |
| 52 | + |
| 53 | + public function testSendPaymentFailedEmail(): void |
| 54 | + { |
| 55 | + [$order, $quote] = $this->prepareOrderFromFixtureQuote(); |
| 56 | + $this->simulatePaymentFailure($order); |
| 57 | + |
| 58 | + $this->checkoutHelper->sendPaymentFailedEmail( |
| 59 | + $quote, |
| 60 | + __('Simulated payment failure')->render(), |
| 61 | + $quote->getPayment()->getMethod(), |
| 62 | + $quote->getCheckoutMethod() |
| 63 | + ); |
| 64 | + |
| 65 | + /** @var TransportBuilderMock $transportBuilder */ |
| 66 | + $transportBuilder = $this->objectManager->get(TransportBuilderMock::class); |
| 67 | + /** @var \Magento\Framework\Mail\EmailMessageInterface $message */ |
| 68 | + $message = $transportBuilder->getSentMessage(); |
| 69 | + $this->assertNotNull($message, 'Expected a payment failed email to be sent.'); |
| 70 | + |
| 71 | + // Compatibility with Magento 2.4.6+ and 2.4.7+ |
| 72 | + $emailBody = $message->getBody(); |
| 73 | + $emailContent = method_exists($emailBody, 'bodyToString') |
| 74 | + ? quoted_printable_decode($emailBody->bodyToString()) |
| 75 | + : $emailBody->getParts()[0]->getRawContent(); |
| 76 | + $this->assertStringNotContainsString( |
| 77 | + 'Shipping Address', |
| 78 | + $emailContent, |
| 79 | + 'Shipping address should not appear in the payment failed email for virtual product.' |
| 80 | + ); |
| 81 | + $this->assertStringNotContainsString( |
| 82 | + 'Shipping Method', |
| 83 | + $emailContent, |
| 84 | + 'Shipping method should not appear in the payment failed email for virtual product.' |
| 85 | + ); |
| 86 | + } |
| 87 | + private function prepareOrderFromFixtureQuote(): array |
| 88 | + { |
| 89 | + /** @var \Magento\Quote\Model\Quote $quote */ |
| 90 | + $quote = $this->quoteFactory->create()->loadByIdWithoutStore(1); |
| 91 | + $this->assertTrue((bool)$quote->getId(), 'Quote was not loaded from fixture.'); |
| 92 | + $this->assertNotEmpty($quote->getAllItems(), 'Quote from fixture is empty.'); |
| 93 | + $quote->getPayment()->setMethod('checkmo'); |
| 94 | + |
| 95 | + $order = $this->quoteManagement->submit($quote); |
| 96 | + $this->assertNotNull($order->getId(), 'Order was not created.'); |
| 97 | + $this->assertNotEmpty($order->getIncrementId(), 'Order increment ID is missing.'); |
| 98 | + return [$order, $quote]; |
| 99 | + } |
| 100 | + private function simulatePaymentFailure(Order $order): void |
| 101 | + { |
| 102 | + $order->setStatus(Order::STATE_CANCELED) |
| 103 | + ->setState(Order::STATE_CANCELED) |
| 104 | + ->addCommentToStatusHistory(__('Simulated: Payment failure due to gateway timeout.')); |
| 105 | + $this->orderRepository->save($order); |
| 106 | + $this->assertSame( |
| 107 | + Order::STATE_CANCELED, |
| 108 | + $order->getState(), |
| 109 | + 'Order state should be canceled after simulating payment failure.' |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
0 commit comments