From 82b29aac0789f2c2a5e0aad598df3a472c00d759 Mon Sep 17 00:00:00 2001 From: eduard13 Date: Fri, 3 Jan 2020 14:58:20 +0200 Subject: [PATCH 1/4] Removing the delete buttons for default customer groups --- .../Listing/Column/GroupActionsTest.php | 251 ++++++++++++++++++ .../Component/Listing/Column/GroupActions.php | 60 +++-- 2 files changed, 288 insertions(+), 23 deletions(-) create mode 100644 app/code/Magento/Customer/Test/Unit/Ui/Component/Listing/Column/GroupActionsTest.php diff --git a/app/code/Magento/Customer/Test/Unit/Ui/Component/Listing/Column/GroupActionsTest.php b/app/code/Magento/Customer/Test/Unit/Ui/Component/Listing/Column/GroupActionsTest.php new file mode 100644 index 0000000000000..51e652b15f53f --- /dev/null +++ b/app/code/Magento/Customer/Test/Unit/Ui/Component/Listing/Column/GroupActionsTest.php @@ -0,0 +1,251 @@ +contextMock = $this->getMockBuilder(ContextInterface::class)->getMockForAbstractClass(); + $this->uiComponentFactoryMock = $this->createMock(UiComponentFactory::class); + $this->escaperMock = $this->createMock(Escaper::class); + $this->groupManagementMock = $this->createMock(GroupManagementInterface::class); + $this->urlBuilderMock = $this->getMockForAbstractClass( + UrlInterface::class, + [], + '', + false + ); + + $this->component = $objectManager->getObject( + GroupActions::class, + [ + 'context' => $this->contextMock, + 'uiComponentFactory' => $this->uiComponentFactoryMock, + 'urlBuilder' => $this->urlBuilderMock, + 'escaper' => $this->escaperMock, + 'components' => [], + 'data' => [ + 'name' => 'name' + ], + 'groupManagement' => $this->groupManagementMock + ] + ); + } + + /** + * Test data source with a non default customer group + * + * @dataProvider customerGroupsDataProvider + * + * @param array $items + * @param bool $isDefaultGroup + * @param array $expected + */ + public function testPrepareDataSourceWithNonDefaultGroup(array $items, bool $isDefaultGroup, array $expected) + { + $customerGroup = 'General'; + $dataSource = [ + 'data' => [ + 'items' => $items + ] + ]; + $expectedDataSource = [ + 'data' => [ + 'items' => $expected + ] + ]; + + $this->groupManagementMock->expects($this->any()) + ->method('isReadonly') + ->with(1) + ->willReturn($isDefaultGroup); + $this->escaperMock->expects($this->any()) + ->method('escapeHtml') + ->with($customerGroup) + ->willReturn($customerGroup); + $this->urlBuilderMock->expects($this->any()) + ->method('getUrl') + ->willReturnMap( + [ + ['customer/group/edit', ['id' => 1], 'http://magento.com/customer/group/edit'], + ['customer/group/delete', ['id' => 1], 'http://magento.com/customer/group/delete'] + ] + ); + + $dataSource = $this->component->prepareDataSource($dataSource); + $this->assertEquals($expectedDataSource, $dataSource); + } + + /** + * Test data source with a default customer group + * + * @dataProvider customerGroupsDataProvider + */ + public function testPrepareDataSourceWithDefaultGroup() + { + $isDefaultGroup = true; + $dataSource = [ + 'data' => [ + 'items' => [ + [ + 'customer_group_id' => 1, + 'customer_group_code' => 'General', + ], + [ + 'customer_group_id' => 0, + 'customer_group_code' => 'Not Logged In', + ], + ] + ] + ]; + $expectedDataSource = [ + 'data' => [ + 'items' => [ + [ + 'customer_group_id' => 1, + 'customer_group_code' => 'General', + 'name' => [ + 'edit' => [ + 'href' => 'http://magento.com/customer/group/edit', + 'label' => __('Edit'), + '__disableTmpl' => true, + ] + ] + ], + [ + 'customer_group_id' => 0, + 'customer_group_code' => 'Not Logged In', + 'name' => [ + 'edit' => [ + 'href' => 'http://magento.com/customer/group/edit', + 'label' => __('Edit'), + '__disableTmpl' => true, + ] + ] + ] + ] + ] + ]; + + $this->groupManagementMock->expects($this->any()) + ->method('isReadonly') + ->willReturn($isDefaultGroup); + $this->escaperMock->expects($this->any()) + ->method('escapeHtml') + ->willReturnMap( + [ + ['General', null, 'General'], + ['Not Logged In', null, 'Not Logged In'] + ] + ); + $this->urlBuilderMock->expects($this->any()) + ->method('getUrl') + ->willReturnMap( + [ + ['customer/group/edit', ['id' => 1], 'http://magento.com/customer/group/edit'], + ['customer/group/edit', ['id' => 0], 'http://magento.com/customer/group/edit'] + ] + ); + + $dataSource = $this->component->prepareDataSource($dataSource); + $this->assertEquals($expectedDataSource, $dataSource); + } + + /** + * Providing customer group data + * + * @return array + */ + public function customerGroupsDataProvider(): array + { + return [ + [ + [ + [ + 'customer_group_id' => 1, + 'customer_group_code' => 'General', + ], + ], + false, + [ + [ + 'customer_group_id' => 1, + 'customer_group_code' => 'General', + 'name' => [ + 'edit' => [ + 'href' => 'http://magento.com/customer/group/edit', + 'label' => __('Edit'), + '__disableTmpl' => true, + ], + 'delete' => [ + 'href' => 'http://magento.com/customer/group/delete', + 'label' => __('Delete'), + 'post' => true, + '__disableTmpl' => true, + 'confirm' => [ + 'title' => __('Delete %1', 'General'), + 'message' => __( + 'Are you sure you want to delete a %1 record?', + 'General' + ) + ], + ] + ] + ] + ] + ] + ]; + } +} diff --git a/app/code/Magento/Customer/Ui/Component/Listing/Column/GroupActions.php b/app/code/Magento/Customer/Ui/Component/Listing/Column/GroupActions.php index 5d974088b0d54..5f575aacb2e10 100644 --- a/app/code/Magento/Customer/Ui/Component/Listing/Column/GroupActions.php +++ b/app/code/Magento/Customer/Ui/Component/Listing/Column/GroupActions.php @@ -8,6 +8,10 @@ namespace Magento\Customer\Ui\Component\Listing\Column; +use Magento\Customer\Api\GroupManagementInterface; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\UrlInterface; use Magento\Framework\View\Element\UiComponent\ContextInterface; use Magento\Framework\View\Element\UiComponentFactory; @@ -16,6 +20,8 @@ /** * Class GroupActions + * + * Customer Groups actions column */ class GroupActions extends Column { @@ -25,6 +31,11 @@ class GroupActions extends Column const URL_PATH_EDIT = 'customer/group/edit'; const URL_PATH_DELETE = 'customer/group/delete'; + /** + * @var GroupManagementInterface + */ + private $groupManagement; + /** * @var UrlInterface */ @@ -44,6 +55,7 @@ class GroupActions extends Column * @param Escaper $escaper * @param array $components * @param array $data + * @param GroupManagementInterface $groupManagement */ public function __construct( ContextInterface $context, @@ -51,10 +63,13 @@ public function __construct( UrlInterface $urlBuilder, Escaper $escaper, array $components = [], - array $data = [] + array $data = [], + GroupManagementInterface $groupManagement = null ) { $this->urlBuilder = $urlBuilder; $this->escaper = $escaper; + $this->groupManagement = $groupManagement ?: ObjectManager::getInstance()->get(GroupManagementInterface::class);; + parent::__construct($context, $uiComponentFactory, $components, $data); } @@ -63,6 +78,8 @@ public function __construct( * * @param array $dataSource * @return array + * @throws LocalizedException + * @throws NoSuchEntityException */ public function prepareDataSource(array $dataSource) { @@ -83,29 +100,26 @@ public function prepareDataSource(array $dataSource) ], ]; - // hide delete action for 'NOT LOGGED IN' group - if ($item['customer_group_id'] == 0 && $item['customer_group_code']) { - continue; + if (!$this->groupManagement->isReadonly($item['customer_group_id'])) { + $item[$this->getData('name')]['delete'] = [ + 'href' => $this->urlBuilder->getUrl( + static::URL_PATH_DELETE, + [ + 'id' => $item['customer_group_id'] + ] + ), + 'label' => __('Delete'), + 'confirm' => [ + 'title' => __('Delete %1', $this->escaper->escapeHtml($title)), + 'message' => __( + 'Are you sure you want to delete a %1 record?', + $this->escaper->escapeHtml($title) + ) + ], + 'post' => true, + '__disableTmpl' => true + ]; } - - $item[$this->getData('name')]['delete'] = [ - 'href' => $this->urlBuilder->getUrl( - static::URL_PATH_DELETE, - [ - 'id' => $item['customer_group_id'] - ] - ), - 'label' => __('Delete'), - 'confirm' => [ - 'title' => __('Delete %1', $this->escaper->escapeHtml($title)), - 'message' => __( - 'Are you sure you want to delete a %1 record?', - $this->escaper->escapeHtml($title) - ) - ], - 'post' => true, - '__disableTmpl' => true - ]; } } } From 198f6e00afa8fa93f7e4927f9edccf43334f5569 Mon Sep 17 00:00:00 2001 From: eduard13 Date: Fri, 3 Jan 2020 15:09:43 +0200 Subject: [PATCH 2/4] Static tests --- .../Test/Unit/Ui/Component/Listing/Column/GroupActionsTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/code/Magento/Customer/Test/Unit/Ui/Component/Listing/Column/GroupActionsTest.php b/app/code/Magento/Customer/Test/Unit/Ui/Component/Listing/Column/GroupActionsTest.php index 51e652b15f53f..51cf0e5395b47 100644 --- a/app/code/Magento/Customer/Test/Unit/Ui/Component/Listing/Column/GroupActionsTest.php +++ b/app/code/Magento/Customer/Test/Unit/Ui/Component/Listing/Column/GroupActionsTest.php @@ -17,6 +17,8 @@ /** * Class GroupActionsTest + * + * Testing GroupAction grid column */ class GroupActionsTest extends TestCase { From 86ff8fa07a878a10c5b188173a6a0b1d4ad00e72 Mon Sep 17 00:00:00 2001 From: eduard13 Date: Fri, 3 Jan 2020 15:39:29 +0200 Subject: [PATCH 3/4] Static tests --- .../Customer/Ui/Component/Listing/Column/GroupActions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Customer/Ui/Component/Listing/Column/GroupActions.php b/app/code/Magento/Customer/Ui/Component/Listing/Column/GroupActions.php index 5f575aacb2e10..e5a536dc6ecd6 100644 --- a/app/code/Magento/Customer/Ui/Component/Listing/Column/GroupActions.php +++ b/app/code/Magento/Customer/Ui/Component/Listing/Column/GroupActions.php @@ -68,7 +68,7 @@ public function __construct( ) { $this->urlBuilder = $urlBuilder; $this->escaper = $escaper; - $this->groupManagement = $groupManagement ?: ObjectManager::getInstance()->get(GroupManagementInterface::class);; + $this->groupManagement = $groupManagement ?: ObjectManager::getInstance()->get(GroupManagementInterface::class); parent::__construct($context, $uiComponentFactory, $components, $data); } From 5d0e12bd42e1c19b718f2e22f246db039e015520 Mon Sep 17 00:00:00 2001 From: eduard13 Date: Wed, 8 Jan 2020 09:26:02 +0200 Subject: [PATCH 4/4] Adjusting the Unit Test --- .../Listing/Column/GroupActionsTest.php | 112 ++++++++++++++---- 1 file changed, 86 insertions(+), 26 deletions(-) diff --git a/app/code/Magento/Customer/Test/Unit/Ui/Component/Listing/Column/GroupActionsTest.php b/app/code/Magento/Customer/Test/Unit/Ui/Component/Listing/Column/GroupActionsTest.php index 51cf0e5395b47..02cacea5c2601 100644 --- a/app/code/Magento/Customer/Test/Unit/Ui/Component/Listing/Column/GroupActionsTest.php +++ b/app/code/Magento/Customer/Test/Unit/Ui/Component/Listing/Column/GroupActionsTest.php @@ -22,6 +22,36 @@ */ class GroupActionsTest extends TestCase { + /** + * @var int + */ + private const STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_ID = 0; + + /** + * @var string + */ + private const STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_NAME = 'Not Logged In'; + + /** + * @var int + */ + private const STUB_GENERAL_CUSTOMER_GROUP_ID = 1; + + /** + * @var string + */ + private const STUB_GENERAL_CUSTOMER_GROUP_NAME = 'General'; + + /** + * @var string + */ + private const STUB_GROUP_EDIT_URL = 'http://magento.com/customer/group/edit'; + + /** + * @var string + */ + private const STUB_GROUP_DELETE_URL = 'http://magento.com/customer/group/delete'; + /** * @var GroupActions */ @@ -97,7 +127,6 @@ public function setUp() */ public function testPrepareDataSourceWithNonDefaultGroup(array $items, bool $isDefaultGroup, array $expected) { - $customerGroup = 'General'; $dataSource = [ 'data' => [ 'items' => $items @@ -111,18 +140,29 @@ public function testPrepareDataSourceWithNonDefaultGroup(array $items, bool $isD $this->groupManagementMock->expects($this->any()) ->method('isReadonly') - ->with(1) + ->with(static::STUB_GENERAL_CUSTOMER_GROUP_ID) ->willReturn($isDefaultGroup); $this->escaperMock->expects($this->any()) ->method('escapeHtml') - ->with($customerGroup) - ->willReturn($customerGroup); + ->with(static::STUB_GENERAL_CUSTOMER_GROUP_NAME) + ->willReturn(static::STUB_GENERAL_CUSTOMER_GROUP_NAME); $this->urlBuilderMock->expects($this->any()) ->method('getUrl') ->willReturnMap( [ - ['customer/group/edit', ['id' => 1], 'http://magento.com/customer/group/edit'], - ['customer/group/delete', ['id' => 1], 'http://magento.com/customer/group/delete'] + [ + 'customer/group/edit', + [ + 'id' => static::STUB_GENERAL_CUSTOMER_GROUP_ID + ], + static::STUB_GROUP_EDIT_URL], + [ + 'customer/group/delete', + [ + 'id' => static::STUB_GENERAL_CUSTOMER_GROUP_ID + ], + static::STUB_GROUP_DELETE_URL + ] ] ); @@ -142,12 +182,12 @@ public function testPrepareDataSourceWithDefaultGroup() 'data' => [ 'items' => [ [ - 'customer_group_id' => 1, - 'customer_group_code' => 'General', + 'customer_group_id' => static::STUB_GENERAL_CUSTOMER_GROUP_ID, + 'customer_group_code' => static::STUB_GENERAL_CUSTOMER_GROUP_NAME, ], [ - 'customer_group_id' => 0, - 'customer_group_code' => 'Not Logged In', + 'customer_group_id' => static::STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_ID, + 'customer_group_code' => static::STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_NAME, ], ] ] @@ -156,22 +196,22 @@ public function testPrepareDataSourceWithDefaultGroup() 'data' => [ 'items' => [ [ - 'customer_group_id' => 1, - 'customer_group_code' => 'General', + 'customer_group_id' => static::STUB_GENERAL_CUSTOMER_GROUP_ID, + 'customer_group_code' => static::STUB_GENERAL_CUSTOMER_GROUP_NAME, 'name' => [ 'edit' => [ - 'href' => 'http://magento.com/customer/group/edit', + 'href' => static::STUB_GROUP_EDIT_URL, 'label' => __('Edit'), '__disableTmpl' => true, ] ] ], [ - 'customer_group_id' => 0, - 'customer_group_code' => 'Not Logged In', + 'customer_group_id' => static::STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_ID, + 'customer_group_code' => static::STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_NAME, 'name' => [ 'edit' => [ - 'href' => 'http://magento.com/customer/group/edit', + 'href' => static::STUB_GROUP_EDIT_URL, 'label' => __('Edit'), '__disableTmpl' => true, ] @@ -188,16 +228,36 @@ public function testPrepareDataSourceWithDefaultGroup() ->method('escapeHtml') ->willReturnMap( [ - ['General', null, 'General'], - ['Not Logged In', null, 'Not Logged In'] + [ + static::STUB_GENERAL_CUSTOMER_GROUP_NAME, + null, + static::STUB_GENERAL_CUSTOMER_GROUP_NAME + ], + [ + static::STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_NAME, + null, + static::STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_NAME + ] ] ); $this->urlBuilderMock->expects($this->any()) ->method('getUrl') ->willReturnMap( [ - ['customer/group/edit', ['id' => 1], 'http://magento.com/customer/group/edit'], - ['customer/group/edit', ['id' => 0], 'http://magento.com/customer/group/edit'] + [ + 'customer/group/edit', + [ + 'id' => static::STUB_GENERAL_CUSTOMER_GROUP_ID + ], + static::STUB_GROUP_EDIT_URL + ], + [ + 'customer/group/edit', + [ + 'id' => static::STUB_NOT_LOGGED_IN_CUSTOMER_GROUP_ID + ], + static::STUB_GROUP_EDIT_URL + ] ] ); @@ -216,23 +276,23 @@ public function customerGroupsDataProvider(): array [ [ [ - 'customer_group_id' => 1, - 'customer_group_code' => 'General', + 'customer_group_id' => static::STUB_GENERAL_CUSTOMER_GROUP_ID, + 'customer_group_code' => static::STUB_GENERAL_CUSTOMER_GROUP_NAME, ], ], false, [ [ - 'customer_group_id' => 1, - 'customer_group_code' => 'General', + 'customer_group_id' => static::STUB_GENERAL_CUSTOMER_GROUP_ID, + 'customer_group_code' => static::STUB_GENERAL_CUSTOMER_GROUP_NAME, 'name' => [ 'edit' => [ - 'href' => 'http://magento.com/customer/group/edit', + 'href' => static::STUB_GROUP_EDIT_URL, 'label' => __('Edit'), '__disableTmpl' => true, ], 'delete' => [ - 'href' => 'http://magento.com/customer/group/delete', + 'href' => static::STUB_GROUP_DELETE_URL, 'label' => __('Delete'), 'post' => true, '__disableTmpl' => true,