88namespace Magento \Newsletter \Model ;
99
1010use Magento \Customer \Api \CustomerRepositoryInterface ;
11- use Magento \Framework \Mail \EmailMessage ;
1211use Magento \Framework \Exception \LocalizedException ;
1312use Magento \Framework \Exception \NoSuchEntityException ;
14- use Magento \Framework \ObjectManagerInterface ;
13+ use Magento \Framework \Mail \EmailMessage ;
14+ use Magento \Newsletter \Model \ResourceModel \Subscriber \CollectionFactory ;
1515use Magento \TestFramework \Helper \Bootstrap ;
1616use Magento \TestFramework \Mail \Template \TransportBuilderMock ;
1717use PHPUnit \Framework \TestCase ;
@@ -26,27 +26,42 @@ class SubscriberTest extends TestCase
2626 private const CONFIRMATION_SUBSCRIBE = 'You have been successfully subscribed to our newsletter. ' ;
2727 private const CONFIRMATION_UNSUBSCRIBE = 'You have been unsubscribed from the newsletter. ' ;
2828
29- /** @var ObjectManagerInterface */
30- private $ objectManager ;
31-
32- /** @var SubscriberFactory */
29+ /**
30+ * @var SubscriberFactory
31+ */
3332 private $ subscriberFactory ;
3433
35- /** @var TransportBuilderMock */
34+ /**
35+ * @var TransportBuilderMock
36+ */
3637 private $ transportBuilder ;
3738
38- /** @var CustomerRepositoryInterface */
39+ /**
40+ * @var CustomerRepositoryInterface
41+ */
3942 private $ customerRepository ;
4043
44+ /**
45+ * @var QueueFactory
46+ */
47+ private $ queueFactory ;
48+
49+ /**
50+ * @var CollectionFactory
51+ */
52+ private $ subscriberCollectionFactory ;
53+
4154 /**
4255 * @inheritdoc
4356 */
4457 protected function setUp (): void
4558 {
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);
5065 }
5166
5267 /**
@@ -157,6 +172,37 @@ public function testConfirm(): void
157172 );
158173 }
159174
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 ('customer@example.com ' , 'subscriber_email ' );
187+ $ secondSubscriber = $ this ->subscriberFactory ->create ()
188+ ->load ('customer_two@example.com ' , '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+ 'customer@example.com ' ,
202+ $ collection ->getFirstItem ()->getData ('subscriber_email ' )
203+ );
204+ }
205+
160206 /**
161207 * @magentoDataFixture Magento/Customer/_files/customer_confirmation_config_enable.php
162208 * @magentoDataFixture Magento/Newsletter/_files/newsletter_unconfirmed_customer.php
0 commit comments