fix: harden CHRNX archive and pattern composition
This commit is contained in:
11 files changed
+214
-16
No files matched your search
@@ -182,12 +182,21 @@ class BlockRenderingTest extends WP_UnitTestCase {
|
||||
|
||||
/** Empty/recovery links always point directly to a portable full Journal URL. */
|
||||
public function test_journal_recovery_links_are_localized_and_portable() {
|
||||
$journal_page = self::factory()->post->create(
|
||||
array(
|
||||
'post_type' => 'page',
|
||||
'post_status' => 'publish',
|
||||
'post_name' => 'journal',
|
||||
'post_title' => 'Journal',
|
||||
)
|
||||
);
|
||||
$portable_home = static function ( $url, $path ) {
|
||||
return 'https://example.test/subdirectory/' . ltrim( (string) $path, '/' );
|
||||
};
|
||||
add_filter( 'home_url', $portable_home, 10, 2 );
|
||||
try {
|
||||
$link = render_block(
|
||||
$expected_url = esc_url( get_permalink( $journal_page ) );
|
||||
$link = render_block(
|
||||
array(
|
||||
'blockName' => 'chrnx/journal-link',
|
||||
'attrs' => array(),
|
||||
@@ -196,7 +205,7 @@ class BlockRenderingTest extends WP_UnitTestCase {
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
$this->assertStringContainsString( 'href="https://example.test/subdirectory/journal/"', $link );
|
||||
$this->assertStringContainsString( 'href="' . $expected_url . '"', $link );
|
||||
$this->assertStringContainsString( 'Browse the full Journal', $link );
|
||||
|
||||
$stream = render_block(
|
||||
@@ -208,13 +217,59 @@ class BlockRenderingTest extends WP_UnitTestCase {
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
$this->assertStringContainsString( 'href="https://example.test/subdirectory/journal/"', $stream );
|
||||
$this->assertStringContainsString( 'href="' . $expected_url . '"', $stream );
|
||||
$this->assertStringContainsString( 'Browse the full Journal', $stream );
|
||||
} finally {
|
||||
remove_filter( 'home_url', $portable_home, 10 );
|
||||
}
|
||||
}
|
||||
|
||||
/** Journal recovery falls back to a valid posts Page and then site home, never a guessed route. */
|
||||
public function test_full_journal_url_has_non_404_plain_pretty_and_subdirectory_fallbacks() {
|
||||
global $wp_rewrite;
|
||||
$previous_structure = get_option( 'permalink_structure' );
|
||||
$previous_posts_page = get_option( 'page_for_posts' );
|
||||
$portable_home = static function ( $url, $path ) {
|
||||
return 'https://example.test/subdirectory/' . ltrim( (string) $path, '/' );
|
||||
};
|
||||
$existing_journal = get_page_by_path( 'journal', OBJECT, 'page' );
|
||||
if ( $existing_journal instanceof WP_Post ) {
|
||||
wp_delete_post( $existing_journal->ID, true );
|
||||
}
|
||||
$posts_page = self::factory()->post->create(
|
||||
array(
|
||||
'post_type' => 'page',
|
||||
'post_status' => 'publish',
|
||||
'post_name' => 'writing',
|
||||
'post_title' => 'Writing',
|
||||
)
|
||||
);
|
||||
update_option( 'page_for_posts', $posts_page );
|
||||
|
||||
try {
|
||||
$wp_rewrite->set_permalink_structure( '' );
|
||||
$this->assertSame( get_permalink( $posts_page ), chrnx_get_full_journal_url() );
|
||||
|
||||
$wp_rewrite->set_permalink_structure( '/%postname%/' );
|
||||
$this->assertSame( get_permalink( $posts_page ), chrnx_get_full_journal_url() );
|
||||
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => $posts_page,
|
||||
'post_status' => 'draft',
|
||||
)
|
||||
);
|
||||
add_filter( 'home_url', $portable_home, 10, 2 );
|
||||
$this->assertSame( 'https://example.test/subdirectory/', chrnx_get_full_journal_url() );
|
||||
$this->assertStringNotContainsString( '/journal/', chrnx_get_full_journal_url() );
|
||||
remove_filter( 'home_url', $portable_home, 10 );
|
||||
} finally {
|
||||
remove_filter( 'home_url', $portable_home, 10 );
|
||||
update_option( 'page_for_posts', $previous_posts_page );
|
||||
$wp_rewrite->set_permalink_structure( $previous_structure );
|
||||
}
|
||||
}
|
||||
|
||||
/** Podcast metadata renders known text and HTTPS links, never embed HTML. */
|
||||
public function test_podcast_meta_renders_only_known_escaped_fields() {
|
||||
$post_id = self::factory()->post->create( array( 'post_title' => 'Episode' ) );
|
||||
|
||||
@@ -181,6 +181,61 @@ class EditorialQueriesTest extends WP_UnitTestCase {
|
||||
$this->assertSame( 'es', $args['tax_query'][1]['terms'] );
|
||||
}
|
||||
|
||||
/** Real topic archives override conflicting request filters and never mix terms. */
|
||||
public function test_topic_archive_context_drives_journal_query_and_selector() {
|
||||
$code = self::factory()->post->create( array( 'post_title' => 'Code archive story' ) );
|
||||
$life = self::factory()->post->create( array( 'post_title' => 'Life archive story' ) );
|
||||
wp_set_object_terms( $code, 'code', 'chrnx_topic' );
|
||||
wp_set_object_terms( $life, 'life', 'chrnx_topic' );
|
||||
$term = get_term_by( 'slug', 'code', 'chrnx_topic' );
|
||||
$this->go_to( get_term_link( $term ) );
|
||||
|
||||
$previous_get = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only request.
|
||||
$_GET = array( 'chrnx_topic' => 'life' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only request.
|
||||
try {
|
||||
$context = chrnx_get_journal_request_context( $_GET ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only request.
|
||||
$output = render_block(
|
||||
array(
|
||||
'blockName' => 'chrnx/journal-stream',
|
||||
'attrs' => array(),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
} finally {
|
||||
$_GET = $previous_get;
|
||||
}
|
||||
|
||||
$this->assertSame( 'code', $context['chrnx_topic'] );
|
||||
$this->assertMatchesRegularExpression( '/value="code"\s+selected=[\'\"]selected[\'\"]/', $output );
|
||||
$this->assertStringContainsString( 'Code archive story', $output );
|
||||
$this->assertStringNotContainsString( 'Life archive story', $output );
|
||||
$this->assertStringContainsString( 'action="' . esc_url( get_term_link( $term ) ) . '"', $output );
|
||||
}
|
||||
|
||||
/** Language archives constrain results while unsupported taxonomy archives are ignored. */
|
||||
public function test_language_archive_context_is_allowlisted_and_other_taxonomies_are_not() {
|
||||
$spanish = self::factory()->post->create( array( 'post_title' => 'Historia en español' ) );
|
||||
$english = self::factory()->post->create( array( 'post_title' => 'English story' ) );
|
||||
wp_set_object_terms( $spanish, 'es', 'chrnx_language' );
|
||||
wp_set_object_terms( $english, 'en', 'chrnx_language' );
|
||||
$spanish_term = get_term_by( 'slug', 'es', 'chrnx_language' );
|
||||
$this->go_to( get_term_link( $spanish_term ) );
|
||||
|
||||
$context = chrnx_get_journal_request_context( array( 'chrnx_language' => 'en' ) );
|
||||
$args = chrnx_get_journal_query_args( $context );
|
||||
$query = chrnx_run_eligible_query( $args );
|
||||
$this->assertSame( 'es', $context['chrnx_language'] );
|
||||
$this->assertSame( array( $spanish ), array_map( 'absint', wp_list_pluck( $query->posts, 'ID' ) ) );
|
||||
|
||||
$tag = self::factory()->tag->create_and_get( array( 'slug' => 'not-allowlisted' ) );
|
||||
$this->go_to( get_term_link( $tag ) );
|
||||
$unsupported = chrnx_get_journal_request_context( array() );
|
||||
$this->assertArrayNotHasKey( 'post_tag', $unsupported );
|
||||
$this->assertArrayNotHasKey( 'tax_query', chrnx_get_journal_query_args( $unsupported ) );
|
||||
}
|
||||
|
||||
/** Malformed journal request values are ignored and never copied into query args. */
|
||||
public function test_journal_query_ignores_malformed_filters_and_caps_search() {
|
||||
$args = chrnx_get_journal_query_args(
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user