Skip to content

Commit e638c0b

Browse files
committed
MAGETWO-56925: [Backport] - “No Payment method available” when customer tries to ship his items to billing restricted country for 2.0.x
1 parent 765855c commit e638c0b

File tree

4 files changed

+149
-64
lines changed

4 files changed

+149
-64
lines changed

app/code/Magento/Payment/Model/Checks/CanUseForCountry/CountryProvider.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,14 @@ public function __construct(DirectoryHelper $directoryHelper)
3131
*/
3232
public function getCountry(Quote $quote)
3333
{
34-
$address = $quote->isVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();
35-
return $address
36-
? $address->getCountry()
37-
: $this->directoryHelper->getDefaultCountry();
34+
/** @var string $country */
35+
$country = $quote->getBillingAddress()->getCountry() ? :
36+
$quote->getShippingAddress()->getCountry();
37+
38+
if (!$country) {
39+
$country = $this->directoryHelper->getDefaultCountry();
40+
}
41+
42+
return $country;
3843
}
3944
}

app/code/Magento/Payment/Test/Unit/Model/Checks/CanUseForCountry/CountryProviderTest.php

Lines changed: 120 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,143 @@
55
*/
66
namespace Magento\Payment\Test\Unit\Model\Checks\CanUseForCountry;
77

