PhpTransliterationTest.php 3.2 KB

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