From aaf38a6f962ab45b15690095535c97e29060f2d3 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Thu, 25 Jun 2020 15:30:01 +0200 Subject: [PATCH] Delete also stats notification scheduled tasks when deleting a newsletter [MAILPOET-3015] --- lib/Newsletter/NewslettersRepository.php | 15 +++++++++++-- .../Newsletter/NewsletterRepositoryTest.php | 22 +++++++++++++++---- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/Newsletter/NewslettersRepository.php b/lib/Newsletter/NewslettersRepository.php index 3b88d3af89..21983988a9 100644 --- a/lib/Newsletter/NewslettersRepository.php +++ b/lib/Newsletter/NewslettersRepository.php @@ -253,15 +253,26 @@ class NewslettersRepository extends Repository { WHERE nl.`newsletter_id` IN (:ids) ", ['ids' => $ids], ['ids' => Connection::PARAM_INT_ARRAY]); - // Delete stats notifications + // Delete stats notifications tasks + $scheduledTasksTable = $entityManager->getClassMetadata(ScheduledTaskEntity::class)->getTableName(); $statsNotificationsTable = $entityManager->getClassMetadata(StatsNotificationEntity::class)->getTableName(); + $taskIds = $entityManager->getConnection()->executeQuery(" + SELECT task_id FROM $statsNotificationsTable sn + WHERE sn.`newsletter_id` IN (:ids) + ", ['ids' => $ids], ['ids' => Connection::PARAM_INT_ARRAY])->fetchAll(); + $taskIds = array_column($taskIds, 'task_id'); + $entityManager->getConnection()->executeUpdate(" + DELETE st FROM $scheduledTasksTable st + WHERE st.`id` IN (:ids) + ", ['ids' => $taskIds], ['ids' => Connection::PARAM_INT_ARRAY]); + + // Delete stats notifications $entityManager->getConnection()->executeUpdate(" DELETE sn FROM $statsNotificationsTable sn WHERE sn.`newsletter_id` IN (:ids) ", ['ids' => $ids], ['ids' => Connection::PARAM_INT_ARRAY]); // Delete scheduled tasks and scheduled task subscribers - $scheduledTasksTable = $entityManager->getClassMetadata(ScheduledTaskEntity::class)->getTableName(); $sendingQueueTable = $entityManager->getClassMetadata(SendingQueueEntity::class)->getTableName(); $scheduledTaskSubscribersTable = $entityManager->getClassMetadata(ScheduledTaskSubscriberEntity::class)->getTableName(); diff --git a/tests/integration/Newsletter/NewsletterRepositoryTest.php b/tests/integration/Newsletter/NewsletterRepositoryTest.php index 978d7a36ab..6e3c1d2a6a 100644 --- a/tests/integration/Newsletter/NewsletterRepositoryTest.php +++ b/tests/integration/Newsletter/NewsletterRepositoryTest.php @@ -137,8 +137,12 @@ class NewsletterRepositoryTest extends \MailPoetTest { assert($notificationHistorySegment instanceof NewsletterSegmentEntity); $notificationHistoryScheduledTaskSubscriber = $this->taskSubscribersRepository->findOneBy(['task' => $notificationHistoryScheduledTask]); assert($notificationHistoryScheduledTaskSubscriber instanceof ScheduledTaskSubscriberEntity); - $standardStatsNotification = $this->createStatNotification($standardNewsletter, $standardScheduledTaks); - $notificationHistoryStatsNotification = $this->createStatNotification($notificationHistory, $notificationHistoryScheduledTask); + $standardStatsNotification = $this->createStatNotification($standardNewsletter); + $standardStatsNotificationScheduledTask = $standardStatsNotification->getTask(); + assert($standardStatsNotificationScheduledTask instanceof ScheduledTaskEntity); + $notificationHistoryStatsNotification = $this->createStatNotification($notificationHistory); + $notificationHistoryStatsNotificationScheduledTask = $notificationHistoryStatsNotification->getTask(); + assert($notificationHistoryStatsNotificationScheduledTask instanceof ScheduledTaskEntity); $standardLink = $this->createNewsletterLink($standardNewsletter, $standardQueue); $notificationHistoryLink = $this->createNewsletterLink($notificationHistory, $notificationHistoryQueue); $optionField = $this->createNewsletterOptionField(NewsletterEntity::TYPE_NOTIFICATION, 'option'); @@ -168,7 +172,9 @@ class NewsletterRepositoryTest extends \MailPoetTest { $this->entityManager->detach($standardSegment); $this->entityManager->detach($notificationHistorySegment); $this->entityManager->detach($standardStatsNotification); + $this->entityManager->detach($standardStatsNotificationScheduledTask); $this->entityManager->detach($notificationHistoryStatsNotification); + $this->entityManager->detach($notificationHistoryStatsNotificationScheduledTask); $this->entityManager->detach($standardLink); $this->entityManager->detach($notificationHistoryLink); $this->entityManager->detach($optionValue); @@ -201,9 +207,13 @@ class NewsletterRepositoryTest extends \MailPoetTest { expect($this->entityManager->find(NewsletterSegmentEntity::class, $notificationHistorySegment->getId()))->null(); // Newsletter stats notifications - expect($this->entityManager->find(StatsNotificationEntity::class, $standardStatsNotification->getId()))->null(); + expect($this->entityManager->find(StatsNotificationEntity::class, $standardStatsNotificationScheduledTask->getId()))->null(); expect($this->entityManager->find(StatsNotificationEntity::class, $notificationHistoryStatsNotification->getId()))->null(); + // Newsletter stats notifications scheduled tasks + expect($this->entityManager->find(ScheduledTaskEntity::class, $standardStatsNotificationScheduledTask->getId()))->null(); + expect($this->entityManager->find(ScheduledTaskEntity::class, $notificationHistoryStatsNotificationScheduledTask->getId()))->null(); + // Newsletter links expect($this->entityManager->find(NewsletterLinkEntity::class, $standardLink->getId()))->null(); expect($this->entityManager->find(NewsletterLinkEntity::class, $notificationHistoryLink->getId()))->null(); @@ -270,7 +280,11 @@ class NewsletterRepositoryTest extends \MailPoetTest { return $queue; } - private function createStatNotification(NewsletterEntity $newsletter, ScheduledTaskEntity $task): StatsNotificationEntity { + private function createStatNotification(NewsletterEntity $newsletter): StatsNotificationEntity { + $task = new ScheduledTaskEntity(); + $task->setType('stats_notification'); + $task->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED); + $this->entityManager->persist($task); $statsNotification = new StatsNotificationEntity($newsletter, $task); $this->entityManager->persist($statsNotification); $this->entityManager->flush();