Skip to content

Commit 7f04b01

Browse files
committed
Merge remote-tracking branch 'act4/ACP2E-3382' into PR_13_OCT_2024
2 parents fd682b1 + b510209 commit 7f04b01

File tree

4 files changed

+220
-10
lines changed

4 files changed

+220
-10
lines changed

app/code/Magento/CustomerImportExport/Model/Import/Address.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2012 Adobe
4+
* All Rights Reserved.
55
*/
66

77
namespace Magento\CustomerImportExport\Model\Import;
88

9-
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites as CountryWithWebsitesSource;
9+
use Magento\CustomerImportExport\Model\Import\CountryWithWebsites as CountryWithWebsitesSource;
1010
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
1111
use Magento\Framework\App\ObjectManager;
1212
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
@@ -365,7 +365,7 @@ public function getAttributeOptions(AbstractAttribute $attribute, array $indexAt
365365
if ($attribute->getAttributeCode() === 'country_id') {
366366
//If we want to get available options for country field then we have to use alternative source
367367
// to get actual data for each website.
368-
$options = $this->countryWithWebsites->getAllOptions();
368+
$options = $this->countryWithWebsites->getCountiesPerWebsite();
369369
//Available country options now will be sorted by websites.
370370
$code = $attribute->getAttributeCode();
371371
$websiteOptions = [Store::DEFAULT_STORE_ID => $standardOptions];
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\CustomerImportExport\Model\Import;
10+
11+
use Magento\Customer\Model\Config\Share as CustomerShareConfig;
12+
use Magento\Directory\Model\AllowedCountries;
13+
use Magento\Directory\Model\ResourceModel\Country\Collection as CountryCollection;
14+
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory as CountryCollectionFactory;
15+
use Magento\Store\Model\ScopeInterface;
16+
use Magento\Store\Model\StoreManagerInterface;
17+
18+
/**
19+
* Return allowed countries for specified website for customer import
20+
*/
21+
class CountryWithWebsites
22+
{
23+
/**
24+
* @var CountryCollectionFactory
25+
*/
26+
private $countriesFactory;
27+
28+
/**
29+
* @var AllowedCountries
30+
*/
31+
private $allowedCountriesReader;
32+
33+
/**
34+
* @var array
35+
*/
36+
private $allowedCountries;
37+
38+
/**
39+
* @var StoreManagerInterface
40+
*/
41+
private $storeManager;
42+
43+
/**
44+
* @var CustomerShareConfig
45+
*/
46+
private $shareConfig;
47+
48+
/**
49+
* @param CountryCollectionFactory $countriesFactory
50+
* @param AllowedCountries $allowedCountriesReader
51+
* @param StoreManagerInterface $storeManager
52+
* @param CustomerShareConfig $shareConfig
53+
*/
54+
public function __construct(
55+
CountryCollectionFactory $countriesFactory,
56+
AllowedCountries $allowedCountriesReader,
57+
StoreManagerInterface $storeManager,
58+
CustomerShareConfig $shareConfig
59+
) {
60+
$this->countriesFactory = $countriesFactory;
61+
$this->allowedCountriesReader = $allowedCountriesReader;
62+
$this->storeManager = $storeManager;
63+
$this->shareConfig = $shareConfig;
64+
}
65+
66+
/**
67+
* Get allowed countries for specified website
68+
*
69+
* @return array
70+
*/
71+
public function getCountiesPerWebsite(): array
72+
{
73+
if (!$this->allowedCountries) {
74+
$websiteIds = [];
75+
$allowedCountries = [];
76+
77+
foreach ($this->storeManager->getWebsites() as $website) {
78+
$countries = $this->allowedCountriesReader
79+
->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $website->getId());
80+
$allowedCountries[] = $countries;
81+
82+
foreach ($countries as $countryCode) {
83+
$websiteIds[$countryCode][] = $website->getId();
84+
}
85+
}
86+
87+
$this->allowedCountries = $this->createCountriesCollection()
88+
->addFieldToFilter('country_id', ['in' => $allowedCountries])
89+
->toOptionArray();
90+
91+
foreach ($this->allowedCountries as &$option) {
92+
if (isset($websiteIds[$option['value']])) {
93+
$option['website_ids'] = $websiteIds[$option['value']];
94+
}
95+
}
96+
}
97+
98+
return $this->allowedCountries;
99+
}
100+
101+
/**
102+
* Create Countries Collection with all countries
103+
*
104+
* @return CountryCollection
105+
*/
106+
private function createCountriesCollection()
107+
{
108+
return $this->countriesFactory->create();
109+
}
110+
}

app/code/Magento/CustomerImportExport/Test/Unit/Model/Import/AddressTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2012 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -12,7 +12,7 @@
1212
use Magento\Customer\Model\CustomerFactory;
1313
use Magento\Customer\Model\Indexer\Processor;
1414
use Magento\Customer\Model\ResourceModel\Address\Attribute as AddressAttribute;
15-
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites;
15+
use Magento\CustomerImportExport\Model\Import\CountryWithWebsites;
1616
use Magento\CustomerImportExport\Model\Import\Address;
1717
use Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\Storage;
1818
use Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\StorageFactory;
@@ -145,7 +145,7 @@ class AddressTest extends TestCase
145145
protected $errorAggregator;
146146

147147
/**
148-
* @var AddressAttribute\Source\CountryWithWebsites|MockObject
148+
* @var CountryWithWebsites|MockObject
149149
*/
150150
private $countryWithWebsites;
151151

@@ -168,8 +168,7 @@ protected function setUp(): void
168168
->disableOriginalConstructor()
169169
->getMock();
170170
$this->countryWithWebsites
171-
172-
->method('getAllOptions')
171+
->method('getCountiesPerWebsite')
173172
->willReturn([]);
174173
$this->_model = $this->_getModelMock();
175174
$this->errorAggregator = $this->createPartialMock(
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerImportExport\Test\Unit\Model\Import;
9+
10+
use PHPUnit\Framework\TestCase;
11+
use Magento\CustomerImportExport\Model\Import\CountryWithWebsites;
12+
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory as CountryCollectionFactory;
13+
use Magento\Directory\Model\AllowedCountries;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
use Magento\Customer\Model\Config\Share as CustomerShareConfig;
16+
use Magento\Store\Api\Data\WebsiteInterface;
17+
use Magento\Store\Model\ScopeInterface;
18+
use Magento\Directory\Model\ResourceModel\Country\Collection as CountryCollection;
19+
use PHPUnit\Framework\MockObject\MockObject;
20+
21+
class CountryWithWebsitesTest extends TestCase
22+
{
23+
/**
24+
* @var CountryCollectionFactory|MockObject
25+
*/
26+
private $countriesFactoryMock;
27+
28+
/**
29+
* @var AllowedCountries|MockObject
30+
*/
31+
private $allowedCountriesReaderMock;
32+
33+
/**
34+
* @var StoreManagerInterface|MockObject
35+
*/
36+
private $storeManagerMock;
37+
38+
/**
39+
* @var CustomerShareConfig|MockObject
40+
*/
41+
private $shareConfigMock;
42+
43+
/**
44+
* @var CountryWithWebsites
45+
*/
46+
private $countryWithWebsites;
47+
48+
/**
49+
* @inheritDoc
50+
*/
51+
protected function setUp(): void
52+
{
53+
$this->countriesFactoryMock = $this->createMock(CountryCollectionFactory::class);
54+
$this->allowedCountriesReaderMock = $this->createMock(AllowedCountries::class);
55+
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
56+
$this->shareConfigMock = $this->createMock(CustomerShareConfig::class);
57+
58+
$this->countryWithWebsites = new CountryWithWebsites(
59+
$this->countriesFactoryMock,
60+
$this->allowedCountriesReaderMock,
61+
$this->storeManagerMock,
62+
$this->shareConfigMock
63+
);
64+
}
65+
66+
/**
67+
* Tests method returns allowed countries for specified website
68+
*
69+
* @return void
70+
* @throws \PHPUnit\Framework\MockObject\Exception
71+
*/
72+
public function testGetCountiesPerWebsite()
73+
{
74+
$websiteMock = $this->createMock(WebsiteInterface::class);
75+
$websiteMock->method('getId')->willReturn(1);
76+
77+
$this->storeManagerMock->method('getWebsites')->willReturn([$websiteMock]);
78+
$this->allowedCountriesReaderMock->method('getAllowedCountries')
79+
->with(ScopeInterface::SCOPE_WEBSITE, 1)
80+
->willReturn(['US', 'CA']);
81+
82+
$countryCollectionMock = $this->createMock(CountryCollection::class);
83+
$countryCollectionMock->method('addFieldToFilter')
84+
->with('country_id', ['in' => [['US', 'CA']]])
85+
->willReturnSelf();
86+
$countryCollectionMock->method('toOptionArray')
87+
->willReturn([
88+
['value' => 'US', 'label' => 'United States'],
89+
['value' => 'CA', 'label' => 'Canada']
90+
]);
91+
92+
$this->countriesFactoryMock->method('create')->willReturn($countryCollectionMock);
93+
94+
$expectedResult = [
95+
['value' => 'US', 'label' => 'United States', 'website_ids' => [1]],
96+
['value' => 'CA', 'label' => 'Canada', 'website_ids' => [1]]
97+
];
98+
99+
$this->assertEquals($expectedResult, $this->countryWithWebsites->getCountiesPerWebsite());
100+
}
101+
}

0 commit comments

Comments
 (0)