Skip to content

Commit 9b7f7a0

Browse files
authored
Merge branch '2.4-develop' into Hammer_PlatForm_Health_246_Scope_14102022
2 parents 49a7ad6 + bb1c882 commit 9b7f7a0

File tree

10 files changed

+251
-199
lines changed

10 files changed

+251
-199
lines changed

app/code/Magento/Checkout/view/frontend/web/js/view/checkout/placeOrderCaptcha.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ function (defaultCaptcha, captchaList, _, placeOrderHooks) {
2020

2121
this._super();
2222
currentCaptcha = captchaList.getCaptchaByFormId(this.formId);
23-
2423
if (currentCaptcha != null) {
2524
currentCaptcha.setIsVisible(true);
2625
this.setCurrentCaptcha(currentCaptcha);
@@ -29,9 +28,11 @@ function (defaultCaptcha, captchaList, _, placeOrderHooks) {
2928
headers['X-Captcha'] = self.captchaValue()();
3029
}
3130
});
32-
placeOrderHooks.afterRequestListeners.push(function () {
33-
self.refresh();
34-
});
31+
if (self.isRequired()) {
32+
placeOrderHooks.afterRequestListeners.push(function () {
33+
self.refresh();
34+
});
35+
}
3536
}
3637
}
3738
});

app/code/Magento/Customer/Test/Fixture/Customer.php

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88

99
namespace Magento\Customer\Test\Fixture;
1010

11+
use Magento\Customer\Api\AccountManagementInterface;
1112
use Magento\Customer\Api\CustomerRepositoryInterface;
1213
use Magento\Customer\Api\Data\AddressInterface;
1314
use Magento\Customer\Api\Data\CustomerInterface;
14-
use Magento\Customer\Model\Customer as CustomerModel;
15-
use Magento\Customer\Model\CustomerFactory;
1615
use Magento\Customer\Model\CustomerRegistry;
1716
use Magento\Framework\DataObject;
1817
use Magento\Framework\Exception\LocalizedException;
@@ -36,10 +35,10 @@ class Customer implements RevertibleDataFixtureInterface
3635
CustomerInterface::CREATED_IN => null,
3736
CustomerInterface::DOB => null,
3837
CustomerInterface::EMAIL => 'customer%uniqid%@mail.com',
39-
CustomerInterface::FIRSTNAME => 'Firstname %uniqid%',
38+
CustomerInterface::FIRSTNAME => 'Firstname%uniqid%',
4039
CustomerInterface::GENDER => null,
4140
CustomerInterface::GROUP_ID => null,
42-
CustomerInterface::LASTNAME => 'Lastname %uniqid%',
41+
CustomerInterface::LASTNAME => 'Lastname%uniqid%',
4342
CustomerInterface::MIDDLENAME => null,
4443
CustomerInterface::PREFIX => null,
4544
CustomerInterface::STORE_ID => null,
@@ -49,7 +48,9 @@ class Customer implements RevertibleDataFixtureInterface
4948
CustomerInterface::DEFAULT_BILLING => null,
5049
CustomerInterface::DEFAULT_SHIPPING => null,
5150
CustomerInterface::KEY_ADDRESSES => [],
52-
CustomerInterface::DISABLE_AUTO_GROUP_CHANGE => null
51+
CustomerInterface::DISABLE_AUTO_GROUP_CHANGE => null,
52+
CustomerInterface::CUSTOM_ATTRIBUTES => [],
53+
CustomerInterface::EXTENSION_ATTRIBUTES_KEY => [],
5354
];
5455

5556
private const DEFAULT_DATA_ADDRESS = [
@@ -58,80 +59,68 @@ class Customer implements RevertibleDataFixtureInterface
5859
AddressInterface::REGION => 'Massachusetts',
5960
AddressInterface::REGION_ID => '32',
6061
AddressInterface::COUNTRY_ID => 'US',
61-
AddressInterface::STREET => '123 Test Street',
62+
AddressInterface::STREET => ['%street_number% Test Street%uniqid%'],
6263
AddressInterface::COMPANY => null,
6364
AddressInterface::TELEPHONE => '1234567890',
6465
AddressInterface::FAX => null,
6566
AddressInterface::POSTCODE => '02108',
6667
AddressInterface::CITY => 'Boston',
67-
AddressInterface::FIRSTNAME => 'Firstname %uniqid%',
68-
AddressInterface::LASTNAME => 'Lastname %uniqid%',
68+
AddressInterface::FIRSTNAME => 'Firstname%uniqid%',
69+
AddressInterface::LASTNAME => 'Lastname%uniqid%',
6970
AddressInterface::MIDDLENAME => null,
7071
AddressInterface::PREFIX => null,
7172
AddressInterface::SUFFIX => null,
7273
AddressInterface::VAT_ID => null,
7374
AddressInterface::DEFAULT_BILLING => true,
74-
AddressInterface::DEFAULT_SHIPPING => true
75+
AddressInterface::DEFAULT_SHIPPING => true,
76+
AddressInterface::CUSTOM_ATTRIBUTES => [],
77+
AddressInterface::EXTENSION_ATTRIBUTES_KEY => [],
7578
];
7679

