Version 1
[yaffs-website] / web / modules / contrib / paragraphs / src / Tests / Experimental / ParagraphsExperimentalBehaviorsTest.php
1 <?php
2
3 namespace Drupal\paragraphs\Tests\Experimental;
4
5 use Drupal\field_ui\Tests\FieldUiTestTrait;
6
7 /**
8  * Tests paragraphs behavior plugins.
9  *
10  * @group paragraphs
11  */
12 class ParagraphsExperimentalBehaviorsTest extends ParagraphsExperimentalTestBase {
13
14   use FieldUiTestTrait;
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['image', 'file', 'views'];
22
23   /**
24    * Tests the behavior plugins for paragraphs.
25    */
26   public function testBehaviorPluginsFields() {
27     $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
28     $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
29
30     // Add a Paragraph type.
31     $paragraph_type = 'text_paragraph';
32     $this->addParagraphsType($paragraph_type);
33
34     // Add a text field to the text_paragraph type.
35     static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
36
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');
40
41     $this->assertText('Behavior plugins are only supported by the EXPERIMENTAL paragraphs widget');
42     // Enable the test plugins, with an invalid configuration value.
43     $edit = [
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',
47     ];
48     $this->drupalPostForm(NULL, $edit, t('Save'));
49     $this->assertText('Red can not be used as the default color.');
50
51     // Ensure the form can be saved with an invalid configuration value when
52     // the plugin is not selected.
53     $edit = [
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',
57     ];
58     $this->drupalPostForm(NULL, $edit, t('Save'));
59     $this->assertText('Saved the text_paragraph Paragraphs type.');
60
61     // Ensure it can be saved with a valid value and that the defaults are
62     // correct.
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');
67
68     $edit = [
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',
72     ];
73     $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
74     $this->assertText('Saved the text_paragraph Paragraphs type.');
75
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';
81     $edit = [
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,
85     ];
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.
90     $plugin_text = 'red';
91     $edit = [
92       'field_paragraphs[0][behavior_plugins][test_text_color][text_color]' => $plugin_text,
93     ];
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');
97
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);
101
102     // Update the value of both plugins.
103     $updated_text = 'blue';
104     $edit = [
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,
107     ];
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);
115
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'));
133
134     // Test a plugin without behavior fields.
135     $edit = [
136       'behavior_plugins[test_dummy_behavior][enabled]' => TRUE,
137       'behavior_plugins[test_text_color][enabled]' => TRUE,
138     ];
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');
141     $edit = [
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',
144     ];
145     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
146     $this->assertRaw('dummy_plugin_text');
147   }
148
149   /**
150    * Tests the behavior plugins summary for paragraphs closed mode.
151    */
152   public function testCollapsedSummary() {
153     $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
154     $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
155
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.
162     $edit = [
163       'behavior_plugins[test_bold_text][enabled]' => TRUE,
164       'behavior_plugins[test_text_color][enabled]' => TRUE,
165     ];
166     $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
167
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.
173     $edit = [
174       'behavior_plugins[test_bold_text][enabled]' => TRUE,
175     ];
176     $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
177
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');
181     $edit = [
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,
187     ];
188     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
189
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');
194   }
195
196   /**
197    * Tests the behavior plugins subform state submit.
198    */
199   public function testBehaviorSubform() {
200     $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
201     $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
202
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.
208     $edit = [
209       'behavior_plugins[test_bold_text][enabled]' => TRUE,
210       'behavior_plugins[test_text_color][enabled]' => TRUE,
211     ];
212     $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
213
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',
220     ], []);
221     // Enable plugins for the nested paragraph type.
222     $edit = [
223       'behavior_plugins[test_bold_text][enabled]' => TRUE,
224     ];
225     $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
226
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');
231     $edit = [
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,
237     ];
238     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
239
240     $this->clickLink('Edit');
241     $edit = [
242       'field_paragraphs[0][_weight]' => 1,
243       'field_paragraphs[1][_weight]' => 0,
244     ];
245     $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
246     $this->assertNoErrorsLogged();
247
248   }
249 }