3 namespace Drupal\paragraphs\Tests\Experimental;
5 use Drupal\field_ui\Tests\FieldUiTestTrait;
6 use Drupal\paragraphs\Entity\Paragraph;
9 * Tests paragraphs behavior plugins.
13 class ParagraphsExperimentalBehaviorsTest extends ParagraphsExperimentalTestBase {
22 public static $modules = ['image', 'file', 'views'];
25 * Tests the behavior plugins for paragraphs.
27 public function testBehaviorPluginsFields() {
28 $this->addParagraphedContentType('paragraphed_test');
29 $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
31 // Add a Paragraph type.
32 $paragraph_type = 'text_paragraph';
33 $this->addParagraphsType($paragraph_type);
35 // Add a text field to the text_paragraph type.
36 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
38 // Check default configuration.
39 $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
40 $this->assertFieldByName('behavior_plugins[test_text_color][settings][default_color]', 'blue');
42 $this->assertText('Behavior plugins are only supported by the EXPERIMENTAL paragraphs widget');
43 // Enable the test plugins, with an invalid configuration value.
45 'behavior_plugins[test_bold_text][enabled]' => TRUE,
46 'behavior_plugins[test_text_color][enabled]' => TRUE,
47 'behavior_plugins[test_text_color][settings][default_color]' => 'red',
49 $this->drupalPostForm(NULL, $edit, t('Save'));
50 $this->assertText('Red can not be used as the default color.');
52 // Ensure the form can be saved with an invalid configuration value when
53 // the plugin is not selected.
55 'behavior_plugins[test_bold_text][enabled]' => TRUE,
56 'behavior_plugins[test_text_color][enabled]' => FALSE,
57 'behavior_plugins[test_text_color][settings][default_color]' => 'red',
59 $this->drupalPostForm(NULL, $edit, t('Save'));
60 $this->assertText('Saved the text_paragraph Paragraphs type.');
62 // Ensure it can be saved with a valid value and that the defaults are
64 $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
65 $this->assertFieldChecked('edit-behavior-plugins-test-bold-text-enabled');
66 $this->assertNoFieldChecked('edit-behavior-plugins-test-text-color-enabled');
67 $this->assertFieldByName('behavior_plugins[test_text_color][settings][default_color]', 'blue');
70 'behavior_plugins[test_bold_text][enabled]' => TRUE,
71 'behavior_plugins[test_text_color][enabled]' => TRUE,
72 'behavior_plugins[test_text_color][settings][default_color]' => 'green',
74 $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
75 $this->assertText('Saved the text_paragraph Paragraphs type.');
77 $this->drupalGet('node/add/paragraphed_test');
79 // Behavior plugin settings is not available to users without
80 // "edit behavior plugin settings" permission.
81 $this->assertNoFieldByName('field_paragraphs[0][behavior_plugins][test_text_color][text_color]', 'green');
84 'create paragraphed_test content',
85 'edit any paragraphed_test content',
86 'edit behavior plugin settings',
89 // Create a node with a Paragraph.
90 $this->drupalGet('node/add/paragraphed_test');
91 $this->assertFieldByName('field_paragraphs[0][behavior_plugins][test_text_color][text_color]', 'green');
92 // Setting a not allowed value in the text color plugin text field.
93 $plugin_text = 'green';
95 'title[0][value]' => 'paragraphs_plugins_test',
96 'field_paragraphs[0][subform][field_text][0][value]' => 'amazing_plugin_test',
97 'field_paragraphs[0][behavior_plugins][test_text_color][text_color]' => $plugin_text,
99 // Assert that the behavior form is after the dropbutton.
100 $behavior_xpath = $this->xpath("//div[@id = 'edit-field-paragraphs-0-top']/following-sibling::*[1][@id = 'edit-field-paragraphs-0-behavior-plugins-test-text-color']");
101 $this->assertNotEqual($behavior_xpath, FALSE, 'Behavior form position incorrect');
103 $this->drupalPostForm(NULL, $edit, t('Save'));
104 // Asserting that the error message is shown.
105 $this->assertText('The only allowed values are blue and red.');
106 // Updating the text color to an allowed value.
107 $plugin_text = 'red';
109 'field_paragraphs[0][behavior_plugins][test_text_color][text_color]' => $plugin_text,
111 $this->drupalPostForm(NULL, $edit, t('Save'));
112 // Assert that the class has been added to the element.
113 $this->assertRaw('class="red_plugin_text');
115 $this->clickLink('Edit');
116 // Assert the plugin fields populate the stored values.
117 $this->assertFieldByName('field_paragraphs[0][behavior_plugins][test_text_color][text_color]', $plugin_text);
119 // Update the value of both plugins.
120 $updated_text = 'blue';
122 'field_paragraphs[0][behavior_plugins][test_text_color][text_color]' => $updated_text,
123 'field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]' => TRUE,
125 $this->drupalPostForm(NULL, $edit, t('Save'));
126 $this->assertNoRaw('class="red_plugin_text');
127 $this->assertRaw('class="blue_plugin_text bold_plugin_text');
128 $this->clickLink('Edit');
129 // Assert the plugin fields populate the stored values.
130 $this->assertFieldByName('field_paragraphs[0][behavior_plugins][test_text_color][text_color]', $updated_text);
131 $this->assertFieldByName('field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]', TRUE);
133 $this->loginAsAdmin([
134 'edit any paragraphed_test content',
137 $node = $this->getNodeByTitle('paragraphs_plugins_test');
138 $this->drupalGet('node/' . $node->id() . '/edit');
140 $this->assertNoFieldByName('field_paragraphs[0][behavior_plugins][test_text_color][text_color]', $updated_text);
141 $this->assertNoFieldByName('field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]', TRUE);
143 $this->drupalPostForm(NULL, [], t('Save'));
145 // Make sure that values don't change if a user without the 'edit behavior
146 // plugin settings' permission saves a node with paragraphs and enabled
148 $this->assertRaw('class="blue_plugin_text bold_plugin_text');
149 $this->assertNoRaw('class="red_plugin_text');
151 // Test plugin applicability. Add a paragraph type.
152 $paragraph_type = 'text_paragraph_test';
153 $this->addParagraphsType($paragraph_type);
154 // Add a text field to the text_paragraph type.
155 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text_test', 'Text', 'text_long', [], []);
156 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'image', 'Image', 'image', [], []);
157 // Assert if the plugin is listed on the edit form of the paragraphs type.
158 $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
159 $this->assertNoFieldByName('behavior_plugins[test_bold_text][enabled]');
160 $this->assertFieldByName('behavior_plugins[test_text_color][enabled]');
161 $this->assertFieldByName('behavior_plugins[test_field_selection][enabled]');
162 $this->assertText('Choose paragraph field to be applied.');
163 // Assert that Field Selection Filter plugin properly filters field types.
164 $this->assertOptionByText('edit-behavior-plugins-test-field-selection-settings-field-selection-filter', t('Image'));
165 // Check that Field Selection Plugin does not filter any field types.
166 $this->assertOptionByText('edit-behavior-plugins-test-field-selection-settings-field-selection', t('Image'));
167 $this->assertOptionByText('edit-behavior-plugins-test-field-selection-settings-field-selection', t('Text'));
169 // Test a plugin without behavior fields.
171 'behavior_plugins[test_dummy_behavior][enabled]' => TRUE,
172 'behavior_plugins[test_text_color][enabled]' => TRUE,
174 $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
175 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_text_paragraph_test_add_more');
177 'title[0][value]' => 'paragraph with no fields',
178 'field_paragraphs[0][subform][field_text_test][0][value]' => 'my behavior plugin does not have any field',
180 $this->drupalPostForm(NULL, $edit, t('Save'));
181 $this->assertRaw('dummy_plugin_text');
183 // Tests behavior plugin on paragraph type with no fields.
184 $this->addParagraphsType('fieldless');
185 $this->drupalPostForm('admin/structure/paragraphs_type/fieldless', ['behavior_plugins[test_dummy_behavior][enabled]' => TRUE], t('Save'));
187 $this->drupalGet('node/add/paragraphed_test');
188 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_fieldless_add_more');
189 $edit = ['title[0][value]' => t('Fieldless')];
190 $this->drupalPostForm(NULL, $edit, t('Save'));
192 $this->assertResponse(200);
196 * Tests the behavior plugins summary for paragraphs closed mode.
198 public function testCollapsedSummary() {
199 $this->addParagraphedContentType('paragraphed_test');
200 $this->loginAsAdmin([
201 'create paragraphed_test content',
202 'edit any paragraphed_test content',
203 'edit behavior plugin settings',
206 // Add a text paragraph type.
207 $paragraph_type = 'text_paragraph';
208 $this->addParagraphsType($paragraph_type);
209 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
210 $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
211 // Enable plugins for the text paragraph type.
213 'behavior_plugins[test_bold_text][enabled]' => TRUE,
214 'behavior_plugins[test_text_color][enabled]' => TRUE,
216 $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
218 // Add a nested Paragraph type.
219 $paragraph_type = 'nested_paragraph';
220 $this->addParagraphsType($paragraph_type);
221 $this->addParagraphsField('nested_paragraph', 'paragraphs', 'paragraph');
222 // Enable plugins for the nested paragraph type.
224 'behavior_plugins[test_bold_text][enabled]' => TRUE,
226 $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
228 // Add a node and enabled plugins.
229 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_nested_paragraph_add_more');
230 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_1_subform_paragraphs_text_paragraph_add_more');
232 $this->assertField('field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]');
233 $this->assertField('field_paragraphs[1][behavior_plugins][test_bold_text][bold_text]');
236 'title[0][value]' => 'collapsed_test',
237 'field_paragraphs[0][subform][field_text][0][value]' => 'first_paragraph',
238 'field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]' => TRUE,
239 'field_paragraphs[1][subform][paragraphs][0][subform][field_text][0][value]' => 'nested_paragraph',
240 'field_paragraphs[1][behavior_plugins][test_bold_text][bold_text]' => TRUE,
242 $this->drupalPostForm(NULL, $edit, t('Save'));
244 // Assert that the summary includes the text of the behavior plugins.
245 $this->clickLink('Edit');
246 $this->assertRaw('class="paragraphs-collapsed-description">first_paragraph, Text color: blue, Bold: Yes');
247 $this->assertRaw('class="paragraphs-collapsed-description">1 child, nested_paragraph, Text color: blue, Bold: No, Bold: Yes');
249 // Add an empty nested paragraph.
250 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_nested_paragraph_add_more');
252 'title[0][value]' => 'collapsed_test',
254 $this->drupalPostForm(NULL, $edit, t('Save'));
256 // Check an empty nested paragraph summary.
257 $this->clickLink('Edit');
258 $this->assertRaw('class="paragraphs-collapsed-description">');
263 * Tests the behavior plugins subform state submit.
265 public function testBehaviorSubform() {
266 $this->addParagraphedContentType('paragraphed_test');
267 $this->loginAsAdmin([
268 'create paragraphed_test content',
269 'edit any paragraphed_test content',
270 'edit behavior plugin settings',
273 // Add a text paragraph type.
274 $paragraph_type = 'text_paragraph';
275 $this->addParagraphsType($paragraph_type);
276 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
277 // Enable plugins for the text paragraph type.
279 'behavior_plugins[test_bold_text][enabled]' => TRUE,
280 'behavior_plugins[test_text_color][enabled]' => TRUE,
282 $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
284 // Add a nested Paragraph type.
285 $paragraph_type = 'nested_paragraph';
286 $this->addParagraphsType($paragraph_type);
287 static::fieldUIAddNewField('admin/structure/paragraphs_type/nested_paragraph', 'nested', 'Nested', 'field_ui:entity_reference_revisions:paragraph', [
288 'settings[target_type]' => 'paragraph',
289 'cardinality' => '-1',
291 // Enable plugins for the nested paragraph type.
293 'behavior_plugins[test_bold_text][enabled]' => TRUE,
295 $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
297 // Add a node and enabled plugins.
298 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_nested_paragraph_add_more');
299 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_paragraph_add_more');
300 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_subform_field_nested_text_paragraph_add_more');
302 'title[0][value]' => 'collapsed_test',
303 'field_paragraphs[0][subform][field_nested][0][subform][field_text][0][value]' => 'nested text paragraph',
304 'field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]' => TRUE,
305 'field_paragraphs[1][subform][field_text][0][value]' => 'first_paragraph',
306 'field_paragraphs[1][behavior_plugins][test_bold_text][bold_text]' => TRUE,
308 $this->drupalPostForm(NULL, $edit, t('Save'));
310 $this->clickLink('Edit');
312 'field_paragraphs[0][_weight]' => 1,
313 'field_paragraphs[1][behavior_plugins][test_bold_text][bold_text]' => FALSE,
314 'field_paragraphs[1][behavior_plugins][test_text_color][text_color]' => 'red',
315 'field_paragraphs[1][_weight]' => 0,
317 $this->drupalPostForm(NULL, $edit, t('Save'));
318 $this->assertNoErrorsLogged();
319 $this->clickLink('Edit');
320 $this->assertFieldByName('field_paragraphs[0][behavior_plugins][test_text_color][text_color]', 'red');