From 0e3c612c3be49380cb1b3c94403aa8df67ff0c68 Mon Sep 17 00:00:00 2001 From: Faisal Ahammad Date: Wed, 25 Nov 2026 19:20:34 +0600 Subject: [PATCH] Tests: Cover invalid UTF-8 term names in `wp_insert_term()` and `wp_update_term()`. A term name with invalid UTF-8 characters is sanitized to an empty string. Since [57251], `wp_insert_term()` returns an `invalid_term_name` error in that case instead of creating a blank term, and `wp_update_term()` returns an `empty_term_name` error. These tests lock in that behavior for the case reported in the ticket. See #38243. --- tests/phpunit/tests/term/wpInsertTerm.php | 13 ++++++++++++ tests/phpunit/tests/term/wpUpdateTerm.php | 24 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/tests/phpunit/tests/term/wpInsertTerm.php b/tests/phpunit/tests/term/wpInsertTerm.php index 24a67e0fe5313..5c3925453772f 100644 --- a/tests/phpunit/tests/term/wpInsertTerm.php +++ b/tests/phpunit/tests/term/wpInsertTerm.php @@ -910,6 +910,19 @@ public function test_wp_insert_term_with_empty_name_after_db_sanitization() { $this->assertSame( 'invalid_term_name', $term->get_error_code() ); } + /** + * @ticket 38243 + */ + public function test_wp_insert_term_with_invalid_utf8_name_should_not_create_blank_term() { + $count_before = wp_count_terms( array( 'taxonomy' => 'post_tag' ) ); + + $term = wp_insert_term( urldecode( '360%BF' ), 'post_tag' ); + + $this->assertWPError( $term ); + $this->assertSame( 'invalid_term_name', $term->get_error_code() ); + $this->assertSame( $count_before, wp_count_terms( array( 'taxonomy' => 'post_tag' ) ) ); + } + /** * @ticket 31270 */ diff --git a/tests/phpunit/tests/term/wpUpdateTerm.php b/tests/phpunit/tests/term/wpUpdateTerm.php index eff2dfbbe6852..7c922cfa212fa 100644 --- a/tests/phpunit/tests/term/wpUpdateTerm.php +++ b/tests/phpunit/tests/term/wpUpdateTerm.php @@ -83,6 +83,30 @@ public function test_wp_update_term_name_empty_string() { _unregister_taxonomy( 'wptests_tax' ); } + /** + * @ticket 38243 + */ + public function test_wp_update_term_with_invalid_utf8_name_should_return_wp_error() { + $t = self::factory()->term->create( + array( + 'name' => 'Foo', + 'taxonomy' => 'post_tag', + ) + ); + + $found = wp_update_term( + $t, + 'post_tag', + array( + 'name' => urldecode( '360%BF' ), + ) + ); + + $this->assertWPError( $found ); + $this->assertSame( 'empty_term_name', $found->get_error_code() ); + $this->assertSame( 'Foo', get_term( $t )->name ); + } + /** * @ticket 29614 */