Skip to content

Commit f5342dc

Browse files
committed
MAGETWO-98832: Multistore Allowed Countries List Problem
1 parent 3a71e2a commit f5342dc

File tree

4 files changed

+150
-5
lines changed

4 files changed

+150
-5
lines changed

app/code/Magento/Customer/Model/AccountManagement.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Magento\Customer\Model\Config\Share as ConfigShare;
1818
use Magento\Customer\Model\Customer as CustomerModel;
1919
use Magento\Customer\Model\Customer\CredentialsValidator;
20-
use Magento\Customer\Model\Data\Address as DataAddress;
2120
use Magento\Customer\Model\Metadata\Validator;
2221
use Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory;
2322
use Magento\Directory\Model\AllowedCountries;
@@ -1618,11 +1617,11 @@ private function setIgnoreValidationFlag($customer)
16181617
/**
16191618
* Check is address allowed for store
16201619
*
1621-
* @param DataAddress $address
1620+
* @param AddressInterface $address
16221621
* @param int $storeId
16231622
* @return bool
16241623
*/
1625-
private function isAddressAllowedForWebsite(DataAddress $address, int $storeId): bool
1624+
private function isAddressAllowedForWebsite(AddressInterface $address, int $storeId): bool
16261625
{
16271626
$allowedCountries = $this->allowedCountriesReader->getAllowedCountries(ScopeInterface::SCOPE_STORE, $storeId);
16281627

app/code/Magento/Customer/Test/Unit/Model/AccountManagementTest.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Customer\Model\AuthenticationInterface;
1313
use Magento\Customer\Model\Data\Customer;
1414
use Magento\Customer\Model\EmailNotificationInterface;
15+
use Magento\Directory\Model\AllowedCountries;
1516
use Magento\Framework\Api\SearchCriteriaBuilder;
1617
use Magento\Framework\App\Area;
1718
use Magento\Framework\Exception\NoSuchEntityException;
@@ -155,6 +156,11 @@ class AccountManagementTest extends \PHPUnit\Framework\TestCase
155156
*/
156157
private $searchCriteriaBuilderMock;
157158

159+
/**
160+
* @var AllowedCountries|\PHPUnit_Framework_MockObject_MockObject
161+
*/
162+
private $allowedCountriesReader;
163+
158164
/**
159165
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
160166
*/
@@ -193,6 +199,7 @@ protected function setUp()
193199
$this->extensibleDataObjectConverter = $this->createMock(
194200
\Magento\Framework\Api\ExtensibleDataObjectConverter::class
195201
);
202+
$this->allowedCountriesReader = $this->createMock(AllowedCountries::class);
196203
$this->authenticationMock = $this->getMockBuilder(AuthenticationInterface::class)
197204
->disableOriginalConstructor()
198205
->getMock();
@@ -256,6 +263,7 @@ protected function setUp()
256263
'visitorCollectionFactory' => $this->visitorCollectionFactory,
257264
'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
258265
'addressRegistry' => $this->addressRegistryMock,
266+
'allowedCountriesReader' => $this->allowedCountriesReader,
259267
]
260268
);
261269
$this->objectManagerHelper->setBackwardCompatibleProperty(
@@ -551,7 +559,14 @@ public function testCreateAccountWithPasswordHashWithAddressException()
551559
->expects($this->once())
552560
->method('delete')
553561
->with($customer);
554-
562+
$this->allowedCountriesReader
563+
->expects($this->atLeastOnce())
564+
->method('getAllowedCountries')
565+
->willReturn(['US' => 'US']);
566+
$address
567+
->expects($this->atLeastOnce())
568+
->method('getCountryId')
569+
->willReturn('US');
555570
$this->accountManagement->createAccountWithPasswordHash($customer, $hash);
556571
}
557572

@@ -725,6 +740,14 @@ public function testCreateAccountWithoutPassword()
725740
$this->emailNotificationMock->expects($this->once())
726741
->method('newAccount')
727742
->willReturnSelf();
743+
$this->allowedCountriesReader
744+
->expects($this->atLeastOnce())
745+
->method('getAllowedCountries')
746+
->willReturn(['US' => 'US']);
747+
$address
748+
->expects($this->atLeastOnce())
749+
->method('getCountryId')
750+
->willReturn('US');
728751

