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