|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Test\Unit\Ui\Component\Product; |
| 9 | + |
| 10 | +use Magento\Catalog\Ui\Component\Product\MassAction; |
| 11 | +use Magento\Framework\AuthorizationInterface; |
| 12 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 13 | +use Magento\Framework\View\Element\UiComponent\ContextInterface; |
| 14 | + |
| 15 | +class MassActionTest extends \PHPUnit\Framework\TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var ContextInterface|\PHPUnit_Framework_MockObject_MockObject |
| 19 | + */ |
| 20 | + private $contextMock; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var ObjectManager |
| 24 | + */ |
| 25 | + private $objectManager; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var AuthorizationInterface|\PHPUnit_Framework_MockObject_MockObject |
| 29 | + */ |
| 30 | + private $authorizationMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var MassAction |
| 34 | + */ |
| 35 | + private $massAction; |
| 36 | + |
| 37 | + protected function setUp() |
| 38 | + { |
| 39 | + $this->objectManager = new ObjectManager($this); |
| 40 | + |
| 41 | + $this->contextMock = $this->getMockBuilder(ContextInterface::class) |
| 42 | + ->getMockForAbstractClass(); |
| 43 | + $this->authorizationMock = $this->getMockBuilder(AuthorizationInterface::class) |
| 44 | + ->getMockForAbstractClass(); |
| 45 | + |
| 46 | + $this->massAction = $this->objectManager->getObject( |
| 47 | + MassAction::class, |
| 48 | + [ |
| 49 | + 'authorization' => $this->authorizationMock, |
| 50 | + 'context' => $this->contextMock, |
| 51 | + 'data' => [] |
| 52 | + ] |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + public function testGetComponentName() |
| 57 | + { |
| 58 | + $this->assertTrue($this->massAction->getComponentName() === MassAction::NAME); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param string $componentName |
| 63 | + * @param array $componentData |
| 64 | + * @param bool $isAllowed |
| 65 | + * @param bool $expectActionConfig |
| 66 | + * @return void |
| 67 | + * @dataProvider getPrepareDataProvider |
| 68 | + */ |
| 69 | + public function testPrepare($componentName, $componentData, $isAllowed = true, $expectActionConfig = true) |
| 70 | + { |
| 71 | + $processor = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\Processor::class) |
| 72 | + ->disableOriginalConstructor() |
| 73 | + ->getMock(); |
| 74 | + $this->contextMock->expects($this->atLeastOnce())->method('getProcessor')->willReturn($processor); |
| 75 | + /** @var \Magento\Ui\Component\MassAction $action */ |
| 76 | + $action = $this->objectManager->getObject( |
| 77 | + \Magento\Ui\Component\MassAction::class, |
| 78 | + [ |
| 79 | + 'context' => $this->contextMock, |
| 80 | + 'data' => [ |
| 81 | + 'name' => $componentName, |
| 82 | + 'config' => $componentData, |
| 83 | + ] |
| 84 | + ] |
| 85 | + ); |
| 86 | + $this->authorizationMock->method('isAllowed') |
| 87 | + ->willReturn($isAllowed); |
| 88 | + $this->massAction->addComponent('action', $action); |
| 89 | + $this->massAction->prepare(); |
| 90 | + $expected = $expectActionConfig ? ['actions' => [$action->getConfiguration()]] : []; |
| 91 | + $this->assertEquals($expected, $this->massAction->getConfiguration()); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @return array |
| 96 | + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
| 97 | + */ |
| 98 | + public function getPrepareDataProvider() : array |
| 99 | + { |
| 100 | + return [ |
| 101 | + [ |
| 102 | + 'test_component1', |
| 103 | + [ |
| 104 | + 'type' => 'first_action', |
| 105 | + 'label' => 'First Action', |
| 106 | + 'url' => '/module/controller/firstAction' |
| 107 | + ], |
| 108 | + ], |
| 109 | + [ |
| 110 | + 'test_component2', |
| 111 | + [ |
| 112 | + 'type' => 'second_action', |
| 113 | + 'label' => 'Second Action', |
| 114 | + 'actions' => [ |
| 115 | + [ |
| 116 | + 'type' => 'second_sub_action1', |
| 117 | + 'label' => 'Second Sub Action 1', |
| 118 | + 'url' => '/module/controller/secondSubAction1' |
| 119 | + ], |
| 120 | + [ |
| 121 | + 'type' => 'second_sub_action2', |
| 122 | + 'label' => 'Second Sub Action 2', |
| 123 | + 'url' => '/module/controller/secondSubAction2' |
| 124 | + ], |
| 125 | + ] |
| 126 | + ], |
| 127 | + ], |
| 128 | + [ |
| 129 | + 'status_component', |
| 130 | + [ |
| 131 | + 'type' => 'status', |
| 132 | + 'label' => 'Status', |
| 133 | + 'actions' => [ |
| 134 | + [ |
| 135 | + 'type' => 'enable', |
| 136 | + 'label' => 'Second Sub Action 1', |
| 137 | + 'url' => '/module/controller/enable' |
| 138 | + ], |
| 139 | + [ |
| 140 | + 'type' => 'disable', |
| 141 | + 'label' => 'Second Sub Action 2', |
| 142 | + 'url' => '/module/controller/disable' |
| 143 | + ], |
| 144 | + ] |
| 145 | + ], |
| 146 | + ], |
| 147 | + [ |
| 148 | + 'status_component_not_allowed', |
| 149 | + [ |
| 150 | + 'type' => 'status', |
| 151 | + 'label' => 'Status', |
| 152 | + 'actions' => [ |
| 153 | + [ |
| 154 | + 'type' => 'enable', |
| 155 | + 'label' => 'Second Sub Action 1', |
| 156 | + 'url' => '/module/controller/enable' |
| 157 | + ], |
| 158 | + [ |
| 159 | + 'type' => 'disable', |
| 160 | + 'label' => 'Second Sub Action 2', |
| 161 | + 'url' => '/module/controller/disable' |
| 162 | + ], |
| 163 | + ] |
| 164 | + ], |
| 165 | + false, |
| 166 | + false |
| 167 | + ], |
| 168 | + [ |
| 169 | + 'delete_component', |
| 170 | + [ |
| 171 | + 'type' => 'delete', |
| 172 | + 'label' => 'First Action', |
| 173 | + 'url' => '/module/controller/delete' |
| 174 | + ], |
| 175 | + ], |
| 176 | + [ |
| 177 | + 'delete_component_not_allowed', |
| 178 | + [ |
| 179 | + 'type' => 'delete', |
| 180 | + 'label' => 'First Action', |
| 181 | + 'url' => '/module/controller/delete' |
| 182 | + ], |
| 183 | + false, |
| 184 | + false |
| 185 | + ], |
| 186 | + [ |
| 187 | + 'attributes_component', |
| 188 | + [ |
| 189 | + 'type' => 'delete', |
| 190 | + 'label' => 'First Action', |
| 191 | + 'url' => '/module/controller/attributes' |
| 192 | + ], |
| 193 | + ], |
| 194 | + [ |
| 195 | + 'attributes_component_not_allowed', |
| 196 | + [ |
| 197 | + 'type' => 'delete', |
| 198 | + 'label' => 'First Action', |
| 199 | + 'url' => '/module/controller/attributes' |
| 200 | + ], |
| 201 | + false, |
| 202 | + false |
| 203 | + ], |
| 204 | + ]; |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * @param bool $expected |
| 209 | + * @param string $actionType |
| 210 | + * @param int $callNum |
| 211 | + * @param string $resource |
| 212 | + * @param bool $isAllowed |
| 213 | + * @dataProvider isActionAllowedDataProvider |
| 214 | + */ |
| 215 | + public function testIsActionAllowed($expected, $actionType, $callNum, $resource = '', $isAllowed = true) |
| 216 | + { |
| 217 | + $this->authorizationMock->expects($this->exactly($callNum)) |
| 218 | + ->method('isAllowed') |
| 219 | + ->with($resource) |
| 220 | + ->willReturn($isAllowed); |
| 221 | + |
| 222 | + $this->assertEquals($expected, $this->massAction->isActionAllowed($actionType)); |
| 223 | + } |
| 224 | + |
| 225 | + public function isActionAllowedDataProvider() |
| 226 | + { |
| 227 | + return [ |
| 228 | + 'other' => [true, 'other', 0,], |
| 229 | + 'delete-allowed' => [true, 'delete', 1, 'Magento_Catalog::products'], |
| 230 | + 'delete-not-allowed' => [false, 'delete', 1, 'Magento_Catalog::products', false], |
| 231 | + 'status-allowed' => [true, 'status', 1, 'Magento_Catalog::products'], |
| 232 | + 'status-not-allowed' => [false, 'status', 1, 'Magento_Catalog::products', false], |
| 233 | + 'attributes-allowed' => [true, 'attributes', 1, 'Magento_Catalog::update_attributes'], |
| 234 | + 'attributes-not-allowed' => [false, 'attributes', 1, 'Magento_Catalog::update_attributes', false], |
| 235 | + |
| 236 | + ]; |
| 237 | + } |
| 238 | +} |
0 commit comments