Skip to content

Commit 50c4d10

Browse files
committed
MAGETWO-97456: Cart's customer and address mismatch
1 parent 720f05c commit 50c4d10

File tree

3 files changed

+9
-14
lines changed

3 files changed

+9
-14
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,16 @@ public function __construct(
5858
* @param AddressInterface $address
5959
* @param int|null $customerId Cart belongs to
6060
* @return void
61-
* @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid.
61+
* @throws NoSuchEntityException The specified customer ID or address ID is not valid.
6262
*/
6363
private function doValidate(AddressInterface $address, $customerId)
6464
{
6565
//validate customer id
6666
if ($customerId) {
67-
$customer = $this->customerRepository->getById($customerId);
68-
if (!$customer->getId()) {
69-
throw new \Magento\Framework\Exception\NoSuchEntityException(
67+
try {
68+
$customer = $this->customerRepository->getById($customerId);
69+
} catch (NoSuchEntityException $exception) {
70+
throw new NoSuchEntityException(
7071
__('Invalid customer id %1', $customerId)
7172
);
7273
}
@@ -75,15 +76,15 @@ private function doValidate(AddressInterface $address, $customerId)
7576
if ($address->getCustomerAddressId()) {
7677
//Existing address cannot belong to a guest
7778
if (!$customerId) {
78-
throw new \Magento\Framework\Exception\NoSuchEntityException(
79+
throw new NoSuchEntityException(
7980
__('Invalid customer address id %1', $address->getCustomerAddressId())
8081
);
8182
}
8283
//Validating address ID
8384
try {
8485
$this->addressRepository->getById($address->getCustomerAddressId());
8586
} catch (NoSuchEntityException $e) {
86-
throw new \Magento\Framework\Exception\NoSuchEntityException(
87+
throw new NoSuchEntityException(
8788
__('Invalid address id %1', $address->getId())
8889
);
8990
}
@@ -93,7 +94,7 @@ private function doValidate(AddressInterface $address, $customerId)
9394
return $address->getId();
9495
}, $this->customerRepository->getById($customerId)->getAddresses());
9596
if (!in_array($address->getCustomerAddressId(), $applicableAddressIds)) {
96-
throw new \Magento\Framework\Exception\NoSuchEntityException(
97+
throw new NoSuchEntityException(
9798
__('Invalid customer address id %1', $address->getCustomerAddressId())
9899
);
99100
}
@@ -105,7 +106,6 @@ private function doValidate(AddressInterface $address, $customerId)
105106
*
106107
* @param \Magento\Quote\Api\Data\AddressInterface $addressData The address data object.
107108
* @return bool
108-
* @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid.
109109
*/
110110
public function validate(AddressInterface $addressData): bool
111111
{
@@ -120,7 +120,6 @@ public function validate(AddressInterface $addressData): bool
120120
* @param CartInterface $cart
121121
* @param AddressInterface $address
122122
* @return void
123-
* @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid.
124123
*/
125124
public function validateForCart(CartInterface $cart, AddressInterface $address)
126125
{

app/code/Magento/Quote/Test/Unit/Model/QuoteAddressValidatorTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function testValidateInvalidCustomer()
7171

7272
$address->expects($this->atLeastOnce())->method('getCustomerId')->willReturn($customerId);
7373
$this->customerRepositoryMock->expects($this->once())->method('getById')->with($customerId)
74-
->willReturn($customerMock);
74+
->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException());
7575
$this->model->validate($address);
7676
}
7777

@@ -111,7 +111,6 @@ public function testValidateWithValidAddress()
111111
$customerAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
112112

113113
$this->customerRepositoryMock->expects($this->exactly(2))->method('getById')->willReturn($customerMock);
114-
$customerMock->expects($this->once())->method('getId')->willReturn($addressCustomer);
115114

116115
$this->addressRepositoryMock->expects($this->once())->method('getById')->willReturn($this->quoteAddressMock);
117116
$this->quoteAddressMock->expects($this->any())->method('getCustomerId')->willReturn($addressCustomer);

dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_addresses_rollback.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
$customer = $customerRepo->get('customer_with_addresses@test.com');
2323
/** @var AddressRepositoryInterface $addressRepo */
2424
$addressRepo = $objectManager->get(AddressRepositoryInterface::class);
25-
foreach ($customer->getAddresses() as $address) {
26-
$addressRepo->delete($address);
27-
}
2825
$customerRepo->delete($customer);
2926
} catch (NoSuchEntityException $exception) {
3027
//Already deleted

0 commit comments

Comments
 (0)