From 360dfe5681a08a76c64454fc2dcdae151ddf39cd Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Wed, 26 Aug 2026 13:27:54 +0300 Subject: [PATCH 1/3] Same title check in `_wp_build_title_and_description_for_single_post_type_block_template` can never match --- src/wp-includes/block-template-utils.php | 3 +- .../buildBlockTemplateResultFromPost.php | 72 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 2d23f8545e87f..d17aa174398e3 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -729,7 +729,8 @@ function _wp_build_title_and_description_for_single_post_type_block_template( $p ); $args = array( - 'title' => $post_title, + 'title' => $post_title, + 'posts_per_page' => 2, ); $args = wp_parse_args( $args, $default_args ); diff --git a/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php b/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php index 5dbf91d911f07..221f8db85873a 100644 --- a/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php +++ b/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php @@ -176,6 +176,78 @@ public function test_should_not_inject_ignored_hooked_block_into_template() { $this->assertStringNotContainsString( '', $template->content ); } + /** + * @ticket 65966 + */ + public function test_should_append_post_slug_to_title_when_posts_share_the_same_title() { + self::factory()->post->create( + array( + 'post_title' => 'Same Title', + 'post_name' => 'first-post', + ) + ); + self::factory()->post->create( + array( + 'post_title' => 'Same Title', + 'post_name' => 'second-post', + ) + ); + + $template_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_template', + 'post_name' => 'single-post-first-post', + 'post_title' => 'single-post-first-post', + 'post_content' => 'Content', + 'post_excerpt' => '', + 'tax_input' => array( + 'wp_theme' => array( + self::TEST_THEME, + ), + ), + ) + ); + wp_set_post_terms( $template_post->ID, self::TEST_THEME, 'wp_theme' ); + + $template = _build_block_template_result_from_post( $template_post ); + + $this->assertNotWPError( $template ); + $this->assertSame( 'Post: Same Title (first-post)', $template->title ); + } + + /** + * @ticket 65966 + */ + public function test_should_not_append_post_slug_to_title_when_post_title_is_unique() { + self::factory()->post->create( + array( + 'post_title' => 'Unique Title', + 'post_name' => 'unique-post', + ) + ); + + $template_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_template', + 'post_name' => 'single-post-unique-post', + 'post_title' => 'single-post-unique-post', + 'post_content' => 'Content', + 'post_excerpt' => '', + 'tax_input' => array( + 'wp_theme' => array( + self::TEST_THEME, + ), + ), + ) + ); + wp_set_post_terms( $template_post->ID, self::TEST_THEME, 'wp_theme' ); + + $template = _build_block_template_result_from_post( $template_post ); + + $this->assertNotWPError( $template ); + $this->assertSame( 'Post: Unique Title', $template->title ); + } + /** * @ticket 59646 * @ticket 60506 From 7b8bd1ce366d369cc6dfe2c620866a60525dff62 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Wed, 26 Aug 2026 14:46:02 +0300 Subject: [PATCH 2/3] more tests (taxonomies) --- .../buildBlockTemplateResultFromPost.php | 123 +++++++++++++++++- 1 file changed, 118 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php b/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php index 221f8db85873a..705228cf21916 100644 --- a/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php +++ b/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php @@ -193,7 +193,7 @@ public function test_should_append_post_slug_to_title_when_posts_share_the_same_ ) ); - $template_post = self::factory()->post->create_and_get( + $first_template_post = self::factory()->post->create_and_get( array( 'post_type' => 'wp_template', 'post_name' => 'single-post-first-post', @@ -207,12 +207,31 @@ public function test_should_append_post_slug_to_title_when_posts_share_the_same_ ), ) ); - wp_set_post_terms( $template_post->ID, self::TEST_THEME, 'wp_theme' ); + wp_set_post_terms( $first_template_post->ID, self::TEST_THEME, 'wp_theme' ); - $template = _build_block_template_result_from_post( $template_post ); + $second_template_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_template', + 'post_name' => 'single-post-second-post', + 'post_title' => 'single-post-second-post', + 'post_content' => 'Content', + 'post_excerpt' => '', + 'tax_input' => array( + 'wp_theme' => array( + self::TEST_THEME, + ), + ), + ) + ); + wp_set_post_terms( $second_template_post->ID, self::TEST_THEME, 'wp_theme' ); - $this->assertNotWPError( $template ); - $this->assertSame( 'Post: Same Title (first-post)', $template->title ); + $first_template = _build_block_template_result_from_post( $first_template_post ); + $second_template = _build_block_template_result_from_post( $second_template_post ); + + $this->assertNotWPError( $first_template ); + $this->assertSame( 'Post: Same Title (first-post)', $first_template->title ); + $this->assertNotWPError( $second_template ); + $this->assertSame( 'Post: Same Title (second-post)', $second_template->title ); } /** @@ -248,6 +267,100 @@ public function test_should_not_append_post_slug_to_title_when_post_title_is_uni $this->assertSame( 'Post: Unique Title', $template->title ); } + /** + * @ticket 65966 + */ + public function test_should_append_term_slug_to_title_when_terms_share_the_same_name() { + self::factory()->term->create( + array( + 'taxonomy' => 'post_tag', + 'name' => 'Same Name', + 'slug' => 'first-tag', + ) + ); + self::factory()->term->create( + array( + 'taxonomy' => 'post_tag', + 'name' => 'Same Name', + 'slug' => 'second-tag', + ) + ); + + $first_template_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_template', + 'post_name' => 'tag-first-tag', + 'post_title' => 'tag-first-tag', + 'post_content' => 'Content', + 'post_excerpt' => '', + 'tax_input' => array( + 'wp_theme' => array( + self::TEST_THEME, + ), + ), + ) + ); + wp_set_post_terms( $first_template_post->ID, self::TEST_THEME, 'wp_theme' ); + + $second_template_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_template', + 'post_name' => 'tag-second-tag', + 'post_title' => 'tag-second-tag', + 'post_content' => 'Content', + 'post_excerpt' => '', + 'tax_input' => array( + 'wp_theme' => array( + self::TEST_THEME, + ), + ), + ) + ); + wp_set_post_terms( $second_template_post->ID, self::TEST_THEME, 'wp_theme' ); + + $first_template = _build_block_template_result_from_post( $first_template_post ); + $second_template = _build_block_template_result_from_post( $second_template_post ); + + $this->assertNotWPError( $first_template ); + $this->assertSame( 'Tag: Same Name (first-tag)', $first_template->title ); + $this->assertNotWPError( $second_template ); + $this->assertSame( 'Tag: Same Name (second-tag)', $second_template->title ); + } + + /** + * @ticket 65966 + */ + public function test_should_not_append_term_slug_to_title_when_term_name_is_unique() { + self::factory()->term->create( + array( + 'taxonomy' => 'category', + 'name' => 'Unique Category', + 'slug' => 'unique-category', + ) + ); + + $template_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_template', + 'post_name' => 'category-unique-category', + 'post_title' => 'category-unique-category', + 'post_content' => 'Content', + 'post_excerpt' => '', + 'tax_input' => array( + 'wp_theme' => array( + self::TEST_THEME, + ), + ), + ) + ); + wp_set_post_terms( $template_post->ID, self::TEST_THEME, 'wp_theme' ); + + $template = _build_block_template_result_from_post( $template_post ); + + $this->assertNotWPError( $template ); + $this->assertSame( 'Category: Unique Category', $template->title ); + } + /** * @ticket 59646 * @ticket 60506 From d785c7ca8cd172ef8cdaad445f4285f20d9cf8b6 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Wed, 26 Aug 2026 15:10:49 +0300 Subject: [PATCH 3/3] add assertion messages --- .../buildBlockTemplateResultFromPost.php | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php b/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php index 705228cf21916..05bd88bcd24dd 100644 --- a/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php +++ b/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php @@ -228,10 +228,10 @@ public function test_should_append_post_slug_to_title_when_posts_share_the_same_ $first_template = _build_block_template_result_from_post( $first_template_post ); $second_template = _build_block_template_result_from_post( $second_template_post ); - $this->assertNotWPError( $first_template ); - $this->assertSame( 'Post: Same Title (first-post)', $first_template->title ); - $this->assertNotWPError( $second_template ); - $this->assertSame( 'Post: Same Title (second-post)', $second_template->title ); + $this->assertNotWPError( $first_template, 'Building the template for the first post should not return an error.' ); + $this->assertSame( 'Post: Same Title (first-post)', $first_template->title, 'The title of the first post template should be suffixed with the post slug.' ); + $this->assertNotWPError( $second_template, 'Building the template for the second post should not return an error.' ); + $this->assertSame( 'Post: Same Title (second-post)', $second_template->title, 'The title of the second post template should be suffixed with the post slug.' ); } /** @@ -263,8 +263,8 @@ public function test_should_not_append_post_slug_to_title_when_post_title_is_uni $template = _build_block_template_result_from_post( $template_post ); - $this->assertNotWPError( $template ); - $this->assertSame( 'Post: Unique Title', $template->title ); + $this->assertNotWPError( $template, 'Building the template should not return an error.' ); + $this->assertSame( 'Post: Unique Title', $template->title, 'The template title should not be suffixed with the post slug when the post title is unique.' ); } /** @@ -321,10 +321,10 @@ public function test_should_append_term_slug_to_title_when_terms_share_the_same_ $first_template = _build_block_template_result_from_post( $first_template_post ); $second_template = _build_block_template_result_from_post( $second_template_post ); - $this->assertNotWPError( $first_template ); - $this->assertSame( 'Tag: Same Name (first-tag)', $first_template->title ); - $this->assertNotWPError( $second_template ); - $this->assertSame( 'Tag: Same Name (second-tag)', $second_template->title ); + $this->assertNotWPError( $first_template, 'Building the template for the first term should not return an error.' ); + $this->assertSame( 'Tag: Same Name (first-tag)', $first_template->title, 'The title of the first term template should be suffixed with the term slug.' ); + $this->assertNotWPError( $second_template, 'Building the template for the second term should not return an error.' ); + $this->assertSame( 'Tag: Same Name (second-tag)', $second_template->title, 'The title of the second term template should be suffixed with the term slug.' ); } /** @@ -357,8 +357,8 @@ public function test_should_not_append_term_slug_to_title_when_term_name_is_uniq $template = _build_block_template_result_from_post( $template_post ); - $this->assertNotWPError( $template ); - $this->assertSame( 'Category: Unique Category', $template->title ); + $this->assertNotWPError( $template, 'Building the template should not return an error.' ); + $this->assertSame( 'Category: Unique Category', $template->title, 'The template title should not be suffixed with the term slug when the term name is unique.' ); } /**