diff --git a/phpunit/blocks/get-block-templates-test.php b/phpunit/blocks/get-block-templates-test.php new file mode 100644 index 00000000000000..76c48047a3e191 --- /dev/null +++ b/phpunit/blocks/get-block-templates-test.php @@ -0,0 +1,109 @@ +theme_root = realpath( __DIR__ . '/../data/themedir1' ); + $this->orig_theme_dir = $GLOBALS['wp_theme_directories']; + + // /themes is necessary as theme.php functions assume /themes is the root if there is only one root. + $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root ); + + add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); + add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); + add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); + // Clear caches. + wp_clean_themes_cache(); + unset( $GLOBALS['wp_themes'] ); + switch_theme( self::TEST_THEME ); + } + + public function tear_down() { + $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir; + wp_clean_themes_cache(); + unset( $GLOBALS['wp_themes'] ); + parent::tear_down(); + } + + public function filter_set_theme_root() { + return $this->theme_root; + } + + /** + * 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 + ); + } + + /** + * @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 = gutenberg_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() { + // @code-merge: This code will go into Core's tests/phpunit/tests/blocks/getBlockTemplates.php. + return array( + 'post' => array( + 'post_type' => 'post', + 'expected' => array( + 'block-theme//custom-hero-template', + 'block-theme//custom-single-post-template', + ), + ), + 'page' => array( + 'post_type' => 'page', + 'expected' => array( + 'block-theme//custom-hero-template', + 'block-theme//page-home', + ), + ), + ); + } +} diff --git a/phpunit/data/themedir1/block-theme/templates/custom-hero-template.html b/phpunit/data/themedir1/block-theme/templates/custom-hero-template.html new file mode 100644 index 00000000000000..0a62cda60641a1 --- /dev/null +++ b/phpunit/data/themedir1/block-theme/templates/custom-hero-template.html @@ -0,0 +1,3 @@ + +
Custom Hero template
+ diff --git a/phpunit/data/themedir1/block-theme/templates/custom-single-post-template.html b/phpunit/data/themedir1/block-theme/templates/custom-single-post-template.html new file mode 100644 index 00000000000000..fc0a5f8a4b154d --- /dev/null +++ b/phpunit/data/themedir1/block-theme/templates/custom-single-post-template.html @@ -0,0 +1,3 @@ + +Custom Single Post template
+ diff --git a/phpunit/data/themedir1/block-theme/templates/index.html b/phpunit/data/themedir1/block-theme/templates/index.html new file mode 100644 index 00000000000000..451c2c1a5c159b --- /dev/null +++ b/phpunit/data/themedir1/block-theme/templates/index.html @@ -0,0 +1,3 @@ + +Index Template
+ diff --git a/phpunit/data/themedir1/block-theme/templates/page-home.html b/phpunit/data/themedir1/block-theme/templates/page-home.html new file mode 100644 index 00000000000000..fd46fc0018fff4 --- /dev/null +++ b/phpunit/data/themedir1/block-theme/templates/page-home.html @@ -0,0 +1,3 @@ + +Page (Home) Template
+