class TranslationStringTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/KernelTests/Core/StringTranslation/TranslationStringTest.php \Drupal\KernelTests\Core\StringTranslation\TranslationStringTest
- 10 core/tests/Drupal/KernelTests/Core/StringTranslation/TranslationStringTest.php \Drupal\KernelTests\Core\StringTranslation\TranslationStringTest
- 8.9.x core/tests/Drupal/KernelTests/Core/StringTranslation/TranslationStringTest.php \Drupal\KernelTests\Core\StringTranslation\TranslationStringTest
Tests the TranslatableMarkup class.
@group StringTranslation
Hierarchy
- class \Drupal\KernelTests\KernelTestBase extends \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait implements \PHPUnit\Framework\TestCase
- class \Drupal\KernelTests\Core\StringTranslation\TranslationStringTest implements \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of TranslationStringTest
File
-
core/
tests/ Drupal/ KernelTests/ Core/ StringTranslation/ TranslationStringTest.php, line 14
Namespace
Drupal\KernelTests\Core\StringTranslationView source
class TranslationStringTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'language',
];
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
ConfigurableLanguage::createFromLangcode('de')->save();
}
/**
* Tests that TranslatableMarkup objects can be compared.
*/
public function testComparison() {
$this->rebootAndPrepareSettings();
$a = \Drupal::service('string_translation')->translate('Example @number', [
'@number' => 42,
], [
'langcode' => 'de',
]);
$this->rebootAndPrepareSettings();
$b = \Drupal::service('string_translation')->translate('Example @number', [
'@number' => 42,
], [
'langcode' => 'de',
]);
$c = \Drupal::service('string_translation')->translate('Example @number', [
'@number' => 43,
], [
'langcode' => 'de',
]);
$d = \Drupal::service('string_translation')->translate('Example @number', [
'@number' => 42,
], [
'langcode' => 'en',
]);
// The two objects have the same settings so == comparison will work.
$this->assertEquals($a, $b);
// The two objects are not the same object.
$this->assertNotSame($a, $b);
// TranslatableMarkup which have different settings are not equal.
$this->assertNotEquals($a, $c);
$this->assertNotEquals($a, $d);
}
/**
* Reboots the kernel to set custom translations in Settings.
*/
protected function rebootAndPrepareSettings() {
// Reboot the container so that different services are injected and the new
// settings are picked.
$kernel = $this->container
->get('kernel');
$kernel->shutdown();
$kernel->boot();
$settings = Settings::getAll();
$settings['locale_custom_strings_de'] = [
'' => [
'Example @number' => 'Example @number translated',
],
];
// Recreate the settings static.
new Settings($settings);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.