Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / StringTranslation / TranslationStringTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\StringTranslation;
4
5 use Drupal\Core\Site\Settings;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\language\Entity\ConfigurableLanguage;
8
9 /**
10  * Tests the TranslatableMarkup class.
11  *
12  * @group StringTranslation
13  */
14 class TranslationStringTest extends KernelTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = [
22     'language',
23   ];
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function setUp() {
29     parent::setUp();
30     ConfigurableLanguage::createFromLangcode('de')->save();
31   }
32
33   /**
34    * Tests that TranslatableMarkup objects can be compared.
35    */
36   public function testComparison() {
37     $this->rebootAndPrepareSettings();
38     $a = \Drupal::service('string_translation')->translate('Example @number', ['@number' => 42], ['langcode' => 'de']);
39
40     $this->rebootAndPrepareSettings();
41     $b = \Drupal::service('string_translation')->translate('Example @number', ['@number' => 42], ['langcode' => 'de']);
42     $c = \Drupal::service('string_translation')->translate('Example @number', ['@number' => 43], ['langcode' => 'de']);
43     $d = \Drupal::service('string_translation')->translate('Example @number', ['@number' => 42], ['langcode' => 'en']);
44
45     // The two objects have the same settings so == comparison will work.
46     $this->assertEquals($a, $b);
47     // The two objects are not the same object.
48     $this->assertNotSame($a, $b);
49     // TranslationWrappers which have different settings are not equal.
50     $this->assertNotEquals($a, $c);
51     $this->assertNotEquals($a, $d);
52   }
53
54   /**
55    * Reboots the kernel to set custom translations in Settings.
56    */
57   protected function rebootAndPrepareSettings() {
58     // Reboot the container so that different services are injected and the new
59     // settings are picked.
60     $kernel = $this->container->get('kernel');
61     $kernel->shutdown();
62     $kernel->boot();
63     $settings = Settings::getAll();
64     $settings['locale_custom_strings_de'] = ['' => ['Example @number' => 'Example @number translated']];
65     // Recreate the settings static.
66     new Settings($settings);
67   }
68
69 }