|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Checkout\Helper; |
| 10 | + |
| 11 | +use Magento\Checkout\Helper\Data; |
| 12 | +use Magento\Framework\ObjectManagerInterface; |
| 13 | +use Magento\Quote\Model\Quote; |
| 14 | +use Magento\Quote\Model\QuoteManagement; |
| 15 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 16 | +use Magento\Sales\Model\Order; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | +use Magento\TestFramework\Mail\Template\TransportBuilderMock; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +class DataTest extends TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var ObjectManagerInterface |
| 25 | + */ |
| 26 | + private ObjectManagerInterface $objectManager; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var QuoteManagement |
| 30 | + */ |
| 31 | + private QuoteManagement $quoteManagement; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var Data |
| 35 | + */ |
| 36 | + private Data $checkoutHelper; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var OrderRepositoryInterface |
| 40 | + */ |
| 41 | + private OrderRepositoryInterface $orderRepository; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var TransportBuilderMock |
| 45 | + */ |
| 46 | + private TransportBuilderMock $transportBuilder; |
| 47 | + |
| 48 | + /** |
| 49 | + * Reserved order ID used in fixture. |
| 50 | + */ |
| 51 | + private const FIXTURE_RESERVED_ORDER_ID = 'test_order_with_virtual_product'; |
| 52 | + |
| 53 | + /** |
| 54 | + * Payment method code to use in test. |
| 55 | + */ |
| 56 | + private const PAYMENT_METHOD = 'checkmo'; |
| 57 | + |
| 58 | + /** |
| 59 | + * Set up required Magento services for the test. |
| 60 | + * |
| 61 | + * @return void |
| 62 | + */ |
| 63 | + protected function setUp(): void |
| 64 | + { |
| 65 | + parent::setUp(); |
| 66 | + |
| 67 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 68 | + $this->quoteManagement = $this->objectManager->get(QuoteManagement::class); |
| 69 | + $this->checkoutHelper = $this->objectManager->get(Data::class); |
| 70 | + $this->orderRepository = $this->objectManager->get(OrderRepositoryInterface::class); |
| 71 | + $this->transportBuilder = $this->objectManager->get(TransportBuilderMock::class); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Test sending the "payment failed" email for an order with a virtual product. |
| 76 | + * |
| 77 | + * This test verifies that: |
| 78 | + * - The payment failure email is sent successfully. |
| 79 | + * - The email content does not include shipping address or shipping method |
| 80 | + * since the product is virtual. |
| 81 | + * |
| 82 | + * @magentoDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php |
| 83 | + * |
| 84 | + * @return void |
| 85 | + */ |
| 86 | + public function testSendPaymentFailedEmail(): void |
| 87 | + { |
| 88 | + [$order, $quote] = $this->createOrderFromFixture(); |
| 89 | + $this->simulatePaymentFailure($order); |
| 90 | + |
| 91 | + $this->checkoutHelper->sendPaymentFailedEmail( |
| 92 | + $quote, |
| 93 | + 'Simulated payment failure', |
| 94 | + 'onepage' |
| 95 | + ); |
| 96 | + |
| 97 | + $message = $this->transportBuilder->getSentMessage(); |
| 98 | + $this->assertNotNull($message, 'Expected a payment failed email to be sent.'); |
| 99 | + |
| 100 | + $emailBody = $message->getBody(); |
| 101 | + if (method_exists($emailBody, 'bodyToString')) { |
| 102 | + $emailContent = quoted_printable_decode($emailBody->bodyToString()); |
| 103 | + } elseif (method_exists($emailBody, 'getParts') && isset($emailBody->getParts()[0])) { |
| 104 | + $emailContent = $emailBody->getParts()[0]->getRawContent(); |
| 105 | + } else { |
| 106 | + $this->fail('Unable to extract email content for assertion.'); |
| 107 | + } |
| 108 | + |
| 109 | + $this->assertStringNotContainsString( |
| 110 | + 'Shipping Address', |
| 111 | + $emailContent, |
| 112 | + 'Shipping address should not appear in the payment failed email for virtual product.' |
| 113 | + ); |
| 114 | + $this->assertStringNotContainsString( |
| 115 | + 'Shipping Method', |
| 116 | + $emailContent, |
| 117 | + 'Shipping method should not appear in the payment failed email for virtual product.' |
| 118 | + ); |
| 119 | + $this->assertStringContainsString( |
| 120 | + 'Simulated payment failure', |
| 121 | + $emailContent, |
| 122 | + 'Expected payment failure message to be present in the email.' |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + private function createOrderFromFixture(): array |
| 127 | + { |
| 128 | + /** @var Quote $quote */ |
| 129 | + $quote = $this->objectManager->create(Quote::class) |
| 130 | + ->load(self::FIXTURE_RESERVED_ORDER_ID, 'reserved_order_id'); |
| 131 | + |
| 132 | + $this->assertNotNull($quote->getId(), 'Failed to load quote from fixture.'); |
| 133 | + $this->assertNotEmpty($quote->getAllItems(), 'Quote from fixture is empty.'); |
| 134 | + |
| 135 | + $quote->getPayment()->setMethod(self::PAYMENT_METHOD); |
| 136 | + |
| 137 | + $order = $this->quoteManagement->submit($quote); |
| 138 | + |
| 139 | + $this->assertNotNull($order->getId(), 'Order was not created from quote.'); |
| 140 | + $this->assertNotEmpty($order->getIncrementId(), 'Order increment ID is missing.'); |
| 141 | + |
| 142 | + return [$order, $quote]; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Simulate a payment failure by cancelling the order and adding a history comment. |
| 147 | + * |
| 148 | + * This method updates the order state and status to 'canceled', |
| 149 | + * adds a comment explaining the payment failure, and saves the order. |
| 150 | + * |
| 151 | + * @param Order $order |
| 152 | + * @return void |
| 153 | + */ |
| 154 | + private function simulatePaymentFailure(Order $order): void |
| 155 | + { |
| 156 | + $order->setState(Order::STATE_CANCELED) |
| 157 | + ->setStatus(Order::STATE_CANCELED) |
| 158 | + ->addCommentToStatusHistory((string)__('Simulated: Payment failure due to gateway timeout.')); |
| 159 | + |
| 160 | + $this->orderRepository->save($order); |
| 161 | + |
| 162 | + $this->assertSame( |
| 163 | + Order::STATE_CANCELED, |
| 164 | + $order->getState(), |
| 165 | + 'Order state should be canceled after simulating payment failure.' |
| 166 | + ); |
| 167 | + } |
| 168 | +} |
0 commit comments