|
| 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\Store\Ui\Component\Listing\Column\Store; |
| 9 | + |
| 10 | +use Magento\Store\Model\ResourceModel\Group as GroupResource; |
| 11 | +use Magento\Store\Model\ResourceModel\Store as StoreResource; |
| 12 | +use Magento\Store\Model\ResourceModel\Website as WebsiteResource; |
| 13 | +use Magento\Store\Model\StoreManagerInterface; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test for \Magento\Store\Ui\Component\Listing\Column\Store\Options. |
| 19 | + */ |
| 20 | +class OptionsTest extends TestCase |
| 21 | +{ |
| 22 | + private const DEFAULT_WEBSITE_NAME = 'Main Website'; |
| 23 | + private const DEFAULT_STORE_GROUP_NAME = 'Main Website Store'; |
| 24 | + private const DEFAULT_STORE_NAME = 'Default Store View'; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var OptionsFactory |
| 28 | + */ |
| 29 | + private $modelFactory; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var StoreManagerInterface |
| 33 | + */ |
| 34 | + private $storeManager; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var WebsiteResource |
| 38 | + */ |
| 39 | + private $websiteResource; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var StoreResource |
| 43 | + */ |
| 44 | + private $storeResource; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var GroupResource |
| 48 | + */ |
| 49 | + private $groupResource; |
| 50 | + |
| 51 | + /** |
| 52 | + * @return void |
| 53 | + */ |
| 54 | + protected function setUp(): void |
| 55 | + { |
| 56 | + $objectManager = Bootstrap::getObjectManager(); |
| 57 | + |
| 58 | + $this->modelFactory = $objectManager->get(OptionsFactory::class); |
| 59 | + $this->storeManager = $objectManager->get(StoreManagerInterface::class); |
| 60 | + |
| 61 | + $this->websiteResource = $objectManager->get(WebsiteResource::class); |
| 62 | + $this->groupResource = $objectManager->get(GroupResource::class); |
| 63 | + $this->storeResource = $objectManager->get(StoreResource::class); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * To option array test with duplicate website, store group, store view names |
| 68 | + * |
| 69 | + * @magentoDataFixture Magento/Store/_files/second_website_with_store_group_and_store.php |
| 70 | + * |
| 71 | + * @return void |
| 72 | + */ |
| 73 | + public function testToOptionArray(): void |
| 74 | + { |
| 75 | + $website = $this->storeManager->getWebsite('test'); |
| 76 | + $this->websiteResource->save($website->setName(self::DEFAULT_WEBSITE_NAME)); |
| 77 | + |
| 78 | + $storeGroup = current($website->getGroups()); |
| 79 | + $this->groupResource->save($storeGroup->setName(self::DEFAULT_STORE_GROUP_NAME)); |
| 80 | + |
| 81 | + $store = current($website->getStores()); |
| 82 | + $this->storeResource->save($store->setName(self::DEFAULT_STORE_NAME)); |
| 83 | + |
| 84 | + $model = $this->modelFactory->create(); |
| 85 | + $storeIds = [$this->storeManager->getStore('default')->getId(), $store->getId()]; |
| 86 | + |
| 87 | + $this->assertEquals($this->getExpectedOptions($storeIds), $model->toOptionArray()); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns expected options |
| 92 | + * |
| 93 | + * @param array $storeIds |
| 94 | + * @return array |
| 95 | + */ |
| 96 | + private function getExpectedOptions(array $storeIds): array |
| 97 | + { |
| 98 | + $expectedOptions = []; |
| 99 | + foreach ($storeIds as $storeId) { |
| 100 | + $expectedOptions[] = [ |
| 101 | + 'label' => self::DEFAULT_WEBSITE_NAME, |
| 102 | + 'value' => [[ |
| 103 | + 'label' => str_repeat(' ', 4) . self::DEFAULT_STORE_GROUP_NAME, |
| 104 | + 'value' => [[ |
| 105 | + 'label' => str_repeat(' ', 8) . self::DEFAULT_STORE_NAME, |
| 106 | + 'value' => $storeId, |
| 107 | + ]], |
| 108 | + ]], |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | + return $expectedOptions; |
| 113 | + } |
| 114 | +} |
0 commit comments