Skip to content

Commit aa03171

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-98157' into borg-qwerty-2.1
2 parents 2633d5f + bbdf0b8 commit aa03171

File tree

5 files changed

+154
-30
lines changed

5 files changed

+154
-30
lines changed

app/code/Magento/User/Block/Role/Grid/User.php

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ protected function _construct()
8181
}
8282

8383
/**
84+
* Adds column filter to collection
85+
*
8486
* @param Column $column
8587
* @return $this
8688
*/
@@ -105,6 +107,8 @@ protected function _addColumnFilterToCollection($column)
105107
}
106108

107109
/**
110+
* Prepares collection
111+
*
108112
* @return $this
109113
*/
110114
protected function _prepareCollection()
@@ -117,6 +121,8 @@ protected function _prepareCollection()
117121
}
118122

119123
/**
124+
* Prepares columns
125+
*
120126
* @return $this
121127
*/
122128
protected function _prepareColumns()
@@ -173,52 +179,51 @@ protected function _prepareColumns()
173179
}
174180

175181
/**
182+
* Gets grid url
183+
*
176184
* @return string
185+
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
177186
*/
178187
public function getGridUrl()
179188
{
180-
$roleId = $this->getRequest()->getParam('rid');
189+
$roleId = $this->escapeHtml($this->getRequest()->getParam('rid'));
181190
return $this->getUrl('*/*/editrolegrid', ['rid' => $roleId]);
182191
}
183192

184193
/**
194+
* Gets users
195+
*
185196
* @param bool $json
186197
* @return string|array
187198
*/
188199
public function getUsers($json = false)
189200
{
190-
if ($this->getRequest()->getParam('in_role_user') != "") {
191-
return $this->getRequest()->getParam('in_role_user');
201+
$inRoleUser = $this->getRequest()->getParam('in_role_user');
202+
if ($inRoleUser) {
203+
if ($json) {
204+
return $this->getJSONString($inRoleUser);
205+
}
206+
return $this->_escaper->escapeJs($this->escapeHtml($inRoleUser));
192207
}
193-
$roleId = $this->getRequest()->getParam(
194-
'rid'
195-
) > 0 ? $this->getRequest()->getParam(
196-
'rid'
197-
) : $this->_coreRegistry->registry(
198-
'RID'
199-
);
200-
208+
$roleId = $this->getRoleId();
201209
$users = $this->getUsersFormData();
202210
if (false === $users) {
203211
$users = $this->_roleFactory->create()->setId($roleId)->getRoleUsers();
204212
}
205-
if (sizeof($users) > 0) {
213+
if (!empty($users)) {
206214
if ($json) {
207215
$jsonUsers = [];
208-
foreach ($users as $usrid) {
209-
$jsonUsers[$usrid] = 0;
216+
foreach ($users as $userid) {
217+
$jsonUsers[$userid] = 0;
210218
}
211219
return $this->_jsonEncoder->encode((object)$jsonUsers);
212-
} else {
213-
return array_values($users);
214-
}
215-
} else {
216-
if ($json) {
217-
return '{}';
218-
} else {
219-
return [];
220220
}
221+
return array_values($users);
222+
}
223+
if ($json) {
224+
return '{}';
221225
}
226+
return [];
222227
}
223228

224229
/**
@@ -239,6 +244,7 @@ protected function getUsersFormData()
239244
* Restore Users Form Data from the registry
240245
*
241246
* @return array|bool
247+
* @SuppressWarnings(PHPMD.DiscouragedFunctionsSniff)
242248
*/
243249
protected function restoreUsersFormData()
244250
{
@@ -252,4 +258,30 @@ protected function restoreUsersFormData()
252258

253259
return false;
254260
}
261+
262+
/**
263+
* Gets role ID
264+
*
265+
* @return string
266+
*/
267+
private function getRoleId()
268+
{
269+
$roleId = $this->getRequest()->getParam('rid');
270+
if ($roleId <= 0) {
271+
$roleId = $this->_coreRegistry->registry('RID');
272+
}
273+
return $roleId;
274+
}
275+
276+
/**
277+
* Gets JSON string
278+
*
279+
* @param string $input
280+
* @return string
281+
*/
282+
private function getJSONString($input)
283+
{
284+
$output = json_decode($input);
285+
return $output ? $this->_jsonEncoder->encode($output) : '{}';
286+
}
255287
}

app/code/Magento/User/Block/User/Edit/Tab/Roles.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
use Magento\Backend\Block\Widget\Grid\Column;
99

10+
/**
11+
* Roles grid
12+
*/
1013
class Roles extends \Magento\Backend\Block\Widget\Grid\Extended
1114
{
1215
/**
@@ -64,6 +67,8 @@ protected function _construct()
6467
}
6568

6669
/**
70+
* Adds column filter to collection
71+
*
6772
* @param Column $column
6873
* @return $this
6974
*/
@@ -88,6 +93,8 @@ protected function _addColumnFilterToCollection($column)
8893
}
8994

9095
/**
96+
* Prepares collection
97+
*
9198
* @return $this
9299
*/
93100
protected function _prepareCollection()
@@ -99,6 +106,8 @@ protected function _prepareCollection()
99106
}
100107

101108
/**
109+
* Prepares columns
110+
*
102111
* @return $this
103112
*/
104113
protected function _prepareColumns()
@@ -122,6 +131,8 @@ protected function _prepareColumns()
122131
}
123132

