3 namespace Drupal\Tests\Core\Transliteration;
5 use Drupal\Component\Utility\Random;
6 use Drupal\Core\Transliteration\PhpTransliteration;
7 use Drupal\Tests\UnitTestCase;
10 * Tests Transliteration component functionality.
12 * @group Transliteration
14 * @coversClass \Drupal\Core\Transliteration\PhpTransliteration
16 class PhpTransliterationTest extends UnitTestCase {
19 * Tests the PhpTransliteration with an alter hook.
21 * @param string $langcode
22 * The langcode of the string.
23 * @param string $original
24 * The string which was not transliterated yet.
25 * @param string $expected
26 * The string expected after the transliteration.
27 * @param string|null $printable
28 * (optional) An alternative version of the original string which is
29 * printable in the output.
31 * @dataProvider providerTestPhpTransliterationWithAlter
33 public function testPhpTransliterationWithAlter($langcode, $original, $expected, $printable = NULL) {
34 if ($printable === NULL) {
35 $printable = $original;
38 // Test each case both with a new instance of the transliteration class,
39 // and with one that builds as it goes.
40 $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
41 $module_handler->expects($this->any())
43 ->will($this->returnCallback(function($hook, &$overrides, $langcode) {
44 if ($langcode == 'zz') {
45 // The default transliteration of Ä is A, but change it to Z for testing.
46 $overrides[0xC4] = 'Z';
47 // Also provide transliterations of two 5-byte characters from
48 // http://wikipedia.org/wiki/Gothic_alphabet.
49 $overrides[0x10330] = 'A';
50 $overrides[0x10338] = 'Th';
53 $transliteration = new PhpTransliteration(NULL, $module_handler);
55 $actual = $transliteration->transliterate($original, $langcode);
56 $this->assertSame($expected, $actual, "'$printable' transliteration to '$actual' is identical to '$expected' for language '$langcode' in service instance.");
60 * Provides test data for testPhpTransliterationWithAlter.
64 public function providerTestPhpTransliterationWithAlter() {
65 $random_generator = new Random();
66 $random = $random_generator->string(10);
67 // Make some strings with two, three, and four-byte characters for testing.
68 // Note that the 3-byte character is overridden by the 'kg' language.
69 $two_byte = 'Ä Ö Ü Å Ø äöüåøhello';
70 // These are two Gothic alphabet letters. See
71 // http://wikipedia.org/wiki/Gothic_alphabet
72 // They are not in our tables, but should at least give us '?' (unknown).
73 $five_byte = html_entity_decode('𐌰𐌸', ENT_NOQUOTES, 'UTF-8');
74 // Five-byte characters do not work in MySQL, so make a printable version.
75 $five_byte_printable = '𐌰𐌸';
78 // Test the language override hook in the test module, which changes
79 // the transliteration of Ä to Z and provides for the 5-byte characters.
80 ['zz', $two_byte, 'Z O U A O aouaohello'],
81 ['zz', $random, $random],
82 ['zz', $five_byte, 'ATh', $five_byte_printable],