xref: /webtrees/tests/app/Encodings/AnselTest.php (revision e873f434551745f888937263ff89e80db3b0f785)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Tests\Encodings;
21
22use Fisharebest\Webtrees\Encodings\AbstractEncoding;
23use Fisharebest\Webtrees\Encodings\ANSEL;
24use Fisharebest\Webtrees\Encodings\UTF8;
25use Normalizer;
26use PHPUnit\Framework\Attributes\CoversClass;
27use PHPUnit\Framework\TestCase;
28
29use function bin2hex;
30use function count;
31use function ctype_alpha;
32use function dechex;
33use function in_array;
34use function preg_split;
35use function range;
36use function strlen;
37
38use const PREG_SPLIT_NO_EMPTY;
39
40#[CoversClass(AbstractEncoding::class)]
41#[CoversClass(ANSEL::class)]
42#[CoversClass(UTF8::class)]
43class AnselTest extends TestCase
44{
45    private const array TEST_DATA = [
46        "\x00\x01\x02\x03\x04\x05\x06\x07"         => "\x00\x01\x02\x03\x04\x05\x06\x07",
47        "\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"         => "\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
48        "\x10\x11\x12\x13\x14\x15\x16\x17"         => "\x10\x11\x12\x13\x14\x15\x16\x17",
49        "\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"         => "\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
50        ' !"#$%&\''                                => "\x20\x21\x22\x23\x24\x25\x26\x27",
51        '()*+,-./'                                 => "\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F",
52        '01234567'                                 => "\x30\x31\x32\x33\x34\x35\x36\x37",
53        '89:;<=>?'                                 => "\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F",
54        '@ABCDEFG'                                 => "\x40\x41\x42\x43\x44\x45\x46\x47",
55        'HIJKLMNO'                                 => "\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F",
56        'PQRSTUVW'                                 => "\x50\x51\x52\x53\x54\x55\x56\x57",
57        'XYZ[\\]^_'                                => "\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F",
58        '`abcdefg'                                 => "\x60\x61\x62\x63\x64\x65\x66\x67",
59        'hijklmno'                                 => "\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F",
60        'pqrstuvw'                                 => "\x70\x71\x72\x73\x74\x75\x76\x77",
61        "xyz{|}~\x7F"                              => "\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F",
62        "\xC2\x98\xC2\x9C\xE2\x80\x8D\xE2\x80\x8C" => "\x88\x89\x8D\x8E",
63        'ŁØĐÞÆŒʹ'                                  => "\xA1\xA2\xA3\xA4\xA5\xA6\xA7",
64        '·♭®±ƠƯʼ'                                  => "\xA8\xA9\xAA\xAB\xAC\xAD\xAE",
65        'ʻłøđþæœʺ'                                 => "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7",
66        'ı£ðơư'                                    => "\xB8\xB9\xBA\xBC\xBD",
67        '°ℓ℗©♯¿¡ẞ€'                                => "\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8",
68        // Combining diacritics
69        'ảàáâãāăȧ'                                 => "\xE0a\xE1a\xE2a\xE3a\xE4a\xE5a\xE6a\xE7a",
70        'äǎåa͡a̕a̋a̐'                              => "\xE8a\xE9a\xEAa\xEBa\xEDa\xEEa\xEFa",
71        'a̧ąạa̤ḁa̳a̲a̦'                            => "\xF0a\xF1a\xF2a\xF3a\xF4a\xF5a\xF6a\xF7a",
72        'a̜a̮a͠a̓a̸'                               => "\xF8a\xF9a\xFAa\xFEa\xFFa",
73        // Diacritics with non-ascii
74        'ǣ'                                        => "\xE5\xB5",
75        // LATIN CAPITAL LETTER O WITH DIAERESIS AND MACRON
76        'Ō̈'                                       => "\xE5\xE8O",
77        // LATIN CAPITAL LETTER O WITH MACRON AND DIAERESIS
78        'Ȫ'                                        => "\xE8\xE5O",
79    ];
80
81    private const array UNPRINTABLE = [
82        "\x80\x81\x82\x83\x84\x85\x86\x87",
83        "\x8A\x8B\x8C\x8F",
84        "\x90\x91\x92\x93\x94\x95\x96\x97",
85        "\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F",
86        "\xA0",
87        "\xAF",
88        "\xBB",
89        "\xC9\xCA\xCB\xCC\xCD\xCE",
90        "\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7",
91        "\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF",
92        "\xFC\xFD",
93    ];
94
95    private const array MULTIPART_DIACRITIC = ["\xEC", "\xFB"];
96
97    public function testPreComposedCharacters(): void
98    {
99        $latin_code_blocks = [
100            range(0x80, 0xFF),
101            range(0x100, 0x17F),
102            range(0x180, 0x24F),
103            range(0x1E00, 0x1EFF),
104        ];
105
106        $ansel_combining_characters = [
107            UTF8::COMBINING_HOOK_ABOVE,
108            UTF8::COMBINING_GRAVE_ACCENT,
109            UTF8::COMBINING_ACUTE_ACCENT,
110            UTF8::COMBINING_CIRCUMFLEX_ACCENT,
111            UTF8::COMBINING_TILDE,
112            UTF8::COMBINING_MACRON,
113            UTF8::COMBINING_BREVE,
114            UTF8::COMBINING_DOT_ABOVE,
115            UTF8::COMBINING_DIAERESIS,
116            UTF8::COMBINING_CARON,
117            UTF8::COMBINING_RING_ABOVE,
118            UTF8::COMBINING_DOUBLE_INVERTED_BREVE,
119            UTF8::COMBINING_COMMA_ABOVE_RIGHT,
120            UTF8::COMBINING_DOUBLE_ACUTE_ACCENT,
121            UTF8::COMBINING_CANDRABINDU,
122            UTF8::COMBINING_CEDILLA,
123            UTF8::COMBINING_OGONEK,
124            UTF8::COMBINING_DOT_BELOW,
125            UTF8::COMBINING_DIAERESIS_BELOW,
126            UTF8::COMBINING_RING_BELOW,
127            UTF8::COMBINING_DOUBLE_LOW_LINE,
128            UTF8::COMBINING_LOW_LINE,
129            UTF8::COMBINING_COMMA_BELOW,
130            UTF8::COMBINING_LEFT_HALF_RING_BELOW,
131            UTF8::COMBINING_BREVE_BELOW,
132            UTF8::COMBINING_DOUBLE_TILDE,
133            UTF8::REPLACEMENT_CHARACTER,
134            UTF8::REPLACEMENT_CHARACTER,
135            UTF8::COMBINING_COMMA_ABOVE,
136            UTF8::COMBINING_LONG_SOLIDUS_OVERLAY,
137        ];
138
139        $encoding = new ANSEL();
140
141        foreach ($latin_code_blocks as $codes) {
142            foreach ($codes as $code) {
143                $utf8 = UTF8::chr($code);
144                $norm = Normalizer::normalize($utf8, Normalizer::FORM_D);
145
146                if ($norm !== $utf8) {
147                    $chars = preg_split('//u', $norm, -1, PREG_SPLIT_NO_EMPTY);
148                    if (!ctype_alpha($chars[0])) {
149                        continue;
150                    }
151                    if (!in_array($chars[1], $ansel_combining_characters, true)) {
152                        continue;
153                    }
154                    if (count($chars) >= 3 && !in_array($chars[2], $ansel_combining_characters, true)) {
155                        continue;
156                    }
157
158                    static::assertSame($utf8, $encoding->toUtf8($encoding->fromUtf8($utf8)), 'U+' . dechex($code));
159                }
160            }
161        }
162    }
163
164    public function testToUtf8(): void
165    {
166        $encoding = new ANSEL();
167
168        foreach (self::TEST_DATA as $utf8 => $ansel) {
169            self::assertSame($utf8, $encoding->toUtf8($ansel), bin2hex($utf8) . ' ' . bin2hex($encoding->toUtf8($ansel)));
170        }
171    }
172
173    public function testFromUtf8(): void
174    {
175        $encoding = new ANSEL();
176
177        foreach (self::TEST_DATA as $utf8 => $other) {
178            self::assertSame($other, $encoding->fromUtf8($utf8));
179        }
180    }
181
182    public function testUnprintable(): void
183    {
184        $encoding = new ANSEL();
185
186        foreach (self::UNPRINTABLE as $chars) {
187            $expected = str_repeat(UTF8::REPLACEMENT_CHARACTER, strlen($chars));
188            self::assertSame($expected, $encoding->toUtf8($chars));
189        }
190    }
191
192    public function testMultiPartDiacritic(): void
193    {
194        $encoding = new ANSEL();
195
196        foreach (self::MULTIPART_DIACRITIC as $chars) {
197            self::assertSame('', $encoding->toUtf8($chars));
198        }
199    }
200}
201