Add Content Renderer test for blocks rendering

[MAILPOET-6043]
This commit is contained in:
Pavel Dohnal
2024-11-12 13:55:04 +01:00
committed by Rostislav Wolný
parent bee6d3a8aa
commit 97729b21ae
2 changed files with 58 additions and 3 deletions

View File

@@ -131,13 +131,14 @@ class Content_Renderer {
}
/**
* Render block
* Renders block
* Translates block's HTML to HTML suitable for email clients. The method is intended as a callback for 'render_block' filter.
*
* @param string $block_content Block content.
* @param array $parsed_block Parsed block.
* @return string
*/
public function render_block( $block_content, $parsed_block ) {
public function render_block( string $block_content, array $parsed_block ): string {
$renderer = $this->blocks_registry->get_block_renderer( $parsed_block['blockName'] );
if ( ! $renderer ) {
$renderer = $this->blocks_registry->get_fallback_renderer();

View File

@@ -72,6 +72,60 @@ class Content_Renderer_Test extends \MailPoetTest {
verify( $paragraph_styles )->stringContainsString( 'display: block' );
}
/**
* Test It Renders Block With Fallback Renderer
*/
public function testItRendersBlockWithFallbackRenderer(): void {
$fallback_renderer = $this->createMock( Block_Renderer::class );
$fallback_renderer->expects( $this->once() )->method( 'render' );
$blocks_registry = $this->createMock( Blocks_Registry::class );
$blocks_registry->expects( $this->once() )->method( 'get_block_renderer' )->willReturn( null );
$blocks_registry->expects( $this->once() )->method( 'get_fallback_renderer' )->willReturn( $fallback_renderer );
$renderer = $this->getServiceWithOverrides(
Content_Renderer::class,
array(
'blocks_registry' => $blocks_registry,
)
);
$renderer->render_block( 'content', array( 'blockName' => 'block' ) );
}
/**
* Test It Renders Block With Block Renderer
*/
public function testItRendersBlockWithBlockRenderer(): void {
$renderer = $this->createMock( Block_Renderer::class );
$blocks_registry = $this->createMock( Blocks_Registry::class );
$blocks_registry->expects( $this->once() )->method( 'get_block_renderer' )->willReturn( $renderer );
$blocks_registry->expects( $this->never() )->method( 'get_fallback_renderer' )->willReturn( null );
$renderer = $this->getServiceWithOverrides(
Content_Renderer::class,
array(
'blocks_registry' => $blocks_registry,
)
);
$renderer->render_block( 'content', array( 'blockName' => 'block' ) );
}
/**
* Test It Renders Block When no Renderer available
*/
public function testItReturnsContentIfNoRendererAvailable(): void {
$blocks_registry = $this->createMock( Blocks_Registry::class );
$blocks_registry->expects( $this->once() )->method( 'get_block_renderer' )->willReturn( null );
$blocks_registry->expects( $this->once() )->method( 'get_fallback_renderer' )->willReturn( null );
$renderer = $this->getServiceWithOverrides(
Content_Renderer::class,
array(
'blocks_registry' => $blocks_registry,
)
);
verify( $renderer->render_block( 'content', array( 'blockName' => 'block' ) ) )->equals( 'content' );
}
/**
* Get the value of the style attribute for a given tag in the HTML.
*