Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / paragraphs / tests / src / Functional / ParagraphsExperimentalWidgetButtonsTest.php
1 <?php
2
3 namespace Drupal\Tests\paragraphs\Functional;
4
5 use Drupal\paragraphs\Tests\Classic\ParagraphsCoreVersionUiTestTrait;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Tests\paragraphs\FunctionalJavascript\LoginAdminTrait;
8 use Drupal\Tests\paragraphs\FunctionalJavascript\ParagraphsTestBaseTrait;
9
10 /**
11  * Tests paragraphs experimental widget buttons.
12  *
13  * @group paragraphs
14  */
15 class ParagraphsExperimentalWidgetButtonsTest extends BrowserTestBase {
16
17   use LoginAdminTrait;
18   use ParagraphsCoreVersionUiTestTrait;
19   use ParagraphsTestBaseTrait;
20
21   /**
22    * Modules to enable.
23    *
24    * @var string[]
25    */
26   public static $modules = [
27     'paragraphs_test',
28     'node',
29     'paragraphs',
30     'field',
31     'field_ui',
32     'block',
33   ];
34
35   /**
36    * Tests the autocollapse functionality.
37    */
38   public function testAutocollapse() {
39     $this->addParagraphedContentType('paragraphed_test');
40
41     $permissions = [
42       'administer content types',
43       'administer node fields',
44       'administer paragraphs types',
45       'administer node form display',
46       'administer paragraph fields',
47       'administer paragraph form display',
48       'create paragraphed_test content',
49       'edit any paragraphed_test content',
50     ];
51     $this->loginAsAdmin($permissions, TRUE);
52
53     // Add a text Paragraph type.
54     $this->addParagraphsType('text_paragraph');
55     $this->addFieldtoParagraphType('text_paragraph', 'field_text', 'text_long');
56
57     // Add another Paragraph type so that there is no default Paragraphs type.
58     $this->addParagraphsType('another_paragraph');
59
60     // Check that the paragraphs field uses the experimental widget.
61     $this->drupalGet('admin/structure/types/manage/paragraphed_test/form-display');
62     $option = $this->assertSession()->optionExists('fields[field_paragraphs][type]', 'paragraphs');
63     $this->assertTrue($option->isSelected());
64     // Check that the autocollapse is disabled by default.
65     $this->assertSession()->pageTextContains('Autocollapse: None');
66
67     // Create a new node with 2 paragraphs.
68     $this->drupalGet('node/add/paragraphed_test');
69     $this->getSession()->getPage()->findButton('field_paragraphs_text_paragraph_add_more')->press();
70     $this->getSession()->getPage()->findButton('field_paragraphs_text_paragraph_add_more')->press();
71     $edit = [
72       'title[0][value]' => 'Autocollapse test node',
73       'field_paragraphs[0][subform][field_text][0][value]' => 'Fist paragraph',
74       'field_paragraphs[1][subform][field_text][0][value]' => 'Second paragraph',
75     ];
76     $this->drupalPostForm(NULL, $edit, t('Save'));
77     $node = $this->drupalGetNodeByTitle('Autocollapse test node');
78
79     // Set the settings to "Open" edit mode without autocollapse.
80     $settings = [
81       'edit_mode' => 'open',
82       'closed_mode' => 'summary',
83       'autocollapse' => 'none',
84     ];
85     $this->setParagraphsWidgetSettings('paragraphed_test', 'field_paragraphs', $settings);
86
87     // Edit the node. Edit mode is "Open". All paragraphs are in the "Edit"
88     // mode.
89     $this->drupalGet('node/' . $node->id() . '/edit');
90     $this->checkParagraphInMode('field_paragraphs_0', 'edit');
91     $this->checkParagraphInMode('field_paragraphs_1', 'edit');
92
93     // Autocollapse is disabled. Closing and opening a paragraphs does not
94     // affect the other one.
95     $this->getSession()->getPage()->findButton('field_paragraphs_0_collapse')->press();
96     $this->checkParagraphInMode('field_paragraphs_0', 'closed');
97     $this->checkParagraphInMode('field_paragraphs_1', 'edit');
98
99     $this->getSession()->getPage()->findButton('field_paragraphs_0_edit')->press();
100     $this->checkParagraphInMode('field_paragraphs_0', 'edit');
101     $this->checkParagraphInMode('field_paragraphs_1', 'edit');
102
103     // "Collapse all" enables autocollapse.
104     $this->getSession()->getPage()->findButton('field_paragraphs_collapse_all')->press();
105     $this->checkParagraphInMode('field_paragraphs_0', 'closed');
106     $this->checkParagraphInMode('field_paragraphs_1', 'closed');
107
108     // Open the first paragraph and then the second. Opening the second closes
109     // the first.
110     $this->getSession()->getPage()->findButton('field_paragraphs_0_edit')->press();
111     $this->checkParagraphInMode('field_paragraphs_0', 'edit');
112     $this->checkParagraphInMode('field_paragraphs_1', 'closed');
113
114     $this->getSession()->getPage()->findButton('field_paragraphs_1_edit')->press();
115     $this->checkParagraphInMode('field_paragraphs_0', 'closed');
116     $this->checkParagraphInMode('field_paragraphs_1', 'edit');
117
118     // "Edit all" disables autocollapse.
119     $this->getSession()->getPage()->findButton('field_paragraphs_edit_all')->press();
120     $this->checkParagraphInMode('field_paragraphs_0', 'edit');
121     $this->checkParagraphInMode('field_paragraphs_1', 'edit');
122
123     // Closing and opening a paragraphs does not affect the other one anymore.
124     $this->getSession()->getPage()->findButton('field_paragraphs_0_collapse')->press();
125     $this->checkParagraphInMode('field_paragraphs_0', 'closed');
126     $this->checkParagraphInMode('field_paragraphs_1', 'edit');
127
128     $this->getSession()->getPage()->findButton('field_paragraphs_0_edit')->press();
129     $this->checkParagraphInMode('field_paragraphs_0', 'edit');
130     $this->checkParagraphInMode('field_paragraphs_1', 'edit');
131
132     // Enable autocollapse. Set edit mode to "Closed".
133     $settings = [
134       'edit_mode' => 'closed',
135       'closed_mode' => 'summary',
136       'autocollapse' => 'all',
137     ];
138     $this->setParagraphsWidgetSettings('paragraphed_test', 'field_paragraphs', $settings);
139
140     // Edit the node. All paragraphs are closed.
141     $this->drupalGet('node/' . $node->id() . '/edit');
142     $this->checkParagraphInMode('field_paragraphs_0', 'closed');
143     $this->checkParagraphInMode('field_paragraphs_1', 'closed');
144
145     // Open the first paragraph and then the second. Opening the second closes
146     // the first.
147     $this->getSession()->getPage()->findButton('field_paragraphs_0_edit')->press();
148     $this->checkParagraphInMode('field_paragraphs_0', 'edit');
149     $this->checkParagraphInMode('field_paragraphs_1', 'closed');
150
151     $this->getSession()->getPage()->findButton('field_paragraphs_1_edit')->press();
152     $this->checkParagraphInMode('field_paragraphs_0', 'closed');
153     $this->checkParagraphInMode('field_paragraphs_1', 'edit');
154
155     // "Edit all" disables auto collapse.
156     $this->getSession()->getPage()->findButton('field_paragraphs_edit_all')->press();
157     $this->checkParagraphInMode('field_paragraphs_0', 'edit');
158     $this->checkParagraphInMode('field_paragraphs_1', 'edit');
159
160     // Closing and opening a paragraphs does not affect the other one anymore.
161     $this->getSession()->getPage()->findButton('field_paragraphs_0_collapse')->press();
162     $this->checkParagraphInMode('field_paragraphs_0', 'closed');
163     $this->checkParagraphInMode('field_paragraphs_1', 'edit');
164
165     $this->getSession()->getPage()->findButton('field_paragraphs_0_edit')->press();
166     $this->checkParagraphInMode('field_paragraphs_0', 'edit');
167     $this->checkParagraphInMode('field_paragraphs_1', 'edit');
168
169     // "Collapse all" re-enables autocollapse.
170     $this->getSession()->getPage()->findButton('field_paragraphs_collapse_all')->press();
171     $this->checkParagraphInMode('field_paragraphs_0', 'closed');
172     $this->checkParagraphInMode('field_paragraphs_1', 'closed');
173
174     // Open the first paragraph and then the second. Opening the second closes
175     // the first.
176     $this->getSession()->getPage()->findButton('field_paragraphs_0_edit')->press();
177     $this->checkParagraphInMode('field_paragraphs_0', 'edit');
178     $this->checkParagraphInMode('field_paragraphs_1', 'closed');
179
180     $this->getSession()->getPage()->findButton('field_paragraphs_1_edit')->press();
181     $this->checkParagraphInMode('field_paragraphs_0', 'closed');
182     $this->checkParagraphInMode('field_paragraphs_1', 'edit');
183
184     // Check that adding a new paragraphs closes the others.
185     $this->getSession()->getPage()->findButton('field_paragraphs_text_paragraph_add_more')->press();
186     $this->getSession()->getPage()->fillField('field_paragraphs[2][subform][field_text][0][value]', 'Third paragraph');
187     $this->checkParagraphInMode('field_paragraphs_0', 'closed');
188     $this->checkParagraphInMode('field_paragraphs_1', 'closed');
189     $this->checkParagraphInMode('field_paragraphs_2', 'edit');
190
191     // Check that duplicating closes the other paragraphs.
192     $this->getSession()->getPage()->findButton('field_paragraphs_2_duplicate')->press();
193     $this->getSession()->getPage()->fillField('field_paragraphs[3][subform][field_text][0][value]', 'Fourth paragraph');
194     $this->checkParagraphInMode('field_paragraphs_0', 'closed');
195     $this->checkParagraphInMode('field_paragraphs_1', 'closed');
196     $this->checkParagraphInMode('field_paragraphs_2', 'closed');
197     $this->checkParagraphInMode('field_paragraphs_3', 'edit');
198
199     // Check that autocollapse does not restore removed paragraphs.
200     $this->getSession()->getPage()->findButton('field_paragraphs_3_remove')->press();
201     $this->checkParagraphInMode('field_paragraphs_3', 'removed');
202     $this->getSession()->getPage()->findButton('field_paragraphs_2_edit')->press();
203     $this->checkParagraphInMode('field_paragraphs_3', 'removed');
204   }
205
206   /**
207    * Asserts that a paragraph is in a particular mode.
208    *
209    * It does this indirectly by checking checking what buttons are available.
210    *
211    * @param string $button_prefix
212    *   An initial part of the button name; namely "<paragraphs_field>_<delta>".
213    *
214    * @param string $mode
215    *   Assert that the paragraphs is in this widget item mode. Supported modes
216    *   are "edit", "closed" and "removed". A paragraph in the "removed" mode
217    *   cannot be distinguished from one that has never been added.
218    */
219   public function checkParagraphInMode($button_prefix, $mode) {
220     switch ($mode) {
221       case 'edit':
222         $this->assertSession()->buttonNotExists($button_prefix . '_edit');
223         $this->assertSession()->buttonExists($button_prefix . '_collapse');
224         break;
225       case 'closed':
226         $this->assertSession()->buttonExists($button_prefix . '_edit');
227         $this->assertSession()->buttonNotExists($button_prefix . '_collapse');
228         break;
229       case 'removed':
230         $this->assertSession()->buttonNotExists($button_prefix . '_edit');
231         $this->assertSession()->buttonNotExists($button_prefix . '_collapse');
232         break;
233       default:
234         throw new \InvalidArgumentException('This function does not support "' . $mode . '" as an argument for "$mode" parameter');
235     }
236   }
237
238 }