Make WordPress Core

Changeset 56562


Ignore:
Timestamp:
09/13/2023 12:03:14 PM (15 months ago)
Author:
Bernhard Reiter
Message:

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.

Location:
trunk/tests/phpunit
Files:
3 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/block-template-utils.php

    r56145 r56562  
    162162    }
    163163
     164    /**
     165     * @ticket 59325
     166     *
     167     * @covers ::_build_block_template_result_from_file
     168     *
     169     * @dataProvider data_build_block_template_result_from_file_injects_theme_attribute
     170     *
     171     * @param string $filename The template's filename.
     172     * @param string $expected The expected block markup.
     173     */
     174    public function test_build_block_template_result_from_file_injects_theme_attribute( $filename, $expected ) {
     175        $template = _build_block_template_result_from_file(
     176            array(
     177                'slug' => 'single',
     178                'path' => DIR_TESTDATA . "/templates/$filename",
     179            ),
     180            'wp_template'
     181        );
     182        $this->assertSame( $expected, $template->content );
     183    }
     184
     185    /**
     186     * Data provider.
     187     *
     188     * @return array[]
     189     */
     190    public function data_build_block_template_result_from_file_injects_theme_attribute() {
     191        $theme = 'block-theme';
     192        return array(
     193            'a template with a template part block'  => array(
     194                'filename' => 'template-with-template-part.html',
     195                'expected' => sprintf(
     196                    '<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header","theme":"%s"} /-->',
     197                    $theme
     198                ),
     199            ),
     200            'a template with a template part block nested inside another block' => array(
     201                'filename' => 'template-with-nested-template-part.html',
     202                'expected' => sprintf(
     203                    '<!-- wp:group -->
     204<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header","theme":"%s"} /-->
     205<!-- /wp:group -->',
     206                    $theme
     207                ),
     208            ),
     209            'a template with a template part block with an existing theme attribute' => array(
     210                'filename' => 'template-with-template-part-with-existing-theme-attribute.html',
     211                'expected' => '<!-- wp:template-part {"slug":"header","theme":"fake-theme","align":"full", "tagName":"header","className":"site-header"} /-->',
     212            ),
     213            'a template with no template part block' => array(
     214                'filename' => 'template.html',
     215                'expected' => '<!-- wp:paragraph -->
     216<p>Just a paragraph</p>
     217<!-- /wp:paragraph -->',
     218            ),
     219        );
     220    }
     221
    164222    public function test_inject_theme_attribute_in_block_template_content() {
    165223        $theme                           = get_stylesheet();
Note: See TracChangeset for help on using the changeset viewer.