Skip to content

Commit 597354e

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-90838' into 2.3-develop-pr25
2 parents e47b0d7 + 99d9b06 commit 597354e

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

app/code/Magento/Customer/Model/Address/Validator/Country.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use Magento\Customer\Model\Address\AbstractAddress;
99
use Magento\Customer\Model\Address\ValidatorInterface;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Store\Model\ScopeInterface;
1012

1113
/**
1214
* Address country and region validator.
@@ -18,13 +20,31 @@ class Country implements ValidatorInterface
1820
*/
1921
private $directoryData;
2022

23+
/**
24+
* @var \Magento\Directory\Model\AllowedCountries
25+
*/
26+
private $allowedCountriesReader;
27+
28+
/**
29+
* @var \Magento\Customer\Model\Config\Share
30+
*/
31+
private $shareConfig;
32+
2133
/**
2234
* @param \Magento\Directory\Helper\Data $directoryData
35+
* @param \Magento\Directory\Model\AllowedCountries|null $allowedCountriesReader
36+
* @param \Magento\Customer\Model\Config\Share|null $shareConfig
2337
*/
2438
public function __construct(
25-
\Magento\Directory\Helper\Data $directoryData
39+
\Magento\Directory\Helper\Data $directoryData,
40+
\Magento\Directory\Model\AllowedCountries $allowedCountriesReader = null,
41+
\Magento\Customer\Model\Config\Share $shareConfig = null
2642
) {
2743
$this->directoryData = $directoryData;
44+
$this->allowedCountriesReader = $allowedCountriesReader
45+
?: ObjectManager::getInstance()->get(\Magento\Directory\Model\AllowedCountries::class);
46+
$this->shareConfig = $shareConfig
47+
?: ObjectManager::getInstance()->get(\Magento\Customer\Model\Config\Share::class);
2848
}
2949

3050
/**
@@ -52,7 +72,7 @@ private function validateCountry(AbstractAddress $address)
5272
$errors = [];
5373
if (!\Zend_Validate::is($countryId, 'NotEmpty')) {
5474
$errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'countryId']);
55-
} elseif (!in_array($countryId, $this->directoryData->getCountryCollection()->getAllIds(), true)) {
75+
} elseif (!in_array($countryId, $this->getWebsiteAllowedCountries($address), true)) {
5676
//Checking if such country exists.
5777
$errors[] = __(
5878
'Invalid value of "%value" provided for the %fieldName field.',
@@ -97,4 +117,21 @@ private function validateRegion(AbstractAddress $address)
97117

98118
return $errors;
99119
}
120+
121+
/**
122+
* Return allowed counties per website.
123+
*
124+
* @param AbstractAddress $address
125+
* @return array
126+
*/
127+
private function getWebsiteAllowedCountries(AbstractAddress $address): array
128+
{
129+
$websiteId = null;
130+
131+
if (!$this->shareConfig->isGlobalScope()) {
132+
$websiteId = $address->getCustomer() ? $address->getCustomer()->getWebsiteId() : null;
133+
}
134+
135+
return $this->allowedCountriesReader->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $websiteId);
136+
}
100137
}

app/code/Magento/Customer/Test/Unit/Model/Address/Validator/CountryTest.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Customer\Test\Unit\Model\Address\Validator;
88

9+
use Magento\Store\Model\ScopeInterface;
10+
911
/**
1012
* Magento\Customer\Model\Address\Validator\Country tests.
1113
*/
@@ -20,14 +22,34 @@ class CountryTest extends \PHPUnit\Framework\TestCase
2022
/** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
2123
private $objectManager;
2224

25+
/**
26+
* @var \Magento\Directory\Model\AllowedCountries|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $allowedCountriesReaderMock;
29+
30+
/**
31+
* @var \Magento\Customer\Model\Config\Share|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $shareConfigMock;
34+
2335
protected function setUp()
2436
{
2537
$this->directoryDataMock = $this->createMock(\Magento\Directory\Helper\Data::class);
2638
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
39+
$this->allowedCountriesReaderMock = $this->createPartialMock(
40+
\Magento\Directory\Model\AllowedCountries::class,
41+
['getAllowedCountries']
42+
);
43+
$this->shareConfigMock = $this->createPartialMock(
44+
\Magento\Customer\Model\Config\Share::class,
45+
['isGlobalScope']
46+
);
2747
$this->model = $this->objectManager->getObject(
2848
\Magento\Customer\Model\Address\Validator\Country::class,
2949
[
3050
'directoryData' => $this->directoryDataMock,
51+
'allowedCountriesReader' => $this->allowedCountriesReaderMock,
52+
'shareConfig' => $this->shareConfigMock,
3153
]
3254
);
3355
}
@@ -59,16 +81,11 @@ public function testValidate(array $data, array $countryIds, array $allowedRegio
5981
->method('isRegionRequired')
6082
->willReturn($data['regionRequired']);
6183

62-
$countryCollectionMock = $this->getMockBuilder(\Magento\Directory\Model\ResourceModel\Country\Collection::class)
63-
->disableOriginalConstructor()
64-
->setMethods(['getAllIds'])
65-
->getMock();
66-
67-
$this->directoryDataMock->expects($this->any())
68-
->method('getCountryCollection')
69-
->willReturn($countryCollectionMock);
70-
71-
$countryCollectionMock->expects($this->any())->method('getAllIds')->willReturn($countryIds);
84+
$this->shareConfigMock->method('isGlobalScope')->willReturn(false);
85+
$this->allowedCountriesReaderMock
86+
->method('getAllowedCountries')
87+
->with(ScopeInterface::SCOPE_WEBSITE, null)
88+
->willReturn($countryIds);
7289

7390
$addressMock->method('getCountryId')->willReturn($data['country_id']);
7491

0 commit comments

Comments
 (0)