Skip to content

Commit 079e54e

Browse files
committed
Merge remote-tracking branch 'origin/MC-25042' into 2.4.1-develop-pr25
2 parents 1df9ed1 + a583d6e commit 079e54e

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

app/code/Magento/Checkout/Block/Checkout/AttributeMerger.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\Customer\Helper\Address as AddressHelper;
1111
use Magento\Customer\Model\Session;
1212
use Magento\Directory\Helper\Data as DirectoryHelper;
13+
use Magento\Directory\Model\AllowedCountries;
14+
use Magento\Framework\App\ObjectManager;
1315
use Magento\Framework\Exception\LocalizedException;
1416
use Magento\Framework\Exception\NoSuchEntityException;
1517

@@ -92,23 +94,32 @@ class AttributeMerger
9294
*/
9395
private $topCountryCodes;
9496

97+
/**
98+
* @var AllowedCountries|null
99+
*/
100+
private $allowedCountryReader;
101+
95102
/**
96103
* @param AddressHelper $addressHelper
97104
* @param Session $customerSession
98105
* @param CustomerRepository $customerRepository
99106
* @param DirectoryHelper $directoryHelper
107+
* @param AllowedCountries $allowedCountryReader
100108
*/
101109
public function __construct(
102110
AddressHelper $addressHelper,
103111
Session $customerSession,
104112
CustomerRepository $customerRepository,
105-
DirectoryHelper $directoryHelper
113+
DirectoryHelper $directoryHelper,
114+
?AllowedCountries $allowedCountryReader = null
106115
) {
107116
$this->addressHelper = $addressHelper;
108117
$this->customerSession = $customerSession;
109118
$this->customerRepository = $customerRepository;
110119
$this->directoryHelper = $directoryHelper;
111120
$this->topCountryCodes = $directoryHelper->getTopCountryCodes();
121+
$this->allowedCountryReader =
122+
$allowedCountryReader ?: ObjectManager::getInstance()->get(AllowedCountries::class);
112123
}
113124

114125
/**
@@ -289,7 +300,7 @@ protected function getMultilineFieldConfig($attributeCode, array $attributeConfi
289300
'dataScope' => $lineIndex,
290301
'provider' => $providerName,
291302
'validation' => $isFirstLine
292-
//phpcs:ignore Magento2.Performance.ForeachArrayMerge
303+
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
293304
? array_merge(
294305
['required-entry' => (bool)$attributeConfig['required']],
295306
$attributeConfig['validation']
@@ -330,7 +341,11 @@ protected function getMultilineFieldConfig($attributeCode, array $attributeConfi
330341
protected function getDefaultValue($attributeCode): ?string
331342
{
332343
if ($attributeCode === 'country_id') {
333-
return $this->directoryHelper->getDefaultCountry();
344+
$defaultCountryId = $this->directoryHelper->getDefaultCountry();
345+
if (!in_array($defaultCountryId, $this->allowedCountryReader->getAllowedCountries())) {
346+
$defaultCountryId = null;
347+
}
348+
return $defaultCountryId;
334349
}
335350

336351
$customer = $this->getCustomer();
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
namespace Magento\Checkout\Block\Checkout;
9+
10+
use Magento\Framework\App\Config\MutableScopeConfigInterface;
11+
use Magento\Store\Model\ScopeInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class LayoutProcessorTest extends TestCase
16+
{
17+
/**
18+
* Tests default country for shipping address.
19+
*
20+
* @param string $defaultCountryId
21+
* @param bool $isCountryValueExpected
22+
* @magentoConfigFixture default_store checkout/options/display_billing_address_on 1
23+
* @magentoDataFixture Magento/Backend/_files/allowed_countries_fr.php
24+
* @dataProvider defaultCountryDataProvider
25+
*/
26+
public function testShippingAddressCountryId(string $defaultCountryId, bool $isCountryValueExpected): void
27+
{
28+
/** @var MutableScopeConfigInterface $mutableConfig */
29+
$mutableConfig = Bootstrap::getObjectManager()->get(MutableScopeConfigInterface::class);
30+
$mutableConfig->setValue('general/country/default', $defaultCountryId, ScopeInterface::SCOPE_STORE, 'default');
31+
32+
/** @var $layoutProcessor LayoutProcessor */
33+
$layoutProcessor = Bootstrap::getObjectManager()->get(LayoutProcessor::class);
34+
35+
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
36+
['children']['shippingAddress']['children']['shipping-address-fieldset']['children'] = [];
37+
$data = $layoutProcessor->process($jsLayout);
38+
39+
$countryId = $data["components"]["checkout"]["children"]["steps"]["children"]["shipping-step"]["children"]
40+
["shippingAddress"]["children"]["shipping-address-fieldset"]["children"]["country_id"];
41+
42+
$isCountryValueExists = array_key_exists('value', $countryId);
43+
44+
$this->assertEquals($isCountryValueExpected, $isCountryValueExists);
45+
if ($isCountryValueExpected) {
46+
$this->assertEquals($defaultCountryId, $countryId['value']);
47+
}
48+
}
49+
50+
/**
51+
* @return array[]
52+
*/
53+
public function defaultCountryDataProvider(): array
54+
{
55+
return [
56+
'Default country isn\'t in allowed country list' => [
57+
'defaultCountryId' => 'US',
58+
'isCountryValueExpected' => false
59+
],
60+
'Default country is in allowed country list' => [
61+
'defaultCountryId' => 'FR',
62+
'isCountryValueExpected' => true
63+
],
64+
];
65+
}
66+
}

0 commit comments

Comments
 (0)