Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Transliteration / PhpTransliterationTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Transliteration;
4
5 use Drupal\Component\Utility\Random;
6 use Drupal\Core\Transliteration\PhpTransliteration;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10  * Tests Transliteration component functionality.
11  *
12  * @group Transliteration
13  *
14  * @coversClass \Drupal\Core\Transliteration\PhpTransliteration
15  */
16 class PhpTransliterationTest extends UnitTestCase {
17
18   /**
19    * Tests the PhpTransliteration with an alter hook.
20    *
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.
30    *
31    * @dataProvider providerTestPhpTransliterationWithAlter
32    */
33   public function testPhpTransliterationWithAlter($langcode, $original, $expected, $printable = NULL) {
34     if ($printable === NULL) {
35       $printable = $original;
36     }
37
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())
42       ->method('alter')
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';
51         }
52       }));
53     $transliteration = new PhpTransliteration(NULL, $module_handler);
54
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.");
57   }
58
59   /**
60    * Provides test data for testPhpTransliterationWithAlter.
61    *
62    * @return array
63    */
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('&#x10330;&#x10338;', ENT_NOQUOTES, 'UTF-8');
74     // Five-byte characters do not work in MySQL, so make a printable version.
75     $five_byte_printable = '&#x10330;&#x10338;';
76
77     $cases = [
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],
83     ];
84
85     return $cases;
86   }
87
88 }