Skip to content

Commit a08b51e

Browse files
ENGCOM-2262: [Backport] MAGETWO-91411: Delete action in grid could be sent multiple times #16702
- Merge Pull Request #16702 from novikor/magento2:15935-mass-delete-deletes-all-products - Merged commits: 1. ab769ad 2. 92320d7 3. fd57435
2 parents ff04f26 + fd57435 commit a08b51e

File tree

7 files changed

+356
-72
lines changed

7 files changed

+356
-72
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
@@ -54,9 +54,12 @@ public function execute()
5454
$product->delete();
5555
$productDeleted++;
5656
}
57-
$this->messageManager->addSuccess(
58-
__('A total of %1 record(s) have been deleted.', $productDeleted)
59-
);
57+
58+
if ($productDeleted) {
59+
$this->messageManager->addSuccess(
60+
__('A total of %1 record(s) have been deleted.', $productDeleted)
61+
);
62+
}
6063

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

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,11 @@ public function getCollection(AbstractDb $collection)
101101
throw new LocalizedException(__('Please select item(s).'));
102102
}
103103
}
104-
$idsArray = $this->getFilterIds();
105-
if (!empty($idsArray)) {
106-
$collection->addFieldToFilter(
107-
$collection->getIdFieldName(),
108-
['in' => $idsArray]
109-
);
110-
}
104+
105+
$collection->addFieldToFilter(
106+
$collection->getIdFieldName(),
107+
['in' => $this->getFilterIds()]
108+
);
111109

112110
return $collection;
113111
}

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

Lines changed: 82 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
*/
66
namespace Magento\Customer\Controller\Adminhtml\Index;
77

8-
use Magento\TestFramework\Helper\Bootstrap;
98
use Magento\Customer\Api\CustomerRepositoryInterface;
9+
use Magento\Customer\Api\Data\CustomerInterface;
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Message\MessageInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\TestCase\AbstractBackendController;
1014

