Themes: Add test for theme atttibute in file-based block template.

While we already have unit test coverage for `_inject_theme_attribute_in_block_template_content`, those tests only verify that ''that'' function does what is supposed to do; there's however no guarantee that `_build_block_template_result_from_file` uses that function (or whatever other technique) to actually inject the theme attribute.

This patch adds test coverage to verify that the theme attribute is correctly injected by `_build_block_template_result_from_file`.

Props costdev, gziolo, mukesh27, spacedmonkey.
Fixes #59325. See #59313.

git-svn-id: https://develop.svn.wordpress.org/trunk@56562 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Bernie Reiter 2023-09-13 12:03:14 +00:00
parent 36f8240cc3
commit 8e4ff2a01d
4 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,3 @@
<!-- wp:group -->
<!-- wp:template-part {"slug":"header","align":"full", "tagName":"header","className":"site-header"} /-->
<!-- /wp:group -->

View File

@ -0,0 +1 @@
<!-- wp:template-part {"slug":"header","theme":"fake-theme","align":"full", "tagName":"header","className":"site-header"} /-->

View File

@ -0,0 +1 @@
<!-- wp:template-part {"slug":"header","align":"full", "tagName":"header","className":"site-header"} /-->

View File

@ -161,6 +161,64 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase {
$this->assertEmpty( $template_part->modified );
}
/**
* @ticket 59325
*
* @covers ::_build_block_template_result_from_file
*
* @dataProvider data_build_block_template_result_from_file_injects_theme_attribute
*
* @param string $filename The template's filename.
* @param string $expected The expected block markup.
*/
public function test_build_block_template_result_from_file_injects_theme_attribute( $filename, $expected ) {
$template = _build_block_template_result_from_file(
array(
'slug' => 'single',
'path' => DIR_TESTDATA . "/templates/$filename",
),
'wp_template'
);
$this->assertSame( $expected, $template->content );
}
/**
* Data provider.
*
* @return array[]
*/
public function data_build_block_template_result_from_file_injects_theme_attribute() {
$theme = 'block-theme';
return array(
'a template with a template part block' => array(
'filename' => 'template-with-template-part.html',
'expected' => sprintf(
'<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header","theme":"%s"} /-->',
$theme
),
),
'a template with a template part block nested inside another block' => array(
'filename' => 'template-with-nested-template-part.html',
'expected' => sprintf(
'<!-- wp:group -->
<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header","theme":"%s"} /-->
<!-- /wp:group -->',
$theme
),
),
'a template with a template part block with an existing theme attribute' => array(
'filename' => 'template-with-template-part-with-existing-theme-attribute.html',
'expected' => '<!-- wp:template-part {"slug":"header","theme":"fake-theme","align":"full", "tagName":"header","className":"site-header"} /-->',
),
'a template with no template part block' => array(
'filename' => 'template.html',
'expected' => '<!-- wp:paragraph -->
<p>Just a paragraph</p>
<!-- /wp:paragraph -->',
),
);
}
public function test_inject_theme_attribute_in_block_template_content() {
$theme = get_stylesheet();
$content_without_theme_attribute = '<!-- wp:template-part {"slug":"header","align":"full", "tagName":"header","className":"site-header"} /-->';