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

+62
View File
@@ -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 ) );
}
}