Skip to content

Commit f130704

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-98832' into 2.3-develop-pr21
2 parents 15dc05a + 175f659 commit f130704

File tree

7 files changed

+221
-12
lines changed

7 files changed

+221
-12
lines changed

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Magento\Customer\Model\Customer\CredentialsValidator;
2020
use Magento\Customer\Model\Metadata\Validator;
2121
use Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory;
22+
use Magento\Directory\Model\AllowedCountries;
2223
use Magento\Eav\Model\Validator\Attribute\Backend;
2324
use Magento\Framework\Api\ExtensibleDataObjectConverter;
2425
use Magento\Framework\Api\SearchCriteriaBuilder;
@@ -339,6 +340,11 @@ class AccountManagement implements AccountManagementInterface
339340
*/
340341
private $addressRegistry;
341342

343+
/**
344+
* @var AllowedCountries
345+
*/
346+
private $allowedCountriesReader;
347+
342348
/**
343349
* @param CustomerFactory $customerFactory
344350
* @param ManagerInterface $eventManager
@@ -371,8 +377,10 @@ class AccountManagement implements AccountManagementInterface
371377
* @param CollectionFactory|null $visitorCollectionFactory
372378
* @param SearchCriteriaBuilder|null $searchCriteriaBuilder
373379
* @param AddressRegistry|null $addressRegistry
380+
* @param AllowedCountries|null $allowedCountriesReader
374381
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
375382
* @SuppressWarnings(PHPMD.NPathComplexity)
383+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
376384
*/
377385
public function __construct(
378386
CustomerFactory $customerFactory,
@@ -405,7 +413,8 @@ public function __construct(
405413
SaveHandlerInterface $saveHandler = null,
406414
CollectionFactory $visitorCollectionFactory = null,
407415
SearchCriteriaBuilder $searchCriteriaBuilder = null,
408-
AddressRegistry $addressRegistry = null
416+
AddressRegistry $addressRegistry = null,
417+
AllowedCountries $allowedCountriesReader = null
409418
) {
410419
$this->customerFactory = $customerFactory;
411420
$this->eventManager = $eventManager;
@@ -445,6 +454,8 @@ public function __construct(
445454
?: ObjectManager::getInstance()->get(SearchCriteriaBuilder::class);
446455
$this->addressRegistry = $addressRegistry
447456
?: ObjectManager::getInstance()->get(AddressRegistry::class);
457+
$this->allowedCountriesReader = $allowedCountriesReader
458+
?: ObjectManager::getInstance()->get(AllowedCountries::class);
448459
}
449460

450461
/**
@@ -899,6 +910,9 @@ public function createAccountWithPasswordHash(CustomerInterface $customer, $hash
899910
}
900911
try {
901912
foreach ($customerAddresses as $address) {
913+
if (!$this->isAddressAllowedForWebsite($address, $customer->getStoreId())) {
914+
continue;
915+
}
902916
if ($address->getId()) {
903917
$newAddress = clone $address;
904918
$newAddress->setId(null);
@@ -1071,6 +1085,7 @@ public function validate(CustomerInterface $customer)
10711085
$result = $this->getEavValidator()->isValid($customerModel);
10721086
if ($result === false && is_array($this->getEavValidator()->getMessages())) {
10731087
return $validationResults->setIsValid(false)->setMessages(
1088+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
10741089
call_user_func_array(
10751090
'array_merge',
10761091
$this->getEavValidator()->getMessages()
@@ -1606,4 +1621,18 @@ private function setIgnoreValidationFlag($customer)
16061621
{
16071622
$customer->setData('ignore_validation_flag', true);
16081623
}
1624+
1625+
/**
1626+
* Check is address allowed for store
1627+
*
1628+
* @param AddressInterface $address
1629+
* @param int|null $storeId
1630+
* @return bool
1631+
*/
1632+
private function isAddressAllowedForWebsite(AddressInterface $address, $storeId): bool
1633+
{
1634+
$allowedCountries = $this->allowedCountriesReader->getAllowedCountries(ScopeInterface::SCOPE_STORE, $storeId);
1635+
1636+
return in_array($address->getCountryId(), $allowedCountries);
1637+
}
16091638
}

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

Lines changed: 43 additions & 2 deletions
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
}
@@ -1951,6 +1982,14 @@ public function testCreateAccountWithPasswordHashWithCustomerAddresses()
19511982
->method('getWebsite')
19521983
->with($websiteId)
19531984
->willReturn($website);
1985+
$this->allowedCountriesReader
1986+
->expects($this->atLeastOnce())
1987+
->method('getAllowedCountries')
1988+
->willReturn(['US' => 'US']);
1989+
$existingAddress
1990+
->expects($this->atLeastOnce())
1991+
->method('getCountryId')
1992+
->willReturn('US');
19541993

