Skip to content

Commit 82b29aa

Browse files
committed
Removing the delete buttons for default customer groups
1 parent f2aa57e commit 82b29aa

File tree

2 files changed

+288
-23
lines changed

2 files changed

+288
-23
lines changed
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
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+
class GroupActionsTest extends TestCase
22+
{
23+
/**
24+
* @var GroupActions
25+
*/
26+
private $component;
27+
28+
/**
29+
* @var ContextInterface|MockObject
30+
*/
31+
private $contextMock;
32+
33+
/**
34+
* @var UiComponentFactory|MockObject
35+
*/
36+
private $uiComponentFactoryMock;
37+
38+
/**
39+
* @var UrlInterface|MockObject
40+
*/
41+
private $urlBuilderMock;
42+
43+
/**
44+
* @var Escaper|MockObject
45+
*/
46+
private $escaperMock;
47+
48+
/**
49+
* @var GroupManagementInterface|MockObject
50+
*/
51+
private $groupManagementMock;
52+
53+
/**
54+
* Set Up
55+
*/
56+
public function setUp()
57+
{
58+
$objectManager = new ObjectManager($this);
59+
60+
$this->contextMock = $this->getMockBuilder(ContextInterface::class)->getMockForAbstractClass();
61+
$this->uiComponentFactoryMock = $this->createMock(UiComponentFactory::class);
62+
$this->escaperMock = $this->createMock(Escaper::class);
63+
$this->groupManagementMock = $this->createMock(GroupManagementInterface::class);
64+
$this->urlBuilderMock = $this->getMockForAbstractClass(
65+
UrlInterface::class,
66+
[],
67+
'',
68+
false
69+
);
70+
71+
$this->component = $objectManager->getObject(
72+
GroupActions::class,
73+
[
74+
'context' => $this->contextMock,
75+
'uiComponentFactory' => $this->uiComponentFactoryMock,
76+
'urlBuilder' => $this->urlBuilderMock,
77+
'escaper' => $this->escaperMock,
78+
'components' => [],
79+
'data' => [
80+
'name' => 'name'
81+
],
82+
'groupManagement' => $this->groupManagementMock
83+
]
84+
);
85+
}
86+
87+
/**
88+
* Test data source with a non default customer group
89+
*
90+
* @dataProvider customerGroupsDataProvider
91+
*
92+
* @param array $items
93+
* @param bool $isDefaultGroup
94+
* @param array $expected
95+
*/
96+
public function testPrepareDataSourceWithNonDefaultGroup(array $items, bool $isDefaultGroup, array $expected)
97+
{
98+
$customerGroup = 'General';
99+
$dataSource = [
100+
'data' => [
101+
'items' => $items
102+
]
103+
];
104+
$expectedDataSource = [
105+
'data' => [
106+
'items' => $expected
107+
]
108+
];
109+
110+
$this->groupManagementMock->expects($this->any())
111+
->method('isReadonly')
112+
->with(1)
113+
->willReturn($isDefaultGroup);
114+
$this->escaperMock->expects($this->any())
115+
->method('escapeHtml')
116+
->with($customerGroup)
117+
->willReturn($customerGroup);
118+
$this->urlBuilderMock->expects($this->any())
119+
->method('getUrl')
120+
->willReturnMap(
121+
[
122+
['customer/group/edit', ['id' => 1], 'http://magento.com/customer/group/edit'],
123+
['customer/group/delete', ['id' => 1], 'http://magento.com/customer/group/delete']
124+
]
125+
);
126+
127+
$dataSource = $this->component->prepareDataSource($dataSource);
128+
$this->assertEquals($expectedDataSource, $dataSource);
129+
}
130+
131+
/**
132+
* Test data source with a default customer group
133+
*
134+
* @dataProvider customerGroupsDataProvider
135+
*/
136+
public function testPrepareDataSourceWithDefaultGroup()
137+
{
138+
$isDefaultGroup = true;
139+
$dataSource = [
140+
'data' => [
141+
'items' => [
142+
[
143+
'customer_group_id' => 1,
144+
'customer_group_code' => 'General',
145+
],
146+
[
147+
'customer_group_id' => 0,
148+
'customer_group_code' => 'Not Logged In',
149+
],
150+
]
151+
]
152+
];
153+
$expectedDataSource = [
154+
'data' => [
155+
'items' => [
156+
[
157+
'customer_group_id' => 1,
158+
'customer_group_code' => 'General',
159+
'name' => [
160+
'edit' => [
161+
'href' => 'http://magento.com/customer/group/edit',
162+
'label' => __('Edit'),
163+
'__disableTmpl' => true,
164+
]
165+
]
166+
],
167+
[
168+
'customer_group_id' => 0,
169+
'customer_group_code' => 'Not Logged In',
170+
'name' => [
171+
'edit' => [
172+
'href' => 'http://magento.com/customer/group/edit',
173+
'label' => __('Edit'),
174+
'__disableTmpl' => true,
175+
]
176+
]
177+
]
178+
]
179+
]
180+
];
181+
182+
$this->groupManagementMock->expects($this->any())
183+
->method('isReadonly')
184+
->willReturn($isDefaultGroup);
185+
$this->escaperMock->expects($this->any())
186+
->method('escapeHtml')
187+
->willReturnMap(
188+
[
189+
['General', null, 'General'],
190+
['Not Logged In', null, 'Not Logged In']
191+
]
192+
);
193+
$this->urlBuilderMock->expects($this->any())
194+
->method('getUrl')
195+
->willReturnMap(
196+
[
197+
['customer/group/edit', ['id' => 1], 'http://magento.com/customer/group/edit'],
198+
['customer/group/edit', ['id' => 0], 'http://magento.com/customer/group/edit']
199+
]
200+
);
201+
202+
$dataSource = $this->component->prepareDataSource($dataSource);
203+
$this->assertEquals($expectedDataSource, $dataSource);
204+
}
205+
206+
/**
207+
* Providing customer group data
208+
*
209+
* @return array
210+
*/
211+
public function customerGroupsDataProvider(): array
212+
{
213+
return [
214+
[
215+
[
216+
[
217+
'customer_group_id' => 1,
218+
'customer_group_code' => 'General',
219+
],
220+
],
221+
false,
222+
[
223+
[
224+
'customer_group_id' => 1,
225+
'customer_group_code' => 'General',
226+
'name' => [
227+
'edit' => [
228+
'href' => 'http://magento.com/customer/group/edit',
229+
'label' => __('Edit'),
230+
'__disableTmpl' => true,
231+
],
232+
'delete' => [
233+
'href' => 'http://magento.com/customer/group/delete',
234+
'label' => __('Delete'),
235+
'post' => true,
236+
'__disableTmpl' => true,
237+
'confirm' => [
238+
'title' => __('Delete %1', 'General'),
239+
'message' => __(
240+
'Are you sure you want to delete a %1 record?',
241+
'General'
242+
)
243+
],
244+
]
245+
]
246+
]
247+
]
248+
]
249+
];
250+
}
251+
}

