3 namespace Drupal\Tests\entity_reference_revisions\Kernel;
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;
16 * Tests the entity_reference_revisions composite relationship.
18 * @group entity_reference_revisions
20 class EntityReferenceRevisionsCompositeTest extends EntityKernelTestBase {
22 use ContentTypeCreationTrait;
23 use NodeCreationTrait;
30 public static $modules = array(
33 'entity_reference_revisions',
34 'entity_composite_relationship_test',
41 protected function setUp() {
44 $this->installEntitySchema('entity_test_composite');
45 $this->installSchema('node', ['node_access']);
47 // Create article content type.
48 NodeType::create(['type' => 'article', 'name' => 'Article'])->save();
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',
56 'target_type' => 'entity_test_composite'
59 $field_storage->save();
60 $field = FieldConfig::create(array(
61 'field_storage' => $field_storage,
62 'bundle' => 'article',
63 'translatable' => FALSE,
69 * Test for maintaining composite relationship.
71 * Tests that the referenced entity saves the parent type and id when saving.
73 public function testEntityReferenceRevisionsCompositeRelationship() {
74 // Create the test composite entity.
75 $composite = EntityTestCompositeRelationship::create(array(
76 'uuid' => $this->randomMachineName(),
77 'name' => $this->randomMachineName(),
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);
85 // Create a node with a reference to the test composite entity.
86 $node = Node::create(array(
87 'title' => $this->randomMachineName(),
89 'composite_reference' => $composite,
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);
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();
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.');
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);
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);
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.');
130 // Test that the composite entity is deleted when its parent is deleted.
132 $this->assertNull(EntityTestCompositeRelationship::load($composite->id()));
136 * Tests composite relationship with translations and an untranslatable field.
138 function testCompositeRelationshipWithTranslationNonTranslatableField() {
140 ConfigurableLanguage::createFromLangcode('de')->save();
142 // Create the test composite entity with a translation.
143 $composite = EntityTestCompositeRelationship::create(array(
144 'uuid' => $this->randomMachineName(),
145 'name' => $this->randomMachineName(),
147 $composite->addTranslation('de', $composite->toArray());
151 // Create a node with a reference to the test composite entity.
152 $node = Node::create(array(
153 'title' => $this->randomMachineName(),
155 'composite_reference' => $composite,
157 $node->addTranslation('de', $node->toArray());
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'));
167 // Test that the composite entity is not when the german translation of the
168 // parent is deleted.
169 $node->removeTranslation('de');
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'));
177 // Test that the composite entity is deleted when its parent is deleted.
179 $composite = EntityTestCompositeRelationship::load($composite->id());
180 $this->assertNull($composite);
184 * Tests composite relationship with translations and a translatable field.
186 function testCompositeRelationshipWithTranslationTranslatableField() {
187 $field_config = FieldConfig::loadByName('node', 'article', 'composite_reference');
188 $field_config->setTranslatable(TRUE);
189 $field_config->save();
191 ConfigurableLanguage::createFromLangcode('de')->save();
193 // Create the test composite entity with a translation.
194 $composite = EntityTestCompositeRelationship::create(array(
195 'uuid' => $this->randomMachineName(),
196 'name' => $this->randomMachineName(),
198 $composite->addTranslation('de', $composite->toArray());
201 // Create a node with a reference to the test composite entity.
202 $node = Node::create(array(
203 'title' => $this->randomMachineName(),
205 'composite_reference' => $composite,
207 $node->addTranslation('de', $node->toArray());
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');
216 // Test that the composite entity is not when the german translation of the parent is deleted.
217 $node->removeTranslation('de');
219 //\Drupal::entityTypeManager()->getStorage('entity_test_composite')->resetCache();
220 $composite = EntityTestCompositeRelationship::load($composite->id());
221 $this->assertNotNull($composite);
223 // Test that the composite entity is deleted when its parent is deleted.
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);
232 * Tests composite relationship with revisions.
234 function testCompositeRelationshipWithRevisions() {
236 // Create the test composite entity with a translation.
237 $composite = EntityTestCompositeRelationship::create(array(
238 'uuid' => $this->randomMachineName(),
239 'name' => $this->randomMachineName(),
243 // Create a node with a reference to the test composite entity.
244 $node = Node::create(array(
245 'title' => $this->randomMachineName(),
247 'composite_reference' => $composite,
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');
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);
266 // Ensure that we saved a new revision ID.
267 $composite = EntityTestCompositeRelationship::load($composite->id());
268 $this->assertNotEqual($composite->getRevisionId(), $composite_original_revision_id);
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);
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);
281 // Test that the composite entity is deleted when its parent is deleted.
283 $composite = EntityTestCompositeRelationship::load($composite->id());
284 $this->assertNull($composite);