3 class StringUtilsTest
extends MediaWikiTestCase
{
6 * This test StringUtils::isUtf8 whenever we have mbstring extension
9 * @cover 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 * @cover 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 for ( $i = 0; $i < strlen( $string ); $i++
) {
46 $escaped .= '\x' . dechex( $val );
55 * See also "UTF-8 decoder capability and stress test" by
57 * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
59 function provideStringsForIsUtf8Check() {
60 // Expected return values for StringUtils::isUtf8()
65 array( $PASS, 'Some ASCII' ),
66 array( $PASS, "Euro sign €" ),
68 # First possible sequences
69 array( $PASS, "\x00" ),
70 array( $PASS, "\xc2\x80" ),
71 array( $PASS, "\xe0\xa0\x80" ),
72 array( $PASS, "\xf0\x90\x80\x80" ),
73 array( $PASS, "\xf8\x88\x80\x80\x80" ),
74 array( $PASS, "\xfc\x84\x80\x80\x80\x80" ),
76 # Last possible sequence
77 array( $PASS, "\x7f" ),
78 array( $PASS, "\xdf\xbf" ),
79 array( $PASS, "\xef\xbf\xbf" ),
80 array( $PASS, "\xf7\xbf\xbf\xbf" ),
81 array( $PASS, "\xfb\xbf\xbf\xbf\xbf" ),
82 array( $FAIL, "\xfd\xbf\xbf\xbf\xbf\xbf" ),
85 array( $PASS, "\xed\x9f\xbf" ),
86 array( $PASS, "\xee\x80\x80" ),
87 array( $PASS, "\xef\xbf\xbd" ),
88 array( $PASS, "\xf4\x8f\xbf\xbf" ),
89 array( $PASS, "\xf4\x90\x80\x80" ),
92 array( $FAIL, "\x80" ),
93 array( $FAIL, "\xBF" ),
94 array( $FAIL, "\x80\xbf" ),
95 array( $FAIL, "\x80\xbf\x80" ),
96 array( $FAIL, "\x80\xbf\x80\xbf" ),
97 array( $FAIL, "\x80\xbf\x80\xbf\x80" ),
98 array( $FAIL, "\x80\xbf\x80\xbf\x80\xbf" ),
99 array( $FAIL, "\x80\xbf\x80\xbf\x80\xbf\x80" ),
102 array( $FAIL, "\xc0" ),
103 array( $FAIL, "\xe0\x80" ),
104 array( $FAIL, "\xf0\x80\x80" ),
105 array( $FAIL, "\xf8\x80\x80\x80" ),
106 array( $FAIL, "\xfc\x80\x80\x80\x80" ),
107 array( $FAIL, "\xdf" ),
108 array( $FAIL, "\xef\xbf" ),
109 array( $FAIL, "\xf7\xbf\xbf" ),
110 array( $FAIL, "\xfb\xbf\xbf\xbf" ),
111 array( $FAIL, "\xfd\xbf\xbf\xbf\xbf" ),
114 array( $FAIL, "\xfe" ),
115 array( $FAIL, "\xff" ),
116 array( $FAIL, "\xfe\xfe\xff\xff" ),
119 # The PHP implementation does not handle characters
120 # being represented in a form which is too long :(
123 array( $FAIL, "\xc0\xaf" ),
124 array( $FAIL, "\xe0\x80\xaf" ),
125 array( $FAIL, "\xf0\x80\x80\xaf" ),
126 array( $FAIL, "\xf8\x80\x80\x80\xaf" ),
127 array( $FAIL, "\xfc\x80\x80\x80\x80\xaf" ),
129 # Maximum overlong sequences
130 array( $FAIL, "\xc1\xbf" ),
131 array( $FAIL, "\xe0\x9f\xbf" ),
132 array( $FAIL, "\xf0\x8F\xbf\xbf" ),
133 array( $FAIL, "\xf8\x87\xbf\xbf" ),
134 array( $FAIL, "\xfc\x83\xbf\xbf\xbf\xbf" ),
138 array( $PASS, "\xef\xbf\xbe" ),
139 array( $PASS, "\xef\xbf\xbf" ),