|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Customer\Test\Unit\Ui\Component\Listing\Column; |
| 7 | + |
| 8 | +use Magento\Customer\Api\GroupManagementInterface; |
| 9 | +use Magento\Customer\Ui\Component\Listing\Column\GroupActions; |
| 10 | +use Magento\Framework\Escaper; |
| 11 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 12 | +use Magento\Framework\UrlInterface; |
| 13 | +use Magento\Framework\View\Element\UiComponent\ContextInterface; |
| 14 | +use Magento\Framework\View\Element\UiComponentFactory; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class GroupActionsTest |
| 20 | + * |
| 21 | + * Testing GroupAction grid column |
| 22 | + */ |
| 23 | +class GroupActionsTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var int |
| 27 | + */ |
| 28 | + private const STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_ID = 0; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + private const STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_NAME = 'Not Logged In'; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var int |
| 37 | + */ |
| 38 | + private const STUB_GENERAL_CUSTOMER_GROUP_ID = 1; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var string |
| 42 | + */ |
| 43 | + private const STUB_GENERAL_CUSTOMER_GROUP_NAME = 'General'; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var string |
| 47 | + */ |
| 48 | + private const STUB_GROUP_EDIT_URL = 'http://magento.com/customer/group/edit'; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var string |
| 52 | + */ |
| 53 | + private const STUB_GROUP_DELETE_URL = 'http://magento.com/customer/group/delete'; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var GroupActions |
| 57 | + */ |
| 58 | + private $component; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var ContextInterface|MockObject |
| 62 | + */ |
| 63 | + private $contextMock; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var UiComponentFactory|MockObject |
| 67 | + */ |
| 68 | + private $uiComponentFactoryMock; |
| 69 | + |
| 70 | + /** |
| 71 | + * @var UrlInterface|MockObject |
| 72 | + */ |
| 73 | + private $urlBuilderMock; |
| 74 | + |
| 75 | + /** |
| 76 | + * @var Escaper|MockObject |
| 77 | + */ |
| 78 | + private $escaperMock; |
| 79 | + |
| 80 | + /** |
| 81 | + * @var GroupManagementInterface|MockObject |
| 82 | + */ |
| 83 | + private $groupManagementMock; |
| 84 | + |
| 85 | + /** |
| 86 | + * Set Up |
| 87 | + */ |
| 88 | + public function setUp() |
| 89 | + { |
| 90 | + $objectManager = new ObjectManager($this); |
| 91 | + |
| 92 | + $this->contextMock = $this->getMockBuilder(ContextInterface::class)->getMockForAbstractClass(); |
| 93 | + $this->uiComponentFactoryMock = $this->createMock(UiComponentFactory::class); |
| 94 | + $this->escaperMock = $this->createMock(Escaper::class); |
| 95 | + $this->groupManagementMock = $this->createMock(GroupManagementInterface::class); |
| 96 | + $this->urlBuilderMock = $this->getMockForAbstractClass( |
| 97 | + UrlInterface::class, |
| 98 | + [], |
| 99 | + '', |
| 100 | + false |
| 101 | + ); |
| 102 | + |
| 103 | + $this->component = $objectManager->getObject( |
| 104 | + GroupActions::class, |
| 105 | + [ |
| 106 | + 'context' => $this->contextMock, |
| 107 | + 'uiComponentFactory' => $this->uiComponentFactoryMock, |
| 108 | + 'urlBuilder' => $this->urlBuilderMock, |
| 109 | + 'escaper' => $this->escaperMock, |
| 110 | + 'components' => [], |
| 111 | + 'data' => [ |
| 112 | + 'name' => 'name' |
| 113 | + ], |
| 114 | + 'groupManagement' => $this->groupManagementMock |
| 115 | + ] |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Test data source with a non default customer group |
| 121 | + * |
| 122 | + * @dataProvider customerGroupsDataProvider |
| 123 | + * |
| 124 | + * @param array $items |
| 125 | + * @param bool $isDefaultGroup |
| 126 | + * @param array $expected |
| 127 | + */ |
| 128 | + public function testPrepareDataSourceWithNonDefaultGroup(array $items, bool $isDefaultGroup, array $expected) |
| 129 | + { |
| 130 | + $dataSource = [ |
| 131 | + 'data' => [ |
| 132 | + 'items' => $items |
| 133 | + ] |
| 134 | + ]; |
| 135 | + $expectedDataSource = [ |
| 136 | + 'data' => [ |
| 137 | + 'items' => $expected |
| 138 | + ] |
| 139 | + ]; |
| 140 | + |
| 141 | + $this->groupManagementMock->expects($this->any()) |
| 142 | + ->method('isReadonly') |
| 143 | + ->with(static::STUB_GENERAL_CUSTOMER_GROUP_ID) |
| 144 | + ->willReturn($isDefaultGroup); |
| 145 | + $this->escaperMock->expects($this->any()) |
| 146 | + ->method('escapeHtml') |
| 147 | + ->with(static::STUB_GENERAL_CUSTOMER_GROUP_NAME) |
| 148 | + ->willReturn(static::STUB_GENERAL_CUSTOMER_GROUP_NAME); |
| 149 | + $this->urlBuilderMock->expects($this->any()) |
| 150 | + ->method('getUrl') |
| 151 | + ->willReturnMap( |
| 152 | + [ |
| 153 | + [ |
| 154 | + 'customer/group/edit', |
| 155 | + [ |
| 156 | + 'id' => static::STUB_GENERAL_CUSTOMER_GROUP_ID |
| 157 | + ], |
| 158 | + static::STUB_GROUP_EDIT_URL], |
| 159 | + [ |
| 160 | + 'customer/group/delete', |
| 161 | + [ |
| 162 | + 'id' => static::STUB_GENERAL_CUSTOMER_GROUP_ID |
| 163 | + ], |
| 164 | + static::STUB_GROUP_DELETE_URL |
| 165 | + ] |
| 166 | + ] |
| 167 | + ); |
| 168 | + |
| 169 | + $dataSource = $this->component->prepareDataSource($dataSource); |
| 170 | + $this->assertEquals($expectedDataSource, $dataSource); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Test data source with a default customer group |
| 175 | + * |
| 176 | + * @dataProvider customerGroupsDataProvider |
| 177 | + */ |
| 178 | + public function testPrepareDataSourceWithDefaultGroup() |
| 179 | + { |
| 180 | + $isDefaultGroup = true; |
| 181 | + $dataSource = [ |
| 182 | + 'data' => [ |
| 183 | + 'items' => [ |
| 184 | + [ |
| 185 | + 'customer_group_id' => static::STUB_GENERAL_CUSTOMER_GROUP_ID, |
| 186 | + 'customer_group_code' => static::STUB_GENERAL_CUSTOMER_GROUP_NAME, |
| 187 | + ], |
| 188 | + [ |
| 189 | + 'customer_group_id' => static::STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_ID, |
| 190 | + 'customer_group_code' => static::STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_NAME, |
| 191 | + ], |
| 192 | + ] |
| 193 | + ] |
| 194 | + ]; |
| 195 | + $expectedDataSource = [ |
| 196 | + 'data' => [ |
| 197 | + 'items' => [ |
| 198 | + [ |
| 199 | + 'customer_group_id' => static::STUB_GENERAL_CUSTOMER_GROUP_ID, |
| 200 | + 'customer_group_code' => static::STUB_GENERAL_CUSTOMER_GROUP_NAME, |
| 201 | + 'name' => [ |
| 202 | + 'edit' => [ |
| 203 | + 'href' => static::STUB_GROUP_EDIT_URL, |
| 204 | + 'label' => __('Edit'), |
| 205 | + '__disableTmpl' => true, |
| 206 | + ] |
| 207 | + ] |
| 208 | + ], |
| 209 | + [ |
| 210 | + 'customer_group_id' => static::STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_ID, |
| 211 | + 'customer_group_code' => static::STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_NAME, |
| 212 | + 'name' => [ |
| 213 | + 'edit' => [ |
| 214 | + 'href' => static::STUB_GROUP_EDIT_URL, |
| 215 | + 'label' => __('Edit'), |
| 216 | + '__disableTmpl' => true, |
| 217 | + ] |
| 218 | + ] |
| 219 | + ] |
| 220 | + ] |
| 221 | + ] |
| 222 | + ]; |
| 223 | + |
| 224 | + $this->groupManagementMock->expects($this->any()) |
| 225 | + ->method('isReadonly') |
| 226 | + ->willReturn($isDefaultGroup); |
| 227 | + $this->escaperMock->expects($this->any()) |
| 228 | + ->method('escapeHtml') |
| 229 | + ->willReturnMap( |
| 230 | + [ |
| 231 | + [ |
| 232 | + static::STUB_GENERAL_CUSTOMER_GROUP_NAME, |
| 233 | + null, |
| 234 | + static::STUB_GENERAL_CUSTOMER_GROUP_NAME |
| 235 | + ], |
| 236 | + [ |
| 237 | + static::STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_NAME, |
| 238 | + null, |
| 239 | + static::STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_NAME |
| 240 | + ] |
| 241 | + ] |
| 242 | + ); |
| 243 | + $this->urlBuilderMock->expects($this->any()) |
| 244 | + ->method('getUrl') |
| 245 | + ->willReturnMap( |
| 246 | + [ |
| 247 | + [ |
| 248 | + 'customer/group/edit', |
| 249 | + [ |
| 250 | + 'id' => static::STUB_GENERAL_CUSTOMER_GROUP_ID |
| 251 | + ], |
| 252 | + static::STUB_GROUP_EDIT_URL |
| 253 | + ], |
| 254 | + [ |
| 255 | + 'customer/group/edit', |
| 256 | + [ |
| 257 | + 'id' => static::STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_ID |
| 258 | + ], |
| 259 | + static::STUB_GROUP_EDIT_URL |
| 260 | + ] |
| 261 | + ] |
| 262 | + ); |
| 263 | + |
| 264 | + $dataSource = $this->component->prepareDataSource($dataSource); |
| 265 | + $this->assertEquals($expectedDataSource, $dataSource); |
| 266 | + } |
| 267 | + |
| 268 | + /** |
| 269 | + * Providing customer group data |
| 270 | + * |
| 271 | + * @return array |
| 272 | + */ |
| 273 | + public function customerGroupsDataProvider(): array |
| 274 | + { |
| 275 | + return [ |
| 276 | + [ |
| 277 | + [ |
| 278 | + [ |
| 279 | + 'customer_group_id' => static::STUB_GENERAL_CUSTOMER_GROUP_ID, |
| 280 | + 'customer_group_code' => static::STUB_GENERAL_CUSTOMER_GROUP_NAME, |
| 281 | + ], |
| 282 | + ], |
| 283 | + false, |
| 284 | + [ |
| 285 | + [ |
| 286 | + 'customer_group_id' => static::STUB_GENERAL_CUSTOMER_GROUP_ID, |
| 287 | + 'customer_group_code' => static::STUB_GENERAL_CUSTOMER_GROUP_NAME, |
| 288 | + 'name' => [ |
| 289 | + 'edit' => [ |
| 290 | + 'href' => static::STUB_GROUP_EDIT_URL, |
| 291 | + 'label' => __('Edit'), |
| 292 | + '__disableTmpl' => true, |
| 293 | + ], |
| 294 | + 'delete' => [ |
| 295 | + 'href' => static::STUB_GROUP_DELETE_URL, |
| 296 | + 'label' => __('Delete'), |
| 297 | + 'post' => true, |
| 298 | + '__disableTmpl' => true, |
| 299 | + 'confirm' => [ |
| 300 | + 'title' => __('Delete %1', 'General'), |
| 301 | + 'message' => __( |
| 302 | + 'Are you sure you want to delete a %1 record?', |
| 303 | + 'General' |
| 304 | + ) |
| 305 | + ], |
| 306 | + ] |
| 307 | + ] |
| 308 | + ] |
| 309 | + ] |
| 310 | + ] |
| 311 | + ]; |
| 312 | + } |
| 313 | +} |
0 commit comments