diff --git a/packages/php/email-editor/tests/integration/Integrations/Utils/Send_Preview_Email_Test.php b/packages/php/email-editor/tests/integration/Integrations/Utils/Send_Preview_Email_Test.php new file mode 100644 index 0000000000..fc763a864d --- /dev/null +++ b/packages/php/email-editor/tests/integration/Integrations/Utils/Send_Preview_Email_Test.php @@ -0,0 +1,113 @@ +createMock( Renderer::class ); + $renderer_mock->method( 'render' )->willReturn( + array( + 'html' => 'test html', + 'text' => 'test text', + ) + ); + + $this->send_preview_email = $this->getServiceWithOverrides( + Send_Preview_Email::class, + array( + 'renderer' => $renderer_mock, + ) + ); + } + + /** + * Test it sends preview email. + */ + public function testItSendsPreviewEmail(): void { + $this->send_preview_email->send_email = function () { + return true; + }; + + $email_post = $this->tester->create_post( + array( + 'post_content' => '
', + ) + ); + + $post_data = array( + 'newsletterId' => 2, + 'email' => 'hello@example.com', + 'postId' => $email_post->ID, + ); + + $result = $this->send_preview_email->send_preview_email( $post_data ); + + verify( $result )->equals( true ); + } + + /** + * Test it throws an exception with invalid email + */ + public function testItThrowsAnExceptionWithInvalidEmail(): void { + $this->expectException( \InvalidArgumentException::class ); + $this->expectExceptionMessage( 'Invalid email' ); + $post_data = array( + 'newsletterId' => 2, + 'email' => 'hello@example', + 'postId' => 4, + ); + $this->send_preview_email->send_preview_email( $post_data ); + } + + /** + * Test it throws an exception when post id is not provided + */ + public function testItThrowsAnExceptionWhenPostIdIsNotProvided(): void { + $this->expectException( \InvalidArgumentException::class ); + $this->expectExceptionMessage( 'Missing required data' ); + $post_data = array( + 'newsletterId' => 2, + 'email' => 'hello@example.com', + 'postId' => null, + ); + $this->send_preview_email->send_preview_email( $post_data ); + } + + /** + * Test it throws an exception when the post cannot be found + */ + public function testItThrowsAnExceptionWhenPostCannotBeFound(): void { + $this->expectException( \Exception::class ); + $this->expectExceptionMessage( 'Invalid post' ); + $post_data = array( + 'newsletterId' => 2, + 'email' => 'hello@example.com', + 'postId' => 100, + ); + $this->send_preview_email->send_preview_email( $post_data ); + } +} diff --git a/packages/php/email-editor/tests/integration/_bootstrap.php b/packages/php/email-editor/tests/integration/_bootstrap.php index 4a776f9557..8ee0e45315 100644 --- a/packages/php/email-editor/tests/integration/_bootstrap.php +++ b/packages/php/email-editor/tests/integration/_bootstrap.php @@ -29,6 +29,7 @@ use MailPoet\EmailEditor\Engine\Templates\Utils; use MailPoet\EmailEditor\Engine\Theme_Controller; use MailPoet\EmailEditor\Integrations\Core\Initializer; use MailPoet\EmailEditor\Integrations\MailPoet\Blocks\BlockTypesController; +use MailPoet\EmailEditor\Integrations\Utils\Send_Preview_Email; use MailPoet\EmailEditor\Utils\Cdn_Asset_Url; if ( (bool) getenv( 'MULTISITE' ) === true ) { @@ -279,6 +280,14 @@ abstract class MailPoetTest extends \Codeception\TestCase\Test { // phpcs:ignore ); } ); + $container->set( + Send_Preview_Email::class, + function ( $container ) { + return new Send_Preview_Email( + $container->get( Renderer::class ), + ); + } + ); $container->set( Email_Editor::class, function ( $container ) { @@ -288,6 +297,7 @@ abstract class MailPoetTest extends \Codeception\TestCase\Test { // phpcs:ignore $container->get( Template_Preview::class ), $container->get( Patterns::class ), $container->get( Settings_Controller::class ), + $container->get( Send_Preview_Email::class ), ); } );