Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\DB\Events\AddMissingIndicesEvent;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
use OCP\IDBConnection;
Expand Down Expand Up @@ -115,6 +116,7 @@ public function register(IRegistrationContext $context): void {
$c->get(IFactory::class),
$c->get(IManager::class),
$c->get(IValidator::class),
$c->get(IAppConfig::class),
$c->get(IConfig::class),
$c->get(LoggerInterface::class),
$c->get(Data::class),
Expand Down
6 changes: 6 additions & 0 deletions lib/MailQueueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCP\Activity\IManager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Defaults;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
use OCP\IDBConnection;
Expand Down Expand Up @@ -50,6 +51,7 @@ public function __construct(
protected IFactory $lFactory,
protected IManager $activityManager,
protected IValidator $richObjectValidator,
protected IAppConfig $appConfig,
protected IConfig $config,
protected LoggerInterface $logger,
protected Data $data,
Expand All @@ -68,6 +70,10 @@ public function __construct(
* @return int Number of users we sent an email to
*/
public function sendEmails(int $limit, int $sendTime, bool $forceSending = false, ?int $restrictEmails = null): int {
if (!$this->appConfig->getValueBool('activity', 'enable_email', true)) {
return 0;
}

// Get all users which should receive an email
$affectedUsers = $this->getAffectedUsers($limit, $sendTime, $forceSending, $restrictEmails);
if (empty($affectedUsers)) {
Expand Down
35 changes: 35 additions & 0 deletions tests/MailQueueHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCA\Activity\UserSettings;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
use OCP\IL10N;
Expand Down Expand Up @@ -77,6 +78,9 @@ class MailQueueHandlerTest extends TestCase {
/** @var IValidator|MockObject */
protected $richObjectValidator;

/** @var IAppConfig|MockObject */
protected $appConfig;

/** @var IConfig|MockObject */
protected $config;

Expand All @@ -102,6 +106,7 @@ protected function setUp(): void {
$app = self::getUniqueID('MailQueueHandlerTest', 10);
$this->userManager = $this->createMock(IUserManager::class);
$this->lFactory = $this->createMock(IFactory::class);
$this->appConfig = $this->createMock(IAppConfig::class);
$this->config = $this->createMock(IConfig::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class);
Expand Down Expand Up @@ -173,6 +178,7 @@ protected function setUp(): void {
$this->lFactory,
$this->activityManager,
$this->richObjectValidator,
$this->appConfig,
$this->config,
$this->logger,
$this->data,
Expand Down Expand Up @@ -333,6 +339,10 @@ public function testSendEmailToUser(): void {
public function testSendEmailsDeletesQueueOnMailerFailure(): void {
$maxTime = 200;

$this->appConfig->method('getValueBool')
->with('activity', 'enable_email', true)
->willReturn(true);

$template = $this->createMock(IEMailTemplate::class);
$this->mailer->method('createEMailTemplate')
->willReturn($template);
Expand Down Expand Up @@ -373,6 +383,10 @@ public function testSendEmailsDeletesQueueOnMailerFailure(): void {
public function testSendEmailsDeletesQueueOnSendReturnFalse(): void {
$maxTime = 200;

$this->appConfig->method('getValueBool')
->with('activity', 'enable_email', true)
->willReturn(true);

$template = $this->createMock(IEMailTemplate::class);
$this->mailer->method('createEMailTemplate')
->willReturn($template);
Expand Down Expand Up @@ -403,6 +417,27 @@ public function testSendEmailsDeletesQueueOnSendReturnFalse(): void {
}
}

public function testSendEmailsSkipsWhenAdminEmailDisabled(): void {
$maxTime = 200;

$this->appConfig->method('getValueBool')
->with('activity', 'enable_email', true)
->willReturn(false);

$this->mailer->expects($this->never())
->method('send');

$result = $this->mailQueueHandler->sendEmails(3, $maxTime);

$this->assertSame(0, $result);

// Queue must be untouched so emails can be sent if admin re-enables the toggle
foreach (['user1', 'user2', 'user3'] as $user) {
[$data,] = self::invokePrivate($this->mailQueueHandler, 'getItemsForUser', [$user, $maxTime]);
$this->assertNotEmpty($data, "Queue entries for $user must survive when email is globally disabled");
}
}

/**
* @param array $users
* @param int $maxTime
Expand Down
Loading