Convert UtfNormalTest into a PHPUnit test
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / normal / UtfNormalTest.php
1 <?php
2 /**
3 * Implements the conformance test at:
4 * http://www.unicode.org/Public/UNIDATA/NormalizationTest.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 * @group UtfNormal
26 * @group large
27 */
28
29 class UtfNormalTest extends PHPUnit_Framework_TestCase {
30
31 protected static $testedChars = array();
32
33 public static function provideNormalizationTest() {
34 global $IP;
35 $in = fopen( "$IP/tests/phpunit/data/normal/NormalizationTest.txt", "rt" );
36
37 $testCases = array();
38 while ( false !== ( $line = fgets( $in ) ) ) {
39 list( $data, $comment ) = explode( '#', $line );
40 if ( $data === '' ) continue;
41 $matches = array();
42 if ( preg_match( '/@Part([\d])/', $data, $matches ) ) {
43 continue;
44 }
45
46 $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
47 array_unshift( $columns, '' );
48
49 self::$testedChars[$columns[1]] = true;
50 $testCases[] = array( $columns, $comment );
51 }
52 fclose( $in );
53
54 return array( array( $testCases ) );
55 }
56
57 function assertStringEquals( $a, $b, $desc ) {
58 $this->assertEquals( 0, strcmp( $a, $b ), $desc );
59 }
60
61 function assertNFC( $c, $desc ) {
62 $this->assertStringEquals( $c[2], UtfNormal::toNFC( $c[1] ), $desc );
63 $this->assertStringEquals( $c[2], UtfNormal::toNFC( $c[2] ), $desc );
64 $this->assertStringEquals( $c[2], UtfNormal::toNFC( $c[3] ), $desc );
65 $this->assertStringEquals( $c[4], UtfNormal::toNFC( $c[4] ), $desc );
66 $this->assertStringEquals( $c[4], UtfNormal::toNFC( $c[5] ), $desc );
67 }
68
69 function assertNFD( $c, $desc ) {
70 $this->assertStringEquals( $c[3], UtfNormal::toNFD( $c[1] ), $desc );
71 $this->assertStringEquals( $c[3], UtfNormal::toNFD( $c[2] ), $desc );
72 $this->assertStringEquals( $c[3], UtfNormal::toNFD( $c[3] ), $desc );
73 $this->assertStringEquals( $c[5], UtfNormal::toNFD( $c[4] ), $desc );
74 $this->assertStringEquals( $c[5], UtfNormal::toNFD( $c[5] ), $desc );
75 }
76
77 function assertNFKC( $c, $desc ) {
78 $this->assertStringEquals( $c[4], UtfNormal::toNFKC( $c[1] ), $desc );
79 $this->assertStringEquals( $c[4], UtfNormal::toNFKC( $c[2] ), $desc );
80 $this->assertStringEquals( $c[4], UtfNormal::toNFKC( $c[3] ), $desc );
81 $this->assertStringEquals( $c[4], UtfNormal::toNFKC( $c[4] ), $desc );
82 $this->assertStringEquals( $c[4], UtfNormal::toNFKC( $c[5] ), $desc );
83 }
84
85 function assertNFKD( $c, $desc ) {
86 $this->assertStringEquals( $c[5], UtfNormal::toNFKD( $c[1] ), $desc );
87 $this->assertStringEquals( $c[5], UtfNormal::toNFKD( $c[2] ), $desc );
88 $this->assertStringEquals( $c[5], UtfNormal::toNFKD( $c[3] ), $desc );
89 $this->assertStringEquals( $c[5], UtfNormal::toNFKD( $c[4] ), $desc );
90 $this->assertStringEquals( $c[5], UtfNormal::toNFKD( $c[5] ), $desc );
91 }
92
93 function assertCleanUp( $c, $desc ) {
94 $this->assertStringEquals( $c[2], UtfNormal::cleanUp( $c[1] ), $desc );
95 $this->assertStringEquals( $c[2], UtfNormal::cleanUp( $c[2] ), $desc );
96 $this->assertStringEquals( $c[2], UtfNormal::cleanUp( $c[3] ), $desc );
97 $this->assertStringEquals( $c[4], UtfNormal::cleanUp( $c[4] ), $desc );
98 $this->assertStringEquals( $c[4], UtfNormal::cleanUp( $c[5] ), $desc );
99 }
100
101 /**
102 * The data provider for this intentionally returns all the
103 * test cases as one since PHPUnit is too slow otherwise
104 *
105 * @dataProvider provideNormalizationTest
106 */
107 function testNormals( $testCases ) {
108 foreach ( $testCases as $case ) {
109 $c = $case[0];
110 $desc = $case[1];
111 $this->assertNFC( $c, $desc );
112 $this->assertNFD( $c, $desc );
113 $this->assertNFKC( $c, $desc );
114 $this->assertNFKD( $c, $desc );
115 $this->assertCleanUp( $c, $desc );
116 }
117 }
118
119 public static function provideUnicodeData() {
120 global $IP;
121 $in = fopen( "$IP/tests/phpunit/data/normal/UnicodeData.txt", "rt" );
122 $testCases = array();
123 while ( false !== ( $line = fgets( $in ) ) ) {
124 $cols = explode( ';', $line );
125 $char = codepointToUtf8( hexdec( $cols[0] ) );
126 $desc = $cols[0] . ": " . $cols[1];
127 if ( $char < "\x20" || $char >= UTF8_SURROGATE_FIRST && $char <= UTF8_SURROGATE_LAST ) {
128 # Can't check NULL with the ICU plugin, as null bytes fail in C land.
129 # Skip other control characters, as we strip them for XML safety.
130 # Surrogates are illegal on their own or in UTF-8, ignore.
131 continue;
132 }
133 if ( empty( self::$testedChars[$char] ) ) {
134 $testCases[] = array( $char, $desc );
135 }
136 }
137 fclose( $in );
138
139 return array( array( $testCases ) );
140 }
141
142 /**
143 * The data provider for this intentionally returns all the
144 * test cases as one since PHPUnit is too slow otherwise
145 *
146 * @depends testNormals
147 * @dataProvider provideUnicodeData
148 */
149 public function testInvariant( $testCases ) {
150 foreach ( $testCases as $case ) {
151 $char = $case[0];
152 $desc = $case[1];
153 $this->assertStringEquals( $char, UtfNormal::toNFC( $char ), $desc );
154 $this->assertStringEquals( $char, UtfNormal::toNFD( $char ), $desc );
155 $this->assertStringEquals( $char, UtfNormal::toNFKC( $char ), $desc );
156 $this->assertStringEquals( $char, UtfNormal::toNFKD( $char ), $desc );
157 $this->assertStringEquals( $char, UtfNormal::cleanUp( $char ), $desc );
158 }
159 }
160 }