Skip to content

Commit 5b47ddd

Browse files
committed
Merge branch 'ACP2E-3433' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-VK-2024-11-27-CE
2 parents 624c108 + 641ecdd commit 5b47ddd

File tree

2 files changed

+109
-6
lines changed
  • app/code/Magento/Customer/Model/ResourceModel/Address/Attribute/Source
  • dev/tests/integration/testsuite/Magento/Customer/Model/ResourceModel/Address/Attribute/Source

2 files changed

+109
-6
lines changed

app/code/Magento/Customer/Model/ResourceModel/Address/Attribute/Source/Country.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2013 Adobe
4+
* All Rights Reserved.
55
*/
66

77
namespace Magento\Customer\Model\ResourceModel\Address\Attribute\Source;
88

9+
use Magento\Framework\App\Area;
910
use Magento\Framework\App\ObjectManager;
11+
use Magento\Store\Model\Store;
1012
use Magento\Store\Model\StoreManagerInterface;
1113

1214
/**
@@ -24,17 +26,25 @@ class Country extends \Magento\Eav\Model\Entity\Attribute\Source\Table
2426
*/
2527
private $storeManager;
2628

29+
/**
30+
* @var \Magento\Framework\App\State
31+
*/
32+
private $state;
33+
2734
/**
2835
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
2936
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
3037
* @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countriesFactory
38+
* @param \Magento\Framework\App\State $state
3139
*/
3240
public function __construct(
3341
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
3442
\Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory,
35-
\Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countriesFactory
43+
\Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countriesFactory,
44+
\Magento\Framework\App\State $state
3645
) {
3746
$this->_countriesFactory = $countriesFactory;
47+
$this->state = $state;
3848
parent::__construct($attrOptionCollectionFactory, $attrOptionFactory);
3949
}
4050

@@ -44,9 +54,14 @@ public function __construct(
4454
public function getAllOptions($withEmpty = true, $defaultValues = false)
4555
{
4656
if (!$this->_options) {
47-
$this->_options = $this->_createCountriesCollection()->loadByStore(
48-
$this->getStoreManager()->getStore()->getId()
49-
)->toOptionArray();
57+
if ($this->state->getAreaCode() === Area::AREA_ADMINHTML) {
58+
$storeId = $this->getAttribute()->getWebsite() &&
59+
$this->getAttribute()->getWebsite()->getDefaultStore() ?
60+
$this->getAttribute()->getWebsite()->getDefaultStore()->getId() : Store::DEFAULT_STORE_ID;
61+
} else {
62+
$storeId = $this->getStoreManager()->getStore()->getId();
63+
}
64+
$this->_options = $this->_createCountriesCollection()->loadByStore($storeId)->toOptionArray();
5065
}
5166
return $this->_options;
5267
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Model\ResourceModel\Address\Attribute\Source;
9+
10+
use Magento\Eav\Model\Config;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Framework\Validator\UniversalFactory;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
16+
class CountryTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* @var ObjectManagerInterface
20+
*/
21+
private $objectManager;
22+
23+
/**
24+
* @var StoreManagerInterface
25+
*/
26+
private $storeManager;
27+
28+
/**
29+
* @var Config
30+
*/
31+
private $eavConfig;
32+
33+
/**
34+
* @var UniversalFactory
35+
*/
36+
private $factory;
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function setUp(): void
42+
{
43+
$this->objectManager = Bootstrap::getObjectManager();
44+
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
45+
$this->eavConfig = $this->objectManager->get(Config::class);
46+
$this->factory = $this->objectManager->get(UniversalFactory::class);
47+
48+
parent::setUp();
49+
}
50+
51+
/**
52+
* Assert that countries are returned according to allowed countries per respective website
53+
*
54+
* @magentoAppArea adminhtml
55+
* @magentoDbIsolation disabled
56+
* @magentoDataFixture Magento/Store/_files/second_website_with_store_group_and_store.php
57+
* @magentoConfigFixture default_store general/country/default NL
58+
* @magentoConfigFixture default_store general/country/allow NL
59+
* @magentoConfigFixture fixture_second_store_store general/country/default UA
60+
* @magentoConfigFixture fixture_second_store_store general/country/allow UA
61+
* @dataProvider dataProvider
62+
*/
63+
public function testMethod($website, $expectedCountryCont, $expectedCountryCode)
64+
{
65+
$websiteId = $this->storeManager->getWebsite($website)->getId();
66+
$attribute = $this->eavConfig->getAttribute('customer_address', 'country_id');
67+
$attribute->setWebsite($websiteId);
68+
69+
$countryOptions = $this->factory->create($attribute->getSourceModel())
70+
->setAttribute($attribute)->getAllOptions();
71+
72+
$countryOptions = array_map(fn($countryOption) => $countryOption['value'], $countryOptions);
73+
$this->assertEquals($expectedCountryCont, count($countryOptions));
74+
$this->assertContains($expectedCountryCode, $countryOptions);
75+
}
76+
77+
/**
78+
* @return array
79+
*/
80+
public static function dataProvider()
81+
{
82+
return [
83+
['admin', 249, 'GB'],
84+
['base', 1, 'NL'],
85+
['test', 1, 'UA'],
86+
];
87+
}
88+
}

0 commit comments

Comments
 (0)