You are here

public function TranslationStringTest::testComparison in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/StringTranslation/TranslationStringTest.php \Drupal\KernelTests\Core\StringTranslation\TranslationStringTest::testComparison()
  2. 10 core/tests/Drupal/KernelTests/Core/StringTranslation/TranslationStringTest.php \Drupal\KernelTests\Core\StringTranslation\TranslationStringTest::testComparison()

Tests that TranslatableMarkup objects can be compared.

File

core/tests/Drupal/KernelTests/Core/StringTranslation/TranslationStringTest.php, line 36

Class

TranslationStringTest
Tests the TranslatableMarkup class.

Namespace

Drupal\KernelTests\Core\StringTranslation

Code

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);
}