You are here

public function EntityConverterLatestRevisionTest::testWithTranslatedPendingRevision in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/ParamConverter/EntityConverterLatestRevisionTest.php \Drupal\KernelTests\Core\ParamConverter\EntityConverterLatestRevisionTest::testWithTranslatedPendingRevision()

Tests with a translated pending revision.

File

core/tests/Drupal/KernelTests/Core/ParamConverter/EntityConverterLatestRevisionTest.php, line 104

Class

EntityConverterLatestRevisionTest
Tests the entity converter when the "load_latest_revision" flag is set.

Namespace

Drupal\KernelTests\Core\ParamConverter

Code

public function testWithTranslatedPendingRevision() {

  // Enable translation for test entities.
  $this->container
    ->get('state')
    ->set('entity_test.translation', TRUE);
  $this->container
    ->get('entity_type.bundle.info')
    ->clearCachedBundles();

  // Create a new English entity.
  $entity = EntityTestMulRev::create();
  $entity
    ->save();

  // Create a translated pending revision.
  $entity_type_id = 'entity_test_mulrev';

  /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage($entity_type_id);

  /** @var \Drupal\Core\Entity\ContentEntityInterface $translated_entity */
  $translated_entity = $storage
    ->createRevision($entity
    ->addTranslation('de'), FALSE);
  $translated_entity
    ->save();

  // Change the site language so the converters will attempt to load entities
  // with language 'de'.
  $this
    ->config('system.site')
    ->set('default_langcode', 'de')
    ->save();

  // The default loaded language is still 'en'.
  EntityTestMulRev::load($entity
    ->id());
  $this
    ->assertEquals('en', $entity
    ->language()
    ->getId());

  // The converter will load the latest revision in the correct language.
  $converted = $this->converter
    ->convert(1, [
    'load_latest_revision' => TRUE,
    'type' => 'entity:entity_test_mulrev',
  ], 'foo', []);
  $this
    ->assertEquals('de', $converted
    ->language()
    ->getId());
  $this
    ->assertEquals($translated_entity
    ->getLoadedRevisionId(), $converted
    ->getLoadedRevisionId());

  // Revert back to English as default language.
  $this
    ->config('system.site')
    ->set('default_langcode', 'en')
    ->save();

  // The converter will load the latest revision in the correct language.
  $converted = $this->converter
    ->convert(1, [
    'load_latest_revision' => TRUE,
    'type' => 'entity:entity_test_mulrev',
  ], 'foo', []);
  $this
    ->assertEquals('en', $converted
    ->language()
    ->getId());
  $this
    ->assertEquals($entity
    ->getLoadedRevisionId(), $converted
    ->getLoadedRevisionId());
}