function EntityFieldTest::testFieldEntityRevisionWrite

Same name and namespace in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php \Drupal\KernelTests\Core\Entity\EntityFieldTest::testFieldEntityRevisionWrite()

Test setting field values on revisionable entities.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php, line 106

Class

EntityFieldTest
Tests the Entity Field API.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testFieldEntityRevisionWrite() : void {
    
    /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()->getStorage('entity_test_rev');
    // Create a new entity, with a field value 'foo'.
    $entity = EntityTestRev::create();
    $entity->field_test_text->value = 'foo';
    $entity->save();
    // Create a new non-default revision and set the field value to 'bar'.
    $entity->setNewRevision(TRUE);
    $entity->isDefaultRevision(FALSE);
    $entity->field_test_text->value = 'bar';
    $entity->save();
    $forward_revision_id = $entity->getRevisionId();
    // Load the forward revision and set the field value to equal the value of
    // the default revision.
    $forward_revision = $storage->loadRevision($forward_revision_id);
    $forward_revision->field_test_text->value = 'foo';
    $forward_revision->save();
    $storage->resetCache();
    // The updated field value should have correctly saved as 'foo'.
    $forward_revision = $storage->loadRevision($forward_revision_id);
    $this->assertEquals('foo', $forward_revision->field_test_text->value);
}

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