function ContentEntityChangedTest::testRevisionChanged

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/ContentEntityChangedTest.php \Drupal\KernelTests\Core\Entity\ContentEntityChangedTest::testRevisionChanged()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/ContentEntityChangedTest.php \Drupal\KernelTests\Core\Entity\ContentEntityChangedTest::testRevisionChanged()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/ContentEntityChangedTest.php \Drupal\KernelTests\Core\Entity\ContentEntityChangedTest::testRevisionChanged()

Tests revisionable EntityChangedInterface functionality.

File

core/tests/Drupal/KernelTests/Core/Entity/ContentEntityChangedTest.php, line 244

Class

ContentEntityChangedTest
Tests basic EntityChangedInterface functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testRevisionChanged() {
    $user1 = $this->createUser();
    $user2 = $this->createUser();
    // Create a test entity.
    $entity = EntityTestMulRevChanged::create([
        'name' => $this->randomString(),
        'user_id' => $user1->id(),
        'language' => 'en',
    ]);
    $entity->save();
    $this->assertTrue($entity->getChangedTime() >= REQUEST_TIME, 'Changed time of original language is valid.');
    // We can't assert equality here because the created time is set to the
    // request time while instances of ChangedTestItem use the current
    // timestamp every time.
    $this->assertTrue($entity->getChangedTime() >= $entity->get('created')->value && $entity->getChangedTime() - $entity->get('created')->value <= time() - REQUEST_TIME, 'Changed and created time of original language can be assumed to be identical.');
    $this->assertEquals($entity->getChangedTimeAcrossTranslations(), $entity->getChangedTime(), 'Changed time of original language is the same as changed time across all translations.');
    $this->assertTrue($this->getRevisionTranslationAffectedFlag($entity), 'Changed flag of original language is set for a new entity.');
    $changed_en = $entity->getChangedTime();
    $entity->setNewRevision();
    // Save entity without any changes but create new revision.
    $entity->save();
    // A new revision without any changes should not set a new changed time.
    $this->assertEquals($changed_en, $entity->getChangedTime(), 'Changed time of original language did not change.');
    $this->assertFalse($this->getRevisionTranslationAffectedFlag($entity), 'Changed flag of original language is not set for new revision without changes.');
    $entity->setNewRevision();
    $entity->setOwner($user2);
    $entity->save();
    $this->assertTrue($entity->getChangedTime() > $changed_en, 'Changed time of original language has been updated by new revision.');
    $this->assertTrue($this->getRevisionTranslationAffectedFlag($entity), 'Changed flag of original language is set for new revision with changes.');
    $changed_en = $entity->getChangedTime();
    
    /** @var \Drupal\entity_test\Entity\EntityTestMulRevChanged $german */
    $german = $entity->addTranslation('de');
    $entity->save();
    $this->assertEquals($changed_en, $entity->getChangedTime(), 'Changed time of original language did not change.');
    $this->assertTrue($german->getChangedTime() > $entity->getChangedTime(), 'Changed time of the German translation is newer then the original language.');
    $this->assertEquals($entity->getChangedTimeAcrossTranslations(), $german->getChangedTime(), 'Changed time of the German translation is the newest time across all translations.');
    $this->assertTrue($this->getRevisionTranslationAffectedFlag($entity), 'Changed flag of original language is not reset by adding a new translation.');
    $this->assertTrue($this->getRevisionTranslationAffectedFlag($german), 'Changed flag of German translation is set when adding the translation.');
    $changed_de = $german->getChangedTime();
    $entity->setNewRevision();
    // Save entity without any changes but create new revision.
    $entity->save();
    $this->assertEquals($changed_en, $entity->getChangedTime(), 'Changed time of original language did not change.');
    $this->assertEquals($changed_de, $german->getChangedTime(), 'Changed time of the German translation did not change.');
    $this->assertFalse($this->getRevisionTranslationAffectedFlag($entity), 'Changed flag of original language is not set for new revision without changes.');
    $this->assertFalse($this->getRevisionTranslationAffectedFlag($german), 'Changed flag of the German translation is not set for new revision without changes.');
    $entity->setNewRevision();
    $german->setOwner($user2);
    $entity->save();
    $this->assertEquals($changed_en, $entity->getChangedTime(), 'Changed time of original language did not change.');
    $this->assertTrue($german->getChangedTime() > $changed_de, 'Changed time of the German translation did change.');
    $this->assertEquals($entity->getChangedTimeAcrossTranslations(), $german->getChangedTime(), 'Changed time of the German translation is the newest time across all translations.');
    $this->assertFalse($this->getRevisionTranslationAffectedFlag($entity), 'Changed flag of original language is not set when changing the German Translation.');
    $this->assertTrue($this->getRevisionTranslationAffectedFlag($german), 'Changed flag of German translation is set when changing the German translation.');
    $french = $entity->addTranslation('fr');
    $entity->setNewRevision();
    $entity->save();
    $this->assertEquals($changed_en, $entity->getChangedTime(), 'Changed time of original language did not change.');
    $this->assertTrue($french->getChangedTime() > $entity->getChangedTime(), 'Changed time of the French translation is newer then the original language.');
    $this->assertTrue($french->getChangedTime() > $entity->getChangedTime(), 'Changed time of the French translation is newer then the German translation.');
    $this->assertEquals($entity->getChangedTimeAcrossTranslations(), $french->getChangedTime(), 'Changed time of the French translation is the newest time across all translations.');
    $this->assertFalse($this->getRevisionTranslationAffectedFlag($entity), 'Changed flag of original language is reset by adding a new translation and a new revision.');
    $this->assertFalse($this->getRevisionTranslationAffectedFlag($german), 'Changed flag of German translation is reset by adding a new translation and a new revision.');
    $this->assertTrue($this->getRevisionTranslationAffectedFlag($french), 'Changed flag of French translation is set when adding the translation and a new revision.');
    $entity->removeTranslation('fr');
    $entity->setNewRevision();
    $entity->save();
    // This block simulates exactly the flow of a node form submission of a new
    // translation and a new revision.
    $form_entity_builder_entity = EntityTestMulRevChanged::load($entity->id());
    // ContentTranslationController::prepareTranslation().
    $form_entity_builder_entity = $form_entity_builder_entity->addTranslation('fr', $form_entity_builder_entity->toArray());
    // EntityForm::buildEntity() during form submit.
    $form_entity_builder_clone = clone $form_entity_builder_entity;
    // NodeForm::submitForm().
    $form_entity_builder_clone->setNewRevision();
    // EntityForm::save().
    $form_entity_builder_clone->save();
    // The assertion fails unless https://www.drupal.org/node/2513094 is
    // committed.
    $this->assertFalse($this->getRevisionTranslationAffectedFlag($entity), 'Changed flag of original language is reset by adding a new translation and a new revision.');
    $this->assertFalse($this->getRevisionTranslationAffectedFlag($german), 'Changed flag of German translation is reset by adding a new translation and a new revision.');
    $this->assertTrue($this->getRevisionTranslationAffectedFlag($french), 'Changed flag of French translation is set when adding the translation and a new revision.');
    // Since above a clone of the entity was saved and then this entity is saved
    // again, we have to update the revision ID to the current one.
    $german->set('revision_id', $form_entity_builder_clone->getRevisionId());
    $german->updateLoadedRevisionId();
    $german->setOwner($user1);
    $german->setRevisionTranslationAffected(FALSE);
    $entity->save();
    $this->assertFalse($this->getRevisionTranslationAffectedFlag($german), 'German translation changed but the changed flag is reset manually.');
    $entity->setNewRevision();
    $german->setRevisionTranslationAffected(TRUE);
    $entity->save();
    $this->assertTrue($this->getRevisionTranslationAffectedFlag($german), 'German translation is not changed and a new revision is created but the changed flag is set manually.');
    $german->setOwner($user2);
    $entity->setNewRevision();
    $german->setRevisionTranslationAffected(FALSE);
    $entity->save();
    $this->assertFalse($this->getRevisionTranslationAffectedFlag($german), 'German translation changed and a new revision is created but the changed flag is reset manually.');
}

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