fix: harden CHRNX editor lifecycle

This commit is contained in:
chrnx committed 2026-08-11 15:06:50 -05:00
1 parent a5b94123a8
commit 835fb74ebf
12 files changed
+512 -95

No files matched your search

+2 -1
View File
@@ -84,7 +84,7 @@ class EditorAssetsTest extends WP_UnitTestCase {
);
$this->assertSame( array( 'chrnx-editor-taxonomies', 'chrnx-editor-podcast' ), $handles );
$expected_dependencies = array( 'wp-components', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-plugins' );
$expected_dependencies = array( 'wp-components', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins' );
foreach (
array(
@@ -96,6 +96,7 @@ class EditorAssetsTest extends WP_UnitTestCase {
$this->assertSame( get_theme_file_uri( $asset ), $scripts->registered[ $handle ]->src );
$this->assertSame( $expected_dependencies, $scripts->registered[ $handle ]->deps );
$this->assertSame( CHRNX_VERSION, $scripts->registered[ $handle ]->ver );
$this->assertSame( 'chrnx', $scripts->registered[ $handle ]->textdomain );
}
}
+18 -1
View File
@@ -27,10 +27,14 @@ class PodcastMetaTest extends WP_UnitTestCase {
$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' ),
array(
'type' => 'string',
'maxLength' => 100,
),
$registered['chrnx_episode_participants']['show_in_rest']['schema']['items']
);
$this->assertSame( 'array', $registered['chrnx_episode_participants']['show_in_rest']['schema']['type'] );
$this->assertSame( 50, $registered['chrnx_episode_participants']['show_in_rest']['schema']['maxItems'] );
$this->assertSame( 'string', $registered['canonical_episode_url']['show_in_rest']['schema']['type'] );
$this->assertArrayNotHasKey( 'chrnx_buzzsprout_embed', $registered );
$this->assertArrayNotHasKey( 'chrnx_episode_player', $registered );
@@ -78,6 +82,19 @@ class PodcastMetaTest extends WP_UnitTestCase {
$this->assertSame( array(), chrnx_sanitize_episode_participants( 'Ada Example' ) );
}
/**
* Participant bounds preserve Unicode and intentional order.
*/
public function test_participants_are_bounded_by_count_and_unicode_length() {
$long_name = str_repeat( 'ñ', 101 );
$input = array_merge( array( $long_name ), array_fill( 0, 55, 'Guest' ) );
$actual = chrnx_sanitize_episode_participants( $input );
$this->assertCount( 50, $actual );
$this->assertSame( str_repeat( 'ñ', 100 ), $actual[0] );
$this->assertSame( 100, mb_strlen( $actual[0], 'UTF-8' ) );
}
/**
* Canonical URLs allow HTTPS only.
*/
+171
View File
@@ -22,6 +22,7 @@ class TaxonomiesTest extends WP_UnitTestCase {
public function set_up() {
parent::set_up();
do_action( 'init' );
wp_set_current_user( 0 );
$this->definitions = array(
'chrnx_topic' => array(
'rest_base' => 'topic',
@@ -161,5 +162,175 @@ class TaxonomiesTest extends WP_UnitTestCase {
$this->assertStringContainsString( 'Topic', $notice );
$this->assertStringNotContainsString( 'Zulu', $notice );
$this->assertStringNotContainsString( 'Alpha', $notice );
ob_start();
chrnx_editorial_taxonomy_admin_notice();
$this->assertSame( '', ob_get_clean(), 'The notice should be consumed after one render.' );
}
/**
* A nested assignment for another post in the same taxonomy still normalizes.
*/
public function test_normalization_guard_is_scoped_by_taxonomy_and_object_id() {
$first_post = self::factory()->post->create();
$second_post = self::factory()->post->create();
$first_term = self::factory()->term->create( array( 'taxonomy' => 'chrnx_topic' ) );
$second_term = self::factory()->term->create( array( 'taxonomy' => 'chrnx_topic' ) );
$term_ids = array( min( $first_term, $second_term ), max( $first_term, $second_term ) );
remove_action( 'set_object_terms', 'chrnx_normalize_editorial_taxonomy_terms', 10 );
wp_set_object_terms( $second_post, array_reverse( $term_ids ), 'chrnx_topic' );
add_action( 'set_object_terms', 'chrnx_normalize_editorial_taxonomy_terms', 10, 4 );
$nested = false;
$assign = static function ( $object_id, $terms, $term_taxonomy_ids, $taxonomy ) use ( $first_post, $second_post, $term_ids, &$nested ) {
if ( ! $nested && $first_post === $object_id && 'chrnx_topic' === $taxonomy && 1 === count( (array) $terms ) ) {
$nested = true;
wp_set_object_terms( $second_post, array_reverse( $term_ids ), 'chrnx_topic' );
}
};
add_action( 'set_object_terms', $assign, 20, 4 );
wp_set_object_terms( $first_post, array_reverse( $term_ids ), 'chrnx_topic' );
remove_action( 'set_object_terms', $assign, 20 );
$this->assertTrue( $nested );
$this->assertSame( array( $term_ids[0] ), wp_get_object_terms( $first_post, 'chrnx_topic', array( 'fields' => 'ids' ) ) );
$this->assertSame( array( $term_ids[0] ), wp_get_object_terms( $second_post, 'chrnx_topic', array( 'fields' => 'ids' ) ) );
}
/**
* An exception during correction cannot leave the same-object guard stuck.
*/
public function test_normalization_guard_clears_after_an_exception() {
$post_id = self::factory()->post->create();
$first_term = self::factory()->term->create( array( 'taxonomy' => 'chrnx_topic' ) );
$second_term = self::factory()->term->create( array( 'taxonomy' => 'chrnx_topic' ) );
$term_ids = array( min( $first_term, $second_term ), max( $first_term, $second_term ) );
$throw = static function ( $object_id, $terms, $term_taxonomy_ids, $taxonomy ) use ( $post_id ) {
if ( $post_id === $object_id && 'chrnx_topic' === $taxonomy && 1 === count( (array) $terms ) ) {
throw new RuntimeException( 'Synthetic corrective failure.' );
}
};
add_action( 'set_object_terms', $throw, 5, 4 );
try {
wp_set_object_terms( $post_id, array_reverse( $term_ids ), 'chrnx_topic' );
$this->fail( 'The synthetic corrective failure should escape the nested assignment.' );
} catch ( RuntimeException $exception ) {
$this->assertSame( 'Synthetic corrective failure.', $exception->getMessage() );
} finally {
remove_action( 'set_object_terms', $throw, 5 );
}
remove_action( 'set_object_terms', 'chrnx_normalize_editorial_taxonomy_terms', 10 );
wp_set_object_terms( $post_id, array_reverse( $term_ids ), 'chrnx_topic' );
add_action( 'set_object_terms', 'chrnx_normalize_editorial_taxonomy_terms', 10, 4 );
wp_set_object_terms( $post_id, array_reverse( $term_ids ), 'chrnx_topic' );
$this->assertSame( array( $term_ids[0] ), wp_get_object_terms( $post_id, 'chrnx_topic', array( 'fields' => 'ids' ) ) );
}
/**
* Corrective failures produce only a generic one-time notice.
*/
public function test_corrective_failure_notice_is_generic_and_one_time() {
chrnx_record_editorial_normalization_result( 'chrnx_topic', new WP_Error( 'forced', 'Private detail' ) );
chrnx_record_editorial_normalization_result( 'chrnx_language', false );
ob_start();
chrnx_editorial_taxonomy_admin_notice();
$notice = ob_get_clean();
$this->assertStringContainsString( 'could not be normalized', $notice );
$this->assertStringNotContainsString( 'Private detail', $notice );
$this->assertStringNotContainsString( 'forced', $notice );
ob_start();
chrnx_editorial_taxonomy_admin_notice();
$this->assertSame( '', ob_get_clean() );
}
/**
* Notices cross requests for one user and never leak to another user.
*/
public function test_normalization_notice_is_user_scoped_and_cross_request_one_time() {
$first_user = self::factory()->user->create();
$second_user = self::factory()->user->create();
wp_set_current_user( $first_user );
chrnx_record_editorial_normalization_result( 'chrnx_topic', array( 1 ) );
wp_set_current_user( $second_user );
ob_start();
chrnx_editorial_taxonomy_admin_notice();
$this->assertSame( '', ob_get_clean() );
wp_set_current_user( $first_user );
ob_start();
chrnx_editorial_taxonomy_admin_notice();
$this->assertStringContainsString( 'Topic supports one value', ob_get_clean() );
ob_start();
chrnx_editorial_taxonomy_admin_notice();
$this->assertSame( '', ob_get_clean() );
}
/**
* Term insertion failures create a generic, one-time setup diagnostic.
*/
public function test_term_seeding_failure_creates_generic_one_time_diagnostic() {
$term = get_term_by( 'slug', 'research', 'chrnx_topic' );
if ( $term ) {
wp_delete_term( $term->term_id, 'chrnx_topic' );
}
$fail_insert = static function ( $term_name, $taxonomy ) {
return 'chrnx_topic' === $taxonomy && 'Research' === $term_name
? new WP_Error( 'forced_seed_error', 'Private seed detail' )
: $term_name;
};
add_filter( 'pre_insert_term', $fail_insert, 10, 2 );
do_action( 'after_switch_theme' );
remove_filter( 'pre_insert_term', $fail_insert, 10 );
ob_start();
chrnx_editorial_taxonomy_admin_notice();
$notice = ob_get_clean();
$this->assertStringContainsString( 'vocabulary setup was incomplete', $notice );
$this->assertStringNotContainsString( 'Private seed detail', $notice );
$this->assertStringNotContainsString( 'forced_seed_error', $notice );
ob_start();
chrnx_editorial_taxonomy_admin_notice();
$this->assertSame( '', ob_get_clean() );
}
/**
* Successful term seeding does not create a diagnostic.
*/
public function test_successful_term_seeding_creates_no_notice() {
do_action( 'after_switch_theme' );
ob_start();
chrnx_editorial_taxonomy_admin_notice();
$this->assertSame( '', ob_get_clean() );
}
/**
* Taxonomy labels and seed names use the CHRNX text domain.
*/
public function test_taxonomy_strings_are_localizable() {
$translate = static function ( $translated, $text, $domain ) {
if ( 'chrnx' !== $domain ) {
return $translated;
}
return array(
'Topic' => 'Tema',
'Research' => 'Investigación',
)[ $text ] ?? $translated;
};
add_filter( 'gettext', $translate, 10, 3 );
$definitions = chrnx_get_editorial_taxonomies();
remove_filter( 'gettext', $translate, 10 );
$this->assertSame( 'Tema', $definitions['chrnx_topic']['singular'] );
$this->assertSame( 'Investigación', $definitions['chrnx_topic']['terms']['research'] );
}
}