Skip to content

Commit 7527e00

Browse files
committed
MC-30601: Admin: Unsubscribe/subscribe newsletter email
1 parent 5b9be66 commit 7527e00

File tree

5 files changed

+220
-47
lines changed

5 files changed

+220
-47
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Customer\Api\AccountManagementInterface;
9+
use Magento\Customer\Api\CustomerMetadataInterface;
10+
use \Magento\Customer\Model\Data\CustomerFactory;
11+
use Magento\Eav\Model\AttributeRepository;
12+
use Magento\Framework\Math\Random;
13+
use Magento\Store\Api\WebsiteRepositoryInterface;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
16+
$objectManager = Bootstrap::getObjectManager();
17+
/** @var AccountManagementInterface $accountManagment */
18+
$accountManagment = $objectManager->get(AccountManagementInterface::class);
19+
/** @var CustomerFactory $customerFactory */
20+
$customerFactory = $objectManager->get(CustomerFactory::class);
21+
/** @var Random $random */
22+
$random = $objectManager->get(Random::class);
23+
/** @var WebsiteRepositoryInterface $websiteRepository */
24+
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
25+
$customer = $customerFactory->create();
26+
$website = $websiteRepository->get('base');
27+
$defaultStoreId = $website->getDefaultStore()->getId();
28+
/** @var AttributeRepository $attributeRepository */
29+
$attributeRepository = $objectManager->get(AttributeRepository::class);
30+
$gender = $attributeRepository->get(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'gender')
31+
->getSource()->getOptionId('Male');
32+
33+
$customer->setWebsiteId($website->getId())
34+
->setEmail('unconfirmedcustomer@example.com')
35+
->setGroupId(1)
36+
->setStoreId($defaultStoreId)
37+
->setPrefix('Mr.')
38+
->setFirstname('John')
39+
->setMiddlename('A')
40+
->setLastname('Smith')
41+
->setSuffix('Esq.')
42+
->setDefaultBilling(1)
43+
->setDefaultShipping(1)
44+
->setConfirmation($random->getUniqueHash())
45+
->setGender($gender);
46+
47+
$accountManagment->createAccount($customer, 'Qwert12345');
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Customer\Api\CustomerRepositoryInterface;
9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Magento\Framework\Registry;
11+
use Magento\Store\Api\WebsiteRepositoryInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
14+
$objectManager = Bootstrap::getObjectManager();
15+
/** @var Registry $registry */
16+
$registry = $objectManager->get(Registry::class);
17+
/** @var CustomerRepositoryInterface $customerRepository */
18+
$customerRepository = $objectManager->get(CustomerRepositoryInterface::class);
19+
/** @var WebsiteRepositoryInterface $websiteRepository */
20+
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
21+
$websiteId = $websiteRepository->get('base')->getId();
22+
$registry->unregister('isSecureArea');
23+
$registry->register('isSecureArea', true);
24+
25+
try {
26+
$customer = $customerRepository->get('unconfirmedcustomer@example.com', $websiteId);
27+
$customerRepository->delete($customer);
28+
} catch (NoSuchEntityException $e) {
29+
//customer already deleted
30+
}
31+
32+
$registry->unregister('isSecureArea');
33+
$registry->register('isSecureArea', false);

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()->getBody()->getParts()[0]->getRawContent()
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()->getBody()->getParts()[0]->getRawContent()
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()->getBody()->getParts()[0]->getRawContent()
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()->getBody()->getParts()[0]->getRawContent()
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()->getBody()->getParts()[0]->getRawContent()
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)