Make WordPress Core


Ignore:
Timestamp:
11/08/2021 11:09:53 PM (3 years ago)
Author:
noisysocks
Message:

Editor: Add block theme infrastructure

Adds the required infrastructure to render block-based themes. This is sourced
from the Gutenberg plugin.

Fixes #54335.
Props bernhard-reiter, youknowriad, ntsekouras, hellofromtonya.

File:
1 edited

Legend:

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

    r52010 r52062  
    77
    88/**
    9  * Tests for the Block Template Loader abstraction layer.
     9 * Tests for the Block Templates abstraction layer.
    1010 */
    1111class Block_Template_Utils_Test extends WP_UnitTestCase {
    1212    private static $post;
     13    private static $template_part_post;
    1314
    1415    public static function wpSetUpBeforeClass() {
     16        // We may need a block theme.
     17        // switch_theme( 'tt1-blocks' );
     18
     19        // Set up a template post corresponding to a different theme.
     20        // We do this to ensure resolution and slug creation works as expected,
     21        // even with another post of that same name present for another theme.
     22        $args       = array(
     23            'post_type'    => 'wp_template',
     24            'post_name'    => 'my_template',
     25            'post_title'   => 'My Template',
     26            'post_content' => 'Content',
     27            'post_excerpt' => 'Description of my template',
     28            'tax_input'    => array(
     29                'wp_theme' => array(
     30                    'this-theme-should-not-resolve',
     31                ),
     32            ),
     33        );
     34        self::$post = self::factory()->post->create_and_get( $args );
     35        wp_set_post_terms( self::$post->ID, 'this-theme-should-not-resolve', 'wp_theme' );
     36
    1537        // Set up template post.
    1638        $args       = array(
     
    2850        self::$post = self::factory()->post->create_and_get( $args );
    2951        wp_set_post_terms( self::$post->ID, get_stylesheet(), 'wp_theme' );
     52
     53        // Set up template part post.
     54        $template_part_args       = array(
     55            'post_type'    => 'wp_template_part',
     56            'post_name'    => 'my_template_part',
     57            'post_title'   => 'My Template Part',
     58            'post_content' => 'Content',
     59            'post_excerpt' => 'Description of my template part',
     60            'tax_input'    => array(
     61                'wp_theme'              => array(
     62                    get_stylesheet(),
     63                ),
     64                'wp_template_part_area' => array(
     65                    WP_TEMPLATE_PART_AREA_HEADER,
     66                ),
     67            ),
     68        );
     69        self::$template_part_post = self::factory()->post->create_and_get( $template_part_args );
     70        wp_set_post_terms( self::$template_part_post->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' );
     71        wp_set_post_terms( self::$template_part_post->ID, get_stylesheet(), 'wp_theme' );
    3072    }
    3173
     
    3577
    3678    public function test_build_template_result_from_post() {
    37         $template = _build_template_result_from_post(
     79        $template = _build_block_template_result_from_post(
    3880            self::$post,
    3981            'wp_template'
     
    4183
    4284        $this->assertNotWPError( $template );
    43         $this->assertSame( get_stylesheet() . '//my_template', $template->id );
    44         $this->assertSame( get_stylesheet(), $template->theme );
    45         $this->assertSame( 'my_template', $template->slug );
    46         $this->assertSame( 'publish', $template->status );
    47         $this->assertSame( 'custom', $template->source );
    48         $this->assertSame( 'My Template', $template->title );
    49         $this->assertSame( 'Description of my template', $template->description );
    50         $this->assertSame( 'wp_template', $template->type );
     85        $this->assertEquals( get_stylesheet() . '//my_template', $template->id );
     86        $this->assertEquals( get_stylesheet(), $template->theme );
     87        $this->assertEquals( 'my_template', $template->slug );
     88        $this->assertEquals( 'publish', $template->status );
     89        $this->assertEquals( 'custom', $template->source );
     90        $this->assertEquals( 'My Template', $template->title );
     91        $this->assertEquals( 'Description of my template', $template->description );
     92        $this->assertEquals( 'wp_template', $template->type );
     93
     94        // Test template parts.
     95        $template_part = _build_block_template_result_from_post(
     96            self::$template_part_post,
     97            'wp_template_part'
     98        );
     99        $this->assertNotWPError( $template_part );
     100        $this->assertEquals( get_stylesheet() . '//my_template_part', $template_part->id );
     101        $this->assertEquals( get_stylesheet(), $template_part->theme );
     102        $this->assertEquals( 'my_template_part', $template_part->slug );
     103        $this->assertEquals( 'publish', $template_part->status );
     104        $this->assertEquals( 'custom', $template_part->source );
     105        $this->assertEquals( 'My Template Part', $template_part->title );
     106        $this->assertEquals( 'Description of my template part', $template_part->description );
     107        $this->assertEquals( 'wp_template_part', $template_part->type );
     108        $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
     109    }
     110
     111    function test_build_block_template_result_from_file() {
     112        $template = _build_block_template_result_from_file(
     113            array(
     114                'slug' => 'single',
     115                'path' => __DIR__ . '/../data/templates/template.html',
     116            ),
     117            'wp_template'
     118        );
     119
     120        $this->assertEquals( get_stylesheet() . '//single', $template->id );
     121        $this->assertEquals( get_stylesheet(), $template->theme );
     122        $this->assertEquals( 'single', $template->slug );
     123        $this->assertEquals( 'publish', $template->status );
     124        $this->assertEquals( 'theme', $template->source );
     125        $this->assertEquals( 'Single Post', $template->title );
     126        $this->assertEquals( 'Template used to display a single blog post.', $template->description );
     127        $this->assertEquals( 'wp_template', $template->type );
     128
     129        // Test template parts.
     130        $template_part = _build_block_template_result_from_file(
     131            array(
     132                'slug' => 'header',
     133                'path' => __DIR__ . '/../data/templates/template.html',
     134                'area' => WP_TEMPLATE_PART_AREA_HEADER,
     135            ),
     136            'wp_template_part'
     137        );
     138        $this->assertEquals( get_stylesheet() . '//header', $template_part->id );
     139        $this->assertEquals( get_stylesheet(), $template_part->theme );
     140        $this->assertEquals( 'header', $template_part->slug );
     141        $this->assertEquals( 'publish', $template_part->status );
     142        $this->assertEquals( 'theme', $template_part->source );
     143        $this->assertEquals( 'header', $template_part->title );
     144        $this->assertEquals( '', $template_part->description );
     145        $this->assertEquals( 'wp_template_part', $template_part->type );
     146        $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
     147    }
     148
     149    function test_inject_theme_attribute_in_block_template_content() {
     150        $theme                           = get_stylesheet();
     151        $content_without_theme_attribute = '<!-- wp:template-part {"slug":"header","align":"full", "tagName":"header","className":"site-header"} /-->';
     152        $template_content                = _inject_theme_attribute_in_block_template_content(
     153            $content_without_theme_attribute,
     154            $theme
     155        );
     156        $expected                        = sprintf(
     157            '<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header","theme":"%s"} /-->',
     158            get_stylesheet()
     159        );
     160        $this->assertEquals( $expected, $template_content );
     161
     162        $content_without_theme_attribute_nested = '<!-- wp:group --><!-- wp:template-part {"slug":"header","align":"full", "tagName":"header","className":"site-header"} /--><!-- /wp:group -->';
     163        $template_content                       = _inject_theme_attribute_in_block_template_content(
     164            $content_without_theme_attribute_nested,
     165            $theme
     166        );
     167        $expected                               = sprintf(
     168            '<!-- wp:group --><!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header","theme":"%s"} /--><!-- /wp:group -->',
     169            get_stylesheet()
     170        );
     171        $this->assertEquals( $expected, $template_content );
     172
     173        // Does not inject theme when there is an existing theme attribute.
     174        $content_with_existing_theme_attribute = '<!-- wp:template-part {"slug":"header","theme":"fake-theme","align":"full", "tagName":"header","className":"site-header"} /-->';
     175        $template_content                      = _inject_theme_attribute_in_block_template_content(
     176            $content_with_existing_theme_attribute,
     177            $theme
     178        );
     179        $this->assertEquals( $content_with_existing_theme_attribute, $template_content );
     180
     181        // Does not inject theme when there is no template part.
     182        $content_with_no_template_part = '<!-- wp:post-content /-->';
     183        $template_content              = _inject_theme_attribute_in_block_template_content(
     184            $content_with_no_template_part,
     185            $theme
     186        );
     187        $this->assertEquals( $content_with_no_template_part, $template_content );
     188    }
     189
     190    /**
     191     * Should retrieve the template from the theme files.
     192     */
     193    function test_get_block_template_from_file() {
     194        $this->markTestIncomplete();
     195        // Requires switching to a block theme.
     196        /* $id       = get_stylesheet() . '//' . 'index';
     197        $template = get_block_template( $id, 'wp_template' );
     198        $this->assertEquals( $id, $template->id );
     199        $this->assertEquals( get_stylesheet(), $template->theme );
     200        $this->assertEquals( 'index', $template->slug );
     201        $this->assertEquals( 'publish', $template->status );
     202        $this->assertEquals( 'theme', $template->source );
     203        $this->assertEquals( 'wp_template', $template->type );
     204
     205        // Test template parts.
     206        $id       = get_stylesheet() . '//' . 'header';
     207        $template = get_block_template( $id, 'wp_template_part' );
     208        $this->assertEquals( $id, $template->id );
     209        $this->assertEquals( get_stylesheet(), $template->theme );
     210        $this->assertEquals( 'header', $template->slug );
     211        $this->assertEquals( 'publish', $template->status );
     212        $this->assertEquals( 'theme', $template->source );
     213        $this->assertEquals( 'wp_template_part', $template->type );
     214        $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template->area );
     215        */
    51216    }
    52217
     
    57222        $id       = get_stylesheet() . '//' . 'my_template';
    58223        $template = get_block_template( $id, 'wp_template' );
    59         $this->assertSame( $id, $template->id );
    60         $this->assertSame( get_stylesheet(), $template->theme );
    61         $this->assertSame( 'my_template', $template->slug );
    62         $this->assertSame( 'publish', $template->status );
    63         $this->assertSame( 'custom', $template->source );
    64         $this->assertSame( 'wp_template', $template->type );
     224        $this->assertEquals( $id, $template->id );
     225        $this->assertEquals( get_stylesheet(), $template->theme );
     226        $this->assertEquals( 'my_template', $template->slug );
     227        $this->assertEquals( 'publish', $template->status );
     228        $this->assertEquals( 'custom', $template->source );
     229        $this->assertEquals( 'wp_template', $template->type );
     230
     231        // Test template parts.
     232        $id       = get_stylesheet() . '//' . 'my_template_part';
     233        $template = get_block_template( $id, 'wp_template_part' );
     234        $this->assertEquals( $id, $template->id );
     235        $this->assertEquals( get_stylesheet(), $template->theme );
     236        $this->assertEquals( 'my_template_part', $template->slug );
     237        $this->assertEquals( 'publish', $template->status );
     238        $this->assertEquals( 'custom', $template->source );
     239        $this->assertEquals( 'wp_template_part', $template->type );
     240        $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template->area );
    65241    }
    66242
    67243    /**
    68      * Should retrieve block templates.
     244     * Should retrieve block templates (file and CPT)
    69245     */
    70246    public function test_get_block_templates() {
     
    85261        $this->assertContains( get_stylesheet() . '//' . 'my_template', $template_ids );
    86262
     263        // The result might change in a block theme.
     264        // $this->assertContains( get_stylesheet() . '//' . 'index', $template_ids );
     265
    87266        // Filter by slug.
    88267        $templates    = get_block_templates( array( 'slug__in' => array( 'my_template' ) ), 'wp_template' );
    89268        $template_ids = get_template_ids( $templates );
    90         $this->assertSame( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
     269        $this->assertEquals( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
    91270
    92271        // Filter by CPT ID.
    93272        $templates    = get_block_templates( array( 'wp_id' => self::$post->ID ), 'wp_template' );
    94273        $template_ids = get_template_ids( $templates );
    95         $this->assertSame( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
     274        $this->assertEquals( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
     275
     276        // Filter template part by area.
     277        // Requires a block theme.
     278        /*$templates    = get_block_templates( array( 'area' => WP_TEMPLATE_PART_AREA_HEADER ), 'wp_template_part' );
     279        $template_ids = get_template_ids( $templates );
     280        $this->assertEquals(
     281            array(
     282                get_stylesheet() . '//' . 'my_template_part',
     283                get_stylesheet() . '//' . 'header',
     284            ),
     285            $template_ids
     286        );
     287        */
     288    }
     289
     290    /**
     291     * Should flatten nested blocks
     292     */
     293    function test_flatten_blocks() {
     294        $content_template_part_inside_group = '<!-- wp:group --><!-- wp:template-part {"slug":"header"} /--><!-- /wp:group -->';
     295        $blocks                             = parse_blocks( $content_template_part_inside_group );
     296        $actual                             = _flatten_blocks( $blocks );
     297        $expected                           = array( $blocks[0], $blocks[0]['innerBlocks'][0] );
     298        $this->assertEquals( $expected, $actual );
     299
     300        $content_template_part_inside_group_inside_group = '<!-- wp:group --><!-- wp:group --><!-- wp:template-part {"slug":"header"} /--><!-- /wp:group --><!-- /wp:group -->';
     301        $blocks   = parse_blocks( $content_template_part_inside_group_inside_group );
     302        $actual   = _flatten_blocks( $blocks );
     303        $expected = array( $blocks[0], $blocks[0]['innerBlocks'][0], $blocks[0]['innerBlocks'][0]['innerBlocks'][0] );
     304        $this->assertEquals( $expected, $actual );
     305
     306        $content_without_inner_blocks = '<!-- wp:group /-->';
     307        $blocks                       = parse_blocks( $content_without_inner_blocks );
     308        $actual                       = _flatten_blocks( $blocks );
     309        $expected                     = array( $blocks[0] );
     310        $this->assertEquals( $expected, $actual );
    96311    }
    97312}
Note: See TracChangeset for help on using the changeset viewer.