function EntityRepositoryTest::testGetActive

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

Tests retrieving active variants.

@covers ::getActive @covers ::getActiveMultiple

File

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

Class

EntityRepositoryTest
Tests the entity repository.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testGetActive() {
    $en_contexts = $this->getLanguageContexts('en');
    // Check that when the entity does not exist NULL is returned.
    $entity_type_id = 'entity_test';
    $active = $this->entityRepository
        ->getActive($entity_type_id, -1);
    $this->assertNull($active);
    // Check that the correct active variant is returned for a non-translatable,
    // non-revisionable entity.
    
    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    $storage = $this->entityTypeManager
        ->getStorage($entity_type_id);
    $values = [
        'name' => $this->randomString(),
    ];
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $storage->create($values);
    $storage->save($entity);
    $entity = $storage->load($entity->id());
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $active */
    $active = $this->entityRepository
        ->getActive($entity_type_id, $entity->id(), $en_contexts);
    $this->assertSame($entity, $active);
    // Check that the correct active variant is returned for a non-translatable
    // revisionable entity.
    $entity_type_id = 'entity_test_rev';
    $storage = $this->entityTypeManager
        ->getStorage($entity_type_id);
    $values = [
        'name' => $this->randomString(),
    ];
    $entity = $storage->create($values);
    $storage->save($entity);
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
    $revision = $storage->createRevision($entity, FALSE);
    $revision->save();
    $active = $this->entityRepository
        ->getActive($entity_type_id, $entity->id(), $en_contexts);
    $this->assertEntityType($active, $entity_type_id);
    $this->assertSame($revision->getLoadedRevisionId(), $active->getLoadedRevisionId());
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $revision2 */
    $revision2 = $storage->createRevision($revision);
    $revision2->save();
    $active = $this->entityRepository
        ->getActive($entity_type_id, $entity->id(), $en_contexts);
    $this->assertSame($revision2->getLoadedRevisionId(), $active->getLoadedRevisionId());
    // Check that the correct active variant is returned for a translatable
    // non-revisionable entity.
    $entity_type_id = 'entity_test_mul';
    $storage = $this->entityTypeManager
        ->getStorage($entity_type_id);
    $values = [
        'name' => $this->randomString(),
    ];
    $entity = $storage->create($values);
    $storage->save($entity);
    $langcode = 'it';
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $translation */
    $translation = $entity->addTranslation($langcode, $values);
    $storage->save($translation);
    $active = $this->entityRepository
        ->getActive($entity_type_id, $entity->id(), $en_contexts);
    $this->assertEntityType($active, $entity_type_id);
    $this->assertSame($entity->language()
        ->getId(), $active->language()
        ->getId());
    $it_contexts = $this->getLanguageContexts($langcode);
    $active = $this->entityRepository
        ->getActive($entity_type_id, $entity->id(), $it_contexts);
    $this->assertSame($translation->language()
        ->getId(), $active->language()
        ->getId());
    // Check that the correct active variant is returned for a translatable and
    // revisionable entity.
    $entity_type_id = 'entity_test_mulrev';
    $storage = $this->entityTypeManager
        ->getStorage($entity_type_id);
    $values = [
        'name' => $this->randomString(),
    ];
    $entity = $storage->create($values);
    $storage->save($entity);
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $en_revision */
    $en_revision = $storage->createRevision($entity, FALSE);
    $storage->save($en_revision);
    $active = $this->entityRepository
        ->getActive($entity_type_id, $entity->id(), $en_contexts);
    $this->assertEntityType($active, $entity_type_id);
    $this->assertSame($en_revision->getLoadedRevisionId(), $active->getLoadedRevisionId());
    $revision_translation = $en_revision->addTranslation($langcode, $values);
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $it_revision */
    $it_revision = $storage->createRevision($revision_translation, FALSE);
    $storage->save($it_revision);
    $active = $this->entityRepository
        ->getActive($entity_type_id, $entity->id(), $en_contexts);
    $this->assertSame($en_revision->getLoadedRevisionId(), $active->getLoadedRevisionId());
    $this->assertSame($en_revision->language()
        ->getId(), $active->language()
        ->getId());
    $active = $this->entityRepository
        ->getActive($entity_type_id, $entity->id(), $it_contexts);
    $this->assertSame($it_revision->getLoadedRevisionId(), $active->getLoadedRevisionId());
    $this->assertSame($it_revision->language()
        ->getId(), $active->language()
        ->getId());
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $en_revision2 */
    $en_revision2 = $storage->createRevision($en_revision);
    $storage->save($en_revision2);
    $active = $this->entityRepository
        ->getActive($entity_type_id, $entity->id(), $en_contexts);
    $this->assertSame($en_revision2->getLoadedRevisionId(), $active->getLoadedRevisionId());
    $this->assertSame($en_revision2->language()
        ->getId(), $active->language()
        ->getId());
    $active = $this->entityRepository
        ->getActive($entity_type_id, $entity->id(), $it_contexts);
    $this->assertSame($it_revision->getLoadedRevisionId(), $active->getLoadedRevisionId());
    $this->assertSame($it_revision->language()
        ->getId(), $active->language()
        ->getId());
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $it_revision2 */
    $it_revision2 = $storage->createRevision($it_revision);
    $storage->save($it_revision2);
    $active = $this->entityRepository
        ->getActive($entity_type_id, $entity->id(), $en_contexts);
    $this->assertSame($it_revision2->getLoadedRevisionId(), $active->getLoadedRevisionId());
    $this->assertSame($it_revision2->getUntranslated()
        ->language()
        ->getId(), $active->language()
        ->getId());
    $active = $this->entityRepository
        ->getActive($entity_type_id, $entity->id(), $it_contexts);
    $this->assertSame($it_revision2->getLoadedRevisionId(), $active->getLoadedRevisionId());
    $this->assertSame($it_revision2->language()
        ->getId(), $active->language()
        ->getId());
    
    /** @var \Drupal\entity_test\Entity\EntityTestMulRev $entity2 */
    $entity2 = $storage->create($values);
    $storage->save($entity2);
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface[] $active */
    $active = $this->entityRepository
        ->getActiveMultiple($entity_type_id, [
        $entity->id(),
        $entity2->id(),
    ], $it_contexts);
    $this->assertSame($it_revision2->getLoadedRevisionId(), $active[$entity->id()]
        ->getLoadedRevisionId());
    $this->assertSame($it_revision2->language()
        ->getId(), $active[$entity->id()]
        ->language()
        ->getId());
    $this->assertSame($entity2->getLoadedRevisionId(), $active[$entity2->id()]
        ->getLoadedRevisionId());
    $this->assertSame($entity2->language()
        ->getId(), $active[$entity2->id()]
        ->language()
        ->getId());
    $this->doTestLanguageFallback('getActive');
}

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