WordPress.org

Make WordPress Core

Changeset 52246


Ignore:
Timestamp:
11/25/2021 09:20:03 AM (9 days ago)
Author:
youknowriad
Message:

Themes: Add block template resolution algorithm unit tests.

Backports the unit tests for block templates
and hybrid themes template resolution from the Gutenberg plugin.

Props bernhard-reiter, jorgefilipecosta.
Fixes #54478.

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

Legend:

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

    r52069 r52246  
    88/**
    99 * Tests for the Block Templates abstraction layer.
     10 *
     11 * @group block-templates
    1012 */
    1113class Block_Template_Utils_Test extends WP_UnitTestCase {
  • trunk/tests/phpunit/tests/block-template.php

    r52010 r52246  
    88/**
    99 * Tests for the block template loading algorithm.
     10 *
     11 * @group block-templates
    1012 */
    1113class Block_Template_Test extends WP_UnitTestCase {
     
    1416    private static $template_canvas_path = ABSPATH . WPINC . '/template-canvas.php';
    1517
    16     public static function wpSetUpBeforeClass() {
    17         // Set up custom template post.
    18         $args       = array(
    19             'post_type'    => 'wp_template',
    20             'post_name'    => 'wp-custom-template-my-block-template',
    21             'post_title'   => 'My Custom Block Template',
    22             'post_content' => 'Content',
    23             'post_excerpt' => 'Description of my block template',
    24             'tax_input'    => array(
    25                 'wp_theme' => array(
    26                     get_stylesheet(),
    27                 ),
    28             ),
    29         );
    30         self::$post = self::factory()->post->create_and_get( $args );
    31         wp_set_post_terms( self::$post->ID, get_stylesheet(), 'wp_theme' );
    32     }
    33 
    34     public static function wpTearDownAfterClass() {
    35         wp_delete_post( self::$post->ID );
     18    public function set_up() {
     19        parent::set_up();
     20        switch_theme( 'block-theme' );
    3621    }
    3722
     
    3924        global $_wp_current_template_content;
    4025        unset( $_wp_current_template_content );
     26
     27        parent::tear_down();
     28    }
     29
     30    function test_page_home_block_template_takes_precedence_over_less_specific_block_templates() {
     31        global $_wp_current_template_content;
     32        $type                   = 'page';
     33        $templates              = array(
     34            'page-home.php',
     35            'page-1.php',
     36            'page.php',
     37        );
     38        $resolved_template_path = locate_block_template( get_stylesheet_directory() . '/page-home.php', $type, $templates );
     39        $this->assertEquals( self::$template_canvas_path, $resolved_template_path );
     40        $this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/page-home.html', $_wp_current_template_content );
     41    }
     42
     43    function test_page_block_template_takes_precedence() {
     44        global $_wp_current_template_content;
     45        $type                   = 'page';
     46        $templates              = array(
     47            'page-slug-doesnt-exist.php',
     48            'page-1.php',
     49            'page.php',
     50        );
     51        $resolved_template_path = locate_block_template( get_stylesheet_directory() . '/page.php', $type, $templates );
     52        $this->assertEquals( self::$template_canvas_path, $resolved_template_path );
     53        $this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/page.html', $_wp_current_template_content );
     54    }
     55
     56    function test_block_template_takes_precedence_over_equally_specific_php_template() {
     57        global $_wp_current_template_content;
     58        $type                   = 'index';
     59        $templates              = array(
     60            'index.php',
     61        );
     62        $resolved_template_path = locate_block_template( get_stylesheet_directory() . '/index.php', $type, $templates );
     63        $this->assertEquals( self::$template_canvas_path, $resolved_template_path );
     64        $this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/index.html', $_wp_current_template_content );
     65    }
     66
     67    /**
     68     * In a hybrid theme, a PHP template of higher specificity will take precedence over a block template
     69     * with lower specificity.
     70     *
     71     * Covers https://github.com/WordPress/gutenberg/pull/29026.
     72     */
     73    function test_more_specific_php_template_takes_precedence_over_less_specific_block_template() {
     74        $page_id_template       = 'page-1.php';
     75        $page_id_template_path  = get_stylesheet_directory() . '/' . $page_id_template;
     76        $type                   = 'page';
     77        $templates              = array(
     78            'page-slug-doesnt-exist.php',
     79            'page-1.php',
     80            'page.php',
     81        );
     82        $resolved_template_path = locate_block_template( $page_id_template_path, $type, $templates );
     83        $this->assertEquals( $page_id_template_path, $resolved_template_path );
     84    }
     85
     86    /**
     87     * If a theme is a child of a block-based parent theme but has php templates for some of its pages,
     88     * a php template of the child will take precedence over the parent's block template if they have
     89     * otherwise equal specificity.
     90     *
     91     * Covers https://github.com/WordPress/gutenberg/pull/31123.
     92     *
     93     */
     94    function test_child_theme_php_template_takes_precedence_over_equally_specific_parent_theme_block_template() {
     95        /**
     96         * @todo This test is currently marked as skipped, since it wouldn't pass. Turns out that in Gutenberg,
     97         * it only passed due to a erroneous test setup.
     98         * For details, see https://github.com/WordPress/wordpress-develop/pull/1920#issuecomment-975929818.
     99         */
     100        $this->markTestSkipped( 'The block template resolution algorithm needs fixing in order for this test to pass.' );
     101
     102        switch_theme( 'block-theme-child' );
     103
     104        $page_slug_template      = 'page-home.php';
     105        $page_slug_template_path = get_stylesheet_directory() . '/' . $page_slug_template;
     106        $type                    = 'page';
     107        $templates               = array(
     108            'page-home.php',
     109            'page-1.php',
     110            'page.php',
     111        );
     112        $resolved_template_path  = locate_block_template( $page_slug_template_path, $type, $templates );
     113        $this->assertEquals( $page_slug_template_path, $resolved_template_path );
     114    }
     115
     116    function test_child_theme_block_template_takes_precedence_over_equally_specific_parent_theme_php_template() {
     117        global $_wp_current_template_content;
     118
     119        switch_theme( 'block-theme-child' );
     120
     121        $page_template                   = 'page-1.php';
     122        $parent_theme_page_template_path = get_template_directory() . '/' . $page_template;
     123        $type                            = 'page';
     124        $templates                       = array(
     125            'page-slug-doesnt-exist.php',
     126            'page-1.php',
     127            'page.php',
     128        );
     129        $resolved_template_path          = locate_block_template( $parent_theme_page_template_path, $type, $templates );
     130        $this->assertEquals( self::$template_canvas_path, $resolved_template_path );
     131        $this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/page-1.html', $_wp_current_template_content );
    41132    }
    42133
     
    64155        global $_wp_current_template_content;
    65156
     157        // Set up custom template post.
     158        $args = array(
     159            'post_type'    => 'wp_template',
     160            'post_name'    => 'wp-custom-template-my-block-template',
     161            'post_title'   => 'My Custom Block Template',
     162            'post_content' => 'Content',
     163            'post_excerpt' => 'Description of my block template',
     164            'tax_input'    => array(
     165                'wp_theme' => array(
     166                    get_stylesheet(),
     167                ),
     168            ),
     169        );
     170        $post = self::factory()->post->create_and_get( $args );
     171        wp_set_post_terms( $post->ID, get_stylesheet(), 'wp_theme' );
     172
    66173        $custom_page_block_template = 'wp-custom-template-my-block-template';
    67174        $page_template_path         = get_stylesheet_directory() . '/' . 'page.php';
     
    75182        $resolved_template_path     = locate_block_template( $page_template_path, $type, $templates );
    76183        $this->assertSame( self::$template_canvas_path, $resolved_template_path );
    77         $this->assertSame( self::$post->post_content, $_wp_current_template_content );
     184        $this->assertSame( $post->post_content, $_wp_current_template_content );
     185
     186        wp_delete_post( $post->ID );
    78187    }
    79188
Note: See TracChangeset for help on using the changeset viewer.