Version 1
[yaffs-website] / web / modules / contrib / paragraphs / src / Tests / Classic / ParagraphsAdministrationTest.php
1 <?php
2
3 namespace Drupal\paragraphs\Tests\Classic;
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 ParagraphsAdministrationTest extends ParagraphsTestBase {
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    * Tests the revision of paragraphs.
38    */
39   public function testParagraphsRevisions() {
40     $this->addParagraphedContentType('article', 'paragraphs');
41     $this->loginAsAdmin([
42       'create paragraphs content',
43       'administer node display',
44       'edit any paragraphs content',
45     ]);
46
47     // Create paragraphs type Headline + Block.
48     $this->addParagraphsType('text');
49     // Create field types for the text.
50     static::fieldUIAddNewField('admin/structure/paragraphs_type/text', 'text', 'Text', 'text', array(), array());
51     $this->assertText('Saved Text configuration.');
52
53     // Create an article with paragraphs field.
54     static::fieldUIAddNewField('admin/structure/types/manage/paragraphs', 'paragraphs', 'Paragraphs', 'entity_reference_revisions', array(
55       'settings[target_type]' => 'paragraph',
56       'cardinality' => '-1',
57     ), array(
58       'settings[handler_settings][target_bundles_drag_drop][text][enabled]' => TRUE,
59     ));
60     // Configure article fields.
61     $this->drupalGet('admin/structure/types/manage/paragraphs/fields');
62     $this->clickLink(t('Manage form display'));
63     $this->drupalPostForm(NULL, array('fields[field_paragraphs][type]' => 'entity_reference_paragraphs'), t('Save'));
64
65     // Create node with our paragraphs.
66     $this->drupalGet('node/add/paragraphs');
67     $this->drupalPostAjaxForm(NULL, array(), 'field_paragraphs_text_add_more');
68     $this->drupalPostAjaxForm(NULL, array(), 'field_paragraphs_text_add_more');
69     $edit = [
70       'title[0][value]' => 'TEST TITEL',
71       'field_paragraphs[0][subform][field_text][0][value]' => 'Test text 1',
72       'field_paragraphs[1][subform][field_text][0][value]' => 'Test text 2',
73     ];
74     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
75
76     $node = $this->drupalGetNodeByTitle('TEST TITEL');
77     $paragraph1 = $node->field_paragraphs[0]->target_id;
78     $paragraph2 = $node->field_paragraphs[1]->target_id;
79
80     $this->countRevisions($node, $paragraph1, $paragraph2, 1);
81
82     // Edit the node without creating a revision. There should still be only 1
83     // revision for nodes and paragraphs.
84     $edit = [
85       'field_paragraphs[0][subform][field_text][0][value]' => 'Foo Bar 1',
86       'revision' => FALSE,
87     ];
88     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
89
90     $this->countRevisions($node, $paragraph1, $paragraph2, 1);
91
92     // Edit the just created node. Create new revision. Now we should have 2
93     // revisions for nodes and paragraphs.
94     $edit = [
95       'title[0][value]' => 'TEST TITLE',
96       'field_paragraphs[0][subform][field_text][0][value]' => 'Foo Bar 2',
97       'revision' => TRUE,
98     ];
99     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
100
101     $this->countRevisions($node, $paragraph1, $paragraph2, 2);
102
103     // Assert the paragraphs have been changed.
104     $this->assertNoText('Foo Bar 1');
105     $this->assertText('Test text 2');
106     $this->assertText('Foo Bar 2');
107     $this->assertText('TEST TITLE');
108
109     // Check out the revisions page and assert there are 2 revisions.
110     $this->drupalGet('node/' . $node->id() . '/revisions');
111     $rows = $this->xpath('//tbody/tr');
112     // Make sure two revisions available.
113     $this->assertEqual(count($rows), 2);
114     // Revert to the old version.
115     $this->clickLink(t('Revert'));
116     $this->drupalPostForm(NULL, [], t('Revert'));
117     $this->drupalGet('node/' . $node->id());
118     // Assert the node has been reverted.
119     $this->assertNoText('Foo Bar 2');
120     $this->assertText('Test text 2');
121     $this->assertText('Foo Bar 1');
122     $this->assertText('TEST TITEL');
123   }
124
125
126   /**
127    * Tests the paragraph creation.
128    */
129   public function testParagraphsCreation() {
130     // Create an article with paragraphs field.
131     $this->addParagraphedContentType('article', 'field_paragraphs');
132     $this->loginAsAdmin([
133       'administer site configuration',
134       'create article content',
135       'create paragraphs content',
136       'administer node display',
137       'administer paragraph display',
138       'edit any article content',
139       'delete any article content',
140       'access files overview',
141     ]);
142
143     // Assert suggested 'Add a paragraph type' link when there is no type yet.
144     $this->drupalGet('admin/structure/paragraphs_type');
145     $this->assertText('There is no Paragraphs type yet.');
146     $this->drupalGet('admin/structure/types/manage/paragraphs/fields/add-field');
147     $edit = [
148       'new_storage_type' => 'field_ui:entity_reference_revisions:paragraph',
149       'label' => 'Paragraph',
150       'field_name' => 'paragraph',
151     ];
152     $this->drupalPostForm(NULL, $edit, 'Save and continue');
153     $this->drupalPostForm(NULL, [], 'Save field settings');
154     $this->assertLinkByHref('admin/structure/paragraphs_type/add');
155     $this->clickLink('here');
156     $this->assertUrl('admin/structure/paragraphs_type/add');
157
158     $this->drupalGet('admin/structure/paragraphs_type');
159     $this->clickLink(t('Add paragraphs type'));
160     $this->assertTitle('Add paragraphs type | Drupal');
161     // Create paragraph type text + image.
162     $this->addParagraphsType('text_image');
163     $this->drupalGet('admin/structure/paragraphs_type/text_image');
164     $this->assertTitle('Edit text_image paragraph type | Drupal');
165     // Create field types for text and image.
166     static::fieldUIAddNewField('admin/structure/paragraphs_type/text_image', 'text', 'Text', 'text_long', array(), array());
167     $this->assertText('Saved Text configuration.');
168     static::fieldUIAddNewField('admin/structure/paragraphs_type/text_image', 'image', 'Image', 'image', array(), array('settings[alt_field_required]' => FALSE));
169     $this->assertText('Saved Image configuration.');
170
171     // Create paragraph type Nested test.
172     $this->addParagraphsType('nested_test');
173
174     static::fieldUIAddNewField('admin/structure/paragraphs_type/nested_test', 'paragraphs', 'Paragraphs', 'entity_reference_revisions', array(
175       'settings[target_type]' => 'paragraph',
176       'cardinality' => '-1',
177     ), array());
178
179     // Change the add more button to select mode.
180     $this->clickLink(t('Manage form display'));
181     $this->drupalPostAjaxForm(NULL, ['fields[field_paragraphs][type]' => 'entity_reference_paragraphs'], 'field_paragraphs_settings_edit');
182     $this->drupalPostForm(NULL, ['fields[field_paragraphs][settings_edit_form][settings][add_mode]' => 'select'], t('Update'));
183     $this->drupalPostForm(NULL, [], t('Save'));
184
185     // Create paragraph type image.
186     $this->addParagraphsType('image');
187     // Create field types for image.
188     static::fieldUIAddNewField('admin/structure/paragraphs_type/image', 'image_only', 'Image only', 'image', array(), array());
189     $this->assertText('Saved Image only configuration.');
190
191     $this->drupalGet('admin/structure/paragraphs_type');
192     $rows = $this->xpath('//tbody/tr');
193     // Make sure 2 types are available with their label.
194     $this->assertEqual(count($rows), 3);
195     $this->assertText('text_image');
196     $this->assertText('image');
197     // Make sure there is an edit link for each type.
198     $this->clickLink(t('Edit'));
199     // Make sure the field UI appears.
200     $this->assertLink('Manage fields');
201     $this->assertLink('Manage form display');
202     $this->assertLink('Manage display');
203     $this->assertTitle('Edit image paragraph type | Drupal');
204
205     // Test for "Add mode" setting.
206     $this->drupalGet('admin/structure/types/manage/article/form-display');
207     $field_name = 'field_paragraphs';
208
209     // Click on the widget settings button to open the widget settings form.
210     $this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");
211
212     // Enable setting.
213     $edit = array('fields[' . $field_name . '][settings_edit_form][settings][add_mode]' => 'button');
214     $this->drupalPostForm(NULL, $edit, t('Save'));
215
216     // Check if the setting is stored.
217     $this->drupalGet('admin/structure/types/manage/article/form-display');
218     $this->assertText('Add mode: Buttons', 'Checking the settings value.');
219
220     $this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");
221     // Assert the 'Buttons' option is selected.
222     $this->assertOptionSelected('edit-fields-field-paragraphs-settings-edit-form-settings-add-mode', 'button', 'Updated value is correct!.');
223
224     // Add two Text + Image paragraphs in article.
225     $this->drupalGet('node/add/article');
226
227     // Checking changes on article.
228     $this->assertRaw('<div class="paragraphs-dropbutton-wrapper"><input', 'Updated value in article.');
229
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);
236
237     $edit = array(
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'),
243     );
244     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
245
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'));
249
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));
255
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");
261     // Enable setting.
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');
275
276     // Test for preview option.
277     $this->drupalGet('admin/structure/types/manage/article/form-display');
278     $this->drupalPostAjaxForm(NULL, array(), "field_paragraphs_settings_edit");
279     $edit = array('fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'preview');
280     $this->drupalPostForm(NULL, $edit, t('Save'));
281     $this->assertText('Edit mode: Preview', 'Checking the settings value.');
282     $this->drupalGet('node/1/edit');
283     // The texts in the paragraphs should be visible.
284     $this->assertNoRaw('field_paragraphs[0][subform][field_text][0][value]');
285     $this->assertNoRaw('field_paragraphs[1][subform][field_text][0][value]');
286     $this->assertText('Test text 1');
287     $this->assertText('Test text 2');
288
289     // Test for open option.
290     $this->drupalGet('admin/structure/types/manage/article/form-display');
291     $this->drupalPostAjaxForm(NULL, array(), "field_paragraphs_settings_edit");
292     // Assert the 'Preview' option is selected.
293     $this->assertOptionSelected('edit-fields-field-paragraphs-settings-edit-form-settings-edit-mode', 'preview', 'Updated value correctly.');
294     // Restore the value to Open for next test.
295     $edit = array('fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'open');
296     $this->drupalPostForm(NULL, $edit, t('Save'));
297     $this->drupalGet('node/1/edit');
298     // The textareas for paragraphs should be visible.
299     $this->assertRaw('field_paragraphs[0][subform][field_text][0][value]');
300     $this->assertRaw('field_paragraphs[1][subform][field_text][0][value]');
301
302     $paragraphs = Paragraph::loadMultiple();
303     $this->assertEqual(count($paragraphs), 2, 'Two paragraphs in article');
304
305     // Check article edit page.
306     $this->drupalGet('node/' . $node->id() . '/edit');
307     // Check both paragraphs in edit page.
308     $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'Test text 1');
309     $this->assertRaw('<a href="' . $img1_url . '" type="image/jpeg; length=21">myImage1.jpg</a>');
310     $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', 'Test text 2');
311     $this->assertRaw('<a href="' . $img2_url . '" type="image/jpeg; length=21">myImage2.jpg</a>');
312     // Remove 2nd paragraph.
313     $this->drupalPostForm(NULL, NULL, t('Remove'));
314     $this->assertNoField('field_paragraphs[1][subform][field_text][0][value]');
315     $this->assertNoRaw('<a href="' . $img2_url . '" type="image/jpeg; length=21">myImage2.jpg</a>');
316     // Restore it again.
317     $this->drupalPostForm(NULL, NULL, t('Restore'));
318     $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', 'Test text 2');
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="entity_reference_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     // Attempt to remove the Paragraph.
490     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_remove');
491     $elements = $this->xpath('//*[@name="field_paragraphs_0_confirm_remove"]');
492     $this->assertTrue(!empty($elements), "'Confirm removal' button appears.");
493     // Restore the Paragraph and fix the broken reference.
494     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_restore');
495     $node = $this->drupalGetNodeByTitle('Example publish/unpublish');
496     $this->drupalPostForm(NULL, ['field_paragraphs[0][subform][field_entity_reference][0][target_id]' => $node->label() . ' (' . $node->id() . ')'], t('Save and keep published'));
497     $this->assertText('choke test has been updated.');
498     $this->assertLink('Example publish/unpublish');
499     // Delete the new referenced node.
500     $node->delete();
501
502     // Set the Paragraph field edit mode to 'Preview'.
503     $this->drupalPostAjaxForm('admin/structure/types/manage/article/form-display', [], 'field_paragraphs_settings_edit');
504     $this->drupalPostForm(NULL, ['fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'preview'], t('Update'));
505     $this->drupalPostForm(NULL, [], t('Save'));
506
507     $node = $this->drupalGetNodeByTitle('choke test');
508     // Attempt to edit the Paragraph.
509     $this->drupalPostAjaxForm('node/' . $node->id() . '/edit', [], 'field_paragraphs_0_edit');
510     // Try to collapse with an invalid reference.
511     $this->drupalPostAjaxForm(NULL, ['field_paragraphs[0][subform][field_entity_reference][0][target_id]' => 'foo'], 'field_paragraphs_0_collapse');
512     $this->assertText('There are no entities matching "foo".');
513     // Remove the Paragraph and save the node.
514     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_remove');
515     $elements = $this->xpath('//*[@name="field_paragraphs_0_confirm_remove"]');
516     $this->assertTrue(!empty($elements), "'Confirm removal' button appears.");
517     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_confirm_remove');
518     $this->drupalPostForm(NULL, [], t('Save and keep published'));
519     $this->assertText('choke test has been updated.');
520
521     // Verify that the text displayed is correct when no paragraph has been
522     // added yet.
523     $this->drupalGet('node/add/article');
524     $this->assertText('No Paragraph added yet.');
525
526     $this->drupalGet('admin/content/files');
527     $this->clickLink('1 place');
528     $label = $this->xpath('//tbody/tr/td[1]');
529     $this->assertEqual(trim(htmlspecialchars_decode(strip_tags($label[0]->asXML()))), 'test required > field_paragraphs > Paragraphs');
530   }
531
532   /**
533    * Asserts that a select option in the current page is checked.
534    *
535    * @param string $id
536    *   ID of select field to assert.
537    * @param string $option
538    *   Option to assert.
539    * @param string $message
540    *   (optional) A message to display with the assertion. Do not translate
541    *   messages: use format_string() to embed variables in the message text, not
542    *   t(). If left blank, a default message will be displayed.
543    * @param string $group
544    *   (optional) The group this message is in, which is displayed in a column
545    *   in test output. Use 'Debug' to indicate this is debugging output. Do not
546    *   translate this string. Defaults to 'Browser'; most tests do not override
547    *   this default.
548    *
549    * @return bool
550    *   TRUE on pass, FALSE on fail.
551    *
552    * @todo Remove function once core issue is resolved: https://www.drupal.org/node/2530092
553    */
554   protected function assertOptionSelected($id, $option, $message = '', $group = 'Browser') {
555     $elements = $this->xpath('//select[contains(@id, :id)]//option[@value=:option]', array(':id' => $id, ':option' => $option));
556     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);
557   }
558
559   /**
560    * Helper function for revision counting.
561    */
562   private function countRevisions($node, $paragraph1, $paragraph2, $revisions_count) {
563     $node_revisions_count = \Drupal::entityQuery('node')->condition('nid', $node->id())->allRevisions()->count()->execute();
564     $this->assertEqual($node_revisions_count, $revisions_count);
565     $this->assertEqual(\Drupal::entityQuery('paragraph')->condition('id', $paragraph1)->allRevisions()->count()->execute(), $revisions_count);
566     $this->assertEqual(\Drupal::entityQuery('paragraph')->condition('id', $paragraph2)->allRevisions()->count()->execute(), $revisions_count);
567   }
568
569 }