Skip to content

Commit 7962878

Browse files
committed
MC-30685: Admin: Mass actions on customers grid
1 parent 5b9be66 commit 7962878

File tree

4 files changed

+254
-0
lines changed

4 files changed

+254
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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\Eav\Model\AttributeRepository;
12+
use Magento\Framework\App\Request\Http as HttpRequest;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\Framework\Serialize\SerializerInterface;
15+
use Magento\Store\Api\WebsiteRepositoryInterface;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use Magento\Customer\Api\CustomerRepositoryInterface;
18+
use Magento\TestFramework\TestCase\AbstractBackendController;
19+
20+
/**
21+
* Test inline edit action on customers grid.
22+
*
23+
* @magentoAppArea adminhtml
24+
* @magentoDbIsolation enabled
25+
*/
26+
class InlineEditTest extends AbstractBackendController
27+
{
28+
/** @var ObjectManagerInterface */
29+
private $objectManager;
30+
31+
/** @var CustomerRepositoryInterface */
32+
private $customerRepository;
33+
34+
/** @var SerializerInterface */
35+
private $json;
36+
37+
/** @var WebsiteRepositoryInterface */
38+
private $websiteRepository;
39+
40+
/** @var AttributeRepository */
41+
private $attributeRepository;
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
protected function setUp()
47+
{
48+
parent::setUp();
49+
50+
$this->objectManager = Bootstrap::getObjectManager();
51+
$this->customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class);
52+
$this->json = $this->objectManager->get(SerializerInterface::class);
53+
$this->websiteRepository = $this->objectManager->get(WebsiteRepositoryInterface::class);
54+
$this->attributeRepository = $this->objectManager->get(AttributeRepository::class);
55+
}
56+
57+
/**
58+
* @magentoDataFixture Magento/Customer/_files/two_customers.php
59+
*
60+
* @return void
61+
*/
62+
public function testInlineEditAction(): void
63+
{
64+
$firstCustomer = $this->customerRepository->get('customer@example.com');
65+
$secondCustomer = $this->customerRepository->get('customer_two@example.com');
66+
$defaultWebsiteId = $this->websiteRepository->get('base')->getId();
67+
$genderId = $this->attributeRepository->get(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'gender')
68+
->getSource()->getOptionId('Male');
69+
$params = [
70+
'items' => [
71+
$firstCustomer->getId() => [
72+
'email' => 'updated_customer@example.com',
73+
'group_id' => 2,
74+
'website_id' => $defaultWebsiteId,
75+
'taxvat' => 123123,
76+
'gender' => $genderId,
77+
],
78+
$secondCustomer->getId() => [
79+
'email' => 'updated_customer_two@example.com',
80+
'group_id' => 3,
81+
'website_id' => $defaultWebsiteId,
82+
'taxvat' => 456456,
83+
'gender' => $genderId,
84+
],
85+
],
86+
'isAjax' => true,
87+
];
88+
$this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST);
89+
$this->dispatch('backend/customer/index/inlineEdit');
90+
$actual = $this->json->unserialize($this->getResponse()->getBody());
91+
$this->assertEquals([], $actual['messages']);
92+
$this->assertEquals(false, $actual['error']);
93+
$this->assertCustomersData($params);
94+
}
95+
96+
/**
97+
* @return void
98+
*/
99+
public function testInlineEditActionNoSelection(): void
100+
{
101+
$params = [
102+
'items' => [],
103+
'isAjax' => true,
104+
];
105+
$this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST);
106+
$this->dispatch('backend/customer/index/inlineEdit');
107+
$actual = $this->json->unserialize($this->getResponse()->getBody());
108+
$this->assertEquals(['Please correct the data sent.'], $actual['messages']);
109+
$this->assertEquals(true, $actual['error']);
110+
}
111+
112+
/**
113+
* Assert customers data.
114+
*
115+
* @param array $data
116+
* @return void
117+
*/
118+
private function assertCustomersData(array $data): void
119+
{
120+
foreach ($data['items'] as $customerId => $expectedData) {
121+
$customer = $this->customerRepository->getById($customerId);
122+
$this->assertEquals($expectedData['email'], $customer->getEmail());
123+
$this->assertEquals($expectedData['group_id'], $customer->getGroupId());
124+
$this->assertEquals($expectedData['website_id'], $customer->getWebsiteId());
125+
$this->assertEquals($expectedData['taxvat'], $customer->getTaxvat());
126+
$this->assertEquals($expectedData['gender'], $customer->getGender());
127+
}
128+
}
129+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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\Subscriber;
14+
use Magento\Newsletter\Model\SubscriberFactory;
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 SubscriberFactory */
31+
private $subscriberFactory;
32+
33+
/** @var CustomerRepositoryInterface */
34+
private $customerRepository;
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
protected function setUp()
40+
{
41+
parent::setUp();
42+
43+
$this->objectManager = Bootstrap::getObjectManager();
44+
$this->subscriberFactory = $this->objectManager->get(SubscriberFactory::class);
45+
$this->customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class);
46+
}
47+
48+
/**
49+
* @magentoDataFixture Magento/Newsletter/_files/two_subscribers.php
50+
*
51+
* @return void
52+
*/
53+
public function testMassUnsubscribeAction(): void
54+
{
55+
$firstCustomer = $this->customerRepository->get('customer@example.com');
56+
$secondCustomer = $this->customerRepository->get('customer_two@example.com');
57+
$params = [
58+
'selected' => [
59+
$firstCustomer->getId(),
60+
$secondCustomer->getId(),
61+
],
62+
'namespace' => 'customer_listing',
63+
];
64+
$this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST);
65+
$this->dispatch('backend/customer/index/massUnsubscribe');
66+
$this->assertRedirect($this->stringContains('backend/customer/index/index'));
67+
$this->assertSessionMessages(
68+
self::equalTo(['A total of 2 record(s) were updated.']),
69+
MessageInterface::TYPE_SUCCESS
70+
);
71+
$this->assertEquals(
72+
Subscriber::STATUS_UNSUBSCRIBED,
73+
$this->subscriberFactory->create()
74+
->loadByEmail('customer@example.com')->getSubscriberStatus()
75+
);
76+
$this->assertEquals(
77+
Subscriber::STATUS_UNSUBSCRIBED,
78+
$this->subscriberFactory->create()
79+
->loadByEmail('customer_two@example.com')->getSubscriberStatus()
80+
);
81+
}
82+
83+
/**
84+
* @return void
85+
*/
86+
public function testMassSubscriberActionNoSelection(): void
87+
{
88+
$params = [
89+
'namespace' => 'customer_listing'
90+
];
91+
$this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST);
92+
$this->dispatch('backend/customer/index/massUnsubscribe');
93+
$this->assertRedirect($this->stringContains('backend/customer/index/index'));
94+
$this->assertSessionMessages(
95+
self::equalTo(['An item needs to be selected. Select and try again.']),
96+
MessageInterface::TYPE_ERROR
97+
);
98+
}
99+
}
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/two_customers.php';
12+
13+
$objectManager = Bootstrap::getObjectManager();
14+
/** @var SubscriberFactory $subscriberFactory */
15+
$subscriberFactory = $objectManager->get(SubscriberFactory::class);
16+
17+
$subscriberFactory->create()->subscribe('customer@example.com');
18+
$subscriberFactory->create()->subscribe('customer_two@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/two_customers_rollback.php';

0 commit comments

Comments
 (0)