3 namespace Drupal\KernelTests\Core\StringTranslation;
5 use Drupal\Core\Site\Settings;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\language\Entity\ConfigurableLanguage;
10 * Tests the TranslatableMarkup class.
12 * @group StringTranslation
14 class TranslationStringTest extends KernelTestBase {
21 public static $modules = [
28 protected function setUp() {
30 ConfigurableLanguage::createFromLangcode('de')->save();
34 * Tests that TranslatableMarkup objects can be compared.
36 public function testComparison() {
37 $this->rebootAndPrepareSettings();
38 $a = \Drupal::service('string_translation')->translate('Example @number', ['@number' => 42], ['langcode' => 'de']);
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']);
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);
55 * Reboots the kernel to set custom translations in Settings.
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');
63 $settings = Settings::getAll();
64 $settings['locale_custom_strings_de'] = ['' => ['Example @number' => 'Example @number translated']];
65 // Recreate the settings static.
66 new Settings($settings);