|
14 | 14 | use Magento\Checkout\Test\Fixture\SetGuestEmail as SetGuestEmailFixture; |
15 | 15 | use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethodFixture; |
16 | 16 | use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture; |
| 17 | +use Magento\Framework\App\Config\MutableScopeConfigInterface; |
17 | 18 | use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture; |
18 | 19 | use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture; |
| 20 | +use Magento\Sales\Model\Order\Email\Container\OrderIdentity; |
| 21 | +use Magento\Sales\Model\Order\Email\Sender\OrderSender; |
| 22 | +use Magento\Sales\Model\ResourceModel\Order\CollectionFactory; |
19 | 23 | use Magento\Sales\Test\Fixture\Creditmemo as CreditmemoFixture; |
20 | 24 | use Magento\Sales\Test\Fixture\Invoice as InvoiceFixture; |
21 | 25 | use Magento\SalesRule\Model\Rule; |
|
25 | 29 | use Magento\TestFramework\Fixture\DataFixtureStorage; |
26 | 30 | use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
27 | 31 | use Magento\TestFramework\Helper\Bootstrap; |
| 32 | +use Magento\TestFramework\Mail\Template\TransportBuilderMock; |
28 | 33 | use PHPUnit\Framework\TestCase; |
29 | 34 |
|
| 35 | +/** |
| 36 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 37 | + */ |
30 | 38 | class OrderTest extends TestCase |
31 | 39 | { |
32 | 40 | /** |
33 | 41 | * @var DataFixtureStorage |
34 | 42 | */ |
35 | 43 | private $fixtures; |
36 | 44 |
|
| 45 | + /** |
| 46 | + * @var TransportBuilderMock |
| 47 | + */ |
| 48 | + private $transportBuilderMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var MutableScopeConfigInterface |
| 52 | + */ |
| 53 | + private $mutableScopeConfig; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var CollectionFactory |
| 57 | + */ |
| 58 | + private $collectionFactory; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var EmailSenderHandler |
| 62 | + */ |
| 63 | + private $emailSenderHandler; |
| 64 | + |
37 | 65 | /** |
38 | 66 | * Set up |
39 | 67 | */ |
40 | 68 | protected function setUp(): void |
41 | 69 | { |
42 | 70 | $this->fixtures = Bootstrap::getObjectManager()->get(DataFixtureStorageManager::class)->getStorage(); |
| 71 | + $objectManager = Bootstrap::getObjectManager(); |
| 72 | + $this->collectionFactory = $objectManager->get(CollectionFactory::class); |
| 73 | + $this->transportBuilderMock = $objectManager->get(TransportBuilderMock::class); |
| 74 | + $this->mutableScopeConfig = $objectManager->get(MutableScopeConfigInterface::class); |
| 75 | + $this->emailSenderHandler = Bootstrap::getObjectManager()->create( |
| 76 | + EmailSenderHandler::class, |
| 77 | + [ |
| 78 | + 'emailSender' => $objectManager->get(OrderSender::class), |
| 79 | + 'entityResource' => $objectManager->get(\Magento\Sales\Model\ResourceModel\Order::class), |
| 80 | + 'entityCollection' => $this->collectionFactory->create(), |
| 81 | + 'identityContainer' => $objectManager->create(OrderIdentity::class), |
| 82 | + ] |
| 83 | + ); |
| 84 | + $this->transportBuilderMock->clean(); |
| 85 | + } |
| 86 | + |
| 87 | + protected function tearDown(): void |
| 88 | + { |
| 89 | + $this->transportBuilderMock->clean(); |
| 90 | + parent::tearDown(); |
43 | 91 | } |
44 | 92 |
|
45 | 93 | /** |
@@ -90,4 +138,44 @@ public function testMultipleCreditmemosForZeroTotalOrder() |
90 | 138 | 'Should be possible to create second credit memo for zero total order if not all items are refunded yet' |
91 | 139 | ); |
92 | 140 | } |
| 141 | + |
| 142 | + #[ |
| 143 | + Config('system/smtp/disable', '1', 'store', 'default'), |
| 144 | + Config('sales_email/general/async_sending', '1'), |
| 145 | + DataFixture(ProductFixture::class, as: 'product'), |
| 146 | + DataFixture(GuestCartFixture::class, as: 'cart'), |
| 147 | + DataFixture( |
| 148 | + AddProductToCartFixture::class, |
| 149 | + ['cart_id' => '$cart.id$', 'product_id' => '$product.id$'] |
| 150 | + ), |
| 151 | + DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']), |
| 152 | + DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']), |
| 153 | + DataFixture(SetGuestEmailFixture::class, ['cart_id' => '$cart.id$']), |
| 154 | + DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$cart.id$']), |
| 155 | + DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$cart.id$']), |
| 156 | + DataFixture(PlaceOrderFixture::class, ['cart_id' => '$cart.id$'], 'order') |
| 157 | + ] |
| 158 | + public function testAsyncEmailForOrderCreatedWhenEmailSendingWasDisabled(): void |
| 159 | + { |
| 160 | + $isEmailSent = false; |
| 161 | + $this->transportBuilderMock->setOnMessageSentCallback( |
| 162 | + function () use (&$isEmailSent) { |
| 163 | + $isEmailSent = true; |
| 164 | + } |
| 165 | + ); |
| 166 | + $order = $this->fixtures->get('order'); |
| 167 | + $this->assertEquals(0, $order->getSendEmail()); |
| 168 | + $this->assertNull($order->getEmailSent()); |
| 169 | + $this->mutableScopeConfig->setValue('system/smtp/disable', 0, 'store', 'default'); |
| 170 | + $this->emailSenderHandler->sendEmails(); |
| 171 | + $this->assertFalse( |
| 172 | + $isEmailSent, |
| 173 | + 'Email is not expected to be sent' |
| 174 | + ); |
| 175 | + $collection = $this->collectionFactory->create(); |
| 176 | + $collection->addFieldToFilter('entity_id', $order->getId()); |
| 177 | + $order = $collection->getFirstItem(); |
| 178 | + $this->assertEquals(0, $order->getSendEmail()); |
| 179 | + $this->assertNull($order->getEmailSent()); |
| 180 | + } |
93 | 181 | } |
0 commit comments