Version 1
[yaffs-website] / web / modules / contrib / entity_reference_revisions / tests / src / Kernel / EntityReferenceRevisionsCompositeTest.php
1 <?php
2
3 namespace Drupal\Tests\entity_reference_revisions\Kernel;
4
5 use Drupal\entity_composite_relationship_test\Entity\EntityTestCompositeRelationship;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
9 use Drupal\language\Entity\ConfigurableLanguage;
10 use Drupal\node\Entity\Node;
11 use Drupal\node\Entity\NodeType;
12 use Drupal\simpletest\ContentTypeCreationTrait;
13 use Drupal\simpletest\NodeCreationTrait;
14
15 /**
16  * Tests the entity_reference_revisions composite relationship.
17  *
18  * @group entity_reference_revisions
19  */
20 class EntityReferenceRevisionsCompositeTest extends EntityKernelTestBase {
21
22   use ContentTypeCreationTrait;
23   use NodeCreationTrait;
24
25   /**
26    * Modules to enable.
27    *
28    * @var array
29    */
30   public static $modules = array(
31     'node',
32     'field',
33     'entity_reference_revisions',
34     'entity_composite_relationship_test',
35     'language'
36   );
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUp() {
42     parent::setUp();
43
44     $this->installEntitySchema('entity_test_composite');
45     $this->installSchema('node', ['node_access']);
46
47     // Create article content type.
48     NodeType::create(['type' => 'article', 'name' => 'Article'])->save();
49
50     // Create the reference to the composite entity test.
51     $field_storage = FieldStorageConfig::create(array(
52       'field_name' => 'composite_reference',
53       'entity_type' => 'node',
54       'type' => 'entity_reference_revisions',
55       'settings' => array(
56         'target_type' => 'entity_test_composite'
57       ),
58     ));
59     $field_storage->save();
60     $field = FieldConfig::create(array(
61       'field_storage' => $field_storage,
62       'bundle' => 'article',
63       'translatable' => FALSE,
64     ));
65     $field->save();
66   }
67
68   /**
69    * Test for maintaining composite relationship.
70    *
71    * Tests that the referenced entity saves the parent type and id when saving.
72    */
73   public function testEntityReferenceRevisionsCompositeRelationship() {
74     // Create the test composite entity.
75     $composite = EntityTestCompositeRelationship::create(array(
76       'uuid' => $this->randomMachineName(),
77       'name' => $this->randomMachineName(),
78     ));
79     $composite->save();
80
81     // Assert that there is only 1 revision of the composite entity.
82     $composite_revisions_count = \Drupal::entityQuery('entity_test_composite')->condition('uuid', $composite->uuid())->allRevisions()->count()->execute();
83     $this->assertEquals(1, $composite_revisions_count);
84
85     // Create a node with a reference to the test composite entity.
86     $node = Node::create(array(
87       'title' => $this->randomMachineName(),
88       'type' => 'article',
89       'composite_reference' => $composite,
90     ));
91     $node->save();
92
93     // Assert that there is only 1 revision when creating a node.
94     $node_revisions_count = \Drupal::entityQuery('node')->condition('nid', $node->id())->allRevisions()->count()->execute();
95     $this->assertEqual($node_revisions_count, 1);
96     // Assert there is no new composite revision after creating a host entity.
97     $composite_revisions_count = \Drupal::entityQuery('entity_test_composite')->condition('uuid', $composite->uuid())->allRevisions()->count()->execute();
98     $this->assertEquals(1, $composite_revisions_count);
99
100     // Verify the value of parent type and id after create a node.
101     $composite = EntityTestCompositeRelationship::load($composite->id());
102     $this->assertEqual($composite->parent_type->value, $node->getEntityTypeId());
103     $this->assertEqual($composite->parent_id->value, $node->id());
104     $this->assertEqual($composite->parent_field_name->value, 'composite_reference');
105     // Create second revision of the node.
106     $original_composite_revision = $node->composite_reference[0]->target_revision_id;
107     $original_node_revision = $node->getRevisionId();
108     $node->setTitle('2nd revision');
109     $node->setNewRevision();
110     $node->save();
111     $node = node_load($node->id(), TRUE);
112     // Check the revision of the node.
113     $this->assertEqual('2nd revision', $node->getTitle(), 'New node revision has changed data.');
114     $this->assertNotEqual($original_composite_revision, $node->composite_reference[0]->target_revision_id, 'Composite entity got new revision when its host did.');
115
116     // Make sure that there are only 2 revisions.
117     $node_revisions_count = \Drupal::entityQuery('node')->condition('nid', $node->id())->allRevisions()->count()->execute();
118     $this->assertEqual($node_revisions_count, 2);
119
120     // Revert to first revision of the node.
121     $node = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($original_node_revision);
122     $node->setNewRevision();
123     $node->isDefaultRevision(TRUE);
124     $node->save();
125     $node = node_load($node->id(), TRUE);
126     // Check the revision of the node.
127     $this->assertNotEqual('2nd revision', $node->getTitle(), 'Node did not keep changed title after reversion.');
128     $this->assertNotEqual($original_composite_revision, $node->composite_reference[0]->target_revision_id, 'Composite entity got new revision when its host reverted to an old revision.');
129
130     // Test that the composite entity is deleted when its parent is deleted.
131     $node->delete();
132     $this->assertNull(EntityTestCompositeRelationship::load($composite->id()));
133   }
134
135   /**
136    * Tests composite relationship with translations and an untranslatable field.
137    */
138   function testCompositeRelationshipWithTranslationNonTranslatableField() {
139
140     ConfigurableLanguage::createFromLangcode('de')->save();
141
142     // Create the test composite entity with a translation.
143     $composite = EntityTestCompositeRelationship::create(array(
144       'uuid' => $this->randomMachineName(),
145       'name' => $this->randomMachineName(),
146     ));
147     $composite->addTranslation('de', $composite->toArray());
148     $composite->save();
149
150
151     // Create a node with a reference to the test composite entity.
152     $node = Node::create(array(
153       'title' => $this->randomMachineName(),
154       'type' => 'article',
155       'composite_reference' => $composite,
156     ));
157     $node->addTranslation('de', $node->toArray());
158     $node->save();
159
160     // Verify the value of parent type and id after create a node.
161     $composite = EntityTestCompositeRelationship::load($composite->id());
162     $this->assertEqual($composite->parent_type->value, $node->getEntityTypeId());
163     $this->assertEqual($composite->parent_id->value, $node->id());
164     $this->assertEqual($composite->parent_field_name->value, 'composite_reference');
165     $this->assertTrue($composite->hasTranslation('de'));
166
167     // Test that the composite entity is not when the german translation of the
168     // parent is deleted.
169     $node->removeTranslation('de');
170     $node->save();
171     $composite = EntityTestCompositeRelationship::load($composite->id());
172     $this->assertNotNull($composite);
173     // @todo Support deleting translations of a composite reference.
174     //   @see https://www.drupal.org/node/2834314.
175     //$this->assertFalse($composite->hasTranslation('de'));
176
177     // Test that the composite entity is deleted when its parent is deleted.
178     $node->delete();
179     $composite = EntityTestCompositeRelationship::load($composite->id());
180     $this->assertNull($composite);
181   }
182
183   /**
184    * Tests composite relationship with translations and a translatable field.
185    */
186   function testCompositeRelationshipWithTranslationTranslatableField() {
187     $field_config = FieldConfig::loadByName('node', 'article', 'composite_reference');
188     $field_config->setTranslatable(TRUE);
189     $field_config->save();
190
191     ConfigurableLanguage::createFromLangcode('de')->save();
192
193     // Create the test composite entity with a translation.
194     $composite = EntityTestCompositeRelationship::create(array(
195       'uuid' => $this->randomMachineName(),
196       'name' => $this->randomMachineName(),
197     ));
198     $composite->addTranslation('de', $composite->toArray());
199     $composite->save();
200
201     // Create a node with a reference to the test composite entity.
202     $node = Node::create(array(
203       'title' => $this->randomMachineName(),
204       'type' => 'article',
205       'composite_reference' => $composite,
206     ));
207     $node->addTranslation('de', $node->toArray());
208     $node->save();
209
210     // Verify the value of parent type and id after create a node.
211     $composite = EntityTestCompositeRelationship::load($composite->id());
212     $this->assertEqual($composite->parent_type->value, $node->getEntityTypeId());
213     $this->assertEqual($composite->parent_id->value, $node->id());
214     $this->assertEqual($composite->parent_field_name->value, 'composite_reference');
215
216     // Test that the composite entity is not when the german translation of the parent is deleted.
217     $node->removeTranslation('de');
218     $node->save();
219     //\Drupal::entityTypeManager()->getStorage('entity_test_composite')->resetCache();
220     $composite = EntityTestCompositeRelationship::load($composite->id());
221     $this->assertNotNull($composite);
222
223     // Test that the composite entity is deleted when its parent is deleted.
224     $node->delete();
225     $composite = EntityTestCompositeRelationship::load($composite->id());
226     // @todo Support deletions for translatable fields.
227     //   @see https://www.drupal.org/node/2834374
228     // $this->assertNull($composite);
229   }
230
231   /**
232    * Tests composite relationship with revisions.
233    */
234   function testCompositeRelationshipWithRevisions() {
235
236     // Create the test composite entity with a translation.
237     $composite = EntityTestCompositeRelationship::create(array(
238       'uuid' => $this->randomMachineName(),
239       'name' => $this->randomMachineName(),
240     ));
241     $composite->save();
242
243     // Create a node with a reference to the test composite entity.
244     $node = Node::create(array(
245       'title' => $this->randomMachineName(),
246       'type' => 'article',
247       'composite_reference' => $composite,
248     ));
249     $node->save();
250
251
252     // Verify the value of parent type and id after create a node.
253     $composite = EntityTestCompositeRelationship::load($composite->id());
254     $composite_original_revision_id = $composite->getRevisionId();
255     $node_original_revision_id = $node->getRevisionId();
256     $this->assertEqual($composite->parent_type->value, $node->getEntityTypeId());
257     $this->assertEqual($composite->parent_id->value, $node->id());
258     $this->assertEqual($composite->parent_field_name->value, 'composite_reference');
259
260     $node->setNewRevision(TRUE);
261     // @todo Enforce this when saving a new revision.
262     $node->composite_reference->entity->setNeedsSave(TRUE);
263     $node->composite_reference->entity->setNewRevision(TRUE);
264     $node->save();
265
266     // Ensure that we saved a new revision ID.
267     $composite = EntityTestCompositeRelationship::load($composite->id());
268     $this->assertNotEqual($composite->getRevisionId(), $composite_original_revision_id);
269
270     // Test that deleting the first revision does not delete the composite.
271     \Drupal::entityTypeManager()->getStorage('node')->deleteRevision($node_original_revision_id);
272     $composite = EntityTestCompositeRelationship::load($composite->id());
273     $this->assertNotNull($composite);
274
275     // Ensure that the composite revision was deleted as well.
276     $composite_revision = \Drupal::entityTypeManager()->getStorage('entity_test_composite')->loadRevision($composite_original_revision_id);
277     // @todo Support host revision delete.
278     //   @see https://www.drupal.org/node/2771523.
279     // $this->assertNull($composite_revision);
280
281     // Test that the composite entity is deleted when its parent is deleted.
282     $node->delete();
283     $composite = EntityTestCompositeRelationship::load($composite->id());
284     $this->assertNull($composite);
285   }
286
287 }