From 58737027deb05117bbcb2c7c881e93003bac4fd1 Mon Sep 17 00:00:00 2001 From: Ankit Parmar Date: Wed, 25 Nov 2026 15:12:49 +0530 Subject: [PATCH 1/2] Comments: Avoid a database error in the duplicate check for four-byte characters. When the `comment_content` column uses the legacy `utf8` character set and the connection uses `utf8mb4`, the duplicate comment check in `wp_allow_comment()` failed with a database error if the comment contained four-byte characters such as emoji, for example "Illegal mix of collations (utf8mb3_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,COERCIBLE) for operation '='". Strip the characters the column cannot store from the value being compared, in the same way `wpdb` does when the comment is saved. This avoids the error and means the value compared matches the value that was stored. See #46947. --- src/wp-includes/comment.php | 16 +- .../phpunit/tests/comment/wpAllowComment.php | 145 ++++++++++++++++++ 2 files changed, 160 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 6a1641cbae4d0..52af63f0b8e3a 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -784,9 +784,23 @@ function wp_allow_comment( $commentdata, $wp_error = false ) { wp_unslash( $commentdata['comment_author_email'] ) ); } + $comment_content = wp_unslash( $commentdata['comment_content'] ); + + /* + * Compare against the value that would actually be stored. When the column cannot + * hold four-byte characters, such as emoji in a `utf8` column, they are stripped + * on insert. Comparing the unstripped string would cause a database error + * on a `utf8mb4` connection and would never match the stored comment. + */ + $stripped_content = $wpdb->strip_invalid_text_for_column( $wpdb->comments, 'comment_content', $comment_content ); + + if ( is_string( $stripped_content ) ) { + $comment_content = $stripped_content; + } + $dupe .= $wpdb->prepare( ') AND comment_content = %s LIMIT 1', - wp_unslash( $commentdata['comment_content'] ) + $comment_content ); $dupe_id = $wpdb->get_var( $dupe ); diff --git a/tests/phpunit/tests/comment/wpAllowComment.php b/tests/phpunit/tests/comment/wpAllowComment.php index d2e077e46872d..16d1865339e4c 100644 --- a/tests/phpunit/tests/comment/wpAllowComment.php +++ b/tests/phpunit/tests/comment/wpAllowComment.php @@ -72,6 +72,151 @@ public function test_die_as_duplicate_if_comment_author_name_and_emails_match() $result = wp_allow_comment( $comment_data ); } + /** + * Tests that the duplicate comment check does not trigger a database error + * when the comment contains four-byte characters, whatever the character set + * of the `comment_content` column. + * + * @ticket 46947 + * + * @dataProvider data_comment_content_column_charsets + * + * @param string $charset Character set to convert the comments table to. + * @param string $collation Collation to convert the comments table to. + */ + public function test_four_byte_characters_do_not_cause_a_database_error_in_the_duplicate_check( $charset, $collation ) { + global $wpdb; + + $query_error = null; + $capture = static function ( $dupe_id ) use ( $wpdb, &$query_error ) { + $query_error = $wpdb->last_error; + return $dupe_id; + }; + + add_filter( 'duplicate_comment_id', $capture ); + + $result = $this->with_comments_table_charset( + $charset, + $collation, + function () { + return wp_allow_comment( $this->get_four_byte_comment_data() ); + } + ); + + remove_filter( 'duplicate_comment_id', $capture ); + + $this->assertSame( '', $query_error, 'The duplicate check query should not produce a database error.' ); + $this->assertSame( 1, $result, 'The comment should be allowed.' ); + } + + /** + * Tests that a duplicate comment containing four-byte characters is still detected, + * whatever the character set of the `comment_content` column. + * + * @ticket 46947 + * + * @dataProvider data_comment_content_column_charsets + * + * @param string $charset Character set to convert the comments table to. + * @param string $collation Collation to convert the comments table to. + */ + public function test_duplicate_comment_with_four_byte_characters_is_detected( $charset, $collation ) { + $result = $this->with_comments_table_charset( + $charset, + $collation, + function () { + global $wpdb; + + $comment_data = $this->get_four_byte_comment_data(); + + // Store the comment as it is saved when the column cannot hold four-byte characters. + $stored_data = $comment_data; + $stored_data['comment_content'] = $wpdb->strip_invalid_text_for_column( $wpdb->comments, 'comment_content', $comment_data['comment_content'] ); + + $this->assertNotEmpty( wp_insert_comment( wp_slash( $stored_data ) ), 'The existing comment should be stored.' ); + + return wp_allow_comment( $comment_data, true ); + } + ); + + $this->assertWPError( $result, 'The duplicate comment should be rejected.' ); + $this->assertSame( 'comment_duplicate', $result->get_error_code() ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_comment_content_column_charsets() { + return array( + 'legacy utf8 column' => array( 'utf8', 'utf8_general_ci' ), + 'utf8mb4 column' => array( 'utf8mb4', 'utf8mb4_unicode_ci' ), + ); + } + + /** + * Gets comment data containing four-byte characters. + * + * @return array Comment data. + */ + private function get_four_byte_comment_data() { + return array( + 'comment_post_ID' => self::$post_id, + 'comment_author' => 'Rhonda', + 'comment_author_email' => 'rhonda@example.com', + 'comment_author_url' => '', + 'comment_content' => "Amen And Amen! Thank you Rabbi!\u{2764}\u{1F525}\u{1F4AF}\u{1F64C}", + 'comment_parent' => 0, + 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s' ), + 'comment_author_IP' => '192.168.0.1', + 'comment_agent' => 'WordPress PHPUnit', + 'comment_type' => 'comment', + ); + } + + /** + * Runs a callback with a temporary comments table that uses the given character set. + * + * Skips the test if the database or the collation is not available. + * + * @param string $charset Character set to convert the temporary table to. + * @param string $collation Collation to convert the temporary table to. + * @param callable $callback Callback to run while the temporary table is in use. + * @return mixed The value returned by the callback. + */ + private function with_comments_table_charset( $charset, $collation, $callback ) { + global $wpdb; + + if ( ! $wpdb->is_mysql ) { + $this->markTestSkipped( 'This test requires MySQL or MariaDB.' ); + } + + if ( 'utf8mb4' !== $wpdb->charset ) { + $this->markTestSkipped( 'This test requires a utf8mb4 database connection.' ); + } + + $comments_table = $wpdb->comments; + $test_table = $wpdb->prefix . 'comments_46947_' . $charset; // A unique name per character set avoids stale cached column metadata. + + $wpdb->query( "CREATE TEMPORARY TABLE `$test_table` LIKE `$comments_table`" ); + $wpdb->query( "ALTER TABLE `$test_table` CONVERT TO CHARACTER SET $charset COLLATE $collation" ); + + if ( $wpdb->last_error ) { + $wpdb->query( "DROP TEMPORARY TABLE IF EXISTS `$test_table`" ); + $this->markTestSkipped( "The $collation collation is not available." ); + } + + $wpdb->comments = $test_table; + + try { + return $callback(); + } finally { + $wpdb->comments = $comments_table; + $wpdb->query( "DROP TEMPORARY TABLE IF EXISTS `$test_table`" ); + } + } + /** * @ticket 65016 * From 539ebf31648724092fa95c96e6a434b39638be95 Mon Sep 17 00:00:00 2001 From: Ankit Parmar Date: Wed, 25 Nov 2026 17:10:40 +0530 Subject: [PATCH 2/2] Tests: Ignore interpolated SQL warnings for test table setup. See #46947. --- tests/phpunit/tests/comment/wpAllowComment.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/comment/wpAllowComment.php b/tests/phpunit/tests/comment/wpAllowComment.php index 16d1865339e4c..29e29c489d3d7 100644 --- a/tests/phpunit/tests/comment/wpAllowComment.php +++ b/tests/phpunit/tests/comment/wpAllowComment.php @@ -199,11 +199,11 @@ private function with_comments_table_charset( $charset, $collation, $callback ) $comments_table = $wpdb->comments; $test_table = $wpdb->prefix . 'comments_46947_' . $charset; // A unique name per character set avoids stale cached column metadata. - $wpdb->query( "CREATE TEMPORARY TABLE `$test_table` LIKE `$comments_table`" ); - $wpdb->query( "ALTER TABLE `$test_table` CONVERT TO CHARACTER SET $charset COLLATE $collation" ); + $wpdb->query( "CREATE TEMPORARY TABLE `$test_table` LIKE `$comments_table`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table and character set names come from the test itself. + $wpdb->query( "ALTER TABLE `$test_table` CONVERT TO CHARACTER SET $charset COLLATE $collation" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table and character set names come from the test itself. if ( $wpdb->last_error ) { - $wpdb->query( "DROP TEMPORARY TABLE IF EXISTS `$test_table`" ); + $wpdb->query( "DROP TEMPORARY TABLE IF EXISTS `$test_table`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table and character set names come from the test itself. $this->markTestSkipped( "The $collation collation is not available." ); } @@ -213,7 +213,7 @@ private function with_comments_table_charset( $charset, $collation, $callback ) return $callback(); } finally { $wpdb->comments = $comments_table; - $wpdb->query( "DROP TEMPORARY TABLE IF EXISTS `$test_table`" ); + $wpdb->query( "DROP TEMPORARY TABLE IF EXISTS `$test_table`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table and character set names come from the test itself. } }