function ContentEntityHasChangesTest::testHasTranslationChanges

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

Tests the correct functionality of the hasTranslationChanges() function.

File

core/tests/Drupal/KernelTests/Core/Entity/ContentEntityHasChangesTest.php, line 42

Class

ContentEntityHasChangesTest
Tests ContentEntityBase::hasTranslationChanges().

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testHasTranslationChanges() : void {
  $user1 = User::create([
    'name' => 'username1',
    'status' => 1,
  ]);
  $user1->save();
  $user2 = User::create([
    'name' => 'username2',
    'status' => 1,
  ]);
  $user2->save();
  /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('entity_test_mulrev_changed_rev');
  /** @var \Drupal\entity_test\Entity\EntityTestMulRevChangedWithRevisionLog $entity */
  $entity = $storage->create([
    'name' => $this->randomString(),
  ]);
  $entity->setRevisionUserId($user1->id());
  $entity->save();
  $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes after the entity has been saved.');
  // Update the revision metadata fields and the changed field, which should
  // be skipped from checking for changes in
  // ContentEntityBase::hasTranslationChanges().
  $entity_previous_rev_id = $entity->getRevisionId();
  // Revision metadata field revision_timestamp.
  $entity->setRevisionCreationTime(time() + 1);
  // Revision metadata field revision_uid.
  $entity->setRevisionUserId($user2->id());
  // Revision metadata field revision_log.
  $entity->setRevisionLogMessage('test');
  // Revision metadata field revision_translation_affected.
  $entity->setRevisionTranslationAffected(TRUE);
  // Changed field.
  $entity->setChangedTime(time() + 1);
  // Check that the revision metadata fields and the changed field have been
  // skipped when comparing same revisions.
  $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
  // Check that the revision metadata fields and the changed field have been
  // skipped when comparing same revisions with enforced new revision to be
  // created on save.
  $entity->setNewRevision(TRUE);
  $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
  // Save the entity in new revision with changes on the revision metadata
  // fields.
  $entity->save();
  // Check that the revision metadata fields and the changed field have been
  // skipped when comparing different revisions.
  $entity = $storage->loadRevision($entity_previous_rev_id);
  $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
}

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