3 namespace Drupal\paragraphs\Tests\Experimental;
5 use Drupal\field_ui\Tests\FieldUiTestTrait;
8 * Tests paragraphs experimental widget buttons.
12 class ParagraphsExperimentalWidgetButtonsTest extends ParagraphsExperimentalTestBase {
17 * Tests the widget buttons of paragraphs.
19 public function testWidgetButtons() {
20 $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
22 $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
23 // Add a Paragraph type.
24 $paragraph_type = 'text_paragraph';
25 $this->addParagraphsType($paragraph_type);
26 $this->addParagraphsType('text');
28 // Add a text field to the text_paragraph type.
29 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
31 'fields[field_paragraphs][type]' => 'paragraphs',
33 $this->drupalPostForm('admin/structure/types/manage/paragraphed_test/form-display', $edit, t('Save'));
34 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_text_paragraph_add_more');
36 // Create a node with a Paragraph.
37 $text = 'recognizable_text';
39 'title[0][value]' => 'paragraphs_mode_test',
40 'field_paragraphs[0][subform][field_text][0][value]' => $text,
42 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_paragraph_add_more');
43 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
44 $node = $this->drupalGetNodeByTitle('paragraphs_mode_test');
46 // Test the 'Open' mode.
47 $this->drupalGet('node/' . $node->id() . '/edit');
48 $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', $text);
49 $this->drupalPostForm(NULL, [], t('Save and keep published'));
50 $this->assertText($text);
52 // Test the 'Closed' mode.
53 $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
54 $this->drupalGet('node/' . $node->id() . '/edit');
55 // Click "Edit" button.
56 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
57 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_1_edit');
58 $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', $text);
59 $closed_mode_text = 'closed_mode_text';
60 // Click "Collapse" button on both paragraphs.
61 $edit = ['field_paragraphs[0][subform][field_text][0][value]' => $closed_mode_text];
62 $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_0_collapse');
63 $edit = ['field_paragraphs[1][subform][field_text][0][value]' => $closed_mode_text];
64 $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_1_collapse');
65 // Verify that we have warning message for each paragraph.
66 $this->assertNoUniqueText('You have unsaved changes on this Paragraph item.');
67 $this->assertRaw('<div class="paragraphs-collapsed-description">' . $closed_mode_text);
68 $this->drupalPostForm(NULL, [], t('Save and keep published'));
69 $this->assertText('paragraphed_test ' . $node->label() . ' has been updated.');
70 $this->assertText($closed_mode_text);
72 // Test the 'Preview' mode.
73 $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'preview');
74 $this->drupalGet('node/' . $node->id() . '/edit');
75 // Click "Edit" button.
76 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
77 $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', $closed_mode_text);
78 $preview_mode_text = 'preview_mode_text';
79 $edit = ['field_paragraphs[0][subform][field_text][0][value]' => $preview_mode_text];
80 // Click "Collapse" button.
81 $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_0_collapse');
82 $this->assertText('You have unsaved changes on this Paragraph item.');
83 $this->assertText($preview_mode_text);
84 $this->drupalPostForm(NULL, [], t('Save and keep published'));
85 $this->assertText('paragraphed_test ' . $node->label() . ' has been updated.');
86 $this->assertText($preview_mode_text);
88 // Test the remove function.
89 $this->drupalGet('node/' . $node->id() . '/edit');
90 // Click "Remove" button.
91 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_remove');
92 $this->drupalPostForm(NULL, [], t('Save and keep published'));
93 $this->assertText('paragraphed_test ' . $node->label() . ' has been updated.');
94 $this->assertNoText($preview_mode_text);
98 * Tests if buttons are present for each widget mode.
100 public function testButtonsVisibility() {
101 $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
103 $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
104 // Add a Paragraph type.
105 $paragraph_type = 'text_paragraph';
106 $this->addParagraphsType($paragraph_type);
107 $this->addParagraphsType('text');
109 // Add a text field to the text_paragraph type.
110 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
112 'fields[field_paragraphs][type]' => 'paragraphs',
114 $this->drupalPostForm('admin/structure/types/manage/paragraphed_test/form-display', $edit, t('Save'));
115 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_text_paragraph_add_more');
117 // Create a node with a Paragraph.
118 $text = 'recognizable_text';
120 'title[0][value]' => 'paragraphs_mode_test',
121 'field_paragraphs[0][subform][field_text][0][value]' => $text,
123 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_paragraph_add_more');
124 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
125 $node = $this->drupalGetNodeByTitle('paragraphs_mode_test');
127 // Checking visible buttons on "Open" mode.
128 $this->drupalGet('node/' . $node->id() . '/edit');
129 $this->assertText('Collapse');
130 $this->assertText('Remove');
131 $this->assertText('Duplicate');
133 // Checking visible buttons on "Closed" mode.
134 $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
135 $this->drupalGet('node/' . $node->id() . '/edit');
136 $this->assertText('Edit');
137 $this->assertText('Remove');
138 $this->assertText('Duplicate');
140 // Checking visible buttons on "Preview" mode.
141 $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
142 $this->drupalGet('node/' . $node->id() . '/edit');
143 $this->assertText('Edit');
144 $this->assertText('Remove');
145 $this->assertText('Duplicate');
147 // Checking always show collapse and edit actions.
148 $this->addParagraphsType('nested_paragraph');
149 static::fieldUIAddNewField('admin/structure/paragraphs_type/nested_paragraph', 'nested', 'Nested', 'field_ui:entity_reference_revisions:paragraph', [
150 'settings[target_type]' => 'paragraph',
151 'cardinality' => '-1',
153 $this->drupalGet('admin/structure/paragraphs_type/nested_paragraph/form-display');
155 'fields[field_nested][type]' => 'paragraphs',
157 $this->drupalPostForm(NULL, $edit, t('Save'));
158 $this->drupalGet('node/' . $node->id() . '/edit');
159 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_nested_paragraph_add_more');
160 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_2_subform_field_nested_nested_paragraph_add_more');
161 // Collapse is present on each nesting level.
162 $this->assertFieldByName('field_paragraphs_2_collapse');
163 $this->assertFieldByName('field_paragraphs_2_subform_field_nested_0_collapse');