19551994
$this->assertSame($customer, $this->accountManagement->createAccountWithPasswordHash($customer, $hash));
19561995
}
@@ -2078,7 +2117,9 @@ public function testCreateAccountUnexpectedValueException(): void
20782117
->method('newAccount')
20792118
->willThrowException($exception);
20802119
$this->logger->expects($this->once())->method('error')->with($exception);
2081-
2120+
$this->allowedCountriesReader->expects($this->atLeastOnce())
2121+
->method('getAllowedCountries')->willReturn(['US' => 'US']);
2122+
$address->expects($this->atLeastOnce())->method('getCountryId')->willReturn('US');
20822123
$this->accountManagement->createAccount($customer);
20832124
}
20842125

app/code/Magento/Quote/Model/Quote.php

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88
use Magento\Customer\Api\Data\CustomerInterface;
99
use Magento\Customer\Api\Data\GroupInterface;
10+
use Magento\Directory\Model\AllowedCountries;
1011
use Magento\Framework\Api\AttributeValueFactory;
1112
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
1213
use Magento\Framework\Model\AbstractExtensibleModel;
1314
use Magento\Quote\Api\Data\PaymentInterface;
1415
use Magento\Quote\Model\Quote\Address;
1516
use Magento\Quote\Model\Quote\Address\Total as AddressTotal;
1617
use Magento\Sales\Model\Status;
18+
use Magento\Store\Model\ScopeInterface;
1719
use Magento\Framework\App\ObjectManager;
1820

1921
/**
@@ -359,6 +361,11 @@ class Quote extends AbstractExtensibleModel implements \Magento\Quote\Api\Data\C
359361
*/
360362
private $orderIncrementIdChecker;
361363

