1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Customer\Controller\Adminhtml\Index;
use Magento\Backend\Model\Session;
use Magento\Framework\Message\MessageInterface;
use Magento\Newsletter\Model\Subscriber;
use Magento\Newsletter\Model\SubscriberFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
/**
* @magentoAppArea adminhtml
*/
class MassSubscribeTest extends \Magento\TestFramework\TestCase\AbstractBackendController
{
/**
* Base controller URL
*
* @var string
*/
protected $baseControllerUrl = 'http://localhost/index.php/backend/customer/index/index';
protected function tearDown()
{
/**
* Unset customer data
*/
Bootstrap::getObjectManager()->get(Session::class)->setCustomerData(null);
/**
* Unset messages
*/
Bootstrap::getObjectManager()->get(Session::class)->getMessages(true);
}
/**
* Tests subscriber status of customers.
*
* @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
* @magentoDbIsolation disabled
*/
public function testMassSubscriberAction()
{
/** @var SubscriberFactory $subscriberFactory */
$subscriberFactory = Bootstrap::getObjectManager()->get(SubscriberFactory::class);
$customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
$this->assertNull(
$subscriberFactory->create()
->loadByEmail('customer1@example.com')
->getSubscriberStatus()
);
$this->assertNull(
$subscriberFactory->create()
->loadByEmail('customer2@example.com')
->getSubscriberStatus()
);
/** @var CustomerInterface $customer1 */
$customer1 = $customerRepository->get('customer1@example.com');
/** @var CustomerInterface $customer2 */
$customer2 = $customerRepository->get('customer2@example.com');
$params = [
'selected' => [
$customer1->getId(),
$customer2->getId(),
],
'namespace' => 'customer_listing',
];
$this->getRequest()->setParams($params);
$this->dispatch('backend/customer/index/massSubscribe');
// Assertions
$this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
$this->assertSessionMessages(
self::equalTo(['A total of 2 record(s) were updated.']),
MessageInterface::TYPE_SUCCESS
);
$this->assertEquals(
Subscriber::STATUS_SUBSCRIBED,
$subscriberFactory->create()
->loadByEmail('customer1@example.com')
->getSubscriberStatus()
);
$this->assertEquals(
Subscriber::STATUS_SUBSCRIBED,
$subscriberFactory->create()
->loadByEmail('customer2@example.com')
->getSubscriberStatus()
);
}
/**
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
*/
public function testMassSubscriberActionNoSelection()
{
$params = [
'namespace' => 'customer_listing'
];
$this->getRequest()->setParams($params);
$this->dispatch('backend/customer/index/massSubscribe');
$this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
$this->assertSessionMessages(
self::equalTo(['An item needs to be selected. Select and try again.']),
MessageInterface::TYPE_ERROR
);
}
}