Files
chrnx-dev-wp/tests/php/TaxonomiesTest.php
T

380 lines
14 KiB
PHP

<?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' );
wp_set_current_user( 0 );
$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 );
ob_start();
chrnx_editorial_taxonomy_admin_notice();
$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.
*/
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'] );
}
}