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