Skip to content

fix Same store name overriding in the store view grid filter #29474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Magento\Store\Model\System\Store as SystemStore;

/**
* Class Options
* Ui stores options
*/
class Options implements OptionSourceInterface
{
Expand Down Expand Up @@ -93,37 +93,38 @@ protected function sanitizeName($name)
*
* @return void
*/
protected function generateCurrentOptions()
protected function generateCurrentOptions(): void
{
$websiteCollection = $this->systemStore->getWebsiteCollection();
$groupCollection = $this->systemStore->getGroupCollection();
$storeCollection = $this->systemStore->getStoreCollection();
/** @var \Magento\Store\Model\Website $website */

foreach ($websiteCollection as $website) {
$groups = [];
/** @var \Magento\Store\Model\Group $group */
foreach ($groupCollection as $group) {
if ($group->getWebsiteId() == $website->getId()) {
if ($group->getWebsiteId() === $website->getId()) {
$stores = [];
/** @var \Magento\Store\Model\Store $store */
foreach ($storeCollection as $store) {
if ($store->getGroupId() == $group->getId()) {
$name = $this->sanitizeName($store->getName());
$stores[$name]['label'] = str_repeat(' ', 8) . $name;
$stores[$name]['value'] = $store->getId();
if ($store->getGroupId() === $group->getId()) {
$stores[] = [
'label' => str_repeat(' ', 8) . $this->sanitizeName($store->getName()),
'value' => $store->getId(),
];
}
}
if (!empty($stores)) {
$name = $this->sanitizeName($group->getName());
$groups[$name]['label'] = str_repeat(' ', 4) . $name;
$groups[$name]['value'] = array_values($stores);
$groups[] = [
'label' => str_repeat(' ', 4) . $this->sanitizeName($group->getName()),
'value' => array_values($stores),
];
}
}
}
if (!empty($groups)) {
$name = $this->sanitizeName($website->getName());
$this->currentOptions[$name]['label'] = $name;
$this->currentOptions[$name]['value'] = array_values($groups);
$this->currentOptions[] = [
'label' => $this->sanitizeName($website->getName()),
'value' => array_values($groups),
];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Store\Ui\Component\Listing\Column\Store;

use Magento\Store\Model\ResourceModel\Group as GroupResource;
use Magento\Store\Model\ResourceModel\Store as StoreResource;
use Magento\Store\Model\ResourceModel\Website as WebsiteResource;
use Magento\Store\Model\StoreManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Test for \Magento\Store\Ui\Component\Listing\Column\Store\Options.
*/
class OptionsTest extends TestCase
{
private const DEFAULT_WEBSITE_NAME = 'Main Website';
private const DEFAULT_STORE_GROUP_NAME = 'Main Website Store';
private const DEFAULT_STORE_NAME = 'Default Store View';

/**
* @var OptionsFactory
*/
private $modelFactory;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var WebsiteResource
*/
private $websiteResource;

/**
* @var StoreResource
*/
private $storeResource;

/**
* @var GroupResource
*/
private $groupResource;

/**
* @return void
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();

$this->modelFactory = $objectManager->get(OptionsFactory::class);
$this->storeManager = $objectManager->get(StoreManagerInterface::class);

$this->websiteResource = $objectManager->get(WebsiteResource::class);
$this->groupResource = $objectManager->get(GroupResource::class);
$this->storeResource = $objectManager->get(StoreResource::class);
}

/**
* To option array test with duplicate website, store group, store view names
*
* @magentoDataFixture Magento/Store/_files/second_website_with_store_group_and_store.php
*
* @return void
*/
public function testToOptionArray(): void
{
$website = $this->storeManager->getWebsite('test');
$this->websiteResource->save($website->setName(self::DEFAULT_WEBSITE_NAME));

$storeGroup = current($website->getGroups());
$this->groupResource->save($storeGroup->setName(self::DEFAULT_STORE_GROUP_NAME));

$store = current($website->getStores());
$this->storeResource->save($store->setName(self::DEFAULT_STORE_NAME));

$model = $this->modelFactory->create();
$storeIds = [$this->storeManager->getStore('default')->getId(), $store->getId()];

$this->assertEquals($this->getExpectedOptions($storeIds), $model->toOptionArray());
}

/**
* Returns expected options
*
* @param array $storeIds
* @return array
*/
private function getExpectedOptions(array $storeIds): array
{
$expectedOptions = [];
foreach ($storeIds as $storeId) {
$expectedOptions[] = [
'label' => self::DEFAULT_WEBSITE_NAME,
'value' => [[
'label' => str_repeat(' ', 4) . self::DEFAULT_STORE_GROUP_NAME,
'value' => [[
'label' => str_repeat(' ', 8) . self::DEFAULT_STORE_NAME,
'value' => $storeId,
]],
]],
];
}

return $expectedOptions;
}
}