function EntityRevisionTranslationTest::testTranslationValuesWhenSavingPendingRevisions

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityRevisionTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityRevisionTranslationTest::testTranslationValuesWhenSavingPendingRevisions()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityRevisionTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityRevisionTranslationTest::testTranslationValuesWhenSavingPendingRevisions()
  3. 11.x 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 102

Class

EntityRevisionTranslationTest
Tests proper revision propagation of entities.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testTranslationValuesWhenSavingPendingRevisions() : void {
  $user = $this->createUser();
  /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
  $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('updated pending revision - en', $pending_revision->name->value);
  $this->assertEquals('pending revision - de', $pending_revision->getTranslation('de')->name->value);
}

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