diff --git a/framework/core/src/Extend/Mail.php b/framework/core/src/Extend/Mail.php new file mode 100644 index 000000000..37058546a --- /dev/null +++ b/framework/core/src/Extend/Mail.php @@ -0,0 +1,38 @@ +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); + }); + } +} diff --git a/framework/core/tests/integration/extenders/MailTest.php b/framework/core/tests/integration/extenders/MailTest.php new file mode 100644 index 000000000..c18cce91a --- /dev/null +++ b/framework/core/tests/integration/extenders/MailTest.php @@ -0,0 +1,127 @@ +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(); + } +}