3 namespace Drupal\paragraphs\Tests\Experimental;
6 * Tests paragraphs duplicate feature.
10 class ParagraphsExperimentalDuplicateFeatureTest extends ParagraphsExperimentalTestBase {
12 public static $modules = [
22 * Tests duplicate paragraph feature.
24 public function testDuplicateButton() {
25 $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
27 $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
28 // Add a Paragraph type.
29 $paragraph_type = 'text_paragraph';
30 $this->addParagraphsType($paragraph_type);
31 $this->addParagraphsType('text');
33 // Add a text field to the text_paragraph type.
34 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
35 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_text_paragraph_add_more');
37 // Create a node with a Paragraph.
38 $text = 'recognizable_text';
40 'title[0][value]' => 'paragraphs_mode_test',
41 'field_paragraphs[0][subform][field_text][0][value]' => $text,
43 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
44 $node = $this->drupalGetNodeByTitle('paragraphs_mode_test');
46 // Change edit mode to "closed".
47 $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
48 $this->drupalGet('node/' . $node->id() . '/edit');
50 // Click "Duplicate" button.
51 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_duplicate');
52 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
53 $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', $text);
54 $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', $text);
56 // Save and check if both paragraphs are present.
57 $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
58 $this->assertNoUniqueText($text);
62 * Tests duplicate paragraph feature with nested paragraphs.
64 public function testDuplicateButtonWithNesting() {
65 $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
67 $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
68 // Add nested Paragraph type.
69 $nested_paragraph_type = 'nested_paragraph';
70 $this->addParagraphsType($nested_paragraph_type);
71 // Add text Paragraph type.
72 $paragraph_type = 'text';
73 $this->addParagraphsType($paragraph_type);
75 // Add a text field to the text_paragraph type.
76 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
78 // Add a ERR paragraph field to the nested_paragraph type.
79 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $nested_paragraph_type, 'nested', 'Nested', 'field_ui:entity_reference_revisions:paragraph', [
80 'settings[target_type]' => 'paragraph',
81 'cardinality' => '-1',
83 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_nested_paragraph_add_more');
85 // Create a node with a Paragraph.
87 'title[0][value]' => 'paragraphs_mode_test',
89 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
90 $node = $this->drupalGetNodeByTitle('paragraphs_mode_test');
92 // Add a text field to nested paragraph.
93 $text = 'recognizable_text';
94 $this->drupalPostAjaxForm('node/' . $node->id() . '/edit', [], 'field_paragraphs_0_subform_field_nested_text_add_more');
96 'field_paragraphs[0][subform][field_nested][0][subform][field_text][0][value]' => $text,
98 $this->drupalPostForm(NULL, $edit, 'Save and keep published');
100 // Switch mode to closed.
101 $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
102 $this->drupalGet('node/' . $node->id() . '/edit');
104 // Click "Duplicate" button.
105 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_duplicate');
106 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
107 $this->assertFieldByName('field_paragraphs[0][subform][field_nested][0][subform][field_text][0][value]', $text);
108 $this->assertFieldByName('field_paragraphs[1][subform][field_nested][0][subform][field_text][0][value]', $text);
110 // Change the text paragraph value of duplicated nested paragraph.
111 $second_paragraph_text = 'duplicated_text';
113 'field_paragraphs[1][subform][field_nested][0][subform][field_text][0][value]' => $second_paragraph_text,
116 // Save and check if the changed text paragraph value of the duplicated
117 // paragraph is not the same as in the original paragraph.
118 $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
119 $this->assertUniqueText($text);
120 $this->assertUniqueText($second_paragraph_text);