Prevents sending to unsubscribed subscribers
This commit is contained in:
@ -8,6 +8,7 @@ use MailPoet\Mailer\MailerLog;
|
||||
use MailPoet\Models\SendingQueue as SendingQueueModel;
|
||||
use MailPoet\Models\StatisticsNewsletters as StatisticsNewslettersModel;
|
||||
use MailPoet\Models\Subscriber as SubscriberModel;
|
||||
use MailPoet\Models\Subscriber;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
@ -42,6 +43,7 @@ class SendingQueue {
|
||||
);
|
||||
foreach($subscriber_batches as $subscribers_to_process_ids) {
|
||||
$found_subscribers = SubscriberModel::whereIn('id', $subscribers_to_process_ids)
|
||||
->where('status', Subscriber::STATUS_SUBSCRIBED)
|
||||
->whereNull('deleted_at')
|
||||
->findMany();
|
||||
$found_subscribers_ids = array_map(function($subscriber) {
|
||||
|
@ -24,6 +24,7 @@ class SendingQueueTest extends MailPoetTest {
|
||||
$this->subscriber->email = 'john@doe.com';
|
||||
$this->subscriber->first_name = 'John';
|
||||
$this->subscriber->last_name = 'Doe';
|
||||
$this->subscriber->status = Subscriber::STATUS_SUBSCRIBED;
|
||||
$this->subscriber->save();
|
||||
$this->newsletter = Newsletter::create();
|
||||
$this->newsletter->type = Newsletter::TYPE_STANDARD;
|
||||
@ -362,6 +363,29 @@ class SendingQueueTest extends MailPoetTest {
|
||||
expect((int)$updated_queue->count_total)->equals(0);
|
||||
}
|
||||
|
||||
function testItDoesNotSendToUnsubscribedSubscribers() {
|
||||
$sending_queue_worker = $this->sending_queue_worker;
|
||||
$sending_queue_worker->mailer_task = Stub::make(
|
||||
new MailerTask(),
|
||||
array('send' => function($newsletter, $subscriber) { return true; })
|
||||
);
|
||||
|
||||
// newsletter is sent to existing subscriber
|
||||
$sending_queue_worker->process();
|
||||
$updated_queue = SendingQueue::findOne($this->queue->id);
|
||||
expect((int)$updated_queue->count_total)->equals(1);
|
||||
|
||||
// newsletter is not sent to trashed subscriber
|
||||
$this->_after();
|
||||
$this->_before();
|
||||
$subscriber = $this->subscriber;
|
||||
$subscriber->status = Subscriber::STATUS_UNSUBSCRIBED;
|
||||
$subscriber->save();
|
||||
$sending_queue_worker->process();
|
||||
$updated_queue = SendingQueue::findOne($this->queue->id);
|
||||
expect((int)$updated_queue->count_total)->equals(0);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||
ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table);
|
||||
|
Reference in New Issue
Block a user