From fe86a0a603ce12688cb70baf1d7b36be6c58a950 Mon Sep 17 00:00:00 2001
From: Sergii Kovalenko <skovalenko@magento.com>
Date: Tue, 13 Sep 2016 00:53:35 -0700
Subject: [PATCH] MAGETWO-56941: [Github] Allowed countries for customer
 address in admin using storeview configuration #2946

---
 .../Customer/Model/Customer/DataProvider.php  |  2 +-
 .../Model/Plugin/AllowedCountries.php         |  6 ++---
 .../Attribute/Source/CountryWithWebsites.php  |  2 +-
 .../Magento/Customer/Setup/UpgradeData.php    |  4 ++--
 .../Directory/Model/AllowedCountries.php      |  4 ++--
 .../ResourceModel/Country/Collection.php      |  2 +-
 .../Test/Unit/Model/AllowedCountriesTest.php  | 22 +------------------
 7 files changed, 11 insertions(+), 31 deletions(-)

diff --git a/app/code/Magento/Customer/Model/Customer/DataProvider.php b/app/code/Magento/Customer/Model/Customer/DataProvider.php
index 8d2c8fbe442..2f46459a794 100644
--- a/app/code/Magento/Customer/Model/Customer/DataProvider.php
+++ b/app/code/Magento/Customer/Model/Customer/DataProvider.php
@@ -321,7 +321,7 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
 
             if ($attribute->usesSource()) {
                 if ($code == AddressInterface::COUNTRY_ID) {
-                    $meta[$code]['arguments']['data']['config']['options'] = $this->getcountryWithWebsiteSource()
+                    $meta[$code]['arguments']['data']['config']['options'] = $this->getCountryWithWebsiteSource()
                         ->getAllOptions();
                 } else {
                     $meta[$code]['arguments']['data']['config']['options'] = $attribute->getSource()->getAllOptions();
diff --git a/app/code/Magento/Customer/Model/Plugin/AllowedCountries.php b/app/code/Magento/Customer/Model/Plugin/AllowedCountries.php
index 366918f8b4e..b49f63d94f8 100644
--- a/app/code/Magento/Customer/Model/Plugin/AllowedCountries.php
+++ b/app/code/Magento/Customer/Model/Plugin/AllowedCountries.php
@@ -47,8 +47,8 @@ class AllowedCountries
      */
     public function beforeGetAllowedCountries(
         \Magento\Directory\Model\AllowedCountries $subject,
-        $scopeCode = null,
-        $scope = ScopeInterface::SCOPE_WEBSITE
+        $scope = ScopeInterface::SCOPE_WEBSITE,
+        $scopeCode = null
     ) {
         if ($this->shareConfig->isGlobalScope()) {
             //Check if we have shared accounts - than merge all website allowed countries
@@ -58,6 +58,6 @@ class AllowedCountries
             $scope = ScopeInterface::SCOPE_WEBSITES;
         }
 
-        return [$scopeCode, $scope];
+        return [$scope, $scopeCode];
     }
 }
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 0cbebf49198..50ff13e2b24 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
@@ -78,7 +78,7 @@ class CountryWithWebsites extends \Magento\Eav\Model\Entity\Attribute\Source\Tab
             if (!$this->shareConfig->isGlobalScope()) {
                 foreach ($this->storeManager->getWebsites() as $website) {
                     $countries = $this->allowedCountriesReader
-                        ->getAllowedCountries($website->getId(), ScopeInterface::SCOPE_WEBSITE);
+                        ->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $website->getId());
                     $allowedCountries = array_merge($allowedCountries, $countries);
 
                     foreach ($countries as $countryCode) {
diff --git a/app/code/Magento/Customer/Setup/UpgradeData.php b/app/code/Magento/Customer/Setup/UpgradeData.php
index a6fc60fe535..f503443c479 100644
--- a/app/code/Magento/Customer/Setup/UpgradeData.php
+++ b/app/code/Magento/Customer/Setup/UpgradeData.php
@@ -203,7 +203,7 @@ class UpgradeData implements UpgradeDataInterface
             $allowedCountries = $this->mergeAllowedCountries(
                 $allowedCountries,
                 $this->getAllowedCountriesReader()
-                    ->getAllowedCountries($store->getId(), ScopeInterface::SCOPE_STORE),
+                    ->getAllowedCountries(ScopeInterface::SCOPE_STORE, $store->getId()),
                 $store->getWebsiteId()
             );
         }
@@ -212,7 +212,7 @@ class UpgradeData implements UpgradeDataInterface
             $allowedCountries = $this->mergeAllowedCountries(
                 $allowedCountries,
                 $this->getAllowedCountriesReader()
-                    ->getAllowedCountries($website->getId(), ScopeInterface::SCOPE_WEBSITE),
+                    ->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $website->getId()),
                 $website->getId()
             );
         }
