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',
42 protected function setUp() {
44 // Create paragraphs and article content types.
45 $values = ['type' => 'article', 'name' => 'Article'];
46 $node_type = NodeType::create($values);
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');
57 * Tests the revision of paragraphs.
59 public function testParagraphsRevisions() {
60 // Create the paragraph type.
61 $paragraph_type = ParagraphsType::create(array(
62 'label' => 'test_text',
65 $paragraph_type->save();
67 $paragraph_type_nested = ParagraphsType::create(array(
68 'label' => 'test_nested',
69 'id' => 'test_nested',
71 $paragraph_type_nested->save();
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',
80 'target_type' => 'paragraph'
83 $field_storage->save();
84 $field = FieldConfig::create(array(
85 'field_storage' => $field_storage,
86 'bundle' => 'test_nested',
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',
97 'target_type' => 'paragraph'
100 $field_storage->save();
101 $field = FieldConfig::create(array(
102 'field_storage' => $field_storage,
103 'bundle' => 'article',
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',
113 'target_type' => 'paragraph'
116 $field_storage->save();
117 $field = FieldConfig::create(array(
118 'field_storage' => $field_storage,
123 // Create a paragraph.
124 $paragraph1 = Paragraph::create([
125 'title' => 'Paragraph',
126 'type' => 'test_text',
129 // Create another paragraph.
130 $paragraph2 = Paragraph::create([
131 'title' => 'Paragraph',
132 'type' => 'test_text',
135 // Create another paragraph.
136 $paragraph3 = Paragraph::create([
137 'title' => 'Paragraph',
138 'type' => 'test_text',
141 // Create another paragraph.
142 $paragraph_nested_children1 = Paragraph::create([
143 'title' => 'Paragraph',
144 'type' => 'test_text',
146 $paragraph_nested_children1->save();
147 // Create another paragraph.
148 $paragraph_nested_children2 = Paragraph::create([
149 'title' => 'Paragraph',
150 'type' => 'test_text',
152 $paragraph_nested_children2->save();
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],
160 $paragraph4_nested_parent->save();
162 // Create another paragraph.
163 $paragraph_user_1 = Paragraph::create([
164 'title' => 'Paragraph',
165 'type' => 'test_text',
167 $paragraph_user_1->save();
169 // Create a node with two paragraphs.
170 $node = Node::create([
171 'title' => $this->randomMachineName(),
173 'node_paragraph_field' => array($paragraph1, $paragraph2, $paragraph3, $paragraph4_nested_parent),
177 // Create an user with a paragraph.
178 $user = User::create([
180 'user_paragraph_field' => $paragraph_user_1,
183 $settings = Settings::getAll();
184 $settings['paragraph_limit'] = 1;
185 new Settings($settings);
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);
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);
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);
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);
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);
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();
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();
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();
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.
269 paragraphs_post_update_set_paragraphs_parent_fields($sandbox);
270 } while ($sandbox['#finished'] < 1);
272 $node_paragraph1 = Paragraph::load($paragraph1->id())->toArray();
273 $this->assertParagraphField($node_paragraph1, $node->id(), $node->getEntityTypeId(), 'node_paragraph_field');
275 $paragraph1_revision1 = \Drupal::entityTypeManager()->getStorage('paragraph')->loadRevision($paragraph1_revision1->getRevisionId())->toArray();
276 $this->assertParagraphField($paragraph1_revision1, $node->id(), $node->getEntityTypeId(), 'node_paragraph_field');
278 $paragraph1_revision2 = \Drupal::entityTypeManager()->getStorage('paragraph')->loadRevision($paragraph1_revision2->getRevisionId())->toArray();
279 $this->assertParagraphField($paragraph1_revision2, $node->id(), $node->getEntityTypeId(), 'node_paragraph_field');
281 $node_paragraph2 = Paragraph::load($paragraph2->id())->toArray();
282 $this->assertParagraphField($node_paragraph2, $node->id(), $node->getEntityTypeId(), 'node_paragraph_field');
284 $user_paragraph = Paragraph::load($paragraph_user_1->id())->toArray();
285 $this->assertParagraphField($user_paragraph, $user->id(), $user->getEntityTypeId(), 'user_paragraph_field');
287 $nested_paragraph_parent = Paragraph::load($paragraph4_nested_parent->id())->toArray();
288 $this->assertParagraphField($nested_paragraph_parent, $node->id(), $node->getEntityTypeId(), 'node_paragraph_field');
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');
293 // Add the german language.
294 ConfigurableLanguage::create(['id' => 'de'])->save();
296 // Create a new paragraph and add a german translation.
297 $paragraph = Paragraph::create([
298 'title' => 'Paragraph',
299 'type' => 'test_text'
301 $paragraph->addTranslation('de');
304 // Load a node and add a german translation.
305 $node = Node::load($node->id());
306 $node->addTranslation('de', [
308 'node_paragraph_field' => $paragraph
312 // Load the paragraph and its german translation.
313 $paragraph = Paragraph::load($paragraph->id());
314 $paragraph = $paragraph->getTranslation('de');
316 // Get the parent entity.
317 $parent = $paragraph->getParentEntity();
318 static::assertEquals($parent->language()->getId(), 'de');
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',
325 $paragraph_needs_save->setNeedsSave(TRUE);
326 $paragraph_needs_save->save();
327 $this->assertFalse($paragraph_needs_save->needsSave());
331 * Checks if $paragraph fields match with host / parent.
334 * The paragraph entity to check.
336 * The parent entity id.
337 * @param $entity_type
338 * The parent entity type.
340 * The parent entity field name.
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.');