3 namespace Drupal\paragraphs\Tests\Experimental;
5 use Drupal\field_ui\Tests\FieldUiTestTrait;
8 * Tests paragraphs behavior plugins.
12 class ParagraphsExperimentalBehaviorsTest extends ParagraphsExperimentalTestBase {
21 public static $modules = ['image', 'file', 'views'];
24 * Tests the behavior plugins for paragraphs.
26 public function testBehaviorPluginsFields() {
27 $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
28 $this->loginAsAdmin(['create paragraphed_test content', '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('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
37 // Check default configuration.
38 $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
39 $this->assertFieldByName('behavior_plugins[test_text_color][settings][default_color]', 'blue');
41 $this->assertText('Behavior plugins are only supported by the EXPERIMENTAL paragraphs widget');
42 // Enable the test plugins, with an invalid configuration value.
44 'behavior_plugins[test_bold_text][enabled]' => TRUE,
45 'behavior_plugins[test_text_color][enabled]' => TRUE,
46 'behavior_plugins[test_text_color][settings][default_color]' => 'red',
48 $this->drupalPostForm(NULL, $edit, t('Save'));
49 $this->assertText('Red can not be used as the default color.');
51 // Ensure the form can be saved with an invalid configuration value when
52 // the plugin is not selected.
54 'behavior_plugins[test_bold_text][enabled]' => TRUE,
55 'behavior_plugins[test_text_color][enabled]' => FALSE,
56 'behavior_plugins[test_text_color][settings][default_color]' => 'red',
58 $this->drupalPostForm(NULL, $edit, t('Save'));
59 $this->assertText('Saved the text_paragraph Paragraphs type.');
61 // Ensure it can be saved with a valid value and that the defaults are
63 $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
64 $this->assertFieldChecked('edit-behavior-plugins-test-bold-text-enabled');
65 $this->assertNoFieldChecked('edit-behavior-plugins-test-text-color-enabled');
66 $this->assertFieldByName('behavior_plugins[test_text_color][settings][default_color]', 'blue');
69 'behavior_plugins[test_bold_text][enabled]' => TRUE,
70 'behavior_plugins[test_text_color][enabled]' => TRUE,
71 'behavior_plugins[test_text_color][settings][default_color]' => 'green',
73 $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
74 $this->assertText('Saved the text_paragraph Paragraphs type.');
76 // Create a node with a Paragraph.
77 $this->drupalGet('node/add/paragraphed_test');
78 $this->assertFieldByName('field_paragraphs[0][behavior_plugins][test_text_color][text_color]', 'green');
79 // Setting a not allowed value in the text color plugin text field.
80 $plugin_text = 'green';
82 'title[0][value]' => 'paragraphs_plugins_test',
83 'field_paragraphs[0][subform][field_text][0][value]' => 'amazing_plugin_test',
84 'field_paragraphs[0][behavior_plugins][test_text_color][text_color]' => $plugin_text,
86 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
87 // Asserting that the error message is shown.
88 $this->assertText('The only allowed values are blue and red.');
89 // Updating the text color to an allowed value.
92 'field_paragraphs[0][behavior_plugins][test_text_color][text_color]' => $plugin_text,
94 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
95 // Assert that the class has been added to the element.
96 $this->assertRaw('class="red_plugin_text');
98 $this->clickLink('Edit');
99 // Assert the plugin fields populate the stored values.
100 $this->assertFieldByName('field_paragraphs[0][behavior_plugins][test_text_color][text_color]', $plugin_text);
102 // Update the value of both plugins.
103 $updated_text = 'blue';
105 'field_paragraphs[0][behavior_plugins][test_text_color][text_color]' => $updated_text,
106 'field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]' => TRUE,
108 $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
109 $this->assertNoRaw('class="red_plugin_text');
110 $this->assertRaw('class="blue_plugin_text bold_plugin_text');
111 $this->clickLink('Edit');
112 // Assert the plugin fields populate the stored values.
113 $this->assertFieldByName('field_paragraphs[0][behavior_plugins][test_text_color][text_color]', $updated_text);
114 $this->assertFieldByName('field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]', TRUE);
116 // Test plugin applicability. Add a paragraph type.
117 $paragraph_type = 'text_paragraph_test';
118 $this->addParagraphsType($paragraph_type);
119 // Add a text field to the text_paragraph type.
120 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text_test', 'Text', 'text_long', [], []);
121 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'image', 'Image', 'image', [], []);
122 // Assert if the plugin is listed on the edit form of the paragraphs type.
123 $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
124 $this->assertNoFieldByName('behavior_plugins[test_bold_text][enabled]');
125 $this->assertFieldByName('behavior_plugins[test_text_color][enabled]');
126 $this->assertFieldByName('behavior_plugins[test_field_selection][enabled]');
127 $this->assertText('Choose paragraph field to be applied.');
128 // Assert that Field Selection Filter plugin properly filters field types.
129 $this->assertOptionByText('edit-behavior-plugins-test-field-selection-settings-field-selection-filter', t('Image'));
130 // Check that Field Selection Plugin does not filter any field types.
131 $this->assertOptionByText('edit-behavior-plugins-test-field-selection-settings-field-selection', t('Image'));
132 $this->assertOptionByText('edit-behavior-plugins-test-field-selection-settings-field-selection', t('Text'));
134 // Test a plugin without behavior fields.
136 'behavior_plugins[test_dummy_behavior][enabled]' => TRUE,
137 'behavior_plugins[test_text_color][enabled]' => TRUE,
139 $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
140 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_text_paragraph_test_add_more');
142 'title[0][value]' => 'paragraph with no fields',
143 'field_paragraphs[0][subform][field_text_test][0][value]' => 'my behavior plugin does not have any field',
145 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
146 $this->assertRaw('dummy_plugin_text');
150 * Tests the behavior plugins summary for paragraphs closed mode.
152 public function testCollapsedSummary() {
153 $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
154 $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
156 // Add a text paragraph type.
157 $paragraph_type = 'text_paragraph';
158 $this->addParagraphsType($paragraph_type);
159 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
160 $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
161 // Enable plugins for the text paragraph type.
163 'behavior_plugins[test_bold_text][enabled]' => TRUE,
164 'behavior_plugins[test_text_color][enabled]' => TRUE,
166 $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
168 // Add a nested Paragraph type.
169 $paragraph_type = 'nested_paragraph';
170 $this->addParagraphsType($paragraph_type);
171 $this->addParagraphsField('nested_paragraph', 'paragraphs', 'paragraph');
172 // Enable plugins for the nested paragraph type.
174 'behavior_plugins[test_bold_text][enabled]' => TRUE,
176 $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
178 // Add a node and enabled plugins.
179 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_nested_paragraph_add_more');
180 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_1_subform_paragraphs_text_paragraph_add_more');
182 'title[0][value]' => 'collapsed_test',
183 'field_paragraphs[0][subform][field_text][0][value]' => 'first_paragraph',
184 'field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]' => TRUE,
185 'field_paragraphs[1][subform][paragraphs][0][subform][field_text][0][value]' => 'nested_paragraph',
186 'field_paragraphs[1][behavior_plugins][test_bold_text][bold_text]' => TRUE,
188 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
190 // Assert that the summary includes the text of the behavior plugins.
191 $this->clickLink('Edit');
192 $this->assertRaw('class="paragraphs-collapsed-description">first_paragraph, Text color: blue, Bold: Yes');
193 $this->assertRaw('class="paragraphs-collapsed-description">nested_paragraph, Text color: blue, Bold: No, Bold: Yes');
197 * Tests the behavior plugins subform state submit.
199 public function testBehaviorSubform() {
200 $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
201 $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
203 // Add a text paragraph type.
204 $paragraph_type = 'text_paragraph';
205 $this->addParagraphsType($paragraph_type);
206 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
207 // Enable plugins for the text paragraph type.
209 'behavior_plugins[test_bold_text][enabled]' => TRUE,
210 'behavior_plugins[test_text_color][enabled]' => TRUE,
212 $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
214 // Add a nested Paragraph type.
215 $paragraph_type = 'nested_paragraph';
216 $this->addParagraphsType($paragraph_type);
217 static::fieldUIAddNewField('admin/structure/paragraphs_type/nested_paragraph', 'nested', 'Nested', 'field_ui:entity_reference_revisions:paragraph', [
218 'settings[target_type]' => 'paragraph',
219 'cardinality' => '-1',
221 // Enable plugins for the nested paragraph type.
223 'behavior_plugins[test_bold_text][enabled]' => TRUE,
225 $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
227 // Add a node and enabled plugins.
228 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_nested_paragraph_add_more');
229 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_paragraph_add_more');
230 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_subform_field_nested_text_paragraph_add_more');
232 'title[0][value]' => 'collapsed_test',
233 'field_paragraphs[0][subform][field_nested][0][subform][field_text][0][value]' => 'nested text paragraph',
234 'field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]' => TRUE,
235 'field_paragraphs[1][subform][field_text][0][value]' => 'first_paragraph',
236 'field_paragraphs[1][behavior_plugins][test_bold_text][bold_text]' => TRUE,
238 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
240 $this->clickLink('Edit');
242 'field_paragraphs[0][_weight]' => 1,
243 'field_paragraphs[1][_weight]' => 0,
245 $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
246 $this->assertNoErrorsLogged();