3 namespace Drupal\paragraphs\Tests\Experimental;
5 use Symfony\Component\CssSelector\CssSelectorConverter;
8 * Tests collapse all button.
12 class ParagraphsExperimentalHeaderActionsTest extends ParagraphsExperimentalTestBase {
15 * Tests header actions.
17 public function testHeaderActions() {
19 // To test with a single header action, ensure the drag and drop action is
20 // shown, even without the library.
21 \Drupal::state()->set('paragraphs_test_dragdrop_force_show', TRUE);
23 $this->addParagraphedContentType('paragraphed_test');
26 'create paragraphed_test content',
27 'edit any paragraphed_test content',
30 // Add a Paragraph type.
31 $paragraph_type = 'text_paragraph';
32 $this->addParagraphsType($paragraph_type);
34 // Add a text field to the text_paragraph type.
35 static::fieldUIAddNewField(
36 'admin/structure/paragraphs_type/' . $paragraph_type,
44 // Add 2 paragraphs and check for Collapse/Edit all button.
45 $this->drupalGet('node/add/paragraphed_test');
46 $this->assertNoRaw('field_paragraphs_collapse_all');
47 $this->assertNoRaw('field_paragraphs_edit_all');
48 $this->assertRaw('field_paragraphs_dragdrop_mode');
50 // Ensure there is only a single table row.
51 $table_rows = $this->xpath('//table[contains(@class, :class)]/tbody/tr', [':class' => 'field-multiple-table']);
52 $this->assertEqual(1, count($table_rows));
54 // Add second paragraph and check for Collapse/Edit all button.
55 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_paragraph_add_more');
56 $this->assertRaw('field_paragraphs_collapse_all');
57 $this->assertRaw('field_paragraphs_edit_all');
60 'field_paragraphs[0][subform][field_text][0][value]' => 'First text',
61 'field_paragraphs[1][subform][field_text][0][value]' => 'Second text',
63 $this->drupalPostForm(NULL, $edit, 'Collapse all');
65 // Checks that after collapsing all we can edit again these paragraphs.
66 $this->assertRaw('field_paragraphs_0_edit');
67 $this->assertRaw('field_paragraphs_1_edit');
69 // Test Edit all button.
70 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_edit_all');
71 $this->assertRaw('field_paragraphs_0_collapse');
72 $this->assertRaw('field_paragraphs_1_collapse');
75 'title[0][value]' => 'Test',
77 $this->drupalPostForm(NULL, $edit, t('Save'));
78 $this->assertText('paragraphed_test Test has been created.');
80 $node = $this->getNodeByTitle('Test');
81 $this->drupalGet('node/' . $node->id());
82 $this->clickLink('Edit');
83 $this->assertNoText('No Paragraph added yet.');
85 // Add and remove another paragraph.
86 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_paragraph_add_more');
88 'field_paragraphs[2][subform][field_text][0][value]' => 'Third text',
90 $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_2_remove');
92 // Check that pressing "Collapse all" does not restore the removed
94 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_edit_all');
95 $this->assertText('First text');
96 $this->assertText('Second text');
97 $this->assertNoText('Third text');
99 // Check that pressing "Edit all" does not restore the removed paragraph,
101 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_collapse_all');
102 $this->assertText('First text');
103 $this->assertText('Second text');
104 $this->assertNoText('Third text');
108 * Tests that header actions works fine with nesting.
110 public function testHeaderActionsWithNesting() {
111 $this->addParagraphedContentType('paragraphed_test');
113 $this->loginAsAdmin([
114 'create paragraphed_test content',
115 'edit any paragraphed_test content',
118 // Add Paragraph types.
119 $nested_paragraph_type = 'nested_paragraph';
120 $this->addParagraphsType($nested_paragraph_type);
121 $paragraph_type = 'text';
122 $this->addParagraphsType($paragraph_type);
124 // Add a text field to the text_paragraph type.
125 static::fieldUIAddNewField(
126 'admin/structure/paragraphs_type/' . $paragraph_type,
134 // Add a ERR paragraph field to the nested_paragraph type.
135 static::fieldUIAddNewField(
136 'admin/structure/paragraphs_type/' . $nested_paragraph_type,
139 'field_ui:entity_reference_revisions:paragraph', [
140 'settings[target_type]' => 'paragraph',
141 'cardinality' => '-1',
146 // Checks that Collapse/Edit all button is presented.
147 $this->drupalGet('node/add/paragraphed_test');
148 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_nested_paragraph_add_more');
149 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_add_more');
150 $this->assertRaw('field_paragraphs_collapse_all');
151 $this->assertRaw('field_paragraphs_edit_all');
153 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_add_more');
154 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_subform_field_nested_text_add_more');
155 $this->assertNoRaw('field_paragraphs_0_collapse_all');
156 $this->assertNoRaw('field_paragraphs_0_edit_all');
158 'field_paragraphs[0][subform][field_nested][0][subform][field_text][0][value]' => 'Nested text',
159 'field_paragraphs[1][subform][field_text][0][value]' => 'Second text paragraph',
161 $this->drupalPostForm(NULL, $edit, 'Collapse all');
162 $this->assertRaw('field-paragraphs-0-edit');
163 $this->assertFieldByXPath((new CssSelectorConverter())->toXPath('[name="field_paragraphs_1_edit"] + .paragraphs-dropdown'));
164 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_edit_all');
165 $this->assertRaw('field-paragraphs-0-collapse');
168 'title[0][value]' => 'Test',
170 $this->drupalPostForm(NULL, $edit, t('Save'));
171 $this->assertText('paragraphed_test Test has been created.');
173 $node = $this->getNodeByTitle('Test');
174 $this->drupalGet('node/' . $node->id());
175 $this->clickLink('Edit');
176 $this->assertNoText('No Paragraph added yet.');
180 * Tests header actions with multi fields.
182 public function testHeaderActionsWithMultiFields() {
183 $this->addParagraphedContentType('paragraphed_test');
184 $this->loginAsAdmin([
185 'create paragraphed_test content',
186 'edit any paragraphed_test content',
188 $this->drupalGet('/admin/structure/types/manage/paragraphed_test/fields/add-field');
190 // Add a Paragraph type.
191 $paragraph_type = 'text_paragraph';
192 $this->addParagraphsType($paragraph_type);
194 'new_storage_type' => 'field_ui:entity_reference_revisions:paragraph',
195 'label' => 'Second paragraph',
196 'field_name' => 'second',
198 $this->drupalPostForm(NULL, $edit, 'Save and continue');
199 $this->drupalPostForm(NULL, [], 'Save field settings');
200 $this->drupalPostForm(NULL, [], 'Save settings');
202 $this->drupalGet('/admin/structure/types/manage/paragraphed_test/form-display');
204 'fields[field_second][type]' => 'paragraphs',
206 $this->drupalPostForm(NULL, $edit, 'Save');
208 // Add a text field to the text_paragraph type.
209 static::fieldUIAddNewField(
210 'admin/structure/paragraphs_type/' . $paragraph_type,
218 $this->drupalGet('node/add/paragraphed_test');
219 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_paragraph_add_more');
220 $this->drupalPostAjaxForm(NULL, [], 'field_second_text_paragraph_add_more');
222 // Checks that we have Collapse\Edit all for each field.
223 $this->assertRaw('field_paragraphs_collapse_all');
224 $this->assertRaw('field_paragraphs_edit_all');
225 $this->assertRaw('field_second_collapse_all');
226 $this->assertRaw('field_second_edit_all');
229 'field_second[0][subform][field_text][0][value]' => 'Second field',
231 $this->drupalPostAjaxForm(NULL, $edit, 'field_second_collapse_all');
233 // Checks that we collapsed only children from second field.
234 $this->assertNoRaw('field_paragraphs_0_edit');
235 $this->assertRaw('field_second_0_edit');
237 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_collapse_all');
238 $this->assertRaw('field_paragraphs_0_edit');
239 $this->assertRaw('field_second_0_edit');
241 $this->drupalPostAjaxForm(NULL, [], 'field_second_edit_all');
242 $this->assertRaw('field_second_0_collapse');
245 'title[0][value]' => 'Test',
247 $this->drupalPostForm(NULL, $edit, t('Save'));
248 $this->assertText('paragraphed_test Test has been created.');
250 $node = $this->getNodeByTitle('Test');
251 $this->drupalGet('node/' . $node->id());
252 $this->clickLink('Edit');
253 $this->assertNoText('No Paragraph added yet.');