app/code/Magento/Customer/Ui/Component/Listing/Column/GroupActions.php

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
namespace Magento\Customer\Ui\Component\Listing\Column;
1010

11+
use Magento\Customer\Api\GroupManagementInterface;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\Exception\NoSuchEntityException;
1115
use Magento\Framework\UrlInterface;
1216
use Magento\Framework\View\Element\UiComponent\ContextInterface;
1317
use Magento\Framework\View\Element\UiComponentFactory;
@@ -16,6 +20,8 @@
1620

1721
/**
1822
* Class GroupActions
23+
*
24+
* Customer Groups actions column
1925
*/
2026
class GroupActions extends Column
2127
{
@@ -25,6 +31,11 @@ class GroupActions extends Column
2531
const URL_PATH_EDIT = 'customer/group/edit';
2632
const URL_PATH_DELETE = 'customer/group/delete';
2733

34+
/**
35+
* @var GroupManagementInterface
36+
*/
37+
private $groupManagement;
38+
2839
/**
2940
* @var UrlInterface
3041
*/
@@ -44,17 +55,21 @@ class GroupActions extends Column
4455
* @param Escaper $escaper
4556
* @param array $components
4657
* @param array $data
58+
* @param GroupManagementInterface $groupManagement
4759
*/
4860
public function __construct(
4961
ContextInterface $context,
5062
UiComponentFactory $uiComponentFactory,
5163
UrlInterface $urlBuilder,
5264
Escaper $escaper,
5365
array $components = [],
54-
array $data = []
66+
array $data = [],
67+
GroupManagementInterface $groupManagement = null
5568
) {
5669
$this->urlBuilder = $urlBuilder;
5770
$this->escaper = $escaper;
71+
$this->groupManagement = $groupManagement ?: ObjectManager::getInstance()->get(GroupManagementInterface::class);;
72+
5873
parent::__construct($context, $uiComponentFactory, $components, $data);
5974
}
6075

@@ -63,6 +78,8 @@ public function __construct(
6378
*
6479
* @param array $dataSource
6580
* @return array
81+
* @throws LocalizedException
82+
* @throws NoSuchEntityException
6683
*/
6784
public function prepareDataSource(array $dataSource)
6885
{
@@ -83,29 +100,26 @@ public function prepareDataSource(array $dataSource)
83100
],
84101
];
85102

86-
// hide delete action for 'NOT LOGGED IN' group
87-
if ($item['customer_group_id'] == 0 && $item['customer_group_code']) {
88-
continue;
103+
if (!$this->groupManagement->isReadonly($item['customer_group_id'])) {
104+
$item[$this->getData('name')]['delete'] = [
105+
'href' => $this->urlBuilder->getUrl(
106+
static::URL_PATH_DELETE,
107+
[
108+
'id' => $item['customer_group_id']
109+
]
110+
),
111+
'label' => __('Delete'),
112+
'confirm' => [
113+
'title' => __('Delete %1', $this->escaper->escapeHtml($title)),
114+
'message' => __(
115+
'Are you sure you want to delete a %1 record?',
116+
$this->escaper->escapeHtml($title)
117+
)
118+
],
119+
'post' => true,
120+
'__disableTmpl' => true
121+
];
89122
}
90-
91-
$item[$this->getData('name')]['delete'] = [
92-
'href' => $this->urlBuilder->getUrl(
93-
static::URL_PATH_DELETE,
94-
[
95-
'id' => $item['customer_group_id']
96-
]
97-
),
98-
'label' => __('Delete'),
99-
'confirm' => [
100-
'title' => __('Delete %1', $this->escaper->escapeHtml($title)),
101-
'message' => __(
102-
'Are you sure you want to delete a %1 record?',
103-
$this->escaper->escapeHtml($title)
104-
)
105-
],
106-
'post' => true,
107-
'__disableTmpl' => true
108-
];
109123
}
110124
}
111125
}

0 commit comments

Comments
 (0)