729752
$this->accountManagement->createAccount($customer);
730753
}
@@ -970,6 +993,14 @@ public function testCreateAccountWithPassword()
970993
$this->emailNotificationMock->expects($this->once())
971994
->method('newAccount')
972995
->willReturnSelf();
996+
$this->allowedCountriesReader
997+
->expects($this->atLeastOnce())
998+
->method('getAllowedCountries')
999+
->willReturn(['US' => 'US']);
1000+
$address
1001+
->expects($this->atLeastOnce())
1002+
->method('getCountryId')
1003+
->willReturn('US');
9731004

9741005
$this->accountManagement->createAccount($customer, $password);
9751006
}
@@ -1954,6 +1985,14 @@ public function testCreateAccountWithPasswordHashWithCustomerAddresses()
19541985
->method('getWebsite')
19551986
->with($websiteId)
19561987
->willReturn($website);
1988+
$this->allowedCountriesReader
1989+
->expects($this->atLeastOnce())
1990+
->method('getAllowedCountries')
1991+
->willReturn(['US' => 'US']);
1992+
$existingAddress
1993+
->expects($this->atLeastOnce())
1994+
->method('getCountryId')
1995+
->willReturn('US');
19571996

19581997
$this->assertSame($customer, $this->accountManagement->createAccountWithPasswordHash($customer, $hash));
19591998
}
@@ -2081,6 +2120,14 @@ public function testCreateAccountUnexpectedValueException(): void
20812120
->method('newAccount')
20822121
->willThrowException($exception);
20832122
$this->logger->expects($this->once())->method('error')->with($exception);
2123+
$this->allowedCountriesReader
2124+
->expects($this->atLeastOnce())
2125+
->method('getAllowedCountries')
2126+
->willReturn(['US' => 'US']);
2127+
$address
2128+
->expects($this->atLeastOnce())
2129+
->method('getCountryId')
2130+
->willReturn('US');
20842131

20852132
$this->accountManagement->createAccount($customer);
20862133
}

dev/tests/integration/testsuite/Magento/Customer/Model/AccountManagementTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Framework\Exception\NoSuchEntityException;
1515
use Magento\Framework\Exception\State\ExpiredException;
1616
use Magento\Framework\Reflection\DataObjectProcessor;
17+
use Magento\Store\Model\StoreManagerInterface;
1718
use Magento\TestFramework\Helper\Bootstrap;
1819

1920
/**
@@ -53,6 +54,9 @@ class AccountManagementTest extends \PHPUnit\Framework\TestCase
5354
/** @var \Magento\Framework\Api\ExtensibleDataObjectConverter */
5455
private $extensibleDataObjectConverter;
5556

57+
/** @var StoreManagerInterface */
58+
private $storeManager;
59+
5660
/** @var \Magento\Framework\Api\DataObjectHelper */
5761
protected $dataObjectHelper;
5862

@@ -114,6 +118,9 @@ protected function setUp()
114118

115119
$this->extensibleDataObjectConverter = $this->objectManager
116120
->create(\Magento\Framework\Api\ExtensibleDataObjectConverter::class);
121+
122+
$this->storeManager = $this->objectManager
123+
->create(StoreManagerInterface::class);
117124
}
118125

