3 namespace Drupal\paragraphs\Tests\Experimental;
5 use Drupal\field_ui\Tests\FieldUiTestTrait;
6 use Drupal\paragraphs\Entity\Paragraph;
7 use Drupal\Tests\paragraphs\FunctionalJavascript\ParagraphsTestBaseTrait;
10 * Tests the configuration of paragraphs.
14 class ParagraphsExperimentalAdministrationTest extends ParagraphsExperimentalTestBase {
17 use ParagraphsTestBaseTrait;
24 public static $modules = array(
33 protected function setUp() {
35 // Create paragraphs content type.
36 $this->drupalCreateContentType(array('type' => 'paragraphs', 'name' => 'Paragraphs'));
40 * Tests the revision of paragraphs.
42 public function testParagraphsRevisions() {
43 $this->addParagraphedContentType('article', 'paragraphs');
45 'create paragraphs content',
46 'administer node display',
47 'edit any paragraphs content',
51 // Create paragraphs type Headline + Block.
52 $this->addParagraphsType('text');
53 // Create field types for the text.
54 static::fieldUIAddNewField('admin/structure/paragraphs_type/text', 'text', 'Text', 'text', array(), array());
55 $this->assertText('Saved Text configuration.');
57 // Create an article with paragraphs field.
58 static::fieldUIAddNewField('admin/structure/types/manage/paragraphs', 'paragraphs', 'Paragraphs', 'entity_reference_revisions', array(
59 'settings[target_type]' => 'paragraph',
60 'cardinality' => '-1',
62 'settings[handler_settings][target_bundles_drag_drop][text][enabled]' => TRUE,
64 // Configure article fields.
65 $this->drupalGet('admin/structure/types/manage/paragraphs/fields');
66 $this->clickLink(t('Manage form display'));
67 $this->drupalPostForm(NULL, array('fields[field_paragraphs][type]' => 'paragraphs'), t('Save'));
69 // Create node with our paragraphs.
70 $this->drupalGet('node/add/paragraphs');
71 $this->drupalPostAjaxForm(NULL, array(), 'field_paragraphs_text_add_more');
72 $this->drupalPostAjaxForm(NULL, array(), 'field_paragraphs_text_add_more');
74 'title[0][value]' => 'TEST TITEL',
75 'field_paragraphs[0][subform][field_text][0][value]' => 'Test text 1',
76 'field_paragraphs[1][subform][field_text][0][value]' => 'Test text 2',
78 $this->drupalPostForm(NULL, $edit + ['status[value]' => TRUE], t('Save'));
80 $node = $this->drupalGetNodeByTitle('TEST TITEL');
81 $paragraph1 = $node->field_paragraphs[0]->target_id;
82 $paragraph2 = $node->field_paragraphs[1]->target_id;
84 $this->countRevisions($node, $paragraph1, $paragraph2, 1);
86 // Edit the node without creating a revision. There should still be only 1
87 // revision for nodes and paragraphs.
89 'field_paragraphs[0][subform][field_text][0][value]' => 'Foo Bar 1',
92 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
94 $this->countRevisions($node, $paragraph1, $paragraph2, 1);
96 // Edit the just created node. Create new revision. Now we should have 2
97 // revisions for nodes and paragraphs.
99 'title[0][value]' => 'TEST TITLE',
100 'field_paragraphs[0][subform][field_text][0][value]' => 'Foo Bar 2',
103 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
105 $this->countRevisions($node, $paragraph1, $paragraph2, 2);
107 // Assert the paragraphs have been changed.
108 $this->assertNoText('Foo Bar 1');
109 $this->assertText('Test text 2');
110 $this->assertText('Foo Bar 2');
111 $this->assertText('TEST TITLE');
113 // Check out the revisions page and assert there are 2 revisions.
114 $this->drupalGet('node/' . $node->id() . '/revisions');
115 $rows = $this->xpath('//tbody/tr');
116 // Make sure two revisions available.
117 $this->assertEqual(count($rows), 2);
118 // Revert to the old version.
119 $this->clickLink(t('Revert'));
120 $this->drupalPostForm(NULL, [], t('Revert'));
121 $this->drupalGet('node/' . $node->id());
122 // Assert the node has been reverted.
123 $this->assertNoText('Foo Bar 2');
124 $this->assertText('Test text 2');
125 $this->assertText('Foo Bar 1');
126 $this->assertText('TEST TITEL');
131 * Tests the paragraph creation.
133 public function testParagraphsCreation() {
134 // Create an article with paragraphs field.
135 $this->addParagraphedContentType('article');
136 $this->loginAsAdmin([
137 'administer site configuration',
138 'create article content',
139 'create paragraphs content',
140 'administer node display',
141 'administer paragraph display',
142 'edit any article content',
143 'delete any article content',
144 'access files overview',
147 // Assert suggested 'Add a paragraph type' link when there is no type yet.
148 $this->drupalGet('admin/structure/paragraphs_type');
149 $this->assertText('There is no Paragraphs type yet.');
150 $this->drupalGet('admin/structure/types/manage/paragraphs/fields/add-field');
152 'new_storage_type' => 'field_ui:entity_reference_revisions:paragraph',
153 'label' => 'Paragraph',
154 'field_name' => 'paragraph',
156 $this->drupalPostForm(NULL, $edit, 'Save and continue');
157 $this->drupalPostForm(NULL, [], 'Save field settings');
158 $this->assertLinkByHref('admin/structure/paragraphs_type/add');
159 $this->clickLink('here');
160 $this->assertUrl('admin/structure/paragraphs_type/add');
162 $this->drupalGet('admin/structure/paragraphs_type');
163 $this->clickLink(t('Add paragraph type'));
164 $this->assertTitle('Add Paragraphs type | Drupal');
165 // Create paragraph type text + image.
166 $this->addParagraphsType('text_image');
167 $this->drupalGet('admin/structure/paragraphs_type/text_image');
168 $this->assertTitle('Edit text_image paragraph type | Drupal');
169 // Create field types for text and image.
170 static::fieldUIAddNewField('admin/structure/paragraphs_type/text_image', 'text', 'Text', 'text_long', array(), array());
171 $this->assertText('Saved Text configuration.');
172 static::fieldUIAddNewField('admin/structure/paragraphs_type/text_image', 'image', 'Image', 'image', array(), array('settings[alt_field_required]' => FALSE));
173 $this->assertText('Saved Image configuration.');
175 // Create paragraph type Nested test.
176 $this->addParagraphsType('nested_test');
178 static::fieldUIAddNewField('admin/structure/paragraphs_type/nested_test', 'paragraphs', 'Paragraphs', 'entity_reference_revisions', array(
179 'settings[target_type]' => 'paragraph',
180 'cardinality' => '-1',
183 // Change the add more button to select mode.
184 $this->clickLink(t('Manage form display'));
185 $this->drupalPostAjaxForm(NULL, ['fields[field_paragraphs][type]' => 'paragraphs'], 'field_paragraphs_settings_edit');
186 $this->drupalPostForm(NULL, ['fields[field_paragraphs][settings_edit_form][settings][add_mode]' => 'select'], t('Update'));
187 $this->drupalPostForm(NULL, [], t('Save'));
189 // Create paragraph type image.
190 $this->addParagraphsType('image');
191 // Create field types for image.
192 static::fieldUIAddNewField('admin/structure/paragraphs_type/image', 'image_only', 'Image only', 'image', array(), array());
193 $this->assertText('Saved Image only configuration.');
195 $this->drupalGet('admin/structure/paragraphs_type');
196 $rows = $this->xpath('//tbody/tr');
197 // Make sure 2 types are available with their label.
198 $this->assertEqual(count($rows), 3);
199 $this->assertText('text_image');
200 $this->assertText('image');
201 // Make sure there is an edit link for each type.
202 $this->clickLink(t('Edit'));
203 // Make sure the field UI appears.
204 $this->assertLink('Manage fields');
205 $this->assertLink('Manage form display');
206 $this->assertLink('Manage display');
207 $this->assertTitle('Edit image paragraph type | Drupal');
209 // Test for "Add mode" setting.
210 $this->drupalGet('admin/structure/types/manage/article/form-display');
211 $field_name = 'field_paragraphs';
213 // Click on the widget settings button to open the widget settings form.
214 $this->drupalPostAjaxForm(NULL, ['fields[field_paragraphs][type]' => 'paragraphs'], $field_name . "_settings_edit");
217 $edit = array('fields[' . $field_name . '][settings_edit_form][settings][add_mode]' => 'button');
218 $this->drupalPostForm(NULL, $edit, t('Save'));
220 // Check if the setting is stored.
221 $this->drupalGet('admin/structure/types/manage/article/form-display');
222 $this->assertText('Add mode: Buttons', 'Checking the settings value.');
224 $this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");
225 // Assert the 'Buttons' option is selected.
226 $this->assertOptionSelected('edit-fields-field-paragraphs-settings-edit-form-settings-add-mode', 'button', 'Updated value is correct!.');
228 // Add two Text + Image paragraphs in article.
229 $this->drupalGet('node/add/article');
230 $this->drupalPostAjaxForm(NULL, array(), 'field_paragraphs_text_image_add_more');
231 $this->drupalPostAjaxForm(NULL, array(), 'field_paragraphs_text_image_add_more');
232 // Create an 'image' file, upload it.
233 $text = 'Trust me I\'m an image';
234 file_put_contents('temporary://myImage1.jpg', $text);
235 file_put_contents('temporary://myImage2.jpg', $text);
238 'title[0][value]' => 'Test article',
239 'field_paragraphs[0][subform][field_text][0][value]' => 'Test text 1',
240 'files[field_paragraphs_0_subform_field_image_0]' => drupal_realpath('temporary://myImage1.jpg'),
241 'field_paragraphs[1][subform][field_text][0][value]' => 'Test text 2',
242 'files[field_paragraphs_1_subform_field_image_0]' => drupal_realpath('temporary://myImage2.jpg'),
244 $this->drupalPostForm(NULL, $edit, t('Save'));
246 $node = $this->drupalGetNodeByTitle('Test article');
247 $img1_url = file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/myImage1.jpg'));
248 $img2_url = file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/myImage2.jpg'));
250 // Check the text and image after publish.
251 $this->assertText('Test text 1');
252 $this->assertRaw('<img src="' . file_url_transform_relative($img1_url));
253 $this->assertText('Test text 2');
254 $this->assertRaw('<img src="' . file_url_transform_relative($img2_url));
256 // Tests for "Edit mode" settings.
257 // Test for closed setting.
258 $this->drupalGet('admin/structure/types/manage/article/form-display');
259 // Click on the widget settings button to open the widget settings form.
260 $this->drupalPostAjaxForm(NULL, array(), "field_paragraphs_settings_edit");
262 $edit = array('fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'closed');
263 $this->drupalPostForm(NULL, $edit, t('Save'));
264 // Check if the setting is stored.
265 $this->assertText('Edit mode: Closed', 'Checking the settings value.');
266 $this->drupalPostAjaxForm(NULL, array(), "field_paragraphs_settings_edit");
267 // Assert the 'Closed' option is selected.
268 $this->assertOptionSelected('edit-fields-field-paragraphs-settings-edit-form-settings-edit-mode', 'closed', 'Updated value correctly.');
269 $this->drupalGet('node/1/edit');
270 // The textareas for paragraphs should not be visible.
271 $this->assertNoRaw('field_paragraphs[0][subform][field_text][0][value]');
272 $this->assertNoRaw('field_paragraphs[1][subform][field_text][0][value]');
273 $this->assertRaw('<div class="paragraphs-collapsed-description">myImage1.jpg, Test text 1');
274 $this->assertRaw('<div class="paragraphs-collapsed-description">myImage2.jpg, Test text 2');
276 // Test for preview option.
277 $this->drupalGet('admin/structure/types/manage/article/form-display');
278 $this->drupalPostAjaxForm(NULL, array(), "field_paragraphs_settings_edit");
280 'fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'closed',
281 'fields[field_paragraphs][settings_edit_form][settings][closed_mode]' => 'preview',
283 $this->drupalPostForm(NULL, $edit, t('Save'));
284 $this->assertText('Edit mode: Closed', 'Checking the "Edit mode" setting value.');
285 $this->assertText('Closed mode: Preview', 'Checking the "Closed mode" settings value.');
286 $this->drupalGet('node/1/edit');
287 // The texts in the paragraphs should be visible.
288 $this->assertNoRaw('field_paragraphs[0][subform][field_text][0][value]');
289 $this->assertNoRaw('field_paragraphs[1][subform][field_text][0][value]');
290 $this->assertText('Test text 1');
291 $this->assertText('Test text 2');
293 // Test for open option.
294 $this->drupalGet('admin/structure/types/manage/article/form-display');
295 $this->drupalPostAjaxForm(NULL, array(), "field_paragraphs_settings_edit");
296 // Assert the "Closed" and "Preview" options are selected.
297 $this->assertOptionSelected('edit-fields-field-paragraphs-settings-edit-form-settings-edit-mode', 'closed', 'Correctly updated the "Edit mode" value.');
298 $this->assertOptionSelected('edit-fields-field-paragraphs-settings-edit-form-settings-closed-mode', 'preview', 'Correctly updated the "Closed mode" value.');
299 // Restore the value to Open for next test.
300 $edit = array('fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'open');
301 $this->drupalPostForm(NULL, $edit, t('Save'));
302 $this->drupalGet('node/1/edit');
303 // The textareas for paragraphs should be visible.
304 $this->assertRaw('field_paragraphs[0][subform][field_text][0][value]');
305 $this->assertRaw('field_paragraphs[1][subform][field_text][0][value]');
307 $paragraphs = Paragraph::loadMultiple();
308 $this->assertEqual(count($paragraphs), 2, 'Two paragraphs in article');
310 // Check article edit page.
311 $this->drupalGet('node/' . $node->id() . '/edit');
312 // Check both paragraphs in edit page.
313 $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'Test text 1');
314 $this->assertRaw('<a href="' . $img1_url . '" type="image/jpeg; length=21">myImage1.jpg</a>');
315 $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', 'Test text 2');
316 $this->assertRaw('<a href="' . $img2_url . '" type="image/jpeg; length=21">myImage2.jpg</a>');
317 // Remove 2nd paragraph.
318 $this->drupalPostForm(NULL, NULL, t('Remove'));
319 $this->assertNoField('field_paragraphs[1][subform][field_text][0][value]');
320 $this->assertNoRaw('<a href="' . $img2_url . '" type="image/jpeg; length=21">myImage2.jpg</a>');
321 // Assert the paragraph is not deleted unless the user saves the node.
322 $this->drupalGet('node/' . $node->id() . '/edit');
323 $this->assertRaw('<a href="' . $img2_url . '" type="image/jpeg; length=21">myImage2.jpg</a>');
324 // Remove the second paragraph.
325 $this->drupalPostForm(NULL, [], t('Remove'));
326 $this->assertNoRaw('<a href="' . $img2_url . '" type="image/jpeg; length=21">myImage2.jpg</a>');
328 'field_paragraphs[0][subform][field_image][0][alt]' => 'test_alt',
329 'field_paragraphs[0][subform][field_image][0][width]' => 300,
330 'field_paragraphs[0][subform][field_image][0][height]' => 300,
332 $this->drupalPostForm(NULL, $edit, t('Save'));
333 // Assert the paragraph is deleted after the user saves the node.
334 $this->drupalGet('node/' . $node->id() . '/edit');
335 $this->assertNoRaw('<a href="' . $img2_url . '" type="image/jpeg; length=21">myImage2.jpg</a>');
338 $this->clickLink(t('Delete'));
339 $this->drupalPostForm(NULL, NULL, t('Delete'));
340 $this->assertText('Test article has been deleted.');
342 // Check if the publish/unpublish option works.
343 $this->drupalGet('admin/structure/paragraphs_type/text_image/form-display');
345 'fields[status][type]' => 'boolean_checkbox',
346 'fields[status][region]' => 'content',
349 $this->drupalPostForm(NULL, $edit, t('Save'));
350 $this->drupalGet('node/add/article');
351 $this->drupalPostForm(NULL, NULL, t('Add text_image'));
352 $this->assertRaw('edit-field-paragraphs-0-subform-status-value');
354 'title[0][value]' => 'Example publish/unpublish',
355 'field_paragraphs[0][subform][field_text][0][value]' => 'Example published and unpublished',
357 $this->drupalPostForm(NULL, $edit, t('Save'));
358 $this->assertText(t('Example published and unpublished'));
359 $this->clickLink(t('Edit'));
361 'field_paragraphs[0][subform][status][value]' => FALSE,
363 $this->drupalPostForm(NULL, $edit, t('Save'));
364 $this->assertNoText(t('Example published and unpublished'));
366 // Set the fields as required.
367 $this->drupalGet('admin/structure/types/manage/article/fields');
368 $this->clickLink('Edit', 1);
369 $this->drupalPostForm(NULL, ['preview_mode' => '1'], t('Save content type'));
370 $this->drupalGet('admin/structure/paragraphs_type/nested_test/fields');
371 $this->clickLink('Edit');
372 $this->drupalPostForm(NULL, ['required' => TRUE], t('Save settings'));
374 // Add a new article.
375 $this->drupalGet('node/add/article');
376 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_nested_test_add_more');
378 // Ensure that nested header actions do not add a visible weight field.
379 $this->assertNoFieldByName('field_paragraphs[0][subform][field_paragraphs][header_actions][_weight]');
382 'field_paragraphs[0][subform][field_paragraphs][add_more][add_more_select]' => 'image',
384 $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_0_subform_field_paragraphs_add_more');
385 // Test the new field is displayed.
386 $this->assertFieldByName('files[field_paragraphs_0_subform_field_paragraphs_0_subform_field_image_only_0]');
388 // Add an image to the required field.
390 'title[0][value]' => 'test required',
391 'files[field_paragraphs_0_subform_field_paragraphs_0_subform_field_image_only_0]' => drupal_realpath('temporary://myImage2.jpg'),
393 $this->drupalPostForm(NULL, $edit, t('Save'));
395 'field_paragraphs[0][subform][field_paragraphs][0][subform][field_image_only][0][width]' => 100,
396 'field_paragraphs[0][subform][field_paragraphs][0][subform][field_image_only][0][height]' => 100,
397 'field_paragraphs[0][subform][field_paragraphs][0][subform][field_image_only][0][alt]' => 'Alternative_text',
399 $this->drupalPostForm(NULL, $edit, t('Save'));
400 $this->assertText('test required has been created.');
401 $this->assertNoRaw('This value should not be null.');
403 // Test that unsupported widgets are not displayed.
404 $this->drupalGet('admin/structure/types/manage/article/form-display');
405 $select = $this->xpath('//*[@id="edit-fields-field-paragraphs-type"]')[0];
406 $this->assertEqual(count($select->option), 2);
407 $this->assertRaw('value="paragraphs" selected="selected"');
409 // Check that Paragraphs is not displayed as an entity_reference field
411 $this->drupalGet('admin/structure/types/manage/article/fields/add-field');
413 'new_storage_type' => 'entity_reference',
414 'label' => 'unsupported field',
415 'field_name' => 'unsupportedfield',
417 $this->drupalPostForm(NULL, $edit, t('Save and continue'));
418 $this->assertNoOption('edit-settings-target-type', 'paragraph');
420 // Test that all Paragraph types can be referenced if none is selected.
421 $this->addParagraphsType('nested_double_test');
422 static::fieldUIAddExistingField('admin/structure/paragraphs_type/nested_double_test', 'field_paragraphs', 'paragraphs_1');
423 $this->clickLink(t('Manage form display'));
424 $this->drupalPostForm(NULL, [], 'Save');
425 //$this->drupalPostForm(NULL, array('fields[field_paragraphs][type]' => 'entity_reference_revisions_entity_view'), t('Save'));
426 static::fieldUIAddNewField('admin/structure/paragraphs_type/nested_double_test', 'paragraphs_2', 'paragraphs_2', 'entity_reference_revisions', array(
427 'settings[target_type]' => 'paragraph',
428 'cardinality' => '-1',
430 $this->clickLink(t('Manage form display'));
431 $this->drupalPostForm(NULL, [], 'Save');
432 $this->drupalPostAjaxForm('node/add/article', [], 'field_paragraphs_nested_test_add_more');
434 'field_paragraphs[0][subform][field_paragraphs][add_more][add_more_select]' => 'nested_double_test',
436 $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_0_subform_field_paragraphs_add_more');
437 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_subform_field_paragraphs_0_subform_field_paragraphs_image_add_more');
439 'title[0][value]' => 'Nested twins',
441 $this->drupalPostForm(NULL, $edit, t('Save'));
442 $this->assertText('Nested twins has been created.');
443 $this->assertNoText('This entity (paragraph: ) cannot be referenced.');
445 // Set the fields as not required.
446 $this->drupalGet('admin/structure/types/manage/article/fields');
447 $this->clickLink('Edit', 1);
448 $this->drupalPostForm(NULL, ['required' => FALSE], t('Save settings'));
450 // Set the Paragraph field edit mode to "Closed" and the closed mode to
453 'edit_mode' => 'closed',
454 'closed_mode' => 'summary',
456 $this->setParagraphsWidgetSettings('article', 'field_paragraphs', $settings);
458 $this->addParagraphsType('node_test');
460 // Add a required node reference field.
461 static::fieldUIAddNewField('admin/structure/paragraphs_type/node_test', 'entity_reference', 'Entity reference', 'entity_reference', array(
462 'settings[target_type]' => 'node',
463 'cardinality' => '-1'
465 'settings[handler_settings][target_bundles][article]' => TRUE,
468 $node = $this->drupalGetNodeByTitle('Nested twins');
470 // Create a node with a reference in a Paragraph.
471 $this->drupalPostAjaxForm('node/add/article', [], 'field_paragraphs_node_test_add_more');
472 \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
474 'field_paragraphs[0][subform][field_entity_reference][0][target_id]' => $node->label() . ' (' . $node->id() . ')',
475 'title[0][value]' => 'choke test',
477 $this->drupalPostForm(NULL, $edit, t('Save'));
478 // Delete the referenced node.
480 // Edit the node with the reference.
481 $this->clickLink(t('Edit'));
482 // Since we have validation error (reference to deleted node), paragraph is
483 // by default in edit mode.
484 $this->assertFieldByName('field_paragraphs[0][subform][field_entity_reference][0][target_id]');
485 $this->assertFieldByName('field_paragraphs[0][subform][field_entity_reference][1][target_id]');
486 // Assert the validation error message.
487 $this->assertText('The referenced entity (node: 4) does not exist');
488 // Triggering unrelated button, assert that error message is still present.
489 $this->drupalPostForm(NULL, [], t('Add another item'));
490 $this->assertText('The referenced entity (node: 4) does not exist');
491 $this->assertText('Entity reference (value 1) field is required.');
492 // Try to collapse with an invalid reference.
493 $this->drupalPostAjaxForm(NULL, ['field_paragraphs[0][subform][field_entity_reference][0][target_id]' => 'foo'], 'field_paragraphs_0_collapse');
494 // Paragraph should be still in edit mode.
495 $this->assertFieldByName('field_paragraphs[0][subform][field_entity_reference][0][target_id]');
496 $this->assertFieldByName('field_paragraphs[0][subform][field_entity_reference][1][target_id]');
497 $this->drupalPostForm(NULL, [], t('Add another item'));
498 // Assert the validation message.
499 $this->assertText('There are no entities matching "foo".');
500 // Fix the broken reference.
501 $node = $this->drupalGetNodeByTitle('Example publish/unpublish');
502 $edit = ['field_paragraphs[0][subform][field_entity_reference][0][target_id]' => $node->label() . ' (' . $node->id() . ')'];
503 $this->drupalPostForm(NULL, $edit, t('Save'));
504 $this->assertText('choke test has been updated.');
505 $this->assertLink('Example publish/unpublish');
506 // Delete the new referenced node.
509 // Set the Paragraph field closed mode to "Preview".
511 'edit_mode' => 'closed',
512 'closed_mode' => 'preview',
514 $this->setParagraphsWidgetSettings('article', 'field_paragraphs', $settings);
516 $node = $this->drupalGetNodeByTitle('choke test');
517 // Attempt to edit the Paragraph.
518 $this->drupalGet('node/' . $node->id() . '/edit');
519 // Since we have another validation error, the paragraph is by default in
520 // the edit mode again.
521 $this->assertFieldByName('field_paragraphs[0][subform][field_entity_reference][0][target_id]');
522 $this->assertFieldByName('field_paragraphs[0][subform][field_entity_reference][1][target_id]');
523 // Try to save with and invalid reference.
524 $edit = ['field_paragraphs[0][subform][field_entity_reference][0][target_id]' => 'foo'];
525 $this->drupalPostForm(NULL, $edit, t('Save'));
526 $this->assertText('There are no entities matching "foo".');
527 // Remove the Paragraph and save the node.
528 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_remove');
529 $this->drupalPostForm(NULL, [], t('Save'));
530 $this->assertText('choke test has been updated.');
532 // Verify that the text displayed is correct when no paragraph has been
534 $this->drupalGet('node/add/article');
535 $this->assertText('No Paragraph added yet.');
537 $this->drupalGet('admin/content/files');
538 $this->clickLink('1 place');
539 $label = $this->xpath('//tbody/tr/td[1]');
540 $this->assertEqual(trim(htmlspecialchars_decode(strip_tags($label[0]->asXML()))), 'test required > field_paragraphs > Paragraphs');
544 * Asserts that a select option in the current page is checked.
547 * ID of select field to assert.
548 * @param string $option
550 * @param string $message
551 * (optional) A message to display with the assertion. Do not translate
552 * messages: use format_string() to embed variables in the message text, not
553 * t(). If left blank, a default message will be displayed.
554 * @param string $group
555 * (optional) The group this message is in, which is displayed in a column
556 * in test output. Use 'Debug' to indicate this is debugging output. Do not
557 * translate this string. Defaults to 'Browser'; most tests do not override
561 * TRUE on pass, FALSE on fail.
563 * @todo Remove function once core issue is resolved: https://www.drupal.org/node/2530092
565 protected function assertOptionSelected($id, $option, $message = '', $group = 'Browser') {
566 $elements = $this->xpath('//select[contains(@id, :id)]//option[@value=:option]', array(':id' => $id, ':option' => $option));
567 return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['selected']), $message ? $message : SafeMarkup::format('Option @option for field @id is selected.', array('@option' => $option, '@id' => $id)), $group);
571 * Helper function for revision counting.
573 private function countRevisions($node, $paragraph1, $paragraph2, $revisions_count) {
574 $node_revisions_count = \Drupal::entityQuery('node')->condition('nid', $node->id())->allRevisions()->count()->execute();
575 $this->assertEqual($node_revisions_count, $revisions_count);
576 $this->assertEqual(\Drupal::entityQuery('paragraph')->condition('id', $paragraph1)->allRevisions()->count()->execute(), $revisions_count);
577 $this->assertEqual(\Drupal::entityQuery('paragraph')->condition('id', $paragraph2)->allRevisions()->count()->execute(), $revisions_count);