|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Propel package. |
| 5 | + * For the full copyright and license information, please view the LICENSE |
| 6 | + * file that was distributed with this source code. |
| 7 | + * |
| 8 | + * @license MIT License |
| 9 | + */ |
| 10 | + |
| 11 | +use Propel\Generator\Util\PhpParser; |
| 12 | +use \Propel\Tests\TestCase; |
| 13 | + |
| 14 | +/** |
| 15 | + * |
| 16 | + */ |
| 17 | +class IconvTransliterationTest extends TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Ensure iconv is available and locale supports transliteration. |
| 21 | + */ |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + if (!function_exists('iconv')) { |
| 25 | + $this->markTestSkipped('iconv() is not available.'); |
| 26 | + } |
| 27 | + |
| 28 | + $currentLocale = setlocale(LC_CTYPE, 0); |
| 29 | + if (in_array($currentLocale, ['C', 'POSIX'], true)) { |
| 30 | + // Attempt to change the locale to something compatible |
| 31 | + $fallbackLocales = ['en_US.UTF-8', 'C.UTF-8', 'de_DE.UTF-8']; |
| 32 | + foreach ($fallbackLocales as $locale) { |
| 33 | + if (setlocale(LC_CTYPE, $locale) !== false) { |
| 34 | + return; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Still stuck in C/POSIX? Skip |
| 39 | + $this->markTestSkipped("Unsupported locale for iconv transliteration: $currentLocale"); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Excerpt from http://php.net/manual/en/function.iconv.php#74101 |
| 45 | + * "Please note that iconv('UTF-8', 'ASCII//TRANSLIT', ...) doesn't work properly when locale category LC_CTYPE is set to C or POSIX. You must choose another locale otherwise all non-ASCII characters will be replaced with question marks." |
| 46 | + */ |
| 47 | + public function testIconvSupportedLocale() |
| 48 | + { |
| 49 | + if (!function_exists('iconv')) { |
| 50 | + $this->markTestSkipped(); |
| 51 | + } |
| 52 | + $LC_CTYPE = setlocale(LC_CTYPE, 0); |
| 53 | + $this->assertNotEquals('C', $LC_CTYPE, 'iconv transliteration does not work properly when locale category LC_CTYPE is set to C or POSIX'); |
| 54 | + $this->assertNotEquals('POSIX', $LC_CTYPE, 'iconv transliteration does not work properly when locale category LC_CTYPE is set to C or POSIX'); |
| 55 | + } |
| 56 | + |
| 57 | + public static function iconvTransliterationSlugProvider() |
| 58 | + { |
| 59 | + return [ |
| 60 | + ['foo', 'foo'], |
| 61 | + ['fôo', 'foo'], |
| 62 | + ['€', 'EUR'], |
| 63 | + ['CŠŒŽšœžŸµÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝßàáâãäåæçèéêëìíîïñòóôõöùúûüýÿ', 'CSOEZsoezYuAAAAAAAECEEEEIIIINOOOOOUUUUYssaaaaaaaeceeeeiiiinooooouuuuyy'], |
| 64 | + ['ø', 'o'], |
| 65 | + ['Ø', 'O'], |
| 66 | + ['¥Ðð', 'JPYDd'], |
| 67 | + ]; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @dataProvider iconvTransliterationSlugProvider |
| 72 | + */ |
| 73 | + public function testIconvTransliteration(string $input, string $expected): void |
| 74 | + { |
| 75 | + $translit = iconv('utf-8', 'us-ascii//TRANSLIT', $input); |
| 76 | + |
| 77 | + // If transliteration results in all question marks, skip (locale likely broken) |
| 78 | + if (preg_match('/^\?+$/', $translit) && $input !== str_repeat('?', strlen($input))) { |
| 79 | + $this->markTestSkipped('iconv() returned only question marks — possibly due to locale issues.'); |
| 80 | + } |
| 81 | + |
| 82 | + $this->assertEquals($expected, $translit, 'iconv transliteration behaves as expected'); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments