Skip to content

Commit

Permalink
MAGETWO-71520: Product Grid filtered to Store View is broken if corre…
Browse files Browse the repository at this point in the history
…sponding Store is deleted

- fixing grid to render and reset filters if filters will throw an exception
- covering with functional tests
  • Loading branch information
cpartica committed Oct 3, 2017
1 parent 29bc9ad commit 2933325
Show file tree
Hide file tree
Showing 10 changed files with 510 additions and 8 deletions.
78 changes: 74 additions & 4 deletions app/code/Magento/Ui/Controller/Adminhtml/Index/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,49 @@

use Magento\Ui\Controller\Adminhtml\AbstractAction;
use Magento\Framework\View\Element\UiComponentInterface;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Element\UiComponentFactory;

class Render extends AbstractAction
{
/**
* @var \Magento\Framework\Controller\Result\JsonFactory
*/
private $resultJsonFactory;

/**
* @var \Magento\Framework\Escaper
*/
private $escaper;

/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;

/**
* @param Context $context
* @param UiComponentFactory $factory
* @param \Magento\Framework\Controller\Result\JsonFactory|null $resultJsonFactory
* @param \Magento\Framework\Escaper|null $escaper
* @param \Psr\Log\LoggerInterface|null $logger
*/
public function __construct(
Context $context,
UiComponentFactory $factory,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory = null,
\Magento\Framework\Escaper $escaper = null,
\Psr\Log\LoggerInterface $logger = null
) {
parent::__construct($context, $factory);
$this->resultJsonFactory = $resultJsonFactory ?: $this->_objectManager
->get(\Magento\Framework\Controller\Result\JsonFactory::class);
$this->escaper = $escaper ?: $this->_objectManager
->get(\Magento\Framework\Escaper::class);
$this->logger = $logger ?: $this->_objectManager
->get(\Psr\Log\LoggerInterface::class);
}

/**
* Action for AJAX request
*
Expand All @@ -22,10 +62,40 @@ public function execute()
return;
}

$component = $this->factory->create($this->_request->getParam('namespace'));
if ($this->validateAclResource($component->getContext()->getDataProvider()->getConfigData())) {
$this->prepareComponent($component);
$this->_response->appendBody((string) $component->render());
try {
$component = $this->factory->create($this->_request->getParam('namespace'));
if ($this->validateAclResource($component->getContext()->getDataProvider()->getConfigData())) {
$this->prepareComponent($component);
$this->_response->appendBody((string) $component->render());
}
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->logger->critical($e);
$result = [
'error' => $this->escaper->escapeHtml($e->getMessage()),
'errorcode' => $this->escaper->escapeHtml($e->getCode())
];
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
$resultJson->setStatusHeader(
\Zend\Http\Response::STATUS_CODE_400,
\Zend\Http\AbstractMessage::VERSION_11,
'Bad Request'
);
return $resultJson->setData($result);
} catch (\Exception $e) {
$this->logger->critical($e);
$result = [
'error' => _('UI component could not be rendered because of system exception'),
'errorcode' => $this->escaper->escapeHtml($e->getCode())
];
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
$resultJson->setStatusHeader(
\Zend\Http\Response::STATUS_CODE_400,
\Zend\Http\AbstractMessage::VERSION_11,
'Bad Request'
);
return $resultJson->setData($result);
}
}

Expand Down
51 changes: 48 additions & 3 deletions app/code/Magento/Ui/view/base/web/js/grid/filters/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ define([
'underscore',
'mageUtils',
'uiLayout',
'uiCollection'
], function (_, utils, layout, Collection) {
'uiCollection',
'mage/translate',
'jquery'
], function (_, utils, layout, Collection, $t, $) {
'use strict';

/**
Expand Down Expand Up @@ -48,6 +50,7 @@ define([
stickyTmpl: 'ui/grid/sticky/filters',
_processed: [],
columnsProvider: 'ns = ${ $.ns }, componentType = columns',
bookmarksProvider: 'ns = ${ $.ns }, componentType = bookmark',
applied: {
placeholder: true
},
Expand Down Expand Up @@ -102,7 +105,9 @@ define([
applied: '${ $.provider }:params.filters'
},
imports: {
'onColumnsUpdate': '${ $.columnsProvider }:elems'
onColumnsUpdate: '${ $.columnsProvider }:elems',
onBackendError: '${ $.provider }:lastError',
bookmarksActiveIndex: '${ $.bookmarksProvider }:activeIndex'
},
modules: {
columns: '${ $.columnsProvider }',
Expand Down Expand Up @@ -350,6 +355,15 @@ define([
return this.active.length;
},

/**
* Assigns filters index change.
*
* @param {integer} index - Selected index of the filter.
*/
onActiveIndexChange: function (index) {
this.activeIndex = index;
},

/**
* Extract previews of a specified filters.
*
Expand All @@ -371,6 +385,37 @@ define([
*/
onColumnsUpdate: function (columns) {
columns.forEach(this.addFilter, this);
},

/**
* Provider ajax error listener.
*
* @param {bool} isError - Selected index of the filter.
*/
onBackendError: function (isError) {
var defaultMessage = 'Something went wrong with processing the default view and we have restored the' +
' filter to its original state.',
customMessage = 'Something went wrong with processing current custom view and filters have been' +
' reset to its original state. Please edit filters then click apply.';

if (isError) {
this.clear();

$('body').notification('clear')
.notification('add', {
error: true,
message: $.mage.__(this.bookmarksActiveIndex !== 'default' ? customMessage : defaultMessage),

/**
* @param {String} message
*/
insertMethod: function (message) {
var $wrapper = $('<div/>').html(message);

$('.page-main-actions').after($wrapper);
}
});
}
}
});
});
8 changes: 7 additions & 1 deletion app/code/Magento/Ui/view/base/web/js/grid/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ define([

request
.done(this.onReload)
.fail(this.onError);
.fail(this.onError.bind(this));

return request;
},
Expand All @@ -144,6 +144,10 @@ define([
return;
}

this.set('lastError', true);

this.firstLoad = false;

alert({
content: $t('Something went wrong.')
});
Expand All @@ -157,6 +161,8 @@ define([
onReload: function (data) {
this.firstLoad = false;

this.set('lastError', false);

this.setData(data)
.trigger('reloaded');
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ public function searchAndOpenWebsite(Website $website)
$this->_rootElement->find(sprintf($this->storeName, $websiteName), Locator::SELECTOR_XPATH)->click();
}

/**
* Search and open appropriate Website by name.
*
* @param string $websiteName
* @return void
*/
public function searchAndOpenWebsiteByName($websiteName)
{
$this->search(['website_title' => $websiteName]);
$this->_rootElement->find(sprintf($this->storeName, $websiteName), Locator::SELECTOR_XPATH)->click();
}

/**
* Search and open appropriate Store View.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class Grid extends DataGrid
'selector' => '[name="attribute_set_id"]',
'input' => 'select',
],
'store_id' => [
'selector' => '[name="store_id"]',
'input' => 'select',
],
];

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Test\Constraint;

/**
* Assert that product grid is rendered correctly.
*/
class AssertProductGridIsRendered extends \Magento\Mtf\Constraint\AbstractConstraint
{
/**
* Assert that product grid is rendered correctly.
*
* @param \Magento\Catalog\Test\Page\Adminhtml\CatalogProductIndex $catalogProductIndex
* @return void
*/
public function processAssert(
\Magento\Catalog\Test\Page\Adminhtml\CatalogProductIndex $catalogProductIndex
) {
$productId = $catalogProductIndex->open()->getProductGrid()->getFirstItemId();
\PHPUnit_Framework_Assert::assertNotNull(
$productId,
'Product grid is not rendered correctly.'
);
}

/**
* Returns a string representation of the object.
*
* @return string
*/
public function toString()
{
return 'Product grid is rendered correctly.';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Test\Constraint;

/**
* Assert that filters have been reset successfully.
*/
class AssertResetFilterMessage extends \Magento\Mtf\Constraint\AbstractConstraint
{
/**
* Assert message that filters have been reset.
*
* @param \Magento\Catalog\Test\Page\Adminhtml\CatalogProductIndex $catalogProductIndex
* @return void
*/
public function processAssert(
\Magento\Catalog\Test\Page\Adminhtml\CatalogProductIndex $catalogProductIndex
) {
\PHPUnit_Framework_Assert::assertContains(
'restored the filter to its original state',
$catalogProductIndex->getMessagesBlock()->getErrorMessage()
);
}

/**
* Returns a string representation of the object.
*
* @return string
*/
public function toString()
{
return 'Filters have been reset successfully.';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../vendor/magento/mtf/etc/variations.xsd">
<testCase name="Magento\Ui\Test\TestCase\GridFilteringDeletedEntityTest" summary="Grid filtering by deleted entity" ticketId="MAGETWO-71940">
<variation name="GridFilteringDeletedEntityTestVariation1">
<data name="tag" xsi:type="string">severity:S2</data>
<data name="steps" xsi:type="array">
<item name="0" xsi:type="array">
<item name="0" xsi:type="string">Magento\Store\Test\TestStep\DeleteWebsitesEntityStep</item>
</item>
</data>
<data name="fixtureName" xsi:type="string">catalogProductSimple</data>
<data name="fixtureDataSet" xsi:type="string">product_with_additional_website</data>
<data name="filters" xsi:type="array">
<item name="0" xsi:type="array">
<item name="name" xsi:type="string">:name</item>
</item>
</data>
<data name="pageClass" xsi:type="string">Magento\Catalog\Test\Page\Adminhtml\CatalogProductIndex</data>
<data name="gridRetriever" xsi:type="string">getProductGrid</data>
<constraint name="\Magento\Catalog\Test\Constraint\AssertProductGridIsRendered"/>
<constraint name="Magento\Catalog\Test\Constraint\AssertResetFilterMessage"/>
</variation>
</testCase>
</config>
Loading

0 comments on commit 2933325

Please sign in to comment.