3 class StringUtilsTest
extends MediaWikiTestCase
{
6 * This test StringUtils::isUtf8 whenever we have mbstring extension
9 * @covers StringUtils::isUtf8
10 * @dataProvider provideStringsForIsUtf8Check
12 function testIsUtf8WithMbstring( $expected, $string ) {
13 if ( !function_exists( 'mb_check_encoding' ) ) {
14 $this->markTestSkipped( 'Test requires the mbstring PHP extension' );
16 $this->assertEquals( $expected,
17 StringUtils
::isUtf8( $string ),
18 'Testing string "' . $this->escaped( $string ) . '" with mb_check_encoding'
23 * This test StringUtils::isUtf8 making sure we use the pure PHP
24 * implementation used as a fallback when mb_check_encoding() is
27 * @covers StringUtils::isUtf8
28 * @dataProvider provideStringsForIsUtf8Check
30 function testIsUtf8WithPhpFallbackImplementation( $expected, $string ) {
31 $this->assertEquals( $expected,
32 StringUtils
::isUtf8( $string, /** disable mbstring: */true ),
33 'Testing string "' . $this->escaped( $string ) . '" with pure PHP implementation'
38 * Print high range characters as an hexadecimal
40 function escaped( $string ) {
42 $length = strlen( $string );
43 for ( $i = 0; $i < $length; $i++
) {
47 $escaped .= '\x' . dechex( $val );
57 * See also "UTF-8 decoder capability and stress test" by
59 * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
61 public static function provideStringsForIsUtf8Check() {
62 // Expected return values for StringUtils::isUtf8()
67 array( $PASS, 'Some ASCII' ),
68 array( $PASS, "Euro sign €" ),
70 // First possible sequences
71 array( $PASS, "\x00" ),
72 array( $PASS, "\xc2\x80" ),
73 array( $PASS, "\xe0\xa0\x80" ),
74 array( $PASS, "\xf0\x90\x80\x80" ),
75 array( $FAIL, "\xf8\x88\x80\x80\x80" ),
76 array( $FAIL, "\xfc\x84\x80\x80\x80\x80" ),
78 // Last possible sequence
79 array( $PASS, "\x7f" ),
80 array( $PASS, "\xdf\xbf" ),
81 array( $PASS, "\xef\xbf\xbf" ),
82 array( $FAIL, "\xf7\xbf\xbf\xbf" ), // U+1FFFFF
83 array( $FAIL, "\xfb\xbf\xbf\xbf\xbf" ),
84 array( $FAIL, "\xfd\xbf\xbf\xbf\xbf\xbf" ),
87 array( $PASS, "\xed\x9f\xbf" ),
88 array( $PASS, "\xee\x80\x80" ),
89 array( $PASS, "\xef\xbf\xbd" ),
90 array( $PASS, "\xf2\x80\x80\x80" ),
91 array( $PASS, "\xf3\xbf\xbf\xbf" ), // U+FFFFF
92 array( $PASS, "\xf4\x80\x80\x80" ), // U+100000
93 array( $PASS, "\xf4\x8f\xbf\xbf" ), // U+10FFFF
94 array( $FAIL, "\xf4\x90\x80\x80" ), // U+110000
97 array( $FAIL, "\x80" ),
98 array( $FAIL, "\xbf" ),
99 array( $FAIL, "\x80\xbf" ),
100 array( $FAIL, "\x80\xbf\x80" ),
101 array( $FAIL, "\x80\xbf\x80\xbf" ),
102 array( $FAIL, "\x80\xbf\x80\xbf\x80" ),
103 array( $FAIL, "\x80\xbf\x80\xbf\x80\xbf" ),
104 array( $FAIL, "\x80\xbf\x80\xbf\x80\xbf\x80" ),
107 array( $FAIL, "\xc0" ),
108 array( $FAIL, "\xe0\x80" ),
109 array( $FAIL, "\xf0\x80\x80" ),
110 array( $FAIL, "\xf8\x80\x80\x80" ),
111 array( $FAIL, "\xfc\x80\x80\x80\x80" ),
112 array( $FAIL, "\xdf" ),
113 array( $FAIL, "\xef\xbf" ),
114 array( $FAIL, "\xf7\xbf\xbf" ),
115 array( $FAIL, "\xfb\xbf\xbf\xbf" ),
116 array( $FAIL, "\xfd\xbf\xbf\xbf\xbf" ),
118 // Extra continuation byte
119 array( $FAIL, "e\xaf" ),
120 array( $FAIL, "\xc3\x89\xaf" ),
121 array( $FAIL, "\xef\xbc\xa5\xaf" ),
122 array( $FAIL, "\xf0\x9d\x99\xb4\xaf" ),
125 array( $FAIL, "\xfe" ),
126 array( $FAIL, "\xff" ),
127 array( $FAIL, "\xfe\xfe\xff\xff" ),
129 // Overlong sequences
130 array( $FAIL, "\xc0\xaf" ),
131 array( $FAIL, "\xc1\xaf" ),
132 array( $FAIL, "\xe0\x80\xaf" ),
133 array( $FAIL, "\xf0\x80\x80\xaf" ),
134 array( $FAIL, "\xf8\x80\x80\x80\xaf" ),
135 array( $FAIL, "\xfc\x80\x80\x80\x80\xaf" ),
137 // Maximum overlong sequences
138 array( $FAIL, "\xc1\xbf" ),
139 array( $FAIL, "\xe0\x9f\xbf" ),
140 array( $FAIL, "\xf0\x8f\xbf\xbf" ),
141 array( $FAIL, "\xf8\x87\xbf\xbf" ),
142 array( $FAIL, "\xfc\x83\xbf\xbf\xbf\xbf" ),
145 array( $PASS, "\xed\x9f\xbf" ), // U+D799
146 array( $PASS, "\xee\x80\x80" ), // U+E000
147 array( $FAIL, "\xed\xa0\x80" ), // U+D800
148 array( $FAIL, "\xed\xaf\xbf" ), // U+DBFF
149 array( $FAIL, "\xed\xb0\x80" ), // U+DC00
150 array( $FAIL, "\xed\xbf\xbf" ), // U+DFFF
151 array( $FAIL, "\xed\xa0\x80\xed\xb0\x80" ), // U+D800 U+DC00
154 array( $PASS, "\xef\xbf\xbe" ),
155 array( $PASS, "\xef\xbf\xbf" ),