|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | +declare(strict_types=1); |
| 7 | + |
6 | 8 | namespace Magento\Newsletter\Model;
|
7 | 9 |
|
| 10 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 11 | +use Magento\Framework\ObjectManagerInterface; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
8 | 13 | use Magento\TestFramework\Mail\Template\TransportBuilderMock;
|
| 14 | +use PHPUnit\Framework\TestCase; |
9 | 15 |
|
10 | 16 | /**
|
11 |
| - * \Magento\Newsletter\Model\Subscriber tests |
| 17 | + * Class checks subscription behavior. |
| 18 | + * |
| 19 | + * @see \Magento\Newsletter\Model\Subscriber |
12 | 20 | */
|
13 |
| -class SubscriberTest extends \PHPUnit\Framework\TestCase |
| 21 | +class SubscriberTest extends TestCase |
14 | 22 | {
|
| 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 | + |
15 | 35 | /**
|
16 |
| - * @var Subscriber |
| 36 | + * @inheritdoc |
17 | 37 | */
|
18 |
| - private $model; |
19 |
| - |
20 | 38 | protected function setUp()
|
21 | 39 | {
|
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); |
25 | 44 | }
|
26 | 45 |
|
27 | 46 | /**
|
28 |
| - * @magentoDataFixture Magento/Newsletter/_files/subscribers.php |
29 | 47 | * @magentoConfigFixture current_store newsletter/subscription/confirm 1
|
| 48 | + * |
| 49 | + * @magentoDataFixture Magento/Newsletter/_files/subscribers.php |
| 50 | + * |
| 51 | + * @return void |
30 | 52 | */
|
31 |
| - public function testEmailConfirmation() |
| 53 | + public function testEmailConfirmation(): void |
32 | 54 | {
|
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'); |
37 | 57 | // confirmationCode 'ysayquyajua23iq29gxwu2eax2qb6gvy' is taken from fixture
|
38 | 58 | $this->assertContains(
|
39 |
| - '/newsletter/subscriber/confirm/id/' . $this->model->getSubscriberId() |
| 59 | + '/newsletter/subscriber/confirm/id/' . $subscriber->getSubscriberId() |
40 | 60 | . '/code/ysayquyajua23iq29gxwu2eax2qb6gvy',
|
41 |
| - $transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent() |
| 61 | + $this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent() |
42 | 62 | );
|
43 |
| - $this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $this->model->getSubscriberStatus()); |
| 63 | + $this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $subscriber->getSubscriberStatus()); |
44 | 64 | }
|
45 | 65 |
|
46 | 66 | /**
|
47 | 67 | * @magentoDataFixture Magento/Newsletter/_files/subscribers.php
|
| 68 | + * |
| 69 | + * @return void |
48 | 70 | */
|
49 |
| - public function testLoadByCustomerId() |
| 71 | + public function testLoadByCustomerId(): void |
50 | 72 | {
|
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()); |
53 | 76 | }
|
54 | 77 |
|
55 | 78 | /**
|
56 | 79 | * @magentoDataFixture Magento/Newsletter/_files/subscribers.php
|
57 |
| - * @magentoAppArea frontend |
| 80 | + * |
| 81 | + * @magentoAppArea frontend |
| 82 | + * |
| 83 | + * @return void |
58 | 84 | */
|
59 |
| - public function testUnsubscribeSubscribe() |
| 85 | + public function testUnsubscribeSubscribe(): void |
60 | 86 | {
|
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()); |
66 | 95 | // 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 | + ); |
69 | 102 | }
|
70 | 103 |
|
71 | 104 | /**
|
72 | 105 | * @magentoDataFixture Magento/Newsletter/_files/subscribers.php
|
73 |
| - * @magentoAppArea frontend |
| 106 | + * |
| 107 | + * @magentoAppArea frontend |
| 108 | + * |
| 109 | + * @return void |
74 | 110 | */
|
75 |
| - public function testUnsubscribeSubscribeByCustomerId() |
| 111 | + public function testUnsubscribeSubscribeByCustomerId(): void |
76 | 112 | {
|
| 113 | + $subscriber = $this->subscriberFactory->create(); |
77 | 114 | // 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 | + ); |
81 | 121 | // 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 | + ); |
84 | 128 | }
|
85 | 129 |
|
86 | 130 | /**
|
87 |
| - * @magentoDataFixture Magento/Newsletter/_files/subscribers.php |
88 | 131 | * @magentoConfigFixture current_store newsletter/subscription/confirm 1
|
| 132 | + * |
| 133 | + * @magentoDataFixture Magento/Newsletter/_files/subscribers.php |
| 134 | + * |
| 135 | + * @return void |
89 | 136 | */
|
90 |
| - public function testConfirm() |
| 137 | + public function testConfirm(): void |
91 | 138 | {
|
| 139 | + $subscriber = $this->subscriberFactory->create(); |
92 | 140 | $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()); |
101 | 144 | $this->assertContains(
|
102 | 145 | 'You have been successfully subscribed to our newsletter.',
|
103 |
| - $transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent() |
| 146 | + $this->transportBuilder->getSentMessage()->getRawMessage() |
104 | 147 | );
|
105 | 148 | }
|
| 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 | + } |
106 | 177 | }
|
0 commit comments