Migrate integration tests in Engine directory to WordPress Coding Standard

[MAILPOET-6240]
This commit is contained in:
Jan Lysý
2024-11-04 13:25:19 +01:00
committed by Jan Lysý
parent 0c781be938
commit e69666776d
7 changed files with 357 additions and 134 deletions

View File

@@ -1,36 +1,62 @@
<?php declare(strict_types = 1); <?php
/**
* This file is part of the MailPoet plugin.
*
* @package MailPoet\EmailEditor
*/
declare(strict_types = 1);
namespace MailPoet\EmailEditor\Engine; namespace MailPoet\EmailEditor\Engine;
/**
* Integration test for Email_Editor class
*/
class Email_Editor_Test extends \MailPoetTest { class Email_Editor_Test extends \MailPoetTest {
/** @var Email_Editor */ /**
private $emailEditor; * Email editor instance
*
* @var Email_Editor
*/
private $email_editor;
/** @var callable */ /**
private $postRegisterCallback; * Callback to register custom post type
*
* @var callable
*/
private $post_register_callback;
/**
* Set up before each test
*/
public function _before() { public function _before() {
parent::_before(); parent::_before();
$this->emailEditor = $this->di_container->get( Email_Editor::class ); $this->email_editor = $this->di_container->get( Email_Editor::class );
$this->postRegisterCallback = function ( $postTypes ) { $this->post_register_callback = function ( $post_types ) {
$postTypes[] = array( $post_types[] = array(
'name' => 'custom_email_type', 'name' => 'custom_email_type',
'args' => array(), 'args' => array(),
'meta' => array(), 'meta' => array(),
); );
return $postTypes; return $post_types;
}; };
add_filter( 'mailpoet_email_editor_post_types', $this->postRegisterCallback ); add_filter( 'mailpoet_email_editor_post_types', $this->post_register_callback );
$this->emailEditor->initialize(); $this->email_editor->initialize();
} }
/**
* Test if the email register custom post type
*/
public function testItRegistersCustomPostTypeAddedViaHook() { public function testItRegistersCustomPostTypeAddedViaHook() {
$postTypes = get_post_types(); $post_types = get_post_types();
$this->assertArrayHasKey( 'custom_email_type', $postTypes ); $this->assertArrayHasKey( 'custom_email_type', $post_types );
} }
/**
* Clean up after each test
*/
public function _after() { public function _after() {
parent::_after(); parent::_after();
remove_filter( 'mailpoet_email_editor_post_types', $this->postRegisterCallback ); remove_filter( 'mailpoet_email_editor_post_types', $this->post_register_callback );
} }
} }

View File

