3 namespace Drupal\Tests\paragraphs\Functional;
5 use Drupal\paragraphs\Tests\Classic\ParagraphsCoreVersionUiTestTrait;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Tests\paragraphs\FunctionalJavascript\LoginAdminTrait;
8 use Drupal\Tests\paragraphs\FunctionalJavascript\ParagraphsTestBaseTrait;
11 * Tests paragraphs experimental widget buttons.
15 class ParagraphsExperimentalWidgetButtonsTest extends BrowserTestBase {
18 use ParagraphsCoreVersionUiTestTrait;
19 use ParagraphsTestBaseTrait;
26 public static $modules = [
36 * Tests the autocollapse functionality.
38 public function testAutocollapse() {
39 $this->addParagraphedContentType('paragraphed_test');
42 'administer content types',
43 'administer node fields',
44 'administer paragraphs types',
45 'administer node form display',
46 'administer paragraph fields',
47 'administer paragraph form display',
48 'create paragraphed_test content',
49 'edit any paragraphed_test content',
51 $this->loginAsAdmin($permissions, TRUE);
53 // Add a text Paragraph type.
54 $this->addParagraphsType('text_paragraph');
55 $this->addFieldtoParagraphType('text_paragraph', 'field_text', 'text_long');
57 // Add another Paragraph type so that there is no default Paragraphs type.
58 $this->addParagraphsType('another_paragraph');
60 // Check that the paragraphs field uses the experimental widget.
61 $this->drupalGet('admin/structure/types/manage/paragraphed_test/form-display');
62 $option = $this->assertSession()->optionExists('fields[field_paragraphs][type]', 'paragraphs');
63 $this->assertTrue($option->isSelected());
64 // Check that the autocollapse is disabled by default.
65 $this->assertSession()->pageTextContains('Autocollapse: None');
67 // Create a new node with 2 paragraphs.
68 $this->drupalGet('node/add/paragraphed_test');
69 $this->getSession()->getPage()->findButton('field_paragraphs_text_paragraph_add_more')->press();
70 $this->getSession()->getPage()->findButton('field_paragraphs_text_paragraph_add_more')->press();
72 'title[0][value]' => 'Autocollapse test node',
73 'field_paragraphs[0][subform][field_text][0][value]' => 'Fist paragraph',
74 'field_paragraphs[1][subform][field_text][0][value]' => 'Second paragraph',
76 $this->drupalPostForm(NULL, $edit, t('Save'));
77 $node = $this->drupalGetNodeByTitle('Autocollapse test node');
79 // Set the settings to "Open" edit mode without autocollapse.
81 'edit_mode' => 'open',
82 'closed_mode' => 'summary',
83 'autocollapse' => 'none',
85 $this->setParagraphsWidgetSettings('paragraphed_test', 'field_paragraphs', $settings);
87 // Edit the node. Edit mode is "Open". All paragraphs are in the "Edit"
89 $this->drupalGet('node/' . $node->id() . '/edit');
90 $this->checkParagraphInMode('field_paragraphs_0', 'edit');
91 $this->checkParagraphInMode('field_paragraphs_1', 'edit');
93 // Autocollapse is disabled. Closing and opening a paragraphs does not
94 // affect the other one.
95 $this->getSession()->getPage()->findButton('field_paragraphs_0_collapse')->press();
96 $this->checkParagraphInMode('field_paragraphs_0', 'closed');
97 $this->checkParagraphInMode('field_paragraphs_1', 'edit');
99 $this->getSession()->getPage()->findButton('field_paragraphs_0_edit')->press();
100 $this->checkParagraphInMode('field_paragraphs_0', 'edit');
101 $this->checkParagraphInMode('field_paragraphs_1', 'edit');
103 // "Collapse all" enables autocollapse.
104 $this->getSession()->getPage()->findButton('field_paragraphs_collapse_all')->press();
105 $this->checkParagraphInMode('field_paragraphs_0', 'closed');
106 $this->checkParagraphInMode('field_paragraphs_1', 'closed');
108 // Open the first paragraph and then the second. Opening the second closes
110 $this->getSession()->getPage()->findButton('field_paragraphs_0_edit')->press();
111 $this->checkParagraphInMode('field_paragraphs_0', 'edit');
112 $this->checkParagraphInMode('field_paragraphs_1', 'closed');
114 $this->getSession()->getPage()->findButton('field_paragraphs_1_edit')->press();
115 $this->checkParagraphInMode('field_paragraphs_0', 'closed');
116 $this->checkParagraphInMode('field_paragraphs_1', 'edit');
118 // "Edit all" disables autocollapse.
119 $this->getSession()->getPage()->findButton('field_paragraphs_edit_all')->press();
120 $this->checkParagraphInMode('field_paragraphs_0', 'edit');
121 $this->checkParagraphInMode('field_paragraphs_1', 'edit');
123 // Closing and opening a paragraphs does not affect the other one anymore.
124 $this->getSession()->getPage()->findButton('field_paragraphs_0_collapse')->press();
125 $this->checkParagraphInMode('field_paragraphs_0', 'closed');
126 $this->checkParagraphInMode('field_paragraphs_1', 'edit');
128 $this->getSession()->getPage()->findButton('field_paragraphs_0_edit')->press();
129 $this->checkParagraphInMode('field_paragraphs_0', 'edit');
130 $this->checkParagraphInMode('field_paragraphs_1', 'edit');
132 // Enable autocollapse. Set edit mode to "Closed".
134 'edit_mode' => 'closed',
135 'closed_mode' => 'summary',
136 'autocollapse' => 'all',
138 $this->setParagraphsWidgetSettings('paragraphed_test', 'field_paragraphs', $settings);
140 // Edit the node. All paragraphs are closed.
141 $this->drupalGet('node/' . $node->id() . '/edit');
142 $this->checkParagraphInMode('field_paragraphs_0', 'closed');
143 $this->checkParagraphInMode('field_paragraphs_1', 'closed');
145 // Open the first paragraph and then the second. Opening the second closes
147 $this->getSession()->getPage()->findButton('field_paragraphs_0_edit')->press();
148 $this->checkParagraphInMode('field_paragraphs_0', 'edit');
149 $this->checkParagraphInMode('field_paragraphs_1', 'closed');
151 $this->getSession()->getPage()->findButton('field_paragraphs_1_edit')->press();
152 $this->checkParagraphInMode('field_paragraphs_0', 'closed');
153 $this->checkParagraphInMode('field_paragraphs_1', 'edit');
155 // "Edit all" disables auto collapse.
156 $this->getSession()->getPage()->findButton('field_paragraphs_edit_all')->press();
157 $this->checkParagraphInMode('field_paragraphs_0', 'edit');
158 $this->checkParagraphInMode('field_paragraphs_1', 'edit');
160 // Closing and opening a paragraphs does not affect the other one anymore.
161 $this->getSession()->getPage()->findButton('field_paragraphs_0_collapse')->press();
162 $this->checkParagraphInMode('field_paragraphs_0', 'closed');
163 $this->checkParagraphInMode('field_paragraphs_1', 'edit');
165 $this->getSession()->getPage()->findButton('field_paragraphs_0_edit')->press();
166 $this->checkParagraphInMode('field_paragraphs_0', 'edit');
167 $this->checkParagraphInMode('field_paragraphs_1', 'edit');
169 // "Collapse all" re-enables autocollapse.
170 $this->getSession()->getPage()->findButton('field_paragraphs_collapse_all')->press();
171 $this->checkParagraphInMode('field_paragraphs_0', 'closed');
172 $this->checkParagraphInMode('field_paragraphs_1', 'closed');
174 // Open the first paragraph and then the second. Opening the second closes
176 $this->getSession()->getPage()->findButton('field_paragraphs_0_edit')->press();
177 $this->checkParagraphInMode('field_paragraphs_0', 'edit');
178 $this->checkParagraphInMode('field_paragraphs_1', 'closed');
180 $this->getSession()->getPage()->findButton('field_paragraphs_1_edit')->press();
181 $this->checkParagraphInMode('field_paragraphs_0', 'closed');
182 $this->checkParagraphInMode('field_paragraphs_1', 'edit');
184 // Check that adding a new paragraphs closes the others.
185 $this->getSession()->getPage()->findButton('field_paragraphs_text_paragraph_add_more')->press();
186 $this->getSession()->getPage()->fillField('field_paragraphs[2][subform][field_text][0][value]', 'Third paragraph');
187 $this->checkParagraphInMode('field_paragraphs_0', 'closed');
188 $this->checkParagraphInMode('field_paragraphs_1', 'closed');
189 $this->checkParagraphInMode('field_paragraphs_2', 'edit');
191 // Check that duplicating closes the other paragraphs.
192 $this->getSession()->getPage()->findButton('field_paragraphs_2_duplicate')->press();
193 $this->getSession()->getPage()->fillField('field_paragraphs[3][subform][field_text][0][value]', 'Fourth paragraph');
194 $this->checkParagraphInMode('field_paragraphs_0', 'closed');
195 $this->checkParagraphInMode('field_paragraphs_1', 'closed');
196 $this->checkParagraphInMode('field_paragraphs_2', 'closed');
197 $this->checkParagraphInMode('field_paragraphs_3', 'edit');
199 // Check that autocollapse does not restore removed paragraphs.
200 $this->getSession()->getPage()->findButton('field_paragraphs_3_remove')->press();
201 $this->checkParagraphInMode('field_paragraphs_3', 'removed');
202 $this->getSession()->getPage()->findButton('field_paragraphs_2_edit')->press();
203 $this->checkParagraphInMode('field_paragraphs_3', 'removed');
207 * Asserts that a paragraph is in a particular mode.
209 * It does this indirectly by checking checking what buttons are available.
211 * @param string $button_prefix
212 * An initial part of the button name; namely "<paragraphs_field>_<delta>".
214 * @param string $mode
215 * Assert that the paragraphs is in this widget item mode. Supported modes
216 * are "edit", "closed" and "removed". A paragraph in the "removed" mode
217 * cannot be distinguished from one that has never been added.
219 public function checkParagraphInMode($button_prefix, $mode) {
222 $this->assertSession()->buttonNotExists($button_prefix . '_edit');
223 $this->assertSession()->buttonExists($button_prefix . '_collapse');
226 $this->assertSession()->buttonExists($button_prefix . '_edit');
227 $this->assertSession()->buttonNotExists($button_prefix . '_collapse');
230 $this->assertSession()->buttonNotExists($button_prefix . '_edit');
231 $this->assertSession()->buttonNotExists($button_prefix . '_collapse');
234 throw new \InvalidArgumentException('This function does not support "' . $mode . '" as an argument for "$mode" parameter');