Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.3-develop' into MAGETWO-61209-…
Browse files Browse the repository at this point in the history
…calendar-widget-number-of-months

* origin/2.3-develop:
  MAGETWO-91545: Error not found page issue in change status and update attribute from product grid
  MAGETWO-91545: Error not found page issue in change status and update attribute from product grid
  MAGETWO-91545: 404 Error not found page issue in change status and update attribute from product grid
  MAGETWO-91545: 404 Error not found page issue in change status and update attribute from product grid
  MAGETWO-91545: 404 Error not found page issue in change status and update attribute from product grid
  MAGETWO-91545: 404 Error not found page ” issue in change status and update attribute from product grid
  MAGETWO-91545: 404 Error not found page ” issue in change status and update attribute from product grid
  MAGETWO-91545: 404 Error not found page ” issue in change status and update attribute from product grid
  MAGETWO-91545: 404 Error not found page ” issue in change status and update attribute from product grid
  MAGETWO-91545: Error not found page issue in change status and update attribute from product grid
  MAGETWO-91545: 404 Error not found page ” issue in change status and update attribute from product grid
  MAGETWO-91545: 404 Error not found page ” issue in change status and update attribute from product grid
  MAGETWO-91545: 404 Error not found page ” issue in change status and update attribute from product grid
  • Loading branch information
Vasilii Burlacu committed Jun 20, 2018
2 parents 54e1a71 + be6a01f commit e874ff0
Show file tree
Hide file tree
Showing 9 changed files with 400 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Test\Unit\Ui\Component\Product;

use Magento\Catalog\Ui\Component\Product\MassAction;
use Magento\Framework\AuthorizationInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\View\Element\UiComponent\ContextInterface;

class MassActionTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ContextInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $contextMock;

/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var AuthorizationInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $authorizationMock;

/**
* @var MassAction
*/
private $massAction;

protected function setUp()
{
$this->objectManager = new ObjectManager($this);

$this->contextMock = $this->getMockBuilder(ContextInterface::class)
->getMockForAbstractClass();
$this->authorizationMock = $this->getMockBuilder(AuthorizationInterface::class)
->getMockForAbstractClass();

$this->massAction = $this->objectManager->getObject(
MassAction::class,
[
'authorization' => $this->authorizationMock,
'context' => $this->contextMock,
'data' => []
]
);
}

public function testGetComponentName()
{
$this->assertTrue($this->massAction->getComponentName() === MassAction::NAME);
}

/**
* @param string $componentName
* @param array $componentData
* @param bool $isAllowed
* @param bool $expectActionConfig
* @return void
* @dataProvider getPrepareDataProvider
*/
public function testPrepare($componentName, $componentData, $isAllowed = true, $expectActionConfig = true)
{
$processor = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\Processor::class)
->disableOriginalConstructor()
->getMock();
$this->contextMock->expects($this->atLeastOnce())->method('getProcessor')->willReturn($processor);
/** @var \Magento\Ui\Component\MassAction $action */
$action = $this->objectManager->getObject(
\Magento\Ui\Component\MassAction::class,
[
'context' => $this->contextMock,
'data' => [
'name' => $componentName,
'config' => $componentData,
]
]
);
$this->authorizationMock->method('isAllowed')
->willReturn($isAllowed);
$this->massAction->addComponent('action', $action);
$this->massAction->prepare();
$expected = $expectActionConfig ? ['actions' => [$action->getConfiguration()]] : [];
$this->assertEquals($expected, $this->massAction->getConfiguration());
}

