Skip to content

Commit a892d87

Browse files
committed
MAGETWO-91411: Delete action in grid could be sent multiple times
1 parent c6dcbb0 commit a892d87

File tree

7 files changed

+353
-88
lines changed

7 files changed

+353
-88
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/MassDelete.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@ public function execute()
6464
$this->productRepository->delete($product);
6565
$productDeleted++;
6666
}
67-
$this->messageManager->addSuccess(
68-
__('A total of %1 record(s) have been deleted.', $productDeleted)
69-
);
67+
68+
if ($productDeleted) {
69+
$this->messageManager->addSuccess(
70+
__('A total of %1 record(s) have been deleted.', $productDeleted)
71+
);
72+
}
7073

7174
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('catalog/*/index');
7275
}

app/code/Magento/Ui/Component/MassAction/Filter.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,12 @@ public function getCollection(AbstractDb $collection)
9898
throw new LocalizedException(__('Please select item(s).'));
9999
}
100100
}
101-
/** @var \Magento\Customer\Model\ResourceModel\Customer\Collection $collection */
102-
$idsArray = $this->getFilterIds();
103-
if (!empty($idsArray)) {
104-
$collection->addFieldToFilter(
105-
$collection->getIdFieldName(),
106-
['in' => $idsArray]
107-
);
108-
}
101+
102+
$collection->addFieldToFilter(
103+
$collection->getIdFieldName(),
104+
['in' => $this->getFilterIds()]
105+
);
106+
109107
return $collection;
110108
}
111109

dev/tests/integration/testsuite/Magento/Customer/Controller/Adminhtml/Index/MassAssignGroupTest.php

Lines changed: 74 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Customer\Controller\Adminhtml\Index;
79

8-
use Magento\TestFramework\Helper\Bootstrap;
910
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Api\Data\CustomerInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Message\MessageInterface;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\TestFramework\TestCase\AbstractBackendController;
1016

