diff --git a/app/code/Magento/Customer/Model/Plugin/AllowedCountries.php b/app/code/Magento/Customer/Model/Plugin/AllowedCountries.php index b49f63d94f806fb68620874a0df5cfc63ac53b3a..157643bdfe5cc070b24b6ed4fe7ce19c6458ba37 100644 --- a/app/code/Magento/Customer/Model/Plugin/AllowedCountries.php +++ b/app/code/Magento/Customer/Model/Plugin/AllowedCountries.php @@ -44,6 +44,8 @@ class AllowedCountries * @param \Magento\Directory\Model\AllowedCountries $subject * @param string | null $filter * @param string $scope + * @return array + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeGetAllowedCountries( \Magento\Directory\Model\AllowedCountries $subject, diff --git a/app/code/Magento/Customer/Model/ResourceModel/Address/Attribute/Source/CountryWithWebsites.php b/app/code/Magento/Customer/Model/ResourceModel/Address/Attribute/Source/CountryWithWebsites.php index 50ff13e2b245e0efc3a1f66370dabf6f0d8e8283..16ceee8a3f28dd91145915cefefaab842ab50c6e 100644 --- a/app/code/Magento/Customer/Model/ResourceModel/Address/Attribute/Source/CountryWithWebsites.php +++ b/app/code/Magento/Customer/Model/ResourceModel/Address/Attribute/Source/CountryWithWebsites.php @@ -43,11 +43,13 @@ class CountryWithWebsites extends \Magento\Eav\Model\Entity\Attribute\Source\Tab private $shareConfig; /** + * CountryWithWebsites constructor. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory * @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countriesFactory * @param AllowedCountries $allowedCountriesReader * @param \Magento\Store\Model\StoreManagerInterface $storeManager + * @param Share $shareConfig */ public function __construct( \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory, @@ -89,7 +91,6 @@ class CountryWithWebsites extends \Magento\Eav\Model\Entity\Attribute\Source\Tab $allowedCountries = $this->allowedCountriesReader->getAllowedCountries(); } - $this->options = $this->createCountriesCollection() ->addFieldToFilter('country_id', ['in' => $allowedCountries]) ->toOptionArray(); diff --git a/app/code/Magento/Customer/Setup/UpgradeData.php b/app/code/Magento/Customer/Setup/UpgradeData.php index f503443c479d651b69202bb9c652cb7a8a0c78d7..03bb6f72f2dc7f8edc9cbe840cebd1ba054d00c9 100644 --- a/app/code/Magento/Customer/Setup/UpgradeData.php +++ b/app/code/Magento/Customer/Setup/UpgradeData.php @@ -60,7 +60,8 @@ class UpgradeData implements UpgradeDataInterface CustomerSetupFactory $customerSetupFactory, IndexerRegistry $indexerRegistry, \Magento\Eav\Model\Config $eavConfig - ) { + ) + { $this->customerSetupFactory = $customerSetupFactory; $this->indexerRegistry = $indexerRegistry; $this->eavConfig = $eavConfig; @@ -189,6 +190,19 @@ class UpgradeData implements UpgradeDataInterface return $countries; } + /** + * Retrieve countries not depending on global scope + * + * @param string $scope + * @param int $scopeCode + * @return array + */ + private function getAllowedCountries($scope, $scopeCode) + { + $reader = $this->getAllowedCountriesReader(); + return $reader->makeCountriesUnique($reader->getCountriesFromConfig($scope, $scopeCode)); + } + /** * Merge allowed countries from stores to websites * @@ -202,8 +216,7 @@ class UpgradeData implements UpgradeDataInterface foreach ($this->getStoreManager()->getStores() as $store) { $allowedCountries = $this->mergeAllowedCountries( $allowedCountries, - $this->getAllowedCountriesReader() - ->getAllowedCountries(ScopeInterface::SCOPE_STORE, $store->getId()), + $this->getAllowedCountries(ScopeInterface::SCOPE_STORE, $store->getId()), $store->getWebsiteId() ); } @@ -211,8 +224,7 @@ class UpgradeData implements UpgradeDataInterface foreach ($this->getStoreManager()->getWebsites() as $website) { $allowedCountries = $this->mergeAllowedCountries( $allowedCountries, - $this->getAllowedCountriesReader() - ->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $website->getId()), + $this->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $website->getId()), $website->getId() ); } diff --git a/app/code/Magento/Customer/Test/Unit/Model/Plugin/AllowedCountriesTest.php b/app/code/Magento/Customer/Test/Unit/Model/Plugin/AllowedCountriesTest.php index fbc3ca53c13adce77709f0cda3968a6ef5132ebf..e332d5deca626fb198669feb23011aa0e92a3be1 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Plugin/AllowedCountriesTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Plugin/AllowedCountriesTest.php @@ -56,8 +56,8 @@ class AllowedCountriesTest extends \PHPUnit_Framework_TestCase ->willReturn([$websiteMock]); $this->assertEquals( - [[$expectedFilter], $expectedScope], + [$expectedScope, [$expectedFilter]], $this->plugin->beforeGetAllowedCountries($originalAllowedCountriesMock) ); } -} \ No newline at end of file +} diff --git a/app/code/Magento/Customer/Test/Unit/Model/ResourceModel/Address/Attribute/Source/CountryWithWebsitesTest.php b/app/code/Magento/Customer/Test/Unit/Model/ResourceModel/Address/Attribute/Source/CountryWithWebsitesTest.php index 3fb6508cb412e0255f99ab1352be2a42fdd26a37..c91070b139ad6700cee3d032bde087359ff9ad3e 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/ResourceModel/Address/Attribute/Source/CountryWithWebsitesTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/ResourceModel/Address/Attribute/Source/CountryWithWebsitesTest.php @@ -93,12 +93,12 @@ class CountryWithWebsitesTest extends \PHPUnit_Framework_TestCase $this->allowedCountriesMock->expects($this->exactly(2)) ->method('getAllowedCountries') ->withConsecutive( - [1, 'website'], - [2, 'website'] + ['website', 1], + ['website', 2] ) ->willReturnMap([ - [1, 'website', ['AM' => 'AM']], - [2, 'website', ['AM' => 'AM', 'DZ' => 'DZ']] + ['website', 1, ['AM' => 'AM']], + ['website', 2, ['AM' => 'AM', 'DZ' => 'DZ']] ]); $this->countriesFactoryMock->expects($this->once()) ->method('create') diff --git a/app/code/Magento/Directory/Model/AllowedCountries.php b/app/code/Magento/Directory/Model/AllowedCountries.php index 9bdd531e7a0b88013f3ba5a703761aa6725791ff..9c8b0c51c2e0ee280527858d8bb9639bf4908385 100644 --- a/app/code/Magento/Directory/Model/AllowedCountries.php +++ b/app/code/Magento/Directory/Model/AllowedCountries.php @@ -71,14 +71,14 @@ class AllowedCountries $allowedCountries = $this->getCountriesFromConfig($scope, $scopeCode); } - return $this->getUniqueCountries($allowedCountries); + return $this->makeCountriesUnique($allowedCountries); } /** * Resolve scope code by scope * * @throws \InvalidArgumentException - * @param $scope + * @param string $scope * @return array|int */ private function getDefaultScopeCode($scope) @@ -105,7 +105,7 @@ class AllowedCountries * @param array $allowedCountries * @return array */ - private function getUniqueCountries(array $allowedCountries) + public function makeCountriesUnique(array $allowedCountries) { return array_combine($allowedCountries, $allowedCountries); } @@ -117,7 +117,7 @@ class AllowedCountries * @param int $scopeCode * @return array */ - private function getCountriesFromConfig($scope, $scopeCode) + public function getCountriesFromConfig($scope, $scopeCode) { return explode( ',',