function TranslationStringTest::testComparison

Same name in other branches
  1. 9 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()
  3. 11.x 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);
    // TranslationWrappers which have different settings are not equal.
    $this->assertNotEquals($a, $c);
    $this->assertNotEquals($a, $d);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.