Skip to content

Commit 80fc4aa

Browse files
committed
MAGETWO-93715: [2.3] Delete action in grid could be sent multiple times
1 parent 3d53578 commit 80fc4aa

File tree

7 files changed

+315
-75
lines changed

7 files changed

+315
-75
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function execute()
6666
}
6767

6868
if ($productDeleted) {
69-
$this->messageManager->addSuccessMessage(
69+
$this->messageManager->addSuccess(
7070
__('A total of %1 record(s) have been deleted.', $productDeleted)
7171
);
7272
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,12 @@ public function getCollection(AbstractDb $collection)
9999
throw new LocalizedException(__('An item needs to be selected. Select and try again.'));
100100
}
101101
}
102-
/** @var \Magento\Customer\Model\ResourceModel\Customer\Collection $collection */
103-
$idsArray = $this->getFilterIds();
104-
if (!empty($idsArray)) {
105-
$collection->addFieldToFilter(
106-
$collection->getIdFieldName(),
107-
['in' => $idsArray]
108-
);
109-
}
102+
103+
$collection->addFieldToFilter(
104+
$collection->getIdFieldName(),
105+
['in' => $this->getFilterIds()]
106+
);
107+
110108
return $collection;
111109
}
112110

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

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Customer\Controller\Adminhtml\Index;
89

9-
use Magento\TestFramework\Helper\Bootstrap;
10+
use Magento\Backend\Model\Session;
1011
use Magento\Customer\Api\CustomerRepositoryInterface;
12+
use Magento\Customer\Api\Data\CustomerInterface;
13+
use Magento\Framework\Message\MessageInterface;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\TestFramework\TestCase\AbstractBackendController;
1116

1217
/**
1318
* @magentoAppArea adminhtml
1419
*/
15-
class MassAssignGroupTest extends \Magento\TestFramework\TestCase\AbstractBackendController
20+
class MassAssignGroupTest extends AbstractBackendController
1621
{
1722
/**
1823
* Base controller URL
@@ -29,85 +34,105 @@ class MassAssignGroupTest extends \Magento\TestFramework\TestCase\AbstractBacken
2934
protected function setUp()
3035
{
3136
parent::setUp();
32-
$this->customerRepository = Bootstrap::getObjectManager()->get(
33-
\Magento\Customer\Api\CustomerRepositoryInterface::class
34-
);
37+
$this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
3538
}
3639

3740
protected function tearDown()
3841
{
3942
/**
4043
* Unset customer data
4144
*/
42-
Bootstrap::getObjectManager()->get(\Magento\Backend\Model\Session::class)->setCustomerData(null);
45+
Bootstrap::getObjectManager()->get(Session::class)->setCustomerData(null);
4346

4447
/**
4548
* Unset messages
4649
*/
47-
Bootstrap::getObjectManager()->get(\Magento\Backend\Model\Session::class)->getMessages(true);
50+
Bootstrap::getObjectManager()->get(Session::class)->getMessages(true);
4851
}
4952

5053
/**
51-
* @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
5258
*/
5359
public function testMassAssignGroupAction()
5460
{
55-
$customer = $this->customerRepository->getById(1);
61+
$customerEmail = 'customer1@example.com';
62+
/** @var CustomerInterface $customer */
63+
$customer = $this->customerRepository->get($customerEmail);
5664
$this->assertEquals(1, $customer->getGroupId());
5765

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

69-
$customer = $this->customerRepository->getById(1);
80+
$customer = $this->customerRepository->get($customerEmail);
7081
$this->assertEquals(0, $customer->getGroupId());
7182
}
7283

