|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Sales\Model\AdminOrder; |
| 9 | + |
| 10 | +use Magento\Backend\Model\Session\Quote as AdminQuoteSession; |
| 11 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 12 | +use Magento\Customer\Test\Fixture\Customer as CustomerFixture; |
| 13 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 14 | +use Magento\Quote\Model\QuoteFactory; |
| 15 | +use Magento\Store\Model\StoreManagerInterface; |
| 16 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | +use Magento\TestFramework\Fixture\DataFixture; |
| 19 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +class CreateGetQuoteAssignCustomerTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var DataFixtureStorage |
| 26 | + */ |
| 27 | + private $fixtures; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var \Magento\Framework\ObjectManagerInterface |
| 31 | + */ |
| 32 | + private $objectManager; |
| 33 | + |
| 34 | + /** |
| 35 | + * @inheritdoc |
| 36 | + */ |
| 37 | + protected function setUp(): void |
| 38 | + { |
| 39 | + parent::setUp(); |
| 40 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 41 | + $this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Ensures Create::getQuote() assigns fresh CustomerInterface to the quote |
| 46 | + * when a customerId is present on the quote. |
| 47 | + * |
| 48 | + * @magentoAppArea adminhtml |
| 49 | + * @magentoDbIsolation enabled |
| 50 | + */ |
| 51 | + #[ |
| 52 | + DataFixture(CustomerFixture::class, as: 'customer') |
| 53 | + ] |
| 54 | + public function testGetQuoteAssignsFreshCustomerDataWhenCustomerIdPresent(): void |
| 55 | + { |
| 56 | + /** @var StoreManagerInterface $storeManager */ |
| 57 | + $storeManager = $this->objectManager->get(StoreManagerInterface::class); |
| 58 | + $storeId = (int)$storeManager->getStore()->getId(); |
| 59 | + |
| 60 | + /** @var CustomerRepositoryInterface $customerRepo */ |
| 61 | + $customerRepo = $this->objectManager->get(CustomerRepositoryInterface::class); |
| 62 | + |
| 63 | + /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */ |
| 64 | + $customer = $this->fixtures->get('customer'); |
| 65 | + $customer = $customerRepo->getById((int)$customer->getId()); // ensure latest |
| 66 | + |
| 67 | + /** @var QuoteFactory $quoteFactory */ |
| 68 | + $quoteFactory = $this->objectManager->get(QuoteFactory::class); |
| 69 | + /** @var CartRepositoryInterface $quoteRepo */ |
| 70 | + $quoteRepo = $this->objectManager->get(CartRepositoryInterface::class); |
| 71 | + /** @var AdminQuoteSession $session */ |
| 72 | + $session = $this->objectManager->get(AdminQuoteSession::class); |
| 73 | + |
| 74 | + // Create a quote with stale customer-related fields |
| 75 | + $quote = $quoteFactory->create(); |
| 76 | + $quote->setStoreId($storeId); |
| 77 | + $quote->setCustomerId((int)$customer->getId()); |
| 78 | + $quote-> setCustomerEmail( '[email protected]'); |
| 79 | + $quoteRepo->save($quote); |
| 80 | + |
| 81 | + $session->setQuoteId((int)$quote->getId()); |
| 82 | + |
| 83 | + /** @var Create $create */ |
| 84 | + $create = $this->objectManager->create(Create::class); |
| 85 | + |
| 86 | + $result = $create->getQuote(); |
| 87 | + |
| 88 | + // Assert quote was refreshed from repository via assignCustomer() |
| 89 | + $this->assertSame((int)$customer->getId(), (int)$result->getCustomerId()); |
| 90 | + $this->assertSame($customer->getEmail(), $result->getCustomerEmail()); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Ensures Create::getQuote() does not assign a customer when no customerId is present. |
| 95 | + * |
| 96 | + * @magentoAppArea adminhtml |
| 97 | + * @magentoAppIsolation enabled |
| 98 | + * @magentoDbIsolation enabled |
| 99 | + */ |
| 100 | + public function testGetQuoteDoesNotAssignWhenNoCustomerId(): void |
| 101 | + { |
| 102 | + $session = $this->objectManager->get(\Magento\Backend\Model\Session\Quote::class); |
| 103 | + $session->unsQuoteId(); |
| 104 | + $session->unsCustomerId(); |
| 105 | + |
| 106 | + $storeManager = $this->objectManager->get(\Magento\Store\Model\StoreManagerInterface::class); |
| 107 | + $storeId = (int)$storeManager->getStore()->getId(); |
| 108 | + |
| 109 | + $quoteFactory = $this->objectManager->get(\Magento\Quote\Model\QuoteFactory::class); |
| 110 | + |
| 111 | + // Create a quote without a customer |
| 112 | + $quote = $quoteFactory->create(); |
| 113 | + $quote->setStoreId($storeId); |
| 114 | + $quote->setCustomerId(null); |
| 115 | + $quote-> setCustomerEmail( '[email protected]'); |
| 116 | + |
| 117 | + /** @var \Magento\Sales\Model\AdminOrder\Create $create */ |
| 118 | + $create = $this->objectManager->create(\Magento\Sales\Model\AdminOrder\Create::class); |
| 119 | + |
| 120 | + // Bypass session state by injecting the quote directly |
| 121 | + $create->setQuote($quote); |
| 122 | + |
| 123 | + $result = $create->getQuote(); |
| 124 | + |
| 125 | + $this->assertSame($quote, $result); |
| 126 | + $this->assertEmpty($result->getCustomerId()); |
| 127 | + $this-> assertSame( '[email protected]', $result-> getCustomerEmail()); |
| 128 | + } |
| 129 | +} |
0 commit comments