Skip to content

Commit 3420116

Browse files
committed
Merge remote-tracking branch 'magento-l3/MC-42623' into L3_PR_21-09-30
2 parents 9d8a761 + 2e35a51 commit 3420116

File tree

14 files changed

+429
-139
lines changed

14 files changed

+429
-139
lines changed

app/code/Magento/Customer/Model/Address/CustomerAddressDataProvider.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
namespace Magento\Customer\Model\Address;
99

10+
use Magento\Customer\Model\Config\Share;
11+
use Magento\Directory\Model\AllowedCountries;
12+
use Magento\Framework\App\ObjectManager;
13+
1014
/**
1115
* Provides customer address data.
1216
*/
@@ -24,13 +28,31 @@ class CustomerAddressDataProvider
2428
*/
2529
private $customerAddressDataFormatter;
2630

31+
/**
32+
* @var \Magento\Customer\Model\Config\Share
33+
*/
34+
private $shareConfig;
35+
36+
/**
37+
* @var AllowedCountries
38+
*/
39+
private $allowedCountryReader;
40+
2741
/**
2842
* @param CustomerAddressDataFormatter $customerAddressDataFormatter
43+
* @param Share|null $share
44+
* @param AllowedCountries|null $allowedCountryReader
2945
*/
3046
public function __construct(
31-
CustomerAddressDataFormatter $customerAddressDataFormatter
47+
CustomerAddressDataFormatter $customerAddressDataFormatter,
48+
?Share $share = null,
49+
?AllowedCountries $allowedCountryReader = null
3250
) {
3351
$this->customerAddressDataFormatter = $customerAddressDataFormatter;
52+
$this->shareConfig = $share ?: ObjectManager::getInstance()
53+
->get(Share::class);
54+
$this->allowedCountryReader = $allowedCountryReader ?: ObjectManager::getInstance()
55+
->get(AllowedCountries::class);
3456
}
3557

3658
/**
@@ -52,8 +74,14 @@ public function getAddressDataByCustomer(
5274
return [];
5375
}
5476

77+
$allowedCountries = $this->allowedCountryReader->getAllowedCountries();
5578
$customerAddresses = [];
5679
foreach ($customerOriginAddresses as $address) {
80+
// Checks if a country id present in the allowed countries list.
81+
if ($this->shareConfig->isGlobalScope() && !in_array($address->getCountryId(), $allowedCountries)) {
82+
continue;
83+
}
84+
5785
$customerAddresses[$address->getId()] = $this->customerAddressDataFormatter->prepareAddress($address);
5886
}
5987

app/code/Magento/Customer/Model/Plugin/AllowedCountries.php

Lines changed: 0 additions & 65 deletions
This file was deleted.

app/code/Magento/Customer/Model/ResourceModel/Address/Attribute/Source/CountryWithWebsites.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
use Magento\Eav\Model\Entity\Attribute\Source\Table;
2020
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory as OptionCollectionFactory;
2121
use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory as AttrubuteOptionFactory;
22+
use Magento\Framework\App\ObjectManager;
2223
use Magento\Store\Model\ScopeInterface;
2324
use Magento\Store\Model\StoreManagerInterface;
25+
use Magento\Framework\App\Request\Http;
26+
use Magento\Customer\Api\CustomerRepositoryInterface;
2427

2528
/**
2629
* Return allowed countries for specified website
@@ -52,26 +55,44 @@ class CountryWithWebsites extends Table
5255
*/
5356
private $shareConfig;
5457

58+
/**
59+
* @var Http
60+
*/
61+
private $request;
62+
63+
/**
64+
* @var CustomerRepositoryInterface
65+
*/
66+
private $customerRepository;
67+
5568
/**
5669
* @param OptionCollectionFactory $attrOptionCollectionFactory
5770
* @param AttrubuteOptionFactory $attrOptionFactory
5871
* @param CountryCollectionFactory $countriesFactory
5972
* @param AllowedCountries $allowedCountriesReader
6073
* @param StoreManagerInterface $storeManager
6174
* @param Share $shareConfig
75+
* @param Http|null $request
76+
* @param CustomerRepositoryInterface|null $customerRepository
6277
*/
6378
public function __construct(
6479
OptionCollectionFactory $attrOptionCollectionFactory,
6580
AttrubuteOptionFactory $attrOptionFactory,
6681
CountryCollectionFactory $countriesFactory,
6782
AllowedCountries $allowedCountriesReader,
6883
StoreManagerInterface $storeManager,
69-
CustomerShareConfig $shareConfig
84+
CustomerShareConfig $shareConfig,
85+
?Http $request = null,
86+
?CustomerRepositoryInterface $customerRepository = null
7087
) {
7188
$this->countriesFactory = $countriesFactory;
7289
$this->allowedCountriesReader = $allowedCountriesReader;
7390
$this->storeManager = $storeManager;
7491
$this->shareConfig = $shareConfig;
92+
$this->request = $request
93+
?? ObjectManager::getInstance()->get(Http::class);
94+
$this->customerRepository = $customerRepository
95+
?? ObjectManager::getInstance()->get(CustomerRepositoryInterface::class);
7596
parent::__construct($attrOptionCollectionFactory, $attrOptionFactory);
7697
}
7798

