Skip to content

Commit 35468fc

Browse files
1 parent 10a7065 commit 35468fc

File tree

3 files changed

+117
-12
lines changed

3 files changed

+117
-12
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
namespace Magento\Newsletter\Model\Plugin;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\DB\Adapter\AdapterInterface;
12+
use Magento\Newsletter\Model\Subscriber;
13+
14+
/**
15+
* Plugin responsible for removing subscriber from queue after unsubscribe
16+
*/
17+
class RemoveSubscriberFromQueue
18+
{
19+
private const STATUS = 'subscriber_status';
20+
21+
/**
22+
* @var AdapterInterface
23+
*/
24+
private $connection;
25+
26+
/**
27+
* @param ResourceConnection $resource
28+
*/
29+
public function __construct(ResourceConnection $resource)
30+
{
31+
$this->connection = $resource->getConnection();
32+
}
33+
34+
/**
35+
* Removes subscriber from queue
36+
*
37+
* @param Subscriber $subject
38+
* @param Subscriber $subscriber
39+
* @return Subscriber
40+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
41+
*/
42+
public function afterUnsubscribe(Subscriber $subject, Subscriber $subscriber): Subscriber
43+
{
44+
if ($subscriber->dataHasChangedFor(self::STATUS)
45+
&& $subscriber->getSubscriberStatus() === Subscriber::STATUS_UNSUBSCRIBED
46+
) {
47+
$this->connection->delete(
48+
$this->connection->getTableName('newsletter_queue_link'),
49+
['subscriber_id = ?' => $subscriber->getId(), 'letter_sent_at IS NULL']
50+
);
51+
}
52+
53+
return $subscriber;
54+
}
55+
}

app/code/Magento/Newsletter/etc/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@
3232
</type>
3333
<preference for="Magento\Newsletter\Model\SubscriptionManagerInterface"
3434
type="Magento\Newsletter\Model\SubscriptionManager"/>
35+
<type name="Magento\Newsletter\Model\Subscriber">
36+
<plugin name="remove_subscriber_from_queue_after_unsubscribe"
37+
type="Magento\Newsletter\Model\Plugin\RemoveSubscriberFromQueue"/>
38+
</type>
3539
</config>

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

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
namespace Magento\Newsletter\Model;
99

1010
use Magento\Customer\Api\CustomerRepositoryInterface;
11-
use Magento\Framework\Mail\EmailMessage;
1211
use Magento\Framework\Exception\LocalizedException;
1312
use Magento\Framework\Exception\NoSuchEntityException;
14-
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\Framework\Mail\EmailMessage;
14+
use Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory;
1515
use Magento\TestFramework\Helper\Bootstrap;
1616
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
1717
use 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('[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+
160206
/**
161207
* @magentoDataFixture Magento/Customer/_files/customer_confirmation_config_enable.php
162208
* @magentoDataFixture Magento/Newsletter/_files/newsletter_unconfirmed_customer.php

0 commit comments

Comments
 (0)