Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / paragraphs / src / Tests / Experimental / ParagraphsExperimentalAddModesTest.php
1 <?php
2
3 namespace Drupal\paragraphs\Tests\Experimental;
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 ParagraphsExperimentalAddModesTest extends ParagraphsExperimentalTestBase {
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');
23     // Edit the field.
24     $this->drupalGet('admin/structure/types/manage/paragraphed_test/fields');
25     $this->clickLink(t('Edit'));
26
27     // Check that the current field does not allow to add default values.
28     $this->assertText('No widget available for: field_paragraphs.');
29     $this->drupalPostForm(NULL, [], t('Save settings'));
30     $this->assertText('Saved field_paragraphs configuration.');
31     $this->assertResponse(200);
32   }
33
34   /**
35    * Tests the field creation when no Paragraphs types are available.
36    */
37   public function testEmptyAllowedTypes() {
38     $this->loginAsAdmin();
39     $this->addParagraphedContentType('paragraphed_test');
40
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 field_paragraphs configuration.');
46   }
47
48   /**
49    * Tests the add drop down button.
50    */
51   public function testDropDownMode() {
52     $this->loginAsAdmin();
53     // Add two Paragraph types.
54     $this->addParagraphsType('btext');
55     $this->addParagraphsType('dtext');
56
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');
61
62     $this->setAddMode('paragraphed_test', 'paragraphs', 'dropdown');
63
64     $this->assertAddButtons(['Add btext', 'Add dtext']);
65
66     $this->addParagraphsType('atext');
67     $this->assertAddButtons(['Add btext', 'Add dtext', 'Add atext']);
68
69     $this->setParagraphsTypeWeight('paragraphed_test', 'dtext', 2, 'paragraphs');
70     $this->assertAddButtons(['Add dtext', 'Add btext', 'Add atext']);
71
72     $this->setAllowedParagraphsTypes('paragraphed_test', ['dtext', 'atext'], TRUE, 'paragraphs');
73     $this->assertAddButtons(['Add dtext', 'Add atext']);
74
75     $this->setParagraphsTypeWeight('paragraphed_test', 'atext', 1, 'paragraphs');
76     $this->assertAddButtons(['Add atext', 'Add dtext']);
77
78     $this->setAllowedParagraphsTypes('paragraphed_test', ['atext', 'dtext', 'btext'], TRUE, 'paragraphs');
79     $this->assertAddButtons(['Add atext', 'Add dtext', 'Add btext']);
80   }
81
82   /**
83    * Tests the add select mode.
84    */
85   public function testSelectMode() {
86     $this->loginAsAdmin();
87     // Add two Paragraph types.
88     $this->addParagraphsType('btext');
89     $this->addParagraphsType('dtext');
90
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');
95
96     $this->setAddMode('paragraphed_test', 'paragraphs', 'select');
97
98     $this->assertSelectOptions(['btext', 'dtext'], 'paragraphs');
99
100     $this->addParagraphsType('atext');
101     $this->assertSelectOptions(['btext', 'dtext', 'atext'], 'paragraphs');
102
103     $this->setParagraphsTypeWeight('paragraphed_test', 'dtext', 2, 'paragraphs');
104     $this->assertSelectOptions(['dtext', 'btext', 'atext'], 'paragraphs');
105
106     $this->setAllowedParagraphsTypes('paragraphed_test', ['dtext', 'atext'], TRUE, 'paragraphs');
107     $this->assertSelectOptions(['dtext', 'atext'], 'paragraphs');
108
109     $this->setParagraphsTypeWeight('paragraphed_test', 'atext', 1, 'paragraphs');
110     $this->assertSelectOptions(['atext', 'dtext'], 'paragraphs');
111
112     $this->setAllowedParagraphsTypes('paragraphed_test', ['atext', 'dtext', 'btext'], TRUE, 'paragraphs');
113     $this->assertSelectOptions(['atext', 'dtext', 'btext'], 'paragraphs');
114   }
115
116   /**
117    * Asserts order and quantity of add buttons.
118    *
119    * @param array $options
120    *   Array of expected add buttons in its correct order.
121    */
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]);
128     }
129     $this->assertTrue(count($buttons) == count($options), 'The amount of drop down options matches with the given array');
130   }
131
132   /**
133    * Asserts order and quantity of select add options.
134    *
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.
139    */
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]);
146     }
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');
149   }
150
151   /**
152    * Tests if setting for default paragraph type is working properly.
153    */
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'
160     ]);
161
162     // Add a Paragraphed test content.
163     $paragraphs_type_text_image = ParagraphsType::create([
164       'id' => 'text_image',
165       'label' => 'Text + Image',
166     ]);
167     $paragraphs_type_text = ParagraphsType::create([
168       'id' => 'text',
169       'label' => 'Text',
170     ]);
171     $paragraphs_type_text_image->save();
172     $paragraphs_type_text->save();
173
174     $this->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', 'text_image');
175
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');
180
181     // Disable text_image as default paragraph type.
182     $this->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', '_none');
183
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.');
187
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     $edit = ['title[0][value]' => 'New Host'];
192     $this->drupalPostForm(NULL, $edit, t('Save'));
193     $this->drupalGet('node/1/edit');
194     $this->assertText('No Paragraph added yet.');
195   }
196
197   /**
198    * Tests the default paragraph type behavior for a field with a single type.
199    */
200   public function testDefaultParagraphTypeWithSingleType() {
201     $this->addParagraphedContentType('paragraphed_test', 'paragraphs');
202     $this->loginAsAdmin([
203       'administer content types',
204       'administer node form display',
205       'edit any paragraphed_test content'
206     ]);
207
208     // Add a Paragraphed test content.
209     $paragraphs_type_text = ParagraphsType::create([
210       'id' => 'text',
211       'label' => 'Text',
212     ]);
213     $paragraphs_type_text->save();
214
215     // Check that when only one paragraph type is allowed in a content type,
216     // one instance is automatically added in the 'Add content' dialogue.
217     $this->drupalGet('node/add/paragraphed_test');
218     $this->assertNoText('No Paragraph added yet.');
219
220     // Check that no paragraph type is automatically added, if the defaut
221     // setting was set to '- None -'.
222     $this->setDefaultParagraphType('paragraphed_test', 'paragraphs', 'paragraphs_settings_edit', '_none');
223     $this->drupalGet('node/add/paragraphed_test');
224     $this->assertText('No Paragraph added yet.');
225   }
226 }