05e589fdf203ccdd9a333adf40019c5a2aca7680
[yaffs-website] / web / modules / contrib / paragraphs / tests / src / Kernel / ParagraphsCompositeRelationshipTest.php
1 <?php
2
3 namespace Drupal\Tests\paragraphs\Kernel;
4
5 use Drupal\Core\Entity\Entity;
6 use Drupal\Core\Site\Settings;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\language\Entity\ConfigurableLanguage;
10 use Drupal\node\Entity\Node;
11 use Drupal\node\Entity\NodeType;
12 use Drupal\paragraphs\Entity\Paragraph;
13 use Drupal\paragraphs\Entity\ParagraphsType;
14 use Drupal\KernelTests\KernelTestBase;
15 use Drupal\user\Entity\User;
16
17 /**
18  * Tests the ERR composite relationship upgrade path.
19  *
20  * @group paragraphs
21  */
22 class ParagraphsCompositeRelationshipTest extends KernelTestBase {
23
24   /**
25    * Modules to enable.
26    *
27    * @var array
28    */
29   public static $modules = array(
30     'paragraphs',
31     'node',
32     'user',
33     'system',
34     'field',
35     'entity_reference_revisions',
36     'language',
37   );
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function setUp() {
43     parent::setUp();
44     // Create paragraphs and article content types.
45     $values = ['type' => 'article', 'name' => 'Article'];
46     $node_type = NodeType::create($values);
47     $node_type->save();
48     $this->installEntitySchema('user');
49     $this->installEntitySchema('node');
50     $this->installEntitySchema('paragraph');
51     $this->installSchema('system', ['sequences']);
52     $this->installSchema('node', ['node_access']);
53     \Drupal::moduleHandler()->loadInclude('paragraphs', 'install');
54   }
55
56   /**
57    * Tests the revision of paragraphs.
58    */
59   public function testParagraphsRevisions() {
60     // Create the paragraph type.
61     $paragraph_type = ParagraphsType::create(array(
62       'label' => 'test_text',
63       'id' => 'test_text',
64     ));
65     $paragraph_type->save();
66
67     $paragraph_type_nested = ParagraphsType::create(array(
68       'label' => 'test_nested',
69       'id' => 'test_nested',
70     ));
71     $paragraph_type_nested->save();
72
73     // Add a paragraph field to the article.
74     $field_storage = FieldStorageConfig::create(array(
75       'field_name' => 'nested_paragraph_field',
76       'entity_type' => 'paragraph',
77       'type' => 'entity_reference_revisions',
78       'cardinality' => '-1',
79       'settings' => array(
80         'target_type' => 'paragraph'
81       ),
82     ));
83     $field_storage->save();
84     $field = FieldConfig::create(array(
85       'field_storage' => $field_storage,
86       'bundle' => 'test_nested',
87     ));
88     $field->save();
89
90     // Add a paragraph field to the article.
91     $field_storage = FieldStorageConfig::create(array(
92       'field_name' => 'node_paragraph_field',
93       'entity_type' => 'node',
94       'type' => 'entity_reference_revisions',
95       'cardinality' => '-1',
96       'settings' => array(
97         'target_type' => 'paragraph'
98       ),
99     ));
100     $field_storage->save();
101     $field = FieldConfig::create(array(
102       'field_storage' => $field_storage,
103       'bundle' => 'article',
104     ));
105     $field->save();
106
107     // Add a paragraph field to the user.
108     $field_storage = FieldStorageConfig::create(array(
109       'field_name' => 'user_paragraph_field',
110       'entity_type' => 'user',
111       'type' => 'entity_reference_revisions',
112       'settings' => array(
113         'target_type' => 'paragraph'
114       ),
115     ));
116     $field_storage->save();
117     $field = FieldConfig::create(array(
118       'field_storage' => $field_storage,
119       'bundle' => 'user',
120     ));
121     $field->save();
122
123     // Create a paragraph.
124     $paragraph1 = Paragraph::create([
125       'title' => 'Paragraph',
126       'type' => 'test_text',
127     ]);
128     $paragraph1->save();
129     // Create another paragraph.
130     $paragraph2 = Paragraph::create([
131       'title' => 'Paragraph',
132       'type' => 'test_text',
133     ]);
134     $paragraph2->save();
135     // Create another paragraph.
136     $paragraph3 = Paragraph::create([
137       'title' => 'Paragraph',
138       'type' => 'test_text',
139     ]);
140     $paragraph3->save();
141     // Create another paragraph.
142     $paragraph_nested_children1 = Paragraph::create([
143       'title' => 'Paragraph',
144       'type' => 'test_text',
145     ]);
146     $paragraph_nested_children1->save();
147     // Create another paragraph.
148     $paragraph_nested_children2 = Paragraph::create([
149       'title' => 'Paragraph',
150       'type' => 'test_text',
151     ]);
152     $paragraph_nested_children2->save();
153
154     // Create another paragraph.
155     $paragraph4_nested_parent = Paragraph::create([
156       'title' => 'Paragraph',
157       'type' => 'test_nested',
158       'nested_paragraph_field' => [$paragraph_nested_children1, $paragraph_nested_children2],
159     ]);
160     $paragraph4_nested_parent->save();
161
162     // Create another paragraph.
163     $paragraph_user_1 = Paragraph::create([
164       'title' => 'Paragraph',
165       'type' => 'test_text',
166     ]);
167     $paragraph_user_1->save();
168
169     // Create a node with two paragraphs.
170     $node = Node::create([
171       'title' => $this->randomMachineName(),
172       'type' => 'article',
173       'node_paragraph_field' => array($paragraph1, $paragraph2, $paragraph3, $paragraph4_nested_parent),
174       ]);
175     $node->save();
176
177     // Create an user with a paragraph.
178     $user = User::create([
179       'name' => 'test',
180       'user_paragraph_field' => $paragraph_user_1,
181     ]);
182     $user->save();
183     $settings = Settings::getAll();
184     $settings['paragraph_limit'] = 1;
185     new Settings($settings);
186
187     // Unset the parent field name, type and id of paragraph1.
188     /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
189     $paragraph = Paragraph::load($paragraph1->id());
190     $paragraph->set('parent_field_name', NULL);
191     $paragraph->set('parent_type', NULL);
192     $paragraph->set('parent_id', NULL);
193     $paragraph->setNewRevision(FALSE);
194     $paragraph->save();
195
196     // Unset the parent field name, type and id of paragraph2.
197     $paragraph = Paragraph::load($paragraph2->id());
198     $paragraph->set('parent_field_name', NULL);
199     $paragraph->set('parent_type', NULL);
200     $paragraph->set('parent_id', NULL);
201     $paragraph->setNewRevision(FALSE);
202     $paragraph->save();
203
204     // Unset the parent field name, type and id of $paragraph_nested_parent.
205     $paragraph = Paragraph::load($paragraph4_nested_parent->id());
206     $paragraph->set('parent_field_name', NULL);
207     $paragraph->set('parent_type', NULL);
208     $paragraph->set('parent_id', NULL);
209     $paragraph->setNewRevision(FALSE);
210     $paragraph->save();
211
212     // Unset the parent field name, type and id of $paragraph_nested_children1.
213     $paragraph = Paragraph::load($paragraph_nested_children1->id());
214     $paragraph->set('parent_field_name', NULL);
215     $paragraph->set('parent_type', NULL);
216     $paragraph->set('parent_id', NULL);
217     $paragraph->setNewRevision(FALSE);
218     $paragraph->save();
219
220     // Unset the parent field name, type and id of paragraph_user_1.
221     /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
222     $paragraph = Paragraph::load($paragraph_user_1->id());
223     $paragraph->set('parent_field_name', NULL);
224     $paragraph->set('parent_type', NULL);
225     $paragraph->set('parent_id', NULL);
226     $paragraph->setNewRevision(FALSE);
227     $paragraph->save();
228
229     // Create a revision for node.
230     /** @var \Drupal\node\Entity\Node $node_revision1 */
231     $node_revision1 = Node::load($node->id());
232     /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph1_revision1 */
233     $paragraph1_revision1 = Paragraph::load($paragraph1->id());
234     $paragraph1_revision1->setNewRevision(TRUE);
235     $paragraph1_revision1->save();
236     /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph2_revision1 */
237     $paragraph2_revision1 = Paragraph::load($paragraph2->id());
238     $paragraph2_revision1->setNewRevision(TRUE);
239     $paragraph2_revision1->save();
240     $node_revision1->set('node_paragraph_field', [$paragraph1_revision1, $paragraph2_revision1]);
241     $node_revision1->setNewRevision(TRUE);
242     $node_revision1->save();
243
244     // Unset the parent field name, type and id of paragraph2_revision1.
245     $paragraph2_revision1 = Paragraph::load($paragraph2_revision1->id());
246     $paragraph2_revision1->set('parent_field_name', NULL);
247     $paragraph2_revision1->set('parent_type', NULL);
248     $paragraph2_revision1->set('parent_id', NULL);
249     $paragraph2_revision1->setNewRevision(FALSE);
250     $paragraph2_revision1->save();
251
252     // Create another revision for node.
253     /** @var \Drupal\node\Entity\Node $node_revision2 */
254     $node_revision2 = Node::load($node->id());
255     /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph1_revision2 */
256     $paragraph1_revision2 = Paragraph::load($paragraph1->id());
257     $paragraph1_revision2->setNewRevision(TRUE);
258     $paragraph1_revision2->save();
259     $node_revision2->set('node_paragraph_field', [$paragraph1_revision2]);
260     $node_revision2->setNewRevision(TRUE);
261     $node_revision2->save();
262
263     // Deletion of referenced paragraphs should not break updates.
264     $paragraph3->delete();
265     \Drupal::moduleHandler()->loadInclude('paragraphs', 'post_update.php');
266     // Run update function and check #finished.
267     $sandbox = [];
268     do {
269       paragraphs_post_update_set_paragraphs_parent_fields($sandbox);
270     } while ($sandbox['#finished'] < 1);
271
272     $node_paragraph1 = Paragraph::load($paragraph1->id())->toArray();
273     $this->assertParagraphField($node_paragraph1, $node->id(), $node->getEntityTypeId(), 'node_paragraph_field');
274
275     $paragraph1_revision1 = \Drupal::entityTypeManager()->getStorage('paragraph')->loadRevision($paragraph1_revision1->getRevisionId())->toArray();
276     $this->assertParagraphField($paragraph1_revision1, $node->id(), $node->getEntityTypeId(), 'node_paragraph_field');
277
278     $paragraph1_revision2 = \Drupal::entityTypeManager()->getStorage('paragraph')->loadRevision($paragraph1_revision2->getRevisionId())->toArray();
279     $this->assertParagraphField($paragraph1_revision2, $node->id(), $node->getEntityTypeId(), 'node_paragraph_field');
280
281     $node_paragraph2 = Paragraph::load($paragraph2->id())->toArray();
282     $this->assertParagraphField($node_paragraph2, $node->id(), $node->getEntityTypeId(), 'node_paragraph_field');
283
284     $user_paragraph = Paragraph::load($paragraph_user_1->id())->toArray();
285     $this->assertParagraphField($user_paragraph, $user->id(), $user->getEntityTypeId(), 'user_paragraph_field');
286
287     $nested_paragraph_parent = Paragraph::load($paragraph4_nested_parent->id())->toArray();
288     $this->assertParagraphField($nested_paragraph_parent, $node->id(), $node->getEntityTypeId(), 'node_paragraph_field');
289
290     $nested_paragraph_children = Paragraph::load($paragraph_nested_children1->id())->toArray();
291     $this->assertParagraphField($nested_paragraph_children, $paragraph4_nested_parent->id(), $paragraph4_nested_parent->getEntityTypeId(), 'nested_paragraph_field');
292
293     // Add the german language.
294     ConfigurableLanguage::create(['id' => 'de'])->save();
295
296     // Create a new paragraph and add a german translation.
297     $paragraph = Paragraph::create([
298       'title' => 'Paragraph',
299       'type' => 'test_text'
300     ]);
301     $paragraph->addTranslation('de');
302     $paragraph->save();
303
304     // Load a node and add a german translation.
305     $node = Node::load($node->id());
306     $node->addTranslation('de', [
307       'title' => 'german',
308       'node_paragraph_field' => $paragraph
309     ]);
310     $node->save();
311
312     // Load the paragraph and its german translation.
313     $paragraph = Paragraph::load($paragraph->id());
314     $paragraph = $paragraph->getTranslation('de');
315
316     // Get the parent entity.
317     $parent = $paragraph->getParentEntity();
318     static::assertEquals($parent->language()->getId(), 'de');
319
320     // Test if the needs save variable is set as false after saving.
321     $paragraph_needs_save = Paragraph::create([
322       'title' => 'Paragraph',
323       'type' => 'test_text',
324     ]);
325     $paragraph_needs_save->setNeedsSave(TRUE);
326     $paragraph_needs_save->save();
327     $this->assertFalse($paragraph_needs_save->needsSave());
328   }
329
330   /**
331    * Checks if $paragraph fields match with host / parent.
332    *
333    * @param $paragraph
334    *   The paragraph entity to check.
335    * @param $id
336    *   The parent entity id.
337    * @param $entity_type
338    *   The parent entity type.
339    * @param $field_name
340    *   The parent entity field name.
341    */
342   public function assertParagraphField($paragraph, $id, $entity_type, $field_name) {
343     self::assertEquals($paragraph['parent_id'][0]['value'], $id, 'Match parent id.');
344     self::assertEquals($paragraph['parent_type'][0]['value'], $entity_type, 'Matching parent type.');
345     self::assertEquals($paragraph['parent_field_name'][0]['value'], $field_name, 'Matching parent field name.');
346   }
347 }