3 namespace Drupal\paragraphs\Tests\Experimental;
5 use Drupal\field_ui\Tests\FieldUiTestTrait;
6 use Drupal\paragraphs\Entity\ParagraphsType;
9 * Tests paragraphs add modes.
13 class ParagraphsExperimentalAddModesTest extends ParagraphsExperimentalTestBase {
18 * Tests that paragraphs field does not allow default values.
20 public function testNoDefaultValue() {
21 $this->loginAsAdmin();
22 $this->addParagraphedContentType('paragraphed_test', 'paragraphs_field');
24 $this->drupalGet('admin/structure/types/manage/paragraphed_test/fields');
25 $this->clickLink(t('Edit'));
27 // Check that the current field does not allow to add default values.
28 $this->assertText('No widget available for: paragraphs_field.');
29 $this->drupalPostForm(NULL, [], t('Save settings'));
30 $this->assertText('Saved paragraphs_field configuration.');
31 $this->assertResponse(200);
35 * Tests the field creation when no paragraphs types are available.
37 public function testEmptyAllowedTypes() {
38 $this->loginAsAdmin();
39 $this->addParagraphedContentType('paragraphed_test', 'paragraphs');
41 // Edit the field and save when there are no paragraphs types available.
42 $this->drupalGet('admin/structure/types/manage/paragraphed_test/fields');
43 $this->clickLink(t('Edit'));
44 $this->drupalPostForm(NULL, [], t('Save settings'));
45 $this->assertText('Saved paragraphs configuration.');
49 * Tests the add drop down button.
51 public function testDropDownMode() {
52 $this->loginAsAdmin();
53 // Add two paragraph types.
54 $this->addParagraphsType('btext');
55 $this->addParagraphsType('dtext');
57 $this->addParagraphedContentType('paragraphed_test', 'paragraphs');
58 // Enter to the field config since the weight is set through the form.
59 $this->drupalGet('admin/structure/types/manage/paragraphed_test/fields/node.paragraphed_test.paragraphs');
60 $this->drupalPostForm(NULL, [], 'Save settings');
62 $this->setAddMode('paragraphed_test', 'paragraphs', 'dropdown');
64 $this->assertAddButtons(['Add btext', 'Add dtext']);
66 $this->addParagraphsType('atext');
67 $this->assertAddButtons(['Add btext', 'Add dtext', 'Add atext']);
69 $this->setParagraphsTypeWeight('paragraphed_test', 'dtext', 2, 'paragraphs');
70 $this->assertAddButtons(['Add dtext', 'Add btext', 'Add atext']);
72 $this->setAllowedParagraphsTypes('paragraphed_test', ['dtext', 'atext'], TRUE, 'paragraphs');
73 $this->assertAddButtons(['Add dtext', 'Add atext']);
75 $this->setParagraphsTypeWeight('paragraphed_test', 'atext', 1, 'paragraphs');
76 $this->assertAddButtons(['Add atext', 'Add dtext']);
78 $this->setAllowedParagraphsTypes('paragraphed_test', ['atext', 'dtext', 'btext'], TRUE, 'paragraphs');
79 $this->assertAddButtons(['Add atext', 'Add dtext', 'Add btext']);
83 * Tests the add select mode.
85 public function testSelectMode() {
86 $this->loginAsAdmin();
87 // Add two paragraph types.
88 $this->addParagraphsType('btext');
89 $this->addParagraphsType('dtext');
91 $this->addParagraphedContentType('paragraphed_test', 'paragraphs');
92 // Enter to the field config since the weight is set through the form.
93 $this->drupalGet('admin/structure/types/manage/paragraphed_test/fields/node.paragraphed_test.paragraphs');
94 $this->drupalPostForm(NULL, [], 'Save settings');
96 $this->setAddMode('paragraphed_test', 'paragraphs', 'select');
98 $this->assertSelectOptions(['btext', 'dtext'], 'paragraphs');
100 $this->addParagraphsType('atext');
101 $this->assertSelectOptions(['btext', 'dtext', 'atext'], 'paragraphs');
103 $this->setParagraphsTypeWeight('paragraphed_test', 'dtext', 2, 'paragraphs');
104 $this->assertSelectOptions(['dtext', 'btext', 'atext'], 'paragraphs');
106 $this->setAllowedParagraphsTypes('paragraphed_test', ['dtext', 'atext'], TRUE, 'paragraphs');
107 $this->assertSelectOptions(['dtext', 'atext'], 'paragraphs');
109 $this->setParagraphsTypeWeight('paragraphed_test', 'atext', 1, 'paragraphs');
110 $this->assertSelectOptions(['atext', 'dtext'], 'paragraphs');
112 $this->setAllowedParagraphsTypes('paragraphed_test', ['atext', 'dtext', 'btext'], TRUE, 'paragraphs');
113 $this->assertSelectOptions(['atext', 'dtext', 'btext'], 'paragraphs');
117 * Asserts order and quantity of add buttons.
119 * @param array $options
120 * Array of expected add buttons in its correct order.
122 protected function assertAddButtons($options) {
123 $this->drupalGet('node/add/paragraphed_test');
124 $buttons = $this->xpath('//input[@class="field-add-more-submit button js-form-submit form-submit"]');
125 // Check if the buttons are in the same order as the given array.
126 foreach ($buttons as $key => $button) {
127 $this->assertEqual($button['value'], $options[$key]);
129 $this->assertTrue(count($buttons) == count($options), 'The amount of drop down options matches with the given array');
133 * Asserts order and quantity of select add options.
135 * @param array $options
136 * Array of expected select options in its correct order.
137 * @param string $paragraphs_field
138 * Name of the paragraphs field to check.
140 protected function assertSelectOptions($options, $paragraphs_field) {
141 $this->drupalGet('node/add/paragraphed_test');
142 $buttons = $this->xpath('//*[@name="' . $paragraphs_field . '[add_more][add_more_select]"]/option');
143 // Check if the options are in the same order as the given array.
144 foreach ($buttons as $key => $button) {
145 $this->assertEqual($button['value'], $options[$key]);
147 $this->assertTrue(count($buttons) == count($options), 'The amount of select options matches with the given array');
148 $this->assertNotEqual($this->xpath('//*[@name="' . $paragraphs_field .'_add_more"]'), [], 'The add button is displayed');
152 * Tests if setting for default paragraph type is working properly.
154 public function testSettingDefaultParagraphType() {
155 $this->addParagraphedContentType('paragraphed_test', 'paragraphs');
156 $this->loginAsAdmin([
157 'administer content types',
158 'administer node form display',
159 'edit any paragraphed_test content'
162 // Add a Paragraphed test content.
163 $paragraphs_type_text_image = ParagraphsType::create([
164 'id' => 'text_image',
165 'label' => 'Text + Image',
167 $paragraphs_type_text = ParagraphsType::create([
171 $paragraphs_type_text_image->save();
172 $paragraphs_type_text->save();
174 $this->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', 'text_image');
176 // Check if default paragraph type is showing.
177 $this->drupalGet('node/add/paragraphed_test');
178 $this->assertText('Text + Image');
179 $this->removeDefaultParagraphType('paragraphed_test');
181 // Disable text_image as default paragraph type.
182 $this->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', '_none');
184 // Check if is Text + Image is added as default paragraph type.
185 $this->drupalGet('node/add/paragraphed_test');
186 $this->assertText('No Paragraph added yet.');
188 // Check if default type is created only for new host
189 $this->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', 'text_image');
190 $this->removeDefaultParagraphType('paragraphed_test');
191 $this->drupalPostForm(NULL, ['title[0][value]' => 'New Host'], 'Save and publish');
192 $this->drupalGet('node/1/edit');
193 $this->assertText('No Paragraph added yet.');
197 * Tests the default paragraph type behavior for a field with a single type.
199 public function testDefaultParagraphTypeWithSingleType() {
200 $this->addParagraphedContentType('paragraphed_test', 'paragraphs');
201 $this->loginAsAdmin([
202 'administer content types',
203 'administer node form display',
204 'edit any paragraphed_test content'
207 // Add a Paragraphed test content.
208 $paragraphs_type_text = ParagraphsType::create([
212 $paragraphs_type_text->save();
214 // Check that when only one paragraph type is allowed in a content type,
215 // one instance is automatically added in the 'Add content' dialogue.
216 $this->drupalGet('node/add/paragraphed_test');
217 $this->assertNoText('No Paragraph added yet.');
219 // Check that no paragraph type is automatically added, if the defaut
220 // setting was set to '- None -'.
221 $this->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', '_none');
222 $this->drupalGet('node/add/paragraphed_test');
223 $this->assertText('No Paragraph added yet.');