Debian Codegraph @

tests/phpunit/includes/collation/CollationTest.php

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