@@ -1,33 +1,58 @@
<?php declare(strict_types = 1); <?php
/**
* This file is part of the MailPoet plugin.
*
* @package MailPoet\EmailEditor
*/
declare(strict_types = 1);
namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer; namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer;
use MailPoet\EmailEditor\Integrations\Core\Renderer\Blocks\Text; use MailPoet\EmailEditor\Integrations\Core\Renderer\Blocks\Text;
require_once __DIR__ . '/Dummy_Block_Renderer.php'; require_once __DIR__ . '/Dummy_Block_Renderer.php';
/**
* Integration test for Blocks_Registry
*/
class Blocks_Registry_Test extends \MailPoetTest { class Blocks_Registry_Test extends \MailPoetTest {
/** @var Blocks_Registry */ /**
* Instance of Blocks_Registry
*
* @var Blocks_Registry
*/
private $registry; private $registry;
/**
* Set up before each test.
*/
public function _before() { public function _before() {
parent::_before(); parent::_before();
$this->registry = $this->di_container->get( Blocks_Registry::class ); $this->registry = $this->di_container->get( Blocks_Registry::class );
} }
/**
* Test it returns null for unknown renderer.
*/
public function testItReturnsNullForUnknownRenderer() { public function testItReturnsNullForUnknownRenderer() {
$storedRenderer = $this->registry->get_block_renderer( 'test' ); $stored_renderer = $this->registry->get_block_renderer( 'test' );
verify( $storedRenderer )->null(); verify( $stored_renderer )->null();
} }
/**
* Test it stores added renderer.
*/
public function testItStoresAddedRenderer() { public function testItStoresAddedRenderer() {
$renderer = new Text(); $renderer = new Text();
$this->registry->add_block_renderer( 'test', $renderer ); $this->registry->add_block_renderer( 'test', $renderer );
$storedRenderer = $this->registry->get_block_renderer( 'test' ); $stored_renderer = $this->registry->get_block_renderer( 'test' );
verify( $storedRenderer )->equals( $renderer ); verify( $stored_renderer )->equals( $renderer );
} }
/**
* Test it reports which renderers are registered.
*/
public function testItReportsWhichRenderersAreRegistered() { public function testItReportsWhichRenderersAreRegistered() {
$renderer = new Text(); $renderer = new Text();
$this->registry->add_block_renderer( 'test', $renderer ); $this->registry->add_block_renderer( 'test', $renderer );

View File

@@ -1,5 +1,11 @@
<?php declare(strict_types = 1); <?php
/**
* This file is part of the MailPoet plugin.
*
* @package MailPoet\EmailEditor
*/
declare(strict_types = 1);
namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer; namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer;
use MailPoet\EmailEditor\Engine\Email_Editor; use MailPoet\EmailEditor\Engine\Email_Editor;
@@ -7,44 +13,71 @@ use MailPoet\EmailEditor\Integrations\MailPoet\Blocks\BlockTypesController;
require_once __DIR__ . '/Dummy_Block_Renderer.php'; require_once __DIR__ . '/Dummy_Block_Renderer.php';
/**
* Integration test for Content_Renderer
*/
class Content_Renderer_Test extends \MailPoetTest { class Content_Renderer_Test extends \MailPoetTest {
/**
* Instance of the renderer.
*
* @var Content_Renderer
*/
private Content_Renderer $renderer; private Content_Renderer $renderer;
/**
* Instance of the email post.
*
* @var \WP_Post
*/
private \WP_Post $email_post;
private \WP_Post $emailPost; /**
* Set up before each test.
*/
public function _before(): void { public function _before(): void {
parent::_before(); parent::_before();
$this->di_container->get( Email_Editor::class )->initialize(); $this->di_container->get( Email_Editor::class )->initialize();
$this->di_container->get( BlockTypesController::class )->initialize(); $this->di_container->get( BlockTypesController::class )->initialize();
$this->renderer = $this->di_container->get( Content_Renderer::class ); $this->renderer = $this->di_container->get( Content_Renderer::class );
$this->emailPost = $this->tester->create_post( $this->email_post = $this->tester->create_post(
array( array(
'post_content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->', 'post_content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->',
) )
); );
} }
/**
* Test it renders the content.
*/
public function testItRendersContent(): void { public function testItRendersContent(): void {
$template = new \WP_Block_Template(); $template = new \WP_Block_Template();
$template->id = 'template-id'; $template->id = 'template-id';
$template->content = '<!-- wp:core/post-content /-->'; $template->content = '<!-- wp:core/post-content /-->';
$content = $this->renderer->render( $content = $this->renderer->render(
$this->emailPost, $this->email_post,
$template $template
); );
verify( $content )->stringContainsString( 'Hello!' ); verify( $content )->stringContainsString( 'Hello!' );
} }
/**
* Test it inlines content styles.
*/
public function testItInlinesContentStyles(): void { public function testItInlinesContentStyles(): void {
$template = new \WP_Block_Template(); $template = new \WP_Block_Template();
$template->id = 'template-id'; $template->id = 'template-id';
$template->content = '<!-- wp:core/post-content /-->'; $template->content = '<!-- wp:core/post-content /-->';
$rendered = $this->renderer->render( $this->emailPost, $template ); $rendered = $this->renderer->render( $this->email_post, $template );
$paragraphStyles = $this->getStylesValueForTag( $rendered, 'p' ); $paragraph_styles = $this->getStylesValueForTag( $rendered, 'p' );
verify( $paragraphStyles )->stringContainsString( 'margin: 0' ); verify( $paragraph_styles )->stringContainsString( 'margin: 0' );
verify( $paragraphStyles )->stringContainsString( 'display: block' ); verify( $paragraph_styles )->stringContainsString( 'display: block' );
} }
/**
* Get the value of the style attribute for a given tag in the HTML.
*
* @param string $html HTML content.
* @param string $tag Tag name.
*/
private function getStylesValueForTag( $html, $tag ): ?string { private function getStylesValueForTag( $html, $tag ): ?string {
$html = new \WP_HTML_Tag_Processor( $html ); $html = new \WP_HTML_Tag_Processor( $html );
if ( $html->next_tag( $tag ) ) { if ( $html->next_tag( $tag ) ) {

View File

@@ -1,10 +1,27 @@
<?php declare(strict_types = 1); <?php
/**
* This file is part of the MailPoet plugin.
*
* @package MailPoet\EmailEditor
*/
declare(strict_types = 1);
namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer; namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer;
use MailPoet\EmailEditor\Engine\Settings_Controller; use MailPoet\EmailEditor\Engine\Settings_Controller;
/**
* Dummy block renderer for testing purposes.
*/
class Dummy_Block_Renderer implements Block_Renderer { class Dummy_Block_Renderer implements Block_Renderer {
/**
* Renders the block.
*
* @param string $block_content The block content.
* @param array $parsed_block The parsed block.
* @param Settings_Controller $settings_controller The settings controller.
* @return string
*/
public function render( string $block_content, array $parsed_block, Settings_Controller $settings_controller ): string { public function render( string $block_content, array $parsed_block, Settings_Controller $settings_controller ): string {
return $parsed_block['innerHtml']; return $parsed_block['innerHtml'];
} }

View File

@@ -1,5 +1,11 @@
<?php declare(strict_types = 1); <?php
/**
* This file is part of the MailPoet plugin.
*
* @package MailPoet\EmailEditor
*/
declare(strict_types = 1);
namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Layout; namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Layout;
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Dummy_Block_Renderer; use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Dummy_Block_Renderer;
@@ -7,14 +13,28 @@ use MailPoet\EmailEditor\Engine\Settings_Controller;
require_once __DIR__ . '/../Dummy_Block_Renderer.php'; require_once __DIR__ . '/../Dummy_Block_Renderer.php';
/**
* Integration test for Flex_Layout_Renderer
*/
class Flex_Layout_Renderer_Test extends \MailPoetTest { class Flex_Layout_Renderer_Test extends \MailPoetTest {
/** @var Flex_Layout_Renderer */ /**
* Instance of the renderer.
*
* @var Flex_Layout_Renderer
*/
private $renderer; private $renderer;
/** @var Settings_Controller */ /**
private $settingsController; * Instance of the settings controller.
*
* @var Settings_Controller
*/
private $settings_controller;
/**
* Set up before each test.
*/
public function _before(): void { public function _before(): void {
parent::_before(); parent::_before();
$this->settings_controller = $this->di_container->get( Settings_Controller::class ); $this->settings_controller = $this->di_container->get( Settings_Controller::class );
@@ -23,8 +43,11 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
add_filter( 'render_block', array( $this, 'renderDummyBlock' ), 10, 2 ); add_filter( 'render_block', array( $this, 'renderDummyBlock' ), 10, 2 );
} }
/**
* Test it renders inner blocks.
*/
public function testItRendersInnerBlocks(): void { public function testItRendersInnerBlocks(): void {
$parsedBlock = array( $parsed_block = array(
'innerBlocks' => array( 'innerBlocks' => array(
array( array(
'blockName' => 'dummy/block', 'blockName' => 'dummy/block',
@@ -37,13 +60,16 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
), ),
'email_attrs' => array(), 'email_attrs' => array(),
); );
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController ); $output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
verify( $output )->stringContainsString( 'Dummy 1' ); verify( $output )->stringContainsString( 'Dummy 1' );
verify( $output )->stringContainsString( 'Dummy 2' ); verify( $output )->stringContainsString( 'Dummy 2' );
} }
/**
* Test it handles justifying the content.
*/
public function testItHandlesJustification(): void { public function testItHandlesJustification(): void {
$parsedBlock = array( $parsed_block = array(
'innerBlocks' => array( 'innerBlocks' => array(
array( array(
'blockName' => 'dummy/block', 'blockName' => 'dummy/block',
@@ -52,24 +78,27 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
), ),
'email_attrs' => array(), 'email_attrs' => array(),
); );
// Default justification is left // Default justification is left.
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController ); $output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
verify( $output )->stringContainsString( 'text-align: left' ); verify( $output )->stringContainsString( 'text-align: left' );
verify( $output )->stringContainsString( 'align="left"' ); verify( $output )->stringContainsString( 'align="left"' );
// Right justification // Right justification.
$parsedBlock['attrs']['layout']['justifyContent'] = 'right'; $parsed_block['attrs']['layout']['justifyContent'] = 'right';
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController ); $output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
verify( $output )->stringContainsString( 'text-align: right' ); verify( $output )->stringContainsString( 'text-align: right' );
verify( $output )->stringContainsString( 'align="right"' ); verify( $output )->stringContainsString( 'align="right"' );
// Center justification // Center justification.
$parsedBlock['attrs']['layout']['justifyContent'] = 'center'; $parsed_block['attrs']['layout']['justifyContent'] = 'center';
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController ); $output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
verify( $output )->stringContainsString( 'text-align: center' ); verify( $output )->stringContainsString( 'text-align: center' );
verify( $output )->stringContainsString( 'align="center"' ); verify( $output )->stringContainsString( 'align="center"' );
} }
/**
* Test it escapes attributes.
*/
public function testItEscapesAttributes(): void { public function testItEscapesAttributes(): void {
$parsedBlock = array( $parsed_block = array(
'innerBlocks' => array( 'innerBlocks' => array(
array( array(
'blockName' => 'dummy/block', 'blockName' => 'dummy/block',
@@ -78,13 +107,16 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
), ),
'email_attrs' => array(), 'email_attrs' => array(),
); );
$parsedBlock['attrs']['layout']['justifyContent'] = '"> <script>alert("XSS")</script><div style="text-align: right'; $parsed_block['attrs']['layout']['justifyContent'] = '"> <script>alert("XSS")</script><div style="text-align: right';
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController ); $output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
verify( $output )->stringNotContainsString( '<script>alert("XSS")</script>' ); verify( $output )->stringNotContainsString( '<script>alert("XSS")</script>' );
} }
/**
* Test that the renderer computes proper widths for reasonable settings.
*/
public function testInComputesProperWidthsForReasonableSettings(): void { public function testInComputesProperWidthsForReasonableSettings(): void {
$parsedBlock = array( $parsed_block = array(
'innerBlocks' => array(), 'innerBlocks' => array(),
'email_attrs' => array( 'email_attrs' => array(
'width' => '640px', 'width' => '640px',
@@ -92,7 +124,7 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
); );
// 50% and 25% // 50% and 25%
$parsedBlock['innerBlocks'] = array( $parsed_block['innerBlocks'] = array(
array( array(
'blockName' => 'dummy/block', 'blockName' => 'dummy/block',
'innerHtml' => 'Dummy 1', 'innerHtml' => 'Dummy 1',
@@ -104,13 +136,13 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
'attrs' => array( 'width' => '25' ), 'attrs' => array( 'width' => '25' ),
), ),
); );
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController ); $output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
$flexItems = $this->getFlexItemsFromOutput( $output ); $flex_items = $this->getFlexItemsFromOutput( $output );
verify( $flexItems[0] )->stringContainsString( 'width:312px;' ); verify( $flex_items[0] )->stringContainsString( 'width:312px;' );
verify( $flexItems[1] )->stringContainsString( 'width:148px;' ); verify( $flex_items[1] )->stringContainsString( 'width:148px;' );
// 25% and 25% and auto // 25% and 25% and auto
$parsedBlock['innerBlocks'] = array( $parsed_block['innerBlocks'] = array(
array( array(
'blockName' => 'dummy/block', 'blockName' => 'dummy/block',
'innerHtml' => 'Dummy 1', 'innerHtml' => 'Dummy 1',
@@ -127,14 +159,14 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
'attrs' => array(), 'attrs' => array(),
), ),
); );
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController ); $output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
$flexItems = $this->getFlexItemsFromOutput( $output ); $flex_items = $this->getFlexItemsFromOutput( $output );
verify( $flexItems[0] )->stringContainsString( 'width:148px;' ); verify( $flex_items[0] )->stringContainsString( 'width:148px;' );
verify( $flexItems[1] )->stringContainsString( 'width:148px;' ); verify( $flex_items[1] )->stringContainsString( 'width:148px;' );
verify( $flexItems[2] )->stringNotContainsString( 'width:' ); verify( $flex_items[2] )->stringNotContainsString( 'width:' );
// 50% and 50% // 50% and 50%
$parsedBlock['innerBlocks'] = array( $parsed_block['innerBlocks'] = array(
array( array(
'blockName' => 'dummy/block', 'blockName' => 'dummy/block',
'innerHtml' => 'Dummy 1', 'innerHtml' => 'Dummy 1',
@@ -146,14 +178,17 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
'attrs' => array( 'width' => '50' ), 'attrs' => array( 'width' => '50' ),
), ),
); );
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController ); $output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
$flexItems = $this->getFlexItemsFromOutput( $output ); $flex_items = $this->getFlexItemsFromOutput( $output );
verify( $flexItems[0] )->stringContainsString( 'width:312px;' ); verify( $flex_items[0] )->stringContainsString( 'width:312px;' );
verify( $flexItems[1] )->stringContainsString( 'width:312px;' ); verify( $flex_items[1] )->stringContainsString( 'width:312px;' );
} }
/**
* Test that the renderer computes proper widths for strange settings values.
*/
public function testInComputesWidthsForStrangeSettingsValues(): void { public function testInComputesWidthsForStrangeSettingsValues(): void {
$parsedBlock = array( $parsed_block = array(
'innerBlocks' => array(), 'innerBlocks' => array(),
'email_attrs' => array( 'email_attrs' => array(
'width' => '640px', 'width' => '640px',
@@ -161,7 +196,7 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
); );
// 100% and 25% // 100% and 25%
$parsedBlock['innerBlocks'] = array( $parsed_block['innerBlocks'] = array(
array( array(
'blockName' => 'dummy/block', 'blockName' => 'dummy/block',
'innerHtml' => 'Dummy 1', 'innerHtml' => 'Dummy 1',
@@ -173,13 +208,13 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
'attrs' => array( 'width' => '25' ), 'attrs' => array( 'width' => '25' ),
), ),
); );
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController ); $output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
$flexItems = $this->getFlexItemsFromOutput( $output ); $flex_items = $this->getFlexItemsFromOutput( $output );
verify( $flexItems[0] )->stringContainsString( 'width:508px;' ); verify( $flex_items[0] )->stringContainsString( 'width:508px;' );
verify( $flexItems[1] )->stringContainsString( 'width:105px;' ); verify( $flex_items[1] )->stringContainsString( 'width:105px;' );
// 100% and 100% // 100% and 100%
$parsedBlock['innerBlocks'] = array( $parsed_block['innerBlocks'] = array(
array( array(
'blockName' => 'dummy/block', 'blockName' => 'dummy/block',
'innerHtml' => 'Dummy 1', 'innerHtml' => 'Dummy 1',
@@ -191,13 +226,13 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
'attrs' => array( 'width' => '100' ), 'attrs' => array( 'width' => '100' ),
), ),
); );
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController ); $output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
$flexItems = $this->getFlexItemsFromOutput( $output ); $flex_items = $this->getFlexItemsFromOutput( $output );
verify( $flexItems[0] )->stringContainsString( 'width:312px;' ); verify( $flex_items[0] )->stringContainsString( 'width:312px;' );
verify( $flexItems[1] )->stringContainsString( 'width:312px;' ); verify( $flex_items[1] )->stringContainsString( 'width:312px;' );
// 100% and auto // 100% and auto
$parsedBlock['innerBlocks'] = array( $parsed_block['innerBlocks'] = array(
array( array(
'blockName' => 'dummy/block', 'blockName' => 'dummy/block',
'innerHtml' => 'Dummy 1', 'innerHtml' => 'Dummy 1',
@@ -209,23 +244,38 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
'attrs' => array(), 'attrs' => array(),
), ),
); );
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController ); $output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
$flexItems = $this->getFlexItemsFromOutput( $output ); $flex_items = $this->getFlexItemsFromOutput( $output );
verify( $flexItems[0] )->stringContainsString( 'width:508px;' ); verify( $flex_items[0] )->stringContainsString( 'width:508px;' );
verify( $flexItems[1] )->stringNotContainsString( 'width:' ); verify( $flex_items[1] )->stringNotContainsString( 'width:' );
} }
/**
* Get flex items from the output.
*
* @param string $output Output.
*/
private function getFlexItemsFromOutput( string $output ): array { private function getFlexItemsFromOutput( string $output ): array {
$matches = array(); $matches = array();
preg_match_all( '/<td class="layout-flex-item" style="(.*)">/', $output, $matches ); preg_match_all( '/<td class="layout-flex-item" style="(.*)">/', $output, $matches );
return explode( '><', $matches[0][0] ?? array() ); return explode( '><', $matches[0][0] ?? array() );
} }
public function renderDummyBlock( $blockContent, $parsedBlock ): string { /**
$dummyRenderer = new Dummy_Block_Renderer(); * Render a dummy block.
return $dummyRenderer->render( $blockContent, $parsedBlock, $this->settingsController ); *
* @param string $block_content Block content.
* @param array $parsed_block Parsed block data.
* @return string
*/
public function renderDummyBlock( $block_content, $parsed_block ): string {
$dummy_renderer = new Dummy_Block_Renderer();
return $dummy_renderer->render( $block_content, $parsed_block, $this->settings_controller );
} }
/**
* Clean up after each test.
*/
public function _after(): void { public function _after(): void {
parent::_after(); parent::_after();
unregister_block_type( 'dummy/block' ); unregister_block_type( 'dummy/block' );

View File

@@ -1,16 +1,37 @@
<?php declare(strict_types = 1); <?php
/**
* This file is part of the MailPoet plugin.
*
* @package MailPoet\EmailEditor
*/
declare(strict_types = 1);
namespace MailPoet\EmailEditor\Engine\Renderer; namespace MailPoet\EmailEditor\Engine\Renderer;
use MailPoet\EmailEditor\Engine\Email_Editor; use MailPoet\EmailEditor\Engine\Email_Editor;
use MailPoet\EmailEditor\Engine\Settings_Controller; use MailPoet\EmailEditor\Engine\Settings_Controller;
use MailPoet\EmailEditor\Engine\Theme_Controller; use MailPoet\EmailEditor\Engine\Theme_Controller;
/**
* Integration test for Renderer
*/
class Renderer_Test extends \MailPoetTest { class Renderer_Test extends \MailPoetTest {
/**
* The renderer.
*
* @var Renderer
*/
private Renderer $renderer; private Renderer $renderer;
/**
* The email post.
*
* @var \WP_Post
*/
private \WP_Post $email_post;
private \WP_Post $emailPost; /**
* Set up before each test.
*/
public function _before(): void { public function _before(): void {
parent::_before(); parent::_before();
$this->di_container->get( Email_Editor::class )->initialize(); $this->di_container->get( Email_Editor::class )->initialize();
@@ -31,36 +52,39 @@ class Renderer_Test extends \MailPoetTest {
'background' => '#123456', 'background' => '#123456',
), ),
); );
$themeJsonMock = $this->createMock( \WP_Theme_JSON::class ); $theme_json_mock = $this->createMock( \WP_Theme_JSON::class );
$themeJsonMock->method( 'get_data' )->willReturn( $theme_json_mock->method( 'get_data' )->willReturn(
array( array(
'styles' => $styles, 'styles' => $styles,
) )
); );
$settingsControllerMock = $this->createMock( Settings_Controller::class ); $settings_controller_mock = $this->createMock( Settings_Controller::class );
$settingsControllerMock->method( 'get_email_styles' )->willReturn( $styles ); $settings_controller_mock->method( 'get_email_styles' )->willReturn( $styles );
$themeControllerMock = $this->createMock( Theme_Controller::class ); $theme_controller_mock = $this->createMock( Theme_Controller::class );
$themeControllerMock->method( 'get_theme' )->willReturn( $themeJsonMock ); $theme_controller_mock->method( 'get_theme' )->willReturn( $theme_json_mock );
$themeControllerMock->method( 'get_styles' )->willReturn( $styles ); $theme_controller_mock->method( 'get_styles' )->willReturn( $styles );
$themeControllerMock->method( 'get_layout_settings' )->willReturn( array( 'contentSize' => '660px' ) ); $theme_controller_mock->method( 'get_layout_settings' )->willReturn( array( 'contentSize' => '660px' ) );
$this->renderer = $this->getServiceWithOverrides( $this->renderer = $this->getServiceWithOverrides(
Renderer::class, Renderer::class,
array( array(
'settingsController' => $settingsControllerMock, 'settingsController' => $settings_controller_mock,
'themeController' => $themeControllerMock, 'themeController' => $theme_controller_mock,
) )
); );
$this->emailPost = $this->tester->create_post( $this->email_post = $this->tester->create_post(
array( array(
'post_content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->', 'post_content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->',
) )
); );
} }
/**
* Test it renders template with content.
*/
public function testItRendersTemplateWithContent(): void { public function testItRendersTemplateWithContent(): void {
$rendered = $this->renderer->render( $rendered = $this->renderer->render(
$this->emailPost, $this->email_post,
'Subject', 'Subject',
'Preheader content', 'Preheader content',
'en', 'en',
@@ -75,31 +99,40 @@ class Renderer_Test extends \MailPoetTest {
verify( $rendered['text'] )->stringContainsString( 'Hello!' ); verify( $rendered['text'] )->stringContainsString( 'Hello!' );
} }
/**
* Test it inlines styles.
*/
public function testItInlinesStyles(): void { public function testItInlinesStyles(): void {
$stylesCallback = function ( $styles ) { $styles_callback = function ( $styles ) {
return $styles . 'body { color: pink; }'; return $styles . 'body { color: pink; }';
}; };
add_filter( 'mailpoet_email_renderer_styles', $stylesCallback ); add_filter( 'mailpoet_email_renderer_styles', $styles_callback );
$rendered = $this->renderer->render( $this->emailPost, 'Subject', '', 'en' ); $rendered = $this->renderer->render( $this->email_post, 'Subject', '', 'en' );
$style = $this->getStylesValueForTag( $rendered['html'], array( 'tag_name' => 'body' ) ); $style = $this->getStylesValueForTag( $rendered['html'], array( 'tag_name' => 'body' ) );
verify( $style )->stringContainsString( 'color: pink' ); verify( $style )->stringContainsString( 'color: pink' );
remove_filter( 'mailpoet_email_renderer_styles', $stylesCallback ); remove_filter( 'mailpoet_email_renderer_styles', $styles_callback );
} }
/**
* Test it inlines body styles.
*/
public function testItInlinesBodyStyles(): void { public function testItInlinesBodyStyles(): void {
$rendered = $this->renderer->render( $this->emailPost, 'Subject', '', 'en' ); $rendered = $this->renderer->render( $this->email_post, 'Subject', '', 'en' );
$style = $this->getStylesValueForTag( $rendered['html'], array( 'tag_name' => 'body' ) ); $style = $this->getStylesValueForTag( $rendered['html'], array( 'tag_name' => 'body' ) );
verify( $style )->stringContainsString( 'margin: 0; padding: 0;' ); verify( $style )->stringContainsString( 'margin: 0; padding: 0;' );
} }
/**
* Test it inlines wrapper styles.
*/
public function testItInlinesWrappersStyles(): void { public function testItInlinesWrappersStyles(): void {
$rendered = $this->renderer->render( $this->emailPost, 'Subject', '', 'en' ); $rendered = $this->renderer->render( $this->email_post, 'Subject', '', 'en' );
// Verify body element styles // Verify body element styles.
$style = $this->getStylesValueForTag( $rendered['html'], array( 'tag_name' => 'body' ) ); $style = $this->getStylesValueForTag( $rendered['html'], array( 'tag_name' => 'body' ) );
verify( $style )->stringContainsString( 'background-color: #123456' ); verify( $style )->stringContainsString( 'background-color: #123456' );
// Verify layout element styles // Verify layout element styles.
$doc = new \DOMDocument(); $doc = new \DOMDocument();
$doc->loadHTML( $rendered['html'] ); $doc->loadHTML( $rendered['html'] );
$xpath = new \DOMXPath( $doc ); $xpath = new \DOMXPath( $doc );
@@ -119,6 +152,13 @@ class Renderer_Test extends \MailPoetTest {
verify( $style )->stringContainsString( 'max-width: 660px;' ); verify( $style )->stringContainsString( 'max-width: 660px;' );
} }
/**
* Returns the value of the style attribute for the first tag that matches the query.
*
* @param string $html HTML content.
* @param array $query Query to find the tag.
* @return string|null
*/
private function getStylesValueForTag( string $html, array $query ): ?string { private function getStylesValueForTag( string $html, array $query ): ?string {
$html = new \WP_HTML_Tag_Processor( $html ); $html = new \WP_HTML_Tag_Processor( $html );
if ( $html->next_tag( $query ) ) { if ( $html->next_tag( $query ) ) {

View File

@@ -1,18 +1,38 @@
<?php declare(strict_types = 1); <?php
/**
* This file is part of the MailPoet plugin.
*
* @package MailPoet\EmailEditor
*/
declare(strict_types = 1);
namespace MailPoet\EmailEditor\Engine; namespace MailPoet\EmailEditor\Engine;
/**
* Integration test for Theme_Controller class
*/
class Theme_Controller_Test extends \MailPoetTest { class Theme_Controller_Test extends \MailPoetTest {
private Theme_Controller $themeController; /**
* Theme controller instance
*
* @var Theme_Controller
*/
private Theme_Controller $theme_controller;
/**
* Set up before each test
*/
public function _before() { public function _before() {
parent::_before(); parent::_before();
$this->themeController = $this->di_container->get( Theme_Controller::class ); $this->theme_controller = $this->di_container->get( Theme_Controller::class );
} }
/**
* Test it generates css styles for renderer
*/
public function testItGeneratesCssStylesForRenderer() { public function testItGeneratesCssStylesForRenderer() {
$css = $this->themeController->get_stylesheet_for_rendering(); $css = $this->theme_controller->get_stylesheet_for_rendering();
// Font families // Font families.
verify( $css )->stringContainsString( '.has-arial-font-family' ); verify( $css )->stringContainsString( '.has-arial-font-family' );
verify( $css )->stringContainsString( '.has-comic-sans-ms-font-family' ); verify( $css )->stringContainsString( '.has-comic-sans-ms-font-family' );
verify( $css )->stringContainsString( '.has-courier-new-font-family' ); verify( $css )->stringContainsString( '.has-courier-new-font-family' );
@@ -41,13 +61,13 @@ class Theme_Controller_Test extends \MailPoetTest {
verify( $css )->stringContainsString( '.has-large-font-size' ); verify( $css )->stringContainsString( '.has-large-font-size' );
verify( $css )->stringContainsString( '.has-x-large-font-size' ); verify( $css )->stringContainsString( '.has-x-large-font-size' );
// Font sizes // Font sizes.
verify( $css )->stringContainsString( '.has-small-font-size' ); verify( $css )->stringContainsString( '.has-small-font-size' );
verify( $css )->stringContainsString( '.has-medium-font-size' ); verify( $css )->stringContainsString( '.has-medium-font-size' );
verify( $css )->stringContainsString( '.has-large-font-size' ); verify( $css )->stringContainsString( '.has-large-font-size' );
verify( $css )->stringContainsString( '.has-x-large-font-size' ); verify( $css )->stringContainsString( '.has-x-large-font-size' );
// Colors // Colors.
verify( $css )->stringContainsString( '.has-black-color' ); verify( $css )->stringContainsString( '.has-black-color' );
verify( $css )->stringContainsString( '.has-black-background-color' ); verify( $css )->stringContainsString( '.has-black-background-color' );
verify( $css )->stringContainsString( '.has-black-border-color' ); verify( $css )->stringContainsString( '.has-black-border-color' );
@@ -64,37 +84,49 @@ class Theme_Controller_Test extends \MailPoetTest {
} }
} }
/**
* Test if the theme controller translates font size slug to font size value
*/
public function testItCanTranslateFontSizeSlug() { public function testItCanTranslateFontSizeSlug() {
verify( $this->themeController->translate_slug_to_font_size( 'small' ) )->equals( '13px' ); verify( $this->theme_controller->translate_slug_to_font_size( 'small' ) )->equals( '13px' );
verify( $this->themeController->translate_slug_to_font_size( 'medium' ) )->equals( '16px' ); verify( $this->theme_controller->translate_slug_to_font_size( 'medium' ) )->equals( '16px' );
verify( $this->themeController->translate_slug_to_font_size( 'large' ) )->equals( '28px' ); verify( $this->theme_controller->translate_slug_to_font_size( 'large' ) )->equals( '28px' );
verify( $this->themeController->translate_slug_to_font_size( 'x-large' ) )->equals( '42px' ); verify( $this->theme_controller->translate_slug_to_font_size( 'x-large' ) )->equals( '42px' );
verify( $this->themeController->translate_slug_to_font_size( 'unknown' ) )->equals( 'unknown' ); verify( $this->theme_controller->translate_slug_to_font_size( 'unknown' ) )->equals( 'unknown' );
} }
/**
* Test if the theme controller translates font family slug to font family name
*/
public function testItCanTranslateColorSlug() { public function testItCanTranslateColorSlug() {
verify( $this->themeController->translate_slug_to_color( 'black' ) )->equals( '#000000' ); verify( $this->theme_controller->translate_slug_to_color( 'black' ) )->equals( '#000000' );
verify( $this->themeController->translate_slug_to_color( 'white' ) )->equals( '#ffffff' ); verify( $this->theme_controller->translate_slug_to_color( 'white' ) )->equals( '#ffffff' );
verify( $this->themeController->translate_slug_to_color( 'cyan-bluish-gray' ) )->equals( '#abb8c3' ); verify( $this->theme_controller->translate_slug_to_color( 'cyan-bluish-gray' ) )->equals( '#abb8c3' );
verify( $this->themeController->translate_slug_to_color( 'pale-pink' ) )->equals( '#f78da7' ); verify( $this->theme_controller->translate_slug_to_color( 'pale-pink' ) )->equals( '#f78da7' );
$this->checkCorrectThemeConfiguration(); $this->checkCorrectThemeConfiguration();
if ( wp_get_theme()->get( 'Name' ) === 'Twenty Twenty-One' ) { if ( wp_get_theme()->get( 'Name' ) === 'Twenty Twenty-One' ) {
verify( $this->themeController->translate_slug_to_color( 'yellow' ) )->equals( '#eeeadd' ); verify( $this->theme_controller->translate_slug_to_color( 'yellow' ) )->equals( '#eeeadd' );
} }
} }
/**
* Test if the theme controller returns correct color palette
*/
public function testItLoadsColorPaletteFromSiteTheme() { public function testItLoadsColorPaletteFromSiteTheme() {
$this->checkCorrectThemeConfiguration(); $this->checkCorrectThemeConfiguration();
$settings = $this->themeController->get_settings(); $settings = $this->theme_controller->get_settings();
if ( wp_get_theme()->get( 'Name' ) === 'Twenty Twenty-One' ) { if ( wp_get_theme()->get( 'Name' ) === 'Twenty Twenty-One' ) {
verify( $settings['color']['palette']['theme'] )->notEmpty(); verify( $settings['color']['palette']['theme'] )->notEmpty();
} }
} }
/**
* Test if the theme controller returns correct preset variables map
*/
public function testItReturnsCorrectPresetVariablesMap() { public function testItReturnsCorrectPresetVariablesMap() {
$variableMap = $this->themeController->get_variables_values_map(); $variable_map = $this->theme_controller->get_variables_values_map();
verify( $variableMap['--wp--preset--color--black'] )->equals( '#000000' ); verify( $variable_map['--wp--preset--color--black'] )->equals( '#000000' );
verify( $variableMap['--wp--preset--spacing--20'] )->equals( '20px' ); verify( $variable_map['--wp--preset--spacing--20'] )->equals( '20px' );
} }
/** /**
@@ -103,8 +135,8 @@ class Theme_Controller_Test extends \MailPoetTest {
* to prevent silent failures in case we change theme configuration in the test environment. * to prevent silent failures in case we change theme configuration in the test environment.
*/ */
private function checkCorrectThemeConfiguration() { private function checkCorrectThemeConfiguration() {
$expectedThemes = array( 'Twenty Twenty-One' ); $expected_themes = array( 'Twenty Twenty-One' );
if ( ! in_array( wp_get_theme()->get( 'Name' ), $expectedThemes ) ) { if ( ! in_array( wp_get_theme()->get( 'Name' ), $expected_themes, true ) ) {
$this->fail( 'Test depends on using Twenty Twenty-One or Twenty Nineteen theme. If you changed the theme, please update the test.' ); $this->fail( 'Test depends on using Twenty Twenty-One or Twenty Nineteen theme. If you changed the theme, please update the test.' );
} }
} }