diff --git a/packages/php/email-editor/tests/integration/Engine/Email_Editor_Test.php b/packages/php/email-editor/tests/integration/Engine/Email_Editor_Test.php index f7143f1187..f5c9f3c5c9 100644 --- a/packages/php/email-editor/tests/integration/Engine/Email_Editor_Test.php +++ b/packages/php/email-editor/tests/integration/Engine/Email_Editor_Test.php @@ -1,36 +1,62 @@ -emailEditor = $this->di_container->get( Email_Editor::class ); - $this->postRegisterCallback = function ( $postTypes ) { - $postTypes[] = array( + $this->email_editor = $this->di_container->get( Email_Editor::class ); + $this->post_register_callback = function ( $post_types ) { + $post_types[] = array( 'name' => 'custom_email_type', 'args' => array(), 'meta' => array(), ); - return $postTypes; + return $post_types; }; - add_filter( 'mailpoet_email_editor_post_types', $this->postRegisterCallback ); - $this->emailEditor->initialize(); + add_filter( 'mailpoet_email_editor_post_types', $this->post_register_callback ); + $this->email_editor->initialize(); } + /** + * Test if the email register custom post type + */ public function testItRegistersCustomPostTypeAddedViaHook() { - $postTypes = get_post_types(); - $this->assertArrayHasKey( 'custom_email_type', $postTypes ); + $post_types = get_post_types(); + $this->assertArrayHasKey( 'custom_email_type', $post_types ); } + /** + * Clean up after each test + */ public function _after() { parent::_after(); - remove_filter( 'mailpoet_email_editor_post_types', $this->postRegisterCallback ); + remove_filter( 'mailpoet_email_editor_post_types', $this->post_register_callback ); } } diff --git a/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Blocks_Registry_Test.php b/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Blocks_Registry_Test.php index b5d9080153..981e64dc22 100644 --- a/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Blocks_Registry_Test.php +++ b/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Blocks_Registry_Test.php @@ -1,33 +1,58 @@ -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 ); diff --git a/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Content_Renderer_Test.php b/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Content_Renderer_Test.php index 9566ed2bba..7211dd4a0a 100644 --- a/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Content_Renderer_Test.php +++ b/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Content_Renderer_Test.php @@ -1,5 +1,11 @@ -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' => '
Hello!
', ) ); } + /** + * Test it renders the content. + */ public function testItRendersContent(): void { $template = new \WP_Block_Template(); $template->id = 'template-id'; $template->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 = ''; - $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 ) ) { diff --git a/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Dummy_Block_Renderer.php b/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Dummy_Block_Renderer.php index df73383863..b7342da0d9 100644 --- a/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Dummy_Block_Renderer.php +++ b/packages/php/email-editor/tests/integration/Engine/Renderer/ContentRenderer/Dummy_Block_Renderer.php @@ -1,10 +1,27 @@ -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'] = '">Hello!
', ) ); } + /** + * 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 ) ) { diff --git a/packages/php/email-editor/tests/integration/Engine/Theme_Controller_Test.php b/packages/php/email-editor/tests/integration/Engine/Theme_Controller_Test.php index 66c4020b25..4b01d21669 100644 --- a/packages/php/email-editor/tests/integration/Engine/Theme_Controller_Test.php +++ b/packages/php/email-editor/tests/integration/Engine/Theme_Controller_Test.php @@ -1,18 +1,38 @@ -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() { - $css = $this->themeController->get_stylesheet_for_rendering(); - // Font families + $css = $this->theme_controller->get_stylesheet_for_rendering(); + // Font families. verify( $css )->stringContainsString( '.has-arial-font-family' ); verify( $css )->stringContainsString( '.has-comic-sans-ms-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-x-large-font-size' ); - // Font sizes + // Font sizes. verify( $css )->stringContainsString( '.has-small-font-size' ); verify( $css )->stringContainsString( '.has-medium-font-size' ); verify( $css )->stringContainsString( '.has-large-font-size' ); verify( $css )->stringContainsString( '.has-x-large-font-size' ); - // Colors + // Colors. verify( $css )->stringContainsString( '.has-black-color' ); verify( $css )->stringContainsString( '.has-black-background-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() { - verify( $this->themeController->translate_slug_to_font_size( 'small' ) )->equals( '13px' ); - verify( $this->themeController->translate_slug_to_font_size( 'medium' ) )->equals( '16px' ); - verify( $this->themeController->translate_slug_to_font_size( 'large' ) )->equals( '28px' ); - verify( $this->themeController->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( 'small' ) )->equals( '13px' ); + verify( $this->theme_controller->translate_slug_to_font_size( 'medium' ) )->equals( '16px' ); + verify( $this->theme_controller->translate_slug_to_font_size( 'large' ) )->equals( '28px' ); + verify( $this->theme_controller->translate_slug_to_font_size( 'x-large' ) )->equals( '42px' ); + 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() { - verify( $this->themeController->translate_slug_to_color( 'black' ) )->equals( '#000000' ); - verify( $this->themeController->translate_slug_to_color( 'white' ) )->equals( '#ffffff' ); - verify( $this->themeController->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( 'black' ) )->equals( '#000000' ); + verify( $this->theme_controller->translate_slug_to_color( 'white' ) )->equals( '#ffffff' ); + verify( $this->theme_controller->translate_slug_to_color( 'cyan-bluish-gray' ) )->equals( '#abb8c3' ); + verify( $this->theme_controller->translate_slug_to_color( 'pale-pink' ) )->equals( '#f78da7' ); $this->checkCorrectThemeConfiguration(); 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() { $this->checkCorrectThemeConfiguration(); - $settings = $this->themeController->get_settings(); + $settings = $this->theme_controller->get_settings(); if ( wp_get_theme()->get( 'Name' ) === 'Twenty Twenty-One' ) { verify( $settings['color']['palette']['theme'] )->notEmpty(); } } + /** + * Test if the theme controller returns correct preset variables map + */ public function testItReturnsCorrectPresetVariablesMap() { - $variableMap = $this->themeController->get_variables_values_map(); - verify( $variableMap['--wp--preset--color--black'] )->equals( '#000000' ); - verify( $variableMap['--wp--preset--spacing--20'] )->equals( '20px' ); + $variable_map = $this->theme_controller->get_variables_values_map(); + verify( $variable_map['--wp--preset--color--black'] )->equals( '#000000' ); + 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. */ private function checkCorrectThemeConfiguration() { - $expectedThemes = array( 'Twenty Twenty-One' ); - if ( ! in_array( wp_get_theme()->get( 'Name' ), $expectedThemes ) ) { + $expected_themes = array( 'Twenty Twenty-One' ); + 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.' ); } }