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');
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);
32 // Add a text field to the text_paragraph type.
33 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
34 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_text_paragraph_add_more');
35 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_paragraph_add_more');
37 // Create a node with a Paragraph.
38 $text_01 = 'recognizable_text_01';
39 $text_02 = 'recognizable_text_02';
41 'title[0][value]' => 'paragraphs_mode_test',
42 'field_paragraphs[0][subform][field_text][0][value]' => 'A',
43 'field_paragraphs[1][subform][field_text][0][value]' => 'B',
44 'field_paragraphs[2][subform][field_text][0][value]' => 'C',
46 $this->drupalPostForm(NULL, $edit, t('Save'));
47 $node = $this->drupalGetNodeByTitle('paragraphs_mode_test');
49 $this->drupalGet('node/' . $node->id() . '/edit');
51 // Click "Duplicate" button on A and move C to the first position.
52 $edit = ['field_paragraphs[2][_weight]' => -1];
53 $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_0_duplicate');
54 $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'A');
55 $this->assertFieldByName('field_paragraphs[0][_weight]', 1);
56 $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', 'B');
57 $this->assertFieldByName('field_paragraphs[1][_weight]', 3);
58 $this->assertFieldByName('field_paragraphs[2][subform][field_text][0][value]', 'C');
59 $this->assertFieldByName('field_paragraphs[2][_weight]', 0);
60 $this->assertFieldByName('field_paragraphs[3][subform][field_text][0][value]', 'A');
61 $this->assertFieldByName('field_paragraphs[3][_weight]', 2);
63 // Move C after the A's and save.
65 'field_paragraphs[0][_weight]' => -2,
66 'field_paragraphs[1][_weight]' => 2,
67 'field_paragraphs[2][_weight]' => 1,
68 'field_paragraphs[3][_weight]' => -1,
71 // Save and check if all paragraphs are present in the correct order.
72 $this->drupalPostForm(NULL, $edit, t('Save'));
73 $this->drupalGet('node/' . $node->id() . '/edit');
74 $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'A');
75 $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', 'A');
76 $this->assertFieldByName('field_paragraphs[2][subform][field_text][0][value]', 'C');
77 $this->assertFieldByName('field_paragraphs[3][subform][field_text][0][value]', 'B');
79 // Delete the second A, then duplicate C.
80 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_1_remove');
81 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_2_duplicate');
82 $this->drupalPostForm(NULL, [], t('Save'));
84 $this->drupalGet('node/' . $node->id() . '/edit');
85 $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'A');
86 $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', 'C');
87 $this->assertFieldByName('field_paragraphs[2][subform][field_text][0][value]', 'C');
88 $this->assertFieldByName('field_paragraphs[3][subform][field_text][0][value]', 'B');
92 * Tests duplicate paragraph feature with nested paragraphs.
94 public function testDuplicateButtonWithNesting() {
95 $this->addParagraphedContentType('paragraphed_test');
97 $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
98 // Add nested Paragraph type.
99 $nested_paragraph_type = 'nested_paragraph';
100 $this->addParagraphsType($nested_paragraph_type);
101 // Add text Paragraph type.
102 $paragraph_type = 'text';
103 $this->addParagraphsType($paragraph_type);
105 // Add a text field to the text_paragraph type.
106 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
108 // Add a ERR paragraph field to the nested_paragraph type.
109 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $nested_paragraph_type, 'nested', 'Nested', 'field_ui:entity_reference_revisions:paragraph', [
110 'settings[target_type]' => 'paragraph',
111 'cardinality' => '-1',
113 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_nested_paragraph_add_more');
115 // Create a node with a Paragraph.
117 'title[0][value]' => 'paragraphs_mode_test',
119 $this->drupalPostForm(NULL, $edit, t('Save'));
120 $node = $this->drupalGetNodeByTitle('paragraphs_mode_test');
122 // Add a text field to nested paragraph.
123 $text = 'recognizable_text';
124 $this->drupalPostAjaxForm('node/' . $node->id() . '/edit', [], 'field_paragraphs_0_subform_field_nested_text_add_more');
126 'field_paragraphs[0][subform][field_nested][0][subform][field_text][0][value]' => $text,
128 $this->drupalPostForm(NULL, $edit, t('Save'));
130 // Switch mode to closed.
131 $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
132 $this->drupalGet('node/' . $node->id() . '/edit');
134 // Click "Duplicate" button.
135 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_duplicate');
136 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
137 $this->assertFieldByName('field_paragraphs[0][subform][field_nested][0][subform][field_text][0][value]', $text);
138 $this->assertFieldByName('field_paragraphs[1][subform][field_nested][0][subform][field_text][0][value]', $text);
140 // Change the text paragraph value of duplicated nested paragraph.
141 $second_paragraph_text = 'duplicated_text';
143 'field_paragraphs[1][subform][field_nested][0][subform][field_text][0][value]' => $second_paragraph_text,
146 // Save and check if the changed text paragraph value of the duplicated
147 // paragraph is not the same as in the original paragraph.
148 $this->drupalPostForm(NULL, $edit, t('Save'));
149 $this->assertUniqueText($text);
150 $this->assertUniqueText($second_paragraph_text);