124133
/**
134+
* Gets grid url
135+
*
125136
* @return string
126137
*/
127138
public function getGridUrl()
@@ -131,13 +142,20 @@ public function getGridUrl()
131142
}
132143

133144
/**
145+
* Get selected roles
146+
*
134147
* @param bool $json
135148
* @return array|string
136149
*/
137150
public function getSelectedRoles($json = false)
138151
{
139-
if ($this->getRequest()->getParam('user_roles') != "") {
140-
return $this->getRequest()->getParam('user_roles');
152+
$userRoles = $this->getRequest()->getParam('user_roles');
153+
if ($userRoles) {
154+
if ($json) {
155+
$result = json_decode($userRoles);
156+
return $result ? $this->_jsonEncoder->encode($result) : '{}';
157+
}
158+
return $this->_escaper->escapeJs($this->escapeHtml($userRoles));
141159
}
142160
/* @var $user \Magento\User\Model\User */
143161
$user = $this->_coreRegistry->registry('permissions_user');

app/code/Magento/User/Test/Unit/Block/Role/Grid/UserTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ public function testGetUsersPositiveNumberOfRolesAndJsonFalse()
130130

131131
$this->requestInterfaceMock->expects($this->at(0))->method('getParam')->willReturn("");
132132
$this->requestInterfaceMock->expects($this->at(1))->method('getParam')->willReturn($roleId);
133-
$this->requestInterfaceMock->expects($this->at(2))->method('getParam')->willReturn($roleId);
134133

135134
$this->registryMock->expects($this->once())
136135
->method('registry')
@@ -157,7 +156,6 @@ public function testGetUsersPositiveNumberOfRolesAndJsonTrue()
157156

158157
$this->requestInterfaceMock->expects($this->at(0))->method('getParam')->willReturn("");
159158
$this->requestInterfaceMock->expects($this->at(1))->method('getParam')->willReturn($roleId);
160-
$this->requestInterfaceMock->expects($this->at(2))->method('getParam')->willReturn($roleId);
161159

162160
$this->registryMock->expects($this->once())
163161
->method('registry')
@@ -182,7 +180,6 @@ public function testGetUsersNoRolesAndJsonFalse()
182180

183181
$this->requestInterfaceMock->expects($this->at(0))->method('getParam')->willReturn("");
184182
$this->requestInterfaceMock->expects($this->at(1))->method('getParam')->willReturn($roleId);
185-
$this->requestInterfaceMock->expects($this->at(2))->method('getParam')->willReturn($roleId);
186183

187184
$this->registryMock->expects($this->once())
188185
->method('registry')
@@ -238,4 +235,21 @@ public function testPrepareColumns()
238235

239236
$this->model->toHtml();
240237
}
238+
239+
public function testGetUsersCorrectInRoleUser()
240+
{
241+
$param = 'in_role_user';
242+
$paramValue = '{"a":"role1","1":"role2","2":"role3"}';
243+
$this->requestInterfaceMock->expects($this->once())->method('getParam')->with($param)->willReturn($paramValue);
244+
$this->jsonEncoderMock->expects($this->once())->method('encode')->willReturn($paramValue);
245+
$this->assertEquals($paramValue, $this->model->getUsers(true));
246+
}
247+
248+
public function testGetUsersIncorrectInRoleUser()
249+
{
250+
$param = 'in_role_user';
251+
$paramValue = 'not_JSON';
252+
$this->requestInterfaceMock->expects($this->once())->method('getParam')->with($param)->willReturn($paramValue);
253+
$this->assertEquals('{}', $this->model->getUsers(true));
254+
}
241255
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\User\Test\Unit\Block\User\Edit\Tab;
8+
9+
/**
10+
* Class UserTest to cover Magento\User\Block\User\Edit\Tab\Roles
11+
*
12+
*/
13+
class RolesTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/** @var \Magento\User\Block\User\Edit\Tab\Roles */
16+
protected $model;
17+
18+
/** @var \Magento\Framework\Json\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject */
19+
protected $jsonEncoderMock;
20+
21+
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
22+
protected $requestInterfaceMock;
23+
24+
protected function setUp()
25+
{
26+
$this->jsonEncoderMock = $this->getMockBuilder('Magento\Framework\Json\EncoderInterface')
27+
->disableOriginalConstructor()
28+
->setMethods([])
29+
->getMock();
30+
31+
$this->requestInterfaceMock = $this->getMockBuilder('Magento\Framework\App\RequestInterface')
32+
->disableOriginalConstructor()
33+
->setMethods([])
34+
->getMock();
35+
36+
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
37+
$this->model = $objectManagerHelper->getObject(
38+
'Magento\User\Block\User\Edit\Tab\Roles',
39+
[
40+
'jsonEncoder' => $this->jsonEncoderMock,
41+
'request' => $this->requestInterfaceMock,
42+
]
43+
);
44+
}
45+
46+
public function testSelectedRolesCorrectUserRoles()
47+
{
48+
$param = 'user_roles';
49+
$paramValue = '{"a":"role1","1":"role2","2":"role3"}';
50+
$this->requestInterfaceMock->expects($this->once())->method('getParam')->with($param)->willReturn($paramValue);
51+
$this->jsonEncoderMock->expects($this->once())->method('encode')->willReturn($paramValue);
52+
$this->assertEquals($paramValue, $this->model->getSelectedRoles(true));
53+
}
54+
55+
public function testSelectedRolesIncorrectInRoleUser()
56+
{
57+
$param = 'user_roles';
58+
$paramValue = 'not_JSON';
59+
$this->requestInterfaceMock->expects($this->once())->method('getParam')->with($param)->willReturn($paramValue);
60+
$this->assertEquals('{}', $this->model->getSelectedRoles(true));
61+
}
62+
}

app/code/Magento/User/view/adminhtml/templates/role/users_grid_js.phtml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ require([
1414
'mage/adminhtml/grid',
1515
'prototype'
1616
], function(jQuery, confirm, _){
17-
<!--
1817
<?php $myBlock = $block->getLayout()->getBlock('roleUsersGrid'); ?>
1918
<?php if (is_object($myBlock) && $myBlock->getJsObjectName()): ?>
20-
var checkBoxes = $H(<?php /* @escapeNotVerified */ echo $myBlock->getUsers(true) ?>);
19+
var checkBoxes = $H(<?= /* @escapeNotVerified */ $myBlock->getUsers(true) ?>);
2120
var warning = false;
2221
if (checkBoxes.size() > 0) {
2322
warning = true;
@@ -135,7 +134,6 @@ require([
135134
}
136135
onLoad();
137136
<?php endif; ?>
138-
//-->
139137

140138
});
141139
</script>

0 commit comments

Comments
 (0)