Skip to content

Commit 86dcc9b

Browse files
committed
AC-14690: Refresh order doesn't get latest custom attribute data
1 parent c2a8d4a commit 86dcc9b

File tree

1 file changed

+130
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)