diff --git a/mailpoet/lib/Cron/Workers/SendingQueue/SendingQueue.php b/mailpoet/lib/Cron/Workers/SendingQueue/SendingQueue.php index ce6f94ea90..5680d7e9ef 100644 --- a/mailpoet/lib/Cron/Workers/SendingQueue/SendingQueue.php +++ b/mailpoet/lib/Cron/Workers/SendingQueue/SendingQueue.php @@ -70,7 +70,7 @@ class SendingQueue { /** @var ScheduledTasksRepository */ private $scheduledTasksRepository; - + /** @var SubscribersRepository */ private $subscribersRepository; @@ -145,7 +145,8 @@ class SendingQueue { ['task_id' => $queue->taskId] ); $newsletter = $this->newsletterTask->getNewsletterFromQueue($queue); - if (!$newsletter) { + $newsletterEntity = $this->newslettersRepository->findOneById($newsletter->id); + if (!$newsletter || !$newsletterEntity) { return; } // pre-process newsletter (render, replace shortcodes/links, etc.) @@ -160,11 +161,7 @@ class SendingQueue { } // clone the original object to be used for processing $_newsletter = (object)$newsletter->asArray(); - $options = $newsletter->options()->findMany(); - if (!empty($options)) { - $options = array_column($options, 'value', 'name'); - } - $_newsletter->options = $options; + $_newsletter->options = $newsletterEntity->getOptionsAsArray(); // configure mailer $this->mailerTask->configureMailer($newsletter); // get newsletter segments diff --git a/mailpoet/lib/Entities/NewsletterEntity.php b/mailpoet/lib/Entities/NewsletterEntity.php index 76133a6e14..e374ab3aa4 100644 --- a/mailpoet/lib/Entities/NewsletterEntity.php +++ b/mailpoet/lib/Entities/NewsletterEntity.php @@ -443,6 +443,21 @@ class NewsletterEntity { return $option ?: null; } + /** + * @return array Associative array of newsletter option values with option names as keys + */ + public function getOptionsAsArray(): array { + $optionsArray = []; + foreach ($this->options as $option) { + $name = $option->getName(); + if (!$name) { + continue; + } + $optionsArray[$name] = $option->getValue(); + } + return $optionsArray; + } + public function getOptionValue(string $name) { $option = $this->getOption($name); return $option ? $option->getValue() : null; diff --git a/mailpoet/lib/Entities/NewsletterOptionEntity.php b/mailpoet/lib/Entities/NewsletterOptionEntity.php index 5ecc59f087..3bc947b8b0 100644 --- a/mailpoet/lib/Entities/NewsletterOptionEntity.php +++ b/mailpoet/lib/Entities/NewsletterOptionEntity.php @@ -52,6 +52,17 @@ class NewsletterOptionEntity { return $this->value; } + /** + * @return string|null + */ + public function getName() { + $optionField = $this->getOptionField(); + if ($optionField === null) { + return null; + } + return $optionField->getName(); + } + /** * @param string|null $value */ diff --git a/mailpoet/tests/integration/Cron/Workers/SendingQueue/SendingQueueTest.php b/mailpoet/tests/integration/Cron/Workers/SendingQueue/SendingQueueTest.php index f62a2486e7..2e10be5217 100644 --- a/mailpoet/tests/integration/Cron/Workers/SendingQueue/SendingQueueTest.php +++ b/mailpoet/tests/integration/Cron/Workers/SendingQueue/SendingQueueTest.php @@ -189,8 +189,8 @@ class SendingQueueTest extends \MailPoetTest { } public function testItEnforcesExecutionLimitsBeforeQueueProcessing() { - $sendingQueueWorker = $this->make($this->getSendingQueueWorker( - $this->makeEmpty(NewslettersRepository::class)), + $sendingQueueWorker = $this->make( + $this->getSendingQueueWorker(), [ 'processQueue' => Expected::never(), 'enforceSendingAndExecutionLimits' => Expected::exactly(1, function() { @@ -202,7 +202,7 @@ class SendingQueueTest extends \MailPoetTest { $this->sendingThrottlingHandler, $this->statsNotificationsWorker, $this->loggerFactory, - $this->makeEmpty(NewslettersRepository::class), + $this->newslettersRepository, $this->cronHelper, $this->subscribersFinder, $this->segmentsRepository, @@ -307,7 +307,7 @@ class SendingQueueTest extends \MailPoetTest { public function testItEnforcesExecutionLimitsAfterQueueProcessing() { $sendingQueueWorker = $this->make( - $this->getSendingQueueWorker($this->makeEmpty(NewslettersRepository::class)), + $this->getSendingQueueWorker(), [ 'processQueue' => function() { // this function returns a queue object @@ -320,7 +320,7 @@ class SendingQueueTest extends \MailPoetTest { $this->sendingThrottlingHandler, $this->statsNotificationsWorker, $this->loggerFactory, - $this->makeEmpty(NewslettersRepository::class), + $this->newslettersRepository, $this->cronHelper, $this->subscribersFinder, $this->segmentsRepository, @@ -781,7 +781,7 @@ class SendingQueueTest extends \MailPoetTest { $this->newsletterSegment->delete(); $sendingQueueWorker = $this->getSendingQueueWorker( - $this->makeEmpty(NewslettersRepository::class), + null, $this->construct( MailerTask::class, [$this->diContainer->get(MailerFactory::class)],