Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / ContentEntityHasChangesTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\user\Entity\User;
7
8 /**
9  * Tests ContentEntityBase::hasTranslationChanges().
10  *
11  * @group Entity
12  */
13 class ContentEntityHasChangesTest extends KernelTestBase {
14
15   /**
16    * Bundle of entity.
17    *
18    * @var string
19    */
20   protected $bundle = 'test';
21
22   /**
23    * {@inheritdoc}
24    */
25   public static $modules = ['system', 'user', 'entity_test'];
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     parent::setUp();
32
33     $this->installEntitySchema('user');
34     $this->installEntitySchema('entity_test_mulrev_chnged_revlog');
35     $this->installSchema('system', 'sequences');
36   }
37
38   /**
39    * Tests the correct functionality of the hasTranslationChanges() function.
40    */
41   public function testHasTranslationChanges() {
42     $user1 = User::create([
43       'name' => 'username1',
44       'status' => 1,
45     ]);
46     $user1->save();
47
48     $user2 = User::create([
49       'name' => 'username2',
50       'status' => 1,
51     ]);
52     $user2->save();
53
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(),
60     ]);
61     $entity->setRevisionUserId($user1->id());
62     $entity->save();
63
64     $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes after the entity has been saved.');
65
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);
78     // Changed field.
79     $entity->setChangedTime(time() + 1);
80
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.');
84
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
87     // created on save.
88     $entity->setNewRevision(TRUE);
89     $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
90
91     // Save the entity in new revision with changes on the revision metadata
92     // fields.
93     $entity->save();
94
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.');
99   }
100
101 }