119126
/**
@@ -844,6 +851,52 @@ public function testCreateNewCustomerWithPasswordHash()
844851
);
845852
}
846853

854+
/**
855+
* Customer has two addresses one of it is allowed in website and second is not
856+
*
857+
* @magentoDataFixture Magento/Customer/_files/customer.php
858+
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
859+
* @magentoDataFixture Magento/Store/_files/websites_different_countries.php
860+
* @magentoConfigFixture fixture_second_store_store general/country/allow UA
861+
* @return void
862+
*/
863+
public function testCreateNewCustomerWithPasswordHashWithNotAllowedCountry()
864+
{
865+
/** Preconditions:
866+
* Customer with two addresses created
867+
* Two websites with different allowed country configured
868+
*/
869+
$customerRepository = $this->objectManager->create(
870+
CustomerRepositoryInterface::class
871+
);
872+
$fixtureCustomerId = 1;
873+
$allowedCountryIdForSecondWebsite = 'UA';
874+
/** @var \Magento\Customer\Model\Customer $customer */
875+
$store =$this->storeManager->getStore('fixture_second_store');
876+
$customerData = $customerRepository->getById($fixtureCustomerId);
877+
$customerData->getAddresses()[1]->setRegion(null)->setCountryId($allowedCountryIdForSecondWebsite)
878+
->setRegionId(null);
879+
$customerData->setStoreId($store->getId())->setWebsiteId($store->getWebsiteId())->setId(null);
880+
$encryptor = $this->objectManager->get(\Magento\Framework\Encryption\EncryptorInterface::class);
881+
/** @var \Magento\Framework\Math\Random $mathRandom */
882+
$password = $this->objectManager->get(\Magento\Framework\Math\Random::class)->getRandomString(8);
883+
$passwordHash = $encryptor->getHash($password, true);
884+
$savedCustomer = $this->accountManagement->createAccountWithPasswordHash(
885+
$customerData,
886+
$passwordHash
887+
);
888+
$this->assertCount(
889+
1,
890+
$savedCustomer->getAddresses(),
891+
'The wrong address quantity was saved'
892+
);
893+
$this->assertSame(
894+
'UA',
895+
$savedCustomer->getAddresses()[0]->getCountryId(),
896+
'The address with the disallowed country was saved'
897+
);
898+
}
899+
847900
/**
848901
* @magentoAppArea frontend
849902
* @magentoDataFixture Magento/Customer/_files/customer.php

dev/tests/integration/testsuite/Magento/Quote/Model/QuoteTest.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
use Magento\Framework\Api\SearchCriteriaBuilder;
2020
use Magento\Quote\Api\CartRepositoryInterface;
2121
use Magento\Framework\Api\ExtensibleDataInterface;
22-
22+
use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
23+
use Magento\Framework\App\Config\ReinitableConfigInterface;
24+
use Magento\Store\Model\StoreManagerInterface;
2325
/**
2426
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2527
*/
@@ -30,12 +32,23 @@ class QuoteTest extends \PHPUnit\Framework\TestCase
3032
*/
3133
private $objectManager;
3234

35+
/**
36+
* @var ConfigInterface
37+
*/
38+
private $config;
39+
40+
/**
41+
* @var string
42+
*/
43+
private $allowedCountriesConfigPath = 'general/country/allow';
44+
3345
/**
3446
* @inheritdoc
3547
*/
3648
protected function setUp()
3749
{
3850
$this->objectManager = Bootstrap::getObjectManager();
51+
$this->config = Bootstrap::getObjectManager()->get(ConfigInterface::class);
3952
}
4053

4154
/**
@@ -330,6 +343,39 @@ public function testAssignCustomerWithAddressChange(): void
330343
}
331344
}
332345

346+
/**
347+
* Customer has address with country which not allowed in website
348+
*
349+
* @magentoDataFixture Magento/Customer/_files/customer.php
350+
* @magentoDataFixture Magento/Customer/_files/customer_address.php
351+
* @return void
352+
*/
353+
public function testAssignCustomerWithAddressChangeWithNotAllowedCountry()
354+
{
355+
/** Preconditions:
356+
* Customer with address is created
357+
*/
358+
$this->config->saveConfig(
359+
$this->allowedCountriesConfigPath,
360+
'FR'
361+
);
362+
Bootstrap::getObjectManager()->get(ReinitableConfigInterface::class)->reinit();
363+
Bootstrap::getObjectManager()->create(StoreManagerInterface::class)->reinitStores();
364+
365+
/** @var Quote $quote */
366+
$quote = $this->objectManager->create(Quote::class);
367+
$customerData = $this->_prepareQuoteForTestAssignCustomerWithAddressChange($quote);
368+
369+
/** Execute SUT */
370+
$quote->assignCustomerWithAddressChange($customerData);
371+
372+
/** Check that addresses are empty */
373+
$this->assertNull($quote->getBillingAddress()->getCountryId());
374+
$this->assertNull($quote->getShippingAddress()->getCountryId());
375+
376+
$this->config->deleteConfig($this->allowedCountriesConfigPath);
377+
}
378+
333379
/**
334380
* @magentoDataFixture Magento/Catalog/_files/product_simple_duplicated.php
335381
* @return void

0 commit comments

Comments
 (0)