Merge "Allow callers to load user objects from slaves if desired"
[mediawiki.git] / tests / phpunit / includes / libs / normal / Utf8Test.php
blob6d8457d64005aa4c1217aa5d59a23fc8225cd19b
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
6 * Copyright © 2004 Brion Vibber <brion@pobox.com>
7 * https://www.mediawiki.org/
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.
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.
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
24 * @file
25 * @ingroup UtfNormal
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" );
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;
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 );
50 $ignore = array(
51 # These two lines actually seem to be corrupt
52 '2.1.1', '2.2.1' );
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',
59 # Literal 0xffff, which is illegal
60 '2.2.3' );
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' );
67 $testCases = array();
69 $section = null;
70 while ( false !== ( $line = fgets( $in ) ) ) {
71 $matches = array();
72 if ( preg_match( '/^(\d+)\s+(.*?)\s*\|/', $line, $matches ) ) {
73 continue;
75 if ( preg_match( '/^(\d+\.\d+\.\d+)\s*/', $line, $matches ) ) {
76 $test = $matches[1];
78 if ( in_array( $test, $ignore ) ) {
79 continue;
81 if ( in_array( $test, $longTests ) ) {
82 fgets( $in );
84 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
85 for ( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) {
86 // @codingStandardsIgnoreEnd
88 $testCases[] = array( $test, $line, $columns, $exceptions );
90 } else {
91 $testCases[] = array( $test, $line, $columns, $exceptions );
96 return $testCases;
101 * @dataProvider provideLines
102 * @covers UtfNormal::quickisNFCVerify
104 function testLine( $test, $line, $columns, $exceptions ) {
105 $stripped = $line;
106 UtfNormal::quickisNFCVerify( $stripped );
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, '|' ) ) );
114 $ok = $same ^ ( $test >= 3 );
116 $ok ^= in_array( $test, $exceptions );
118 $ok &= ( $columns == $len );
120 $this->assertEquals( 1, $ok );