From 2ba32a2d4845de1e245a5d54ab1144a652047798 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Mon, 17 Oct 2016 23:53:20 +0000 Subject: [PATCH] Charset: Allow `_canonical_charset()` to handle mixed-case strings. Add improved unit tests, and collect existing unit tests together. Props pbearne. Fixes #38337. git-svn-id: https://develop.svn.wordpress.org/trunk@38809 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 14 +-- .../tests/functions/canonical-charset.php | 88 +++++++++++++++++++ 2 files changed, 96 insertions(+), 6 deletions(-) create mode 100755 tests/phpunit/tests/functions/canonical-charset.php diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index a434ea5622..989772e765 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -555,7 +555,7 @@ function do_enclose( $content, $post_ID ) { global $wpdb; //TODO: Tidy this ghetto code up and make the debug code optional - include_once( ABSPATH . WPINC . '/class-IXR.php' ); + include_once( ABSPATH . WPINC . '/class-IXR.php' ); $post_links = array(); @@ -5250,13 +5250,15 @@ function get_tag_regex( $tag ) { * @return string The canonical form of the charset. */ function _canonical_charset( $charset ) { - if ( 'UTF-8' === $charset || 'utf-8' === $charset || 'utf8' === $charset || - 'UTF8' === $charset ) - return 'UTF-8'; + if ( 'utf-8' === strtolower( $charset ) || 'utf8' === strtolower( $charset) ) { + + return 'UTF-8'; + } + + if ( 'iso-8859-1' === strtolower( $charset ) || 'iso8859-1' === strtolower( $charset ) ) { - if ( 'ISO-8859-1' === $charset || 'iso-8859-1' === $charset || - 'iso8859-1' === $charset || 'ISO8859-1' === $charset ) return 'ISO-8859-1'; + } return $charset; } diff --git a/tests/phpunit/tests/functions/canonical-charset.php b/tests/phpunit/tests/functions/canonical-charset.php new file mode 100755 index 0000000000..cc6008a6ad --- /dev/null +++ b/tests/phpunit/tests/functions/canonical-charset.php @@ -0,0 +1,88 @@ +assertEquals( 'UTF-8', _canonical_charset( 'utf-8' ) ); + } + + public function test_utf_8_upper() { + $this->assertEquals( 'UTF-8', _canonical_charset( 'UTF-8' ) ); + } + + public function test_utf_8_mixxed() { + $this->assertEquals( 'UTF-8', _canonical_charset( 'Utf-8' ) ); + } + + public function test_utf_8() { + $this->assertEquals( 'UTF-8', _canonical_charset( 'UTF8' ) ); + } + + public function test_iso_lower() { + $this->assertEquals( 'ISO-8859-1', _canonical_charset( 'iso-8859-1' ) ); + } + + public function test_iso_upper() { + $this->assertEquals( 'ISO-8859-1', _canonical_charset( 'ISO-8859-1' ) ); + } + + public function test_iso_mixxed() { + $this->assertEquals( 'ISO-8859-1', _canonical_charset( 'Iso8859-1' ) ); + } + + public function test_iso() { + $this->assertEquals( 'ISO-8859-1', _canonical_charset( 'ISO8859-1' ) ); + } + + public function test_random() { + $this->assertEquals( 'random', _canonical_charset( 'random' ) ); + } + + public function test_empty() { + $this->assertEquals( '', _canonical_charset( '' ) ); + } + + /** + * @ticket 23688 + */ + function test_update_option_blog_charset() { + $orig_blog_charset = get_option( 'blog_charset' ); + + update_option( 'blog_charset', 'utf8' ); + $this->assertEquals( 'UTF-8', get_option( 'blog_charset') ); + + update_option( 'blog_charset', 'utf-8' ); + $this->assertEquals( 'UTF-8', get_option( 'blog_charset') ); + + update_option( 'blog_charset', 'UTF8' ); + $this->assertEquals( 'UTF-8', get_option( 'blog_charset') ); + + update_option( 'blog_charset', 'UTF-8' ); + $this->assertEquals( 'UTF-8', get_option( 'blog_charset') ); + + update_option( 'blog_charset', 'ISO-8859-1' ); + $this->assertEquals( 'ISO-8859-1', get_option( 'blog_charset') ); + + update_option( 'blog_charset', 'ISO8859-1' ); + $this->assertEquals( 'ISO-8859-1', get_option( 'blog_charset') ); + + update_option( 'blog_charset', 'iso8859-1' ); + $this->assertEquals( 'ISO-8859-1', get_option( 'blog_charset') ); + + update_option( 'blog_charset', 'iso-8859-1' ); + $this->assertEquals( 'ISO-8859-1', get_option( 'blog_charset') ); + + // Arbitrary strings are passed through. + update_option( 'blog_charset', 'foobarbaz' ); + $this->assertEquals( 'foobarbaz', get_option( 'blog_charset') ); + + update_option( 'blog_charset', $orig_blog_charset ); + } + +}