a3b99fe0c95e836dc8262cc72bfeeb977a3d190e
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / EntityLoadedRevisionTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity;
4
5 use Drupal\entity_test\Entity\EntityTestMulRev;
6 use Drupal\language\Entity\ConfigurableLanguage;
7
8 /**
9  * Tests the loaded Revision of an entity.
10  *
11  * @group entity
12  */
13 class EntityLoadedRevisionTest extends EntityKernelTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = [
21     'system',
22     'entity_test',
23     'language',
24     'content_translation',
25   ];
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     parent::setUp();
32
33     $this->installEntitySchema('entity_test_mulrev');
34   }
35
36   /**
37    * Test getLoadedRevisionId() returns the correct ID throughout the process.
38    */
39   public function testLoadedRevisionId() {
40     // Create a basic EntityTestMulRev entity and save it.
41     $entity = EntityTestMulRev::create();
42     $entity->save();
43
44     // Load the created entity and create a new revision.
45     $loaded = EntityTestMulRev::load($entity->id());
46     $loaded->setNewRevision(TRUE);
47
48     // Before saving, the loaded Revision ID should be the same as the created
49     // entity, not the same as the loaded entity (which does not have a revision
50     // ID yet).
51     $this->assertEquals($entity->getRevisionId(), $loaded->getLoadedRevisionId());
52     $this->assertNotEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
53     $this->assertSame(NULL, $loaded->getRevisionId());
54
55     // After updating the loaded Revision ID the result should be the same.
56     $loaded->updateLoadedRevisionId();
57     $this->assertEquals($entity->getRevisionId(), $loaded->getLoadedRevisionId());
58     $this->assertNotEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
59     $this->assertSame(NULL, $loaded->getRevisionId());
60
61     $loaded->save();
62
63     // In entity_test_entity_update() the loaded Revision ID was stored in
64     // state. This should be the same as we had before calling $loaded->save().
65     /** @var \Drupal\Core\Entity\ContentEntityInterface $loaded_original */
66     $loadedRevisionId = \Drupal::state()->get('entity_test.loadedRevisionId');
67     $this->assertEquals($entity->getRevisionId(), $loadedRevisionId);
68     $this->assertNotEquals($loaded->getRevisionId(), $loadedRevisionId);
69
70     // The revision ID and loaded Revision ID should be different for the two
71     // versions of the entity, but the same for a saved entity.
72     $this->assertNotEquals($loaded->getRevisionId(), $entity->getRevisionId());
73     $this->assertNotEquals($loaded->getLoadedRevisionId(), $entity->getLoadedRevisionId());
74     $this->assertEquals($entity->getRevisionId(), $entity->getLoadedRevisionId());
75     $this->assertEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
76   }
77
78   /**
79    * Tests the loaded revision ID after an entity re-save, clone and duplicate.
80    */
81   public function testLoadedRevisionIdWithNoNewRevision() {
82     // Create a basic EntityTestMulRev entity and save it.
83     $entity = EntityTestMulRev::create();
84     $entity->save();
85
86     // Load the created entity and create a new revision.
87     $loaded = EntityTestMulRev::load($entity->id());
88     $loaded->setNewRevision(TRUE);
89     $loaded->save();
90
91     // Make a change to the loaded entity.
92     $loaded->set('name', 'dublin');
93
94     // The revision id and loaded Revision id should still be the same.
95     $this->assertEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
96
97     $loaded->save();
98
99     // After saving, the loaded Revision id set in entity_test_entity_update()
100     // and returned from the entity should be the same as the entity's revision
101     // id because a new revision wasn't created, the existing revision was
102     // updated.
103     $loadedRevisionId = \Drupal::state()->get('entity_test.loadedRevisionId');
104     $this->assertEquals($loaded->getRevisionId(), $loadedRevisionId);
105     $this->assertEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
106
107     // Creating a clone should keep the loaded Revision ID.
108     $clone = clone $loaded;
109     $this->assertSame($loaded->getLoadedRevisionId(), $clone->getLoadedRevisionId());
110
111     // Creating a duplicate should set a NULL loaded Revision ID.
112     $duplicate = $loaded->createDuplicate();
113     $this->assertSame(NULL, $duplicate->getLoadedRevisionId());
114   }
115
116   /**
117    * Tests the loaded revision ID for translatable entities.
118    */
119   public function testTranslatedLoadedRevisionId() {
120     ConfigurableLanguage::createFromLangcode('fr')->save();
121
122     // Create a basic EntityTestMulRev entity and save it.
123     $entity = EntityTestMulRev::create();
124     $entity->save();
125
126     // Load the created entity and create a new revision.
127     $loaded = EntityTestMulRev::load($entity->id());
128     $loaded->setNewRevision(TRUE);
129     $loaded->save();
130
131     // Check it all works with translations.
132     $french = $loaded->addTranslation('fr');
133     // Adding a revision should return the same for each language.
134     $this->assertEquals($french->getRevisionId(), $french->getLoadedRevisionId());
135     $this->assertEquals($loaded->getRevisionId(), $french->getLoadedRevisionId());
136     $this->assertEquals($loaded->getLoadedRevisionId(), $french->getLoadedRevisionId());
137     $french->save();
138     // After saving nothing should change.
139     $this->assertEquals($french->getRevisionId(), $french->getLoadedRevisionId());
140     $this->assertEquals($loaded->getRevisionId(), $french->getLoadedRevisionId());
141     $this->assertEquals($loaded->getLoadedRevisionId(), $french->getLoadedRevisionId());
142     $first_revision_id = $french->getRevisionId();
143     $french->setNewRevision();
144     // Setting a new revision will reset the loaded Revision ID.
145     $this->assertEquals($first_revision_id, $french->getLoadedRevisionId());
146     $this->assertEquals($first_revision_id, $loaded->getLoadedRevisionId());
147     $this->assertNotEquals($french->getRevisionId(), $french->getLoadedRevisionId());
148     $this->assertGreaterThan($french->getRevisionId(), $french->getLoadedRevisionId());
149     $this->assertNotEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
150     $this->assertGreaterThan($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
151     $french->save();
152     // Saving the new revision will reset the origin revision ID again.
153     $this->assertEquals($french->getRevisionId(), $french->getLoadedRevisionId());
154     $this->assertEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
155   }
156
157   /**
158    * Tests re-saving the entity in entity_test_entity_insert().
159    */
160   public function testSaveInHookEntityInsert() {
161     // Create an entity which will be saved again in entity_test_entity_insert().
162     $entity = EntityTestMulRev::create(['name' => 'EntityLoadedRevisionTest']);
163     $entity->save();
164     $loadedRevisionId = \Drupal::state()->get('entity_test.loadedRevisionId');
165     $this->assertEquals($entity->getLoadedRevisionId(), $loadedRevisionId);
166     $this->assertEquals($entity->getRevisionId(), $entity->getLoadedRevisionId());
167
168   }
169
170 }