Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity_reference_revisions / tests / src / Kernel / EntityReferenceRevisionsSaveTest.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\KernelTestBase;
9 use Drupal\node\Entity\Node;
10 use Drupal\node\Entity\NodeType;
11
12 /**
13  * Tests the entity_reference_revisions NeedsSaveInterface.
14  *
15  * @group entity_reference_revisions
16  */
17 class EntityReferenceRevisionsSaveTest extends KernelTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = array(
25     'node',
26     'user',
27     'system',
28     'field',
29     'entity_reference_revisions',
30     'entity_composite_relationship_test',
31   );
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function setUp() {
37     parent::setUp();
38     // Create article content type.
39     $values = ['type' => 'article', 'name' => 'Article'];
40     $node_type = NodeType::create($values);
41     $node_type->save();
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']);
47   }
48
49   /**
50    * Test for NeedsSaveInterface implementation.
51    *
52    * Tests that the referenced entity is saved when needsSave() is TRUE.
53    */
54   public function testNeedsSave() {
55
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',
61       'settings' => array(
62         'target_type' => 'entity_test_composite'
63       ),
64     ));
65     $field_storage->save();
66     $field = FieldConfig::create(array(
67       'field_storage' => $field_storage,
68       'bundle' => 'article',
69     ));
70     $field->save();
71
72     $text = 'Dummy text';
73     // Create the test composite entity.
74     $entity_test = EntityTestCompositeRelationship::create(array(
75       'uuid' => $text,
76       'name' => $text,
77     ));
78     $entity_test->save();
79
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(),
88       'type' => 'article',
89       'composite_reference' => $entity_test,
90     ]);
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);
98     $node->save();
99
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);
104
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);
109
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;
116     $node->save();
117
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);
122
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());
127     $node->save();
128
129     // Test if the needs save variable is set as false after saving.
130     $entity_needs_save = EntityTestCompositeRelationship::create([
131       'uuid' => $text,
132       'name' => $text,
133     ]);
134     $entity_needs_save->setNeedsSave(TRUE);
135     $entity_needs_save->save();
136     $this->assertFalse($entity_needs_save->needsSave());
137   }
138
139   /**
140    * Test for NeedsSaveInterface implementation.
141    *
142    * Tests that the fields in the parent are properly updated.
143    */
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',
150       'settings' => array(
151         'target_type' => 'entity_test_composite'
152       ),
153     ));
154     $field_storage->save();
155     $field = FieldConfig::create(array(
156       'field_storage' => $field_storage,
157       'bundle' => 'article',
158     ));
159     $field->save();
160
161     $text = 'Dummy text';
162     // Create the test entity.
163     $entity_test = EntityTestCompositeRelationship::create(array(
164       'uuid' => $text,
165       'name' => $text,
166     ));
167
168     // Create a node with a reference to the test entity and save.
169     $node = Node::create([
170       'title' => $this->randomMachineName(),
171       'type' => 'article',
172       'composite_reference' => $entity_test,
173     ]);
174     $validate = $node->validate();
175     $this->assertEmpty($validate);
176     $node->save();
177
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());
184
185     // Create a new test entity.
186     $text = 'Smart text';
187     $second_entity_test = EntityTestCompositeRelationship::create(array(
188       'uuid' => $text,
189       'name' => $text,
190     ));
191     $second_entity_test->save();
192
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());
198   }
199
200   /**
201    * Tests entity_reference_revisions default value and config dependencies.
202    */
203   public function testEntityReferenceRevisionsDefaultValue() {
204
205     // Create a test target node used as entity reference by another test node.
206     $node_target = Node::create([
207       'title' => 'Target node',
208       'type' => 'article',
209       'body' => 'Target body text',
210       'uuid' => '2d04c2b4-9c3d-4fa6-869e-ecb6fa5c9410',
211     ]);
212     $node_target->save();
213
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'],
221     ]);
222     $field_storage->save();
223     /** @var \Drupal\field\Entity\FieldConfig $field */
224     $field = FieldConfig::create([
225       'field_storage' => $field_storage,
226       'bundle' => 'article',
227       'required' => FALSE,
228       'settings' => ['handler_settings' => ['target_bundles' => ['article' => 'article']]],
229     ]);
230     // Add reference values to field config that will be used as default value.
231     $default_value = [
232       [
233         'target_id' => $node_target->id(),
234         'target_revision_id' => $node_target->getRevisionId(),
235         'target_uuid' => $node_target->uuid(),
236       ],
237     ];
238     $field->setDefaultValue($default_value)->save();
239
240     // Resave the target node, so that the default revision is not the one we
241     // want to use.
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);
247
248     // Create another node.
249     $node_host = Node::create([
250       'title' => 'Host node',
251       'type' => 'article',
252       'body' => 'Host body text',
253       'target_node_reference' => $node_target,
254     ]);
255     $node_host->save();
256
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);
261
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');
268   }
269
270   /**
271    * Tests FieldType\EntityReferenceRevisionsItem::deleteRevision
272    */
273   public function testEntityReferenceRevisionsDeleteHandleDeletedChild() {
274     $field_storage = FieldStorageConfig::create([
275       'field_name' => 'field_reference',
276       'entity_type' => 'node',
277       'type' => 'entity_reference_revisions',
278       'settings' => [
279         'target_type' => 'node',
280       ],
281     ]);
282     $field_storage->save();
283     $field = FieldConfig::create([
284       'field_storage' => $field_storage,
285       'bundle' => 'article',
286     ]);
287     $field->save();
288
289     $child = Node::create([
290       'type' => 'article',
291       'title' => 'Child node',
292     ]);
293     $child->save();
294
295     $node = Node::create([
296       'type' => 'article',
297       'title' => 'Parent node',
298       'field_reference' => [
299         [
300           'target_id' => $child->id(),
301           'target_revision_id' => $child->getRevisionId(),
302         ]
303       ],
304     ]);
305
306     // Create two revisions.
307     $node->save();
308     $revisionId = $node->getRevisionId();
309     $node->setNewRevision(TRUE);
310     $node->save();
311
312     // Force delete the child Paragraph.
313     // Core APIs allow this although it is an inconsistent storage situation
314     // for Paragraphs.
315     $child->delete();
316
317     // Previously deleting a revision with a lost child failed fatal.
318     \Drupal::entityTypeManager()->getStorage('node')->deleteRevision($revisionId);
319   }
320
321 }