Merge "DatabaseMssql: Don't duplicate body of makeList()"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / normal / Utf8Test.php
1 <?php
2 /**
3 * Runs the UTF-8 decoder test at:
4 * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
5 *
6 * Copyright © 2004 Brion Vibber <brion@pobox.com>
7 * https://www.mediawiki.org/
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 * @ingroup UtfNormal
26 */
27
28 class Utf8Test extends PHPUnit_Framework_TestCase {
29 public static function provideLines() {
30 global $IP;
31 $in = fopen( "$IP/tests/phpunit/data/normal/UTF-8-test.txt", "rt" );
32
33 $columns = 0;
34 while ( false !== ( $line = fgets( $in ) ) ) {
35 $matches = array();
36 if ( preg_match( '/^(Here come the tests:\s*)\|$/', $line, $matches ) ) {
37 $columns = strpos( $line, '|' );
38 break;
39 }
40 }
41
42 if ( !$columns ) {
43 print "Something seems to be wrong; couldn't extract line length.\n";
44 print "Check that UTF-8-test.txt was downloaded correctly from\n";
45 print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt\n";
46 exit( -1 );
47 }
48
49
50 $ignore = array(
51 # These two lines actually seem to be corrupt
52 '2.1.1', '2.2.1' );
53
54 $exceptions = array(
55 # Tests that should mark invalid characters due to using long
56 # sequences beyond what is now considered legal.
57 '2.1.5', '2.1.6', '2.2.4', '2.2.5', '2.2.6', '2.3.5',
58
59 # Literal 0xffff, which is illegal
60 '2.2.3' );
61
62 $longTests = array(
63 # These tests span multiple lines
64 '3.1.9', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5',
65 '3.4' );
66
67 $testCases = array();
68
69 $section = null;
70 while ( false !== ( $line = fgets( $in ) ) ) {
71 $matches = array();
72 if ( preg_match( '/^(\d+)\s+(.*?)\s*\|/', $line, $matches ) ) {
73 continue;
74 }
75 if ( preg_match( '/^(\d+\.\d+\.\d+)\s*/', $line, $matches ) ) {
76 $test = $matches[1];
77
78 if ( in_array( $test, $ignore ) ) {
79 continue;
80 }
81 if ( in_array( $test, $longTests ) ) {
82 fgets( $in );
83
84 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
85 for ( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) {
86 // @codingStandardsIgnoreEnd
87
88 $testCases[] = array( $test, $line, $columns, $exceptions );
89 }
90 } else {
91 $testCases[] = array( $test, $line, $columns, $exceptions );
92 }
93 }
94 }
95
96 return $testCases;
97 }
98
99
100 /**
101 * @dataProvider provideLines
102 * @covers UtfNormal::quickisNFCVerify
103 */
104 function testLine( $test, $line, $columns, $exceptions ) {
105 $stripped = $line;
106 UtfNormal::quickisNFCVerify( $stripped );
107
108 $same = ( $line == $stripped );
109 $len = mb_strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
110 if ( $len == 0 ) {
111 $len = strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
112 }
113
114 $ok = $same ^ ( $test >= 3 );
115
116 $ok ^= in_array( $test, $exceptions );
117
118 $ok &= ( $columns == $len );
119
120 $this->assertEquals( 1, $ok );
121 }
122
123 }