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:
Sergey Biryukov
2022-09-18 13:08:33 +00:00
parent b2a2270ae7
commit 216a9ef513
2 changed files with 131 additions and 136 deletions

View File

@@ -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
*/