Skip to content

Commit

Permalink
ENGCOM-4405: Sorting by Websites not working in product grid in backo…
Browse files Browse the repository at this point in the history
  • Loading branch information
sidolov authored Mar 14, 2019
2 parents 03b4e15 + bbb357a commit 5376a81
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
<test name="AdminSortingByWebsitesTest">
<annotations>
<stories value="View sorting by websites"/>
<title value="Sorting by websites in Admin"/>
<description value="Sorting products by websites in Admin"/>
</annotations>
<before>
<createData entity="_defaultCategory" stepKey="createCategory"/>
<createData entity="_defaultProduct" stepKey="productAssignedToCustomWebsite">
<requiredEntity createDataKey="createCategory"/>
</createData>
<createData entity="SimpleProduct" stepKey="productAssignedToMainWebsite">
<requiredEntity createDataKey="createCategory"/>
</createData>

<actionGroup ref="LoginAsAdmin" stepKey="loginAsAdmin"/>
<!--Create new website -->
<actionGroup ref="AdminCreateWebsiteActionGroup" stepKey="createAdditionalWebsite">
<argument name="newWebsiteName" value="{{customWebsite.name}}"/>
<argument name="websiteCode" value="{{customWebsite.code}}"/>
</actionGroup>
<actionGroup ref="EnableWebUrlOptions" stepKey="addStoreCodeToUrls"/>
<magentoCLI command="cache:flush" stepKey="flushCacheAfterEnableWebUrlOptions"/>
</before>
<after>
<deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
<deleteData createDataKey="productAssignedToCustomWebsite" stepKey="deleteProductAssignedToCustomWebsite"/>
<deleteData createDataKey="productAssignedToMainWebsite" stepKey="deleteProductAssignedToMainWebsite"/>
<actionGroup ref="AdminDeleteWebsiteActionGroup" stepKey="deleteTestWebsite">
<argument name="websiteName" value="{{customWebsite.name}}"/>
</actionGroup>
<actionGroup ref="ResetWebUrlOptions" stepKey="resetUrlOption"/>
<magentoCLI command="indexer:reindex" stepKey="reindex"/>
<magentoCLI command="cache:flush" stepKey="flushCache"/>
<actionGroup ref="logout" stepKey="logout"/>
</after>

<!--Assign Custom Website to Simple Product -->
<amOnPage url="{{AdminProductIndexPage.url}}" stepKey="navigateToCatalogProductGrid"/>
<waitForPageLoad stepKey="waitForCatalogProductGrid"/>

<conditionalClick selector="{{AdminProductGridFilterSection.clearFilters}}" dependentSelector="{{AdminProductGridFilterSection.clearFilters}}" visible="true" stepKey="clickClearFiltersInitial"/>
<actionGroup ref="OpenEditProductOnBackendActionGroup" stepKey="assignCustomWebsiteToProduct">
<argument name="product" value="$$productAssignedToCustomWebsite$$"/>
</actionGroup>
<scrollTo selector="{{ProductInWebsitesSection.sectionHeader}}" stepKey="scrollToWebsites"/>
<conditionalClick selector="{{ProductInWebsitesSection.sectionHeader}}" dependentSelector="{{AdminProductContentSection.sectionHeaderShow}}" visible="false" stepKey="expandSection"/>
<waitForPageLoad stepKey="waitForPageOpened"/>
<uncheckOption selector="{{ProductInWebsitesSection.website(_defaultWebsite.name)}}" stepKey="deselectMainWebsite"/>
<checkOption selector="{{ProductInWebsitesSection.website(customWebsite.name)}}" stepKey="selectWebsite"/>

<click selector="{{AdminProductFormActionSection.saveButton}}" stepKey="clickSave"/>
<waitForLoadingMaskToDisappear stepKey="waitForProductPageToSaveAgain"/>
<seeElement selector="{{AdminProductMessagesSection.successMessage}}" stepKey="seeSaveProductMessageAgain"/>

<!--Navigate To Product Grid To Check Website Sorting-->
<amOnPage url="{{AdminProductIndexPage.url}}" stepKey="navigateToCatalogProductGridToSortByWebsite"/>
<waitForPageLoad stepKey="waitForCatalogProductGridLoaded"/>

<!--Sorting works (By Websites) ASC-->
<click selector="{{AdminProductGridSection.columnHeader('Websites')}}" stepKey="clickWebsitesHeaderToSortAsc"/>
<see selector="{{AdminProductGridSection.productGridContentsOnRow('1')}}" userInput="Main Website" stepKey="checkIfProduct1WebsitesAsc"/>

<!--Sorting works (By Websites) DESC-->
<click selector="{{AdminProductGridSection.columnHeader('Websites')}}" stepKey="clickWebsitesHeaderToSortDesc"/>
<see selector="{{AdminProductGridSection.productGridContentsOnRow('1')}}" userInput="{{customWebsite.name}}" stepKey="checkIfProduct1WebsitesDesc"/>
</test>
</tests>
74 changes: 69 additions & 5 deletions app/code/Magento/Catalog/Ui/Component/Listing/Columns/Websites.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Catalog\Ui\Component\Listing\Columns;

use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\DB\Helper;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Store\Model\StoreManagerInterface;

/**
* Websites listing column component.
*
* @api
* @since 100.0.2
*/
Expand All @@ -20,33 +26,48 @@ class Websites extends \Magento\Ui\Component\Listing\Columns\Column
*/
const NAME = 'websites';

/**
* Data for concatenated website names value.
*/
private $websiteNames = 'website_names';

/**
* Store manager
*
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* @var \Magento\Framework\DB\Helper
*/
private $resourceHelper;

/**
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param StoreManagerInterface $storeManager
* @param array $components
* @param array $data
* @param Helper $resourceHelper
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
StoreManagerInterface $storeManager,
array $components = [],
array $data = []
array $data = [],
Helper $resourceHelper = null
) {
parent::__construct($context, $uiComponentFactory, $components, $data);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->storeManager = $storeManager;
$this->resourceHelper = $resourceHelper ?: $objectManager->get(Helper::class);
}

/**
* {@inheritdoc}
* @inheritdoc
*
* @deprecated 101.0.0
*/
public function prepareDataSource(array $dataSource)
Expand All @@ -71,9 +92,10 @@ public function prepareDataSource(array $dataSource)

return $dataSource;
}

/**
* Prepare component configuration
* Prepare component configuration.
*
* @return void
*/
public function prepare()
Expand All @@ -83,4 +105,46 @@ public function prepare()
$this->_data['config']['componentDisabled'] = true;
}
}

/**
* Apply sorting.
*
* @return void
*/
protected function applySorting()
{
$sorting = $this->getContext()->getRequestParam('sorting');
$isSortable = $this->getData('config/sortable');
if ($isSortable !== false
&& !empty($sorting['field'])
&& !empty($sorting['direction'])
&& $sorting['field'] === $this->getName()
) {
$collection = $this->getContext()->getDataProvider()->getCollection();
$collection
->joinField(
'websites_ids',
'catalog_product_website',
'website_id',
'product_id=entity_id',
null,
'left'
)
->joinTable(
'store_website',
'website_id = websites_ids',
['name'],
null,
'left'
)
->groupByAttribute('entity_id');
$this->resourceHelper->addGroupConcatColumn(
$collection->getSelect(),
$this->websiteNames,
'name'
);

$collection->getSelect()->order($this->websiteNames . ' ' . $sorting['direction']);
}
}
}

0 comments on commit 5376a81

Please sign in to comment.