diff --git a/app/code/Magento/Directory/Model/AllowedCountries.php b/app/code/Magento/Directory/Model/AllowedCountries.php
index 8f5ce54d602..9bdd531e7a0 100644
--- a/app/code/Magento/Directory/Model/AllowedCountries.php
+++ b/app/code/Magento/Directory/Model/AllowedCountries.php
@@ -49,8 +49,8 @@ class AllowedCountries
      * @return array
      */
     public function getAllowedCountries(
-        $scopeCode = null,
-        $scope = ScopeInterface::SCOPE_WEBSITE
+        $scope = ScopeInterface::SCOPE_WEBSITE,
+        $scopeCode = null
     ) {
         if (empty($scopeCode)) {
             $scopeCode = $this->getDefaultScopeCode($scope);
diff --git a/app/code/Magento/Directory/Model/ResourceModel/Country/Collection.php b/app/code/Magento/Directory/Model/ResourceModel/Country/Collection.php
index 128a8afbcaa..1093e41b6da 100644
--- a/app/code/Magento/Directory/Model/ResourceModel/Country/Collection.php
+++ b/app/code/Magento/Directory/Model/ResourceModel/Country/Collection.php
@@ -150,7 +150,7 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
     public function loadByStore($store = null)
     {
         $allowedCountries = $this->getAllowedCountriesReader()
-            ->getAllowedCountries($store, ScopeInterface::SCOPE_STORE);
+            ->getAllowedCountries(ScopeInterface::SCOPE_STORE, $store);
 
         if (!empty($allowedCountries)) {
             $this->addFieldToFilter("country_id", ['in' => $allowedCountries]);
diff --git a/app/code/Magento/Directory/Test/Unit/Model/AllowedCountriesTest.php b/app/code/Magento/Directory/Test/Unit/Model/AllowedCountriesTest.php
index a39770181c2..f5ed44753c4 100644
--- a/app/code/Magento/Directory/Test/Unit/Model/AllowedCountriesTest.php
+++ b/app/code/Magento/Directory/Test/Unit/Model/AllowedCountriesTest.php
@@ -67,27 +67,7 @@ class AllowedCountriesTest extends \PHPUnit_Framework_TestCase
 
         $this->assertEquals(
             ['AM' => 'AM'],
-            $this->allowedCountriesReader->getAllowedCountries(1, ScopeInterface::SCOPE_WEBSITE, true)
+            $this->allowedCountriesReader->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, true)
         );
     }
-
-//    public function testLoadByScope()
-//    {
-//        $this->scopeConfigMock->expects($this->once())
-//            ->method('getValue')
-//            ->with(CountryHandlerInterface::ALLOWED_COUNTRIES_PATH, 'website', 1)
-//            ->willReturn('AM');
-//
-//        $collectionMock = $this->getMockBuilder(AbstractDb::class)
-//            ->disableOriginalConstructor()
-//            ->getMock();
-//        $collectionMock->expects($this->once())
-//            ->method('addFieldToFilter')
-//            ->with('country_id', ['in' => ['AM' => 'AM']]);
-//
-//        $this->assertEquals(
-//            $collectionMock,
-//            $this->countryHandler->loadByScope($collectionMock, 1, ScopeInterface::SCOPE_WEBSITE)
-//        );
-//    }
 }
-- 
GitLab