function EntityRepositoryTest::testGetCanonical

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

Tests retrieving canonical variants.

@covers ::getCanonical
@covers ::getCanonicalMultiple

File

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

Class

EntityRepositoryTest
Tests the entity repository.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testGetCanonical() : void {
  // Check that when the entity does not exist NULL is returned.
  $entity_type_id = 'entity_test_mul';
  $canonical = $this->entityRepository
    ->getActive($entity_type_id, -1);
  $this->assertNull($canonical);
  // Check that the correct language fallback rules are applied.
  $storage = $this->entityTypeManager
    ->getStorage($entity_type_id);
  $values = [
    'name' => $this->randomString(),
  ];
  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $storage->create($values);
  $storage->save($entity);
  $langcode = 'it';
  $it_contexts = $this->getLanguageContexts($langcode);
  $canonical = $this->entityRepository
    ->getCanonical($entity_type_id, $entity->id(), $it_contexts);
  $this->assertSame($entity->getUntranslated()
    ->language()
    ->getId(), $canonical->language()
    ->getId());
  /** @var \Drupal\Core\Entity\ContentEntityInterface $translation */
  $translation = $entity->addTranslation($langcode, $values);
  $storage->save($translation);
  $canonical = $this->entityRepository
    ->getCanonical($entity_type_id, $entity->id(), $it_contexts);
  $this->assertSame($translation->language()
    ->getId(), $canonical->language()
    ->getId());
  $canonical = $this->entityRepository
    ->getCanonical($entity_type_id, $entity->id());
  $this->assertSame($entity->getUntranslated()
    ->language()
    ->getId(), $canonical->language()
    ->getId());
  /** @var \Drupal\entity_test\Entity\EntityTestMul $entity2 */
  $entity2 = $storage->create($values);
  $storage->save($entity2);
  /** @var \Drupal\Core\Entity\ContentEntityInterface[] $canonical */
  $canonical = $this->entityRepository
    ->getCanonicalMultiple($entity_type_id, [
    $entity->id(),
    $entity2->id(),
  ], $it_contexts);
  $this->assertSame($translation->language()
    ->getId(), $canonical[$entity->id()]
    ->language()
    ->getId());
  $this->assertSame($entity2->language()
    ->getId(), $canonical[$entity2->id()]
    ->language()
    ->getId());
  $this->doTestLanguageFallback('getCanonical');
}

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