function EntityRevisionTranslationTest::testIsAnyStoredRevisionTranslated

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

Tests that revision translations are correctly detected.

@covers \Drupal\Core\Entity\ContentEntityStorageBase::isAnyStoredRevisionTranslated

File

core/tests/Drupal/KernelTests/Core/Entity/EntityRevisionTranslationTest.php, line 205

Class

EntityRevisionTranslationTest
Tests proper revision propagation of entities.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testIsAnyStoredRevisionTranslated() : void {
    
    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    $storage = $this->entityTypeManager
        ->getStorage('entity_test_mul');
    $method = new \ReflectionMethod(get_class($storage), 'isAnyStoredRevisionTranslated');
    // Check that a non-revisionable new entity is handled correctly.
    $entity = EntityTestMul::create();
    $this->assertEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertFalse($method->invoke($storage, $entity));
    $entity->addTranslation('it');
    $this->assertNotEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertFalse($method->invoke($storage, $entity));
    // Check that not yet stored translations are handled correctly.
    $entity = EntityTestMul::create();
    $entity->save();
    $entity->addTranslation('it');
    $this->assertNotEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertFalse($method->invoke($storage, $entity));
    // Check that removed translations are handled correctly.
    $entity->save();
    $entity->removeTranslation('it');
    $this->assertEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertTrue($method->invoke($storage, $entity));
    $entity->save();
    $this->assertEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertFalse($method->invoke($storage, $entity));
    $entity->addTranslation('de');
    $entity->removeTranslation('de');
    $this->assertEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertFalse($method->invoke($storage, $entity));
    // Check that a non-revisionable not translated entity is handled correctly.
    $entity = EntityTestMul::create();
    $entity->save();
    $this->assertEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertFalse($method->invoke($storage, $entity));
    // Check that a non-revisionable translated entity is handled correctly.
    $entity->addTranslation('it');
    $entity->save();
    $this->assertNotEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertTrue($method->invoke($storage, $entity));
    
    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    $storage = $this->entityTypeManager
        ->getStorage('entity_test_mulrev');
    // Check that a revisionable new entity is handled correctly.
    $entity = EntityTestMulRev::create();
    $this->assertEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertFalse($method->invoke($storage, $entity));
    $entity->addTranslation('it');
    $this->assertNotEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertFalse($method->invoke($storage, $entity));
    // Check that a revisionable not translated entity is handled correctly.
    $entity = EntityTestMulRev::create();
    $entity->save();
    $this->assertEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertFalse($method->invoke($storage, $entity));
    // Check that a revisionable translated pending revision is handled
    // correctly.
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $new_revision */
    $new_revision = $storage->createRevision($entity, FALSE);
    $new_revision->addTranslation('it');
    $new_revision->save();
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $storage->loadUnchanged($entity->id());
    $this->assertEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertNotEmpty($new_revision->getTranslationLanguages(FALSE));
    $this->assertTrue($method->invoke($storage, $entity));
    // Check that a revisionable translated default revision is handled
    // correctly.
    $new_revision->isDefaultRevision(TRUE);
    $new_revision->save();
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $storage->loadUnchanged($entity->id());
    $this->assertNotEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertNotEmpty($new_revision->getTranslationLanguages(FALSE));
    $this->assertTrue($method->invoke($storage, $entity));
}

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