Skip to content

Commit 18eaad4

Browse files
committed
MC-31528: Admin: Mass actions on customers grid
1 parent a25c107 commit 18eaad4

File tree

4 files changed

+270
-0
lines changed

4 files changed

+270
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Controller\Adminhtml\Index;
9+
10+
use Magento\Customer\Api\CustomerMetadataInterface;
11+
use Magento\Customer\Api\CustomerRepositoryInterface;
12+
use Magento\Customer\Api\Data\CustomerInterface;
13+
use Magento\Eav\Model\AttributeRepository;
14+
use Magento\Framework\App\Request\Http as HttpRequest;
15+
use Magento\Framework\ObjectManagerInterface;
16+
use Magento\Framework\Serialize\SerializerInterface;
17+
use Magento\Store\Api\WebsiteRepositoryInterface;
18+
use Magento\TestFramework\Helper\Bootstrap;
19+
use Magento\TestFramework\TestCase\AbstractBackendController;
20+
21+
/**
22+
* Test inline edit action on customers grid.
23+
*
24+
* @magentoAppArea adminhtml
25+
* @magentoDbIsolation enabled
26+
*/
27+
class InlineEditTest extends AbstractBackendController
28+
{
29+
/** @var ObjectManagerInterface */
30+
private $objectManager;
31+
32+
/** @var CustomerRepositoryInterface */
33+
private $customerRepository;
34+
35+
/** @var SerializerInterface */
36+
private $json;
37+
38+
/** @var WebsiteRepositoryInterface */
39+
private $websiteRepository;
40+
41+
/** @var AttributeRepository */
42+
private $attributeRepository;
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
protected function setUp()
48+
{
49+
parent::setUp();
50+
51+
$this->objectManager = Bootstrap::getObjectManager();
52+
$this->customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class);
53+
$this->json = $this->objectManager->get(SerializerInterface::class);
54+
$this->websiteRepository = $this->objectManager->get(WebsiteRepositoryInterface::class);
55+
$this->attributeRepository = $this->objectManager->get(AttributeRepository::class);
56+
}
57+
58+
/**
59+
* @magentoDataFixture Magento/Customer/_files/two_customers.php
60+
*
61+
* @return void
62+
*/
63+
public function testInlineEditAction(): void
64+
{
65+
$firstCustomer = $this->customerRepository->get('customer@example.com');
66+
$secondCustomer = $this->customerRepository->get('customer_two@example.com');
67+
$defaultWebsiteId = $this->websiteRepository->get('base')->getId();
68+
$genderId = $this->attributeRepository->get(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'gender')
69+
->getSource()->getOptionId('Male');
70+
$params = [
71+
'items' => [
72+
$firstCustomer->getId() => [
73+
CustomerInterface::EMAIL => 'updated_customer@example.com',
74+
CustomerInterface::GROUP_ID => 2,
75+
CustomerInterface::WEBSITE_ID => $defaultWebsiteId,
76+
CustomerInterface::TAXVAT => 123123,
77+
CustomerInterface::GENDER => $genderId,
78+
],
79+
$secondCustomer->getId() => [
80+
CustomerInterface::EMAIL => 'updated_customer_two@example.com',
81+
CustomerInterface::GROUP_ID => 3,
82+
CustomerInterface::WEBSITE_ID => $defaultWebsiteId,
83+
CustomerInterface::TAXVAT => 456456,
84+
CustomerInterface::GENDER => $genderId,
85+
],
86+
],
87+
'isAjax' => true,
88+
];
89+
$actual = $this->performInlineEditRequest($params);
90+
$this->assertEmpty($actual['messages']);
91+
$this->assertFalse($actual['error']);
92+
$this->assertCustomersData($params);
93+
}
94+
95+
/**
96+
* @dataProvider inlineEditParametersDataProvider
97+
*
98+
* @param array $params
99+
* @return void
100+
*/
101+
public function testInlineEditWithWrongParams(array $params): void
102+
{
103+
$actual = $this->performInlineEditRequest($params);
104+
$this->assertEquals([(string)__('Please correct the data sent.')], $actual['messages']);
105+
$this->assertTrue($actual['error']);
106+
}
107+
108+
/**
109+
* @return array
110+
*/
111+
public function inlineEditParametersDataProvider(): array
112+
{
113+
return [
114+
[
115+
'items' => [],
116+
'isAjax' => true,
117+
],
118+
[
119+
'items' => [],
120+
],
121+
];
122+
}
123+
124+
/**
125+
* Perform inline edit request.
126+
*
127+
* @param array $params
128+
* @return array
129+
*/
130+
private function performInlineEditRequest(array $params): array
131+
{
132+
$this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST);
133+
$this->dispatch('backend/customer/index/inlineEdit');
134+
135+
return $this->json->unserialize($this->getResponse()->getBody());
136+
}
137+
138+
/**
139+
* Assert customers data.
140+
*
141+
* @param array $data
142+
* @return void
143+
*/
144+
private function assertCustomersData(array $data): void
145+
{
146+
foreach ($data['items'] as $customerId => $expectedData) {
147+
$customerData = $this->customerRepository->getById($customerId)->__toArray();
148+
foreach ($expectedData as $key => $value) {
149+
$this->assertEquals($value, $customerData[$key]);
150+
}
151+
}
152+
}
153+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Controller\Adminhtml\Index;
9+
10+
use Magento\Framework\App\Request\Http as HttpRequest;
11+
use Magento\Framework\Message\MessageInterface;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory;
14+
use Magento\Newsletter\Model\Subscriber;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use Magento\Customer\Api\CustomerRepositoryInterface;
17+
use Magento\TestFramework\TestCase\AbstractBackendController;
18+
19+
/**
20+
* Test mass subscribe action on customers grid.
21+
*
22+
* @magentoAppArea adminhtml
23+
* @magentoDbIsolation enabled
24+
*/
25+
class MassUnsubscribeTest extends AbstractBackendController
26+
{
27+
/** @var ObjectManagerInterface */
28+
private $objectManager;
29+
30+
/** @var CustomerRepositoryInterface */
31+
private $customerRepository;
32+
33+
/** @var CollectionFactory */
34+
private $subscriberCollectionFactory;
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
protected function setUp()
40+
{
41+
parent::setUp();
42+
43+
$this->objectManager = Bootstrap::getObjectManager();
44+
$this->subscriberCollectionFactory = $this->objectManager->get(CollectionFactory::class);
45+
$this->customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class);
46+
}
47+
48+
/**
49+
* @magentoDataFixture Magento/Newsletter/_files/three_subscribers.php
50+
*
51+
* @return void
52+
*/
53+
public function testMassUnsubscribeAction(): void
54+
{
55+
$params = [
56+
'selected' => [1, 2, 3],
57+
'namespace' => 'customer_listing',
58+
];
59+
$this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST);
60+
$this->dispatch('backend/customer/index/massUnsubscribe');
61+
$this->assertRedirect($this->stringContains('backend/customer/index/index'));
62+
$this->assertSessionMessages(
63+
$this->equalTo([(string)__('A total of 3 record(s) were updated.')]),
64+
MessageInterface::TYPE_SUCCESS
65+
);
66+
$emails = ['customer@search.example.com', 'customer2@search.example.com', 'customer3@search.example.com'];
67+
$collection = $this->subscriberCollectionFactory->create()->addFieldToFilter('subscriber_email', $emails)
68+
->addFieldToSelect('subscriber_status');
69+
$this->assertCount(3, $collection);
70+
foreach ($collection as $subscriber) {
71+
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $subscriber->getData('subscriber_status'));
72+
}
73+
}
74+
75+
/**
76+
* @return void
77+
*/
78+
public function testMassSubscriberActionNoSelection(): void
79+
{
80+
$params = [
81+
'namespace' => 'customer_listing',
82+
];
83+
$this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST);
84+
$this->dispatch('backend/customer/index/massUnsubscribe');
85+
$this->assertRedirect($this->stringContains('backend/customer/index/index'));
86+
$this->assertSessionMessages(
87+
$this->equalTo([(string)__('An item needs to be selected. Select and try again.')]),
88+
MessageInterface::TYPE_ERROR
89+
);
90+
}
91+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Newsletter\Model\SubscriberFactory;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
11+
require __DIR__ . '/../../../Magento/Customer/_files/three_customers.php';
12+
13+
$objectManager = Bootstrap::getObjectManager();
14+
$subscriberFactory = $objectManager->get(SubscriberFactory::class);
15+
16+
$subscriberFactory->create()->subscribe('customer@search.example.com');
17+
$subscriberFactory->create()->subscribe('customer2@search.example.com');
18+
$subscriberFactory->create()->subscribe('customer3@search.example.com');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
require __DIR__ . '/../../../Magento/Customer/_files/three_customers_rollback.php';

0 commit comments

Comments
 (0)