/**
* @return array
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function getPrepareDataProvider() : array
{
return [
[
'test_component1',
[
'type' => 'first_action',
'label' => 'First Action',
'url' => '/module/controller/firstAction'
],
],
[
'test_component2',
[
'type' => 'second_action',
'label' => 'Second Action',
'actions' => [
[
'type' => 'second_sub_action1',
'label' => 'Second Sub Action 1',
'url' => '/module/controller/secondSubAction1'
],
[
'type' => 'second_sub_action2',
'label' => 'Second Sub Action 2',
'url' => '/module/controller/secondSubAction2'
],
]
],
],
[
'status_component',
[
'type' => 'status',
'label' => 'Status',
'actions' => [
[
'type' => 'enable',
'label' => 'Second Sub Action 1',
'url' => '/module/controller/enable'
],
[
'type' => 'disable',
'label' => 'Second Sub Action 2',
'url' => '/module/controller/disable'
],
]
],
],
[
'status_component_not_allowed',
[
'type' => 'status',
'label' => 'Status',
'actions' => [
[
'type' => 'enable',
'label' => 'Second Sub Action 1',
'url' => '/module/controller/enable'
],
[
'type' => 'disable',
'label' => 'Second Sub Action 2',
'url' => '/module/controller/disable'
],
]
],
false,
false
],
[
'delete_component',
[
'type' => 'delete',
'label' => 'First Action',
'url' => '/module/controller/delete'
],
],
[
'delete_component_not_allowed',
[
'type' => 'delete',
'label' => 'First Action',
'url' => '/module/controller/delete'
],
false,
false
],
[
'attributes_component',
[
'type' => 'delete',
'label' => 'First Action',
'url' => '/module/controller/attributes'
],
],
[
'attributes_component_not_allowed',
[
'type' => 'delete',
'label' => 'First Action',
'url' => '/module/controller/attributes'
],
false,
false
],
];
}

/**
* @param bool $expected
* @param string $actionType
* @param int $callNum
* @param string $resource
* @param bool $isAllowed
* @dataProvider isActionAllowedDataProvider
*/
public function testIsActionAllowed($expected, $actionType, $callNum, $resource = '', $isAllowed = true)
{
$this->authorizationMock->expects($this->exactly($callNum))
->method('isAllowed')
->with($resource)
->willReturn($isAllowed);

$this->assertEquals($expected, $this->massAction->isActionAllowed($actionType));
}

public function isActionAllowedDataProvider()
{
return [
'other' => [true, 'other', 0,],
'delete-allowed' => [true, 'delete', 1, 'Magento_Catalog::products'],
'delete-not-allowed' => [false, 'delete', 1, 'Magento_Catalog::products', false],
'status-allowed' => [true, 'status', 1, 'Magento_Catalog::products'],
'status-not-allowed' => [false, 'status', 1, 'Magento_Catalog::products', false],
'attributes-allowed' => [true, 'attributes', 1, 'Magento_Catalog::update_attributes'],
'attributes-not-allowed' => [false, 'attributes', 1, 'Magento_Catalog::update_attributes', false],

];
}
}
98 changes: 98 additions & 0 deletions app/code/Magento/Catalog/Ui/Component/Product/MassAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Ui\Component\Product;

use Magento\Framework\AuthorizationInterface;
use Magento\Framework\View\Element\UiComponentInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\AbstractComponent;

class MassAction extends AbstractComponent
{
const NAME = 'massaction';

/**
* @var AuthorizationInterface
*/
private $authorization;

/**
* Constructor
*
* @param AuthorizationInterface $authorization
* @param ContextInterface $context
* @param UiComponentInterface[] $components
* @param array $data
*/
public function __construct(
AuthorizationInterface $authorization,
ContextInterface $context,
array $components = [],
array $data = []
) {
$this->authorization = $authorization;
parent::__construct($context, $components, $data);
}

/**
* {@inheritdoc}
*/
public function prepare() : void
{
$config = $this->getConfiguration();

foreach ($this->getChildComponents() as $actionComponent) {
$actionType = $actionComponent->getConfiguration()['type'];
if ($this->isActionAllowed($actionType)) {
$config['actions'][] = $actionComponent->getConfiguration();
}
}
$origConfig = $this->getConfiguration();
if ($origConfig !== $config) {
$config = array_replace_recursive($config, $origConfig);
}

$this->setData('config', $config);
$this->components = [];

parent::prepare();
}

/**
* {@inheritdoc}
*/
public function getComponentName() : string
{
return static::NAME;
}

/**
* Check if the given type of action is allowed
*
* @param string $actionType
* @return bool
*/
public function isActionAllowed($actionType) : bool
{
$isAllowed = true;
switch ($actionType) {
case 'delete':
$isAllowed = $this->authorization->isAllowed('Magento_Catalog::products');
break;
case 'status':
$isAllowed = $this->authorization->isAllowed('Magento_Catalog::products');
break;
case 'attributes':
$isAllowed = $this->authorization->isAllowed('Magento_Catalog::update_attributes');
break;
default:
break;
}
return $isAllowed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
</settings>
</filterSelect>
</filters>
<massaction name="listing_massaction" component="Magento_Ui/js/grid/tree-massactions">
<massaction name="listing_massaction"
component="Magento_Ui/js/grid/tree-massactions"
class="\Magento\Catalog\Ui\Component\Product\MassAction">
<action name="delete">
<settings>
<confirm>
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Ui/DataProvider/AbstractDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,6 @@ public function setConfigData($config)
*/
public function getAllIds()
{
return $this->collection->getAllIds();
return $this->getCollection()->getAllIds();
}
}
Loading

0 comments on commit e874ff0

Please sign in to comment.