3 namespace Drupal\paragraphs\Tests\Experimental;
5 use Drupal\field_ui\Tests\FieldUiTestTrait;
6 use Drupal\Tests\paragraphs\FunctionalJavascript\ParagraphsTestBaseTrait;
9 * Tests paragraphs experimental widget buttons.
13 class ParagraphsExperimentalWidgetButtonsTest extends ParagraphsExperimentalTestBase {
16 use ParagraphsTestBaseTrait;
23 public static $modules = [
28 * Tests the widget buttons of paragraphs.
30 public function testWidgetButtons() {
31 $this->addParagraphedContentType('paragraphed_test');
33 $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
34 // Add a Paragraph type.
35 $paragraph_type = 'text_paragraph';
36 $this->addParagraphsType($paragraph_type);
37 $this->addParagraphsType('text');
39 // Add a text field to the text_paragraph type.
40 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
42 'fields[field_paragraphs][type]' => 'paragraphs',
44 $this->drupalPostForm('admin/structure/types/manage/paragraphed_test/form-display', $edit, t('Save'));
45 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_text_paragraph_add_more');
47 // Create a node with a Paragraph.
48 $text = 'recognizable_text';
50 'title[0][value]' => 'paragraphs_mode_test',
51 'field_paragraphs[0][subform][field_text][0][value]' => $text,
53 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_paragraph_add_more');
54 $this->drupalPostForm(NULL, $edit, t('Save'));
55 $node = $this->drupalGetNodeByTitle('paragraphs_mode_test');
57 // Test the 'Open' edit mode.
58 $this->drupalGet('node/' . $node->id() . '/edit');
59 $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', $text);
60 $this->drupalPostForm(NULL, [], t('Save'));
61 $this->assertText($text);
63 // Test the 'Closed' edit mode.
64 $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
65 $this->drupalGet('node/' . $node->id() . '/edit');
66 // Click "Edit" button.
67 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
68 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_1_edit');
69 $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', $text);
70 $closed_mode_text = 'closed_mode_text';
71 // Click "Collapse" button on both paragraphs.
72 $edit = ['field_paragraphs[0][subform][field_text][0][value]' => $closed_mode_text];
73 $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_0_collapse');
74 $edit = ['field_paragraphs[1][subform][field_text][0][value]' => $closed_mode_text];
75 $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_1_collapse');
76 // Verify that we have warning message for each paragraph.
77 $this->assertEqual(2, count($this->xpath("//*[contains(@class, 'paragraphs-icon-changed')]")));
78 $this->assertRaw('<div class="paragraphs-collapsed-description">' . $closed_mode_text);
79 $this->drupalPostForm(NULL, [], t('Save'));
80 $this->assertText('paragraphed_test ' . $node->label() . ' has been updated.');
81 $this->assertText($closed_mode_text);
83 // Test the 'Preview' closed mode.
84 $this->setParagraphsWidgetSettings('paragraphed_test', 'field_paragraphs', ['closed_mode' => 'preview']);
85 $this->drupalGet('node/' . $node->id() . '/edit');
86 // Click "Edit" button.
87 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
88 $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', $closed_mode_text);
89 $preview_mode_text = 'preview_mode_text';
90 $edit = ['field_paragraphs[0][subform][field_text][0][value]' => $preview_mode_text];
91 // Click "Collapse" button.
92 $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_0_collapse');
93 $this->assertText('You have unsaved changes on this Paragraph item.');
94 $this->assertEqual(1, count($this->xpath("//*[contains(@class, 'paragraphs-icon-changed')]")));
95 $this->assertText($preview_mode_text);
96 $this->drupalPostForm(NULL, [], t('Save'));
97 $this->assertText('paragraphed_test ' . $node->label() . ' has been updated.');
98 $this->assertText($preview_mode_text);
100 // Test the remove function.
101 $this->drupalGet('node/' . $node->id() . '/edit');
102 // Click "Remove" button.
103 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_remove');
104 $this->drupalPostForm(NULL, [], t('Save'));
105 $this->assertText('paragraphed_test ' . $node->label() . ' has been updated.');
106 $this->assertNoText($preview_mode_text);
110 * Tests if buttons are present for each widget mode.
112 public function testButtonsVisibility() {
113 $this->addParagraphedContentType('paragraphed_test');
115 $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
116 // Add a Paragraph type.
117 $paragraph_type = 'text_paragraph';
118 $this->addParagraphsType($paragraph_type);
119 $this->addParagraphsType('text');
121 // Add a text field to the text_paragraph type.
122 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
124 'fields[field_paragraphs][type]' => 'paragraphs',
126 $this->drupalPostForm('admin/structure/types/manage/paragraphed_test/form-display', $edit, t('Save'));
127 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_text_paragraph_add_more');
129 // Create a node with a Paragraph.
130 $text = 'recognizable_text';
132 'title[0][value]' => 'paragraphs_mode_test',
133 'field_paragraphs[0][subform][field_text][0][value]' => $text,
135 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_paragraph_add_more');
136 $this->drupalPostForm(NULL, $edit, t('Save'));
137 $node = $this->drupalGetNodeByTitle('paragraphs_mode_test');
139 // Checking visible buttons on "Open" mode.
140 $this->drupalGet('node/' . $node->id() . '/edit');
141 $this->assertField('field_paragraphs_0_collapse');
142 $this->assertField('field_paragraphs_0_remove');
143 $this->assertField('field_paragraphs_0_duplicate');
145 // Checking visible buttons on "Closed" mode.
146 $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
147 $this->drupalGet('node/' . $node->id() . '/edit');
148 $this->assertField('field_paragraphs_0_edit');
149 $this->assertField('field_paragraphs_0_remove');
150 $this->assertField('field_paragraphs_0_duplicate');
152 // Checking visible buttons on "Preview" mode.
153 $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
154 $this->drupalGet('node/' . $node->id() . '/edit');
155 $this->assertField('field_paragraphs_0_edit');
156 $this->assertField('field_paragraphs_0_remove');
157 $this->assertField('field_paragraphs_0_duplicate');
159 // Checking always show collapse and edit actions.
160 $this->addParagraphsType('nested_paragraph');
161 static::fieldUIAddNewField('admin/structure/paragraphs_type/nested_paragraph', 'nested', 'Nested', 'field_ui:entity_reference_revisions:paragraph', [
162 'settings[target_type]' => 'paragraph',
163 'cardinality' => '-1',
165 $this->drupalGet('admin/structure/paragraphs_type/nested_paragraph/form-display');
167 'fields[field_nested][type]' => 'paragraphs',
169 $this->drupalPostForm(NULL, $edit, t('Save'));
170 $this->drupalGet('node/' . $node->id() . '/edit');
171 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_nested_paragraph_add_more');
172 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_2_subform_field_nested_nested_paragraph_add_more');
173 // Collapse is present on each nesting level.
174 $this->assertFieldByName('field_paragraphs_2_collapse');
175 $this->assertFieldByName('field_paragraphs_2_subform_field_nested_0_collapse');
177 // Tests hook_paragraphs_widget_actions_alter.
178 $this->drupalGet('node/add/paragraphed_test');
179 $this->drupalPostForm(NULL, NULL, t('Add text'));
180 $this->assertNoField('edit-field-paragraphs-0-top-links-test-button');
181 \Drupal::state()->set('paragraphs_test_dropbutton', TRUE);
182 $this->drupalGet('node/add/paragraphed_test');
183 $this->drupalPostForm(NULL, NULL, t('Add text'));
184 $this->assertNoField('edit-field-paragraphs-0-top-links-test-button');