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