Merge "Revert "Add .gitignore to the /skins directory""
[lhc/web/wiklou.git] / tests / phpunit / includes / CollationTest.php
1 <?php
2
3 /**
4 * Class CollationTest
5 * @covers Collation
6 * @covers IcuCollation
7 * @covers IdentityCollation
8 * @covers UppercaseCollation
9 */
10 class CollationTest extends MediaWikiLangTestCase {
11 protected function setUp() {
12 parent::setUp();
13 if ( !extension_loaded( 'intl' ) ) {
14 $this->markTestSkipped( 'These tests require intl extension' );
15 }
16 }
17
18 /**
19 * Test to make sure, that if you
20 * have "X" and "XY", the binary
21 * sortkey also has "X" being a
22 * prefix of "XY". Our collation
23 * code makes this assumption.
24 *
25 * @param $lang String Language code for collator
26 * @param $base String Base string
27 * @param $extended String String containing base as a prefix.
28 *
29 * @dataProvider prefixDataProvider
30 */
31 public function testIsPrefix( $lang, $base, $extended ) {
32 $cp = Collator::create( $lang );
33 $cp->setStrength( Collator::PRIMARY );
34 $baseBin = $cp->getSortKey( $base );
35 // Remove sortkey terminator
36 $baseBin = rtrim( $baseBin, "\0" );
37 $extendedBin = $cp->getSortKey( $extended );
38 $this->assertStringStartsWith( $baseBin, $extendedBin, "$base is not a prefix of $extended" );
39 }
40
41 public static function prefixDataProvider() {
42 return array(
43 array( 'en', 'A', 'AA' ),
44 array( 'en', 'A', 'AAA' ),
45 array( 'en', 'Д', 'ДЂ' ),
46 array( 'en', 'Д', 'ДA' ),
47 // 'Ʒ' should expand to 'Z ' (note space).
48 array( 'fi', 'Z', 'Ʒ' ),
49 // 'Þ' should expand to 'th'
50 array( 'sv', 't', 'Þ' ),
51 // Javanese is a limited use alphabet, so should have 3 bytes
52 // per character, so do some tests with it.
53 array( 'en', 'ꦲ', 'ꦲꦤ' ),
54 array( 'en', 'ꦲ', 'ꦲД' ),
55 array( 'en', 'A', 'Aꦲ' ),
56 );
57 }
58
59 /**
60 * Opposite of testIsPrefix
61 *
62 * @dataProvider notPrefixDataProvider
63 */
64 public function testNotIsPrefix( $lang, $base, $extended ) {
65 $cp = Collator::create( $lang );
66 $cp->setStrength( Collator::PRIMARY );
67 $baseBin = $cp->getSortKey( $base );
68 // Remove sortkey terminator
69 $baseBin = rtrim( $baseBin, "\0" );
70 $extendedBin = $cp->getSortKey( $extended );
71 $this->assertStringStartsNotWith( $baseBin, $extendedBin, "$base is a prefix of $extended" );
72 }
73
74 public static function notPrefixDataProvider() {
75 return array(
76 array( 'en', 'A', 'B' ),
77 array( 'en', 'AC', 'ABC' ),
78 array( 'en', 'Z', 'Ʒ' ),
79 array( 'en', 'A', 'ꦲ' ),
80 );
81 }
82
83 /**
84 * Test correct first letter is fetched.
85 *
86 * @param $collation String Collation name (aka uca-en)
87 * @param $string String String to get first letter of
88 * @param $firstLetter String Expected first letter.
89 *
90 * @dataProvider firstLetterProvider
91 */
92 public function testGetFirstLetter( $collation, $string, $firstLetter ) {
93 $col = Collation::factory( $collation );
94 $this->assertEquals( $firstLetter, $col->getFirstLetter( $string ) );
95 }
96
97 function firstLetterProvider() {
98 return array(
99 array( 'uppercase', 'Abc', 'A' ),
100 array( 'uppercase', 'abc', 'A' ),
101 array( 'identity', 'abc', 'a' ),
102 array( 'uca-en', 'abc', 'A' ),
103 array( 'uca-en', ' ', ' ' ),
104 array( 'uca-en', 'Êveryone', 'E' ),
105 array( 'uca-vi', 'Êveryone', 'Ê' ),
106 // Make sure thorn is not a first letter.
107 array( 'uca-sv', 'The', 'T' ),
108 array( 'uca-sv', 'Å', 'Å' ),
109 array( 'uca-hu', 'dzsdo', 'Dzs' ),
110 array( 'uca-hu', 'dzdso', 'Dz' ),
111 array( 'uca-hu', 'CSD', 'Cs' ),
112 array( 'uca-root', 'CSD', 'C' ),
113 array( 'uca-fi', 'Ǥ', 'G' ),
114 array( 'uca-fi', 'Ŧ', 'T' ),
115 array( 'uca-fi', 'Ʒ', 'Z' ),
116 array( 'uca-fi', 'Ŋ', 'N' ),
117 );
118 }
119 }