Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / paragraphs / src / Tests / Experimental / ParagraphsExperimentalHeaderActionsTest.php
1 <?php
2
3 namespace Drupal\paragraphs\Tests\Experimental;
4
5 use Symfony\Component\CssSelector\CssSelectorConverter;
6
7 /**
8  * Tests collapse all button.
9  *
10  * @group paragraphs
11  */
12 class ParagraphsExperimentalHeaderActionsTest extends ParagraphsExperimentalTestBase {
13
14   /**
15    * Tests header actions.
16    */
17   public function testHeaderActions() {
18
19     // To test with a single header action, ensure the drag and drop action is
20     // shown, even without the library.
21     \Drupal::state()->set('paragraphs_test_dragdrop_force_show', TRUE);
22
23     $this->addParagraphedContentType('paragraphed_test');
24
25     $this->loginAsAdmin([
26       'create paragraphed_test content',
27       'edit any paragraphed_test content',
28     ]);
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(
36       'admin/structure/paragraphs_type/' . $paragraph_type,
37       'text',
38       'Text',
39       'text_long',
40       [],
41       []
42     );
43
44     // Add 2 paragraphs and check for Collapse/Edit all button.
45     $this->drupalGet('node/add/paragraphed_test');
46     $this->assertNoRaw('field_paragraphs_collapse_all');
47     $this->assertNoRaw('field_paragraphs_edit_all');
48     $this->assertRaw('field_paragraphs_dragdrop_mode');
49
50     // Ensure there is only a single table row.
51     $table_rows = $this->xpath('//table[contains(@class, :class)]/tbody/tr', [':class' => 'field-multiple-table']);
52     $this->assertEqual(1, count($table_rows));
53
54     // Add second paragraph and check for Collapse/Edit all button.
55     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_paragraph_add_more');
56     $this->assertRaw('field_paragraphs_collapse_all');
57     $this->assertRaw('field_paragraphs_edit_all');
58
59     $edit = [
60       'field_paragraphs[0][subform][field_text][0][value]' => 'First text',
61       'field_paragraphs[1][subform][field_text][0][value]' => 'Second text',
62     ];
63     $this->drupalPostForm(NULL, $edit, 'Collapse all');
64
65     // Checks that after collapsing all we can edit again these paragraphs.
66     $this->assertRaw('field_paragraphs_0_edit');
67     $this->assertRaw('field_paragraphs_1_edit');
68
69     // Test Edit all button.
70     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_edit_all');
71     $this->assertRaw('field_paragraphs_0_collapse');
72     $this->assertRaw('field_paragraphs_1_collapse');
73
74     $edit = [
75       'title[0][value]' => 'Test',
76     ];
77     $this->drupalPostForm(NULL, $edit, t('Save'));
78     $this->assertText('paragraphed_test Test has been created.');
79
80     $node = $this->getNodeByTitle('Test');
81     $this->drupalGet('node/' . $node->id());
82     $this->clickLink('Edit');
83     $this->assertNoText('No Paragraph added yet.');
84
85     // Add and remove another paragraph.
86     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_paragraph_add_more');
87     $edit = [
88       'field_paragraphs[2][subform][field_text][0][value]' => 'Third text',
89     ];
90     $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_2_remove');
91
92     // Check that pressing "Collapse all" does not restore the removed
93     // paragraph.
94     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_edit_all');
95     $this->assertText('First text');
96     $this->assertText('Second text');
97     $this->assertNoText('Third text');
98
99     // Check that pressing "Edit all" does not restore the removed paragraph,
100     // either.
101     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_collapse_all');
102     $this->assertText('First text');
103     $this->assertText('Second text');
104     $this->assertNoText('Third text');
105   }
106
107   /**
108    * Tests that header actions works fine with nesting.
109    */
110   public function testHeaderActionsWithNesting() {
111     $this->addParagraphedContentType('paragraphed_test');
112
113     $this->loginAsAdmin([
114       'create paragraphed_test content',
115       'edit any paragraphed_test content',
116     ]);
117
118     // Add Paragraph types.
119     $nested_paragraph_type = 'nested_paragraph';
120     $this->addParagraphsType($nested_paragraph_type);
121     $paragraph_type = 'text';
122     $this->addParagraphsType($paragraph_type);
123
124     // Add a text field to the text_paragraph type.
125     static::fieldUIAddNewField(
126       'admin/structure/paragraphs_type/' . $paragraph_type,
127       'text',
128       'Text',
129       'text_long',
130       [],
131       []
132     );
133
134     // Add a ERR paragraph field to the nested_paragraph type.
135     static::fieldUIAddNewField(
136       'admin/structure/paragraphs_type/' . $nested_paragraph_type,
137       'nested',
138       'Nested',
139       'field_ui:entity_reference_revisions:paragraph', [
140         'settings[target_type]' => 'paragraph',
141         'cardinality' => '-1',
142       ],
143       []
144     );
145
146     // Checks that Collapse/Edit all button is presented.
147     $this->drupalGet('node/add/paragraphed_test');
148     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_nested_paragraph_add_more');
149     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_add_more');
150     $this->assertRaw('field_paragraphs_collapse_all');
151     $this->assertRaw('field_paragraphs_edit_all');
152
153     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_add_more');
154     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_subform_field_nested_text_add_more');
155     $this->assertNoRaw('field_paragraphs_0_collapse_all');
156     $this->assertNoRaw('field_paragraphs_0_edit_all');
157     $edit = [
158       'field_paragraphs[0][subform][field_nested][0][subform][field_text][0][value]' => 'Nested text',
159       'field_paragraphs[1][subform][field_text][0][value]' => 'Second text paragraph',
160     ];
161     $this->drupalPostForm(NULL, $edit, 'Collapse all');
162     $this->assertRaw('field-paragraphs-0-edit');
163     $this->assertFieldByXPath((new CssSelectorConverter())->toXPath('[name="field_paragraphs_1_edit"] + .paragraphs-dropdown'));
164     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_edit_all');
165     $this->assertRaw('field-paragraphs-0-collapse');
166
167     $edit = [
168       'title[0][value]' => 'Test',
169     ];
170     $this->drupalPostForm(NULL, $edit, t('Save'));
171     $this->assertText('paragraphed_test Test has been created.');
172
173     $node = $this->getNodeByTitle('Test');
174     $this->drupalGet('node/' . $node->id());
175     $this->clickLink('Edit');
176     $this->assertNoText('No Paragraph added yet.');
177   }
178
179   /**
180    * Tests header actions with multi fields.
181    */
182   public function testHeaderActionsWithMultiFields() {
183     $this->addParagraphedContentType('paragraphed_test');
184     $this->loginAsAdmin([
185       'create paragraphed_test content',
186       'edit any paragraphed_test content',
187     ]);
188     $this->drupalGet('/admin/structure/types/manage/paragraphed_test/fields/add-field');
189
190     // Add a Paragraph type.
191     $paragraph_type = 'text_paragraph';
192     $this->addParagraphsType($paragraph_type);
193     $edit = [
194       'new_storage_type' => 'field_ui:entity_reference_revisions:paragraph',
195       'label' => 'Second paragraph',
196       'field_name' => 'second',
197     ];
198     $this->drupalPostForm(NULL, $edit, 'Save and continue');
199     $this->drupalPostForm(NULL, [], 'Save field settings');
200     $this->drupalPostForm(NULL, [], 'Save settings');
201
202     $this->drupalGet('/admin/structure/types/manage/paragraphed_test/form-display');
203     $edit = [
204       'fields[field_second][type]' => 'paragraphs',
205     ];
206     $this->drupalPostForm(NULL, $edit, 'Save');
207
208     // Add a text field to the text_paragraph type.
209     static::fieldUIAddNewField(
210       'admin/structure/paragraphs_type/' . $paragraph_type,
211       'text',
212       'Text',
213       'text_long',
214       [],
215       []
216     );
217
218     $this->drupalGet('node/add/paragraphed_test');
219     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_paragraph_add_more');
220     $this->drupalPostAjaxForm(NULL, [], 'field_second_text_paragraph_add_more');
221
222     // Checks that we have Collapse\Edit all for each field.
223     $this->assertRaw('field_paragraphs_collapse_all');
224     $this->assertRaw('field_paragraphs_edit_all');
225     $this->assertRaw('field_second_collapse_all');
226     $this->assertRaw('field_second_edit_all');
227
228     $edit = [
229       'field_second[0][subform][field_text][0][value]' => 'Second field',
230     ];
231     $this->drupalPostAjaxForm(NULL, $edit, 'field_second_collapse_all');
232
233     // Checks that we collapsed only children from second field.
234     $this->assertNoRaw('field_paragraphs_0_edit');
235     $this->assertRaw('field_second_0_edit');
236
237     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_collapse_all');
238     $this->assertRaw('field_paragraphs_0_edit');
239     $this->assertRaw('field_second_0_edit');
240
241     $this->drupalPostAjaxForm(NULL, [], 'field_second_edit_all');
242     $this->assertRaw('field_second_0_collapse');
243
244     $edit = [
245       'title[0][value]' => 'Test',
246     ];
247     $this->drupalPostForm(NULL, $edit, t('Save'));
248     $this->assertText('paragraphed_test Test has been created.');
249
250     $node = $this->getNodeByTitle('Test');
251     $this->drupalGet('node/' . $node->id());
252     $this->clickLink('Edit');
253     $this->assertNoText('No Paragraph added yet.');
254   }
255
256 }