public function EntityRepositoryTest::testGetCanonical in Drupal 8
Same name and namespace in other branches
- 9 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 215
Class
- EntityRepositoryTest
- Tests the entity repository.
Namespace
Drupal\KernelTests\Core\EntityCode
public function testGetCanonical() {
// 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');
}