Skip to content

Commit

Permalink
Merge branch 'MAGETWO-71520-grid-filter-broken' into MAGETWO-80201
Browse files Browse the repository at this point in the history
  • Loading branch information
irenelagno committed Oct 6, 2017
2 parents eaf2ffa + a6a2301 commit 4746d64
Show file tree
Hide file tree
Showing 11 changed files with 595 additions and 11 deletions.
83 changes: 78 additions & 5 deletions app/code/Magento/Ui/Controller/Adminhtml/Index/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,56 @@

use Magento\Ui\Controller\Adminhtml\AbstractAction;
use Magento\Framework\View\Element\UiComponentInterface;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Element\UiComponentFactory;
use Psr\Log\LoggerInterface;
use Magento\Framework\Escaper;
use Magento\Framework\Controller\Result\JsonFactory;

class Render extends AbstractAction
{
/**
* @var JsonFactory
*/
private $resultJsonFactory;

/**
* @var Escaper
*/
private $escaper;

/**
* @var LoggerInterface
*/
private $logger;

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

/**
* Action for AJAX request
*
* @return void
* @return void|\Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
Expand All @@ -22,10 +65,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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/
namespace Magento\Ui\Test\Unit\Controller\Adminhtml\Index;

use \Magento\Ui\Controller\Adminhtml\Index\Render;
use Magento\Ui\Controller\Adminhtml\Index\Render;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand All @@ -17,6 +18,11 @@ class RenderTest extends \PHPUnit\Framework\TestCase
*/
private $render;

/**
* @var ObjectManagerHelper
*/
private $objectManagerHelper;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -73,6 +79,16 @@ class RenderTest extends \PHPUnit\Framework\TestCase
*/
private $uiComponentMock;

/**
* @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $resultJsonFactoryMock;

/**
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $loggerMock;

protected function setUp()
{
$this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
Expand Down Expand Up @@ -114,6 +130,14 @@ protected function setUp()
['render']
);

$this->resultJsonFactoryMock = $this->getMockBuilder(
\Magento\Framework\Controller\Result\JsonFactory::class
)
->disableOriginalConstructor()
->getMock();

$this->loggerMock = $this->getMockForAbstractClass(\Psr\Log\LoggerInterface::class);

$this->contextMock->expects($this->any())
->method('getRequest')
->willReturn($this->requestMock);
Expand All @@ -136,7 +160,70 @@ protected function setUp()
->method('getDataProvider')
->willReturn($this->dataProviderMock);

$this->render = new Render($this->contextMock, $this->uiFactoryMock);
$this->objectManagerHelper = new ObjectManagerHelper($this);

$this->render = $this->objectManagerHelper->getObject(
\Magento\Ui\Controller\Adminhtml\Index\Render::class,
[
'context' => $this->contextMock,
'factory' => $this->uiFactoryMock,
'resultJsonFactory' => $this->resultJsonFactoryMock,
'logger' => $this->loggerMock
]
);
}

public function testExecuteAjaxRequestException()
{
$name = 'test-name';
$renderedData = '<html>data</html>';

$this->requestMock->expects($this->any())
->method('getParam')
->with('namespace')
->willReturn($name);
$this->requestMock->expects($this->any())
->method('getParams')
->willReturn([]);
$this->responseMock->expects($this->once())
->method('appendBody')
->willThrowException(new \Exception('exception'));

$jsonResultMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
->disableOriginalConstructor()
->setMethods(['setData'])
->getMock();

$this->resultJsonFactoryMock->expects($this->once())
->method('create')
->willReturn($jsonResultMock);

$jsonResultMock->expects($this->once())
->method('setData')
->willReturnSelf();

$this->loggerMock->expects($this->once())
->method('critical')
->willReturnSelf();

$this->dataProviderMock->expects($this->once())
->method('getConfigData')
->willReturn([]);

$this->uiComponentMock->expects($this->once())
->method('render')
->willReturn($renderedData);
$this->uiComponentMock->expects($this->once())
->method('getChildComponents')
->willReturn([]);
$this->uiComponentMock->expects($this->once())
->method('getContext')
->willReturn($this->uiComponentContextMock);
$this->uiFactoryMock->expects($this->once())
->method('create')
->willReturn($this->uiComponentMock);

$this->render->executeAjaxRequest();
}

public function testExecuteAjaxRequest()
Expand Down
42 changes: 39 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 @@ -371,6 +376,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);
}
});
}
}
});
});
9 changes: 8 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 @@ -21,6 +21,7 @@ define([
return Element.extend({
defaults: {
firstLoad: true,
lastError: false,
storageConfig: {
component: 'Magento_Ui/js/grid/data-storage',
provider: '${ $.storageConfig.name }',
Expand Down Expand Up @@ -120,7 +121,7 @@ define([

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

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

this.set('lastError', true);

this.firstLoad = false;

alert({
content: $t('Something went wrong.')
});
Expand All @@ -157,6 +162,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
Loading

0 comments on commit 4746d64

Please sign in to comment.