mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
Tests: Move the basic get_block_templates() test to the dedicated file.
Now that the function has its own test class, the remaining test from `Tests_Block_Template_Utils` can be moved to `Tests_Blocks_GetBlockTemplates` for consistency. Includes: * Uncommenting some assertions previously commented out. * Moving the `get_template_ids()` helper method to the top of the class. * Standardizing on `wpSetUpBeforeClass()`/`wpTearDownAfterClass()` in both classes. * Declaring the test theme name as a constant in both classes, since the value is not changed by any of the tests. * Renaming some properties in both classes for clarity. Follow-up to [51003], [52062], [53927], [54184], [54187]. See #55652. git-svn-id: https://develop.svn.wordpress.org/trunk@54198 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
b2a2270ae7
commit
216a9ef513
@ -11,78 +11,88 @@
|
||||
* @group block-templates
|
||||
*/
|
||||
class Tests_Block_Template_Utils extends WP_UnitTestCase {
|
||||
private static $post;
|
||||
private static $template_part_post;
|
||||
private static $test_theme = 'block-theme';
|
||||
|
||||
public static function wpSetUpBeforeClass() {
|
||||
// Set up a template post corresponding to a different theme.
|
||||
// We do this to ensure resolution and slug creation works as expected,
|
||||
// even with another post of that same name present for another theme.
|
||||
$args = array(
|
||||
'post_type' => 'wp_template',
|
||||
'post_name' => 'my_template',
|
||||
'post_title' => 'My Template',
|
||||
'post_content' => 'Content',
|
||||
'post_excerpt' => 'Description of my template',
|
||||
'tax_input' => array(
|
||||
'wp_theme' => array(
|
||||
'this-theme-should-not-resolve',
|
||||
const TEST_THEME = 'block-theme';
|
||||
|
||||
private static $template_post;
|
||||
private static $template_part_post;
|
||||
|
||||
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
|
||||
/*
|
||||
* Set up a template post corresponding to a different theme.
|
||||
* We do this to ensure resolution and slug creation works as expected,
|
||||
* even with another post of that same name present for another theme.
|
||||
*/
|
||||
self::$template_post = $factory->post->create_and_get(
|
||||
array(
|
||||
'post_type' => 'wp_template',
|
||||
'post_name' => 'my_template',
|
||||
'post_title' => 'My Template',
|
||||
'post_content' => 'Content',
|
||||
'post_excerpt' => 'Description of my template',
|
||||
'tax_input' => array(
|
||||
'wp_theme' => array(
|
||||
'this-theme-should-not-resolve',
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
self::$post = self::factory()->post->create_and_get( $args );
|
||||
wp_set_post_terms( self::$post->ID, 'this-theme-should-not-resolve', 'wp_theme' );
|
||||
|
||||
wp_set_post_terms( self::$template_post->ID, 'this-theme-should-not-resolve', 'wp_theme' );
|
||||
|
||||
// Set up template post.
|
||||
$args = array(
|
||||
'post_type' => 'wp_template',
|
||||
'post_name' => 'my_template',
|
||||
'post_title' => 'My Template',
|
||||
'post_content' => 'Content',
|
||||
'post_excerpt' => 'Description of my template',
|
||||
'tax_input' => array(
|
||||
'wp_theme' => array(
|
||||
self::$test_theme,
|
||||
self::$template_post = $factory->post->create_and_get(
|
||||
array(
|
||||
'post_type' => 'wp_template',
|
||||
'post_name' => 'my_template',
|
||||
'post_title' => 'My Template',
|
||||
'post_content' => 'Content',
|
||||
'post_excerpt' => 'Description of my template',
|
||||
'tax_input' => array(
|
||||
'wp_theme' => array(
|
||||
self::TEST_THEME,
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
self::$post = self::factory()->post->create_and_get( $args );
|
||||
wp_set_post_terms( self::$post->ID, self::$test_theme, 'wp_theme' );
|
||||
|
||||
wp_set_post_terms( self::$template_post->ID, self::TEST_THEME, 'wp_theme' );
|
||||
|
||||
// Set up template part post.
|
||||
$template_part_args = array(
|
||||
'post_type' => 'wp_template_part',
|
||||
'post_name' => 'my_template_part',
|
||||
'post_title' => 'My Template Part',
|
||||
'post_content' => 'Content',
|
||||
'post_excerpt' => 'Description of my template part',
|
||||
'tax_input' => array(
|
||||
'wp_theme' => array(
|
||||
self::$test_theme,
|
||||
self::$template_part_post = $factory->post->create_and_get(
|
||||
array(
|
||||
'post_type' => 'wp_template_part',
|
||||
'post_name' => 'my_template_part',
|
||||
'post_title' => 'My Template Part',
|
||||
'post_content' => 'Content',
|
||||
'post_excerpt' => 'Description of my template part',
|
||||
'tax_input' => array(
|
||||
'wp_theme' => array(
|
||||
self::TEST_THEME,
|
||||
),
|
||||
'wp_template_part_area' => array(
|
||||
WP_TEMPLATE_PART_AREA_HEADER,
|
||||
),
|
||||
),
|
||||
'wp_template_part_area' => array(
|
||||
WP_TEMPLATE_PART_AREA_HEADER,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
self::$template_part_post = self::factory()->post->create_and_get( $template_part_args );
|
||||
|
||||
wp_set_post_terms( self::$template_part_post->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' );
|
||||
wp_set_post_terms( self::$template_part_post->ID, self::$test_theme, 'wp_theme' );
|
||||
wp_set_post_terms( self::$template_part_post->ID, self::TEST_THEME, 'wp_theme' );
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
wp_delete_post( self::$template_post->ID );
|
||||
}
|
||||
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
switch_theme( self::$test_theme );
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
wp_delete_post( self::$post->ID );
|
||||
switch_theme( self::TEST_THEME );
|
||||
}
|
||||
|
||||
public function test_build_block_template_result_from_post() {
|
||||
$template = _build_block_template_result_from_post(
|
||||
self::$post,
|
||||
self::$template_post,
|
||||
'wp_template'
|
||||
);
|
||||
|
||||
@ -274,53 +284,6 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase {
|
||||
$this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template->area );
|
||||
}
|
||||
|
||||
/**
|
||||
* Should retrieve block templates (file and CPT)
|
||||
*/
|
||||
public function test_get_block_templates() {
|
||||
function get_template_ids( $templates ) {
|
||||
return array_map(
|
||||
static function( $template ) {
|
||||
return $template->id;
|
||||
},
|
||||
$templates
|
||||
);
|
||||
}
|
||||
|
||||
// All results.
|
||||
$templates = get_block_templates( array(), 'wp_template' );
|
||||
$template_ids = get_template_ids( $templates );
|
||||
|
||||
// Avoid testing the entire array because the theme might add/remove templates.
|
||||
$this->assertContains( get_stylesheet() . '//' . 'my_template', $template_ids );
|
||||
|
||||
// The result might change in a block theme.
|
||||
// $this->assertContains( get_stylesheet() . '//' . 'index', $template_ids );
|
||||
|
||||
// Filter by slug.
|
||||
$templates = get_block_templates( array( 'slug__in' => array( 'my_template' ) ), 'wp_template' );
|
||||
$template_ids = get_template_ids( $templates );
|
||||
$this->assertSame( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
|
||||
|
||||
// Filter by CPT ID.
|
||||
$templates = get_block_templates( array( 'wp_id' => self::$post->ID ), 'wp_template' );
|
||||
$template_ids = get_template_ids( $templates );
|
||||
$this->assertSame( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
|
||||
|
||||
// Filter template part by area.
|
||||
// Requires a block theme.
|
||||
/*$templates = get_block_templates( array( 'area' => WP_TEMPLATE_PART_AREA_HEADER ), 'wp_template_part' );
|
||||
$template_ids = get_template_ids( $templates );
|
||||
$this->assertSame(
|
||||
array(
|
||||
get_stylesheet() . '//' . 'my_template_part',
|
||||
get_stylesheet() . '//' . 'header',
|
||||
),
|
||||
$template_ids
|
||||
);
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Should flatten nested blocks
|
||||
*/
|
||||
|
||||
@ -11,7 +11,7 @@ class Tests_Blocks_GetBlockTemplates extends WP_UnitTestCase {
|
||||
/**
|
||||
* @var WP_Post
|
||||
*/
|
||||
private static $template;
|
||||
private static $index_template;
|
||||
|
||||
/**
|
||||
* @var WP_Post
|
||||
@ -21,30 +21,28 @@ class Tests_Blocks_GetBlockTemplates extends WP_UnitTestCase {
|
||||
/**
|
||||
* @var WP_Post
|
||||
*/
|
||||
private static $template_part;
|
||||
|
||||
public static function set_up_before_class() {
|
||||
parent::set_up_before_class();
|
||||
private static $small_header_template_part;
|
||||
|
||||
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
|
||||
/*
|
||||
* This template has to have the same ID ("block-theme/index") as the template
|
||||
* that is shipped with the "block-theme" theme. This is needed for testing purposes.
|
||||
*/
|
||||
static::$template = self::factory()->post->create_and_get(
|
||||
self::$index_template = $factory->post->create_and_get(
|
||||
array(
|
||||
'post_type' => 'wp_template',
|
||||
'post_name' => 'index',
|
||||
'tax_input' => array(
|
||||
'wp_theme' => array(
|
||||
static::TEST_THEME,
|
||||
self::TEST_THEME,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
wp_set_post_terms( static::$template->ID, static::TEST_THEME, 'wp_theme' );
|
||||
wp_set_post_terms( self::$index_template->ID, self::TEST_THEME, 'wp_theme' );
|
||||
|
||||
static::$custom_single_post_template = self::factory()->post->create_and_get(
|
||||
self::$custom_single_post_template = $factory->post->create_and_get(
|
||||
array(
|
||||
'post_type' => 'wp_template',
|
||||
'post_name' => 'custom-single-post-template',
|
||||
@ -53,25 +51,25 @@ class Tests_Blocks_GetBlockTemplates extends WP_UnitTestCase {
|
||||
'post_excerpt' => 'Description of custom single post template',
|
||||
'tax_input' => array(
|
||||
'wp_theme' => array(
|
||||
static::TEST_THEME,
|
||||
self::TEST_THEME,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
wp_set_post_terms( static::$custom_single_post_template->ID, static::TEST_THEME, 'wp_theme' );
|
||||
wp_set_post_terms( self::$custom_single_post_template->ID, self::TEST_THEME, 'wp_theme' );
|
||||
|
||||
/*
|
||||
* This template part has to have the same ID ("block-theme/small-header") as the template part
|
||||
* that is shipped with the "block-theme" theme. This is needed for testing purposes.
|
||||
*/
|
||||
self::$template_part = self::factory()->post->create_and_get(
|
||||
self::$small_header_template_part = $factory->post->create_and_get(
|
||||
array(
|
||||
'post_type' => 'wp_template_part',
|
||||
'post_name' => 'small-header',
|
||||
'tax_input' => array(
|
||||
'wp_theme' => array(
|
||||
static::TEST_THEME,
|
||||
self::TEST_THEME,
|
||||
),
|
||||
'wp_template_part_area' => array(
|
||||
WP_TEMPLATE_PART_AREA_HEADER,
|
||||
@ -80,21 +78,70 @@ class Tests_Blocks_GetBlockTemplates extends WP_UnitTestCase {
|
||||
)
|
||||
);
|
||||
|
||||
wp_set_post_terms( self::$template_part->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' );
|
||||
wp_set_post_terms( self::$template_part->ID, static::TEST_THEME, 'wp_theme' );
|
||||
wp_set_post_terms( self::$small_header_template_part->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' );
|
||||
wp_set_post_terms( self::$small_header_template_part->ID, self::TEST_THEME, 'wp_theme' );
|
||||
}
|
||||
|
||||
public static function tear_down_after_class() {
|
||||
wp_delete_post( static::$template->ID );
|
||||
wp_delete_post( static::$custom_single_post_template->ID );
|
||||
wp_delete_post( static::$template_part->ID );
|
||||
|
||||
parent::tear_down_after_class();
|
||||
public static function wpTearDownAfterClass() {
|
||||
wp_delete_post( self::$index_template->ID );
|
||||
wp_delete_post( self::$custom_single_post_template->ID );
|
||||
wp_delete_post( self::$small_header_template_part->ID );
|
||||
}
|
||||
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
switch_theme( static::TEST_THEME );
|
||||
switch_theme( self::TEST_THEME );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the template IDs from the given array.
|
||||
*
|
||||
* @param object[] $templates Array of template objects to parse.
|
||||
* @return string[] The template IDs.
|
||||
*/
|
||||
private function get_template_ids( $templates ) {
|
||||
return array_map(
|
||||
static function( $template ) {
|
||||
return $template->id;
|
||||
},
|
||||
$templates
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should retrieve block templates (file and CPT)
|
||||
*/
|
||||
public function test_get_block_templates() {
|
||||
// All results.
|
||||
$templates = get_block_templates( array(), 'wp_template' );
|
||||
$template_ids = $this->get_template_ids( $templates );
|
||||
|
||||
// Avoid testing the entire array because the theme might add/remove templates.
|
||||
$this->assertContains( get_stylesheet() . '//' . 'custom-single-post-template', $template_ids );
|
||||
|
||||
// The result might change in a block theme.
|
||||
$this->assertContains( get_stylesheet() . '//' . 'index', $template_ids );
|
||||
|
||||
// Filter by slug.
|
||||
$templates = get_block_templates( array( 'slug__in' => array( 'custom-single-post-template' ) ), 'wp_template' );
|
||||
$template_ids = $this->get_template_ids( $templates );
|
||||
$this->assertSame( array( get_stylesheet() . '//' . 'custom-single-post-template' ), $template_ids );
|
||||
|
||||
// Filter by CPT ID.
|
||||
$templates = get_block_templates( array( 'wp_id' => self::$custom_single_post_template->ID ), 'wp_template' );
|
||||
$template_ids = $this->get_template_ids( $templates );
|
||||
$this->assertSame( array( get_stylesheet() . '//' . 'custom-single-post-template' ), $template_ids );
|
||||
|
||||
// Filter template part by area.
|
||||
// Requires a block theme.
|
||||
$templates = get_block_templates( array( 'area' => WP_TEMPLATE_PART_AREA_HEADER ), 'wp_template_part' );
|
||||
$template_ids = $this->get_template_ids( $templates );
|
||||
$this->assertSame(
|
||||
array(
|
||||
get_stylesheet() . '//' . 'small-header',
|
||||
),
|
||||
$template_ids
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -174,19 +221,4 @@ class Tests_Blocks_GetBlockTemplates extends WP_UnitTestCase {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the template IDs from the given array.
|
||||
*
|
||||
* @param object[] $templates Array of template objects to parse.
|
||||
* @return string[] The template IDs.
|
||||
*/
|
||||
private function get_template_ids( $templates ) {
|
||||
return array_map(
|
||||
static function( $template ) {
|
||||
return $template->id;
|
||||
},
|
||||
$templates
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user