3 namespace Drupal\KernelTests\Core\Entity;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\user\Entity\User;
9 * Tests ContentEntityBase::hasTranslationChanges().
13 class ContentEntityHasChangesTest extends KernelTestBase {
20 protected $bundle = 'test';
25 public static $modules = ['system', 'user', 'entity_test'];
30 protected function setUp() {
33 $this->installEntitySchema('user');
34 $this->installEntitySchema('entity_test_mulrev_chnged_revlog');
35 $this->installSchema('system', 'sequences');
39 * Tests the correct functionality of the hasTranslationChanges() function.
41 public function testHasTranslationChanges() {
42 $user1 = User::create([
43 'name' => 'username1',
48 $user2 = User::create([
49 'name' => 'username2',
54 /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
55 $storage = $this->container->get('entity_type.manager')
56 ->getStorage('entity_test_mulrev_chnged_revlog');
57 /** @var \Drupal\entity_test\Entity\EntityTestMulRevChangedWithRevisionLog $entity */
58 $entity = $storage->create([
59 'name' => $this->randomString(),
61 $entity->setRevisionUserId($user1->id());
64 $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes after the entity has been saved.');
66 // Update the revision metadata fields and the changed field, which should
67 // be skipped from checking for changes in
68 // ContentEntityBase::hasTranslationChanges().
69 $entity_previous_rev_id = $entity->getRevisionId();
70 // Revision metadata field revision_timestamp.
71 $entity->setRevisionCreationTime(time() + 1);
72 // Revision metadata field revision_uid.
73 $entity->setRevisionUserId($user2->id());
74 // Revision metadata field revision_log.
75 $entity->setRevisionLogMessage('test');
76 // Revision metadata field revision_translation_affected.
77 $entity->setRevisionTranslationAffected(TRUE);
79 $entity->setChangedTime(time() + 1);
81 // Check that the revision metadata fields and the changed field have been
82 // skipped when comparing same revisions.
83 $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
85 // Check that the revision metadata fields and the changed field have been
86 // skipped when comparing same revisions with enforced new revision to be
88 $entity->setNewRevision(TRUE);
89 $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
91 // Save the entity in new revision with changes on the revision metadata
95 // Check that the revision metadata fields and the changed field have been
96 // skipped when comparing different revisions.
97 $entity = $storage->loadRevision($entity_previous_rev_id);
98 $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');