3 namespace Drupal\paragraphs\Tests\Classic;
5 use Drupal\field_ui\Tests\FieldUiTestTrait;
6 use Drupal\paragraphs\Entity\ParagraphsType;
9 * Tests paragraphs add modes.
13 class ParagraphsAddModesTest extends ParagraphsTestBase {
18 * Tests that paragraphs field does not allow default values.
20 public function testNoDefaultValue() {
21 $this->loginAsAdmin();
22 $this->addParagraphedContentType('paragraphed_test', 'paragraphs_field', 'entity_reference_paragraphs');
25 $this->drupalGet('admin/structure/types/manage/paragraphed_test/fields');
26 $this->clickLink(t('Edit'));
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);
36 * Tests the field creation when no Paragraphs types are available.
38 public function testEmptyAllowedTypes() {
39 $this->loginAsAdmin();
40 $this->addParagraphedContentType('paragraphed_test', 'paragraphs', 'entity_reference_paragraphs');
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.');
50 * Tests the add drop down button.
52 public function testDropDownMode() {
53 $this->loginAsAdmin();
54 // Add two Paragraph types.
55 $this->addParagraphsType('btext');
56 $this->addParagraphsType('dtext');
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');
63 $this->setAddMode('paragraphed_test', 'paragraphs', 'dropdown');
65 $this->assertAddButtons(['Add btext', 'Add dtext']);
67 $this->addParagraphsType('atext');
68 $this->assertAddButtons(['Add btext', 'Add dtext', 'Add atext']);
70 $this->setParagraphsTypeWeight('paragraphed_test', 'dtext', 2, 'paragraphs');
71 $this->assertAddButtons(['Add dtext', 'Add btext', 'Add atext']);
73 $this->setAllowedParagraphsTypes('paragraphed_test', ['dtext', 'atext'], TRUE, 'paragraphs');
74 $this->assertAddButtons(['Add dtext', 'Add atext']);
76 $this->setParagraphsTypeWeight('paragraphed_test', 'atext', 1, 'paragraphs');
77 $this->assertAddButtons(['Add atext', 'Add dtext']);
79 $this->setAllowedParagraphsTypes('paragraphed_test', ['atext', 'dtext', 'btext'], TRUE, 'paragraphs');
80 $this->assertAddButtons(['Add atext', 'Add dtext', 'Add btext']);
84 * Tests the add select mode.
86 public function testSelectMode() {
87 $this->loginAsAdmin();
88 // Add two Paragraph types.
89 $this->addParagraphsType('btext');
90 $this->addParagraphsType('dtext');
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');
97 $this->setAddMode('paragraphed_test', 'paragraphs', 'select');
99 $this->assertSelectOptions(['btext', 'dtext'], 'paragraphs');
101 $this->addParagraphsType('atext');
102 $this->assertSelectOptions(['btext', 'dtext', 'atext'], 'paragraphs');
104 $this->setParagraphsTypeWeight('paragraphed_test', 'dtext', 2, 'paragraphs');
105 $this->assertSelectOptions(['dtext', 'btext', 'atext'], 'paragraphs');
107 $this->setAllowedParagraphsTypes('paragraphed_test', ['dtext', 'atext'], TRUE, 'paragraphs');
108 $this->assertSelectOptions(['dtext', 'atext'], 'paragraphs');
110 $this->setParagraphsTypeWeight('paragraphed_test', 'atext', 1, 'paragraphs');
111 $this->assertSelectOptions(['atext', 'dtext'], 'paragraphs');
113 $this->setAllowedParagraphsTypes('paragraphed_test', ['atext', 'dtext', 'btext'], TRUE, 'paragraphs');
114 $this->assertSelectOptions(['atext', 'dtext', 'btext'], 'paragraphs');
118 * Asserts order and quantity of add buttons.
120 * @param array $options
121 * Array of expected add buttons in its correct order.
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]);
130 $this->assertTrue(count($buttons) == count($options), 'The amount of drop down options matches with the given array');
134 * Asserts order and quantity of select add options.
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.
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]);
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');
153 * Tests if setting for default paragraph type is working properly.
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'
163 // Add a Paragraphed test content.
164 $paragraphs_type_text_image = ParagraphsType::create([
165 'id' => 'text_image',
166 'label' => 'Text + Image',
168 $paragraphs_type_text = ParagraphsType::create([
172 $paragraphs_type_text_image->save();
173 $paragraphs_type_text->save();
175 $this->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', 'text_image');
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');
182 // Disable text_image as default paragraph type.
183 $this->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', '_none');
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.');
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.');
199 * Tests the default paragraph type behavior for a field with a single type.
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'
209 // Add a Paragraphed test content.
210 $paragraphs_type_text = ParagraphsType::create([
214 $paragraphs_type_text->save();
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.');
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.');