Make WordPress Core

Changeset 56983


Ignore:
Timestamp:
10/23/2023 05:36:50 AM (14 months ago)
Author:
gziolo
Message:

Tests: Improve code coverage for _build_block_template_result_from_file

Props costdev, bernhard-reiter.
See #54335, #59325.
Follow-up for [56562].

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

Legend:

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

    r56960 r56983  
    8787    }
    8888
    89     public function test_build_block_template_result_from_post() {
    90         $template = _build_block_template_result_from_post(
    91             self::$template_post,
    92             'wp_template'
    93         );
    94 
    95         $this->assertNotWPError( $template );
    96         $this->assertSame( get_stylesheet() . '//my_template', $template->id );
    97         $this->assertSame( get_stylesheet(), $template->theme );
    98         $this->assertSame( 'my_template', $template->slug );
    99         $this->assertSame( 'publish', $template->status );
    100         $this->assertSame( 'custom', $template->source );
    101         $this->assertSame( 'My Template', $template->title );
    102         $this->assertSame( 'Description of my template', $template->description );
    103         $this->assertSame( 'wp_template', $template->type );
    104         $this->assertSame( self::$template_post->post_modified, $template->modified, 'Template result properties match' );
    105 
    106         // Test template parts.
    107         $template_part = _build_block_template_result_from_post(
    108             self::$template_part_post,
    109             'wp_template_part'
    110         );
    111         $this->assertNotWPError( $template_part );
    112         $this->assertSame( get_stylesheet() . '//my_template_part', $template_part->id );
    113         $this->assertSame( get_stylesheet(), $template_part->theme );
    114         $this->assertSame( 'my_template_part', $template_part->slug );
    115         $this->assertSame( 'publish', $template_part->status );
    116         $this->assertSame( 'custom', $template_part->source );
    117         $this->assertSame( 'My Template Part', $template_part->title );
    118         $this->assertSame( 'Description of my template part', $template_part->description );
    119         $this->assertSame( 'wp_template_part', $template_part->type );
    120         $this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
    121         $this->assertSame( self::$template_part_post->post_modified, $template_part->modified, 'Template part result properties match' );
    122     }
    123 
    124     public function test_build_block_template_result_from_file() {
    125         $template = _build_block_template_result_from_file(
    126             array(
    127                 'slug' => 'single',
    128                 'path' => __DIR__ . '/../data/templates/template.html',
    129             ),
    130             'wp_template'
    131         );
    132 
    133         $this->assertSame( get_stylesheet() . '//single', $template->id );
    134         $this->assertSame( get_stylesheet(), $template->theme );
    135         $this->assertSame( 'single', $template->slug );
    136         $this->assertSame( 'publish', $template->status );
    137         $this->assertSame( 'theme', $template->source );
    138         $this->assertSame( 'Single Posts', $template->title );
    139         $this->assertSame( 'Displays single posts on your website unless a custom template has been applied to that post or a dedicated template exists.', $template->description );
    140         $this->assertSame( 'wp_template', $template->type );
    141         $this->assertEmpty( $template->modified );
    142 
    143         // Test template parts.
    144         $template_part = _build_block_template_result_from_file(
    145             array(
    146                 'slug' => 'header',
    147                 'path' => __DIR__ . '/../data/templates/template.html',
    148                 'area' => WP_TEMPLATE_PART_AREA_HEADER,
    149             ),
    150             'wp_template_part'
    151         );
    152         $this->assertSame( get_stylesheet() . '//header', $template_part->id );
    153         $this->assertSame( get_stylesheet(), $template_part->theme );
    154         $this->assertSame( 'header', $template_part->slug );
    155         $this->assertSame( 'publish', $template_part->status );
    156         $this->assertSame( 'theme', $template_part->source );
    157         $this->assertSame( 'header', $template_part->title );
    158         $this->assertSame( '', $template_part->description );
    159         $this->assertSame( 'wp_template_part', $template_part->type );
    160         $this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
    161         $this->assertEmpty( $template_part->modified );
    162     }
    163 
    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 
    22289    /**
    22390     * @ticket 59338
Note: See TracChangeset for help on using the changeset viewer.