Skip to content

Commit 811097d

Browse files
committed
MC-31752: Admin: Unsubscribe/subscribe newsletter email
1 parent 555a134 commit 811097d

File tree

4 files changed

+141
-48
lines changed

4 files changed

+141
-48
lines changed

dev/tests/integration/testsuite/Magento/Customer/_files/unconfirmed_customer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use Magento\Customer\Api\AccountManagementInterface;
99
use Magento\Customer\Api\CustomerMetadataInterface;
10-
use \Magento\Customer\Model\Data\CustomerFactory;
10+
use Magento\Customer\Model\Data\CustomerFactory;
1111
use Magento\Eav\Model\AttributeRepository;
1212
use Magento\Framework\Math\Random;
1313
use Magento\Store\Api\WebsiteRepositoryInterface;

dev/tests/integration/testsuite/Magento/Newsletter/Model/SubscriberTest.php

Lines changed: 118 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,104 +3,175 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Newsletter\Model;
79

10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
813
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
14+
use PHPUnit\Framework\TestCase;
915

1016
/**
11-
* \Magento\Newsletter\Model\Subscriber tests
17+
* Class checks subscription behavior.
18+
*
19+
* @see \Magento\Newsletter\Model\Subscriber
1220
*/
13-
class SubscriberTest extends \PHPUnit\Framework\TestCase
21+
class SubscriberTest extends TestCase
1422
{
23+
/** @var ObjectManagerInterface */
24+
private $objectManager;
25+
26+
/** @var SubscriberFactory */
27+
private $subscriberFactory;
28+
29+
/** @var TransportBuilderMock */
30+
private $transportBuilder;
31+
32+
/** @var CustomerRepositoryInterface */
33+
private $customerRepository;
34+
1535
/**
16-
* @var Subscriber
36+
* @inheritdoc
1737
*/
18-
private $model;
19-
2038
protected function setUp()
2139
{
22-
$this->model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
23-
\Magento\Newsletter\Model\Subscriber::class
24-
);
40+
$this->objectManager = Bootstrap::getObjectManager();
41+
$this->subscriberFactory = $this->objectManager->get(SubscriberFactory::class);
42+
$this->transportBuilder = $this->objectManager->get(TransportBuilderMock::class);
43+
$this->customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class);
2544
}
2645

2746
/**
28-
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php
2947
* @magentoConfigFixture current_store newsletter/subscription/confirm 1
48+
*
49+
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php
50+
*
51+
* @return void
3052
*/
31-
public function testEmailConfirmation()
53+
public function testEmailConfirmation(): void
3254
{
33-
$this->model->subscribe('customer_confirm@example.com');
34-
/** @var TransportBuilderMock $transportBuilder */
35-
$transportBuilder = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
36-
->get(\Magento\TestFramework\Mail\Template\TransportBuilderMock::class);
55+
$subscriber = $this->subscriberFactory->create();
56+
$subscriber->subscribe('customer_confirm@example.com');
3757
// confirmationCode 'ysayquyajua23iq29gxwu2eax2qb6gvy' is taken from fixture
3858
$this->assertContains(
39-
'/newsletter/subscriber/confirm/id/' . $this->model->getSubscriberId()
59+
'/newsletter/subscriber/confirm/id/' . $subscriber->getSubscriberId()
4060
. '/code/ysayquyajua23iq29gxwu2eax2qb6gvy',
41-
$transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent()
61+
$this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent()
4262
);
43-
$this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $this->model->getSubscriberStatus());
63+
$this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $subscriber->getSubscriberStatus());
4464
}
4565

4666
/**
4767
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php
68+
*
69+
* @return void
4870
*/
49-
public function testLoadByCustomerId()
71+
public function testLoadByCustomerId(): void
5072
{
51-
$this->assertSame($this->model, $this->model->loadByCustomerId(1));
52-
$this->assertEquals('customer@example.com', $this->model->getSubscriberEmail());
73+
$subscriber = $this->subscriberFactory->create();
74+
$this->assertSame($subscriber, $subscriber->loadByCustomerId(1));
75+
$this->assertEquals('customer@example.com', $subscriber->getSubscriberEmail());
5376
}
5477

5578
/**
5679
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php
57-
* @magentoAppArea frontend
80+
*
81+
* @magentoAppArea frontend
82+
*
83+
* @return void
5884
*/
59-
public function testUnsubscribeSubscribe()
85+
public function testUnsubscribeSubscribe(): void
6086
{
61-
// Unsubscribe and verify
62-
$this->assertSame($this->model, $this->model->loadByCustomerId(1));
63-
$this->assertEquals($this->model, $this->model->unsubscribe());
64-
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $this->model->getSubscriberStatus());
65-
87+
$subscriber = $this->subscriberFactory->create();
88+
$this->assertSame($subscriber, $subscriber->loadByCustomerId(1));
89+
$this->assertEquals($subscriber, $subscriber->unsubscribe());
90+
$this->assertContains(
91+
'You have been unsubscribed from the newsletter.',
92+
$this->transportBuilder->getSentMessage()->getRawMessage()
93+
);
94+
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $subscriber->getSubscriberStatus());
6695
// Subscribe and verify
67-
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $this->model->subscribe('customer@example.com'));
68-
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $this->model->getSubscriberStatus());
96+
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->subscribe('customer@example.com'));
97+
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getSubscriberStatus());
98+
$this->assertContains(
99+
'You have been successfully subscribed to our newsletter.',
100+
$this->transportBuilder->getSentMessage()->getRawMessage()
101+
);
69102
}
70103

