3 namespace Drupal\Tests\paragraphs\Kernel;
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;
18 * Tests the ERR composite relationship upgrade path.
22 class ParagraphsCompositeRelationshipTest extends KernelTestBase {
29 public static $modules = array(
35 'entity_reference_revisions',
43 protected function setUp() {
45 // Create paragraphs and article content types.
46 $values = ['type' => 'article', 'name' => 'Article'];
47 $node_type = NodeType::create($values);
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');
58 * Tests the revision of paragraphs.
60 public function testParagraphsRevisions() {
61 // Create the paragraph type.
62 $paragraph_type = ParagraphsType::create(array(
63 'label' => 'test_text',
66 $paragraph_type->save();
68 $paragraph_type_nested = ParagraphsType::create(array(
69 'label' => 'test_nested',
70 'id' => 'test_nested',
72 $paragraph_type_nested->save();
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',
81 'target_type' => 'paragraph'
84 $field_storage->save();
85 $field = FieldConfig::create(array(
86 'field_storage' => $field_storage,
87 'bundle' => 'test_nested',
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',
98 'target_type' => 'paragraph'
101 $field_storage->save();
102 $field = FieldConfig::create(array(
103 'field_storage' => $field_storage,
104 'bundle' => 'article',
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',
114 'target_type' => 'paragraph'
117 $field_storage->save();
118 $field = FieldConfig::create(array(
119 'field_storage' => $field_storage,
124 // Create a paragraph.
125 $paragraph1 = Paragraph::create([
126 'title' => 'Paragraph',
127 'type' => 'test_text',
130 // Create another paragraph.
131 $paragraph2 = Paragraph::create([
132 'title' => 'Paragraph',
133 'type' => 'test_text',
136 // Create another paragraph.
137 $paragraph3 = Paragraph::create([
138 'title' => 'Paragraph',
139 'type' => 'test_text',
142 // Create another paragraph.
143 $paragraph_nested_children1 = Paragraph::create([
144 'title' => 'Paragraph',
145 'type' => 'test_text',
147 $paragraph_nested_children1->save();
148 // Create another paragraph.
149 $paragraph_nested_children2 = Paragraph::create([
150 'title' => 'Paragraph',
151 'type' => 'test_text',
153 $paragraph_nested_children2->save();
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],
161 $paragraph4_nested_parent->save();
163 // Create another paragraph.
164 $paragraph_user_1 = Paragraph::create([
165 'title' => 'Paragraph',
166 'type' => 'test_text',
168 $paragraph_user_1->save();
170 // Create a node with two paragraphs.
171 $node = Node::create([
172 'title' => $this->randomMachineName(),
174 'node_paragraph_field' => array($paragraph1, $paragraph2, $paragraph3, $paragraph4_nested_parent),
178 // Create an user with a paragraph.
179 $user = User::create([
181 'user_paragraph_field' => $paragraph_user_1,
184 $settings = Settings::getAll();
185 $settings['paragraph_limit'] = 1;
186 new Settings($settings);
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);
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);
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);
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);
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);
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();
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();
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();
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.
270 paragraphs_post_update_set_paragraphs_parent_fields($sandbox);
271 } while ($sandbox['#finished'] < 1);
273 $node_paragraph1 = Paragraph::load($paragraph1->id())->toArray();
274 $this->assertParagraphField($node_paragraph1, $node->id(), $node->getEntityTypeId(), 'node_paragraph_field');
276 $paragraph1_revision1 = \Drupal::entityTypeManager()->getStorage('paragraph')->loadRevision($paragraph1_revision1->getRevisionId())->toArray();
277 $this->assertParagraphField($paragraph1_revision1, $node->id(), $node->getEntityTypeId(), 'node_paragraph_field');
279 $paragraph1_revision2 = \Drupal::entityTypeManager()->getStorage('paragraph')->loadRevision($paragraph1_revision2->getRevisionId())->toArray();
280 $this->assertParagraphField($paragraph1_revision2, $node->id(), $node->getEntityTypeId(), 'node_paragraph_field');
282 $node_paragraph2 = Paragraph::load($paragraph2->id())->toArray();
283 $this->assertParagraphField($node_paragraph2, $node->id(), $node->getEntityTypeId(), 'node_paragraph_field');
285 $user_paragraph = Paragraph::load($paragraph_user_1->id())->toArray();
286 $this->assertParagraphField($user_paragraph, $user->id(), $user->getEntityTypeId(), 'user_paragraph_field');
288 $nested_paragraph_parent = Paragraph::load($paragraph4_nested_parent->id())->toArray();
289 $this->assertParagraphField($nested_paragraph_parent, $node->id(), $node->getEntityTypeId(), 'node_paragraph_field');
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');
294 // Add the german language.
295 ConfigurableLanguage::create(['id' => 'de'])->save();
297 // Create a new paragraph and add a german translation.
298 $paragraph = Paragraph::create([
299 'title' => 'Paragraph',
300 'type' => 'test_text'
302 $paragraph->addTranslation('de');
305 // Load a node and add a german translation.
306 $node = Node::load($node->id());
307 $node->addTranslation('de', [
309 'node_paragraph_field' => $paragraph
313 // Load the paragraph and its german translation.
314 $paragraph = Paragraph::load($paragraph->id());
315 $paragraph = $paragraph->getTranslation('de');
317 // Get the parent entity.
318 $parent = $paragraph->getParentEntity();
319 static::assertEquals($parent->language()->getId(), 'de');
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',
326 $paragraph_needs_save->setNeedsSave(TRUE);
327 $paragraph_needs_save->save();
328 $this->assertFalse($paragraph_needs_save->needsSave());
332 * Checks if $paragraph fields match with host / parent.
335 * The paragraph entity to check.
337 * The parent entity id.
338 * @param $entity_type
339 * The parent entity type.
341 * The parent entity field name.
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.');