Skip to content
Draft
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
15 changes: 15 additions & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,21 @@ public function __construct($webRoot, \OC\Config $config) {
});

$this->registerService(IMailer::class, function (Server $c) {
$config = $c->get(\OCP\IConfig::class);
$mailerClass = $config->getSystemValueString('custom_mailer_class', '');

if ($mailerClass !== '' && class_exists($mailerClass) && in_array(IMailer::class, class_implements($mailerClass), true)) {
return new $mailerClass(
$c->get(\OCP\IConfig::class),
$c->get(LoggerInterface::class),
$c->get(Defaults::class),
$c->get(IURLGenerator::class),
$c->getL10N('lib'),
$c->get(IEventDispatcher::class),
$c->get(IFactory::class)
);
}

return new Mailer(
$c->get(\OCP\IConfig::class),
$c->get(LoggerInterface::class),
Expand Down
48 changes: 48 additions & 0 deletions tests/lib/Mail/FakeMailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Test\Mail;

use OCP\Defaults;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
use OCP\Mail\IAttachment;
use OCP\Mail\IEMailTemplate;
use OCP\Mail\IMessage;
use Psr\Log\LoggerInterface;

/**
* Class FakeMailer
*/
class FakeMailer implements \OCP\Mail\IMailer {

public function __construct(
private IConfig $config,
private LoggerInterface $logger,
private Defaults $defaults,
private IURLGenerator $urlGenerator,
private IL10N $l10n,
private IEventDispatcher $dispatcher,
private IFactory $l10nFactory,
) {
}
public function createMessage(): IMessage {
}

public function createAttachment($data = null, $filename = null, $contentType = null): IAttachment {
}

public function createAttachmentFromPath(string $path, $contentType = null): IAttachment {
}

public function createEMailTemplate(string $emailId, array $data = []): IEMailTemplate {
}

public function send(IMessage $message): array {
}

public function validateMailAddress(string $email): bool {
}
}
20 changes: 20 additions & 0 deletions tests/lib/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,24 @@ public function testOverwriteDefaultCommentsManager(): void {

$config->setSystemValue('comments.managerFactory', $defaultManagerFactory);
}

public function testOverwriteDefaultMailer() {
$config = $this->server->getConfig();
$config->deleteSystemValue('custom_mailer_class');

$this->assertEquals('not_set', $config->getSystemValue('custom_mailer_class', 'not_set'), "custom_mailer_class is not set");

$serviceName = 'Mailer';
$instanceOf = '\OC\Mail\Mailer';

$this->assertInstanceOf($instanceOf, $this->server->query($serviceName), 'Service "' . $serviceName . '"" did not return the right class');
// $this->assertInstanceOf($instanceOf, $this->server->query($serviceName), 'Service "' . $serviceName . '"" did not return the right class. serviceName=' . $serviceName);

$instanceOf = 'Test\\Mail\\FakeMailer';
$config->setSystemValue('custom_mailer_class', 'Test\\Mail\\FakeMailer');

$this->assertInstanceOf($instanceOf, $this->server->query($serviceName), 'Service "' . $serviceName . '"" did not return the right class');

$config->deleteSystemValue('custom_mailer_class');
}
}
Loading