Skip to content
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

Sorting by Websites not working in product grid in backoffice #20511 #20512

Merged
merged 11 commits into from
Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
@@ -0,0 +1,85 @@
<?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>
<actionGroup ref="LoginAsAdmin" stepKey="loginAsAdmin"/>
<!--Create new website -->
<actionGroup ref="AdminCreateWebsiteActionGroup" stepKey="createAdditionalWebsite">
<argument name="newWebsiteName" value="Second Website"/>
<argument name="websiteCode" value="second_website"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend to use data entity customWebsite.
It will create unique WebSite Name and WebSite code.
Arguments will look like this

<argument name="newWebsiteName" value="{{customWebsite.name}}"/>
<argument name="websiteCode" value="{{customWebsite.code}}"/>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

</actionGroup>
</before>
<after>
<actionGroup ref="AdminDeleteWebsiteActionGroup" stepKey="deleteTestWebsite">
<argument name="websiteName" value="Second Website"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you will use customWebsite entity, you should also specify it here: <argument name="websiteName" value="{{customWebsite.name}}"/>
And also you should specify {{customWebsite.name}} in all actions you specified 'Second Website'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

</actionGroup>
<actionGroup ref="logout" stepKey="logout"/>
</after>

<actionGroup ref="EnableWebUrlOptions" stepKey="addStoreCodeToUrls"/>
<!--Create a Simple Product -->
<amOnPage url="{{AdminProductIndexPage.url}}" stepKey="navigateToCatalogProductGrid"/>
<waitForPageLoad stepKey="waitForCatalogProductGrid"/>
<click selector="{{AdminProductGridActionSection.addProductToggle}}" stepKey="clickAddProductDropdown"/>
<click selector="{{AdminProductGridActionSection.addSimpleProduct}}" stepKey="clickAddSimpleProduct"/>
<fillField userInput="{{_defaultProduct.name}}" selector="{{AdminProductFormSection.productName}}" stepKey="fillName"/>
<fillField userInput="{{_defaultProduct.sku}}" selector="{{AdminProductFormSection.productSku}}" stepKey="fillSKU"/>
<fillField userInput="{{_defaultProduct.price}}" selector="{{AdminProductFormSection.productPrice}}" stepKey="fillPrice"/>
<fillField userInput="{{_defaultProduct.quantity}}" selector="{{AdminProductFormSection.productQuantity}}" stepKey="fillQuantity"/>

<!--Save the product -->
<click selector="{{AdminProductFormActionSection.saveButton}}" stepKey="clickSaveButton"/>
<waitForLoadingMaskToDisappear stepKey="waitProductPageSave"/>
<seeElement selector="{{AdminProductMessagesSection.successMessage}}" stepKey="seeProductSavedMessage"/>

<!-- Add this product to second website -->
<click selector="{{ProductInWebsitesSection.sectionHeader}}" stepKey="openProductInWebsitesSection1"/>
<click selector="{{ProductInWebsitesSection.website('Second Website')}}" stepKey="selectSecondWebsite"/>
<click selector="{{AdminProductFormActionSection.saveButton}}" stepKey="clickSave"/>
<waitForLoadingMaskToDisappear stepKey="waitForProductPagetoSaveAgain"/>
<seeElement selector="{{AdminProductMessagesSection.successMessage}}" stepKey="seeSaveProductMessageAgain"/>

<!--Create a Simple Product 2 -->
<amOnPage url="{{ProductCatalogPage.url}}" stepKey="openProductCatalogPage"/>
<waitForPageLoad stepKey="waitForProductCatalogPage"/>
<click selector="{{AdminProductGridActionSection.addProductToggle}}" stepKey="clickAddProductToggle"/>
<waitForPageLoad stepKey="waitForProductToggleToSelectSimpleProduct"/>
<click selector="{{AdminProductGridActionSection.addSimpleProduct}}" stepKey="clickSimpleProductFromDropDownList"/>

<fillField selector="{{AdminProductFormSection.productName}}" userInput="{{nameAndAttributeSkuMaskSimpleProduct.name}}" stepKey="fillSimpleProductName"/>
<fillField selector="{{AdminProductFormSection.productPrice}}" userInput="{{nameAndAttributeSkuMaskSimpleProduct.price}}" stepKey="fillSimpleProductPrice"/>
<fillField selector="{{AdminProductFormSection.productWeight}}" userInput="{{nameAndAttributeSkuMaskSimpleProduct.weight}}" stepKey="fillSimpleProductWeight"/>
<fillField selector="{{AdminProductFormSection.productQuantity}}" userInput="{{nameAndAttributeSkuMaskSimpleProduct.quantity}}" stepKey="fillSimpleProductQuantity"/>
<click selector="{{AdminProductFormSection.save}}" stepKey="clickSaveButton2"/>
<waitForPageLoad stepKey="waitForSimpleProductToSave"/>
<!-- Verify customer see success message -->
<see selector="{{AdminProductFormSection.successMessage}}" userInput="You saved the product." stepKey="seeAssertSimpleProductSaveSuccessMessage"/>

<amOnPage url="{{AdminProductIndexPage.url}}" stepKey="navigateToCatalogProductGrid2"/>
<waitForPageLoad stepKey="waitForCatalogProductGrid2"/>

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

<!--Sorting works (By Websites) DESC-->
<click selector="{{AdminProductGridSection.columnHeader('Websites')}}" stepKey="clickWebsitesHeaderToSortDesc"/>
<see selector="{{AdminProductGridSection.productGridContentsOnRow('1')}}" userInput="Second Website" stepKey="checkIfProduct1WebsitesDesc"/>
<see selector="{{AdminProductGridSection.productGridContentsOnRow('2')}}" userInput="Main Website" stepKey="checkIfProduct2WebsitesDesc"/>
</test>
</tests>
61 changes: 59 additions & 2 deletions app/code/Magento/Catalog/Ui/Component/Listing/Columns/Websites.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
Expand All @@ -8,6 +8,7 @@
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\DB\Helper;

/**
* @api
Expand All @@ -20,29 +21,43 @@ class Websites extends \Magento\Ui\Component\Listing\Columns\Column
*/
const NAME = 'websites';

/**
* Data for concatenated website names value
*/
const WEBSITE_NAMES = 'website_names';

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

/**
* @var \Magento\Framework\DB\Helper
*/
protected $_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);
}

/**
Expand Down Expand Up @@ -83,4 +98,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(),
self::WEBSITE_NAMES,
'name'
);

$collection->getSelect()->order(self::WEBSITE_NAMES . ' ' . $sorting['direction']);
}
}
}