Skip to content

Commit 342ebb1

Browse files
committed
MAGETWO-63101: Customers are able to place an order with not allowed country
- Fixed incorrect behavior for virtual quote
1 parent 487c488 commit 342ebb1

File tree

2 files changed

+38
-43
lines changed

2 files changed

+38
-43
lines changed

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ public function validateBeforeSubmit(QuoteEntity $quote)
7070
)
7171
);
7272
}
73+
74+
// Checks if country id present in the allowed countries list.
75+
if (!in_array(
76+
$quote->getShippingAddress()->getCountryId(),
77+
$this->allowedCountryReader->getAllowedCountries()
78+
)) {
79+
throw new \Magento\Framework\Exception\LocalizedException(
80+
__('Some addresses cannot be used due to country-specific configurations.')
81+
);
82+
}
83+
7384
$method = $quote->getShippingAddress()->getShippingMethod();
7485
$rate = $quote->getShippingAddress()->getShippingRateByCode($method);
7586
if (!$method || !$rate) {
@@ -88,18 +99,6 @@ public function validateBeforeSubmit(QuoteEntity $quote)
8899
throw new \Magento\Framework\Exception\LocalizedException(__('Please select a valid payment method.'));
89100
}
90101

91-
// Checks if country id present in the allowed countries list.
92-
if (
93-
!in_array(
94-
$quote->getShippingAddress()->getCountryId(),
95-
$this->allowedCountryReader->getAllowedCountries()
96-
)
97-
) {
98-
throw new \Magento\Framework\Exception\LocalizedException(
99-
__('Some addresses cannot be used due to country-specific configurations.')
100-
);
101-
}
102-
103102
return $this;
104103
}
105104
}

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

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
*/
66
namespace Magento\Quote\Test\Unit\Model;
77

8-
use \Magento\Quote\Model\QuoteValidator;
9-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
108
use Magento\Directory\Model\AllowedCountries;
119
use Magento\Quote\Model\Quote\Address;
12-
use Magento\Quote\Model\Quote\Paymen;
10+
use Magento\Quote\Model\Quote\Payment;
11+
use Magento\Quote\Model\QuoteValidator;
1312

1413
/**
1514
* Class QuoteValidatorTest
@@ -26,12 +25,21 @@ class QuoteValidatorTest extends \PHPUnit_Framework_TestCase
2625
*/
2726
protected $quoteMock;
2827

28+
/**
29+
* @var AllowedCountries|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $allowedCountryReader;
32+
2933
/**
3034
* @return void
3135
*/
3236
protected function setUp()
3337
{
34-
$this->quoteValidator = new \Magento\Quote\Model\QuoteValidator();
38+
$this->allowedCountryReader = $this->getMockBuilder(AllowedCountries::class)
39+
->disableOriginalConstructor()
40+
->getMock();
41+
42+
$this->quoteValidator = new \Magento\Quote\Model\QuoteValidator($this->allowedCountryReader);
3543

3644
$this->quoteMock = $this->getMock(
3745
\Magento\Quote\Model\Quote::class,
@@ -128,21 +136,18 @@ public function testValidateBeforeSubmitThrowsExceptionIfShippingAddressIsInvali
128136
public function testValidateBeforeSubmitThrowsExceptionIfShippingRateIsNotSelected()
129137
{
130138
$shippingMethod = 'checkmo';
131-
$shippingAddressMock = $this->getMock(
132-
\Magento\Quote\Model\Quote\Address::class,
133-
[
134-
'validate',
135-
'getShippingMethod',
136-
'getShippingRateByCode',
137-
'__wakeup'
138-
],
139-
[],
140-
'',
141-
false
142-
);
139+
$shippingAddressMock = $this->getMockBuilder(Address::class)
140+
->disableOriginalConstructor()
141+
->getMock();
142+
143+
$this->allowedCountryReader->method('getAllowedCountries')
144+
->willReturn(['US' => 'US']);
145+
143146
$this->quoteMock->expects($this->any())->method('getShippingAddress')->willReturn($shippingAddressMock);
144147
$this->quoteMock->expects($this->any())->method('isVirtual')->willReturn(false);
145148
$shippingAddressMock->expects($this->any())->method('validate')->willReturn(true);
149+
$shippingAddressMock->method('getCountryId')
150+
->willReturn('US');
146151
$shippingAddressMock->expects($this->any())->method('getShippingMethod')->willReturn($shippingMethod);
147152
$shippingAddressMock->expects($this->once())->method('getShippingRateByCode')->with($shippingMethod);
148153

@@ -188,22 +193,18 @@ public function testValidateBeforeSubmitThrowsExceptionIfPaymentMethodIsNotSelec
188193
*/
189194
public function testValidateBeforeSubmitThrowsExceptionIfCountrySpecificConfigurations()
190195
{
191-
$objectManagerHelper = new ObjectManager($this);
192-
$allowedCountryReader = $this->getMockBuilder(AllowedCountries::class)
193-
->disableOriginalConstructor()
194-
->setMethods(['getAllowedCountries'])
195-
->getMock();
196-
$allowedCountryReader->method('getAllowedCountries')
196+
$this->allowedCountryReader->method('getAllowedCountries')
197197
->willReturn(['EE' => 'EE']);
198198

199199
$addressMock = $this->getMockBuilder(Address::class)
200200
->disableOriginalConstructor()
201-
->setMethods(['getCountryId'])
202201
->getMock();
202+
$addressMock->method('validate')
203+
->willReturn(true);
203204
$addressMock->method('getCountryId')
204205
->willReturn('EU');
205206

206-
$paymentMock = $this->getMockBuilder(Paymen::class)
207+
$paymentMock = $this->getMockBuilder(Payment::class)
207208
->setMethods(['getMethod'])
208209
->disableOriginalConstructor()
209210
->getMock();
@@ -220,17 +221,12 @@ public function testValidateBeforeSubmitThrowsExceptionIfCountrySpecificConfigur
220221
$this->quoteMock->method('getShippingAddress')
221222
->willReturn($addressMock);
222223
$this->quoteMock->method('isVirtual')
223-
->willReturn(true);
224+
->willReturn(false);
224225
$this->quoteMock->method('getBillingAddress')
225226
->willReturn($billingAddressMock);
226227
$this->quoteMock->method('getPayment')
227228
->willReturn($paymentMock);
228229

229-
$quoteValidator = $objectManagerHelper->getObject(
230-
QuoteValidator::class,
231-
['allowedCountryReader' => $allowedCountryReader]
232-
);
233-
234-
$quoteValidator->validateBeforeSubmit($this->quoteMock);
230+
$this->quoteValidator->validateBeforeSubmit($this->quoteMock);
235231
}
236232
}

0 commit comments

Comments
 (0)