8+
use Magento\Directory\Helper\Data;
9+
use Magento\Payment\Model\Checks\CanUseForCountry\CountryProvider;
10+
use Magento\Quote\Model\Quote;
11+
use Magento\Quote\Model\Quote\Address;
12+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
13+
14+
/**
15+
* CountryProviderTest contains tests for CountryProvider class
16+
*/
817
class CountryProviderTest extends \PHPUnit_Framework_TestCase
918
{
1019
/**
11-
* @var \Magento\Payment\Model\Checks\CanUseForCountry\CountryProvider
20+
* @var CountryProvider
21+
*/
22+
private $countryProvider;
23+
24+
/**
25+
* @var Data|MockObject
1226
*/
13-
protected $model;
27+
private $directory;
1428

1529
/**
16-
* @var \PHPUnit_Framework_MockObject_MockObject
30+
* @var Quote|MockObject
1731
*/
18-
protected $directoryMock;
32+
private $quote;
1933

20-
public function setUp()
34+
protected function setUp()
2135
{
22-
$this->directoryMock = $this->getMock('Magento\Directory\Helper\Data', [], [], '', false, false);
23-
$this->model = new \Magento\Payment\Model\Checks\CanUseForCountry\CountryProvider($this->directoryMock);
36+
$this->directory = $this->getMockBuilder(Data::class)
37+
->disableOriginalConstructor()
38+
->setMethods(['getDefaultCountry'])
39+
->getMock();
40+
41+
$this->quote = $this->getMockBuilder(Quote::class)
42+
->disableOriginalConstructor()
43+
->setMethods(['getBillingAddress', 'getShippingAddress'])
44+
->getMock();
45+
46+
$this->countryProvider = new CountryProvider($this->directory);
2447
}
2548

26-
public function testGetCountryForNonVirtualQuote()
49+
/**
50+
* @covers \Magento\Payment\Model\Checks\CanUseForCountry\CountryProvider::getCountry
51+
*/
52+
public function testGetCountry()
2753
{
28-
$quoteMock = $this->getMock('Magento\Quote\Model\Quote', [], [], '', false, false);
29-
$quoteMock->expects($this->once())->method('isVirtual')->willReturn(false);
30-
$addressMock = $this->getMock('Magento\Quote\Model\Quote\Address', [], [], '', false, false);
31-
$addressMock->expects($this->once())->method('getCountry')->will($this->returnValue(1));
32-
$quoteMock->expects($this->once())->method('getShippingAddress')->will($this->returnValue($addressMock));
33-
$this->assertEquals(1, $this->model->getCountry($quoteMock));
54+
$address = $this->getMockBuilder(Address::class)
55+
->disableOriginalConstructor()
56+
->setMethods(['getCountry'])
57+
->getMock();
58+
59+
$this->quote->expects(static::once())
60+
->method('getBillingAddress')
61+
->willReturn($address);
62+
63+
$this->quote->expects(static::never())
64+
->method('getShippingAddress');
65+
66+
$address->expects(static::once())
67+
->method('getCountry')
68+
->willReturn('UK');
69+
$this->directory->expects(static::never())
70+
->method('getDefaultCountry');
71+
72+
static::assertEquals('UK', $this->countryProvider->getCountry($this->quote));
3473
}
3574

36-
public function testGetCountryForVirtualQuoteWhenBillingAddressNotExist()
75+
/**
76+
* @covers \Magento\Payment\Model\Checks\CanUseForCountry\CountryProvider::getCountry
77+
*/
78+
public function testGetCountryForBillingAddressWithoutCountry()
3779
{
38-
$quoteMock = $this->getMock('Magento\Quote\Model\Quote', [], [], '', false, false);
39-
$quoteMock->expects($this->once())->method('isVirtual')->willReturn(true);
40-
$addressMock = $this->getMock('Magento\Quote\Model\Quote\Address', [], [], '', false, false);
41-
$addressMock->expects($this->never())->method('getCountry');
42-
$quoteMock->expects($this->never())->method('getShippingAddress');
43-
$quoteMock->expects($this->once())->method('getBillingAddress')->willReturn(null);
44-
$this->directoryMock->expects($this->once())->method('getDefaultCountry')->willReturn(10);
45-
$this->assertEquals(10, $this->model->getCountry($quoteMock));
80+
$billingAddress = $this->getMockBuilder(Address::class)
81+
->disableOriginalConstructor()
82+
->setMethods(['getCountry'])
83+
->getMock();
84+
85+
$shippingAddress = $this->getMockBuilder(Address::class)
86+
->disableOriginalConstructor()
87+
->setMethods(['getCountry'])
88+
->getMock();
89+
90+
$this->quote->expects(static::once())
91+
->method('getShippingAddress')
92+
->willReturn($shippingAddress);
93+
$this->quote->expects(static::once())
94+
->method('getBillingAddress')
95+
->willReturn($billingAddress);
96+
97+
$billingAddress->expects(static::once())
98+
->method('getCountry')
99+
->willReturn(null);
100+
101+
$shippingAddress->expects(static::once())
102+
->method('getCountry')
103+
->willReturn(null);
104+
105+
$this->directory->expects(static::once())
106+
->method('getDefaultCountry')
107+
->willReturn('US');
108+
static::assertEquals('US', $this->countryProvider->getCountry($this->quote));
46109
}
47110

48-
public function testGetCountryForVirtualQuoteWhenBillingAddressExist()
111+
/**
112+
* @covers \Magento\Payment\Model\Checks\CanUseForCountry\CountryProvider::getCountry
113+
*/
114+
public function testGetCountryShippingAddress()
49115
{
50-
$quoteMock = $this->getMock('Magento\Quote\Model\Quote', [], [], '', false, false);
51-
$quoteMock->expects($this->once())->method('isVirtual')->willReturn(true);
52-
$addressMock = $this->getMock('Magento\Quote\Model\Quote\Address', [], [], '', false, false);
53-
$addressMock->expects($this->once())->method('getCountry')->willReturn(10);
54-
$quoteMock->expects($this->never())->method('getShippingAddress');
55-
$quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($addressMock);
56-
$this->directoryMock->expects($this->never())->method('getDefaultCountry');
57-
$this->assertEquals(10, $this->model->getCountry($quoteMock));
116+
$shippingAddress = $this->getMockBuilder(Address::class)
117+
->disableOriginalConstructor()
118+
->setMethods(['getCountry'])
119+
->getMock();
120+
121+
$billingAddress = $this->getMockBuilder(Address::class)
122+
->disableOriginalConstructor()
123+
->setMethods(['getCountry'])
124+
->getMock();
125+
126+
$this->quote->expects(static::once())
127+
->method('getBillingAddress')
128+
->willReturn($billingAddress);
129+
130+
$this->quote->expects(static::once())
131+
->method('getShippingAddress')
132+
->willReturn($shippingAddress);
133+
134+
$shippingAddress->expects(static::once())
135+
->method('getCountry')
136+
->willReturn('CA');
137+
138+
$shippingAddress->expects(static::once())
139+
->method('getCountry')
140+
->willReturn(null);
141+
142+
$this->directory->expects(static::never())
143+
->method('getDefaultCountry');
144+
145+
static::assertEquals('CA', $this->countryProvider->getCountry($this->quote));
58146
}
59147
}

