ContentEntityHasChangesTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Drupal\KernelTests\Core\Entity;
  3. use Drupal\KernelTests\KernelTestBase;
  4. use Drupal\user\Entity\User;
  5. /**
  6. * Tests ContentEntityBase::hasTranslationChanges().
  7. *
  8. * @group Entity
  9. */
  10. class ContentEntityHasChangesTest extends KernelTestBase {
  11. /**
  12. * Bundle of entity.
  13. *
  14. * @var string
  15. */
  16. protected $bundle = 'test';
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public static $modules = ['system', 'user', 'entity_test'];
  21. /**
  22. * {@inheritdoc}
  23. */
  24. protected function setUp() {
  25. parent::setUp();
  26. $this->installEntitySchema('user');
  27. $this->installEntitySchema('entity_test_mulrev_chnged_revlog');
  28. $this->installSchema('system', 'sequences');
  29. }
  30. /**
  31. * Tests the correct functionality of the hasTranslationChanges() function.
  32. */
  33. public function testHasTranslationChanges() {
  34. $user1 = User::create([
  35. 'name' => 'username1',
  36. 'status' => 1,
  37. ]);
  38. $user1->save();
  39. $user2 = User::create([
  40. 'name' => 'username2',
  41. 'status' => 1,
  42. ]);
  43. $user2->save();
  44. /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
  45. $storage = $this->container->get('entity_type.manager')
  46. ->getStorage('entity_test_mulrev_chnged_revlog');
  47. /** @var \Drupal\entity_test\Entity\EntityTestMulRevChangedWithRevisionLog $entity */
  48. $entity = $storage->create([
  49. 'name' => $this->randomString(),
  50. ]);
  51. $entity->setRevisionUserId($user1->id());
  52. $entity->save();
  53. $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes after the entity has been saved.');
  54. // Update the revision metadata fields and the changed field, which should
  55. // be skipped from checking for changes in
  56. // ContentEntityBase::hasTranslationChanges().
  57. $entity_previous_rev_id = $entity->getRevisionId();
  58. // Revision metadata field revision_timestamp.
  59. $entity->setRevisionCreationTime(time() + 1);
  60. // Revision metadata field revision_uid.
  61. $entity->setRevisionUserId($user2->id());
  62. // Revision metadata field revision_log.
  63. $entity->setRevisionLogMessage('test');
  64. // Revision metadata field revision_translation_affected.
  65. $entity->setRevisionTranslationAffected(TRUE);
  66. // Changed field.
  67. $entity->setChangedTime(time() + 1);
  68. // Check that the revision metadata fields and the changed field have been
  69. // skipped when comparing same revisions.
  70. $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
  71. // Check that the revision metadata fields and the changed field have been
  72. // skipped when comparing same revisions with enforced new revision to be
  73. // created on save.
  74. $entity->setNewRevision(TRUE);
  75. $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
  76. // Save the entity in new revision with changes on the revision metadata
  77. // fields.
  78. $entity->save();
  79. // Check that the revision metadata fields and the changed field have been
  80. // skipped when comparing different revisions.
  81. $entity = $storage->loadRevision($entity_previous_rev_id);
  82. $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
  83. }
  84. }