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,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;
use MailPoet\EmailEditor\Integrations\Core\Renderer\Blocks\Text;
require_once __DIR__ . '/Dummy_Block_Renderer.php';
/**
* Integration test for Blocks_Registry
*/
class Blocks_Registry_Test extends \MailPoetTest {
/** @var Blocks_Registry */
/**
* Instance of Blocks_Registry
*
* @var Blocks_Registry
*/
private $registry;
/**
* Set up before each test.
*/
public function _before() {
parent::_before();
$this->registry = $this->di_container->get( Blocks_Registry::class );
}
/**
* Test it returns null for unknown renderer.
*/
public function testItReturnsNullForUnknownRenderer() {
$storedRenderer = $this->registry->get_block_renderer( 'test' );
verify( $storedRenderer )->null();
$stored_renderer = $this->registry->get_block_renderer( 'test' );
verify( $stored_renderer )->null();
}
/**
* Test it stores added renderer.
*/
public function testItStoresAddedRenderer() {
$renderer = new Text();
$this->registry->add_block_renderer( 'test', $renderer );
$storedRenderer = $this->registry->get_block_renderer( 'test' );
verify( $storedRenderer )->equals( $renderer );
$stored_renderer = $this->registry->get_block_renderer( 'test' );
verify( $stored_renderer )->equals( $renderer );
}
/**
* Test it reports which renderers are registered.
*/
public function testItReportsWhichRenderersAreRegistered() {
$renderer = new Text();
$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;
use MailPoet\EmailEditor\Engine\Email_Editor;
@@ -7,44 +13,71 @@ use MailPoet\EmailEditor\Integrations\MailPoet\Blocks\BlockTypesController;
require_once __DIR__ . '/Dummy_Block_Renderer.php';
/**
* Integration test for Content_Renderer
*/
class Content_Renderer_Test extends \MailPoetTest {
/**
* Instance of the renderer.
*
* @var Content_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 {
parent::_before();
$this->di_container->get( Email_Editor::class )->initialize();
$this->di_container->get( BlockTypesController::class )->initialize();
$this->renderer = $this->di_container->get( Content_Renderer::class );
$this->emailPost = $this->tester->create_post(
$this->renderer = $this->di_container->get( Content_Renderer::class );
$this->email_post = $this->tester->create_post(
array(
'post_content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->',
)
);
}
/**
* Test it renders the content.
*/
public function testItRendersContent(): void {
$template = new \WP_Block_Template();
$template->id = 'template-id';
$template->content = '<!-- wp:core/post-content /-->';
$content = $this->renderer->render(
$this->emailPost,
$this->email_post,
$template
);
verify( $content )->stringContainsString( 'Hello!' );
}
/**
* Test it inlines content styles.
*/
public function testItInlinesContentStyles(): void {
$template = new \WP_Block_Template();
$template->id = 'template-id';
$template->content = '<!-- wp:core/post-content /-->';
$rendered = $this->renderer->render( $this->emailPost, $template );
$paragraphStyles = $this->getStylesValueForTag( $rendered, 'p' );
verify( $paragraphStyles )->stringContainsString( 'margin: 0' );
verify( $paragraphStyles )->stringContainsString( 'display: block' );
$rendered = $this->renderer->render( $this->email_post, $template );
$paragraph_styles = $this->getStylesValueForTag( $rendered, 'p' );
verify( $paragraph_styles )->stringContainsString( 'margin: 0' );
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 {
$html = new \WP_HTML_Tag_Processor( $html );
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;
use MailPoet\EmailEditor\Engine\Settings_Controller;
/**
* Dummy block renderer for testing purposes.
*/
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 {
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;
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';
/**
* Integration test for Flex_Layout_Renderer
*/
class Flex_Layout_Renderer_Test extends \MailPoetTest {
/** @var Flex_Layout_Renderer */
/**
* Instance of the renderer.
*
* @var Flex_Layout_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 {
parent::_before();
$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 );
}
/**
* Test it renders inner blocks.
*/
public function testItRendersInnerBlocks(): void {
$parsedBlock = array(
$parsed_block = array(
'innerBlocks' => array(
array(
'blockName' => 'dummy/block',
@@ -37,13 +60,16 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
),
'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 2' );
}
/**
* Test it handles justifying the content.
*/
public function testItHandlesJustification(): void {
$parsedBlock = array(
$parsed_block = array(
'innerBlocks' => array(
array(
'blockName' => 'dummy/block',
@@ -52,24 +78,27 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
),
'email_attrs' => array(),
);
// Default justification is left
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController );
// Default justification is left.
$output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
verify( $output )->stringContainsString( 'text-align: left' );
verify( $output )->stringContainsString( 'align="left"' );
// Right justification
$parsedBlock['attrs']['layout']['justifyContent'] = 'right';
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController );
// Right justification.
$parsed_block['attrs']['layout']['justifyContent'] = 'right';
$output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
verify( $output )->stringContainsString( 'text-align: right' );
verify( $output )->stringContainsString( 'align="right"' );
// Center justification
$parsedBlock['attrs']['layout']['justifyContent'] = 'center';
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController );
// Center justification.
$parsed_block['attrs']['layout']['justifyContent'] = 'center';
$output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
verify( $output )->stringContainsString( 'text-align: center' );
verify( $output )->stringContainsString( 'align="center"' );
}
/**
* Test it escapes attributes.
*/
public function testItEscapesAttributes(): void {
$parsedBlock = array(
$parsed_block = array(
'innerBlocks' => array(
array(
'blockName' => 'dummy/block',
@@ -78,13 +107,16 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
),
'email_attrs' => array(),
);
$parsedBlock['attrs']['layout']['justifyContent'] = '"> <script>alert("XSS")</script><div style="text-align: right';
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController );
$parsed_block['attrs']['layout']['justifyContent'] = '"> <script>alert("XSS")</script><div style="text-align: right';
$output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
verify( $output )->stringNotContainsString( '<script>alert("XSS")</script>' );
}
/**
* Test that the renderer computes proper widths for reasonable settings.
*/
public function testInComputesProperWidthsForReasonableSettings(): void {
$parsedBlock = array(
$parsed_block = array(
'innerBlocks' => array(),
'email_attrs' => array(
'width' => '640px',
@@ -92,7 +124,7 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
);
// 50% and 25%
$parsedBlock['innerBlocks'] = array(
$parsed_block['innerBlocks'] = array(
array(
'blockName' => 'dummy/block',
'innerHtml' => 'Dummy 1',
@@ -104,13 +136,13 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
'attrs' => array( 'width' => '25' ),
),
);
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController );
$flexItems = $this->getFlexItemsFromOutput( $output );
verify( $flexItems[0] )->stringContainsString( 'width:312px;' );
verify( $flexItems[1] )->stringContainsString( 'width:148px;' );
$output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
$flex_items = $this->getFlexItemsFromOutput( $output );
verify( $flex_items[0] )->stringContainsString( 'width:312px;' );
verify( $flex_items[1] )->stringContainsString( 'width:148px;' );
// 25% and 25% and auto
$parsedBlock['innerBlocks'] = array(
$parsed_block['innerBlocks'] = array(
array(
'blockName' => 'dummy/block',
'innerHtml' => 'Dummy 1',
@@ -127,14 +159,14 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
'attrs' => array(),
),
);
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController );
$flexItems = $this->getFlexItemsFromOutput( $output );
verify( $flexItems[0] )->stringContainsString( 'width:148px;' );
verify( $flexItems[1] )->stringContainsString( 'width:148px;' );
verify( $flexItems[2] )->stringNotContainsString( 'width:' );
$output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
$flex_items = $this->getFlexItemsFromOutput( $output );
verify( $flex_items[0] )->stringContainsString( 'width:148px;' );
verify( $flex_items[1] )->stringContainsString( 'width:148px;' );
verify( $flex_items[2] )->stringNotContainsString( 'width:' );
// 50% and 50%
$parsedBlock['innerBlocks'] = array(
$parsed_block['innerBlocks'] = array(
array(
'blockName' => 'dummy/block',
'innerHtml' => 'Dummy 1',
@@ -146,14 +178,17 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
'attrs' => array( 'width' => '50' ),
),
);
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController );
$flexItems = $this->getFlexItemsFromOutput( $output );
verify( $flexItems[0] )->stringContainsString( 'width:312px;' );
verify( $flexItems[1] )->stringContainsString( 'width:312px;' );
$output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
$flex_items = $this->getFlexItemsFromOutput( $output );
verify( $flex_items[0] )->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 {
$parsedBlock = array(
$parsed_block = array(
'innerBlocks' => array(),
'email_attrs' => array(
'width' => '640px',
@@ -161,7 +196,7 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
);
// 100% and 25%
$parsedBlock['innerBlocks'] = array(
$parsed_block['innerBlocks'] = array(
array(
'blockName' => 'dummy/block',
'innerHtml' => 'Dummy 1',
@@ -173,13 +208,13 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
'attrs' => array( 'width' => '25' ),
),
);
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController );
$flexItems = $this->getFlexItemsFromOutput( $output );
verify( $flexItems[0] )->stringContainsString( 'width:508px;' );
verify( $flexItems[1] )->stringContainsString( 'width:105px;' );
$output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
$flex_items = $this->getFlexItemsFromOutput( $output );
verify( $flex_items[0] )->stringContainsString( 'width:508px;' );
verify( $flex_items[1] )->stringContainsString( 'width:105px;' );
// 100% and 100%
$parsedBlock['innerBlocks'] = array(
$parsed_block['innerBlocks'] = array(
array(
'blockName' => 'dummy/block',
'innerHtml' => 'Dummy 1',
@@ -191,13 +226,13 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
'attrs' => array( 'width' => '100' ),
),
);
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController );
$flexItems = $this->getFlexItemsFromOutput( $output );
verify( $flexItems[0] )->stringContainsString( 'width:312px;' );
verify( $flexItems[1] )->stringContainsString( 'width:312px;' );
$output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
$flex_items = $this->getFlexItemsFromOutput( $output );
verify( $flex_items[0] )->stringContainsString( 'width:312px;' );
verify( $flex_items[1] )->stringContainsString( 'width:312px;' );
// 100% and auto
$parsedBlock['innerBlocks'] = array(
$parsed_block['innerBlocks'] = array(
array(
'blockName' => 'dummy/block',
'innerHtml' => 'Dummy 1',
@@ -209,23 +244,38 @@ class Flex_Layout_Renderer_Test extends \MailPoetTest {
'attrs' => array(),
),
);
$output = $this->renderer->render_inner_blocks_in_layout( $parsedBlock, $this->settingsController );
$flexItems = $this->getFlexItemsFromOutput( $output );
verify( $flexItems[0] )->stringContainsString( 'width:508px;' );
verify( $flexItems[1] )->stringNotContainsString( 'width:' );
$output = $this->renderer->render_inner_blocks_in_layout( $parsed_block, $this->settings_controller );
$flex_items = $this->getFlexItemsFromOutput( $output );
verify( $flex_items[0] )->stringContainsString( 'width:508px;' );
verify( $flex_items[1] )->stringNotContainsString( 'width:' );
}
/**
* Get flex items from the output.
*
* @param string $output Output.
*/
private function getFlexItemsFromOutput( string $output ): array {
$matches = array();
preg_match_all( '/<td class="layout-flex-item" style="(.*)">/', $output, $matches );
return explode( '><', $matches[0][0] ?? array() );
}
public function renderDummyBlock( $blockContent, $parsedBlock ): string {
$dummyRenderer = new Dummy_Block_Renderer();
return $dummyRenderer->render( $blockContent, $parsedBlock, $this->settingsController );
/**
* Render a dummy block.
*
* @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 {
parent::_after();
unregister_block_type( 'dummy/block' );

View File

@@ -1,21 +1,42 @@
<?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;
use MailPoet\EmailEditor\Engine\Email_Editor;
use MailPoet\EmailEditor\Engine\Settings_Controller;
use MailPoet\EmailEditor\Engine\Theme_Controller;
/**
* Integration test for Renderer
*/
class Renderer_Test extends \MailPoetTest {
/**
* The renderer.
*
* @var 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 {
parent::_before();
$this->di_container->get( Email_Editor::class )->initialize();
$this->renderer = $this->di_container->get( Renderer::class );
$styles = array(
$this->renderer = $this->di_container->get( Renderer::class );
$styles = array(
'spacing' => array(
'padding' => array(
'bottom' => '4px',
@@ -31,36 +52,39 @@ class Renderer_Test extends \MailPoetTest {
'background' => '#123456',
),
);
$themeJsonMock = $this->createMock( \WP_Theme_JSON::class );
$themeJsonMock->method( 'get_data' )->willReturn(
$theme_json_mock = $this->createMock( \WP_Theme_JSON::class );
$theme_json_mock->method( 'get_data' )->willReturn(
array(
'styles' => $styles,
)
);
$settingsControllerMock = $this->createMock( Settings_Controller::class );
$settingsControllerMock->method( 'get_email_styles' )->willReturn( $styles );
$themeControllerMock = $this->createMock( Theme_Controller::class );
$themeControllerMock->method( 'get_theme' )->willReturn( $themeJsonMock );
$themeControllerMock->method( 'get_styles' )->willReturn( $styles );
$themeControllerMock->method( 'get_layout_settings' )->willReturn( array( 'contentSize' => '660px' ) );
$settings_controller_mock = $this->createMock( Settings_Controller::class );
$settings_controller_mock->method( 'get_email_styles' )->willReturn( $styles );
$theme_controller_mock = $this->createMock( Theme_Controller::class );
$theme_controller_mock->method( 'get_theme' )->willReturn( $theme_json_mock );
$theme_controller_mock->method( 'get_styles' )->willReturn( $styles );
$theme_controller_mock->method( 'get_layout_settings' )->willReturn( array( 'contentSize' => '660px' ) );
$this->renderer = $this->getServiceWithOverrides(
$this->renderer = $this->getServiceWithOverrides(
Renderer::class,
array(
'settingsController' => $settingsControllerMock,
'themeController' => $themeControllerMock,
'settingsController' => $settings_controller_mock,
'themeController' => $theme_controller_mock,
)
);
$this->emailPost = $this->tester->create_post(
$this->email_post = $this->tester->create_post(
array(
'post_content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->',
)
);
}
/**
* Test it renders template with content.
*/
public function testItRendersTemplateWithContent(): void {
$rendered = $this->renderer->render(
$this->emailPost,
$this->email_post,
'Subject',
'Preheader content',
'en',
@@ -75,31 +99,40 @@ class Renderer_Test extends \MailPoetTest {
verify( $rendered['text'] )->stringContainsString( 'Hello!' );
}
/**
* Test it inlines styles.
*/
public function testItInlinesStyles(): void {
$stylesCallback = function ( $styles ) {
$styles_callback = function ( $styles ) {
return $styles . 'body { color: pink; }';
};
add_filter( 'mailpoet_email_renderer_styles', $stylesCallback );
$rendered = $this->renderer->render( $this->emailPost, 'Subject', '', 'en' );
add_filter( 'mailpoet_email_renderer_styles', $styles_callback );
$rendered = $this->renderer->render( $this->email_post, 'Subject', '', 'en' );
$style = $this->getStylesValueForTag( $rendered['html'], array( 'tag_name' => 'body' ) );
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 {
$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' ) );
verify( $style )->stringContainsString( 'margin: 0; padding: 0;' );
}
/**
* Test it inlines wrapper styles.
*/
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' ) );
verify( $style )->stringContainsString( 'background-color: #123456' );
// Verify layout element styles
// Verify layout element styles.
$doc = new \DOMDocument();
$doc->loadHTML( $rendered['html'] );
$xpath = new \DOMXPath( $doc );
@@ -119,6 +152,13 @@ class Renderer_Test extends \MailPoetTest {
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 {
$html = new \WP_HTML_Tag_Processor( $html );
if ( $html->next_tag( $query ) ) {