1117
/**
1218
* @magentoAppArea adminhtml
1319
*/
14-
class MassAssignGroupTest extends \Magento\TestFramework\TestCase\AbstractBackendController
20+
class MassAssignGroupTest extends AbstractBackendController
1521
{
1622
/**
1723
* Base controller URL
@@ -28,9 +34,7 @@ class MassAssignGroupTest extends \Magento\TestFramework\TestCase\AbstractBacken
2834
protected function setUp()
2935
{
3036
parent::setUp();
31-
$this->customerRepository = Bootstrap::getObjectManager()->get(
32-
\Magento\Customer\Api\CustomerRepositoryInterface::class
33-
);
37+
$this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
3438
}
3539

3640
protected function tearDown()
@@ -47,66 +51,100 @@ protected function tearDown()
4751
}
4852

4953
/**
50-
* @magentoDataFixture Magento/Customer/_files/customer.php
54+
* Tests os update a single customer record.
55+
*
56+
* @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
57+
* @magentoDbIsolation disabled
5158
*/
5259
public function testMassAssignGroupAction()
5360
{
54-
$customer = $this->customerRepository->getById(1);
55-
$this->assertEquals(1, $customer->getGroupId());
61+
$customerEmail = 'customer1@example.com';
62+
try {
63+
/** @var CustomerInterface $customer */
64+
$customer = $this->customerRepository->get($customerEmail);
65+
$this->assertEquals(1, $customer->getGroupId());
5666

57-
$this->getRequest()
58-
->setParam('group', 0)
59-
->setPostValue('namespace', 'customer_listing')
60-
->setPostValue('selected', [1]);
61-
$this->dispatch('backend/customer/index/massAssignGroup');
62-
$this->assertSessionMessages(
63-
$this->equalTo(['A total of 1 record(s) were updated.']),
64-
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
65-
);
66-
$this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
67+
$params = [
68+
'group' => 0,
69+
'namespace' => 'customer_listing',
70+
'selected' => [$customer->getId()]
71+
];
6772

68-
$customer = $this->customerRepository->getById(1);
69-
$this->assertEquals(0, $customer->getGroupId());
73+
$this->getRequest()->setParams($params);
74+
$this->dispatch('backend/customer/index/massAssignGroup');
75+
$this->assertSessionMessages(
76+
self::equalTo(['A total of 1 record(s) were updated.']),
77+
MessageInterface::TYPE_SUCCESS
78+
);
79+
$this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
80+
81+
$customer = $this->customerRepository->get($customerEmail);
82+
$this->assertEquals(0, $customer->getGroupId());
83+
} catch (LocalizedException $e) {
84+
self::fail($e->getMessage());
85+
}
7086
}
7187

7288
/**
73-
* @magentoDataFixture Magento/Customer/_files/twenty_one_customers.php
89+
* Tests os update a multiple customer records.
90+
*
91+
* @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
92+
* @magentoDbIsolation disabled
7493
*/
7594
public function testLargeGroupMassAssignGroupAction()
7695
{
77-
78-
for ($i = 1; $i < 22; $i++) {
79-
$customer = $this->customerRepository->getById($i);
80-
$this->assertEquals(1, $customer->getGroupId());
96+
$ids = [];
97+
for ($i = 1; $i <= 5; $i++) {
98+
/** @var CustomerInterface $customer */
99+
try {
100+
$customer = $this->customerRepository->get('customer'.$i.'@example.com');
101+
$this->assertEquals(1, $customer->getGroupId());
102+
$ids[] = $customer->getId();
103+
} catch (\Exception $e) {
104+
self::fail($e->getMessage());
105+
}
81106
}
82107

83-
$this->getRequest()
84-
->setParam('group', 0)
85-
->setPostValue('namespace', 'customer_listing')
86-
->setPostValue('selected', [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]);
108+
$params = [
109+
'group' => 0,
110+
'namespace' => 'customer_listing',
111+
'selected' => $ids,
112+
];
113+
114+
$this->getRequest()->setParams($params);
87115
$this->dispatch('backend/customer/index/massAssignGroup');
88116
$this->assertSessionMessages(
89-
$this->equalTo(['A total of 21 record(s) were updated.']),
90-
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
117+
self::equalTo(['A total of 5 record(s) were updated.']),
118+
MessageInterface::TYPE_SUCCESS
91119
);
92120
$this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
93-
for ($i = 1; $i < 22; $i++) {
94-
$customer = $this->customerRepository->getById($i);
95-
$this->assertEquals(0, $customer->getGroupId());
121+
for ($i = 1; $i < 5; $i++) {
122+
try {
123+
/** @var CustomerInterface $customer */
124+
$customer = $this->customerRepository->get('customer'.$i.'@example.com');
125+
$this->assertEquals(0, $customer->getGroupId());
126+
} catch (\Exception $e) {
127+
self::fail($e->getMessage());
128+
}
96129
}
97130
}
98131

99132
/**
100133
* Valid group Id but no customer Ids specified
134+
*
101135
* @magentoDbIsolation enabled
102136
*/
103137
public function testMassAssignGroupActionNoCustomerIds()
104138
{
105-
$this->getRequest()->setParam('group', 0)->setPostValue('namespace', 'customer_listing');
139+
$params = [
140+
'group' => 0,
141+
'namespace' => 'customer_listing',
142+
];
143+
$this->getRequest()->setParams($params);
106144
$this->dispatch('backend/customer/index/massAssignGroup');
107145
$this->assertSessionMessages(
108146
$this->equalTo(['Please select item(s).']),
109-
\Magento\Framework\Message\MessageInterface::TYPE_ERROR
147+
MessageInterface::TYPE_ERROR
110148
);
111149
}
112150
}

dev/tests/integration/testsuite/Magento/Customer/Controller/Adminhtml/Index/MassDeleteTest.php

Lines changed: 131 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,166 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Customer\Controller\Adminhtml\Index;
79

10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Api\Data\CustomerInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use PHPUnit\Framework\Constraint\Constraint;
14+
use Magento\Framework\Message\MessageInterface;
815
use Magento\TestFramework\Helper\Bootstrap;
16+
use Magento\TestFramework\TestCase\AbstractBackendController;
917

1018
/**
1119
* @magentoAppArea adminhtml
1220
*/
13-
class MassDeleteTest extends \Magento\TestFramework\TestCase\AbstractBackendController
21+
class MassDeleteTest extends AbstractBackendController
1422
{
23+
/**
24+
* @var CustomerRepositoryInterface
25+
*/
26+
private $customerRepository;
27+
1528
/**
1629
* Base controller URL
1730
*
1831
* @var string
1932
*/
20-
protected $baseControllerUrl = 'http://localhost/index.php/backend/customer/index/index';
33+
private $baseControllerUrl = 'http://localhost/index.php/backend/customer/index/index';
2134

22-
protected function tearDown()
35+
protected function setUp()
2336
{
24-
/**
25-
* Unset customer data
26-
*/
27-
Bootstrap::getObjectManager()->get(\Magento\Backend\Model\Session::class)->setCustomerData(null);
37+
parent::setUp();
38+
$this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
39+
}
2840

29-
/**
30-
* Unset messages
31-
*/
32-
Bootstrap::getObjectManager()->get(\Magento\Backend\Model\Session::class)->getMessages(true);
41+
/**
42+
* Validates failure attempts to delete customers from grid.
43+
*
44+
* @param array|null $ids
45+
* @param Constraint $constraint
46+
* @param string|null $messageType
47+
* @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
48+
* @magentoDbIsolation disabled
49+
* @dataProvider failedRequestDataProvider
50+
*/
51+
public function testFailedMassDeleteAction($ids, Constraint $constraint, $messageType)
52+
{
53+
$this->massDeleteAssertions($ids, $constraint, $messageType);
54+
}
55+
56+
/**
57+
* Validates success attempt to delete customer from grid.
58+
*
59+
* @param array $emails
60+
* @param Constraint $constraint
61+
* @param string $messageType
62+
* @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
63+
* @magentoDbIsolation disabled
64+
* @dataProvider successRequestDataProvider
65+
*/
66+
public function testSuccessMassDeleteAction(array $emails, Constraint $constraint, string $messageType)
67+
{
68+
try {
69+
$ids = [];
70+
foreach ($emails as $email) {
71+
/** @var CustomerInterface $customer */
72+
$customer = $this->customerRepository->get($email);
73+
$ids[] = $customer->getId();
74+
}
75+
76+
$this->massDeleteAssertions(
77+
$ids,
78+
$constraint,
79+
$messageType
80+
);
81+
} catch (LocalizedException $e) {
82+
self::fail($e->getMessage());
83+
}
3384
}
3485

3586
/**
36-
* @magentoDataFixture Magento/Customer/_files/customer.php
87+
* Performs required request and assertions.
88+
*
89+
* @param array|null $ids
90+
* @param Constraint $constraint
91+
* @param string|null $messageType
3792
*/
38-
public function testMassDeleteAction()
93+
private function massDeleteAssertions($ids, Constraint $constraint, $messageType)
3994
{
40-
$this->getRequest()->setPostValue('selected', [1])->setPostValue('namespace', 'customer_listing');
95+
$requestData = [
96+
'selected' => $ids,
97+
'namespace' => 'customer_listing',
98+
];
99+
100+
$this->getRequest()->setParams($requestData);
41101
$this->dispatch('backend/customer/index/massDelete');
42102
$this->assertSessionMessages(
43-
$this->equalTo(['A total of 1 record(s) were deleted.']),
44-
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
103+
$constraint,
104+
$messageType
45105
);
46106
$this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
47107
}
48108

49109
/**
50-
* Valid group Id but no customer Ids specified
51-
* @magentoDbIsolation enabled
110+
* Provides sets of data for unsuccessful attempts.
111+
*
112+
* @return array
52113
*/
53-
public function testMassDeleteActionNoCustomerIds()
114+
public function failedRequestDataProvider(): array
54115
{
55-
$this->getRequest()->setPostValue('namespace', 'customer_listing');
56-
$this->dispatch('backend/customer/index/massDelete');
57-
$this->assertSessionMessages(
58-
$this->equalTo(['Please select item(s).']),
59-
\Magento\Framework\Message\MessageInterface::TYPE_ERROR
60-
);
116+
return [
117+
[
118+
'ids' => [],
119+
'constraint' => self::equalTo(['Please select item(s).']),
120+
'messageType' => MessageInterface::TYPE_ERROR,
121+
],
122+
[
123+
'ids' => [111],
124+
'constraint' => self::isEmpty(),
125+
'messageType' => null,
126+
],
127+
[
128+
'ids' => null,
129+
'constraint' => self::equalTo(['Please select item(s).']),
130+
'messageType' => MessageInterface::TYPE_ERROR,
131+
]
132+
];
133+
}
134+
135+
/**
136+
* Provides sets of data for successful attempts.
137+
*
138+
* @return array
139+
*/
140+
public function successRequestDataProvider(): array
141+
{
142+
return [
143+
[
144+
'customerEmails' => ['customer1@example.com'],
145+
'constraint' => self::equalTo(['A total of 1 record(s) were deleted.']),
146+
'messageType' => MessageInterface::TYPE_SUCCESS,
147+
],
148+
[
149+
'customerEmails' => ['customer2@example.com', 'customer3@example.com'],
150+
'constraint' => self::equalTo(['A total of 2 record(s) were deleted.']),
151+
'messageType' => MessageInterface::TYPE_SUCCESS,
152+
],
153+
];
154+
}
155+
156+
protected function tearDown()
157+
{
158+
/**
159+
* Unset customer data
160+
*/
161+
Bootstrap::getObjectManager()->get(\Magento\Backend\Model\Session::class)->setCustomerData(null);
162+
163+
/**
164+
* Unset messages
165+
*/
166+
Bootstrap::getObjectManager()->get(\Magento\Backend\Model\Session::class)->getMessages(true);
61167
}
62168
}

0 commit comments

Comments
 (0)