function EntityTranslationTest::testTranslationObjectCache

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityTranslationTest::testTranslationObjectCache()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityTranslationTest::testTranslationObjectCache()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityTranslationTest::testTranslationObjectCache()

Tests the translation object cache.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php, line 1003

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testTranslationObjectCache() : void {
    $default_langcode = $this->langcodes[1];
    $translation_langcode = $this->langcodes[2];
    $entity = EntityTestMul::create([
        'name' => 'test',
        'langcode' => $default_langcode,
    ]);
    $entity->save();
    $entity->addTranslation($translation_langcode)
        ->save();
    // Test that the default translation object is put into the translation
    // object cache when a new translation object is initialized.
    $entity = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())
        ->loadUnchanged($entity->id());
    $default_translation_spl_object_hash = spl_object_hash($entity);
    $this->assertEquals($default_translation_spl_object_hash, spl_object_hash($entity->getTranslation($translation_langcode)
        ->getTranslation($default_langcode)));
    // Test that non-default translations are always served from the translation
    // object cache.
    $entity = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())
        ->loadUnchanged($entity->id());
    $this->assertEquals(spl_object_hash($entity->getTranslation($translation_langcode)), spl_object_hash($entity->getTranslation($translation_langcode)));
    $this->assertEquals(spl_object_hash($entity->getTranslation($translation_langcode)), spl_object_hash($entity->getTranslation($translation_langcode)
        ->getTranslation($default_langcode)
        ->getTranslation($translation_langcode)));
}

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