-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add an admin notice when production access isn't enabled in ses
- Loading branch information
1 parent
514679b
commit 204327c
Showing
10 changed files
with
303 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Ymir WordPress plugin. | ||
* | ||
* (c) Carl Alexander <support@ymirapp.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ymir\Plugin\Subscriber; | ||
|
||
use Ymir\Plugin\Email\EmailClientInterface; | ||
use Ymir\Plugin\EventManagement\SubscriberInterface; | ||
use Ymir\Plugin\Support\Collection; | ||
|
||
/** | ||
* Subscriber that handles the email integration. | ||
*/ | ||
class EmailSubscriber implements SubscriberInterface | ||
{ | ||
/** | ||
* The email client. | ||
* | ||
* @var EmailClientInterface | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* Flag whether email sending is enabled or not. | ||
* | ||
* @var bool | ||
*/ | ||
private $isEmailSendingEnabled; | ||
|
||
/** | ||
* Flag whether the WordPress site is using a vanity domain or not. | ||
* | ||
* @var bool | ||
*/ | ||
private $usingVanityDomain; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct(EmailClientInterface $client, bool $isEmailSendingEnabled, bool $usingVanityDomain) | ||
{ | ||
$this->client = $client; | ||
$this->isEmailSendingEnabled = $isEmailSendingEnabled; | ||
$this->usingVanityDomain = $usingVanityDomain; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
'ymir_admin_notices' => 'displayAdminNotices', | ||
]; | ||
} | ||
|
||
/** | ||
* Display admin notices related to email integration. | ||
*/ | ||
public function displayAdminNotices($notices) | ||
{ | ||
if (!$this->isEmailSendingEnabled || !$notices instanceof Collection) { | ||
return $notices; | ||
} | ||
|
||
if (!$this->client->canSendEmails()) { | ||
$notices[] = [ | ||
'message' => 'Sending emails using SES is disabled because your AWS isn\'t approved to send emails. To learn how to approve your AWS account, check out <a href="https://docs.ymirapp.com/team-resources/email.html#getting-your-aws-account-approved-for-sending-email">the documentation</a>.', | ||
'type' => 'error', | ||
]; | ||
} elseif ($this->usingVanityDomain) { | ||
$notices[] = [ | ||
'message' => 'Sending emails using SES is disabled because the site is using a vanity domain. To learn how to map a domain to your environment, check out <a href="https://docs.ymirapp.com/guides/domain-mapping.html">this guide</a>.', | ||
'type' => 'warning', | ||
]; | ||
} | ||
|
||
return $notices; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Ymir WordPress plugin. | ||
* | ||
* (c) Carl Alexander <support@ymirapp.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ymir\Plugin\Tests\Integration\Aws; | ||
|
||
use Ymir\Plugin\CloudProvider\Aws\SesClient; | ||
use Ymir\Plugin\Http\CurlClient; | ||
use Ymir\Plugin\Tests\Unit\TestCase; | ||
|
||
/** | ||
* @covers \Ymir\Plugin\CloudProvider\Aws\SesClient | ||
*/ | ||
class SesClientTest extends TestCase | ||
{ | ||
public function testCanSendEmailsReturnsFalse() | ||
{ | ||
$client = new SesClient(new CurlClient('test'), getenv('AWS_TEST_ACCESS_KEY_ID') ?: $_ENV['AWS_TEST_ACCESS_KEY_ID'], 'us-west-1', getenv('AWS_TEST_SECRET_ACCESS_KEY') ?: $_ENV['AWS_TEST_SECRET_ACCESS_KEY']); | ||
|
||
$this->assertFalse($client->canSendEmails()); | ||
} | ||
|
||
public function testCanSendEmailsReturnsTrue() | ||
{ | ||
$client = new SesClient(new CurlClient('test'), getenv('AWS_TEST_ACCESS_KEY_ID') ?: $_ENV['AWS_TEST_ACCESS_KEY_ID'], 'us-east-1', getenv('AWS_TEST_SECRET_ACCESS_KEY') ?: $_ENV['AWS_TEST_SECRET_ACCESS_KEY']); | ||
|
||
$this->assertTrue($client->canSendEmails()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Ymir WordPress plugin. | ||
* | ||
* (c) Carl Alexander <support@ymirapp.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ymir\Plugin\Tests\Mock; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Ymir\Plugin\Email\EmailClientInterface; | ||
|
||
trait EmailClientMockTrait | ||
{ | ||
/** | ||
* Creates a mock of a EmailClientInterface object. | ||
*/ | ||
private function getEmailClientMock(): MockObject | ||
{ | ||
return $this->getMockBuilder(EmailClientInterface::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Ymir WordPress plugin. | ||
* | ||
* (c) Carl Alexander <support@ymirapp.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ymir\Plugin\Tests\Unit\Subscriber; | ||
|
||
use Ymir\Plugin\Subscriber\EmailSubscriber; | ||
use Ymir\Plugin\Support\Collection; | ||
use Ymir\Plugin\Tests\Mock\EmailClientMockTrait; | ||
use Ymir\Plugin\Tests\Unit\TestCase; | ||
|
||
/** | ||
* @covers \Ymir\Plugin\Subscriber\EmailSubscriber | ||
*/ | ||
class EmailSubscriberTest extends TestCase | ||
{ | ||
use EmailClientMockTrait; | ||
|
||
public function testDisplayAdminNoticeAddsNoticeIfEmailClientCannotSendEmails() | ||
{ | ||
$client = $this->getEmailClientMock(); | ||
|
||
$client->expects($this->once()) | ||
->method('canSendEmails') | ||
->willReturn(false); | ||
|
||
$notices = (new EmailSubscriber($client, true, false))->displayAdminNotices(new Collection()); | ||
|
||
$this->assertCount(1, $notices); | ||
$this->assertSame('error', $notices[0]['type']); | ||
$this->assertSame('Sending emails using SES is disabled because your AWS isn\'t approved to send emails. To learn how to approve your AWS account, check out <a href="https://docs.ymirapp.com/team-resources/email.html#getting-your-aws-account-approved-for-sending-email">the documentation</a>.', $notices[0]['message']); | ||
} | ||
|
||
public function testDisplayAdminNoticeAddsNoticeIfEmailClientCannotSendEmailsAndUsingVanityDomainIsTrue() | ||
{ | ||
$client = $this->getEmailClientMock(); | ||
|
||
$client->expects($this->once()) | ||
->method('canSendEmails') | ||
->willReturn(false); | ||
|
||
$notices = (new EmailSubscriber($client, true, true))->displayAdminNotices(new Collection()); | ||
|
||
$this->assertCount(1, $notices); | ||
$this->assertSame('error', $notices[0]['type']); | ||
$this->assertSame('Sending emails using SES is disabled because your AWS isn\'t approved to send emails. To learn how to approve your AWS account, check out <a href="https://docs.ymirapp.com/team-resources/email.html#getting-your-aws-account-approved-for-sending-email">the documentation</a>.', $notices[0]['message']); | ||
} | ||
|
||
public function testDisplayAdminNoticeAddsNoticeIfUsingVanityDomainIsTrue() | ||
{ | ||
$client = $this->getEmailClientMock(); | ||
|
||
$client->expects($this->once()) | ||
->method('canSendEmails') | ||
->willReturn(true); | ||
|
||
$notices = (new EmailSubscriber($client, true, true))->displayAdminNotices(new Collection()); | ||
|
||
$this->assertCount(1, $notices); | ||
$this->assertSame('warning', $notices[0]['type']); | ||
$this->assertSame('Sending emails using SES is disabled because the site is using a vanity domain. To learn how to map a domain to your environment, check out <a href="https://docs.ymirapp.com/guides/domain-mapping.html">this guide</a>.', $notices[0]['message']); | ||
} | ||
|
||
public function testDisplayAdminNoticeDoesNothingIfEmailSendingIsDisabled() | ||
{ | ||
$this->assertCount(0, (new EmailSubscriber($this->getEmailClientMock(), false, true))->displayAdminNotices(new Collection())); | ||
} | ||
|
||
public function testDisplayAdminNoticeDoesNothingIfWeDontPassACollectionObject() | ||
{ | ||
$this->assertNull((new EmailSubscriber($this->getEmailClientMock(), true, false))->displayAdminNotices(null)); | ||
} | ||
|
||
public function testGetSubscribedEvents() | ||
{ | ||
$callbacks = EmailSubscriber::getSubscribedEvents(); | ||
|
||
foreach ($callbacks as $callback) { | ||
$this->assertTrue(method_exists(EmailSubscriber::class, is_array($callback) ? $callback[0] : $callback)); | ||
} | ||
|
||
$subscribedEvents = [ | ||
'ymir_admin_notices' => 'displayAdminNotices', | ||
]; | ||
|
||
$this->assertSame($subscribedEvents, $callbacks); | ||
} | ||
} |