Use user theme in renderer instead of template themes

We no longer read styles associate with templates when we render an email
but we read the user theme instead
[MAILPOET-6335]
This commit is contained in:
Rostislav Wolny
2024-12-10 17:04:13 +01:00
committed by Aschepikov
parent 15818f37ef
commit 1e1bec4ce0
5 changed files with 73 additions and 104 deletions

View File

@@ -65,38 +65,10 @@ class Renderer_Test extends \MailPoetTest {
$theme_controller_mock->method( 'get_styles' )->willReturn( $styles );
$theme_controller_mock->method( 'get_layout_settings' )->willReturn( array( 'contentSize' => '660px' ) );
// We need to mock only the get_block_template_theme method and templates need to be initialized.
$templates_mock = $this->getMockBuilder( Templates::class )
->setConstructorArgs( array( $this->di_container->get( Utils::class ) ) )
->onlyMethods( array( 'get_block_template_theme' ) )
->getMock();
$templates_mock->initialize();
$templates_mock->method( 'get_block_template_theme' )->willReturn(
array(
'version' => 3,
'styles' => array(
'elements' => array(
'h1' => array(
'typography' => array(
'fontFamily' => 'lato test',
),
),
'heading' => array(
'color' => array(
'background' => 'pale-pink',
),
),
),
),
)
);
$this->renderer = $this->getServiceWithOverrides(
Renderer::class,
array(
'theme_controller' => $theme_controller_mock,
'templates' => $templates_mock,
)
);
$this->email_post = $this->tester->create_post(
@@ -179,28 +151,6 @@ class Renderer_Test extends \MailPoetTest {
verify( $style )->stringContainsString( 'max-width: 660px;' );
}
/**
* Test it inlines block styles from template.
*/
public function testItRendersTemplateStyles(): void {
$email = $this->tester->create_post(
array(
'post_content' => '<!-- wp:heading {"level":1} --><h1 class="wp-block-heading">Hello!</h1><!-- /wp:heading -->',
)
);
$rendered = $this->renderer->render(
$email,
'Subject 1',
'Preheader content 2',
'en',
'noindex,nofollow'
);
verify( $rendered['html'] )->stringContainsString( 'Subject' );
verify( $rendered['html'] )->stringContainsString( 'font-family: lato test;' );
verify( $rendered['html'] )->stringContainsString( ' background-color: pale-pink;' );
}
/**
* Returns the value of the style attribute for the first tag that matches the query.
*

View File

@@ -24,6 +24,27 @@ class Theme_Controller_Test extends \MailPoetTest {
*/
public function _before() {
parent::_before();
// Crete a custom user theme post.
$styles_data = array(
'version' => 3,
'isGlobalStylesUserThemeJSON' => true,
'styles' => array(
'color' => array(
'background' => '#123456',
'text' => '#654321',
),
),
);
$post_data = array(
'post_title' => __( 'Custom Email Styles', 'mailpoet' ),
'post_name' => 'wp-global-styles-mailpoet-email',
'post_content' => (string) wp_json_encode( $styles_data, JSON_FORCE_OBJECT ),
'post_status' => 'publish',
'post_type' => 'wp_global_styles',
);
wp_insert_post( $post_data );
$this->theme_controller = $this->di_container->get( Theme_Controller::class );
}
@@ -122,6 +143,34 @@ class Theme_Controller_Test extends \MailPoetTest {
}
}
/**
* Test if the theme controller loads custom user theme
*/
public function testItLoadsCustomUserTheme() {
$theme = $this->theme_controller->get_theme();
verify( $theme->get_raw_data()['styles']['color']['background'] )->equals( '#123456' );
verify( $theme->get_raw_data()['styles']['color']['text'] )->equals( '#654321' );
}
/**
* Test if the theme controller loads custom user theme and applies it to the styles
*/
public function testItAddCustomUserThemeToStyles() {
$theme = $this->theme_controller->get_theme();
$styles = $theme->get_stylesheet();
verify( $styles )->stringContainsString( 'color: #654321' );
verify( $styles )->stringContainsString( 'background-color: #123456' );
}
/**
* Test if the theme controller returns correct layout settings
*/
public function testGetBaseThemeDoesNotIncludeUserThemeData() {
$theme = $this->theme_controller->get_base_theme();
verify( $theme->get_raw_data()['styles']['color']['background'] )->equals( '#f0f0f0' );
verify( $theme->get_raw_data()['styles']['color']['text'] )->equals( '#000000' );
}
/**
* Test if the theme controller returns correct color palette
*/