From 863c05a9b2bf6f98934e66aebb81ebd3bf1cca05 Mon Sep 17 00:00:00 2001 From: Sergii Kovalenko <skovalenko@magento.com> Date: Thu, 15 Sep 2016 06:34:02 -0700 Subject: [PATCH] MAGETWO-56941: [Github] Allowed countries for customer address in admin using storeview configuration #2946 --- .../Model/Plugin/AllowedCountries.php | 2 ++ .../Attribute/Source/CountryWithWebsites.php | 3 ++- .../Magento/Customer/Setup/UpgradeData.php | 22 ++++++++++++++----- .../Model/Plugin/AllowedCountriesTest.php | 4 ++-- .../Source/CountryWithWebsitesTest.php | 8 +++---- .../Directory/Model/AllowedCountries.php | 8 +++---- 6 files changed, 31 insertions(+), 16 deletions(-) diff --git a/app/code/Magento/Customer/Model/Plugin/AllowedCountries.php b/app/code/Magento/Customer/Model/Plugin/AllowedCountries.php index b49f63d94f8..157643bdfe5 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 50ff13e2b24..16ceee8a3f2 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 f503443c479..03bb6f72f2d 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 fbc3ca53c13..e332d5deca6 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 3fb6508cb41..c91070b139a 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 9bdd531e7a0..9c8b0c51c2e 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( ',', -- GitLab