7780
/**
7881
* @var ServiceFactory
7982
*/
80-
private $serviceFactory;
83+
private ServiceFactory $serviceFactory;
8184

8285
/**
83-
* @var CustomerRepositoryInterface
86+
* @var AccountManagementInterface
8487
*/
85-
private $customerRepository;
86-
87-
/**
88-
* @var CustomerFactory
89-
*/
90-
private $customerFactory;
88+
private AccountManagementInterface $accountManagement;
9189

9290
/**
9391
* @var CustomerRegistry
9492
*/
95-
private $customerRegistry;
93+
private CustomerRegistry $customerRegistry;
9694

9795
/**
9896
* @var ProcessorInterface
9997
*/
100-
private $dataProcessor;
98+
private ProcessorInterface $dataProcessor;
10199

102100
/**
103101
* @var DataMerger
104102
*/
105-
private $dataMerger;
106-
107-
/**
108-
* @var CustomerModel|null
109-
*/
110-
private $customer;
103+
private DataMerger $dataMerger;
111104

112105
/**
113106
* @param ServiceFactory $serviceFactory
114-
* @param CustomerRepositoryInterface $customerRepository
115-
* @param CustomerFactory $customerFactory
107+
* @param AccountManagementInterface $accountManagement
116108
* @param CustomerRegistry $customerRegistry
117109
* @param ProcessorInterface $dataProcessor
118110
* @param DataMerger $dataMerger
119111
*/
120112
public function __construct(
121113
ServiceFactory $serviceFactory,
122-
CustomerRepositoryInterface $customerRepository,
123-
CustomerFactory $customerFactory,
114+
AccountManagementInterface $accountManagement,
124115
CustomerRegistry $customerRegistry,
125116
ProcessorInterface $dataProcessor,
126-
DataMerger $dataMerger,
117+
DataMerger $dataMerger
127118
) {
128119
$this->serviceFactory = $serviceFactory;
129-
$this->customerRepository = $customerRepository;
130-
$this->customerFactory = $customerFactory;
120+
$this->accountManagement = $accountManagement;
131121
$this->customerRegistry = $customerRegistry;
132122
$this->dataProcessor = $dataProcessor;
133123
$this->dataMerger = $dataMerger;
134-
$this->customer = null;
135124
}
136125

137126
/**
@@ -145,14 +134,12 @@ public function apply(array $data = []): ?DataObject
145134
{
146135
$customerSaveService = $this->serviceFactory->create(CustomerRepositoryInterface::class, 'save');
147136
$data = $this->prepareData($data);
148-
if (count($data[CustomerInterface::KEY_ADDRESSES])) {
149-
$addresses = $this->prepareCustomerAddress($data[CustomerInterface::KEY_ADDRESSES]);
150-
$data[CustomerInterface::KEY_ADDRESSES] = $addresses;
151-
}
137+
$passwordHash = $this->accountManagement->getPasswordHash($data['password']);
138+
unset($data['password']);
152139
$customerSaveService->execute(
153140
[
154141
'customer' => $data,
155-
'passwordHash' => $this->customer->getPasswordHash()
142+
'passwordHash' => $passwordHash
156143
]
157144
);
158145
return $this->customerRegistry->retrieveByEmail($data['email'], $data['website_id']);
@@ -180,29 +167,33 @@ public function revert(DataObject $data): void
180167
*/
181168
private function prepareData(array $data): array
182169
{
183-
$data = $this->dataMerger->merge(self::DEFAULT_DATA, $data, false);
184-
185-
$this->customer = $this->customerFactory->create(['data' => $data]);
186-
$this->customer->setPassword($data['password']);
187-
if (isset($data['password'])) {
188-
unset($data['password']);
189-
}
170+
$data = $this->dataMerger->merge(self::DEFAULT_DATA, $data);
171+
$data[CustomerInterface::KEY_ADDRESSES] = $this->prepareAddresses($data[CustomerInterface::KEY_ADDRESSES]);
190172

191173
return $this->dataProcessor->process($this, $data);
192174
}
193175