364+
/**
365+
* @var AllowedCountries
366+
*/
367+
private $allowedCountriesReader;
368+
362369
/**
363370
* @param \Magento\Framework\Model\Context $context
364371
* @param \Magento\Framework\Registry $registry
@@ -401,6 +408,7 @@ class Quote extends AbstractExtensibleModel implements \Magento\Quote\Api\Data\C
401408
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
402409
* @param array $data
403410
* @param \Magento\Sales\Model\OrderIncrementIdChecker|null $orderIncrementIdChecker
411+
* @param AllowedCountries|null $allowedCountriesReader
404412
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
405413
*/
406414
public function __construct(
@@ -444,7 +452,8 @@ public function __construct(
444452
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
445453
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
446454
array $data = [],
447-
\Magento\Sales\Model\OrderIncrementIdChecker $orderIncrementIdChecker = null
455+
\Magento\Sales\Model\OrderIncrementIdChecker $orderIncrementIdChecker = null,
456+
AllowedCountries $allowedCountriesReader = null
448457
) {
449458
$this->quoteValidator = $quoteValidator;
450459
$this->_catalogProduct = $catalogProduct;
@@ -481,6 +490,8 @@ public function __construct(
481490
$this->shippingAssignmentFactory = $shippingAssignmentFactory;
482491
$this->orderIncrementIdChecker = $orderIncrementIdChecker ?: ObjectManager::getInstance()
483492
->get(\Magento\Sales\Model\OrderIncrementIdChecker::class);
493+
$this->allowedCountriesReader = $allowedCountriesReader
494+
?: ObjectManager::getInstance()->get(AllowedCountries::class);
484495
parent::__construct(
485496
$context,
486497
$registry,
@@ -941,7 +952,7 @@ public function assignCustomerWithAddressChange(
941952
/** @var \Magento\Quote\Model\Quote\Address $billingAddress */
942953
$billingAddress = $this->_quoteAddressFactory->create();
943954
$billingAddress->importCustomerAddressData($defaultBillingAddress);
944-
$this->setBillingAddress($billingAddress);
955+
$this->assignAddress($billingAddress);
945956
}
946957
}
947958

@@ -959,7 +970,8 @@ public function assignCustomerWithAddressChange(
959970
$shippingAddress = $this->_quoteAddressFactory->create();
960971
}
961972
}
962-
$this->setShippingAddress($shippingAddress);
973+
974+
$this->assignAddress($shippingAddress, false);
963975
}
964976

965977
return $this;
@@ -1374,7 +1386,7 @@ public function addShippingAddress(\Magento\Quote\Api\Data\AddressInterface $add
13741386
* Retrieve quote items collection
13751387
*
13761388
* @param bool $useCache
1377-
* @return \Magento\Eav\Model\Entity\Collection\AbstractCollection
1389+
* @return \Magento\Eav\Model\Entity\Collection\AbstractCollection
13781390
*/
13791391
public function getItemsCollection($useCache = true)
13801392
{
@@ -1429,7 +1441,7 @@ public function getAllVisibleItems()
14291441
*/
14301442
public function hasItems()
14311443
{
1432-
return sizeof($this->getAllItems()) > 0;
1444+
return count($this->getAllItems()) > 0;
14331445
}
14341446

14351447
/**
@@ -1488,7 +1500,7 @@ public function getItemById($itemId)
14881500
/**
14891501
* Delete quote item. If it does not have identifier then it will be only removed from collection
14901502
*
1491-
* @param \Magento\Quote\Model\Quote\Item $item
1503+
* @param \Magento\Quote\Model\Quote\Item $item
14921504
* @return $this
14931505
*/
14941506
public function deleteItem(\Magento\Quote\Model\Quote\Item $item)
@@ -1518,7 +1530,7 @@ public function deleteItem(\Magento\Quote\Model\Quote\Item $item)
15181530
/**
15191531
* Remove quote item by item identifier
15201532
*
1521-
* @param int $itemId
1533+
* @param int $itemId
15221534
* @return $this
15231535
*/
15241536
public function removeItem($itemId)
@@ -1569,7 +1581,7 @@ public function removeAllItems()
15691581
/**
15701582
* Adding new item to quote
15711583
*
1572-
* @param \Magento\Quote\Model\Quote\Item $item
1584+
* @param \Magento\Quote\Model\Quote\Item $item
15731585
* @return $this
15741586
* @throws \Magento\Framework\Exception\LocalizedException
15751587
*/
@@ -2363,7 +2375,7 @@ public function hasVirtualItems()
23632375
/**
23642376
* Merge quotes
23652377
*
2366-
* @param Quote $quote
2378+
* @param Quote $quote
23672379
* @return $this
23682380
*/
23692381
public function merge(Quote $quote)
@@ -2596,4 +2608,34 @@ public function setExtensionAttributes(\Magento\Quote\Api\Data\CartExtensionInte
25962608
{
25972609
return $this->_setExtensionAttributes($extensionAttributes);
25982610
}
2611+
2612+
/**
2613+
* Check is address allowed for store
2614+
*
2615+
* @param Address $address
2616+
* @param int|null $storeId
2617+
* @return bool
2618+
*/
2619+
private function isAddressAllowedForWebsite(Address $address, $storeId): bool
2620+
{
2621+
$allowedCountries = $this->allowedCountriesReader->getAllowedCountries(ScopeInterface::SCOPE_STORE, $storeId);
2622+
2623+
return in_array($address->getCountryId(), $allowedCountries);
2624+
}
2625+
2626+
/**
2627+
* Assign address to quote
2628+
*
2629+
* @param Address $address
2630+
* @param bool $isBillingAddress
2631+
* @return void
2632+
*/
2633+
private function assignAddress(Address $address, bool $isBillingAddress = true): void
2634+
{
2635+
if ($this->isAddressAllowedForWebsite($address, $this->getStoreId())) {
2636+
$isBillingAddress
2637+
? $this->setBillingAddress($address)
2638+
: $this->setShippingAddress($address);
2639+
}
2640+
}
25992641
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types = 1);
7+
8+
use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
9+
use Magento\Framework\App\Config\ReinitableConfigInterface;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
$objectManager = Bootstrap::getObjectManager();
13+
/** @var ConfigInterface $config */
14+
$config = $objectManager->get(ConfigInterface::class);
15+
$config->saveConfig('general/country/allow', 'FR');
16+
$objectManager->get(ReinitableConfigInterface::class)->reinit();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types = 1);
7+
8+
use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
9+
use Magento\Framework\App\Config\ReinitableConfigInterface;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
$objectManager = Bootstrap::getObjectManager();
13+
/** @var ConfigInterface $config */
14+
$config = $objectManager->get(ConfigInterface::class);
15+
$config->deleteConfig('general/country/allow');
16+
$objectManager->get(ReinitableConfigInterface::class)->reinit();

0 commit comments

Comments
 (0)