Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / paragraphs / src / Tests / Classic / ParagraphsAddModesTest.php
1 <?php
2
3 namespace Drupal\paragraphs\Tests\Classic;
4
5 use Drupal\field_ui\Tests\FieldUiTestTrait;
6 use Drupal\paragraphs\Entity\ParagraphsType;
7
8 /**
9  * Tests paragraphs add modes.
10  *
11  * @group paragraphs
12  */
13 class ParagraphsAddModesTest extends ParagraphsTestBase {
14
15   use FieldUiTestTrait;
16
17   /**
18    * Tests that paragraphs field does not allow default values.
19    */
20   public function testNoDefaultValue() {
21     $this->loginAsAdmin();
22     $this->addParagraphedContentType('paragraphed_test', 'paragraphs_field', 'entity_reference_paragraphs');
23
24     // Edit the field.
25     $this->drupalGet('admin/structure/types/manage/paragraphed_test/fields');
26     $this->clickLink(t('Edit'));
27
28     // Check that the current field does not allow to add default values.
29     $this->assertText('No widget available for: paragraphs_field.');
30     $this->drupalPostForm(NULL, [], t('Save settings'));
31     $this->assertText('Saved paragraphs_field configuration.');
32     $this->assertResponse(200);
33   }
34
35   /**
36    * Tests the field creation when no Paragraphs types are available.
37    */
38   public function testEmptyAllowedTypes() {
39     $this->loginAsAdmin();
40     $this->addParagraphedContentType('paragraphed_test', 'paragraphs', 'entity_reference_paragraphs');
41
42     // Edit the field and save when there are no Paragraphs types available.
43     $this->drupalGet('admin/structure/types/manage/paragraphed_test/fields');
44     $this->clickLink(t('Edit'));
45     $this->drupalPostForm(NULL, [], t('Save settings'));
46     $this->assertText('Saved paragraphs configuration.');
47   }
48
49   /**
50    * Tests the add drop down button.
51    */
52   public function testDropDownMode() {
53     $this->loginAsAdmin();
54     // Add two Paragraph types.
55     $this->addParagraphsType('btext');
56     $this->addParagraphsType('dtext');
57
58     $this->addParagraphedContentType('paragraphed_test', 'paragraphs', 'entity_reference_paragraphs');
59     // Enter to the field config since the weight is set through the form.
60     $this->drupalGet('admin/structure/types/manage/paragraphed_test/fields/node.paragraphed_test.paragraphs');
61     $this->drupalPostForm(NULL, [], 'Save settings');
62
63     $this->setAddMode('paragraphed_test', 'paragraphs', 'dropdown');
64
65     $this->assertAddButtons(['Add btext', 'Add dtext']);
66
67     $this->addParagraphsType('atext');
68     $this->assertAddButtons(['Add btext', 'Add dtext', 'Add atext']);
69
70     $this->setParagraphsTypeWeight('paragraphed_test', 'dtext', 2, 'paragraphs');
71     $this->assertAddButtons(['Add dtext', 'Add btext', 'Add atext']);
72
73     $this->setAllowedParagraphsTypes('paragraphed_test', ['dtext', 'atext'], TRUE, 'paragraphs');
74     $this->assertAddButtons(['Add dtext', 'Add atext']);
75
76     $this->setParagraphsTypeWeight('paragraphed_test', 'atext', 1, 'paragraphs');
77     $this->assertAddButtons(['Add atext', 'Add dtext']);
78
79     $this->setAllowedParagraphsTypes('paragraphed_test', ['atext', 'dtext', 'btext'], TRUE, 'paragraphs');
80     $this->assertAddButtons(['Add atext', 'Add dtext', 'Add btext']);
81   }
82
83   /**
84    * Tests the add select mode.
85    */
86   public function testSelectMode() {
87     $this->loginAsAdmin();
88     // Add two Paragraph types.
89     $this->addParagraphsType('btext');
90     $this->addParagraphsType('dtext');
91
92     $this->addParagraphedContentType('paragraphed_test', 'paragraphs', 'entity_reference_paragraphs');
93     // Enter to the field config since the weight is set through the form.
94     $this->drupalGet('admin/structure/types/manage/paragraphed_test/fields/node.paragraphed_test.paragraphs');
95     $this->drupalPostForm(NULL, [], 'Save settings');
96
97     $this->setAddMode('paragraphed_test', 'paragraphs', 'select');
98
99     $this->assertSelectOptions(['btext', 'dtext'], 'paragraphs');
100
101     $this->addParagraphsType('atext');
102     $this->assertSelectOptions(['btext', 'dtext', 'atext'], 'paragraphs');
103
104     $this->setParagraphsTypeWeight('paragraphed_test', 'dtext', 2, 'paragraphs');
105     $this->assertSelectOptions(['dtext', 'btext', 'atext'], 'paragraphs');
106
107     $this->setAllowedParagraphsTypes('paragraphed_test', ['dtext', 'atext'], TRUE, 'paragraphs');
108     $this->assertSelectOptions(['dtext', 'atext'], 'paragraphs');
109
110     $this->setParagraphsTypeWeight('paragraphed_test', 'atext', 1, 'paragraphs');
111     $this->assertSelectOptions(['atext', 'dtext'], 'paragraphs');
112
113     $this->setAllowedParagraphsTypes('paragraphed_test', ['atext', 'dtext', 'btext'], TRUE, 'paragraphs');
114     $this->assertSelectOptions(['atext', 'dtext', 'btext'], 'paragraphs');
115   }
116
117   /**
118    * Asserts order and quantity of add buttons.
119    *
120    * @param array $options
121    *   Array of expected add buttons in its correct order.
122    */
123   protected function assertAddButtons($options) {
124     $this->drupalGet('node/add/paragraphed_test');
125     $buttons = $this->xpath('//input[@class="field-add-more-submit button js-form-submit form-submit"]');
126     // Check if the buttons are in the same order as the given array.
127     foreach ($buttons as $key => $button) {
128       $this->assertEqual($button['value'], $options[$key]);
129     }
130     $this->assertTrue(count($buttons) == count($options), 'The amount of drop down options matches with the given array');
131   }
132
133   /**
134    * Asserts order and quantity of select add options.
135    *
136    * @param array $options
137    *   Array of expected select options in its correct order.
138    * @param string $paragraphs_field
139    *   Name of the paragraphs field to check.
140    */
141   protected function assertSelectOptions($options, $paragraphs_field) {
142     $this->drupalGet('node/add/paragraphed_test');
143     $buttons = $this->xpath('//*[@name="' . $paragraphs_field . '[add_more][add_more_select]"]/option');
144     // Check if the options are in the same order as the given array.
145     foreach ($buttons as $key => $button) {
146       $this->assertEqual($button['value'], $options[$key]);
147     }
148     $this->assertTrue(count($buttons) == count($options), 'The amount of select options matches with the given array');
149     $this->assertNotEqual($this->xpath('//*[@name="' . $paragraphs_field .'_add_more"]'), [], 'The add button is displayed');
150   }
151
152   /**
153    * Tests if setting for default paragraph type is working properly.
154    */
155   public function testSettingDefaultParagraphType() {
156     $this->addParagraphedContentType('paragraphed_test', 'paragraphs', 'entity_reference_paragraphs');
157     $this->loginAsAdmin([
158       'administer content types',
159       'administer node form display',
160       'edit any paragraphed_test content'
161     ]);
162
163     // Add a Paragraphed test content.
164     $paragraphs_type_text_image = ParagraphsType::create([
165       'id' => 'text_image',
166       'label' => 'Text + Image',
167     ]);
168     $paragraphs_type_text = ParagraphsType::create([
169       'id' => 'text',
170       'label' => 'Text',
171     ]);
172     $paragraphs_type_text_image->save();
173     $paragraphs_type_text->save();
174
175     $this->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', 'text_image');
176
177     // Check if default paragraph type is showing.
178     $this->drupalGet('node/add/paragraphed_test');
179     $this->assertText('Text + Image');
180     $this->removeDefaultParagraphType('paragraphed_test');
181
182     // Disable text_image as default paragraph type.
183     $this->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', '_none');
184
185     // Check if is Text + Image is added as default paragraph type.
186     $this->drupalGet('node/add/paragraphed_test');
187     $this->assertText('No Paragraph added yet.');
188
189     // Check if default type is created only for new host
190     $this->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', 'text_image');
191     $this->removeDefaultParagraphType('paragraphed_test');
192     $edit = ['title[0][value]' => 'New Host'];
193     $this->drupalPostForm(NULL, $edit, t('Save'));
194     $this->drupalGet('node/1/edit');
195     $this->assertText('No Paragraph added yet.');
196   }
197
198   /**
199    * Tests the default paragraph type behavior for a field with a single type.
200    */
201   public function testDefaultParagraphTypeWithSingleType() {
202     $this->addParagraphedContentType('paragraphed_test', 'paragraphs', 'entity_reference_paragraphs');
203     $this->loginAsAdmin([
204       'administer content types',
205       'administer node form display',
206       'edit any paragraphed_test content'
207     ]);
208
209     // Add a Paragraphed test content.
210     $paragraphs_type_text = ParagraphsType::create([
211       'id' => 'text',
212       'label' => 'Text',
213     ]);
214     $paragraphs_type_text->save();
215
216     // Check that when only one paragraph type is allowed in a content type,
217     // one instance is automatically added in the 'Add content' dialogue.
218     $this->drupalGet('node/add/paragraphed_test');
219     $this->assertNoText('No Paragraph added yet.');
220
221     // Check that no paragraph type is automatically added, if the defaut
222     // setting was set to '- None -'.
223     $this->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', '_none');
224     $this->drupalGet('node/add/paragraphed_test');
225     $this->assertText('No Paragraph added yet.');
226   }
227 }