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