diff --git a/lib/MailNotifications.php b/lib/MailNotifications.php index 6bfc75ab4..e8d2e6034 100644 --- a/lib/MailNotifications.php +++ b/lib/MailNotifications.php @@ -92,6 +92,13 @@ public function sendEmails(int $batchSize, int $sendTime): void { $batchTime = $defaultBatchTime; } + if ($batchTime === 0) { + // Mails are off, so drop the user from the queue of the background job + $settings->setNextSendTime(0); + $this->settingsMapper->update($settings); + continue; + } + $userId = $settings->getUserId(); if (isset($userEnabled[$userId]) && $userEnabled[$userId] === false) { // User is disabled, skip sending the email for them diff --git a/tests/Unit/MailNotificationsTest.php b/tests/Unit/MailNotificationsTest.php index 5289eea11..a1ac25430 100644 --- a/tests/Unit/MailNotificationsTest.php +++ b/tests/Unit/MailNotificationsTest.php @@ -176,4 +176,34 @@ public function testSendEmailsKeepsNextSendTimeOfDisabledUserInFuture(): void { $this->assertSame($futureSendTime, $settings->getNextSendTime()); } + + public function testSendEmailsSkipsDefaultUserWhenDefaultIsOff(): void { + $settings = new Settings(); + $settings->setUserId('user1'); + $settings->setBatchTime(Settings::EMAIL_SEND_DEFAULT); + $settings->setLastSendId(0); + $settings->setNextSendTime(1); + + $this->settingsMapper->method('getUsersByNextSendTime') + ->willReturn([$settings]); + $this->userConfig->method('getValuesByUsers') + ->willReturn([]); + $this->appConfig->method('getAppValueInt') + ->with('setting_batchtime') + ->willReturn(Settings::EMAIL_SEND_OFF); + $this->systemConfig->method('getSystemValueString') + ->willReturn(''); + + $this->handler->expects($this->never()) + ->method('getAfterId'); + $this->mailer->expects($this->never()) + ->method('send'); + $this->settingsMapper->expects($this->once()) + ->method('update') + ->with($settings); + + $this->mailNotifications->sendEmails(MailNotifications::BATCH_SIZE_WEB, 2000); + + $this->assertSame(0, $settings->getNextSendTime()); + } }