Skip to content

Commit

Permalink
Merge remote-tracking branch 'magento-l3/MC-42623' into L3_PR_21-09-30
Browse files Browse the repository at this point in the history
  • Loading branch information
veloraven committed Oct 12, 2021
2 parents 9d8a761 + 2e35a51 commit 3420116
Show file tree
Hide file tree
Showing 14 changed files with 429 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

namespace Magento\Customer\Model\Address;

use Magento\Customer\Model\Config\Share;
use Magento\Directory\Model\AllowedCountries;
use Magento\Framework\App\ObjectManager;

/**
* Provides customer address data.
*/
Expand All @@ -24,13 +28,31 @@ class CustomerAddressDataProvider
*/
private $customerAddressDataFormatter;

/**
* @var \Magento\Customer\Model\Config\Share
*/
private $shareConfig;

/**
* @var AllowedCountries
*/
private $allowedCountryReader;

/**
* @param CustomerAddressDataFormatter $customerAddressDataFormatter
* @param Share|null $share
* @param AllowedCountries|null $allowedCountryReader
*/
public function __construct(
CustomerAddressDataFormatter $customerAddressDataFormatter
CustomerAddressDataFormatter $customerAddressDataFormatter,
?Share $share = null,
?AllowedCountries $allowedCountryReader = null
) {
$this->customerAddressDataFormatter = $customerAddressDataFormatter;
$this->shareConfig = $share ?: ObjectManager::getInstance()
->get(Share::class);
$this->allowedCountryReader = $allowedCountryReader ?: ObjectManager::getInstance()
->get(AllowedCountries::class);
}

/**
Expand All @@ -52,8 +74,14 @@ public function getAddressDataByCustomer(
return [];
}

$allowedCountries = $this->allowedCountryReader->getAllowedCountries();
$customerAddresses = [];
foreach ($customerOriginAddresses as $address) {
// Checks if a country id present in the allowed countries list.
if ($this->shareConfig->isGlobalScope() && !in_array($address->getCountryId(), $allowedCountries)) {
continue;
}

$customerAddresses[$address->getId()] = $this->customerAddressDataFormatter->prepareAddress($address);
}

Expand Down
65 changes: 0 additions & 65 deletions app/code/Magento/Customer/Model/Plugin/AllowedCountries.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
use Magento\Eav\Model\Entity\Attribute\Source\Table;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory as OptionCollectionFactory;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory as AttrubuteOptionFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Request\Http;
use Magento\Customer\Api\CustomerRepositoryInterface;

/**
* Return allowed countries for specified website
Expand Down Expand Up @@ -52,26 +55,44 @@ class CountryWithWebsites extends Table
*/
private $shareConfig;

/**
* @var Http
*/
private $request;

/**
* @var CustomerRepositoryInterface
*/
private $customerRepository;

/**
* @param OptionCollectionFactory $attrOptionCollectionFactory
* @param AttrubuteOptionFactory $attrOptionFactory
* @param CountryCollectionFactory $countriesFactory
* @param AllowedCountries $allowedCountriesReader
* @param StoreManagerInterface $storeManager
* @param Share $shareConfig
* @param Http|null $request
* @param CustomerRepositoryInterface|null $customerRepository
*/
public function __construct(
OptionCollectionFactory $attrOptionCollectionFactory,
AttrubuteOptionFactory $attrOptionFactory,
CountryCollectionFactory $countriesFactory,
AllowedCountries $allowedCountriesReader,
StoreManagerInterface $storeManager,
CustomerShareConfig $shareConfig
CustomerShareConfig $shareConfig,
?Http $request = null,
?CustomerRepositoryInterface $customerRepository = null
) {
$this->countriesFactory = $countriesFactory;
$this->allowedCountriesReader = $allowedCountriesReader;
$this->storeManager = $storeManager;
$this->shareConfig = $shareConfig;
$this->request = $request
?? ObjectManager::getInstance()->get(Http::class);
$this->customerRepository = $customerRepository
?? ObjectManager::getInstance()->get(CustomerRepositoryInterface::class);
parent::__construct($attrOptionCollectionFactory, $attrOptionFactory);
}

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

$allowedCountries = array_unique(array_merge([], ...$allowedCountries));
} else {
$allowedCountries = $this->allowedCountriesReader->getAllowedCountries();
// Address can be added only for the allowed country list.
$storeId = null;
$customerId = $this->request->getParam('parent_id') ?? null;
if ($customerId) {
$customer = $this->customerRepository->getById($customerId);
$storeId = $customer->getStoreId();
}

$allowedCountries = $this->allowedCountriesReader->getAllowedCountries(
ScopeInterface::SCOPE_WEBSITE,
$storeId
);
}

$this->options = $this->createCountriesCollection()
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions app/code/Magento/Customer/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,6 @@
<type name="Magento\Customer\Api\CustomerRepositoryInterface">
<plugin name="transactionWrapper" type="Magento\Customer\Model\Plugin\CustomerRepository\TransactionWrapper" sortOrder="-1"/>
</type>
<type name="Magento\Directory\Model\AllowedCountries">
<plugin name="customerAllowedCountries" type="Magento\Customer\Model\Plugin\AllowedCountries"/>
</type>
<type name="Magento\Framework\App\ActionInterface">
<plugin name="customerNotification" type="Magento\Customer\Model\Plugin\CustomerNotification"/>
</type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\Directory\Test\Unit\Model;

use Magento\Customer\Model\Config\Share;
use Magento\Directory\Model\AllowedCountries;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Api\Data\WebsiteInterface;
Expand All @@ -32,11 +33,19 @@ class AllowedCountriesTest extends TestCase
*/
private $allowedCountriesReader;

/**
* @var Share|MockObject
*/
private $shareConfigMock;

/**
* Test setUp
*/
protected function setUp(): void
{
$this->shareConfigMock = $this->getMockBuilder(Share::class)
->disableOriginalConstructor()
->getMock();
$this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
$this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class);

Expand Down Expand Up @@ -100,4 +109,35 @@ public function testGetAllowedCountriesDefaultScope()
$this->allowedCountriesReader->getAllowedCountries(ScopeInterface::SCOPE_STORE, 0)
);
}

/**
* Test for getAllowedCountries with global scope
*/
public function testGetAllowedCountriesWithGlobalScope()
{
$expectedFilter = 1;
$expectedScope = ScopeInterface::SCOPE_WEBSITES;

$this->shareConfigMock->expects($this->once())
->method('isGlobalScope')
->willReturn(true);
if ($this->shareConfigMock->isGlobalScope()) {

$websiteMock = $this->getMockForAbstractClass(WebsiteInterface::class);
$websiteMock->expects($this->once())
->method('getId')
->willReturn($expectedFilter);

$this->scopeConfigMock->expects($this->once())
->method('getValue')
->with(AllowedCountries::ALLOWED_COUNTRIES_PATH, 'website', $websiteMock->getId())
->willReturn('AM');

//$scopeCode should have single valued array only eg:[1]
$this->assertEquals(
['AM' => 'AM'],
$this->allowedCountriesReader->getAllowedCountries($expectedScope, [$expectedFilter])
);
}
}
}
Loading

0 comments on commit 3420116

Please sign in to comment.