app/design/frontend/Magento/blank/Magento_Checkout/web/css/source/module/checkout/_payments.less

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
margin: 0 0 @indent__s;
8888
}
8989

90-
.payment-method-billing-address {
90+
.checkout-billing-address {
9191
margin: 0 0 @indent__base;
9292

9393
.primary {
@@ -103,15 +103,11 @@
103103
.billing-address-details {
104104
.lib-css(line-height, @checkout-billing-address-details__line-height);
105105
.lib-css(padding, @checkout-billing-address-details__padding);
106-
107-
.action-edit-address {
108-
&:extend(.abs-action-button-as-link all);
109-
}
110106
}
111107
}
112108

113109
.payment-method-note {
114-
& + .payment-method-billing-address {
110+
& + .checkout-billing-address {
115111
margin-top: @indent__base;
116112
}
117113
}
@@ -155,7 +151,7 @@
155151
.lib-css(padding, 0 @checkout-payment-method-title-mobile__padding @indent__base);
156152
}
157153

158-
.payment-method-billing-address {
154+
.checkout-billing-address {
159155
.action-cancel {
160156
margin-top: @indent__s;
161157
}
@@ -169,14 +165,13 @@
169165

170166
.media-width(@extremum, @break) when (@extremum = 'min') and (@break = @screen__m) {
171167
.checkout-payment-method {
172-
.payment-methods {
173-
.actions-toolbar {
174-
.primary {
175-
float: right;
176-
margin: 0;
177-
}
168+
.actions-toolbar {
169+
.primary {
170+
float: right;
171+
margin: 0;
178172
}
179173
}
174+
180175
.fieldset {
181176
> .field-select-billing {
182177
> .control {
@@ -185,6 +180,7 @@
185180
}
186181
}
187182
}
183+
188184
.payment-method-content {
189185
.fieldset {
190186
> .field {
@@ -203,7 +199,8 @@
203199
}
204200
}
205201
}
206-
.payment-method-billing-address {
202+
203+
.checkout-billing-address {
207204
.action-update {
208205
float: right;
209206
}

app/design/frontend/Magento/luma/Magento_Checkout/web/css/source/module/checkout/_payments.less

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
margin: 0 0 @indent__s;
8888
}
8989

90-
.payment-method-billing-address {
90+
.checkout-billing-address {
9191
margin: 0 0 @indent__base;
9292

9393
.primary {
@@ -103,15 +103,11 @@
103103
.billing-address-details {
104104
.lib-css(line-height, @checkout-billing-address-details__line-height);
105105
.lib-css(padding, @checkout-billing-address-details__padding);
106-
107-
.action-edit-address {
108-
&:extend(.abs-action-button-as-link all);
109-
}
110106
}
111107
}
112108

113109
.payment-method-note {
114-
& + .payment-method-billing-address {
110+
& + .checkout-billing-address {
115111
margin-top: @indent__base;
116112
}
117113
}
@@ -155,7 +151,7 @@
155151
.lib-css(padding, 0 @checkout-payment-method-title-mobile__padding @indent__base);
156152
}
157153

158-
.payment-method-billing-address {
154+
.checkout-billing-address {
159155
.action-cancel {
160156
margin-top: @indent__s;
161157
}
@@ -169,12 +165,10 @@
169165

170166
.media-width(@extremum, @break) when (@extremum = 'min') and (@break = @screen__m) {
171167
.checkout-payment-method {
172-
.payment-methods {
173-
.actions-toolbar {
174-
.primary {
175-
float: right;
176-
margin: 0;
177-
}
168+
.actions-toolbar {
169+
.primary {
170+
float: right;
171+
margin: 0;
178172
}
179173
}
180174
.fieldset {
@@ -186,7 +180,8 @@
186180
}
187181
}
188182
}
189-
.payment-method-billing-address {
183+
184+
.checkout-billing-address {
190185
.action-update {
191186
float: right;
192187
}

0 commit comments

Comments
 (0)