mirror of
https://github.com/flarum/framework.git
synced 2025-02-17 02:02:47 +08:00
Mail Extender (#2012)
This allows registering new drivers, or overwriting existing ones.
This commit is contained in:
parent
3ac5e58fa1
commit
2d86eb9b9f
38
src/Extend/Mail.php
Normal file
38
src/Extend/Mail.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Extend;
|
||||
|
||||
use Flarum\Extension\Extension;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class Mail implements ExtenderInterface
|
||||
{
|
||||
protected $drivers = [];
|
||||
|
||||
/**
|
||||
* Add a mail driver.
|
||||
*
|
||||
* @param string $identifier Identifier for mail driver. E.g. 'smtp' for SmtpDriver
|
||||
* @param string $driver ::class attribute of driver class, which must implement Flarum\Mail\DriverInterface
|
||||
*/
|
||||
public function driver(string $identifier, $driver)
|
||||
{
|
||||
$this->drivers[$identifier] = $driver;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function extend(Container $container, Extension $extension = null)
|
||||
{
|
||||
$container->extend('mail.supported_drivers', function ($existingDrivers) {
|
||||
return array_merge($existingDrivers, $this->drivers);
|
||||
});
|
||||
}
|
||||
}
|
127
tests/integration/extenders/MailTest.php
Normal file
127
tests/integration/extenders/MailTest.php
Normal file
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Tests\integration\extenders;
|
||||
|
||||
use Flarum\Extend;
|
||||
use Flarum\Mail\DriverInterface;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\Tests\integration\BuildsHttpRequests;
|
||||
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
||||
use Flarum\Tests\integration\TestCase;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Swift_NullTransport;
|
||||
use Swift_Transport;
|
||||
|
||||
class MailTest extends TestCase
|
||||
{
|
||||
use RetrievesAuthorizedUsers;
|
||||
use BuildsHttpRequests;
|
||||
|
||||
protected function prepDb()
|
||||
{
|
||||
$this->prepareDatabase([
|
||||
'users' => [
|
||||
$this->adminUser(),
|
||||
$this->normalUser(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_driver_doesnt_exist_by_default()
|
||||
{
|
||||
$this->prepDb();
|
||||
|
||||
$response = $this->send(
|
||||
$this->requestAsUser(
|
||||
$this->request('GET', '/api/mail-settings'),
|
||||
1
|
||||
)
|
||||
);
|
||||
|
||||
$drivers = json_decode($response->getBody(), true)['data']['attributes']['fields'];
|
||||
|
||||
$this->assertArrayNotHasKey('custom', $drivers);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function added_driver_appears_in_mail_settings()
|
||||
{
|
||||
$this->extend(
|
||||
(new Extend\Mail())
|
||||
->driver('custom', CustomDriver::class)
|
||||
);
|
||||
|
||||
$this->prepDb();
|
||||
|
||||
$response = $this->send(
|
||||
$this->requestAsUser(
|
||||
$this->request('GET', '/api/mail-settings'),
|
||||
1
|
||||
)
|
||||
);
|
||||
|
||||
$drivers = json_decode($response->getBody(), true)['data']['attributes']['fields'];
|
||||
|
||||
$this->assertArrayHasKey('custom', $drivers);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function adding_driver_with_duplicate_name_overrides_fields()
|
||||
{
|
||||
$this->extend(
|
||||
(new Extend\Mail())
|
||||
->driver('smtp', CustomDriver::class)
|
||||
);
|
||||
|
||||
$this->prepDb();
|
||||
|
||||
$response = $this->send(
|
||||
$this->requestAsUser(
|
||||
$this->request('GET', '/api/mail-settings'),
|
||||
1
|
||||
)
|
||||
);
|
||||
|
||||
$requiredFields = json_decode($response->getBody(), true)['data']['attributes']['fields']['smtp'];
|
||||
|
||||
$this->assertEquals($requiredFields, ['customSetting1' => '']);
|
||||
}
|
||||
}
|
||||
|
||||
class CustomDriver implements DriverInterface
|
||||
{
|
||||
public function availableSettings(): array
|
||||
{
|
||||
return ['customSetting1' => ''];
|
||||
}
|
||||
|
||||
public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag
|
||||
{
|
||||
return new MessageBag;
|
||||
}
|
||||
|
||||
public function canSend(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
||||
{
|
||||
return new Swift_NullTransport();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user