fix: bound CHRNX editorial queries

This commit is contained in:
chrnx committed 2026-08-11 15:49:58 -05:00
1 parent 52f08a1efd
commit a181f405bf
5 files changed
+429 -87

No files matched your search

+130
View File
@@ -275,6 +275,136 @@ class BlockRenderingTest extends WP_UnitTestCase {
}
}
/** 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 one dynamic post-context block for a specific post. */
private function render_for_post( $block_name, $post_id ) {
global $post;