From dabab3f6367e093b139739a9f15fef610fac542b Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 16 Sep 2022 15:10:58 +0000 Subject: [PATCH] Tests: Update the test for respecting the post type in `get_block_templates()`. * Use `assertSameSets()` to avoid a failure when array items are returned in different order ([https://github.com/WordPress/wordpress-develop/actions/runs/3067320461/jobs/4953478602#step:19:274 example]). * Move the test to a more appropriate place now that the function has its own test class. * Rename the test method to match the function name. Follow-up to [52062], [53927], [54184]. See #55881. git-svn-id: https://develop.svn.wordpress.org/trunk@54187 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/block-template-utils.php | 74 ------------------ .../tests/blocks/getBlockTemplates.php | 76 +++++++++++++++++++ 2 files changed, 76 insertions(+), 74 deletions(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index abb40d6314..319e54ac29 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -12,7 +12,6 @@ */ class Tests_Block_Template_Utils extends WP_UnitTestCase { private static $post; - private static $custom_single_post_template_post; private static $template_part_post; private static $test_theme = 'block-theme'; @@ -51,22 +50,6 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase { self::$post = self::factory()->post->create_and_get( $args ); wp_set_post_terms( self::$post->ID, self::$test_theme, 'wp_theme' ); - // Set up template post. - $args = array( - 'post_type' => 'wp_template', - 'post_name' => 'custom-single-post-template', - 'post_title' => 'Custom Single Post template (modified)', - 'post_content' => 'Content', - 'post_excerpt' => 'Description of custom single post template', - 'tax_input' => array( - 'wp_theme' => array( - self::$test_theme, - ), - ), - ); - self::$custom_single_post_template_post = self::factory()->post->create_and_get( $args ); - wp_set_post_terms( self::$custom_single_post_template_post->ID, self::$test_theme, 'wp_theme' ); - // Set up template part post. $template_part_args = array( 'post_type' => 'wp_template_part', @@ -95,7 +78,6 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase { public static function wpTearDownAfterClass() { wp_delete_post( self::$post->ID ); - wp_delete_post( self::$custom_single_post_template_post->ID ); } public function test_build_block_template_result_from_post() { @@ -339,62 +321,6 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase { */ } - /** - * @dataProvider data_get_block_template_should_respect_posttypes_property - * @ticket 55881 - * @covers ::get_block_templates - * - * @param string $post_type Post type for query. - * @param array $expected Expected template IDs. - */ - public function test_get_block_template_should_respect_posttypes_property( $post_type, $expected ) { - $templates = get_block_templates( array( 'post_type' => $post_type ) ); - - $this->assertSame( - $expected, - $this->get_template_ids( $templates ) - ); - } - - /** - * Data provider. - * - * @return array - */ - public function data_get_block_template_should_respect_posttypes_property() { - return array( - 'post' => array( - 'post_type' => 'post', - 'expected' => array( - 'block-theme//my_template', - 'block-theme//custom-single-post-template', - ), - ), - 'page' => array( - 'post_type' => 'page', - 'expected' => array( - 'block-theme//my_template', - 'block-theme//page-home', - ), - ), - ); - } - - /** - * 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 flatten nested blocks */ diff --git a/tests/phpunit/tests/blocks/getBlockTemplates.php b/tests/phpunit/tests/blocks/getBlockTemplates.php index 89754f12ef..a2c820e2e5 100644 --- a/tests/phpunit/tests/blocks/getBlockTemplates.php +++ b/tests/phpunit/tests/blocks/getBlockTemplates.php @@ -13,6 +13,11 @@ class Tests_Blocks_GetBlockTemplates extends WP_UnitTestCase { */ private static $template; + /** + * @var WP_Post + */ + private static $custom_single_post_template; + /** * @var WP_Post */ @@ -39,6 +44,23 @@ class Tests_Blocks_GetBlockTemplates extends WP_UnitTestCase { wp_set_post_terms( static::$template->ID, static::TEST_THEME, 'wp_theme' ); + static::$custom_single_post_template = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_template', + 'post_name' => 'custom-single-post-template', + 'post_title' => 'Custom Single Post template (modified)', + 'post_content' => 'Content', + 'post_excerpt' => 'Description of custom single post template', + 'tax_input' => array( + 'wp_theme' => array( + static::TEST_THEME, + ), + ), + ) + ); + + wp_set_post_terms( static::$custom_single_post_template->ID, static::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. @@ -64,6 +86,7 @@ class Tests_Blocks_GetBlockTemplates extends WP_UnitTestCase { 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(); @@ -113,4 +136,57 @@ class Tests_Blocks_GetBlockTemplates extends WP_UnitTestCase { ), ); } + + /** + * @dataProvider data_get_block_templates_should_respect_posttypes_property + * @ticket 55881 + * + * @param string $post_type Post type for query. + * @param array $expected Expected template IDs. + */ + public function test_get_block_templates_should_respect_posttypes_property( $post_type, $expected ) { + $templates = get_block_templates( array( 'post_type' => $post_type ) ); + + $this->assertSameSets( + $expected, + $this->get_template_ids( $templates ) + ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_get_block_templates_should_respect_posttypes_property() { + return array( + 'post' => array( + 'post_type' => 'post', + 'expected' => array( + 'block-theme//custom-single-post-template', + ), + ), + 'page' => array( + 'post_type' => 'page', + 'expected' => array( + 'block-theme//page-home', + ), + ), + ); + } + + /** + * 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 + ); + } }