1115
/**
1216
* @magentoAppArea adminhtml
1317
*/
14-
class MassAssignGroupTest extends \Magento\TestFramework\TestCase\AbstractBackendController
18+
class MassAssignGroupTest extends AbstractBackendController
1519
{
1620
/**
1721
* Base controller URL
@@ -28,9 +32,7 @@ class MassAssignGroupTest extends \Magento\TestFramework\TestCase\AbstractBacken
2832
protected function setUp()
2933
{
3034
parent::setUp();
31-
$this->customerRepository = Bootstrap::getObjectManager()->get(
32-
'Magento\Customer\Api\CustomerRepositoryInterface'
33-
);
35+
$this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
3436
}
3537

3638
protected function tearDown()
@@ -47,39 +49,100 @@ protected function tearDown()
4749
}
4850

4951
/**
50-
* @magentoDataFixture Magento/Customer/_files/customer.php
52+
* Tests os update a single customer record.
53+
*
54+
* @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
55+
* @magentoDbIsolation disabled
5156
*/
5257
public function testMassAssignGroupAction()
5358
{
54-
$customer = $this->customerRepository->getById(1);
55-
$this->assertEquals(1, $customer->getGroupId());
59+
$customerEmail = 'customer1@example.com';
60+
try {
61+
/** @var CustomerInterface $customer */
62+
$customer = $this->customerRepository->get($customerEmail);
63+
$this->assertEquals(1, $customer->getGroupId());
64+
65+
$params = [
66+
'group' => 0,
67+
'namespace' => 'customer_listing',
68+
'selected' => [$customer->getId()]
69+
];
70+
71+
$this->getRequest()->setParams($params);
72+
$this->dispatch('backend/customer/index/massAssignGroup');
73+
$this->assertSessionMessages(
74+
self::equalTo(['A total of 1 record(s) were updated.']),
75+
MessageInterface::TYPE_SUCCESS
76+
);
77+
$this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
78+
79+
$customer = $this->customerRepository->get($customerEmail);
80+
$this->assertEquals(0, $customer->getGroupId());
81+
} catch (LocalizedException $e) {
82+
self::fail($e->getMessage());
83+
}
84+
}
85+
86+
/**
87+
* Tests os update a multiple customer records.
88+
*
89+
* @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
90+
* @magentoDbIsolation disabled
91+
*/
92+
public function testLargeGroupMassAssignGroupAction()
93+
{
94+
$ids = [];
95+
for ($i = 1; $i <= 5; $i++) {
96+
/** @var CustomerInterface $customer */
97+
try {
98+
$customer = $this->customerRepository->get('customer'.$i.'@example.com');
99+
$this->assertEquals(1, $customer->getGroupId());
100+
$ids[] = $customer->getId();
101+
} catch (\Exception $e) {
102+
self::fail($e->getMessage());
103+
}
104+
}
105+
106+
$params = [
107+
'group' => 0,
108+
'namespace' => 'customer_listing',
109+
'selected' => $ids,
110+
];
56111

57-
$this->getRequest()
58-
->setParam('group', 0)
59-
->setPostValue('namespace', 'customer_listing')
60-
->setPostValue('selected', [1]);
112+
$this->getRequest()->setParams($params);
61113
$this->dispatch('backend/customer/index/massAssignGroup');
62114
$this->assertSessionMessages(
63-
$this->equalTo(['A total of 1 record(s) were updated.']),
64-
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
115+
self::equalTo(['A total of 5 record(s) were updated.']),
116+
MessageInterface::TYPE_SUCCESS
65117
);
66118
$this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
67-
68-
$customer = $this->customerRepository->getById(1);
69-
$this->assertEquals(0, $customer->getGroupId());
119+
for ($i = 1; $i < 5; $i++) {
120+
try {
121+
/** @var CustomerInterface $customer */
122+
$customer = $this->customerRepository->get('customer'.$i.'@example.com');
123+
$this->assertEquals(0, $customer->getGroupId());
124+
} catch (\Exception $e) {
125+
self::fail($e->getMessage());
126+
}
127+
}
70128
}
71129

72130
/**
73131
* Valid group Id but no customer Ids specified
132+
*
74133
* @magentoDbIsolation enabled
75134
*/
76135
public function testMassAssignGroupActionNoCustomerIds()
77136
{
78-
$this->getRequest()->setParam('group', 0)->setPostValue('namespace', 'customer_listing');
137+
$params = [
138+
'group' => 0,
139+
'namespace' => 'customer_listing',
140+
];
141+
$this->getRequest()->setParams($params);
79142
$this->dispatch('backend/customer/index/massAssignGroup');
80143
$this->assertSessionMessages(
81144
$this->equalTo(['Please select item(s).']),
82-
\Magento\Framework\Message\MessageInterface::TYPE_ERROR
145+
MessageInterface::TYPE_ERROR
83146
);
84147
}
85148
}

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

Lines changed: 129 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,162 @@
55
*/
66
namespace Magento\Customer\Controller\Adminhtml\Index;
77

8+
use Magento\Customer\Api\CustomerRepositoryInterface;
9+
use Magento\Customer\Api\Data\CustomerInterface;
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Message\MessageInterface;
812
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\TestCase\AbstractBackendController;
14+
use PHPUnit_Framework_Constraint;
915

