Add and update tests for Send preview email

MAILPOET-6092
This commit is contained in:
Oluwaseun Olorunsola
2024-11-14 17:59:55 +01:00
committed by Rostislav Wolný
parent 2216c065b2
commit 5969e3438e
2 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
<?php
/**
* This file is part of the MailPoet plugin.
*
* @package MailPoet\EmailEditor
*/
declare(strict_types = 1);
namespace MailPoet\EmailEditor\Integrations\Utils;
use MailPoet\EmailEditor\Engine\Renderer\Renderer;
/**
* Unit test for Send_Preview_Email_Test class.
*/
class Send_Preview_Email_Test extends \MailPoetTest {
/**
* Instance of Send_Preview_Email
*
* @var Send_Preview_Email
*/
private $send_preview_email;
/**
* Set up before each test
*/
public function _before() {
parent::_before();
$renderer_mock = $this->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' => '<!-- wp:button --><div class="wp-block-button"><a class="wp-block-button__link has-background wp-element-button">Button</a></div><!-- /wp:button -->',
)
);
$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 );
}
}

View File

@@ -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 ),
);
}
);