Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support preset parameter in email action #257

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 29 additions & 1 deletion src/Actions/EmailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Kirby\Cms\App;
use Kirby\Exception\NotFoundException;
use Kirby\Toolkit\Str;
use Kirby\Toolkit\I18n;

Expand Down Expand Up @@ -31,6 +32,8 @@ class EmailAction extends Action
*/
public function perform()
{
$this->options = $this->preset($this->options['preset'] ?? null);
bezin marked this conversation as resolved.
Show resolved Hide resolved

$params = array_merge($this->options, [
'to' => $this->requireOption('to'),
'from' => $this->requireOption('from'),
Expand Down Expand Up @@ -97,7 +100,7 @@ protected function handleException($e)
*/
protected function sendEmail(array $params)
{
App::instance()->email($params);
App::instance()->email([], $params);
}

/**
Expand Down Expand Up @@ -175,4 +178,29 @@ protected function shouldReceiveCopy()
return $this->option('receive-copy') === true
&& $this->form->data(self::RECEIVE_COPY_KEY);
}

/**
* Loads more options from Kirby email presets, if `preset` was set
*
* @return array
*/
private function preset(string|null $preset): array
{
if (!$preset) {
return $this->options;
}

if (($presetOptions = App::instance()->option('email.presets.' . $preset)) === null) {
throw new NotFoundException([
'key' => 'email.preset.notFound',
'data' => ['name' => $preset],
]);
}

// Options passed to the action always superseed preset options
$options = array_merge($presetOptions, $this->options);
unset($options['preset']);

return $options;
}
}
41 changes: 41 additions & 0 deletions tests/Actions/EmailActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Uniform\Tests\TestCase;
use Uniform\Actions\EmailAction;
use Uniform\Exceptions\PerformerException;
use Kirby\Exception\NotFoundException;

class EmailActionTest extends TestCase
{
Expand Down Expand Up @@ -98,6 +99,46 @@ public function testPassthroughOptions()
$this->assertEquals($expect, $action->params['data']);
}

public function testEmailPresets()
{
App::instance()->extend([
'options' => [
'email' => [
'presets' => [
'default' => [
'from' => '[email protected]',
'fromName' => 'John Doe'
],
],
],
],
]);

$this->form->data('message', 'hello');
$action = new EmailActionStub($this->form, [
'preset' => 'default',
'to' => '[email protected]',
'fromName' => 'Janet Doe'
]);
$action->perform();

$email = $action->email;
$this->assertEquals('[email protected]', $email->from());
$this->assertEquals('Janet Doe', $email->fromName());
}

public function testEmailPresetNotDefined()
{
$action = new EmailActionStub($this->form, [
'preset' => 'default',
'to' => '[email protected]',
'fromName' => 'Janet Doe'
]);

$this->expectException(NotFoundException::class);
$action->perform();
}

public function testSubjectTemplate()
{
$this->form->data('email', "[email protected]");
Expand Down
Loading