From 9e8aaec6c93453278d783246285f9ed5bdcef76d Mon Sep 17 00:00:00 2001 From: Brezo Cordero <8002881+brezocordero@users.noreply.github.com> Date: Fri, 18 Mar 2022 01:42:14 -0500 Subject: [PATCH] Add lifetime email counts to inactive subscribers criteria [MAILPOET-4177] --- .../InactiveSubscribersController.php | 3 +++ .../InactiveSubscribersControllerTest.php | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mailpoet/lib/Subscribers/InactiveSubscribersController.php b/mailpoet/lib/Subscribers/InactiveSubscribersController.php index 1be55e6607..641afa7475 100644 --- a/mailpoet/lib/Subscribers/InactiveSubscribersController.php +++ b/mailpoet/lib/Subscribers/InactiveSubscribersController.php @@ -15,6 +15,7 @@ use MailPoetVendor\Doctrine\ORM\EntityManager; class InactiveSubscribersController { const UNOPENED_EMAILS_THRESHOLD = 3; + const LIFETIME_EMAILS_THRESHOLD = 10; private $processedTaskIdsTableCreated = false; @@ -100,6 +101,7 @@ class InactiveSubscribersController { // Select subscribers who received at least a number of emails after threshold date and subscribed before that $startId = (int)$startId; $endId = $startId + $batchSize; + $lifetimeEmailsThreshold = self::LIFETIME_EMAILS_THRESHOLD; $inactiveSubscriberIdsTmpTable = 'inactive_subscriber_ids'; $connection->executeQuery(" CREATE TEMPORARY TABLE IF NOT EXISTS {$inactiveSubscriberIdsTmpTable} @@ -111,6 +113,7 @@ class InactiveSubscribersController { AND s.status = :status AND s.id >= :startId AND s.id < :endId + AND s.emails_count >= {$lifetimeEmailsThreshold} GROUP BY s.id HAVING count(s.id) >= :unopenedEmailsThreshold ", diff --git a/mailpoet/tests/integration/Subscribers/InactiveSubscribersControllerTest.php b/mailpoet/tests/integration/Subscribers/InactiveSubscribersControllerTest.php index d38ab5686c..482946c0ba 100644 --- a/mailpoet/tests/integration/Subscribers/InactiveSubscribersControllerTest.php +++ b/mailpoet/tests/integration/Subscribers/InactiveSubscribersControllerTest.php @@ -156,6 +156,19 @@ class InactiveSubscribersControllerTest extends \MailPoetTest { expect($subscriber->getStatus())->equals(SubscriberEntity::STATUS_SUBSCRIBED); } + public function testItDoesNotDeactivateSubscriberWithLessEmailsCountThanThreshold(): void { + $subscriber1 = $this->createSubscriber('s1@email.com', 10, SubscriberEntity::STATUS_SUBSCRIBED, 9); + $this->createCompletedSendingTasksForSubscriber($subscriber1, self::UNOPENED_EMAILS_THRESHOLD, 3); + + $result = $this->controller->markInactiveSubscribers(self::INACTIVITY_DAYS_THRESHOLD, self::PROCESS_BATCH_SIZE); + expect($result)->equals(0); + $this->entityManager->clear(); + + $subscriber1 = $this->subscribersRepository->findOneById($subscriber1->getId()); + assert($subscriber1 instanceof SubscriberEntity); + expect($subscriber1->getStatus())->equals(SubscriberEntity::STATUS_SUBSCRIBED); + } + public function testItDoesNotDeactivatesSubscribersWhenMP2MigrationHappenedWithinInterval(): void { $this->createSetting(MP2Migrator::MIGRATION_COMPLETE_SETTING_KEY, true, (new Carbon())->subDays(3)); @@ -257,11 +270,13 @@ class InactiveSubscribersControllerTest extends \MailPoetTest { private function createSubscriber( string $email, int $createdDaysAgo = 0, - string $status = SubscriberEntity::STATUS_SUBSCRIBED + string $status = SubscriberEntity::STATUS_SUBSCRIBED, + int $emailsCount = InactiveSubscribersController::LIFETIME_EMAILS_THRESHOLD ): SubscriberEntity { $createdAt = (new Carbon())->subDays($createdDaysAgo); $subscriber = new SubscriberEntity(); $subscriber->setEmail($email); + $subscriber->setEmailsCount($emailsCount); $subscriber->setStatus($status); $subscriber->setCreatedAt($createdAt); $this->entityManager->persist($subscriber);