194176
/**
195-
* Prepare customer address
177+
* Prepare customer addresses
196178
*
197179
* @param array $data
198180
* @return array
199181
*/
200-
private function prepareCustomerAddress(array $data): array
182+
private function prepareAddresses(array $data): array
201183
{
202184
$addresses = [];
185+
$default = self::DEFAULT_DATA_ADDRESS;
186+
$streetNumber = 123;
203187
foreach ($data as $dataAddress) {
204-
$dataAddress = $this->dataMerger->merge(self::DEFAULT_DATA_ADDRESS, $dataAddress, false);
205-
$addresses[] = $this->dataProcessor->process($this, $dataAddress);
188+
$dataAddress = $this->dataMerger->merge($default, $dataAddress);
189+
$placeholders = ['%street_number%' => $streetNumber++];
190+
$dataAddress[AddressInterface::STREET] = array_map(
191+
fn ($str) => strtr($str, $placeholders),
192+
$dataAddress[AddressInterface::STREET]
193+
);
194+
$addresses[] = $dataAddress;
195+
$default[AddressInterface::DEFAULT_BILLING] = false;
196+
$default[AddressInterface::DEFAULT_SHIPPING] = false;
206197
}
207198

208199
return $addresses;

app/code/Magento/Quote/Model/QuoteManagement.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Magento\Framework\Exception\NoSuchEntityException;
2525
use Magento\Framework\Exception\StateException;
2626
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
27+
use Magento\Framework\Lock\LockManagerInterface;
2728
use Magento\Framework\Model\AbstractExtensibleModel;
2829
use Magento\Framework\Validator\Exception as ValidatorException;
2930
use Magento\Payment\Model\Method\AbstractMethod;
@@ -51,6 +52,10 @@
5152
*/
5253
class QuoteManagement implements CartManagementInterface
5354
{
55+
private const LOCK_PREFIX = 'PLACE_ORDER_';
56+
57+
private const LOCK_TIMEOUT = 10;
58+
5459
/**
5560
* @var EventManager
5661
*/
@@ -151,6 +156,11 @@ class QuoteManagement implements CartManagementInterface
151156
*/
152157
protected $quoteFactory;
153158

159+
/**
160+
* @var LockManagerInterface
161+
*/
162+
private $lockManager;
163+
154164
/**
155165
* @var QuoteIdMaskFactory
156166
*/
@@ -201,6 +211,7 @@ class QuoteManagement implements CartManagementInterface
201211
* @param AddressRepositoryInterface|null $addressRepository
202212
* @param RequestInterface|null $request
203213
* @param RemoteAddress $remoteAddress
214+
* @param LockManagerInterface $lockManager
204215
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
205216
*/
206217
public function __construct(
@@ -227,7 +238,8 @@ public function __construct(
227238
QuoteIdMaskFactory $quoteIdMaskFactory = null,
228239
AddressRepositoryInterface $addressRepository = null,
229240
RequestInterface $request = null,
230-
RemoteAddress $remoteAddress = null
241+
RemoteAddress $remoteAddress = null,
242+
LockManagerInterface $lockManager = null
231243
) {
232244
$this->eventManager = $eventManager;
233245
$this->submitQuoteValidator = $submitQuoteValidator;
@@ -257,6 +269,8 @@ public function __construct(
257269
->get(RequestInterface::class);
258270
$this->remoteAddress = $remoteAddress ?: ObjectManager::getInstance()
259271
->get(RemoteAddress::class);
272+
$this->lockManager = $lockManager ?: ObjectManager::getInstance()
273+
->get(LockManagerInterface::class);
260274
}
261275

262276
/**
@@ -584,25 +598,29 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
584598
$order->setCustomerFirstname($quote->getCustomerFirstname());
585599
$order->setCustomerMiddlename($quote->getCustomerMiddlename());
586600
$order->setCustomerLastname($quote->getCustomerLastname());
587-
588601
if ($quote->getOrigOrderId()) {
589602
$order->setEntityId($quote->getOrigOrderId());
590603
}
591-
592604
if ($quote->getReservedOrderId()) {
593605
$order->setIncrementId($quote->getReservedOrderId());
594606
}
595-
596607
$this->submitQuoteValidator->validateOrder($order);
597-
598608
$this->eventManager->dispatch(
599609
'sales_model_service_quote_submit_before',
600610
[
601611
'order' => $order,
602612
'quote' => $quote
603613
]
604614
);
615+
616+
$lockedName = self::LOCK_PREFIX . $quote->getId();
617+
if ($this->lockManager->isLocked($lockedName)) {
618+
throw new LocalizedException(__(
619+
'A server error stopped your order from being placed. Please try to place your order again.'
620+
));
621+
}
605622
try {
623+
$this->lockManager->lock($lockedName, self::LOCK_TIMEOUT);
606624
$order = $this->orderManagement->place($order);
607625
$quote->setIsActive(false);
608626
$this->eventManager->dispatch(
@@ -613,7 +631,9 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
613631
]
614632
);
615633
$this->quoteRepository->save($quote);
634+
$this->lockManager->unlock($lockedName);
616635
} catch (\Exception $e) {
636+
$this->lockManager->unlock($lockedName);
617637
$this->rollbackAddresses($quote, $order, $e);
618638
throw $e;
619639
}

0 commit comments

Comments
 (0)