1016
/**
1117
* @magentoAppArea adminhtml
1218
*/
13-
class MassDeleteTest extends \Magento\TestFramework\TestCase\AbstractBackendController
19+
class MassDeleteTest extends AbstractBackendController
1420
{
21+
/**
22+
* @var CustomerRepositoryInterface
23+
*/
24+
private $customerRepository;
25+
1526
/**
1627
* Base controller URL
1728
*
1829
* @var string
1930
*/
20-
protected $baseControllerUrl = 'http://localhost/index.php/backend/customer/index/index';
31+
private $baseControllerUrl = 'http://localhost/index.php/backend/customer/index/index';
2132

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

29-
/**
30-
* Unset messages
31-
*/
32-
Bootstrap::getObjectManager()->get('Magento\Backend\Model\Session')->getMessages(true);
39+
/**
40+
* Validates failure attempts to delete customers from grid.
41+
*
42+
* @param array|null $ids
43+
* @param \PHPUnit_Framework_Constraint $constraint
44+
* @param string|null $messageType
45+
* @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
46+
* @magentoDbIsolation disabled
47+
* @dataProvider failedRequestDataProvider
48+
*/
49+
public function testFailedMassDeleteAction($ids, PHPUnit_Framework_Constraint $constraint, $messageType)
50+
{
51+
$this->massDeleteAssertions($ids, $constraint, $messageType);
3352
}
3453

3554
/**
36-
* @magentoDataFixture Magento/Customer/_files/customer.php
55+
* Validates success attempt to delete customer from grid.
56+
*
57+
* @param array $emails
58+
* @param PHPUnit_Framework_Constraint $constraint
59+
* @param string $messageType
60+
* @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
61+
* @magentoDbIsolation disabled
62+
* @dataProvider successRequestDataProvider
63+
*/
64+
public function testSuccessMassDeleteAction(array $emails, PHPUnit_Framework_Constraint $constraint, $messageType)
65+
{
66+
try {
67+
$ids = [];
68+
foreach ($emails as $email) {
69+
/** @var CustomerInterface $customer */
70+
$customer = $this->customerRepository->get($email);
71+
$ids[] = $customer->getId();
72+
}
73+
74+
$this->massDeleteAssertions(
75+
$ids,
76+
$constraint,
77+
$messageType
78+
);
79+
} catch (LocalizedException $e) {
80+
self::fail($e->getMessage());
81+
}
82+
}
83+
84+
/**
85+
* Performs required request and assertions.
86+
*
87+
* @param array|null $ids
88+
* @param PHPUnit_Framework_Constraint $constraint
89+
* @param string|null $messageType
3790
*/
38-
public function testMassDeleteAction()
91+
private function massDeleteAssertions($ids, PHPUnit_Framework_Constraint $constraint, $messageType)
3992
{
40-
$this->getRequest()->setPostValue('selected', [1])->setPostValue('namespace', 'customer_listing');
93+
$requestData = [
94+
'selected' => $ids,
95+
'namespace' => 'customer_listing',
96+
];
97+
98+
$this->getRequest()->setParams($requestData);
4199
$this->dispatch('backend/customer/index/massDelete');
42100
$this->assertSessionMessages(
43-
$this->equalTo(['A total of 1 record(s) were deleted.']),
44-
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
101+
$constraint,
102+
$messageType
45103
);
46104
$this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
47105
}
48106

49107
/**
50-
* Valid group Id but no customer Ids specified
51-
* @magentoDbIsolation enabled
108+
* Provides sets of data for unsuccessful attempts.
109+
*
110+
* @return array
52111
*/
53-
public function testMassDeleteActionNoCustomerIds()
112+
public function failedRequestDataProvider()
54113
{
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-
);
114+
return [
115+
[
116+
'ids' => [],
117+
'constraint' => self::equalTo(['Please select item(s).']),
118+
'messageType' => MessageInterface::TYPE_ERROR,
119+
],
120+
[
121+
'ids' => [111],
122+
'constraint' => self::isEmpty(),
123+
'messageType' => null,
124+
],
125+
[
126+
'ids' => null,
127+
'constraint' => self::equalTo(['Please select item(s).']),
128+
'messageType' => MessageInterface::TYPE_ERROR,
129+
]
130+
];
131+
}
132+
133+
/**
134+
* Provides sets of data for successful attempts.
135+
*
136+
* @return array
137+
*/
138+
public function successRequestDataProvider()
139+
{
140+
return [
141+
[
142+
'customerEmails' => ['customer1@example.com'],
143+
'constraint' => self::equalTo(['A total of 1 record(s) were deleted.']),
144+
'messageType' => MessageInterface::TYPE_SUCCESS,
145+
],
146+
[
147+
'customerEmails' => ['customer2@example.com', 'customer3@example.com'],
148+
'constraint' => self::equalTo(['A total of 2 record(s) were deleted.']),
149+
'messageType' => MessageInterface::TYPE_SUCCESS,
150+
],
151+
];
152+
}
153+
154+
protected function tearDown()
155+
{
156+
/**
157+
* Unset customer data
158+
*/
159+
Bootstrap::getObjectManager()->get(\Magento\Backend\Model\Session::class)->setCustomerData(null);
160+
161+
/**
162+
* Unset messages
163+
*/
164+
Bootstrap::getObjectManager()->get(\Magento\Backend\Model\Session::class)->getMessages(true);
61165
}
62166
}

0 commit comments

Comments
 (0)