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\KernelTestBase;
9 use Drupal\node\Entity\Node;
10 use Drupal\node\Entity\NodeType;
13 * Tests the entity_reference_revisions NeedsSaveInterface.
15 * @group entity_reference_revisions
17 class EntityReferenceRevisionsSaveTest extends KernelTestBase {
24 public static $modules = array(
29 'entity_reference_revisions',
30 'entity_composite_relationship_test',
36 protected function setUp() {
38 // Create article content type.
39 $values = ['type' => 'article', 'name' => 'Article'];
40 $node_type = NodeType::create($values);
42 $this->installEntitySchema('user');
43 $this->installEntitySchema('node');
44 $this->installEntitySchema('entity_test_composite');
45 $this->installSchema('system', ['sequences']);
46 $this->installSchema('node', ['node_access']);
50 * Test for NeedsSaveInterface implementation.
52 * Tests that the referenced entity is saved when needsSave() is TRUE.
54 public function testNeedsSave() {
56 // Add the entity_reference_revisions field to article.
57 $field_storage = FieldStorageConfig::create(array(
58 'field_name' => 'composite_reference',
59 'entity_type' => 'node',
60 'type' => 'entity_reference_revisions',
62 'target_type' => 'entity_test_composite'
65 $field_storage->save();
66 $field = FieldConfig::create(array(
67 'field_storage' => $field_storage,
68 'bundle' => 'article',
73 // Create the test composite entity.
74 $entity_test = EntityTestCompositeRelationship::create(array(
80 $text = 'Clever text';
81 // Set the name to a new text.
82 /** @var \Drupal\entity_composite_relationship_test\Entity\EntityTestCompositeRelationship $entity_test */
83 $entity_test->name = $text;
84 $entity_test->setNeedsSave(TRUE);
85 // Create a node with a reference to the test entity and save.
86 $node = Node::create([
87 'title' => $this->randomMachineName(),
89 'composite_reference' => $entity_test,
91 // Check the name is properly set and that getValue() returns the entity
92 // when it is marked as needs save."
93 $values = $node->composite_reference->getValue();
94 $this->assertTrue(isset($values[0]['entity']));
95 static::assertEquals($values[0]['entity']->name->value, $text);
96 $node->composite_reference->setValue($values);
97 static::assertEquals($node->composite_reference->entity->name->value, $text);
100 // Check that the name has been updated when the parent has been saved.
101 /** @var \Drupal\entity_composite_relationship_test\Entity\EntityTestCompositeRelationship $entity_test_after */
102 $entity_test_after = EntityTestCompositeRelationship::load($entity_test->id());
103 static::assertEquals($entity_test_after->name->value, $text);
105 $new_text = 'Dummy text again';
106 // Set another name and save the node without marking it as needs saving.
107 $entity_test_after->name = $new_text;
108 $entity_test_after->setNeedsSave(FALSE);
110 // Load the Node and check the composite reference entity is not returned
111 // from getValue() if it is not marked as needs saving.
112 $node = Node::load($node->id());
113 $values = $node->composite_reference->getValue();
114 $this->assertFalse(isset($values[0]['entity']));
115 $node->composite_reference = $entity_test_after;
118 // Check the name is not updated.
119 \Drupal::entityTypeManager()->getStorage('entity_test_composite')->resetCache();
120 $entity_test_after = EntityTestCompositeRelationship::load($entity_test->id());
121 static::assertEquals($text, $entity_test_after->name->value);
123 // Test if after delete the referenced entity there are no problems setting
124 // the referencing values to the parent.
125 $entity_test->delete();
126 $node = Node::load($node->id());
129 // Test if the needs save variable is set as false after saving.
130 $entity_needs_save = EntityTestCompositeRelationship::create([
134 $entity_needs_save->setNeedsSave(TRUE);
135 $entity_needs_save->save();
136 $this->assertFalse($entity_needs_save->needsSave());
140 * Test for NeedsSaveInterface implementation.
142 * Tests that the fields in the parent are properly updated.
144 public function testSaveNewEntity() {
145 // Add the entity_reference_revisions field to article.
146 $field_storage = FieldStorageConfig::create(array(
147 'field_name' => 'composite_reference',
148 'entity_type' => 'node',
149 'type' => 'entity_reference_revisions',
151 'target_type' => 'entity_test_composite'
154 $field_storage->save();
155 $field = FieldConfig::create(array(
156 'field_storage' => $field_storage,
157 'bundle' => 'article',
161 $text = 'Dummy text';
162 // Create the test entity.
163 $entity_test = EntityTestCompositeRelationship::create(array(
168 // Create a node with a reference to the test entity and save.
169 $node = Node::create([
170 'title' => $this->randomMachineName(),
172 'composite_reference' => $entity_test,
174 $validate = $node->validate();
175 $this->assertEmpty($validate);
178 // Test that the fields on node are properly set.
179 $node_after = Node::load($node->id());
180 static::assertEquals($node_after->composite_reference[0]->target_id, $entity_test->id());
181 static::assertEquals($node_after->composite_reference[0]->target_revision_id, $entity_test->getRevisionId());
182 // Check that the entity is not new after save parent.
183 $this->assertFalse($entity_test->isNew());
185 // Create a new test entity.
186 $text = 'Smart text';
187 $second_entity_test = EntityTestCompositeRelationship::create(array(
191 $second_entity_test->save();
193 // Set the new test entity to the node field.
194 $node_after->composite_reference = $second_entity_test;
195 // Check the fields have been updated.
196 static::assertEquals($node_after->composite_reference[0]->target_id, $second_entity_test->id());
197 static::assertEquals($node_after->composite_reference[0]->target_revision_id, $second_entity_test->getRevisionId());
201 * Tests entity_reference_revisions default value and config dependencies.
203 public function testEntityReferenceRevisionsDefaultValue() {
205 // Create a test target node used as entity reference by another test node.
206 $node_target = Node::create([
207 'title' => 'Target node',
209 'body' => 'Target body text',
210 'uuid' => '2d04c2b4-9c3d-4fa6-869e-ecb6fa5c9410',
212 $node_target->save();
214 // Create an entity reference field to reference to the test target node.
215 /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
216 $field_storage = FieldStorageConfig::create([
217 'field_name' => 'target_node_reference',
218 'entity_type' => 'node',
219 'type' => 'entity_reference_revisions',
220 'settings' => ['target_type' => 'node'],
222 $field_storage->save();
223 /** @var \Drupal\field\Entity\FieldConfig $field */
224 $field = FieldConfig::create([
225 'field_storage' => $field_storage,
226 'bundle' => 'article',
228 'settings' => ['handler_settings' => ['target_bundles' => ['article' => 'article']]],
230 // Add reference values to field config that will be used as default value.
233 'target_id' => $node_target->id(),
234 'target_revision_id' => $node_target->getRevisionId(),
235 'target_uuid' => $node_target->uuid(),
238 $field->setDefaultValue($default_value)->save();
240 // Resave the target node, so that the default revision is not the one we
242 $revision_id = $node_target->getRevisionId();
243 $node_target_after = Node::load($node_target->id());
244 $node_target_after->setNewRevision();
245 $node_target_after->save();
246 $this->assertTrue($node_target_after->getRevisionId() != $revision_id);
248 // Create another node.
249 $node_host = Node::create([
250 'title' => 'Host node',
252 'body' => 'Host body text',
253 'target_node_reference' => $node_target,
257 // Check if the ERR default values are properly created.
258 $node_host_after = Node::load($node_host->id());
259 $this->assertEquals($node_host_after->target_node_reference->target_id, $node_target->id());
260 $this->assertEquals($node_host_after->target_node_reference->target_revision_id, $revision_id);
262 // Check if the configuration dependencies are properly created.
263 $dependencies = $field->calculateDependencies()->getDependencies();
264 $this->assertEquals($dependencies['content'][0], 'node:article:2d04c2b4-9c3d-4fa6-869e-ecb6fa5c9410');
265 $this->assertEquals($dependencies['config'][0], 'field.storage.node.target_node_reference');
266 $this->assertEquals($dependencies['config'][1], 'node.type.article');
267 $this->assertEquals($dependencies['module'][0], 'entity_reference_revisions');
271 * Tests FieldType\EntityReferenceRevisionsItem::deleteRevision
273 public function testEntityReferenceRevisionsDeleteHandleDeletedChild() {
274 $field_storage = FieldStorageConfig::create([
275 'field_name' => 'field_reference',
276 'entity_type' => 'node',
277 'type' => 'entity_reference_revisions',
279 'target_type' => 'node',
282 $field_storage->save();
283 $field = FieldConfig::create([
284 'field_storage' => $field_storage,
285 'bundle' => 'article',
289 $child = Node::create([
291 'title' => 'Child node',
295 $node = Node::create([
297 'title' => 'Parent node',
298 'field_reference' => [
300 'target_id' => $child->id(),
301 'target_revision_id' => $child->getRevisionId(),
306 // Create two revisions.
308 $revisionId = $node->getRevisionId();
309 $node->setNewRevision(TRUE);
312 // Force delete the child Paragraph.
313 // Core APIs allow this although it is an inconsistent storage situation
317 // Previously deleting a revision with a lost child failed fatal.
318 \Drupal::entityTypeManager()->getStorage('node')->deleteRevision($revisionId);