mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@52246 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
388b59319e
commit
90cdaabd2f
@ -0,0 +1,3 @@
|
||||
<!-- wp:paragraph -->
|
||||
<p>Page (ID 1) Template</p>
|
||||
<!-- /wp:paragraph -->
|
||||
@ -0,0 +1,3 @@
|
||||
<?php
|
||||
|
||||
echo 'PHP template for page with slug "home"';
|
||||
@ -0,0 +1,3 @@
|
||||
<!-- wp:paragraph -->
|
||||
<p>Index Template</p>
|
||||
<!-- /wp:paragraph -->
|
||||
@ -0,0 +1,3 @@
|
||||
<!-- wp:paragraph -->
|
||||
<p>Page (Home) Template</p>
|
||||
<!-- /wp:paragraph -->
|
||||
@ -0,0 +1,3 @@
|
||||
<!-- wp:paragraph -->
|
||||
<p>Page Template</p>
|
||||
<!-- /wp:paragraph -->
|
||||
3
tests/phpunit/data/themedir1/block-theme/page-1.php
Normal file
3
tests/phpunit/data/themedir1/block-theme/page-1.php
Normal file
@ -0,0 +1,3 @@
|
||||
<?php
|
||||
|
||||
echo 'PHP template for page with ID 1';
|
||||
@ -7,6 +7,8 @@
|
||||
|
||||
/**
|
||||
* Tests for the Block Templates abstraction layer.
|
||||
*
|
||||
* @group block-templates
|
||||
*/
|
||||
class Block_Template_Utils_Test extends WP_UnitTestCase {
|
||||
private static $post;
|
||||
|
||||
@ -7,37 +7,128 @@
|
||||
|
||||
/**
|
||||
* Tests for the block template loading algorithm.
|
||||
*
|
||||
* @group block-templates
|
||||
*/
|
||||
class Block_Template_Test extends WP_UnitTestCase {
|
||||
private static $post;
|
||||
|
||||
private static $template_canvas_path = ABSPATH . WPINC . '/template-canvas.php';
|
||||
|
||||
public static function wpSetUpBeforeClass() {
|
||||
// Set up custom template post.
|
||||
$args = array(
|
||||
'post_type' => 'wp_template',
|
||||
'post_name' => 'wp-custom-template-my-block-template',
|
||||
'post_title' => 'My Custom Block Template',
|
||||
'post_content' => 'Content',
|
||||
'post_excerpt' => 'Description of my block template',
|
||||
'tax_input' => array(
|
||||
'wp_theme' => array(
|
||||
get_stylesheet(),
|
||||
),
|
||||
),
|
||||
);
|
||||
self::$post = self::factory()->post->create_and_get( $args );
|
||||
wp_set_post_terms( self::$post->ID, get_stylesheet(), 'wp_theme' );
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
wp_delete_post( self::$post->ID );
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
switch_theme( 'block-theme' );
|
||||
}
|
||||
|
||||
public function tear_down() {
|
||||
global $_wp_current_template_content;
|
||||
unset( $_wp_current_template_content );
|
||||
|
||||
parent::tear_down();
|
||||
}
|
||||
|
||||
function test_page_home_block_template_takes_precedence_over_less_specific_block_templates() {
|
||||
global $_wp_current_template_content;
|
||||
$type = 'page';
|
||||
$templates = array(
|
||||
'page-home.php',
|
||||
'page-1.php',
|
||||
'page.php',
|
||||
);
|
||||
$resolved_template_path = locate_block_template( get_stylesheet_directory() . '/page-home.php', $type, $templates );
|
||||
$this->assertEquals( self::$template_canvas_path, $resolved_template_path );
|
||||
$this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/page-home.html', $_wp_current_template_content );
|
||||
}
|
||||
|
||||
function test_page_block_template_takes_precedence() {
|
||||
global $_wp_current_template_content;
|
||||
$type = 'page';
|
||||
$templates = array(
|
||||
'page-slug-doesnt-exist.php',
|
||||
'page-1.php',
|
||||
'page.php',
|
||||
);
|
||||
$resolved_template_path = locate_block_template( get_stylesheet_directory() . '/page.php', $type, $templates );
|
||||
$this->assertEquals( self::$template_canvas_path, $resolved_template_path );
|
||||
$this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/page.html', $_wp_current_template_content );
|
||||
}
|
||||
|
||||
function test_block_template_takes_precedence_over_equally_specific_php_template() {
|
||||
global $_wp_current_template_content;
|
||||
$type = 'index';
|
||||
$templates = array(
|
||||
'index.php',
|
||||
);
|
||||
$resolved_template_path = locate_block_template( get_stylesheet_directory() . '/index.php', $type, $templates );
|
||||
$this->assertEquals( self::$template_canvas_path, $resolved_template_path );
|
||||
$this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/index.html', $_wp_current_template_content );
|
||||
}
|
||||
|
||||
/**
|
||||
* In a hybrid theme, a PHP template of higher specificity will take precedence over a block template
|
||||
* with lower specificity.
|
||||
*
|
||||
* Covers https://github.com/WordPress/gutenberg/pull/29026.
|
||||
*/
|
||||
function test_more_specific_php_template_takes_precedence_over_less_specific_block_template() {
|
||||
$page_id_template = 'page-1.php';
|
||||
$page_id_template_path = get_stylesheet_directory() . '/' . $page_id_template;
|
||||
$type = 'page';
|
||||
$templates = array(
|
||||
'page-slug-doesnt-exist.php',
|
||||
'page-1.php',
|
||||
'page.php',
|
||||
);
|
||||
$resolved_template_path = locate_block_template( $page_id_template_path, $type, $templates );
|
||||
$this->assertEquals( $page_id_template_path, $resolved_template_path );
|
||||
}
|
||||
|
||||
/**
|
||||
* If a theme is a child of a block-based parent theme but has php templates for some of its pages,
|
||||
* a php template of the child will take precedence over the parent's block template if they have
|
||||
* otherwise equal specificity.
|
||||
*
|
||||
* Covers https://github.com/WordPress/gutenberg/pull/31123.
|
||||
*
|
||||
*/
|
||||
function test_child_theme_php_template_takes_precedence_over_equally_specific_parent_theme_block_template() {
|
||||
/**
|
||||
* @todo This test is currently marked as skipped, since it wouldn't pass. Turns out that in Gutenberg,
|
||||
* it only passed due to a erroneous test setup.
|
||||
* For details, see https://github.com/WordPress/wordpress-develop/pull/1920#issuecomment-975929818.
|
||||
*/
|
||||
$this->markTestSkipped( 'The block template resolution algorithm needs fixing in order for this test to pass.' );
|
||||
|
||||
switch_theme( 'block-theme-child' );
|
||||
|
||||
$page_slug_template = 'page-home.php';
|
||||
$page_slug_template_path = get_stylesheet_directory() . '/' . $page_slug_template;
|
||||
$type = 'page';
|
||||
$templates = array(
|
||||
'page-home.php',
|
||||
'page-1.php',
|
||||
'page.php',
|
||||
);
|
||||
$resolved_template_path = locate_block_template( $page_slug_template_path, $type, $templates );
|
||||
$this->assertEquals( $page_slug_template_path, $resolved_template_path );
|
||||
}
|
||||
|
||||
function test_child_theme_block_template_takes_precedence_over_equally_specific_parent_theme_php_template() {
|
||||
global $_wp_current_template_content;
|
||||
|
||||
switch_theme( 'block-theme-child' );
|
||||
|
||||
$page_template = 'page-1.php';
|
||||
$parent_theme_page_template_path = get_template_directory() . '/' . $page_template;
|
||||
$type = 'page';
|
||||
$templates = array(
|
||||
'page-slug-doesnt-exist.php',
|
||||
'page-1.php',
|
||||
'page.php',
|
||||
);
|
||||
$resolved_template_path = locate_block_template( $parent_theme_page_template_path, $type, $templates );
|
||||
$this->assertEquals( self::$template_canvas_path, $resolved_template_path );
|
||||
$this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/page-1.html', $_wp_current_template_content );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -63,6 +154,22 @@ class Block_Template_Test extends WP_UnitTestCase {
|
||||
public function test_custom_page_block_template_takes_precedence_over_all_other_templates() {
|
||||
global $_wp_current_template_content;
|
||||
|
||||
// Set up custom template post.
|
||||
$args = array(
|
||||
'post_type' => 'wp_template',
|
||||
'post_name' => 'wp-custom-template-my-block-template',
|
||||
'post_title' => 'My Custom Block Template',
|
||||
'post_content' => 'Content',
|
||||
'post_excerpt' => 'Description of my block template',
|
||||
'tax_input' => array(
|
||||
'wp_theme' => array(
|
||||
get_stylesheet(),
|
||||
),
|
||||
),
|
||||
);
|
||||
$post = self::factory()->post->create_and_get( $args );
|
||||
wp_set_post_terms( $post->ID, get_stylesheet(), 'wp_theme' );
|
||||
|
||||
$custom_page_block_template = 'wp-custom-template-my-block-template';
|
||||
$page_template_path = get_stylesheet_directory() . '/' . 'page.php';
|
||||
$type = 'page';
|
||||
@ -74,7 +181,9 @@ class Block_Template_Test extends WP_UnitTestCase {
|
||||
);
|
||||
$resolved_template_path = locate_block_template( $page_template_path, $type, $templates );
|
||||
$this->assertSame( self::$template_canvas_path, $resolved_template_path );
|
||||
$this->assertSame( self::$post->post_content, $_wp_current_template_content );
|
||||
$this->assertSame( $post->post_content, $_wp_current_template_content );
|
||||
|
||||
wp_delete_post( $post->ID );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
Reference in New Issue
Block a user