Upgraded imagemagick and manually altered pdf to image module to handle changes....
[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 use Drupal\paragraphs\Entity\Paragraph;
7
8 /**
9  * Tests paragraphs behavior plugins.
10  *
11  * @group paragraphs
12  */
13 class ParagraphsExperimentalBehaviorsTest extends ParagraphsExperimentalTestBase {
14
15   use FieldUiTestTrait;
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['image', 'file', 'views'];
23
24   /**
25    * Tests the behavior plugins for paragraphs.
26    */
27   public function testBehaviorPluginsFields() {
28     $this->addParagraphedContentType('paragraphed_test');
29     $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
30
31     // Add a Paragraph type.
32     $paragraph_type = 'text_paragraph';
33     $this->addParagraphsType($paragraph_type);
34
35     // Add a text field to the text_paragraph type.
36     static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
37
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');
41
42     $this->assertText('Behavior plugins are only supported by the EXPERIMENTAL paragraphs widget');
43     // Enable the test plugins, with an invalid configuration value.
44     $edit = [
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',
48     ];
49     $this->drupalPostForm(NULL, $edit, t('Save'));
50     $this->assertText('Red can not be used as the default color.');
51
52     // Ensure the form can be saved with an invalid configuration value when
53     // the plugin is not selected.
54     $edit = [
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',
58     ];
59     $this->drupalPostForm(NULL, $edit, t('Save'));
60     $this->assertText('Saved the text_paragraph Paragraphs type.');
61
62     // Ensure it can be saved with a valid value and that the defaults are
63     // correct.
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');
68
69     $edit = [
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',
73     ];
74     $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
75     $this->assertText('Saved the text_paragraph Paragraphs type.');
76
77     $this->drupalGet('node/add/paragraphed_test');
78
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');
82
83     $this->loginAsAdmin([
84       'create paragraphed_test content',
85       'edit any paragraphed_test content',
86       'edit behavior plugin settings',
87     ]);
88
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';
94     $edit = [
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,
98     ];
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');
102
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';
108     $edit = [
109       'field_paragraphs[0][behavior_plugins][test_text_color][text_color]' => $plugin_text,
110     ];
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');
114
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);
118
119     // Update the value of both plugins.
120     $updated_text = 'blue';
121     $edit = [
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,
124     ];
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);
132
133     $this->loginAsAdmin([
134       'edit any paragraphed_test content',
135     ]);
136
137     $node = $this->getNodeByTitle('paragraphs_plugins_test');
138     $this->drupalGet('node/' . $node->id() . '/edit');
139
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);
142
143     $this->drupalPostForm(NULL, [], t('Save'));
144
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
147     // behaviors.
148     $this->assertRaw('class="blue_plugin_text bold_plugin_text');
149     $this->assertNoRaw('class="red_plugin_text');
150
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'));
168
169     // Test a plugin without behavior fields.
170     $edit = [
171       'behavior_plugins[test_dummy_behavior][enabled]' => TRUE,
172       'behavior_plugins[test_text_color][enabled]' => TRUE,
173     ];
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');
176     $edit = [
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',
179     ];
180     $this->drupalPostForm(NULL, $edit, t('Save'));
181     $this->assertRaw('dummy_plugin_text');
182
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'));
186
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'));
191
192     $this->assertResponse(200);
193   }
194
195   /**
196    * Tests the behavior plugins summary for paragraphs closed mode.
197    */
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',
204     ]);
205
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.
212     $edit = [
213       'behavior_plugins[test_bold_text][enabled]' => TRUE,
214       'behavior_plugins[test_text_color][enabled]' => TRUE,
215     ];
216     $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
217
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.
223     $edit = [
224       'behavior_plugins[test_bold_text][enabled]' => TRUE,
225     ];
226     $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
227
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');
231
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]');
234
235     $edit = [
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,
241     ];
242     $this->drupalPostForm(NULL, $edit, t('Save'));
243
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');
248
249     // Add an empty nested paragraph.
250     $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_nested_paragraph_add_more');
251     $edit = [
252       'title[0][value]' => 'collapsed_test',
253     ];
254     $this->drupalPostForm(NULL, $edit, t('Save'));
255
256     // Check an empty nested paragraph summary.
257     $this->clickLink('Edit');
258     $this->assertRaw('class="paragraphs-collapsed-description">');
259
260   }
261
262   /**
263    * Tests the behavior plugins subform state submit.
264    */
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',
271     ]);
272
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.
278     $edit = [
279       'behavior_plugins[test_bold_text][enabled]' => TRUE,
280       'behavior_plugins[test_text_color][enabled]' => TRUE,
281     ];
282     $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
283
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',
290     ], []);
291     // Enable plugins for the nested paragraph type.
292     $edit = [
293       'behavior_plugins[test_bold_text][enabled]' => TRUE,
294     ];
295     $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
296
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');
301     $edit = [
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,
307     ];
308     $this->drupalPostForm(NULL, $edit, t('Save'));
309
310     $this->clickLink('Edit');
311     $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,
316     ];
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');
321
322   }
323 }