Merge "Correct case of Title::getPrefixedDBkey() calls"
[lhc/web/wiklou.git] / tests / phpunit / includes / StringUtilsTest.php
1 <?php
2
3 class StringUtilsTest extends MediaWikiTestCase {
4
5 /**
6 * This test StringUtils::isUtf8 whenever we have mbstring extension
7 * loaded.
8 *
9 * @covers StringUtils::isUtf8
10 * @dataProvider provideStringsForIsUtf8Check
11 */
12 function testIsUtf8WithMbstring( $expected, $string ) {
13 if ( !function_exists( 'mb_check_encoding' ) ) {
14 $this->markTestSkipped( 'Test requires the mbstring PHP extension' );
15 }
16 $this->assertEquals( $expected,
17 StringUtils::isUtf8( $string ),
18 'Testing string "' . $this->escaped( $string ) . '" with mb_check_encoding'
19 );
20 }
21
22 /**
23 * This test StringUtils::isUtf8 making sure we use the pure PHP
24 * implementation used as a fallback when mb_check_encoding() is
25 * not available.
26 *
27 * @covers StringUtils::isUtf8
28 * @dataProvider provideStringsForIsUtf8Check
29 */
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'
34 );
35 }
36
37 /**
38 * Print high range characters as an hexadecimal
39 */
40 function escaped( $string ) {
41 $escaped = '';
42 $length = strlen( $string );
43 for ( $i = 0; $i < $length; $i++ ) {
44 $char = $string[$i];
45 $val = ord( $char );
46 if ( $val > 127 ) {
47 $escaped .= '\x' . dechex( $val );
48 } else {
49 $escaped .= $char;
50 }
51 }
52 return $escaped;
53 }
54
55 /**
56 * See also "UTF-8 decoder capability and stress test" by
57 * Markus Kuhn:
58 * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
59 */
60 function provideStringsForIsUtf8Check() {
61 // Expected return values for StringUtils::isUtf8()
62 $PASS = true;
63 $FAIL = false;
64
65 return array(
66 array( $PASS, 'Some ASCII' ),
67 array( $PASS, "Euro sign €" ),
68
69 # First possible sequences
70 array( $PASS, "\x00" ),
71 array( $PASS, "\xc2\x80" ),
72 array( $PASS, "\xe0\xa0\x80" ),
73 array( $PASS, "\xf0\x90\x80\x80" ),
74 array( $PASS, "\xf8\x88\x80\x80\x80" ),
75 array( $PASS, "\xfc\x84\x80\x80\x80\x80" ),
76
77 # Last possible sequence
78 array( $PASS, "\x7f" ),
79 array( $PASS, "\xdf\xbf" ),
80 array( $PASS, "\xef\xbf\xbf" ),
81 array( $PASS, "\xf7\xbf\xbf\xbf" ),
82 array( $PASS, "\xfb\xbf\xbf\xbf\xbf" ),
83 array( $FAIL, "\xfd\xbf\xbf\xbf\xbf\xbf" ),
84
85 # boundaries:
86 array( $PASS, "\xed\x9f\xbf" ),
87 array( $PASS, "\xee\x80\x80" ),
88 array( $PASS, "\xef\xbf\xbd" ),
89 array( $PASS, "\xf4\x8f\xbf\xbf" ),
90 array( $PASS, "\xf4\x90\x80\x80" ),
91
92 # Malformed
93 array( $FAIL, "\x80" ),
94 array( $FAIL, "\xBF" ),
95 array( $FAIL, "\x80\xbf" ),
96 array( $FAIL, "\x80\xbf\x80" ),
97 array( $FAIL, "\x80\xbf\x80\xbf" ),
98 array( $FAIL, "\x80\xbf\x80\xbf\x80" ),
99 array( $FAIL, "\x80\xbf\x80\xbf\x80\xbf" ),
100 array( $FAIL, "\x80\xbf\x80\xbf\x80\xbf\x80" ),
101
102 # last byte missing
103 array( $FAIL, "\xc0" ),
104 array( $FAIL, "\xe0\x80" ),
105 array( $FAIL, "\xf0\x80\x80" ),
106 array( $FAIL, "\xf8\x80\x80\x80" ),
107 array( $FAIL, "\xfc\x80\x80\x80\x80" ),
108 array( $FAIL, "\xdf" ),
109 array( $FAIL, "\xef\xbf" ),
110 array( $FAIL, "\xf7\xbf\xbf" ),
111 array( $FAIL, "\xfb\xbf\xbf\xbf" ),
112 array( $FAIL, "\xfd\xbf\xbf\xbf\xbf" ),
113
114 # impossible bytes
115 array( $FAIL, "\xfe" ),
116 array( $FAIL, "\xff" ),
117 array( $FAIL, "\xfe\xfe\xff\xff" ),
118
119 /**
120 # The PHP implementation does not handle characters
121 # being represented in a form which is too long :(
122
123 # overlong sequences
124 array( $FAIL, "\xc0\xaf" ),
125 array( $FAIL, "\xe0\x80\xaf" ),
126 array( $FAIL, "\xf0\x80\x80\xaf" ),
127 array( $FAIL, "\xf8\x80\x80\x80\xaf" ),
128 array( $FAIL, "\xfc\x80\x80\x80\x80\xaf" ),
129
130 # Maximum overlong sequences
131 array( $FAIL, "\xc1\xbf" ),
132 array( $FAIL, "\xe0\x9f\xbf" ),
133 array( $FAIL, "\xf0\x8F\xbf\xbf" ),
134 array( $FAIL, "\xf8\x87\xbf\xbf" ),
135 array( $FAIL, "\xfc\x83\xbf\xbf\xbf\xbf" ),
136 **/
137
138 # non characters
139 array( $PASS, "\xef\xbf\xbe" ),
140 array( $PASS, "\xef\xbf\xbf" ),
141 );
142 }
143 }