fix: harden CHRNX editor lifecycle
This commit is contained in:
12 files changed
+512
-95
No files matched your search
@@ -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'] );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user