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.
92 $values = $node->composite_reference->getValue();
93 $this->assertTrue(isset($values[0]['entity']));
94 static::assertEquals($values[0]['entity']->name->value, $text);
95 $node->composite_reference->setValue($values);
96 static::assertEquals($node->composite_reference->entity->name->value, $text);
99 // Check that the name has been updated when the parent has been saved.
100 /** @var \Drupal\entity_composite_relationship_test\Entity\EntityTestCompositeRelationship $entity_test_after */
101 $entity_test_after = EntityTestCompositeRelationship::load($entity_test->id());
102 static::assertEquals($entity_test_after->name->value, $text);
104 $new_text = 'Dummy text again';
105 // Set the name again.
106 $entity_test->name = $new_text;
107 $entity_test->setNeedsSave(FALSE);
109 // Load the Node and check the composite reference field is not set.
110 $node = Node::load($node->id());
111 $values = $node->composite_reference->getValue();
112 $this->assertFalse(isset($values[0]['entity']));
113 $node->composite_reference = $entity_test;
116 // Check the name is not updated.
117 $entity_test_after = EntityTestCompositeRelationship::load($entity_test->id());
118 static::assertEquals($entity_test_after->name->value, $text);
120 // Test if after delete the referenced entity there are no problems setting
121 // the referencing values to the parent.
122 $entity_test->delete();
123 $node = Node::load($node->id());
126 // Test if the needs save variable is set as false after saving.
127 $entity_needs_save = EntityTestCompositeRelationship::create([
131 $entity_needs_save->setNeedsSave(TRUE);
132 $entity_needs_save->save();
133 $this->assertFalse($entity_needs_save->needsSave());
137 * Test for NeedsSaveInterface implementation.
139 * Tests that the fields in the parent are properly updated.
141 public function testSaveNewEntity() {
142 // Add the entity_reference_revisions field to article.
143 $field_storage = FieldStorageConfig::create(array(
144 'field_name' => 'composite_reference',
145 'entity_type' => 'node',
146 'type' => 'entity_reference_revisions',
148 'target_type' => 'entity_test_composite'
151 $field_storage->save();
152 $field = FieldConfig::create(array(
153 'field_storage' => $field_storage,
154 'bundle' => 'article',
158 $text = 'Dummy text';
159 // Create the test entity.
160 $entity_test = EntityTestCompositeRelationship::create(array(
165 // Create a node with a reference to the test entity and save.
166 $node = Node::create([
167 'title' => $this->randomMachineName(),
169 'composite_reference' => $entity_test,
171 $validate = $node->validate();
172 $this->assertEmpty($validate);
175 // Test that the fields on node are properly set.
176 $node_after = Node::load($node->id());
177 static::assertEquals($node_after->composite_reference[0]->target_id, $entity_test->id());
178 static::assertEquals($node_after->composite_reference[0]->target_revision_id, $entity_test->getRevisionId());
179 // Check that the entity is not new after save parent.
180 $this->assertFalse($entity_test->isNew());
182 // Create a new test entity.
183 $text = 'Smart text';
184 $second_entity_test = EntityTestCompositeRelationship::create(array(
188 $second_entity_test->save();
190 // Set the new test entity to the node field.
191 $node_after->composite_reference = $second_entity_test;
192 // Check the fields have been updated.
193 static::assertEquals($node_after->composite_reference[0]->target_id, $second_entity_test->id());
194 static::assertEquals($node_after->composite_reference[0]->target_revision_id, $second_entity_test->getRevisionId());
198 * Tests entity_reference_revisions default value and config dependencies.
200 public function testEntityReferenceRevisionsDefaultValue() {
202 // Create a test target node used as entity reference by another test node.
203 $node_target = Node::create([
204 'title' => 'Target node',
206 'body' => 'Target body text',
207 'uuid' => '2d04c2b4-9c3d-4fa6-869e-ecb6fa5c9410',
209 $node_target->save();
211 // Create an entity reference field to reference to the test target node.
212 /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
213 $field_storage = FieldStorageConfig::create([
214 'field_name' => 'target_node_reference',
215 'entity_type' => 'node',
216 'type' => 'entity_reference_revisions',
217 'settings' => ['target_type' => 'node'],
219 $field_storage->save();
220 /** @var \Drupal\field\Entity\FieldConfig $field */
221 $field = FieldConfig::create([
222 'field_storage' => $field_storage,
223 'bundle' => 'article',
225 'settings' => ['handler_settings' => ['target_bundles' => ['article' => 'article']]],
227 // Add reference values to field config that will be used as default value.
230 'target_id' => $node_target->id(),
231 'target_revision_id' => $node_target->getRevisionId(),
232 'target_uuid' => $node_target->uuid(),
235 $field->setDefaultValue($default_value)->save();
237 // Resave the target node, so that the default revision is not the one we
239 $revision_id = $node_target->getRevisionId();
240 $node_target_after = Node::load($node_target->id());
241 $node_target_after->setNewRevision();
242 $node_target_after->save();
243 $this->assertTrue($node_target_after->getRevisionId() != $revision_id);
245 // Create another node.
246 $node_host = Node::create([
247 'title' => 'Host node',
249 'body' => 'Host body text',
250 'target_node_reference' => $node_target,
254 // Check if the ERR default values are properly created.
255 $node_host_after = Node::load($node_host->id());
256 $this->assertEquals($node_host_after->target_node_reference->target_id, $node_target->id());
257 $this->assertEquals($node_host_after->target_node_reference->target_revision_id, $revision_id);
259 // Check if the configuration dependencies are properly created.
260 $dependencies = $field->calculateDependencies()->getDependencies();
261 $this->assertEquals($dependencies['content'][0], 'node:article:2d04c2b4-9c3d-4fa6-869e-ecb6fa5c9410');
262 $this->assertEquals($dependencies['config'][0], 'field.storage.node.target_node_reference');
263 $this->assertEquals($dependencies['config'][1], 'node.type.article');
264 $this->assertEquals($dependencies['module'][0], 'entity_reference_revisions');