fix: harden CHRNX archive and pattern composition

This commit is contained in:
chrnx committed 2026-08-11 18:27:22 -05:00
1 parent 7297523e26
commit 2230731e1b
11 files changed
+214 -16

No files matched your search

+45
View File
@@ -133,6 +133,51 @@ class PatternsTest extends WP_UnitTestCase {
}
}
/** Insertable page patterns render below the template's one H1 without duplicating post titles. */
public function test_insertable_page_patterns_preserve_single_template_h1() {
global $post;
$patterns = array( 'about', 'music-landing', 'podcast-landing', 'project-detail', 'podcast-show', 'podcast-episode' );
$template = file_get_contents( get_theme_file_path( 'templates/page.html' ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local integration fixture.
foreach ( $patterns as $name ) {
$content = $this->render_pattern( get_theme_file_path( 'patterns/' . $name . '.php' ) );
$this->assertStringNotContainsString( 'wp:post-title', $content, $name );
$this->assertDoesNotMatchRegularExpression( '/<h1\b|wp:heading \{[^}]*"level":1/', $content, $name );
$this->assertNotEmpty( parse_blocks( $content ), $name );
$page_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_status' => 'publish',
'post_title' => 'Template page title',
'post_content' => $content,
)
);
$previous_post = $post;
$post = get_post( $page_id );
setup_postdata( $post );
$rendered = '';
try {
foreach ( parse_blocks( $template ) as $template_block ) {
$rendered .= ( new WP_Block(
$template_block,
array(
'postId' => $page_id,
'postType' => 'page',
)
) )->render();
}
} finally {
wp_reset_postdata();
$post = $previous_post;
}
$this->assertSame( 1, substr_count( $rendered, '<h1' ), $name );
}
foreach ( array( 'about', 'music-landing', 'podcast-landing' ) as $name ) {
$content = $this->render_pattern( get_theme_file_path( 'patterns/' . $name . '.php' ) );
$this->assertMatchesRegularExpression( '/<!-- wp:heading(?: \{"level":2\})? -->\s*<h2\b/', $content, $name );
}
}
/** Render a pattern file exactly as WordPress does. */
private function render_pattern( $path ) {
ob_start();