8
8
namespace Magento \Newsletter \Model ;
9
9
10
10
use Magento \Customer \Api \CustomerRepositoryInterface ;
11
- use Magento \Framework \Mail \EmailMessage ;
12
11
use Magento \Framework \Exception \LocalizedException ;
13
12
use Magento \Framework \Exception \NoSuchEntityException ;
14
- use Magento \Framework \ObjectManagerInterface ;
13
+ use Magento \Framework \Mail \EmailMessage ;
14
+ use Magento \Newsletter \Model \ResourceModel \Subscriber \CollectionFactory ;
15
15
use Magento \TestFramework \Helper \Bootstrap ;
16
16
use Magento \TestFramework \Mail \Template \TransportBuilderMock ;
17
17
use PHPUnit \Framework \TestCase ;
@@ -26,27 +26,42 @@ class SubscriberTest extends TestCase
26
26
private const CONFIRMATION_SUBSCRIBE = 'You have been successfully subscribed to our newsletter. ' ;
27
27
private const CONFIRMATION_UNSUBSCRIBE = 'You have been unsubscribed from the newsletter. ' ;
28
28
29
- /** @var ObjectManagerInterface */
30
- private $ objectManager ;
31
-
32
- /** @var SubscriberFactory */
29
+ /**
30
+ * @var SubscriberFactory
31
+ */
33
32
private $ subscriberFactory ;
34
33
35
- /** @var TransportBuilderMock */
34
+ /**
35
+ * @var TransportBuilderMock
36
+ */
36
37
private $ transportBuilder ;
37
38
38
- /** @var CustomerRepositoryInterface */
39
+ /**
40
+ * @var CustomerRepositoryInterface
41
+ */
39
42
private $ customerRepository ;
40
43
44
+ /**
45
+ * @var QueueFactory
46
+ */
47
+ private $ queueFactory ;
48
+
49
+ /**
50
+ * @var CollectionFactory
51
+ */
52
+ private $ subscriberCollectionFactory ;
53
+
41
54
/**
42
55
* @inheritdoc
43
56
*/
44
57
protected function setUp (): void
45
58
{
46
- $ this ->objectManager = Bootstrap::getObjectManager ();
47
- $ this ->subscriberFactory = $ this ->objectManager ->get (SubscriberFactory::class);
48
- $ this ->transportBuilder = $ this ->objectManager ->get (TransportBuilderMock::class);
49
- $ this ->customerRepository = $ this ->objectManager ->get (CustomerRepositoryInterface::class);
59
+ $ objectManager = Bootstrap::getObjectManager ();
60
+ $ this ->subscriberFactory = $ objectManager ->get (SubscriberFactory::class);
61
+ $ this ->transportBuilder = $ objectManager ->get (TransportBuilderMock::class);
62
+ $ this ->customerRepository = $ objectManager ->get (CustomerRepositoryInterface::class);
63
+ $ this ->queueFactory = $ objectManager ->get (QueueFactory::class);
64
+ $ this ->subscriberCollectionFactory = $ objectManager ->get (CollectionFactory::class);
50
65
}
51
66
52
67
/**
@@ -157,6 +172,37 @@ public function testConfirm(): void
157
172
);
158
173
}
159
174
175
+ /**
176
+ * Unsubscribe and check queue
177
+ *
178
+ * @magentoDataFixture Magento/Newsletter/_files/queue.php
179
+ * @magentoDbIsolation disabled
180
+ *
181
+ * @return void
182
+ */
183
+ public function testUnsubscribeCustomer (): void
184
+ {
185
+ $ firstSubscriber = $ this ->subscriberFactory ->create ()
186
+ ->
load (
'[email protected] ' ,
'subscriber_email ' );
187
+ $ secondSubscriber = $ this ->subscriberFactory ->create ()
188
+ ->
load (
'[email protected] ' ,
'subscriber_email ' );
189
+
190
+ $ queue = $ this ->queueFactory ->create ()
191
+ ->load ('CustomerSupport ' , 'newsletter_sender_name ' );
192
+ $ queue ->addSubscribersToQueue ([$ firstSubscriber ->getId (), $ secondSubscriber ->getId ()]);
193
+
194
+ $ secondSubscriber ->unsubscribe ();
195
+
196
+ $ collection = $ this ->subscriberCollectionFactory ->create ()
197
+ ->useQueue ($ queue );
198
+
199
+ $ this ->assertCount (1 , $ collection );
200
+ $ this ->assertEquals (
201
+
202
+ $ collection ->getFirstItem ()->getData ('subscriber_email ' )
203
+ );
204
+ }
205
+
160
206
/**
161
207
* @magentoDataFixture Magento/Customer/_files/customer_confirmation_config_enable.php
162
208
* @magentoDataFixture Magento/Newsletter/_files/newsletter_unconfirmed_customer.php
0 commit comments