post->create(
array_merge(
array(
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'post',
'post_date' => $date,
),
$extra
)
);
if ( '' !== $topic ) {
wp_set_object_terms( $post_id, $topic, 'chrnx_topic' );
}
return $post_id;
}
/** Execute an eligible query and return IDs, using the scoped wrapper when available. */
private function get_eligible_query_ids( $args ) {
$args['fields'] = 'ids';
if ( function_exists( 'chrnx_run_eligible_query' ) ) {
return array_map( 'absint', chrnx_run_eligible_query( $args )->posts );
}
return array_map( 'absint', get_posts( $args ) );
}
/** Newest eligible sticky is primary and older stickies rejoin chronology. */
public function test_featured_primary_is_newest_eligible_sticky_and_other_stickies_rejoin_chronology() {
$old_sticky = $this->create_post( 'Old sticky', '2026-01-01 10:00:00', 'research' );
$regular = $this->create_post( 'Regular', '2026-01-03 10:00:00', 'code' );
$new_sticky = $this->create_post( 'New sticky', '2026-01-02 10:00:00', 'life' );
update_option( 'sticky_posts', array( $old_sticky, $new_sticky ) );
$this->assertSame( array( $new_sticky, $regular, $old_sticky ), chrnx_get_featured_story_ids( 3 ) );
}
/** Secondary selection maximizes distinct topics before filling by date. */
public function test_featured_secondaries_prefer_distinct_topics_then_fill_by_date() {
$primary = $this->create_post( 'Primary', '2026-01-05 10:00:00', 'research' );
$same_new = $this->create_post( 'Same newest', '2026-01-04 10:00:00', 'research' );
$distinct_new = $this->create_post( 'Code', '2026-01-03 10:00:00', 'code' );
$same_distinct = $this->create_post( 'Code again', '2026-01-02 10:00:00', 'code' );
$distinct_old = $this->create_post( 'Life', '2026-01-01 10:00:00', 'life' );
update_option( 'sticky_posts', array( $primary ) );
$this->assertSame( array( $primary, $distinct_new, $distinct_old, $same_new ), chrnx_get_featured_story_ids( 4 ) );
$this->assertNotContains( $same_distinct, array_slice( chrnx_get_featured_story_ids( 4 ), 0, 3 ) );
}
/** Eligibility excludes anything not suitable for a public story card. */
public function test_featured_excludes_ineligible_content_and_never_returns_placeholders() {
$eligible = $this->create_post( 'Eligible', '2026-01-08 10:00:00' );
$this->create_post( 'Draft', '2026-01-09 10:00:00', '', array( 'post_status' => 'draft' ) );
$this->create_post( 'Private', '2026-01-10 10:00:00', '', array( 'post_status' => 'private' ) );
$this->create_post( 'Page', '2026-01-11 10:00:00', '', array( 'post_type' => 'page' ) );
$this->create_post( 'Trash', '2026-01-12 10:00:00', '', array( 'post_status' => 'trash' ) );
$this->create_post( ' ', '2026-01-13 10:00:00' );
$this->assertSame( array( $eligible ), chrnx_get_featured_story_ids( 8 ) );
}
/** No-sticky fallback and ties are deterministic; invalid limits are bounded. */
public function test_featured_falls_back_to_newest_with_deterministic_ties_and_bounded_limits() {
$older = $this->create_post( 'Older', '2026-01-01 10:00:00' );
$tie_a = $this->create_post( 'Tie A', '2026-01-02 10:00:00' );
$tie_b = $this->create_post( 'Tie B', '2026-01-02 10:00:00' );
$actual = chrnx_get_featured_story_ids( 3 );
$this->assertSame( array( max( $tie_a, $tie_b ), min( $tie_a, $tie_b ), $older ), $actual );
$this->assertSame( array(), chrnx_get_featured_story_ids( 0 ) );
$this->assertLessThanOrEqual( 12, count( chrnx_get_featured_story_ids( 999 ) ) );
}
/** Podcast surfaces require both the podcast topic and an assigned show. */
public function test_latest_podcast_requires_topic_and_any_registered_show() {
$without_show = $this->create_post( 'Podcast without show', '2026-02-03 10:00:00', 'podcasts' );
$with_show = $this->create_post( 'Podcast with show', '2026-02-02 10:00:00', 'podcasts' );
$wrong_topic = $this->create_post( 'Show but not podcast', '2026-02-04 10:00:00', 'code' );
$empty_title = $this->create_post( ' ', '2026-02-05 10:00:00', 'podcasts' );
wp_set_object_terms( $with_show, 'silicon-ranch', 'chrnx_show' );
wp_set_object_terms( $wrong_topic, 'silicon-ranch', 'chrnx_show' );
wp_set_object_terms( $empty_title, 'silicon-ranch', 'chrnx_show' );
$this->assertSame( $with_show, chrnx_get_latest_podcast_id() );
$journal_ids = $this->get_eligible_query_ids(
chrnx_get_journal_query_args( array() ) + array(
'posts_per_page' => 20,
)
);
$this->assertContains( $without_show, $journal_ids );
$this->assertNotContains( $empty_title, $journal_ids );
}
/** Show queries fail closed unless the slug names a registered existing show. */
public function test_show_query_validates_slug_and_silicon_ranch_membership() {
$member = $this->create_post( 'Member', '2026-02-02 10:00:00', 'podcasts' );
$outsider = $this->create_post( 'Outsider', '2026-02-03 10:00:00', 'podcasts' );
$untitled = $this->create_post( ' ', '2026-02-04 10:00:00', 'podcasts' );
wp_set_object_terms( $member, 'silicon-ranch', 'chrnx_show' );
wp_set_object_terms( $untitled, 'silicon-ranch', 'chrnx_show' );
$this->assertSame( array( $member ), $this->get_eligible_query_ids( chrnx_get_show_query_args( 'silicon-ranch' ) ) );
foreach ( array( 'unknown', '../silicon-ranch', '', "silicon-ranch' OR 1=1" ) as $slug ) {
$args = chrnx_get_show_query_args( $slug );
$this->assertSame( array( 0 ), $args['post__in'] );
$this->assertSame( array(), $this->get_eligible_query_ids( $args ) );
}
$this->assertNotContains( $outsider, $this->get_eligible_query_ids( chrnx_get_show_query_args( 'silicon-ranch' ) ) );
}
/** 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(
array(
'chrnx_topic' => 'code',
'chrnx_language' => 'es',
'paged' => '3',
's' => " hello\nworld ",
)
);
$this->assertSame( 'post', $args['post_type'] );
$this->assertSame( 'publish', $args['post_status'] );
$this->assertSame(
array(
'date' => 'DESC',
'ID' => 'DESC',
),
$args['orderby']
);
$this->assertSame( 3, $args['paged'] );
$this->assertSame( 'hello world', $args['s'] );
$this->assertCount( 2, $args['tax_query'] );
$this->assertSame( 'code', $args['tax_query'][0]['terms'] );
$this->assertSame( 'es', $args['tax_query'][1]['terms'] );
}
/** Malformed journal request values are ignored and never copied into query args. */
public function test_journal_query_ignores_malformed_filters_and_caps_search() {
$args = chrnx_get_journal_query_args(
array(
'chrnx_topic' => '../code',
'chrnx_language' => array( 'es' ),
'paged' => '-200',
's' => str_repeat( '', 50 ),
)
);
$this->assertArrayNotHasKey( 'tax_query', $args );
$this->assertSame( 1, $args['paged'] );
$search = $args['s'] ?? '';
$this->assertLessThanOrEqual( 100, mb_strlen( $search, 'UTF-8' ) );
$this->assertStringNotContainsString( '<', $search );
}
/** Journal pages accept only canonical positive integers within the public bound. */
public function test_journal_query_rejects_noncanonical_or_out_of_range_pages() {
foreach ( array( '3junk', '0', 0, '-2', -2, '3.0', 3.0, '+3', '03', array( '3' ), '10001', 10001 ) as $invalid ) {
$args = chrnx_get_journal_query_args( array( 'paged' => $invalid ) );
$this->assertSame( 1, $args['paged'], 'Invalid page values must fail closed.' );
}
$this->assertSame( 3, chrnx_get_journal_query_args( array( 'paged' => '3' ) )['paged'] );
$this->assertSame( 10000, chrnx_get_journal_query_args( array( 'paged' => '10000' ) )['paged'] );
$this->assertSame( 10000, chrnx_get_journal_query_args( array( 'paged' => 10000 ) )['paged'] );
}
/** Related stories tier by topic, then tags, then recency without duplicates. */
public function test_related_posts_use_ordered_deduplicated_tiers_and_eligibility() {
$current = $this->create_post( 'Current', '2026-03-05 10:00:00', 'research' );
$same_topic = $this->create_post( 'Same topic', '2026-03-01 10:00:00', 'research' );
$shared_tag = $this->create_post( 'Shared tag', '2026-03-04 10:00:00', 'code' );
$recent = $this->create_post( 'Recent', '2026-03-03 10:00:00', 'life' );
$tag_id = self::factory()->tag->create();
wp_set_post_tags( $current, array( $tag_id ) );
wp_set_post_tags( $same_topic, array( $tag_id ) );
wp_set_post_tags( $shared_tag, array( $tag_id ) );
$this->create_post( 'Draft related', '2026-03-06 10:00:00', 'research', array( 'post_status' => 'draft' ) );
$this->assertSame( array( $same_topic, $shared_tag, $recent ), chrnx_get_related_post_ids( $current, 3 ) );
$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 ) );
}
/** Tag-only titles are ineligible while marked-up titles with visible text remain eligible. */
public function test_tag_only_titles_are_excluded_from_featured_and_related_surfaces() {
$tag_only = $this->create_post( '', '2026-06-05 10:00:00', 'research' );
$nested = $this->create_post( ' ', '2026-06-04 10:00:00', 'code' );
$visible = $this->create_post( 'Visible story', '2026-06-03 10:00:00', 'life' );
update_option( 'sticky_posts', array( $tag_only, $nested ) );
$this->assertSame( array( $visible ), chrnx_get_featured_story_ids( 1 ) );
$current = $this->create_post( 'Current tags', '2026-06-06 10:00:00', 'research' );
$this->assertSame( array( $visible ), chrnx_get_related_post_ids( $current, 1 ) );
}
/** Journal and Show counts exclude tag-only titles without hiding visible marked-up titles. */
public function test_tag_only_titles_are_excluded_from_journal_and_show_queries() {
$tag_only = $this->create_post( '', '2026-07-03 10:00:00', 'podcasts' );
$visible = $this->create_post( 'Visible episode', '2026-07-02 10:00:00', 'podcasts' );
wp_set_object_terms( $tag_only, 'silicon-ranch', 'chrnx_show' );
wp_set_object_terms( $visible, 'silicon-ranch', 'chrnx_show' );
$journal = function_exists( 'chrnx_run_eligible_query' )
? chrnx_run_eligible_query( chrnx_get_journal_query_args( array() ) )
: new WP_Query( chrnx_get_journal_query_args( array() ) );
$this->assertSame( array( $visible ), array_map( 'absint', wp_list_pluck( $journal->posts, 'ID' ) ) );
$this->assertSame( 1, (int) $journal->found_posts );
$this->assertSame( array( $visible ), $this->get_eligible_query_ids( chrnx_get_show_query_args( 'silicon-ranch' ) ) );
}
/** Latest Podcast excludes tag-only titles and retains marked-up visible titles. */
public function test_tag_only_titles_are_excluded_from_latest_podcast() {
$visible = $this->create_post( 'Visible podcast', '2026-08-01 10:00:00', 'podcasts' );
$tag_only = $this->create_post( ' ', '2026-08-02 10:00:00', 'podcasts' );
wp_set_object_terms( $visible, 'silicon-ranch', 'chrnx_show' );
wp_set_object_terms( $tag_only, 'silicon-ranch', 'chrnx_show' );
$this->assertSame( $visible, chrnx_get_latest_podcast_id() );
}
/** Eligible SQL filtering is absent outside the focused wrapper, including exceptions. */
public function test_eligible_title_filter_is_scoped_and_removed_on_success_or_exception() {
$this->assertFalse( has_filter( 'posts_where', 'chrnx_filter_nonempty_editorial_titles' ) );
$this->assertTrue( function_exists( 'chrnx_run_eligible_query' ) );
if ( ! function_exists( 'chrnx_run_eligible_query' ) ) {
return;
}
chrnx_run_eligible_query( chrnx_get_journal_query_args( array() ) );
$this->assertFalse( has_filter( 'posts_where', 'chrnx_filter_nonempty_editorial_titles' ) );
$throw = static function () {
throw new RuntimeException( 'Synthetic query failure.' );
};
add_filter( 'posts_pre_query', $throw );
try {
chrnx_run_eligible_query( chrnx_get_journal_query_args( array() ) );
$this->fail( 'The synthetic query failure should escape.' );
} catch ( RuntimeException $exception ) {
$this->assertSame( 'Synthetic query failure.', $exception->getMessage() );
} finally {
remove_filter( 'posts_pre_query', $throw );
}
$this->assertFalse( has_filter( 'posts_where', 'chrnx_filter_nonempty_editorial_titles' ) );
$unmarked = new WP_Query();
$this->assertSame( 'WHERE 1=1', chrnx_filter_nonempty_editorial_titles( 'WHERE 1=1', $unmarked ) );
}
/** Deleted show, topic, and tag relationships invalidate cached editorial selections. */
public function test_deleted_relationships_invalidate_podcast_topic_and_tag_caches() {
$podcast = $this->create_post( 'Cached podcast', '2026-07-01 10:00:00', 'podcasts' );
wp_set_object_terms( $podcast, 'silicon-ranch', 'chrnx_show' );
$this->assertSame( $podcast, chrnx_get_latest_podcast_id() );
wp_delete_object_term_relationships( $podcast, 'chrnx_show' );
$this->assertSame( 0, chrnx_get_latest_podcast_id() );
$current = $this->create_post( 'Relationship current', '2026-07-05 10:00:00', 'research' );
$topic_match = $this->create_post( 'Topic match', '2026-07-01 10:00:00', 'research' );
$recent_other = $this->create_post( 'Recent other', '2026-07-04 10:00:00', 'code' );
$this->assertSame( array( $topic_match ), chrnx_get_related_post_ids( $current, 1 ) );
wp_delete_object_term_relationships( $topic_match, 'chrnx_topic' );
$this->assertSame( array( $recent_other ), chrnx_get_related_post_ids( $current, 1 ) );
$tag_match = $this->create_post( 'Tag match', '2026-07-02 10:00:00', 'life' );
$tag_id = self::factory()->tag->create();
wp_set_post_tags( $current, array( $tag_id ) );
wp_set_post_tags( $tag_match, array( $tag_id ) );
$this->assertSame( array( $tag_match ), chrnx_get_related_post_ids( $current, 1 ) );
wp_delete_object_term_relationships( $tag_match, 'post_tag' );
$this->assertSame( array( $recent_other ), chrnx_get_related_post_ids( $current, 1 ) );
}
}