-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Honor postTypes property in theme's theme.json when getting templates from post #3131
Honor postTypes property in theme's theme.json when getting templates from post #3131
Conversation
Hi @Aljullu! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in this blog post. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
Thanks a lot for your backport PR, @Aljullu! Could you maybe add a bit of test coverage for the updated behavior to We already have some coverage for |
916828f
to
551eeaf
Compare
Here's some basic unit test coverage: commit 4ea8873d670bfd785e51d5414938ae02b63d960c
Author: Bernie Reiter <ockham@raz.or.at>
Date: Thu Sep 8 15:44:59 2022 +0200
Add basic test coverage
diff --git a/tests/phpunit/data/themedir1/block-theme/templates/custom-single-post-template.html b/tests/phpunit/data/themedir1/block-theme/templates/custom-single-post-template.html
new file mode 100644
index 0000000000..fc0a5f8a4b
--- /dev/null
+++ b/tests/phpunit/data/themedir1/block-theme/templates/custom-single-post-template.html
@@ -0,0 +1,3 @@
+<!-- wp:paragraph -->
+<p>Custom Single Post template</p>
+<!-- /wp:paragraph -->
diff --git a/tests/phpunit/data/themedir1/block-theme/theme.json b/tests/phpunit/data/themedir1/block-theme/theme.json
index 38fcb1d9dd..b4fc0500cc 100644
--- a/tests/phpunit/data/themedir1/block-theme/theme.json
+++ b/tests/phpunit/data/themedir1/block-theme/theme.json
@@ -59,6 +59,11 @@
{
"name": "page-home",
"title": "Homepage template"
+ },
+ {
+ "name": "custom-single-post-template",
+ "title": "Custom Single Post template",
+ "postTypes": ["post"]
}
],
"templateParts": [
diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php
index 666edd020a..1c4adc66ff 100644
--- a/tests/phpunit/tests/block-template-utils.php
+++ b/tests/phpunit/tests/block-template-utils.php
@@ -319,6 +319,27 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase {
$template_ids
);
*/
+
+ // Filter by post type.
+ $templates = get_block_templates( array( 'post_type' => 'post' ), 'wp_template' );
+ $template_ids = get_template_ids( $templates );
+ $this->assertSame(
+ array(
+ get_stylesheet() . '//' . 'my_template',
+ get_stylesheet() . '//' . 'custom-single-post-template'
+ ),
+ $template_ids
+ );
+
+ $templates = get_block_templates( array( 'post_type' => 'page' ), 'wp_template' );
+ $template_ids = get_template_ids( $templates );
+ $this->assertSame(
+ array(
+ get_stylesheet() . '//' . 'my_template',
+ get_stylesheet() . '//' . 'page-home'
+ ),
+ $template_ids
+ );
}
/**
This is currently passing both on this branch, and on We probably need to do that in a similar way as we're doing here: wordpress-develop/tests/phpunit/tests/block-template-utils.php Lines 37 to 51 in cb6f447
I haven't figured out how to set |
The following test fails on commit d526a41f1cfb994ca1d2876e985b94936f83f5f7
Author: Bernie Reiter <ockham@raz.or.at>
Date: Thu Sep 8 15:44:59 2022 +0200
Add basic test coverage
diff --git a/tests/phpunit/data/themedir1/block-theme/templates/custom-single-post-template.html b/tests/phpunit/data/themedir1/block-theme/templates/custom-single-post-template.html
new file mode 100644
index 0000000000..fc0a5f8a4b
--- /dev/null
+++ b/tests/phpunit/data/themedir1/block-theme/templates/custom-single-post-template.html
@@ -0,0 +1,3 @@
+<!-- wp:paragraph -->
+<p>Custom Single Post template</p>
+<!-- /wp:paragraph -->
diff --git a/tests/phpunit/data/themedir1/block-theme/theme.json b/tests/phpunit/data/themedir1/block-theme/theme.json
index 7676d642b2..4961a620a2 100644
--- a/tests/phpunit/data/themedir1/block-theme/theme.json
+++ b/tests/phpunit/data/themedir1/block-theme/theme.json
@@ -58,6 +58,11 @@
{
"name": "page-home",
"title": "Homepage template"
+ },
+ {
+ "name": "custom-single-post-template",
+ "title": "Custom Single Post template",
+ "postTypes": ["post"]
}
],
"templateParts": [
diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php
index 666edd020a..6d16cc7260 100644
--- a/tests/phpunit/tests/block-template-utils.php
+++ b/tests/phpunit/tests/block-template-utils.php
@@ -12,6 +12,7 @@
*/
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';
@@ -50,6 +51,22 @@ 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',
@@ -319,6 +336,27 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase {
$template_ids
);
*/
+
+ // Filter by post type.
+ $templates = get_block_templates( array( 'post_type' => 'post' ), 'wp_template' );
+ $template_ids = get_template_ids( $templates );
+ $this->assertSame(
+ array(
+ get_stylesheet() . '//' . 'my_template',
+ get_stylesheet() . '//' . 'custom-single-post-template'
+ ),
+ $template_ids
+ );
+
+ $templates = get_block_templates( array( 'post_type' => 'page' ), 'wp_template' );
+ $template_ids = get_template_ids( $templates );
+ $this->assertSame(
+ array(
+ get_stylesheet() . '//' . 'my_template',
+ get_stylesheet() . '//' . 'page-home'
+ ),
+ $template_ids
+ );
}
/** (Formatting might still be off, Run with |
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.
LGTM. Let's include some test coverage (this patch)
Thanks for looking into this, @ockham! Do you want to push the commit yourself, or do you want me to do it? (Both work for me, just want to make sure you get proper attribution for your work 🙂 ) |
@Aljullu I don't think I have write access to your |
4dc0582
to
b86a12f
Compare
b86a12f
to
e5df864
Compare
Thanks @ockham! I applied your patch and rebased this PR. You will notice I had to make some small changes in some other tests as well, so I would appreciate if you can take a look. |
Ah, thank you -- I should've run all tests at least once I was done updating the block template utils one 😅 It's a bit unfortunate that we have to add the new template to the tests in |
Here's a new patch. It keeps the check for Finally, I'd forgotten to remove the template post object during teardown in diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php
index 1f034fdc78..340bbfff4f 100644
--- a/tests/phpunit/tests/block-template-utils.php
+++ b/tests/phpunit/tests/block-template-utils.php
@@ -95,6 +95,7 @@ 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() {
diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php
index 7aabc1c599..a8a93f9b23 100644
--- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php
+++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php
@@ -154,18 +154,15 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
),
$theme_data->get_settings()
);
- $this->assertSameSets(
+
+ $custom_templates = $theme_data->get_custom_templates();
+ $this->assertArrayHasKey( 'page-home', $custom_templates );
+ $this->assertSame(
+ $custom_templates['page-home'],
array(
- 'page-home' => array(
- 'title' => 'Szablon strony głównej',
- 'postTypes' => array( 'page' ),
- ),
- 'custom-single-post-template' => array(
- 'title' => 'Custom Single Post template',
- 'postTypes' => array( 'post' ),
- ),
- ),
- $theme_data->get_custom_templates()
+ 'title' => 'Szablon strony głównej',
+ 'postTypes' => array( 'page' ),
+ )
);
$this->assertSameSets(
array( |
e5df864
to
442ff41
Compare
New patch applied. Thanks for following-up on this, @ockham! |
Tests fail before the fix. Tests pass after the fix.
* @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 ) { |
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.
To isolate the tests for the fix, I've:
- created each dataset for switching the post type
- encapsulated the tests for validating that it respects the post type property
By isolating the tests, it makes it easier to identify the purpose of the test. It will also will make it easier when the test class is split into separate files one per function (this is out-of-scope for this PR).
The test class itself needs to be split into separate files, one for each function. But that's out-of-scope for this PR.
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.
- I am able to reproduce the reported issue.
- I can confirm this PR does fix the reported issue.
- And the tests do validate it is fixed by:
- Before applying the fix, the tests fail.
- After applying the fix, the tests pass ✅
This PR is ready for commit.
Great job @Aljullu and @ockham 👏 Thank you for your contributions! This PR has been committed via changeset https://core.trac.wordpress.org/changeset/54184. |
When loading a block template from post (ie: the user had made some modifications to it), WP was not setting the
post_types
attribute for that template. That caused templates specific to some post types to show available for all.Trac ticket: https://core.trac.wordpress.org/ticket/55881#ticket
Steps to test