Skip to content

Commit 6f4d9ca

Browse files
committed
Merge branch 'MAGETWO-63101' into MPI-Bugfixes
2 parents 88663e0 + c5359a9 commit 6f4d9ca

File tree

8 files changed

+174
-3
lines changed

8 files changed

+174
-3
lines changed

app/code/Magento/Multishipping/Model/Checkout/Type/Multishipping.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
1414
use Magento\Framework\Exception\LocalizedException;
1515
use Magento\Framework\App\ObjectManager;
16+
use Magento\Directory\Model\AllowedCountries;
1617

1718
/**
1819
* Multishipping checkout model
@@ -146,12 +147,19 @@ class Multishipping extends \Magento\Framework\DataObject
146147
*/
147148
private $cartExtensionFactory;
148149

150+
/**
151+
* @var AllowedCountries
152+
*/
153+
private $allowedCountryReader;
154+
149155
/**
150156
* @var \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor
151157
*/
152158
private $shippingAssignmentProcessor;
153159

154160
/**
161+
* Multishipping constructor.
162+
*
155163
* @param \Magento\Checkout\Model\Session $checkoutSession
156164
* @param \Magento\Customer\Model\Session $customerSession
157165
* @param \Magento\Sales\Model\OrderFactory $orderFactory
@@ -174,6 +182,7 @@ class Multishipping extends \Magento\Framework\DataObject
174182
* @param \Magento\Framework\Api\FilterBuilder $filterBuilder
175183
* @param \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector
176184
* @param array $data
185+
* @param AllowedCountries|null $allowedCountryReader
177186
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
178187
*/
179188
public function __construct(
@@ -198,7 +207,8 @@ public function __construct(
198207
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
199208
\Magento\Framework\Api\FilterBuilder $filterBuilder,
200209
\Magento\Quote\Model\Quote\TotalsCollector $totalsCollector,
201-
array $data = []
210+
array $data = [],
211+
AllowedCountries $allowedCountryReader = null
202212
) {
203213
$this->_eventManager = $eventManager;
204214
$this->_scopeConfig = $scopeConfig;
@@ -221,6 +231,8 @@ public function __construct(
221231
$this->quotePaymentToOrderPayment = $quotePaymentToOrderPayment;
222232
$this->quoteAddressToOrderAddress = $quoteAddressToOrderAddress;
223233
$this->totalsCollector = $totalsCollector;
234+
$this->allowedCountryReader = $allowedCountryReader ?: ObjectManager::getInstance()
235+
->get(AllowedCountries::class);
224236
parent::__construct($data);
225237
$this->_init();
226238
}
@@ -696,6 +708,18 @@ protected function _validate()
696708
__('Please specify shipping methods for all addresses.')
697709
);
698710
}
711+
712+
// Checks if a country id present in the allowed countries list.
713+
if (
714+
!in_array(
715+
$address->getCountryId(),
716+
$this->allowedCountryReader->getAllowedCountries()
717+
)
718+
) {
719+
throw new \Magento\Framework\Exception\LocalizedException(
720+
__('Some addresses cannot be used due to country-specific configurations.')
721+
);
722+
}
699723
}
700724
$addressValidation = $quote->getBillingAddress()->validate();
701725
if ($addressValidation !== true) {

app/code/Magento/Multishipping/Test/Unit/Model/Checkout/Type/MultishippingTest.php

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
use Magento\Store\Model\StoreManagerInterface;
4545
use PHPUnit_Framework_MockObject_MockObject;
4646
use PHPUnit_Framework_TestCase;
47+
use Magento\Quote\Model\Quote\Payment;
48+
use Magento\Payment\Model\Method\AbstractMethod;
49+
use Magento\Directory\Model\AllowedCountries;
4750

4851
/**
4952
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -146,6 +149,13 @@ protected function setUp()
146149
$this->customerSessionMock->expects($this->atLeastOnce())->method('getCustomerDataObject')
147150
->willReturn($this->customerMock);
148151
$this->totalsCollectorMock = $this->createSimpleMock(TotalsCollector::class);
152+
$allowedCountryReaderMock = $this->getMockBuilder(AllowedCountries::class)
153+
->disableOriginalConstructor()
154+
->setMethods(['getAllowedCountries'])
155+
->getMock();
156+
$allowedCountryReaderMock->method('getAllowedCountries')
157+
->willReturn(['EN'=>'EN']);
158+
149159
$this->model = new Multishipping(
150160
$this->checkoutSessionMock,
151161
$this->customerSessionMock,
@@ -168,7 +178,8 @@ protected function setUp()
168178
$this->searchCriteriaBuilderMock,
169179
$this->filterBuilderMock,
170180
$this->totalsCollectorMock,
171-
$data
181+
$data,
182+
$allowedCountryReaderMock
172183
);
173184

174185
$this->cartExtensionFactoryMock = $this->getMockBuilder(CartExtensionFactory::class)
@@ -357,6 +368,49 @@ public function testSetShippingMethods()
357368
$this->model->setShippingMethods($methodsArray);
358369
}
359370

371+
/**
372+
* Tests exception for addresses with country id not in the allowed countries list.
373+
*
374+
* @expectedException \Magento\Framework\Exception\LocalizedException
375+
* @expectedExceptionMessage Some addresses cannot be used due to country-specific configurations.
376+
*/
377+
public function testCreateOrdersCountryNotPresentInAllowedListException()
378+
{
379+
$abstractMethod = $this->getMockBuilder(AbstractMethod::class)
380+
->disableOriginalConstructor()
381+
->setMethods(['isAvailable'])
382+
->getMockForAbstractClass();
383+
$abstractMethod->method('isAvailable')
384+
->willReturn(true);
385+
386+
$paymentMock = $this->getMockBuilder(Payment::class)
387+
->disableOriginalConstructor()
388+
->setMethods(['getMethodInstance'])
389+
->getMock();
390+
$paymentMock->method('getMethodInstance')
391+
->willReturn($abstractMethod);
392+
393+
$shippingAddressMock = $this->getMockBuilder(Address::class)
394+
->disableOriginalConstructor()
395+
->setMethods(['validate', 'getShippingMethod', 'getShippingRateByCode', 'getCountryId'])
396+
->getMock();
397+
$shippingAddressMock->method('validate')
398+
->willReturn(true);
399+
$shippingAddressMock->method('getShippingMethod')
400+
->willReturn('carrier');
401+
$shippingAddressMock->method('getShippingRateByCode')
402+
->willReturn('code');
403+
$shippingAddressMock->method('getCountryId')
404+
->willReturn('EU');
405+
406+
$this->quoteMock->method('getPayment')
407+
->willReturn($paymentMock);
408+
$this->quoteMock->method('getAllShippingAddresses')
409+
->willReturn([$shippingAddressMock]);
410+
411+
$this->model->createOrders();
412+
}
413+
360414
/**
361415
* @param ShippingAssignment $shippingAssignmentMock
362416
* @return CartExtension|PHPUnit_Framework_MockObject_MockObject

app/code/Magento/Multishipping/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"magento/module-customer": "100.2.*",
1212
"magento/module-theme": "100.2.*",
1313
"magento/module-quote": "100.2.*",
14-
"magento/framework": "100.2.*"
14+
"magento/framework": "100.2.*",
15+
"magento/module-directory": "100.2.*"
1516
},
1617
"type": "magento2-module",
1718
"version": "100.2.0-dev",

app/code/Magento/Multishipping/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,4 @@ Options,Options
8787
"Review Order","Review Order"
8888
"Select Shipping Method","Select Shipping Method"
8989
"We received your order!","We received your order!"
90+
"Some addresses cannot be used due to country-specific configurations.","Some addresses cannot be used due to country-specific configurations."

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
namespace Magento\Quote\Model;
88

99
use Magento\Quote\Model\Quote as QuoteEntity;
10+
use Magento\Directory\Model\AllowedCountries;
11+
use Magento\Framework\App\ObjectManager;
1012

1113
/**
1214
* @api
@@ -18,6 +20,22 @@ class QuoteValidator
1820
*/
1921
const MAXIMUM_AVAILABLE_NUMBER = 99999999;
2022

23+
/**
24+
* @var AllowedCountries
25+
*/
26+
private $allowedCountryReader;
27+
28+
/**
29+
* QuoteValidator constructor.
30+
*
31+
* @param AllowedCountries|null $allowedCountryReader
32+
*/
33+
public function __construct(AllowedCountries $allowedCountryReader = null)
34+
{
35+
$this->allowedCountryReader = $allowedCountryReader ?: ObjectManager::getInstance()
36+
->get(AllowedCountries::class);
37+
}
38+
2139
/**
2240
* Validate quote amount
2341
*
@@ -69,6 +87,19 @@ public function validateBeforeSubmit(QuoteEntity $quote)
6987
if (!$quote->getPayment()->getMethod()) {
7088
throw new \Magento\Framework\Exception\LocalizedException(__('Please select a valid payment method.'));
7189
}
90+
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+
72103
return $this;
73104
}
74105
}

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
namespace Magento\Quote\Test\Unit\Model;
77

88
use \Magento\Quote\Model\QuoteValidator;
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
use Magento\Directory\Model\AllowedCountries;
11+
use Magento\Quote\Model\Quote\Address;
12+
use Magento\Quote\Model\Quote\Paymen;
913

1014
/**
1115
* Class QuoteValidatorTest
@@ -175,4 +179,58 @@ public function testValidateBeforeSubmitThrowsExceptionIfPaymentMethodIsNotSelec
175179

176180
$this->quoteValidator->validateBeforeSubmit($this->quoteMock);
177181
}
182+
183+
/**
184+
* Test case when country id not present in allowed countries list.
185+
*
186+
* @expectedException \Magento\Framework\Exception\LocalizedException
187+
* @expectedExceptionMessage Some addresses cannot be used due to country-specific configurations.
188+
*/
189+
public function testValidateBeforeSubmitThrowsExceptionIfCountrySpecificConfigurations()
190+
{
191+
$objectManagerHelper = new ObjectManager($this);
192+
$allowedCountryReader = $this->getMockBuilder(AllowedCountries::class)
193+
->disableOriginalConstructor()
194+
->setMethods(['getAllowedCountries'])
195+
->getMock();
196+
$allowedCountryReader->method('getAllowedCountries')
197+
->willReturn(['EE' => 'EE']);
198+
199+
$addressMock = $this->getMockBuilder(Address::class)
200+
->disableOriginalConstructor()
201+
->setMethods(['getCountryId'])
202+
->getMock();
203+
$addressMock->method('getCountryId')
204+
->willReturn('EU');
205+
206+
$paymentMock = $this->getMockBuilder(Paymen::class)
207+
->setMethods(['getMethod'])
208+
->disableOriginalConstructor()
209+
->getMock();
210+
$paymentMock->method('getMethod')
211+
->willReturn(true);
212+
213+
$billingAddressMock = $this->getMockBuilder(Address::class)
214+
->disableOriginalConstructor()
215+
->setMethods(['validate'])
216+
->getMock();
217+
$billingAddressMock->method('validate')
218+
->willReturn(true);
219+
220+
$this->quoteMock->method('getShippingAddress')
221+
->willReturn($addressMock);
222+
$this->quoteMock->method('isVirtual')
223+
->willReturn(true);
224+
$this->quoteMock->method('getBillingAddress')
225+
->willReturn($billingAddressMock);
226+
$this->quoteMock->method('getPayment')
227+
->willReturn($paymentMock);
228+
229+
$quoteValidator = $objectManagerHelper->getObject(
230+
QuoteValidator::class,
231+
['allowedCountryReader' => $allowedCountryReader]
232+
);
233+
234+
$quoteValidator->validateBeforeSubmit($this->quoteMock);
235+
}
178236
}

app/code/Magento/Quote/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@ error123,error123
5555
error345,error345
5656
Carts,Carts
5757
"Manage carts","Manage carts"
58+
"Some addresses cannot be used due to country-specific configurations.","Some addresses cannot be used due to country-specific configurations."

dev/tests/integration/testsuite/Magento/Sales/Model/AdminOrder/CreateTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function testInitFromOrderAndCreateOrderFromQuoteWithAdditionalOptions()
9393
$rate->setCode('freeshipping_freeshipping');
9494

9595
$this->_model->getQuote()->getShippingAddress()->addShippingRate($rate);
96+
$this->_model->getQuote()->getShippingAddress()->setCountryId('EE');
9697
$this->_model->setShippingAsBilling(0);
9798
$this->_model->setPaymentData(['method' => 'checkmo']);
9899

0 commit comments

Comments
 (0)