You are here

public function EntityRevisionTranslationTest::testTranslationValuesWhenSavingPendingRevisions in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityRevisionTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityRevisionTranslationTest::testTranslationValuesWhenSavingPendingRevisions()

Tests the translation values when saving a pending revision.

File

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

Class

EntityRevisionTranslationTest
Tests proper revision propagation of entities.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testTranslationValuesWhenSavingPendingRevisions() {
  $user = $this
    ->createUser();
  $storage = $this->entityTypeManager
    ->getStorage('entity_test_mulrev');

  // Create a test entity and a translation for it.
  $entity = EntityTestMulRev::create([
    'name' => 'default revision - en',
    'user_id' => $user
      ->id(),
    'language' => 'en',
  ]);
  $entity
    ->addTranslation('de', [
    'name' => 'default revision - de',
  ]);
  $entity
    ->save();

  // Create a pending revision for the entity and change a field value for
  // both languages.
  $pending_revision = $this
    ->reloadEntity($entity);
  $pending_revision
    ->setNewRevision();
  $pending_revision
    ->isDefaultRevision(FALSE);
  $pending_revision->name = 'pending revision - en';
  $pending_revision
    ->save();
  $pending_revision_translation = $pending_revision
    ->getTranslation('de');
  $pending_revision_translation->name = 'pending revision - de';
  $pending_revision_translation
    ->save();
  $pending_revision_id = $pending_revision
    ->getRevisionId();
  $pending_revision = $storage
    ->loadRevision($pending_revision_id);

  // Change the value of the field in the default language, save the pending
  // revision and check that the value of the field in the second language is
  // also taken from the pending revision, *not* from the default revision.
  $pending_revision->name = 'updated pending revision - en';
  $pending_revision
    ->save();
  $pending_revision = $storage
    ->loadRevision($pending_revision_id);
  $this
    ->assertEquals($pending_revision->name->value, 'updated pending revision - en');
  $this
    ->assertEquals($pending_revision
    ->getTranslation('de')->name->value, 'pending revision - de');
}