feat: add editorial query blocks
This commit is contained in:
24 files changed
+1421
No files matched your search
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for server-rendered editorial blocks.
|
||||
*
|
||||
* @package CHRNX
|
||||
*/
|
||||
|
||||
/** Verify block registration, fixed art, and safe dynamic rendering. */
|
||||
class BlockRenderingTest extends WP_UnitTestCase {
|
||||
/** Register dependencies and blocks. */
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
do_action( 'init' );
|
||||
chrnx_seed_editorial_terms();
|
||||
delete_option( 'chrnx_subscribe_page_id' );
|
||||
}
|
||||
|
||||
/** All seven block types register from metadata. */
|
||||
public function test_all_editorial_blocks_are_registered() {
|
||||
$names = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta' );
|
||||
foreach ( $names as $name ) {
|
||||
$type = WP_Block_Type_Registry::get_instance()->get_registered( 'chrnx/' . $name );
|
||||
$this->assertInstanceOf( WP_Block_Type::class, $type, $name );
|
||||
$this->assertTrue( is_callable( $type->render_callback ), $name );
|
||||
}
|
||||
}
|
||||
|
||||
/** Topic art is an exact fixed mapping and rejects arbitrary paths. */
|
||||
public function test_topic_art_uses_only_the_fixed_mapping() {
|
||||
$expected = array(
|
||||
'research' => 'alter-ego-research.png',
|
||||
'code' => 'alter-ego-code.png',
|
||||
'literature' => 'alter-ego-literature.png',
|
||||
'music' => 'the-no-names-musician-cutout.png',
|
||||
'podcasts' => 'alter-ego-podcasts.png',
|
||||
'life' => 'alter-ego-life.png',
|
||||
'gaming' => 'alter-ego-gaming.png',
|
||||
);
|
||||
foreach ( $expected as $slug => $filename ) {
|
||||
$this->assertSame( 'assets/images/' . $filename, chrnx_get_topic_art_asset( $slug ) );
|
||||
}
|
||||
foreach ( array( '', 'unknown', '../secret.png', 'assets/images/anything.png' ) as $value ) {
|
||||
$this->assertSame( '', chrnx_get_topic_art_asset( $value ) );
|
||||
}
|
||||
}
|
||||
|
||||
/** Podcast metadata renders known text and HTTPS links, never embed HTML. */
|
||||
public function test_podcast_meta_renders_only_known_escaped_fields() {
|
||||
$post_id = self::factory()->post->create( array( 'post_title' => 'Episode' ) );
|
||||
update_post_meta( $post_id, 'chrnx_episode_number', 42 );
|
||||
update_post_meta( $post_id, 'chrnx_episode_duration', '1:02:03' );
|
||||
update_post_meta( $post_id, 'chrnx_episode_participants', array( 'Ada & Bob', '<script>alert(1)</script>' ) );
|
||||
update_post_meta( $post_id, 'canonical_episode_url', 'https://example.test/episode' );
|
||||
update_post_meta( $post_id, 'chrnx_buzzsprout_embed', '<iframe src="https://evil.test"></iframe>' );
|
||||
|
||||
$output = $this->render_for_post( 'chrnx/podcast-meta', $post_id );
|
||||
$this->assertStringContainsString( '42', $output );
|
||||
$this->assertStringContainsString( '1:02:03', $output );
|
||||
$this->assertStringContainsString( 'Ada & Bob', $output );
|
||||
$this->assertStringContainsString( 'https://example.test/episode', $output );
|
||||
$this->assertStringNotContainsString( '<script', $output );
|
||||
$this->assertStringNotContainsString( '<iframe', $output );
|
||||
$this->assertSame( 1, substr_count( $output, 'https://example.test/episode' ) );
|
||||
}
|
||||
|
||||
/** Invalid/empty metadata is omitted rather than guessed. */
|
||||
public function test_podcast_meta_omits_unknown_or_empty_values() {
|
||||
$post_id = self::factory()->post->create();
|
||||
update_post_meta( $post_id, 'canonical_episode_url', 'http://example.test/episode' );
|
||||
$output = $this->render_for_post( 'chrnx/podcast-meta', $post_id );
|
||||
$this->assertStringNotContainsString( 'http://example.test', $output );
|
||||
$this->assertStringNotContainsString( 'Episode 0', $output );
|
||||
}
|
||||
|
||||
/** Latest podcast is a card/link and never a player. */
|
||||
public function test_latest_podcast_renders_a_linked_card_without_player() {
|
||||
$post_id = self::factory()->post->create( array( 'post_title' => 'Safe Podcast' ) );
|
||||
wp_set_object_terms( $post_id, 'podcasts', 'chrnx_topic' );
|
||||
wp_set_object_terms( $post_id, 'silicon-ranch', 'chrnx_show' );
|
||||
|
||||
$output = render_block(
|
||||
array(
|
||||
'blockName' => 'chrnx/latest-podcast',
|
||||
'attrs' => array(),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
$this->assertStringContainsString( '<article', $output );
|
||||
$this->assertStringContainsString( 'Safe Podcast', $output );
|
||||
$this->assertStringContainsString( esc_url( get_permalink( $post_id ) ), $output );
|
||||
$this->assertStringNotContainsString( '<audio', $output );
|
||||
$this->assertStringNotContainsString( '<iframe', $output );
|
||||
}
|
||||
|
||||
/** Topic hero uses a mapped explicit topic and falls back accessibly if art is absent. */
|
||||
public function test_topic_hero_rejects_paths_and_uses_css_fallback_for_missing_art() {
|
||||
$output = render_block(
|
||||
array(
|
||||
'blockName' => 'chrnx/topic-hero',
|
||||
'attrs' => array(
|
||||
'topic' => 'research',
|
||||
'headingLevel' => 2,
|
||||
),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
$this->assertStringContainsString( '<h2', $output );
|
||||
$this->assertStringContainsString( 'Research', $output );
|
||||
$this->assertStringContainsString( 'aria-hidden="true"', $output );
|
||||
$this->assertStringNotContainsString( '<img', $output );
|
||||
|
||||
$unsafe = render_block(
|
||||
array(
|
||||
'blockName' => 'chrnx/topic-hero',
|
||||
'attrs' => array( 'topic' => '../secret.png' ),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
$this->assertSame( '', $unsafe );
|
||||
}
|
||||
|
||||
/** Subscribe CTA is exactly empty until a published Page is configured. */
|
||||
public function test_subscribe_cta_requires_a_valid_published_page() {
|
||||
$block = array(
|
||||
'blockName' => 'chrnx/subscribe-cta',
|
||||
'attrs' => array(),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
);
|
||||
$this->assertSame( '', render_block( $block ) );
|
||||
|
||||
$page = self::factory()->post->create(
|
||||
array(
|
||||
'post_type' => 'page',
|
||||
'post_status' => 'publish',
|
||||
'post_title' => 'Subscribe',
|
||||
)
|
||||
);
|
||||
update_option( 'chrnx_subscribe_page_id', $page );
|
||||
$output = render_block( $block );
|
||||
$this->assertStringContainsString( '<section', $output );
|
||||
$this->assertStringContainsString( esc_url( get_permalink( $page ) ), $output );
|
||||
}
|
||||
|
||||
/** Featured cards render only real stories and use a CSS image fallback. */
|
||||
public function test_featured_stories_have_semantic_variable_cards_and_no_placeholder_images() {
|
||||
$post_id = self::factory()->post->create( array( 'post_title' => 'Only Story' ) );
|
||||
wp_set_object_terms( $post_id, 'code', 'chrnx_topic' );
|
||||
$output = render_block(
|
||||
array(
|
||||
'blockName' => 'chrnx/featured-stories',
|
||||
'attrs' => array(
|
||||
'limit' => 3,
|
||||
'headingLevel' => 2,
|
||||
),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertSame( 1, substr_count( $output, '<article' ) );
|
||||
$this->assertStringContainsString( 'Only Story', $output );
|
||||
$this->assertStringContainsString( 'aria-hidden="true"', $output );
|
||||
$this->assertStringNotContainsString( 'placeholder', strtolower( $output ) );
|
||||
}
|
||||
|
||||
/** Render one dynamic post-context block for a specific post. */
|
||||
private function render_for_post( $block_name, $post_id ) {
|
||||
global $post;
|
||||
$previous = $post;
|
||||
$post = get_post( $post_id );
|
||||
setup_postdata( $post );
|
||||
try {
|
||||
return render_block(
|
||||
array(
|
||||
'blockName' => $block_name,
|
||||
'attrs' => array(),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
} finally {
|
||||
wp_reset_postdata();
|
||||
$post = $previous;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for editorial query services.
|
||||
*
|
||||
* @package CHRNX
|
||||
*/
|
||||
|
||||
/**
|
||||
* Verify editorial selection and query argument contracts.
|
||||
*/
|
||||
class EditorialQueriesTest extends WP_UnitTestCase {
|
||||
/**
|
||||
* Register and seed editorial taxonomies.
|
||||
*/
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
do_action( 'init' );
|
||||
chrnx_seed_editorial_terms();
|
||||
update_option( 'sticky_posts', array() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a dated post and optionally assign one topic.
|
||||
*
|
||||
* @param string $title Post title.
|
||||
* @param string $date GMT-compatible post date.
|
||||
* @param string $topic Topic slug.
|
||||
* @param array $extra Extra factory arguments.
|
||||
* @return int Post ID.
|
||||
*/
|
||||
private function create_post( $title, $date, $topic = '', $extra = array() ) {
|
||||
$post_id = self::factory()->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;
|
||||
}
|
||||
|
||||
/** 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 = get_posts(
|
||||
chrnx_get_journal_query_args( array() ) + array(
|
||||
'fields' => 'ids',
|
||||
'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 ), get_posts( chrnx_get_show_query_args( 'silicon-ranch' ) + array( 'fields' => 'ids' ) ) );
|
||||
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(), get_posts( $args + array( 'fields' => 'ids' ) ) );
|
||||
}
|
||||
$this->assertNotContains( $outsider, get_posts( chrnx_get_show_query_args( 'silicon-ranch' ) + array( 'fields' => 'ids' ) ) );
|
||||
}
|
||||
|
||||
/** 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( '<script>x</script>', 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 );
|
||||
}
|
||||
|
||||
/** 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 ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for Reading settings.
|
||||
*
|
||||
* @package CHRNX
|
||||
*/
|
||||
|
||||
/** Verify the subscribe page setting and public URL helper. */
|
||||
class SettingsTest extends WP_UnitTestCase {
|
||||
/** Reset state before each test. */
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
delete_option( 'chrnx_subscribe_page_id' );
|
||||
wp_set_current_user( 0 );
|
||||
if ( function_exists( 'chrnx_register_subscribe_setting' ) ) {
|
||||
chrnx_register_subscribe_setting();
|
||||
}
|
||||
}
|
||||
|
||||
/** Setting registration uses an integer sanitizer and a Reading field. */
|
||||
public function test_subscribe_setting_is_registered_on_reading_screen() {
|
||||
global $wp_settings_fields;
|
||||
$this->assertTrue( function_exists( 'chrnx_register_subscribe_setting' ) );
|
||||
$registered = get_registered_settings();
|
||||
|
||||
$this->assertSame( 'integer', $registered['chrnx_subscribe_page_id']['type'] );
|
||||
$this->assertSame( 0, call_user_func( $registered['chrnx_subscribe_page_id']['sanitize_callback'], 12345 ) );
|
||||
$this->assertArrayHasKey( 'chrnx_subscribe_page_id', $wp_settings_fields['reading']['default'] );
|
||||
}
|
||||
|
||||
/** Sanitizer accepts only a page editable by the current user. */
|
||||
public function test_subscribe_setting_sanitizer_requires_an_editable_page() {
|
||||
$admin = self::factory()->user->create( array( 'role' => 'administrator' ) );
|
||||
$page = self::factory()->post->create(
|
||||
array(
|
||||
'post_type' => 'page',
|
||||
'post_status' => 'publish',
|
||||
)
|
||||
);
|
||||
$post = self::factory()->post->create(
|
||||
array(
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish',
|
||||
)
|
||||
);
|
||||
|
||||
wp_set_current_user( $admin );
|
||||
$this->assertSame( $page, chrnx_sanitize_subscribe_page_id( $page ) );
|
||||
$this->assertSame( 0, chrnx_sanitize_subscribe_page_id( $post ) );
|
||||
|
||||
wp_set_current_user( 0 );
|
||||
$this->assertSame( 0, chrnx_sanitize_subscribe_page_id( $page ) );
|
||||
}
|
||||
|
||||
/** Public helper returns only a published configured Page permalink. */
|
||||
public function test_subscribe_url_requires_a_published_page() {
|
||||
$admin = self::factory()->user->create( array( 'role' => 'administrator' ) );
|
||||
$page = self::factory()->post->create(
|
||||
array(
|
||||
'post_type' => 'page',
|
||||
'post_status' => 'publish',
|
||||
)
|
||||
);
|
||||
$this->assertSame( '', chrnx_get_subscribe_url() );
|
||||
|
||||
wp_set_current_user( $admin );
|
||||
update_option( 'chrnx_subscribe_page_id', $page );
|
||||
$this->assertSame( get_permalink( $page ), chrnx_get_subscribe_url() );
|
||||
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => $page,
|
||||
'post_status' => 'draft',
|
||||
)
|
||||
);
|
||||
$this->assertSame( '', chrnx_get_subscribe_url() );
|
||||
wp_delete_post( $page, true );
|
||||
$this->assertSame( '', chrnx_get_subscribe_url() );
|
||||
|
||||
$post = self::factory()->post->create(
|
||||
array(
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish',
|
||||
)
|
||||
);
|
||||
update_option( 'chrnx_subscribe_page_id', $post );
|
||||
$this->assertSame( '', chrnx_get_subscribe_url() );
|
||||
}
|
||||
|
||||
/** Field callback uses the core page dropdown and escaped description. */
|
||||
public function test_subscribe_field_renders_a_core_page_dropdown() {
|
||||
self::factory()->post->create(
|
||||
array(
|
||||
'post_type' => 'page',
|
||||
'post_status' => 'publish',
|
||||
)
|
||||
);
|
||||
ob_start();
|
||||
chrnx_render_subscribe_page_field();
|
||||
$output = ob_get_clean();
|
||||
|
||||
$this->assertMatchesRegularExpression( '/name=[\'\"]chrnx_subscribe_page_id[\'\"]/', $output );
|
||||
$this->assertStringContainsString( '<select', $output );
|
||||
$this->assertStringContainsString( 'description', $output );
|
||||
}
|
||||
}
|
||||
@@ -168,6 +168,49 @@ class TaxonomiesTest extends WP_UnitTestCase {
|
||||
$this->assertSame( '', ob_get_clean(), 'The notice should be consumed after one render.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Appending a second editorial term still enforces the one-term contract.
|
||||
*/
|
||||
public function test_appending_an_editorial_term_normalizes_the_final_assignment() {
|
||||
$post_id = self::factory()->post->create();
|
||||
$first = self::factory()->term->create(
|
||||
array(
|
||||
'taxonomy' => 'chrnx_topic',
|
||||
'name' => 'Append first',
|
||||
)
|
||||
);
|
||||
$second = self::factory()->term->create(
|
||||
array(
|
||||
'taxonomy' => 'chrnx_topic',
|
||||
'name' => 'Append second',
|
||||
)
|
||||
);
|
||||
|
||||
wp_set_object_terms( $post_id, array( $second ), 'chrnx_topic' );
|
||||
wp_set_object_terms( $post_id, array( $first ), 'chrnx_topic', true );
|
||||
|
||||
$this->assertSame(
|
||||
array( min( $first, $second ) ),
|
||||
wp_get_object_terms( $post_id, 'chrnx_topic', array( 'fields' => 'ids' ) )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty assignment and explicit deletion remain empty without synthetic terms.
|
||||
*/
|
||||
public function test_empty_assignment_and_term_deletion_remain_empty() {
|
||||
$post_id = self::factory()->post->create();
|
||||
$term_id = self::factory()->term->create( array( 'taxonomy' => 'chrnx_topic' ) );
|
||||
|
||||
wp_set_object_terms( $post_id, array( $term_id ), 'chrnx_topic' );
|
||||
wp_set_object_terms( $post_id, array(), 'chrnx_topic' );
|
||||
$this->assertSame( array(), wp_get_object_terms( $post_id, 'chrnx_topic', array( 'fields' => 'ids' ) ) );
|
||||
|
||||
wp_set_object_terms( $post_id, array( $term_id ), 'chrnx_topic' );
|
||||
wp_delete_object_term_relationships( $post_id, 'chrnx_topic' );
|
||||
$this->assertSame( array(), wp_get_object_terms( $post_id, 'chrnx_topic', array( 'fields' => 'ids' ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* A nested assignment for another post in the same taxonomy still normalizes.
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user