function EntityRepositoryTest::doTestLanguageFallback

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

Check that language fallback is applied.

Parameters

string $method_name: An entity repository method name.

2 calls to EntityRepositoryTest::doTestLanguageFallback()
EntityRepositoryTest::testGetActive in core/tests/Drupal/KernelTests/Core/Entity/EntityRepositoryTest.php
Tests retrieving active variants.
EntityRepositoryTest::testGetCanonical in core/tests/Drupal/KernelTests/Core/Entity/EntityRepositoryTest.php
Tests retrieving canonical variants.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityRepositoryTest.php, line 259

Class

EntityRepositoryTest
Tests the entity repository.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function doTestLanguageFallback($method_name) {
    $entity_type_id = 'entity_test_mulrev';
    $en_contexts = $this->getLanguageContexts('en');
    $it_contexts = $this->getLanguageContexts('it');
    $ro_contexts = $this->getLanguageContexts('ro');
    
    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    $storage = $this->entityTypeManager
        ->getStorage($entity_type_id);
    $values = [
        'name' => $this->randomString(),
    ];
    
    /** @var \Drupal\entity_test\Entity\EntityTestMulRev $entity3 */
    $entity3 = $storage->create([
        'langcode' => 'it',
    ] + $values);
    $entity3->addTranslation('ro', $values);
    $storage->save($entity3);
    
    /** @var \Drupal\entity_test\Entity\EntityTestMulRev $active */
    $active = $this->entityRepository
        ->{$method_name}($entity_type_id, $entity3->id(), $en_contexts);
    $this->assertSame('it', $active->language()
        ->getId());
    $active = $this->entityRepository
        ->{$method_name}($entity_type_id, $entity3->id(), $ro_contexts);
    $this->assertSame('ro', $active->language()
        ->getId());
    
    /** @var \Drupal\entity_test\Entity\EntityTestMulRev $entity4 */
    $entity4 = $storage->create([
        'langcode' => 'ro',
    ] + $values);
    $entity4->addTranslation('en', $values);
    $storage->save($entity4);
    $active = $this->entityRepository
        ->{$method_name}($entity_type_id, $entity4->id(), $it_contexts);
    $this->assertSame('en', $active->language()
        ->getId());
    
    /** @var \Drupal\entity_test\Entity\EntityTestMulRev $entity5 */
    $entity5 = $storage->create([
        'langcode' => 'ro',
    ] + $values);
    $storage->save($entity5);
    $active = $this->entityRepository
        ->{$method_name}($entity_type_id, $entity5->id(), $it_contexts);
    $this->assertSame('ro', $active->language()
        ->getId());
    $active = $this->entityRepository
        ->{$method_name}($entity_type_id, $entity5->id(), $en_contexts);
    $this->assertSame('ro', $active->language()
        ->getId());
}

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