@@ -98,7 +119,18 @@ public function getAllOptions($withEmpty = true, $defaultValues = false)
98119

99120
$allowedCountries = array_unique(array_merge([], ...$allowedCountries));
100121
} else {
101-
$allowedCountries = $this->allowedCountriesReader->getAllowedCountries();
122+
// Address can be added only for the allowed country list.
123+
$storeId = null;
124+
$customerId = $this->request->getParam('parent_id') ?? null;
125+
if ($customerId) {
126+
$customer = $this->customerRepository->getById($customerId);
127+
$storeId = $customer->getStoreId();
128+
}
129+
130+
$allowedCountries = $this->allowedCountriesReader->getAllowedCountries(
131+
ScopeInterface::SCOPE_WEBSITE,
132+
$storeId
133+
);
102134
}
103135

104136
$this->options = $this->createCountriesCollection()

app/code/Magento/Customer/Test/Unit/Model/Plugin/AllowedCountriesTest.php

Lines changed: 0 additions & 67 deletions
This file was deleted.

app/code/Magento/Customer/etc/di.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,6 @@
366366
<type name="Magento\Customer\Api\CustomerRepositoryInterface">
367367
<plugin name="transactionWrapper" type="Magento\Customer\Model\Plugin\CustomerRepository\TransactionWrapper" sortOrder="-1"/>
368368
</type>
369-
<type name="Magento\Directory\Model\AllowedCountries">
370-
<plugin name="customerAllowedCountries" type="Magento\Customer\Model\Plugin\AllowedCountries"/>
371-
</type>
372369
<type name="Magento\Framework\App\ActionInterface">
373370
<plugin name="customerNotification" type="Magento\Customer\Model\Plugin\CustomerNotification"/>
374371
</type>

app/code/Magento/Directory/Test/Unit/Model/AllowedCountriesTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Directory\Test\Unit\Model;
99

10+
use Magento\Customer\Model\Config\Share;
1011
use Magento\Directory\Model\AllowedCountries;
1112
use Magento\Framework\App\Config\ScopeConfigInterface;
1213
use Magento\Store\Api\Data\WebsiteInterface;
@@ -32,11 +33,19 @@ class AllowedCountriesTest extends TestCase
3233
*/
3334
private $allowedCountriesReader;
3435

36+
/**
37+
* @var Share|MockObject
38+
*/
39+
private $shareConfigMock;
40+
3541
/**
3642
* Test setUp
3743
*/
3844
protected function setUp(): void
3945
{
46+
$this->shareConfigMock = $this->getMockBuilder(Share::class)
47+
->disableOriginalConstructor()
48+
->getMock();
4049
$this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
4150
$this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class);
4251

@@ -100,4 +109,35 @@ public function testGetAllowedCountriesDefaultScope()
100109
$this->allowedCountriesReader->getAllowedCountries(ScopeInterface::SCOPE_STORE, 0)
101110
);
102111
}
112+
113+
/**
114+
* Test for getAllowedCountries with global scope
115+
*/
116+
public function testGetAllowedCountriesWithGlobalScope()
117+
{
118+
$expectedFilter = 1;
119+
$expectedScope = ScopeInterface::SCOPE_WEBSITES;
120+
121+
$this->shareConfigMock->expects($this->once())
122+
->method('isGlobalScope')
123+
->willReturn(true);
124+
if ($this->shareConfigMock->isGlobalScope()) {
125+
126+
$websiteMock = $this->getMockForAbstractClass(WebsiteInterface::class);
127+
$websiteMock->expects($this->once())
128+
->method('getId')
129+
->willReturn($expectedFilter);
130+
131+
$this->scopeConfigMock->expects($this->once())
132+
->method('getValue')
133+
->with(AllowedCountries::ALLOWED_COUNTRIES_PATH, 'website', $websiteMock->getId())
134+
->willReturn('AM');
135+
136+
//$scopeCode should have single valued array only eg:[1]
137+
$this->assertEquals(
138+
['AM' => 'AM'],
139+
$this->allowedCountriesReader->getAllowedCountries($expectedScope, [$expectedFilter])
140+
);
141+
}
142+
}
103143
}

0 commit comments

Comments
 (0)