Upgraded imagemagick and manually altered pdf to image module to handle changes....
[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.
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);
97     $node->save();
98
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);
103
104     $new_text = 'Dummy text again';
105     // Set the name again.
106     $entity_test->name = $new_text;
107     $entity_test->setNeedsSave(FALSE);
108
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;
114     $node->save();
115
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);
119
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());
124     $node->save();
125
126     // Test if the needs save variable is set as false after saving.
127     $entity_needs_save = EntityTestCompositeRelationship::create([
128       'uuid' => $text,
129       'name' => $text,
130     ]);
131     $entity_needs_save->setNeedsSave(TRUE);
132     $entity_needs_save->save();
133     $this->assertFalse($entity_needs_save->needsSave());
134   }
135
136   /**
137    * Test for NeedsSaveInterface implementation.
138    *
139    * Tests that the fields in the parent are properly updated.
140    */
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',
147       'settings' => array(
148         'target_type' => 'entity_test_composite'
149       ),
150     ));
151     $field_storage->save();
152     $field = FieldConfig::create(array(
153       'field_storage' => $field_storage,
154       'bundle' => 'article',
155     ));
156     $field->save();
157
158     $text = 'Dummy text';
159     // Create the test entity.
160     $entity_test = EntityTestCompositeRelationship::create(array(
161       'uuid' => $text,
162       'name' => $text,
163     ));
164
165     // Create a node with a reference to the test entity and save.
166     $node = Node::create([
167       'title' => $this->randomMachineName(),
168       'type' => 'article',
169       'composite_reference' => $entity_test,
170     ]);
171     $validate = $node->validate();
172     $this->assertEmpty($validate);
173     $node->save();
174
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());
181
182     // Create a new test entity.
183     $text = 'Smart text';
184     $second_entity_test = EntityTestCompositeRelationship::create(array(
185       'uuid' => $text,
186       'name' => $text,
187     ));
188     $second_entity_test->save();
189
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());
195   }
196
197   /**
198    * Tests entity_reference_revisions default value and config dependencies.
199    */
200   public function testEntityReferenceRevisionsDefaultValue() {
201
202     // Create a test target node used as entity reference by another test node.
203     $node_target = Node::create([
204       'title' => 'Target node',
205       'type' => 'article',
206       'body' => 'Target body text',
207       'uuid' => '2d04c2b4-9c3d-4fa6-869e-ecb6fa5c9410',
208     ]);
209     $node_target->save();
210
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'],
218     ]);
219     $field_storage->save();
220     /** @var \Drupal\field\Entity\FieldConfig $field */
221     $field = FieldConfig::create([
222       'field_storage' => $field_storage,
223       'bundle' => 'article',
224       'required' => FALSE,
225       'settings' => ['handler_settings' => ['target_bundles' => ['article' => 'article']]],
226     ]);
227     // Add reference values to field config that will be used as default value.
228     $default_value = [
229       [
230         'target_id' => $node_target->id(),
231         'target_revision_id' => $node_target->getRevisionId(),
232         'target_uuid' => $node_target->uuid(),
233       ],
234     ];
235     $field->setDefaultValue($default_value)->save();
236
237     // Resave the target node, so that the default revision is not the one we
238     // want to use.
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);
244
245     // Create another node.
246     $node_host = Node::create([
247       'title' => 'Host node',
248       'type' => 'article',
249       'body' => 'Host body text',
250       'target_node_reference' => $node_target,
251     ]);
252     $node_host->save();
253
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);
258
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');
265   }
266 }