Files
chrnx-dev-wp/tests/php/BlockRenderingTest.php
T

867 lines
35 KiB
PHP

<?php
/**
* Tests for server-rendered editorial blocks.
*
* @package CHRNX
*/
/** Verify block registration, fixed art, and safe dynamic rendering. */
class BlockRenderingTest extends WP_UnitTestCase {
/** Register dependencies and blocks. */
public function set_up() {
parent::set_up();
do_action( 'init' );
chrnx_seed_editorial_terms();
delete_option( 'chrnx_subscribe_page_id' );
}
/** All fourteen block types register from metadata. */
public function test_all_editorial_blocks_are_registered() {
$names = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta', 'theme-toggle', 'site-brand', 'mobile-menu-toggle', 'footer-content', 'header-tools', 'reading-time', 'journal-link' );
foreach ( $names as $name ) {
$type = WP_Block_Type_Registry::get_instance()->get_registered( 'chrnx/' . $name );
$this->assertInstanceOf( WP_Block_Type::class, $type, $name );
$this->assertTrue( is_callable( $type->render_callback ), $name );
}
}
/** Header navigation and search use translated labels and portable home URLs. */
public function test_header_tools_render_localized_portable_navigation_and_search() {
$translate = static function ( $translated, $text, $domain ) {
if ( 'chrnx' !== $domain ) {
return $translated;
}
return array(
'Primary navigation' => 'Navegación principal',
'Journal' => 'Diario',
'Search' => 'Buscar',
'Open search' => 'Abrir búsqueda',
)[ $text ] ?? $translated;
};
$portable_home = static function ( $url, $path ) {
return 'https://example.test/subdirectory/' . ltrim( (string) $path, '/' );
};
add_filter( 'gettext', $translate, 10, 3 );
add_filter( 'home_url', $portable_home, 10, 2 );
try {
$output = render_block(
array(
'blockName' => 'chrnx/header-tools',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
} finally {
remove_filter( 'home_url', $portable_home, 10 );
remove_filter( 'gettext', $translate, 10 );
}
$this->assertSame( 1, substr_count( $output, '<nav ' ) );
$this->assertStringContainsString( 'aria-label="Navegación principal"', $output );
$this->assertSame( 1, substr_count( $output, 'aria-label="Navegación principal"' ) );
$this->assertStringContainsString( '>Diario</a>', $output );
foreach ( array( 'journal', 'projects', 'music', 'podcasts', 'about' ) as $path ) {
$this->assertStringContainsString( 'href="https://example.test/subdirectory/' . $path . '/"', $output );
}
$this->assertStringContainsString( 'role="search"', $output );
$this->assertSame( 1, substr_count( $output, 'method="get"' ) );
$this->assertStringContainsString( 'action="https://example.test/subdirectory/"', $output );
$this->assertStringContainsString( 'name="s"', $output );
$this->assertStringContainsString( 'chrnx-screen-reader-text">Buscar</span>', $output );
$this->assertStringContainsString( 'data-label-open="Abrir búsqueda"', $output );
$this->assertMatchesRegularExpression(
'/data-chrnx-search-button[^>]+aria-controls="([^"]+)"[^>]+aria-expanded="true"/',
$output
);
preg_match( '/data-chrnx-search-button[^>]+aria-controls="([^"]+)"/', $output, $search_control );
$this->assertNotEmpty( $search_control[1] );
$this->assertSame( 1, substr_count( $output, 'id="' . $search_control[1] . '"' ) );
preg_match(
'/<[^>]+id="' . preg_quote( $search_control[1], '/' ) . '"[^>]+data-chrnx-search-panel[^>]*>/',
$output,
$search_panel
);
$this->assertNotEmpty( $search_panel[0] );
$this->assertStringNotContainsString( ' hidden', $search_panel[0] );
}
/** Theme preference keeps native controls behind one translated mobile trigger. */
public function test_theme_toggle_renders_accessible_mobile_trigger_and_native_panel() {
$translate = static function ( $translated, $text, $domain ) {
if ( 'chrnx' === $domain && 'Choose color theme' === $text ) {
return 'Elegir tema de color';
}
return $translated;
};
add_filter( 'gettext', $translate, 10, 3 );
try {
$output = render_block(
array(
'blockName' => 'chrnx/theme-toggle',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
} finally {
remove_filter( 'gettext', $translate, 10 );
}
$this->assertSame( 1, substr_count( $output, 'data-chrnx-theme-button' ) );
$this->assertMatchesRegularExpression(
'/data-chrnx-theme-button[^>]+aria-controls="([^"]+)"[^>]+aria-expanded="false"[^>]+aria-label="Elegir tema de color"/',
$output
);
preg_match( '/data-chrnx-theme-button[^>]+aria-controls="([^"]+)"/', $output, $theme_control );
$this->assertNotEmpty( $theme_control[1] );
$this->assertSame( 1, substr_count( $output, 'id="' . $theme_control[1] . '"' ) );
$this->assertMatchesRegularExpression(
'/<fieldset[^>]+id="' . preg_quote( $theme_control[1], '/' ) . '"[^>]+data-chrnx-theme-toggle[^>]+data-chrnx-theme-panel/',
$output
);
$this->assertSame( 3, substr_count( $output, 'type="radio"' ) );
foreach ( array( 'light', 'dark', 'system' ) as $value ) {
$this->assertMatchesRegularExpression(
'/<input[^>]+type="radio"[^>]+value="' . $value . '"/',
$output
);
}
$this->assertStringContainsString( 'data-chrnx-theme-status', $output );
$this->assertStringContainsString( '<noscript>', $output );
}
/** Mobile menu control is localized, initially hidden, and targets the one primary navigation. */
public function test_mobile_menu_toggle_renders_progressive_control() {
$output = render_block(
array(
'blockName' => 'chrnx/mobile-menu-toggle',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$this->assertStringContainsString( 'data-chrnx-menu-toggle', $output );
$this->assertStringContainsString( 'aria-controls="chrnx-primary-navigation"', $output );
$this->assertStringContainsString( 'aria-expanded="false"', $output );
$this->assertStringContainsString( 'hidden', $output );
$this->assertStringContainsString( 'Open primary navigation', $output );
$this->assertStringContainsString( 'Close primary navigation', $output );
}
/** Footer destinations and personal copy render through escaped translation functions. */
public function test_footer_content_renders_local_internal_navigation() {
$output = render_block(
array(
'blockName' => 'chrnx/footer-content',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$this->assertStringContainsString( 'aria-label="Footer navigation"', $output );
foreach ( array( 'journal', 'projects', 'music', 'podcasts', 'about' ) as $path ) {
$this->assertStringContainsString( 'href="' . esc_url( home_url( '/' . $path . '/' ) ) . '"', $output );
}
$this->assertStringContainsString( 'Made in Sinaloa, shared from home.', $output );
$this->assertStringContainsString( esc_html( get_bloginfo( 'name' ) ), $output );
$this->assertStringNotContainsString( 'facebook', strtolower( $output ) );
$this->assertStringNotContainsString( '<form', $output );
}
/** The approved local wordmark links to the portable site home URL. */
public function test_site_brand_renders_escaped_local_home_and_wordmark_urls() {
$output = render_block(
array(
'blockName' => 'chrnx/site-brand',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$this->assertStringContainsString( 'href="' . esc_url( home_url( '/' ) ) . '"', $output );
$this->assertStringContainsString( 'src="' . esc_url( get_theme_file_uri( 'assets/icons/chrnx-wordmark.svg' ) ) . '"', $output );
$this->assertStringContainsString( 'alt="CHRNX"', $output );
$this->assertStringContainsString( 'href="#chrnx-main"', $output );
}
/** Topic art is an exact fixed mapping and rejects arbitrary paths. */
public function test_topic_art_uses_only_the_fixed_mapping() {
$expected = array(
'research' => 'alter-ego-research.png',
'code' => 'alter-ego-code.png',
'literature' => 'alter-ego-literature.png',
'music' => 'the-no-names-musician-cutout.png',
'podcasts' => 'alter-ego-podcasts.png',
'life' => 'alter-ego-life.png',
'gaming' => 'alter-ego-gaming.png',
);
foreach ( $expected as $slug => $filename ) {
$this->assertSame( 'assets/images/' . $filename, chrnx_get_topic_art_asset( $slug ) );
$this->assertSame( 'gaming' === $slug ? array( 1024, 1536 ) : array( 1122, 1402 ), chrnx_get_topic_art_dimensions( $slug ) );
}
foreach ( array( '', 'unknown', '../secret.png', 'assets/images/anything.png' ) as $value ) {
$this->assertSame( '', chrnx_get_topic_art_asset( $value ) );
$this->assertSame( array(), chrnx_get_topic_art_dimensions( $value ) );
}
}
/** Reading time counts real Unicode words, ignores markup, and localizes singular/plural labels. */
public function test_reading_time_is_robust_and_localized() {
$this->assertSame( 0, chrnx_get_reading_time_minutes( '' ) );
$this->assertSame( 1, chrnx_get_reading_time_minutes( '<!-- wp:paragraph --><p>Árbol, café &amp; código.</p><!-- /wp:paragraph -->' ) );
$this->assertSame( 2, chrnx_get_reading_time_minutes( implode( ' ', array_fill( 0, 201, 'palabra' ) ) ) );
$post_id = self::factory()->post->create(
array(
'post_content' => implode( ' ', array_fill( 0, 201, 'palabra' ) ),
)
);
$translate = static function ( $translation, $single, $plural, $number, $domain ) {
if ( 'chrnx' === $domain && '%s minute read' === $single ) {
return 1 === $number ? '%s minuto de lectura' : '%s minutos de lectura';
}
return $translation;
};
add_filter( 'ngettext', $translate, 10, 5 );
try {
$output = $this->render_for_post( 'chrnx/reading-time', $post_id );
} finally {
remove_filter( 'ngettext', $translate, 10 );
}
$this->assertStringContainsString( '2 minutos de lectura', $output );
$this->assertStringNotContainsString( 'palabra', $output );
}
/** 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 {
$expected_url = esc_url( get_permalink( $journal_page ) );
$link = render_block(
array(
'blockName' => 'chrnx/journal-link',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$this->assertStringContainsString( 'href="' . $expected_url . '"', $link );
$this->assertStringContainsString( 'Browse the full Journal', $link );
$stream = render_block(
array(
'blockName' => 'chrnx/journal-stream',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$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 );
}
}
/** Fixed archive dimensions render as context while the other selector and clear exit remain usable. */
public function test_topic_archive_hides_inoperable_filter_and_keeps_language_and_clear_actions() {
$code_es = self::factory()->post->create( array( 'post_title' => 'Code Spanish' ) );
$code_en = self::factory()->post->create( array( 'post_title' => 'Code English' ) );
$life_es = self::factory()->post->create( array( 'post_title' => 'Life Spanish' ) );
foreach ( array( $code_es, $code_en ) as $post_id ) {
wp_set_object_terms( $post_id, 'code', 'chrnx_topic' );
}
wp_set_object_terms( $life_es, 'life', 'chrnx_topic' );
wp_set_object_terms( $code_es, 'es', 'chrnx_language' );
wp_set_object_terms( $code_en, 'en', 'chrnx_language' );
wp_set_object_terms( $life_es, 'es', 'chrnx_language' );
$journal_page = self::factory()->post->create(
array(
'post_type' => 'page',
'post_status' => 'publish',
'post_name' => 'journal',
'post_title' => 'Journal',
)
);
$this->go_to( get_term_link( get_term_by( 'slug', 'code', 'chrnx_topic' ) ) );
$portable_home = static function ( $url, $path ) {
return 'https://example.test/subdirectory/' . ltrim( (string) $path, '/' );
};
$previous_get = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only requests.
add_filter( 'home_url', $portable_home, 10, 2 );
try {
// A conflicting fixed-topic submit must remain on Code while the free language filter works.
$_GET = array( // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only request.
'chrnx_topic' => 'life',
'chrnx_language' => 'es',
);
$filtered = $this->render_journal_stream();
$this->assertStringNotContainsString( '<select name="chrnx_topic">', $filtered );
$this->assertStringNotContainsString( '>All topics<', $filtered );
$this->assertMatchesRegularExpression( '/chrnx-journal-stream__fixed-filter[^>]*>.*Topic.*Code/s', $filtered );
$this->assertStringContainsString( '<select name="chrnx_language">', $filtered );
$this->assertMatchesRegularExpression( '/value="es"\s+selected=[\'\"]selected[\'\"]/', $filtered );
$this->assertStringContainsString( 'Code Spanish', $filtered );
$this->assertStringNotContainsString( 'Code English', $filtered );
$this->assertStringNotContainsString( 'Life Spanish', $filtered );
// Submitting All for the fixed dimension cannot clear the route context or create a dead control.
$_GET = array( 'chrnx_topic' => '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only request.
$all = $this->render_journal_stream();
$this->assertStringNotContainsString( '<select name="chrnx_topic">', $all );
$this->assertStringNotContainsString( '>All topics<', $all );
$this->assertStringContainsString( 'Code Spanish', $all );
$this->assertStringContainsString( 'Code English', $all );
$this->assertStringNotContainsString( 'Life Spanish', $all );
$this->assertStringContainsString( 'class="chrnx-journal-stream__clear"', $all );
$this->assertStringContainsString( 'href="' . esc_url( get_permalink( $journal_page ) ) . '"', $all );
} finally {
$_GET = $previous_get;
remove_filter( 'home_url', $portable_home, 10 );
}
}
/** 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' ) );
update_post_meta( $post_id, 'chrnx_episode_number', 42 );
update_post_meta( $post_id, 'chrnx_episode_duration', '1:02:03' );
update_post_meta( $post_id, 'chrnx_episode_participants', array( 'Ada & Bob', '<script>alert(1)</script>' ) );
update_post_meta( $post_id, 'canonical_episode_url', 'https://example.test/episode' );
update_post_meta( $post_id, 'chrnx_buzzsprout_embed', '<iframe src="https://evil.test"></iframe>' );
$output = $this->render_for_post( 'chrnx/podcast-meta', $post_id );
$this->assertStringContainsString( '42', $output );
$this->assertStringContainsString( '1:02:03', $output );
$this->assertStringContainsString( 'Ada &amp; Bob', $output );
$this->assertStringContainsString( 'https://example.test/episode', $output );
$this->assertStringNotContainsString( '<script', $output );
$this->assertStringNotContainsString( '<iframe', $output );
$this->assertSame( 1, substr_count( $output, 'https://example.test/episode' ) );
}
/** Invalid/empty metadata is omitted rather than guessed. */
public function test_podcast_meta_omits_unknown_or_empty_values() {
$post_id = self::factory()->post->create();
update_post_meta( $post_id, 'canonical_episode_url', 'http://example.test/episode' );
$output = $this->render_for_post( 'chrnx/podcast-meta', $post_id );
$this->assertStringNotContainsString( 'http://example.test', $output );
$this->assertStringNotContainsString( 'Episode 0', $output );
}
/** Latest podcast is a card/link and never a player. */
public function test_latest_podcast_renders_a_linked_card_without_player() {
$post_id = self::factory()->post->create( array( 'post_title' => 'Safe Podcast' ) );
wp_set_object_terms( $post_id, 'podcasts', 'chrnx_topic' );
wp_set_object_terms( $post_id, 'silicon-ranch', 'chrnx_show' );
$output = render_block(
array(
'blockName' => 'chrnx/latest-podcast',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$this->assertStringContainsString( '<article', $output );
$this->assertStringContainsString( 'Safe Podcast', $output );
$this->assertStringContainsString( esc_url( get_permalink( $post_id ) ), $output );
$this->assertStringNotContainsString( '<audio', $output );
$this->assertStringNotContainsString( '<iframe', $output );
}
/** Topic hero uses approved decorative art for mapped topics and rejects arbitrary paths. */
public function test_topic_hero_rejects_paths_and_uses_approved_decorative_art() {
$output = render_block(
array(
'blockName' => 'chrnx/topic-hero',
'attrs' => array(
'topic' => 'research',
'headingLevel' => 2,
),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$this->assertStringContainsString( '<h2', $output );
$this->assertStringContainsString( 'Research', $output );
$this->assertStringContainsString( 'aria-hidden="true"', $output );
$this->assertStringContainsString( '<img', $output );
$this->assertStringContainsString( 'alt=""', $output );
$this->assertStringContainsString( 'width="1122"', $output );
$this->assertStringContainsString( 'height="1402"', $output );
$this->assertStringContainsString( 'loading="lazy"', $output );
$this->assertStringContainsString( 'decoding="async"', $output );
$unsafe = render_block(
array(
'blockName' => 'chrnx/topic-hero',
'attrs' => array( 'topic' => '../secret.png' ),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$this->assertSame( '', $unsafe );
$gaming = render_block(
array(
'blockName' => 'chrnx/topic-hero',
'attrs' => array( 'topic' => 'gaming' ),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$this->assertStringContainsString( 'width="1024"', $gaming );
$this->assertStringContainsString( 'height="1536"', $gaming );
}
/** Subscribe CTA is exactly empty until a published Page is configured. */
public function test_subscribe_cta_requires_a_valid_published_page() {
$block = array(
'blockName' => 'chrnx/subscribe-cta',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
);
$this->assertSame( '', render_block( $block ) );
$page = self::factory()->post->create(
array(
'post_type' => 'page',
'post_status' => 'publish',
'post_title' => 'Subscribe',
)
);
update_option( 'chrnx_subscribe_page_id', $page );
$output = render_block( $block );
$this->assertStringContainsString( '<section', $output );
$this->assertStringContainsString( esc_url( get_permalink( $page ) ), $output );
}
/** Featured cards render only real stories and use a CSS image fallback. */
public function test_featured_stories_have_semantic_variable_cards_and_no_placeholder_images() {
$post_id = self::factory()->post->create( array( 'post_title' => 'Only Story' ) );
wp_set_object_terms( $post_id, 'code', 'chrnx_topic' );
$output = render_block(
array(
'blockName' => 'chrnx/featured-stories',
'attrs' => array(
'limit' => 3,
'headingLevel' => 2,
),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$this->assertSame( 1, substr_count( $output, '<article' ) );
$this->assertStringContainsString( 'Only Story', $output );
$this->assertStringContainsString( 'aria-hidden="true"', $output );
$this->assertStringNotContainsString( 'placeholder', strtolower( $output ) );
}
/** Primed editorial cards reuse relationship caches instead of querying once per fallback. */
public function test_maximum_featured_render_has_no_term_relationship_n_plus_one() {
for ( $index = 1; $index <= 12; ++$index ) {
$post_id = self::factory()->post->create(
array(
'post_title' => 'Cached card ' . $index,
'post_date' => sprintf( '2026-05-%02d 10:00:00', $index ),
)
);
wp_set_object_terms( $post_id, 'research', 'chrnx_topic' );
}
$ids = chrnx_get_featured_story_ids( 12 );
$this->assertCount( 12, $ids );
_prime_post_caches( $ids, true, true );
$term_queries = array();
$capture = static function ( $sql ) use ( &$term_queries ) {
if ( false !== stripos( $sql, 'term_relationships' ) ) {
$term_queries[] = $sql;
}
return $sql;
};
add_filter( 'query', $capture );
try {
$output = render_block(
array(
'blockName' => 'chrnx/featured-stories',
'attrs' => array( 'limit' => 12 ),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
} finally {
remove_filter( 'query', $capture );
}
$this->assertSame( 12, substr_count( $output, '<article' ) );
$this->assertSame( array(), $term_queries, 'Warm rendering must not issue per-card relationship queries.' );
}
/** Stored title markup is displayed literally and cannot inject executable elements. */
public function test_editorial_cards_escape_stored_title_markup() {
$title = '<b title="> x" onmouseover="alert(1)"><script>alert(2)</script></b>';
$post_id = self::factory()->post->create( array( 'post_title' => $title ) );
$output = render_block(
array(
'blockName' => 'chrnx/featured-stories',
'attrs' => array( 'limit' => 1 ),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$this->assertSame( $post_id, chrnx_get_featured_story_ids( 1 )[0] );
$this->assertStringContainsString( esc_html( get_the_title( $post_id ) ), $output );
$this->assertStringNotContainsString( '<script', $output );
$this->assertStringNotContainsString( '<b title=', $output );
}
/** Every CSS fallback is visible, deterministic, decorative, and registered with its block. */
public function test_editorial_fallbacks_have_registered_visible_static_styles() {
$contracts = array(
'featured-stories' => array( 'chrnx-featured-story__fallback', 'aspect-ratio' ),
'journal-stream' => array( 'chrnx-journal-card__fallback', 'min-height' ),
'latest-podcast' => array( 'chrnx-podcast-card__fallback', 'aspect-ratio' ),
'topic-hero' => array( 'chrnx-topic-hero__fallback', 'aspect-ratio' ),
'related-posts' => array( 'chrnx-related-card__fallback', 'aspect-ratio' ),
);
foreach ( $contracts as $block_name => $contract ) {
$type = WP_Block_Type_Registry::get_instance()->get_registered( 'chrnx/' . $block_name );
$this->assertNotEmpty( $type->style_handles, $block_name . ' must register its stylesheet.' );
$path = get_theme_file_path( 'blocks/' . $block_name . '/style.css' );
$this->assertFileExists( $path );
// Local test fixture inspection; no remote URL is accepted here.
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$css = file_get_contents( $path );
$this->assertStringContainsString( '.' . $contract[0], $css );
$this->assertStringContainsString( $contract[1], $css );
$this->assertMatchesRegularExpression( '/background(?:-image)?:/', $css );
$this->assertStringNotContainsString( 'animation:', $css );
}
}
/** Related cards expose a visibly styled decorative fallback when thumbnails are absent. */
public function test_related_posts_render_decorative_css_fallbacks() {
$current = self::factory()->post->create( array( 'post_title' => 'Current story' ) );
$related = self::factory()->post->create( array( 'post_title' => 'Related story' ) );
wp_set_object_terms( $current, 'research', 'chrnx_topic' );
wp_set_object_terms( $related, 'research', 'chrnx_topic' );
$output = $this->render_for_post( 'chrnx/related-posts', $current );
$this->assertStringContainsString( 'chrnx-related-card__fallback', $output );
$this->assertMatchesRegularExpression( '/class="[^"]*chrnx-related-card__fallback[^"]*" aria-hidden="true"/', $output );
$this->assertStringNotContainsString( '<img', $output );
}
/** Journal pagination preserves only validated filters in actual rendered links. */
public function test_journal_pagination_links_preserve_only_valid_bounded_filters() {
$raw_search = 'needle' . str_repeat( 'x', 150 ) . '<b>unsafe</b>';
$bounded_search = mb_substr( trim( sanitize_text_field( $raw_search ) ), 0, 100, 'UTF-8' );
for ( $index = 1; $index <= 12; ++$index ) {
$post_id = self::factory()->post->create(
array(
'post_title' => 'Journal result ' . $index,
'post_content' => $bounded_search,
)
);
wp_set_object_terms( $post_id, 'code', 'chrnx_topic' );
wp_set_object_terms( $post_id, 'es', 'chrnx_language' );
}
$previous_get = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only filter request.
$previous_uri = $_SERVER['REQUEST_URI'] ?? null;
// Synthetic read-only filter request; no state-changing action requires a nonce.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$_GET = array(
'chrnx_topic' => 'code',
'chrnx_language' => 'es',
's' => $raw_search,
'paged' => '3junk',
'evil' => 'drop-me',
'chrnx_show' => 'silicon-ranch',
);
$_SERVER['REQUEST_URI'] = '/journal/?chrnx_topic=code&chrnx_language=es&s=raw&paged=3junk&evil=drop-me&chrnx_show=silicon-ranch';
try {
$output = render_block(
array(
'blockName' => 'chrnx/journal-stream',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
} finally {
$_GET = $previous_get;
if ( null === $previous_uri ) {
unset( $_SERVER['REQUEST_URI'] );
} else {
$_SERVER['REQUEST_URI'] = $previous_uri;
}
}
$this->assertMatchesRegularExpression( '/<nav class="chrnx-journal-stream__pagination".*?<\/nav>/s', $output );
preg_match( '/<nav class="chrnx-journal-stream__pagination".*?<\/nav>/s', $output, $nav_match );
preg_match_all( '/href="([^"]+)"/', $nav_match[0], $href_matches );
$this->assertNotEmpty( $href_matches[1] );
foreach ( $href_matches[1] as $encoded_href ) {
$href = html_entity_decode( $encoded_href, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
parse_str( (string) wp_parse_url( $href, PHP_URL_QUERY ), $query );
$this->assertSame( 'code', $query['chrnx_topic'] ?? '' );
$this->assertSame( 'es', $query['chrnx_language'] ?? '' );
$this->assertSame( $bounded_search, $query['s'] ?? '' );
$this->assertArrayNotHasKey( 'evil', $query );
$this->assertArrayNotHasKey( 'chrnx_show', $query );
$this->assertNotSame( '3junk', $query['paged'] ?? '' );
}
}
/** Plain-permalink Journal links preserve Page routing and only validated public filters. */
public function test_journal_pagination_preserves_plain_page_routing() {
global $wp_rewrite;
$previous_structure = get_option( 'permalink_structure' );
$page_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_status' => 'publish',
'post_title' => 'Journal Page',
)
);
for ( $index = 1; $index <= 12; ++$index ) {
$post_id = self::factory()->post->create( array( 'post_title' => 'Plain result ' . $index ) );
wp_set_object_terms( $post_id, 'code', 'chrnx_topic' );
}
$wp_rewrite->set_permalink_structure( '' );
$this->go_to( get_permalink( $page_id ) );
$previous_get = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only request.
$_GET = array( // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only request.
'chrnx_topic' => 'code',
'evil' => 'drop-me',
);
try {
$output = render_block(
array(
'blockName' => 'chrnx/journal-stream',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
} finally {
$_GET = $previous_get;
$wp_rewrite->set_permalink_structure( $previous_structure );
}
preg_match( '/<nav class="chrnx-journal-stream__pagination".*?href="([^"]+)".*?<\/nav>/s', $output, $match );
$this->assertNotEmpty( $match[1] ?? '' );
$href = html_entity_decode( $match[1], ENT_QUOTES | ENT_HTML5, 'UTF-8' );
parse_str( (string) wp_parse_url( $href, PHP_URL_QUERY ), $query );
$this->assertSame( (string) $page_id, $query['page_id'] ?? '' );
$this->assertSame( '2', (string) ( $query['paged'] ?? '' ) );
$this->assertSame( 'code', $query['chrnx_topic'] ?? '' );
$this->assertArrayNotHasKey( 'evil', $query );
}
/** Multiple block instances produce unique matching aria-labelledby targets. */
public function test_journal_and_related_instances_have_unique_heading_targets() {
$current = self::factory()->post->create( array( 'post_title' => 'Unique current' ) );
$related = self::factory()->post->create( array( 'post_title' => 'Unique related' ) );
wp_set_object_terms( $current, 'life', 'chrnx_topic' );
wp_set_object_terms( $related, 'life', 'chrnx_topic' );
$journal_one = render_block(
array(
'blockName' => 'chrnx/journal-stream',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$journal_two = render_block(
array(
'blockName' => 'chrnx/journal-stream',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$related_one = $this->render_for_post( 'chrnx/related-posts', $current );
$related_two = $this->render_for_post( 'chrnx/related-posts', $current );
$this->assert_unique_label_targets( $journal_one, $journal_two );
$this->assert_unique_label_targets( $related_one, $related_two );
}
/** Journal rendering leaves a nested outer global post context untouched. */
public function test_journal_render_preserves_outer_global_post_context() {
global $post;
$main_page = self::factory()->post->create(
array(
'post_type' => 'page',
'post_status' => 'publish',
)
);
$outer_id = self::factory()->post->create( array( 'post_title' => 'Outer context' ) );
$this->go_to( get_permalink( $main_page ) );
$post = get_post( $outer_id );
setup_postdata( $post );
$context_resets = 0;
$count_reset = static function () use ( &$context_resets ) {
++$context_resets;
};
add_action( 'the_post', $count_reset );
try {
render_block(
array(
'blockName' => 'chrnx/journal-stream',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$this->assertSame( $outer_id, $post->ID );
$this->assertSame( $outer_id, get_the_ID() );
$this->assertSame( 0, $context_resets, 'Journal must not reset or set up global post data.' );
} finally {
remove_action( 'the_post', $count_reset );
wp_reset_postdata();
}
}
/** Assert two rendered sections have matching but distinct label targets. */
private function assert_unique_label_targets( $first, $second ) {
preg_match( '/aria-labelledby="([^"]+)"/', $first, $first_label );
preg_match( '/id="([^"]+)"/', $first, $first_id );
preg_match( '/aria-labelledby="([^"]+)"/', $second, $second_label );
preg_match( '/id="([^"]+)"/', $second, $second_id );
$this->assertSame( $first_label[1] ?? '', $first_id[1] ?? '' );
$this->assertSame( $second_label[1] ?? '', $second_id[1] ?? '' );
$this->assertNotSame( $first_label[1] ?? '', $second_label[1] ?? '' );
}
/** Render the Journal stream block for the current request. */
private function render_journal_stream() {
return render_block(
array(
'blockName' => 'chrnx/journal-stream',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
}
/** Render one dynamic post-context block for a specific post. */
private function render_for_post( $block_name, $post_id ) {
global $post;
$previous = $post;
$post = get_post( $post_id );
setup_postdata( $post );
try {
return render_block(
array(
'blockName' => $block_name,
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
} finally {
wp_reset_postdata();
$post = $previous;
}
}
}