7384
/**
74-
* @magentoDataFixture Magento/Customer/_files/twenty_one_customers.php
85+
* Tests os update a multiple customer records.
86+
*
87+
* @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
88+
* @magentoDbIsolation disabled
7589
*/
7690
public function testLargeGroupMassAssignGroupAction()
7791
{
78-
79-
for ($i = 1; $i < 22; $i++) {
80-
$customer = $this->customerRepository->getById($i);
92+
$ids = [];
93+
for ($i = 1; $i <= 5; $i++) {
94+
/** @var CustomerInterface $customer */
95+
$customer = $this->customerRepository->get('customer' . $i . '@example.com');
8196
$this->assertEquals(1, $customer->getGroupId());
97+
$ids[] = $customer->getId();
8298
}
8399

84-
$this->getRequest()
85-
->setParam('group', 0)
86-
->setPostValue('namespace', 'customer_listing')
87-
->setPostValue('selected', [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]);
100+
$params = [
101+
'group' => 0,
102+
'namespace' => 'customer_listing',
103+
'selected' => $ids,
104+
];
105+
106+
$this->getRequest()->setParams($params);
88107
$this->dispatch('backend/customer/index/massAssignGroup');
89108
$this->assertSessionMessages(
90-
$this->equalTo(['A total of 21 record(s) were updated.']),
91-
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
109+
self::equalTo(['A total of 5 record(s) were updated.']),
110+
MessageInterface::TYPE_SUCCESS
92111
);
93112
$this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
94-
for ($i = 1; $i < 22; $i++) {
95-
$customer = $this->customerRepository->getById($i);
113+
for ($i = 1; $i < 5; $i++) {
114+
/** @var CustomerInterface $customer */
115+
$customer = $this->customerRepository->get('customer' . $i . '@example.com');
96116
$this->assertEquals(0, $customer->getGroupId());
97117
}
98118
}
99119

100120
/**
101121
* Valid group Id but no customer Ids specified
122+
*
102123
* @magentoDbIsolation enabled
103124
*/
104125
public function testMassAssignGroupActionNoCustomerIds()
105126
{
106-
$this->getRequest()->setParam('group', 0)->setPostValue('namespace', 'customer_listing');
127+
$params = [
128+
'group' => 0,
129+
'namespace' => 'customer_listing',
130+
];
131+
$this->getRequest()->setParams($params);
107132
$this->dispatch('backend/customer/index/massAssignGroup');
108133
$this->assertSessionMessages(
109134
$this->equalTo(['An item needs to be selected. Select and try again.']),
110-
\Magento\Framework\Message\MessageInterface::TYPE_ERROR
135+
MessageInterface::TYPE_ERROR
111136
);
112137
}
113138
}

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

Lines changed: 119 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,162 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Customer\Controller\Adminhtml\Index;
89

10+
use Magento\Backend\Model\Session;
11+
use Magento\Customer\Api\CustomerRepositoryInterface;
12+
use Magento\Customer\Api\Data\CustomerInterface;
13+
use PHPUnit\Framework\Constraint\Constraint;
14+
use Magento\Framework\Message\MessageInterface;
915
use Magento\TestFramework\Helper\Bootstrap;
16+
use Magento\TestFramework\TestCase\AbstractBackendController;
1017

1118
/**
1219
* @magentoAppArea adminhtml
1320
*/
14-
class MassDeleteTest extends \Magento\TestFramework\TestCase\AbstractBackendController
21+
class MassDeleteTest extends AbstractBackendController
1522
{
23+
/**
24+
* @var CustomerRepositoryInterface
25+
*/
26+
private $customerRepository;
27+
1628
/**
1729
* Base controller URL
1830
*
1931
* @var string
2032
*/
21-
protected $baseControllerUrl = 'http://localhost/index.php/backend/customer/index/index';
33+
private $baseControllerUrl = 'http://localhost/index.php/backend/customer/index/index';
34+
35+
protected function setUp()
36+
{
37+
parent::setUp();
38+
$this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
39+
}
2240

2341
protected function tearDown()
2442
{
2543
/**
2644
* Unset customer data
2745
*/
28-
Bootstrap::getObjectManager()->get(\Magento\Backend\Model\Session::class)->setCustomerData(null);
46+
Bootstrap::getObjectManager()->get(Session::class)->setCustomerData(null);
2947

3048
/**
3149
* Unset messages
3250
*/
33-
Bootstrap::getObjectManager()->get(\Magento\Backend\Model\Session::class)->getMessages(true);
51+
Bootstrap::getObjectManager()->get(Session::class)->getMessages(true);
3452
}
3553

3654
/**
37-
* @magentoDataFixture Magento/Customer/_files/customer.php
55+
* Validates failure attempts to delete customers from grid.
56+
*
57+
* @param array|null $ids
58+
* @param Constraint $constraint
59+
* @param string|null $messageType
60+
* @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
61+
* @magentoDbIsolation disabled
62+
* @dataProvider failedRequestDataProvider
3863
*/
39-
public function testMassDeleteAction()
64+
public function testFailedMassDeleteAction($ids, Constraint $constraint, $messageType)
4065
{
41-
$this->getRequest()->setPostValue('selected', [1])->setPostValue('namespace', 'customer_listing');
42-
$this->dispatch('backend/customer/index/massDelete');
43-
$this->assertSessionMessages(
44-
$this->equalTo(['A total of 1 record(s) were deleted.']),
45-
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
66+
$this->massDeleteAssertions($ids, $constraint, $messageType);
67+
}
68+
69+
/**
70+
* Validates success attempt to delete customer from grid.
71+
*
72+
* @param array $emails
73+
* @param Constraint $constraint
74+
* @param string $messageType
75+
* @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
76+
* @magentoDbIsolation disabled
77+
* @dataProvider successRequestDataProvider
78+
*/
79+
public function testSuccessMassDeleteAction(array $emails, Constraint $constraint, string $messageType)
80+
{
81+
$ids = [];
82+
foreach ($emails as $email) {
83+
/** @var CustomerInterface $customer */
84+
$customer = $this->customerRepository->get($email);
85+
$ids[] = $customer->getId();
86+
}
87+
88+
$this->massDeleteAssertions(
89+
$ids,
90+
$constraint,
91+
$messageType
4692
);
47-
$this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
4893
}
4994

5095
/**
51-
* Valid group Id but no customer Ids specified
52-
* @magentoDbIsolation enabled
96+
* Performs required request and assertions.
97+
*
98+
* @param array|null $ids
99+
* @param Constraint $constraint
100+
* @param string|null $messageType
53101
*/
54-
public function testMassDeleteActionNoCustomerIds()
102+
private function massDeleteAssertions($ids, Constraint $constraint, $messageType)
55103
{
56-
$this->getRequest()->setPostValue('namespace', 'customer_listing');
104+
$requestData = [
105+
'selected' => $ids,
106+
'namespace' => 'customer_listing',
107+
];
108+
109+
$this->getRequest()->setParams($requestData);
57110
$this->dispatch('backend/customer/index/massDelete');
58111
$this->assertSessionMessages(
59-
$this->equalTo(['An item needs to be selected. Select and try again.']),
60-
\Magento\Framework\Message\MessageInterface::TYPE_ERROR
112+
$constraint,
113+
$messageType
61114
);
115+
$this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
116+
}
117+
118+
/**
119+
* Provides sets of data for unsuccessful attempts.
120+
*
121+
* @return array
122+
*/
123+
public function failedRequestDataProvider(): array
124+
{
125+
return [
126+
[
127+
'ids' => [],
128+
'constraint' => self::equalTo(['Please select item(s).']),
129+
'messageType' => MessageInterface::TYPE_ERROR,
130+
],
131+
[
132+
'ids' => [111],
133+
'constraint' => self::isEmpty(),
134+
'messageType' => null,
135+
],
136+
[
137+
'ids' => null,
138+
'constraint' => self::equalTo(['Please select item(s).']),
139+
'messageType' => MessageInterface::TYPE_ERROR,
140+
]
141+
];
142+
}
143+
144+
/**
145+
* Provides sets of data for successful attempts.
146+
*
147+
* @return array
148+
*/
149+
public function successRequestDataProvider(): array
150+
{
151+
return [
152+
[
153+
'customerEmails' => ['customer1@example.com'],
154+
'constraint' => self::equalTo(['A total of 1 record(s) were deleted.']),
155+
'messageType' => MessageInterface::TYPE_SUCCESS,
156+
],
157+
[
158+
'customerEmails' => ['customer2@example.com', 'customer3@example.com'],
159+
'constraint' => self::equalTo(['A total of 2 record(s) were deleted.']),
160+
'messageType' => MessageInterface::TYPE_SUCCESS,
161+
],
162+
];
62163
}
63164
}

0 commit comments

Comments
 (0)