fix: validate CHRNX editorial fallbacks

This commit is contained in:
chrnx committed 2026-08-11 15:37:25 -05:00
1 parent 89dcc53e02
commit 52f08a1efd
8 files changed
+232 -17

No files matched your search

+103
View File
@@ -172,6 +172,109 @@ class BlockRenderingTest extends WP_UnitTestCase {
$this->assertStringNotContainsString( 'placeholder', strtolower( $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'] ?? '' );
}
}
/** Render one dynamic post-context block for a specific post. */
private function render_for_post( $block_name, $post_id ) {
global $post;