function EntityValidationTest::testEntityChangedConstraintOnConcurrentMultilingualEditing

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

Tests the EntityChangedConstraintValidator with multiple translations.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php, line 221

Class

EntityValidationTest
Tests the Entity Validation API.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testEntityChangedConstraintOnConcurrentMultilingualEditing() : void {
    $this->installEntitySchema('entity_test_mulrev_changed');
    $storage = \Drupal::entityTypeManager()->getStorage('entity_test_mulrev_changed');
    // Create a test entity.
    $entity = $this->createTestEntity('entity_test_mulrev_changed');
    $entity->save();
    $entity->setChangedTime($entity->getChangedTime() - 1);
    $violations = $entity->validate();
    $this->assertEquals(1, $violations->count());
    $this->assertEquals('The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.', $violations[0]->getMessage());
    $entity = $storage->loadUnchanged($entity->id());
    $translation = $entity->addTranslation('de');
    $entity->save();
    // Ensure that the new translation has a newer changed timestamp than the
    // default translation.
    $this->assertGreaterThan($entity->getChangedTime(), $translation->getChangedTime());
    // Simulate concurrent form editing by saving the entity with an altered
    // non-translatable field in order for the changed timestamp to be updated
    // across all entity translations.
    $original_entity_time = $entity->getChangedTime();
    $entity->set('not_translatable', $this->randomString());
    $entity->save();
    // Simulate form submission of an uncached form by setting the previous
    // timestamp of an entity translation on the saved entity object. This
    // happens in the entity form API where we put the changed timestamp of
    // the entity in a form hidden value and then set it on the entity which on
    // form submit is loaded from the storage if the form is not yet cached.
    $entity->setChangedTime($original_entity_time);
    // Setting the changed timestamp from the user input on the entity loaded
    // from the storage is used as a prevention from saving a form built with a
    // previous version of the entity and thus reverting changes by other users.
    $violations = $entity->validate();
    $this->assertEquals(1, $violations->count());
    $this->assertEquals('The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.', $violations[0]->getMessage());
}

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