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