-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Templates API: Fix template files query by post-type (#61244)
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: creativecoder <grantmkin@git.wordpress.org> Co-authored-by: mike-henderson <mikehend@git.wordpress.org> Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
- Loading branch information
1 parent
e881a47
commit f9e725b
Showing
7 changed files
with
399 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
/** | ||
* @group blocks | ||
* @group block-templates | ||
* | ||
* @covers ::get_block_templates | ||
*/ | ||
class Tests_Blocks_GetBlockTemplates extends WP_UnitTestCase { | ||
|
||
const TEST_THEME = 'block-theme'; | ||
|
||
/** | ||
* Theme root directory. | ||
* | ||
* @var string | ||
*/ | ||
private $theme_root; | ||
|
||
/** | ||
* Original theme directory. | ||
* | ||
* @var string | ||
*/ | ||
private $orig_theme_dir; | ||
|
||
public function set_up() { | ||
parent::set_up(); | ||
$this->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', | ||
), | ||
), | ||
); | ||
} | ||
} |
Oops, something went wrong.
f9e725b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in f9e725b.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/8908759823
📝 Reported issues:
/test/e2e/specs/editor/blocks/image.spec.js