71104
/**
72105
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php
73-
* @magentoAppArea frontend
106+
*
107+
* @magentoAppArea frontend
108+
*
109+
* @return void
74110
*/
75-
public function testUnsubscribeSubscribeByCustomerId()
111+
public function testUnsubscribeSubscribeByCustomerId(): void
76112
{
113+
$subscriber = $this->subscriberFactory->create();
77114
// Unsubscribe and verify
78-
$this->assertSame($this->model, $this->model->unsubscribeCustomerById(1));
79-
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $this->model->getSubscriberStatus());
80-
115+
$this->assertSame($subscriber, $subscriber->unsubscribeCustomerById(1));
116+
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $subscriber->getSubscriberStatus());
117+
$this->assertContains(
118+
'You have been unsubscribed from the newsletter.',
119+
$this->transportBuilder->getSentMessage()->getRawMessage()
120+
);
81121
// Subscribe and verify
82-
$this->assertSame($this->model, $this->model->subscribeCustomerById(1));
83-
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $this->model->getSubscriberStatus());
122+
$this->assertSame($subscriber, $subscriber->subscribeCustomerById(1));
123+
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getSubscriberStatus());
124+
$this->assertContains(
125+
'You have been successfully subscribed to our newsletter.',
126+
$this->transportBuilder->getSentMessage()->getRawMessage()
127+
);
84128
}
85129

86130
/**
87-
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php
88131
* @magentoConfigFixture current_store newsletter/subscription/confirm 1
132+
*
133+
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php
134+
*
135+
* @return void
89136
*/
90-
public function testConfirm()
137+
public function testConfirm(): void
91138
{
139+
$subscriber = $this->subscriberFactory->create();
92140
$customerEmail = 'customer_confirm@example.com';
93-
$this->model->subscribe($customerEmail);
94-
$this->model->loadByEmail($customerEmail);
95-
$this->model->confirm($this->model->getSubscriberConfirmCode());
96-
97-
$transportBuilder = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
98-
\Magento\TestFramework\Mail\Template\TransportBuilderMock::class
99-
);
100-
141+
$subscriber->subscribe($customerEmail);
142+
$subscriber->loadByEmail($customerEmail);
143+
$subscriber->confirm($subscriber->getSubscriberConfirmCode());
101144
$this->assertContains(
102145
'You have been successfully subscribed to our newsletter.',
103-
$transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent()
146+
$this->transportBuilder->getSentMessage()->getRawMessage()
104147
);
105148
}
149+
150+
/**
151+
* @magentoDataFixture Magento/Customer/_files/customer_confirmation_config_enable.php
152+
* @magentoDataFixture Magento/Newsletter/_files/newsletter_unconfirmed_customer.php
153+
*
154+
* @return void
155+
*/
156+
public function testSubscribeUnconfirmedCustomerWithSubscription(): void
157+
{
158+
$customer = $this->customerRepository->get('unconfirmedcustomer@example.com');
159+
$subscriber = $this->subscriberFactory->create();
160+
$subscriber->subscribeCustomerById($customer->getId());
161+
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getStatus());
162+
}
163+
164+
/**
165+
* @magentoDataFixture Magento/Customer/_files/customer_confirmation_config_enable.php
166+
* @magentoDataFixture Magento/Customer/_files/unconfirmed_customer.php
167+
*
168+
* @return void
169+
*/
170+
public function testSubscribeUnconfirmedCustomerWithoutSubscription(): void
171+
{
172+
$customer = $this->customerRepository->get('unconfirmedcustomer@example.com');
173+
$subscriber = $this->subscriberFactory->create();
174+
$subscriber->subscribeCustomerById($customer->getId());
175+
$this->assertEquals(Subscriber::STATUS_UNCONFIRMED, $subscriber->getStatus());
176+
}
106177
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
10+
require __DIR__ . '/../../../Magento/Customer/_files/unconfirmed_customer.php';
11+
12+
/** @var SubscriberFactory $subscriberFactory */
13+
$subscriberFactory = $objectManager->get(SubscriberFactory::class);
14+
$subscriberFactory->create()->subscribe('unconfirmedcustomer@example.com');
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/unconfirmed_customer_rollback.php';

0 commit comments

Comments
 (0)