|
| 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