-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathNewActionTest.php
320 lines (286 loc) · 9.96 KB
/
NewActionTest.php
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
* Copyright 2020 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento\Newsletter\Controller\Subscriber;
use Exception;
use Laminas\Stdlib\Parameters;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\AccountManagement;
use Magento\Customer\Model\Session;
use Magento\Customer\Model\Url;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Newsletter\Model\ResourceModel\Subscriber as SubscriberResource;
use Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory;
use Magento\Newsletter\Model\ResourceModel\Subscriber\Grid\Collection as GridCollection;
use Magento\Store\Model\ScopeInterface;
use Magento\TestFramework\TestCase\AbstractController;
/**
* Class checks subscription behaviour from frontend
*
* @magentoDbIsolation enabled
* @see \Magento\Newsletter\Controller\Subscriber\NewAction
*/
class NewActionTest extends AbstractController
{
/** @var CustomerRepositoryInterface */
private $customerRepository;
/** @var Session */
private $session;
/** @var CollectionFactory */
private $subscriberCollectionFactory;
/** @var SubscriberResource */
private $subscriberResource;
/** @var string|null */
private $subscriberToDelete;
/** @var Url */
private $customerUrl;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->session = $this->_objectManager->get(Session::class);
$this->subscriberCollectionFactory = $this->_objectManager->get(CollectionFactory::class);
$this->subscriberResource = $this->_objectManager->get(SubscriberResource::class);
$this->customerRepository = $this->_objectManager->get(CustomerRepositoryInterface::class);
$this->customerUrl = $this->_objectManager->get(Url::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
if ($this->subscriberToDelete) {
$this->deleteSubscriber($this->subscriberToDelete);
}
parent::tearDown();
}
/**
* @dataProvider subscribersDataProvider
*
* @param string $email
* @param string $expectedMessage
* @return void
*/
public function testNewAction(string $email, string $expectedMessage): void
{
$this->subscriberToDelete = $email ? $email : null;
$this->prepareRequest($email);
$this->dispatch('newsletter/subscriber/new');
$this->performAsserts($expectedMessage);
}
/**
* @magentoConfigFixture current_store newsletter/general/active 1
*
* @return void
*/
public function testNewActionWithSubscriptionConfigEnabled(): void
{
$email = '[email protected]';
$this->subscriberToDelete = $email;
$this->prepareRequest($email);
$this->dispatch('newsletter/subscriber/new');
$subscriberCollection = $this->subscriberCollectionFactory->create();
$subscriberCollection->addFieldToFilter('subscriber_email', $email)->setPageSize(1);
$this->assertEquals(1, count($subscriberCollection));
$this->assertEquals($email, $subscriberCollection->getFirstItem()->getEmail());
}
/**
* @magentoConfigFixture current_store newsletter/general/active 0
*
* @return void
*/
public function testNewActionWithSubscriptionConfigDisabled(): void
{
$email = '[email protected]';
$this->prepareRequest($email);
$this->dispatch('newsletter/subscriber/new');
$subscriberCollection = $this->subscriberCollectionFactory->create();
$subscriberCollection->addFieldToFilter('subscriber_email', $email)->setPageSize(1);
$this->assertEquals(0, count($subscriberCollection));
}
/**
* @return array
*/
public static function subscribersDataProvider(): array
{
return [
'without_email' => [
'email' => '',
'expectedMessage' => '',
],
'with_unused_email' => [
'email' => '[email protected]',
'expectedMessage' => 'Thank you for your subscription.',
],
'with_invalid_email' => [
'email' => 'invalid_email.com',
'expectedMessage' => 'Please enter a valid email address.'
],
];
}
/**
* @magentoDataFixture Magento/Customer/_files/new_customer.php
* @dataProvider emailAndStatusDataProvider
*
* @return void
*/
public function testNewActionUsedEmail($email, $subscriptionType): void
{
$this->prepareRequest($email);
$this->dispatch('newsletter/subscriber/new');
/** @var GridCollection $gridCollection */
$gridCollection = $this->_objectManager->create(GridCollection::class);
$item = $gridCollection->getFirstItem();
self::assertEquals($subscriptionType, (int)$item->getType());
$this->performAsserts('Thank you for your subscription.');
}
/**
* @return array
*/
public static function emailAndStatusDataProvider()
{
return [
'email' => ['[email protected]', 2],
'subscriptionType' => ['[email protected]', 1],
];
}
/**
* @magentoDataFixture Magento/Customer/_files/new_customer.php
*
* @return void
*/
public function testNewActionOwnerEmail(): void
{
$this->prepareRequest('[email protected]');
$this->session->loginById(1);
$this->dispatch('newsletter/subscriber/new');
$this->performAsserts('Thank you for your subscription.');
}
/**
* @magentoDataFixture Magento/Newsletter/_files/customer_with_subscription.php
*
* @return void
*/
public function testAlreadyExistEmail(): void
{
$this->prepareRequest('[email protected]');
$this->dispatch('newsletter/subscriber/new');
$this->performAsserts('This email address is already subscribed.');
}
/**
* @magentoConfigFixture current_store newsletter/subscription/allow_guest_subscribe 0
*
* @return void
*/
public function testWithNotAllowedGuestSubscription(): void
{
$message = sprintf(
'Sorry, but the administrator denied subscription for guests. Please <a href="%s">register</a>.',
$this->customerUrl->getRegisterUrl()
);
$this->subscriberToDelete = '[email protected]';
$this->prepareRequest('[email protected]');
$this->dispatch('newsletter/subscriber/new');
$this->performAsserts($message);
}
/**
* @magentoConfigFixture current_store newsletter/subscription/allow_guest_subscribe 0
*
* @magentoDataFixture Magento/Customer/_files/new_customer.php
*
* @return void
*/
public function testCustomerSubscribeUnrelatedEmailWithNotAllowedGuestSubscription(): void
{
$this->markTestSkipped('Blocked by MC-31662');
$this->subscriberToDelete = '[email protected]';
$this->session->loginById($this->customerRepository->get('[email protected]')->getId());
$this->prepareRequest('[email protected]');
$this->dispatch('newsletter/subscriber/new');
//ToDo message need to be specified after bug MC-31662 fixing
$this->performAsserts('');
}
/**
* @magentoConfigFixture current_store newsletter/subscription/confirm 1
*
* @return void
*/
public function testWithRequiredConfirmation(): void
{
$this->subscriberToDelete = '[email protected]';
$this->prepareRequest('[email protected]');
$this->dispatch('newsletter/subscriber/new');
$this->performAsserts('The confirmation request has been sent.');
}
/**
* @magentoDataFixture Magento/Newsletter/_files/three_subscribers.php
*
* @return void
*/
public function testWithEmailAssignedToAnotherCustomer(): void
{
$this->session->loginById(1);
$this->prepareRequest('[email protected]');
$this->dispatch('newsletter/subscriber/new');
$scopeConfig = $this->_objectManager->get(ScopeConfigInterface::class);
$guestLoginConfig = $scopeConfig->getValue(
AccountManagement::GUEST_CHECKOUT_LOGIN_OPTION_SYS_CONFIG,
ScopeInterface::SCOPE_WEBSITE,
1
);
if ($guestLoginConfig) {
$this->performAsserts('This email address is already assigned to another user.');
} else {
$this->performAsserts('This email address is already subscribed.');
}
}
/**
* Prepare request
*
* @param string $email
* @return void
*/
private function prepareRequest(string $email): void
{
$parameters = $this->_objectManager->create(Parameters::class);
$parameters->set('HTTP_REFERER', 'http://localhost/testRedirect');
$this->getRequest()->setServer($parameters);
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setPostValue(['email' => $email]);
}
/**
* Assert session message and expected redirect
*
* @param string $message
* @return void
*/
private function performAsserts(string $message): void
{
if ($message) {
$this->assertSessionMessages($this->equalTo([(string)__($message)]));
}
$this->assertRedirect($this->equalTo('http://localhost/testRedirect'));
}
/**
* Delete subscribers by email
*
* @param string $email
*
* @return void
* @throws Exception
*/
private function deleteSubscriber(string $email): void
{
$collection = $this->subscriberCollectionFactory->create();
$item = $collection->addFieldToFilter('subscriber_email', $email)->setPageSize(1)->getFirstItem();
if ($item->getId()) {
$this->subscriberResource->delete($item);
}
}
}