fix: bound CHRNX editorial queries
This commit is contained in:
5 files changed
+429
-87
No files matched your search
@@ -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;
|
||||
|
||||
@@ -131,6 +131,21 @@ class EditorialQueriesTest extends WP_UnitTestCase {
|
||||
$this->assertNotContains( $outsider, get_posts( chrnx_get_show_query_args( 'silicon-ranch' ) + array( 'fields' => 'ids' ) ) );
|
||||
}
|
||||
|
||||
/** Show pagination reuses canonical bounded public page parsing. */
|
||||
public function test_show_query_normalizes_invalid_and_extreme_pages() {
|
||||
foreach ( array( '3junk', '0', -2, '3.0', 3.5, array( '3' ), 10001 ) as $invalid ) {
|
||||
try {
|
||||
$args = chrnx_get_show_query_args( 'silicon-ranch', $invalid );
|
||||
} catch ( TypeError $error ) {
|
||||
$this->fail( 'Malformed show pages must normalize to page 1 instead of throwing.' );
|
||||
}
|
||||
$this->assertSame( 1, $args['paged'] );
|
||||
}
|
||||
|
||||
$this->assertSame( 4, chrnx_get_show_query_args( 'silicon-ranch', '4' )['paged'] );
|
||||
$this->assertSame( 10000, chrnx_get_show_query_args( 'silicon-ranch', 10000 )['paged'] );
|
||||
}
|
||||
|
||||
/** Journal filters are term-whitelisted, normalized, and safe WP_Query arguments. */
|
||||
public function test_journal_query_sanitizes_and_preserves_valid_filters() {
|
||||
$args = chrnx_get_journal_query_args(
|
||||
@@ -204,4 +219,51 @@ class EditorialQueriesTest extends WP_UnitTestCase {
|
||||
$this->assertSame( array(), chrnx_get_related_post_ids( $current, 0 ) );
|
||||
$this->assertSame( array(), chrnx_get_related_post_ids( 999999, 3 ) );
|
||||
}
|
||||
|
||||
/** Editorial selectors issue only bounded candidate queries and Journal avoids archive-sized post__in. */
|
||||
public function test_editorial_services_never_request_unbounded_candidate_sets() {
|
||||
$current = $this->create_post( 'Current bounded', '2026-04-30 10:00:00', 'research' );
|
||||
for ( $index = 1; $index <= 30; ++$index ) {
|
||||
$this->create_post( 'Candidate ' . $index, sprintf( '2026-04-%02d 10:00:00', $index ), 0 === $index % 2 ? 'research' : 'code' );
|
||||
}
|
||||
|
||||
$observed = array();
|
||||
$capture = static function ( $query ) use ( &$observed ) {
|
||||
if ( 'post' === $query->get( 'post_type' ) ) {
|
||||
$observed[] = (int) $query->get( 'posts_per_page' );
|
||||
}
|
||||
};
|
||||
add_action( 'pre_get_posts', $capture );
|
||||
try {
|
||||
chrnx_get_featured_story_ids( 4 );
|
||||
chrnx_get_related_post_ids( $current, 4 );
|
||||
} finally {
|
||||
remove_action( 'pre_get_posts', $capture );
|
||||
}
|
||||
|
||||
$this->assertNotEmpty( $observed );
|
||||
$this->assertNotContains( -1, $observed );
|
||||
foreach ( $observed as $candidate_limit ) {
|
||||
$this->assertGreaterThanOrEqual( 1, $candidate_limit );
|
||||
$this->assertLessThanOrEqual( 12, $candidate_limit );
|
||||
}
|
||||
|
||||
$journal_args = chrnx_get_journal_query_args( array() );
|
||||
$this->assertArrayNotHasKey( 'post__in', $journal_args );
|
||||
$this->assertTrue( $journal_args['chrnx_require_nonempty_title'] );
|
||||
}
|
||||
|
||||
/** Identical request-local selections are cached without becoming stale after a post mutation. */
|
||||
public function test_featured_request_cache_reuses_results_and_invalidates_after_post_creation() {
|
||||
$first = $this->create_post( 'Cached first', '2026-05-01 10:00:00', 'research' );
|
||||
$this->assertSame( array( $first ), chrnx_get_featured_story_ids( 1 ) );
|
||||
|
||||
global $wpdb;
|
||||
$before = $wpdb->num_queries;
|
||||
$this->assertSame( array( $first ), chrnx_get_featured_story_ids( 1 ) );
|
||||
$this->assertSame( 0, $wpdb->num_queries - $before, 'A normalized identical request should reuse its request-local selection.' );
|
||||
|
||||
$newer = $this->create_post( 'Cached newer', '2026-05-02 10:00:00', 'code' );
|
||||
$this->assertSame( array( $newer ), chrnx_get_featured_story_ids( 1 ) );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user