feat: add CHRNX editorial metadata
This commit is contained in:
10 files changed
+1127
-1
No files matched your search
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for podcast post metadata.
|
||||
*
|
||||
* @package CHRNX
|
||||
*/
|
||||
|
||||
/**
|
||||
* Verify podcast metadata registration and sanitization.
|
||||
*/
|
||||
class PodcastMetaTest extends WP_UnitTestCase {
|
||||
/**
|
||||
* Register metadata before each test.
|
||||
*/
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
do_action( 'init' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Meta registration is post-only with exact REST schemas.
|
||||
*/
|
||||
public function test_post_meta_has_explicit_rest_schemas_and_no_embed_field() {
|
||||
$registered = get_registered_meta_keys( 'post', 'post' );
|
||||
|
||||
$this->assertSame( 'integer', $registered['chrnx_episode_number']['type'] );
|
||||
$this->assertSame( 0, $registered['chrnx_episode_number']['show_in_rest']['schema']['minimum'] );
|
||||
$this->assertSame( 'string', $registered['chrnx_episode_duration']['show_in_rest']['schema']['type'] );
|
||||
$this->assertSame(
|
||||
array( 'type' => 'string' ),
|
||||
$registered['chrnx_episode_participants']['show_in_rest']['schema']['items']
|
||||
);
|
||||
$this->assertSame( 'array', $registered['chrnx_episode_participants']['show_in_rest']['schema']['type'] );
|
||||
$this->assertSame( 'string', $registered['canonical_episode_url']['show_in_rest']['schema']['type'] );
|
||||
$this->assertArrayNotHasKey( 'chrnx_buzzsprout_embed', $registered );
|
||||
$this->assertArrayNotHasKey( 'chrnx_episode_player', $registered );
|
||||
|
||||
$podcast_keys = array( 'chrnx_episode_number', 'chrnx_episode_duration', 'chrnx_episode_participants', 'canonical_episode_url' );
|
||||
foreach ( $podcast_keys as $key ) {
|
||||
$this->assertTrue( $registered[ $key ]['single'] );
|
||||
$this->assertIsCallable( $registered[ $key ]['sanitize_callback'] );
|
||||
$this->assertIsCallable( $registered[ $key ]['auth_callback'] );
|
||||
}
|
||||
$page_meta = get_registered_meta_keys( 'post', 'page' );
|
||||
foreach ( $podcast_keys as $key ) {
|
||||
$this->assertArrayNotHasKey( $key, $page_meta );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Episode numbers are nonnegative integers.
|
||||
*/
|
||||
public function test_episode_number_sanitizer_clamps_to_zero() {
|
||||
$this->assertSame( 12, chrnx_sanitize_episode_number( '12' ) );
|
||||
$this->assertSame( 0, chrnx_sanitize_episode_number( '-9' ) );
|
||||
$this->assertSame( 0, chrnx_sanitize_episode_number( 'unsafe' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Duration accepts display time only and rejects markup or malformed values.
|
||||
*/
|
||||
public function test_episode_duration_sanitizer_rejects_unsafe_or_malformed_values() {
|
||||
$this->assertSame( '1:18:23', chrnx_sanitize_episode_duration( ' 1:18:23 ' ) );
|
||||
$this->assertSame( '42:05', chrnx_sanitize_episode_duration( '42:05' ) );
|
||||
$this->assertSame( '', chrnx_sanitize_episode_duration( '<b>1:18:23</b>' ) );
|
||||
$this->assertSame( '', chrnx_sanitize_episode_duration( '1:99:23' ) );
|
||||
$this->assertSame( '', chrnx_sanitize_episode_duration( 'soon' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Participant order is preserved while unsafe and empty names are removed.
|
||||
*/
|
||||
public function test_participants_are_sanitized_trimmed_and_ordered() {
|
||||
$this->assertSame(
|
||||
array( 'Ada Example', 'Bob & Sons' ),
|
||||
chrnx_sanitize_episode_participants( array( ' Ada Example ', '', '<b>Bob</b> & Sons', ' ' ) )
|
||||
);
|
||||
$this->assertSame( array(), chrnx_sanitize_episode_participants( 'Ada Example' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Canonical URLs allow HTTPS only.
|
||||
*/
|
||||
public function test_canonical_url_sanitizer_accepts_only_https() {
|
||||
$this->assertSame( 'https://example.test/episode?ref=1', chrnx_sanitize_canonical_episode_url( ' https://example.test/episode?ref=1 ' ) );
|
||||
$this->assertSame( '', chrnx_sanitize_canonical_episode_url( 'http://example.test/episode' ) );
|
||||
$this->assertSame( '', chrnx_sanitize_canonical_episode_url( 'javascript:alert(1)' ) );
|
||||
$this->assertSame( '', chrnx_sanitize_canonical_episode_url( 'not a url' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Authorization follows edit_post for the actual post.
|
||||
*/
|
||||
public function test_meta_authorization_requires_edit_post_capability() {
|
||||
$author_id = self::factory()->user->create( array( 'role' => 'author' ) );
|
||||
$other_id = self::factory()->user->create( array( 'role' => 'author' ) );
|
||||
$post_id = self::factory()->post->create( array( 'post_author' => $author_id ) );
|
||||
$callback = get_registered_meta_keys( 'post', 'post' )['chrnx_episode_number']['auth_callback'];
|
||||
|
||||
wp_set_current_user( $author_id );
|
||||
$this->assertTrue( (bool) call_user_func( $callback, false, 'chrnx_episode_number', $post_id ) );
|
||||
|
||||
wp_set_current_user( $other_id );
|
||||
$this->assertFalse( (bool) call_user_func( $callback, false, 'chrnx_episode_number', $post_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Stored metadata is sanitized and independent of topic assignment.
|
||||
*/
|
||||
public function test_metadata_persists_without_assigning_a_topic() {
|
||||
$post_id = self::factory()->post->create();
|
||||
|
||||
update_post_meta( $post_id, 'chrnx_episode_number', -3 );
|
||||
update_post_meta( $post_id, 'chrnx_episode_duration', '<i>1:02</i>' );
|
||||
update_post_meta( $post_id, 'chrnx_episode_participants', array( ' First ', '<b>Second</b>' ) );
|
||||
update_post_meta( $post_id, 'canonical_episode_url', 'http://example.test' );
|
||||
|
||||
$this->assertSame( '0', get_post_meta( $post_id, 'chrnx_episode_number', true ) );
|
||||
$this->assertSame( '', get_post_meta( $post_id, 'chrnx_episode_duration', true ) );
|
||||
$this->assertSame( array( 'First', 'Second' ), get_post_meta( $post_id, 'chrnx_episode_participants', true ) );
|
||||
$this->assertSame( '', get_post_meta( $post_id, 'canonical_episode_url', true ) );
|
||||
$this->assertSame( array(), wp_get_object_terms( $post_id, 'chrnx_topic', array( 'fields' => 'ids' ) ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for editorial taxonomies.
|
||||
*
|
||||
* @package CHRNX
|
||||
*/
|
||||
|
||||
/**
|
||||
* Verify the editorial taxonomy contract.
|
||||
*/
|
||||
class TaxonomiesTest extends WP_UnitTestCase {
|
||||
/**
|
||||
* Expected taxonomy definitions.
|
||||
*
|
||||
* @var array<string,array<string,mixed>>
|
||||
*/
|
||||
private $definitions;
|
||||
|
||||
/**
|
||||
* Register taxonomies before each test.
|
||||
*/
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
do_action( 'init' );
|
||||
$this->definitions = array(
|
||||
'chrnx_topic' => array(
|
||||
'rest_base' => 'topic',
|
||||
'terms' => array(
|
||||
'research' => 'Research',
|
||||
'code' => 'Code',
|
||||
'literature' => 'Literature',
|
||||
'music' => 'Music',
|
||||
'podcasts' => 'Podcasts',
|
||||
'life' => 'Life',
|
||||
'gaming' => 'Gaming',
|
||||
),
|
||||
),
|
||||
'chrnx_language' => array(
|
||||
'rest_base' => 'language',
|
||||
'terms' => array(
|
||||
'en' => 'English',
|
||||
'es' => 'Español',
|
||||
),
|
||||
),
|
||||
'chrnx_show' => array(
|
||||
'rest_base' => 'show',
|
||||
'terms' => array(
|
||||
'silicon-ranch' => 'Silicon Ranch',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Taxonomies are post-only, public, REST-visible, and stable.
|
||||
*/
|
||||
public function test_taxonomies_have_stable_public_post_only_registration() {
|
||||
$this->assertTrue( function_exists( 'chrnx_register_editorial_taxonomy' ) );
|
||||
$this->assertNotFalse( has_action( 'init', 'chrnx_register_editorial_taxonomies' ) );
|
||||
|
||||
foreach ( $this->definitions as $taxonomy_name => $definition ) {
|
||||
$taxonomy = get_taxonomy( $taxonomy_name );
|
||||
|
||||
$this->assertInstanceOf( WP_Taxonomy::class, $taxonomy );
|
||||
$this->assertSame( array( 'post' ), $taxonomy->object_type );
|
||||
$this->assertTrue( $taxonomy->public );
|
||||
$this->assertTrue( $taxonomy->show_in_rest );
|
||||
$this->assertFalse( $taxonomy->hierarchical );
|
||||
$this->assertSame( $definition['rest_base'], $taxonomy->rest_base );
|
||||
$this->assertSame( $definition['rest_base'], $taxonomy->rewrite['slug'] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The source definitions remain exact and explicit.
|
||||
*/
|
||||
public function test_taxonomy_definitions_include_exact_initial_terms() {
|
||||
$this->assertTrue( function_exists( 'chrnx_get_editorial_taxonomies' ) );
|
||||
$actual = chrnx_get_editorial_taxonomies();
|
||||
|
||||
foreach ( $this->definitions as $taxonomy_name => $definition ) {
|
||||
$this->assertSame( $definition['terms'], $actual[ $taxonomy_name ]['terms'] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Terms are seeded only by theme activation and only when absent.
|
||||
*/
|
||||
public function test_terms_are_seeded_only_on_after_switch_theme_without_overwriting_existing_terms() {
|
||||
$slug = 'silicon-ranch';
|
||||
$term = get_term_by( 'slug', $slug, 'chrnx_show' );
|
||||
if ( $term ) {
|
||||
wp_delete_term( $term->term_id, 'chrnx_show' );
|
||||
}
|
||||
|
||||
do_action( 'init' );
|
||||
$this->assertFalse( get_term_by( 'slug', $slug, 'chrnx_show' ) );
|
||||
|
||||
do_action( 'after_switch_theme' );
|
||||
$seeded = get_term_by( 'slug', $slug, 'chrnx_show' );
|
||||
$this->assertInstanceOf( WP_Term::class, $seeded );
|
||||
$this->assertSame( 'Silicon Ranch', $seeded->name );
|
||||
|
||||
wp_update_term( $seeded->term_id, 'chrnx_show', array( 'name' => 'Existing Name' ) );
|
||||
do_action( 'after_switch_theme' );
|
||||
$this->assertSame( 'Existing Name', get_term( $seeded->term_id, 'chrnx_show' )->name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Missing values remain missing and legacy categories are untouched.
|
||||
*/
|
||||
public function test_missing_metadata_stays_empty_and_categories_are_not_rewritten() {
|
||||
$post_id = self::factory()->post->create();
|
||||
$category_id = self::factory()->category->create( array( 'name' => 'Legacy' ) );
|
||||
wp_set_post_categories( $post_id, array( $category_id ) );
|
||||
|
||||
do_action( 'after_switch_theme' );
|
||||
|
||||
$this->assertSame( array(), wp_get_object_terms( $post_id, 'chrnx_topic', array( 'fields' => 'ids' ) ) );
|
||||
$this->assertSame( array(), wp_get_object_terms( $post_id, 'chrnx_language', array( 'fields' => 'ids' ) ) );
|
||||
$this->assertSame( array(), wp_get_object_terms( $post_id, 'chrnx_show', array( 'fields' => 'ids' ) ) );
|
||||
$this->assertSame( array( $category_id ), wp_get_post_categories( $post_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiple values normalize deterministically and expose a safe notice.
|
||||
*/
|
||||
public function test_multiple_terms_normalize_to_lowest_term_id_without_recursion() {
|
||||
$post_id = self::factory()->post->create();
|
||||
$high = self::factory()->term->create(
|
||||
array(
|
||||
'taxonomy' => 'chrnx_topic',
|
||||
'name' => 'Zulu',
|
||||
)
|
||||
);
|
||||
$low = self::factory()->term->create(
|
||||
array(
|
||||
'taxonomy' => 'chrnx_topic',
|
||||
'name' => 'Alpha',
|
||||
)
|
||||
);
|
||||
$ids = array( min( $low, $high ), max( $low, $high ) );
|
||||
$calls = 0;
|
||||
$counter = static function ( $object_id, $terms, $term_taxonomy_ids, $taxonomy ) use ( $post_id, &$calls ) {
|
||||
if ( $post_id === $object_id && 'chrnx_topic' === $taxonomy ) {
|
||||
++$calls;
|
||||
}
|
||||
};
|
||||
add_action( 'set_object_terms', $counter, 20, 4 );
|
||||
|
||||
wp_set_object_terms( $post_id, array( $ids[1], $ids[0] ), 'chrnx_topic' );
|
||||
remove_action( 'set_object_terms', $counter, 20 );
|
||||
|
||||
$this->assertSame( array( $ids[0] ), wp_get_object_terms( $post_id, 'chrnx_topic', array( 'fields' => 'ids' ) ) );
|
||||
$this->assertLessThanOrEqual( 2, $calls, 'Normalization should make at most one guarded corrective assignment.' );
|
||||
|
||||
ob_start();
|
||||
chrnx_editorial_taxonomy_admin_notice();
|
||||
$notice = ob_get_clean();
|
||||
$this->assertStringContainsString( 'is-dismissible', $notice );
|
||||
$this->assertStringContainsString( 'Topic', $notice );
|
||||
$this->assertStringNotContainsString( 'Zulu', $notice );
|
||||
$this->assertStringNotContainsString( 'Alpha', $notice );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user