|
9 | 9 | */
|
10 | 10 | namespace Magento\Customer\Model\ResourceModel\Address;
|
11 | 11 |
|
12 |
| -use Magento\Store\Model\ScopeInterface; |
| 12 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 13 | +use Magento\Framework\App\Config\ConfigResource\ConfigInterface; |
| 14 | +use Magento\Framework\App\Config\ReinitableConfigInterface; |
| 15 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 16 | +use Magento\Framework\App\Config\Storage\Writer; |
| 17 | +use Magento\Framework\App\Config\Storage\WriterInterface; |
| 18 | +use Magento\TestFramework\Helper\Bootstrap; |
| 19 | +use Magento\TestFramework\ObjectManager; |
| 20 | +use PHPUnit\Framework\TestCase; |
13 | 21 |
|
14 |
| -class StoreAddressCollectionTest extends \PHPUnit\Framework\TestCase |
| 22 | +/** |
| 23 | + * Assert that only relevant addresses for the allowed countries under a website/store fetch. |
| 24 | + * |
| 25 | + * @magentoDbIsolation enabled |
| 26 | + */ |
| 27 | +class StoreAddressCollectionTest extends TestCase |
15 | 28 | {
|
16 |
| - public function testSetCustomerFilter() |
| 29 | + /** |
| 30 | + * @var ObjectManager |
| 31 | + */ |
| 32 | + protected $objectManager; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var CustomerRepositoryInterface |
| 36 | + */ |
| 37 | + protected $customerRepository; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var StoreAddressCollection |
| 41 | + */ |
| 42 | + private $storeAddressCollection; |
| 43 | + |
| 44 | + protected function setUp(): void |
17 | 45 | {
|
18 |
| - $collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
19 |
| - \Magento\Customer\Model\ResourceModel\Address\StoreAddressCollection::class |
20 |
| - ); |
21 |
| - $select = $collection->getSelect(); |
22 |
| - $this->assertSame($collection, $collection->setCustomerFilter([1, 2])); |
23 |
| - $customer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
24 |
| - \Magento\Customer\Model\Customer::class |
25 |
| - ); |
26 |
| - $collection->setCustomerFilter($customer); |
27 |
| - $customer->setId(3); |
28 |
| - $collection->setCustomerFilter($customer); |
29 |
| - $format = '%AWHERE%S(%Sparent_id%S IN(%S1%S, %S2%S))%SAND%S(%Sparent_id%S = %S-1%S)%SAND%S(%Sparent_id%S = %S3%S)%A'; |
30 |
| - |
31 |
| - $storeId = $customer->getStoreId() ?? 1; |
32 |
| - if ($storeId) { |
33 |
| - $allowedCountriesObj = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
34 |
| - \Magento\Directory\Model\AllowedCountries::class |
35 |
| - ); |
36 |
| - $allowedCountries = $allowedCountriesObj->getAllowedCountries(ScopeInterface::SCOPE_STORE, $storeId); |
37 |
| - $strAllowedCountries = implode("%S, %S", $allowedCountries); |
38 |
| - $format = "%AWHERE%S(%Sparent_id%S IN(%S1%S, %S2%S))%SAND%S(%Scountry_id%S IN(%S$strAllowedCountries%S))%A"; |
| 46 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 47 | + $this->customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class); |
| 48 | + $this->storeAddressCollection = $this->objectManager->create(StoreAddressCollection::class); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Ensure that config changes are deleted or restored. |
| 53 | + */ |
| 54 | + protected function tearDown(): void |
| 55 | + { |
| 56 | + /** @var \Magento\Framework\Registry $registry */ |
| 57 | + $registry = $this->objectManager->get(\Magento\Framework\Registry::class); |
| 58 | + $registry->unregister('isSecureArea'); |
| 59 | + $registry->register('isSecureArea', true); |
| 60 | + |
| 61 | + /** @var ConfigInterface $config */ |
| 62 | + $config = $this->objectManager->get(ConfigInterface::class); |
| 63 | + $config->deleteConfig('general/country/allow'); |
| 64 | + $this->objectManager->get(ReinitableConfigInterface::class)->reinit(); |
| 65 | + |
| 66 | + /** @var Writer $configWriter */ |
| 67 | + $configWriter = $this->objectManager->get(WriterInterface::class); |
| 68 | + |
| 69 | + $configWriter->save('customer/account_share/scope', 1); |
| 70 | + $scopeConfig = $this->objectManager->get(ScopeConfigInterface::class); |
| 71 | + $scopeConfig->clean(); |
| 72 | + |
| 73 | + $registry->unregister('isSecureArea'); |
| 74 | + $registry->register('isSecureArea', false); |
| 75 | + parent::tearDown(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Assert that only allowed country address fetched. |
| 80 | + * |
| 81 | + * @magentoDataFixture Magento/Customer/_files/customer.php |
| 82 | + * @magentoDataFixture Magento/Customer/_files/customer_address.php |
| 83 | + * |
| 84 | + * @dataProvider addressesDataProvider |
| 85 | + * |
| 86 | + * @param $customerId |
| 87 | + * @param $allowedCountries |
| 88 | + * @return void |
| 89 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 90 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 91 | + */ |
| 92 | + public function testSetCustomerFilter($customerId, $allowedCountries) : void |
| 93 | + { |
| 94 | + /** @var ConfigInterface $config */ |
| 95 | + $config = $this->objectManager->get(ConfigInterface::class); |
| 96 | + $config->saveConfig('general/country/allow', implode(',', $allowedCountries)); |
| 97 | + $this->objectManager->get(ReinitableConfigInterface::class)->reinit(); |
| 98 | + |
| 99 | + /** @var Writer $configWriter */ |
| 100 | + $configWriter = $this->objectManager->get(WriterInterface::class); |
| 101 | + $configWriter->save('customer/account_share/scope', 0); |
| 102 | + $scopeConfig = $this->objectManager->get(ScopeConfigInterface::class); |
| 103 | + $scopeConfig->clean(); |
| 104 | + |
| 105 | + $customer = $this->customerRepository->getById($customerId); |
| 106 | + $addresses = $this->storeAddressCollection->setCustomerFilter($customer); |
| 107 | + $this->assertIsArray($addresses->getData()); |
| 108 | + |
| 109 | + foreach ($addresses->getData() as $address) { |
| 110 | + $this->assertContains($address['country_id'], $allowedCountries); |
39 | 111 | }
|
40 |
| - $this->assertStringMatchesFormat($format, (string)$select); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Data provider for create allowed or not allowed countries. |
| 116 | + * |
| 117 | + * @return array |
| 118 | + */ |
| 119 | + public function addressesDataProvider(): array |
| 120 | + { |
| 121 | + return [ |
| 122 | + 'address_in_single_allowed_country' => [1, ['US']], |
| 123 | + 'address_not_in_single_allowed_country' => [1, ['FR']], |
| 124 | + 'address_in_multiple_allowed_countries' => [1, ['US', 'IN']], |
| 125 | + 'address_not_in_multiple_allowed_countries' => [1, ['FR', 'DE']], |
| 126 | + ]; |
41 | 127 | }
|
42 | 128 | }
|
0 commit comments