Upgraded imagemagick and manually altered pdf to image module to handle changes....
[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 use Drupal\Tests\paragraphs\FunctionalJavascript\ParagraphsTestBaseTrait;
8
9 /**
10  * Tests the configuration of paragraphs.
11  *
12  * @group paragraphs
13  */
14 class ParagraphsExperimentalAdministrationTest extends ParagraphsExperimentalTestBase {
15
16   use FieldUiTestTrait;
17   use ParagraphsTestBaseTrait;
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = array(
25     'image',
26     'file',
27     'views'
28   );
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp() {
34     parent::setUp();
35     // Create paragraphs content type.
36     $this->drupalCreateContentType(array('type' => 'paragraphs', 'name' => 'Paragraphs'));
37   }
38
39   /**
40    * Tests the revision of paragraphs.
41    */
42   public function testParagraphsRevisions() {
43     $this->addParagraphedContentType('article', 'paragraphs');
44     $this->loginAsAdmin([
45       'create paragraphs content',
46       'administer node display',
47       'edit any paragraphs content',
48       'administer nodes',
49     ]);
50
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.');
56
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',
61     ), array(
62       'settings[handler_settings][target_bundles_drag_drop][text][enabled]' => TRUE,
63     ));
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'));
68
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');
73     $edit = [
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',
77     ];
78     $this->drupalPostForm(NULL, $edit + ['status[value]' => TRUE], t('Save'));
79
80     $node = $this->drupalGetNodeByTitle('TEST TITEL');
81     $paragraph1 = $node->field_paragraphs[0]->target_id;
82     $paragraph2 = $node->field_paragraphs[1]->target_id;
83
84     $this->countRevisions($node, $paragraph1, $paragraph2, 1);
85
86     // Edit the node without creating a revision. There should still be only 1
87     // revision for nodes and paragraphs.
88     $edit = [
89       'field_paragraphs[0][subform][field_text][0][value]' => 'Foo Bar 1',
90       'revision' => FALSE,
91     ];
92     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
93
94     $this->countRevisions($node, $paragraph1, $paragraph2, 1);
95
96     // Edit the just created node. Create new revision. Now we should have 2
97     // revisions for nodes and paragraphs.
98     $edit = [
99       'title[0][value]' => 'TEST TITLE',
100       'field_paragraphs[0][subform][field_text][0][value]' => 'Foo Bar 2',
101       'revision' => TRUE,
102     ];
103     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
104
105     $this->countRevisions($node, $paragraph1, $paragraph2, 2);
106
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');
112
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');
127   }
128
129
130   /**
131    * Tests the paragraph creation.
132    */
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',
145     ]);
146
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');
151     $edit = [
152       'new_storage_type' => 'field_ui:entity_reference_revisions:paragraph',
153       'label' => 'Paragraph',
154       'field_name' => 'paragraph',
155     ];
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');
161
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.');
174
175     // Create paragraph type Nested test.
176     $this->addParagraphsType('nested_test');
177
178     static::fieldUIAddNewField('admin/structure/paragraphs_type/nested_test', 'paragraphs', 'Paragraphs', 'entity_reference_revisions', array(
179       'settings[target_type]' => 'paragraph',
180       'cardinality' => '-1',
181     ), array());
182
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'));
188
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.');
194
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');
208
209     // Test for "Add mode" setting.
210     $this->drupalGet('admin/structure/types/manage/article/form-display');
211     $field_name = 'field_paragraphs';
212
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");
215
216     // Enable setting.
217     $edit = array('fields[' . $field_name . '][settings_edit_form][settings][add_mode]' => 'button');
218     $this->drupalPostForm(NULL, $edit, t('Save'));
219
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.');
223
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!.');
227
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);
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'));
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 = [
280       'fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'closed',
281       'fields[field_paragraphs][settings_edit_form][settings][closed_mode]' => 'preview',
282     ];
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');
292
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]');
306
307     $paragraphs = Paragraph::loadMultiple();
308     $this->assertEqual(count($paragraphs), 2, 'Two paragraphs in article');
309
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>');
327     $edit = [
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,
331     ];
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>');
336
337     // Delete the node.
338     $this->clickLink(t('Delete'));
339     $this->drupalPostForm(NULL, NULL, t('Delete'));
340     $this->assertText('Test article has been deleted.');
341
342     // Check if the publish/unpublish option works.
343     $this->drupalGet('admin/structure/paragraphs_type/text_image/form-display');
344     $edit = [
345       'fields[status][type]' => 'boolean_checkbox',
346       'fields[status][region]' => 'content',
347     ];
348
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');
353     $edit = [
354       'title[0][value]' => 'Example publish/unpublish',
355       'field_paragraphs[0][subform][field_text][0][value]' => 'Example published and unpublished',
356     ];
357     $this->drupalPostForm(NULL, $edit, t('Save'));
358     $this->assertText(t('Example published and unpublished'));
359     $this->clickLink(t('Edit'));
360     $edit = [
361       'field_paragraphs[0][subform][status][value]' => FALSE,
362     ];
363     $this->drupalPostForm(NULL, $edit, t('Save'));
364     $this->assertNoText(t('Example published and unpublished'));
365
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'));
373
374     // Add a new article.
375     $this->drupalGet('node/add/article');
376     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_nested_test_add_more');
377
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]');
380
381     $edit = [
382       'field_paragraphs[0][subform][field_paragraphs][add_more][add_more_select]' => 'image',
383     ];
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]');
387
388     // Add an image to the required field.
389     $edit = array(
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'),
392     );
393     $this->drupalPostForm(NULL, $edit, t('Save'));
394     $edit = [
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',
398     ];
399     $this->drupalPostForm(NULL, $edit, t('Save'));
400     $this->assertText('test required has been created.');
401     $this->assertNoRaw('This value should not be null.');
402
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"');
408
409     // Check that Paragraphs is not displayed as an entity_reference field
410     // reference option.
411     $this->drupalGet('admin/structure/types/manage/article/fields/add-field');
412     $edit = [
413       'new_storage_type' => 'entity_reference',
414       'label' => 'unsupported field',
415       'field_name' => 'unsupportedfield',
416     ];
417     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
418     $this->assertNoOption('edit-settings-target-type', 'paragraph');
419
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',
429     ), array());
430     $this->clickLink(t('Manage form display'));
431     $this->drupalPostForm(NULL, [], 'Save');
432     $this->drupalPostAjaxForm('node/add/article', [], 'field_paragraphs_nested_test_add_more');
433     $edit = [
434       'field_paragraphs[0][subform][field_paragraphs][add_more][add_more_select]' => 'nested_double_test',
435     ];
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');
438     $edit = array(
439       'title[0][value]' => 'Nested twins',
440     );
441     $this->drupalPostForm(NULL, $edit, t('Save'));
442     $this->assertText('Nested twins has been created.');
443     $this->assertNoText('This entity (paragraph: ) cannot be referenced.');
444
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'));
449
450     // Set the Paragraph field edit mode to "Closed" and the closed mode to
451     // "Summary".
452     $settings = [
453       'edit_mode' => 'closed',
454       'closed_mode' => 'summary',
455     ];
456     $this->setParagraphsWidgetSettings('article', 'field_paragraphs', $settings);
457
458     $this->addParagraphsType('node_test');
459
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'
464     ), [
465       'settings[handler_settings][target_bundles][article]' => TRUE,
466       'required' => TRUE,
467     ]);
468     $node = $this->drupalGetNodeByTitle('Nested twins');
469
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();
473     $edit = [
474       'field_paragraphs[0][subform][field_entity_reference][0][target_id]' => $node->label() . ' (' . $node->id() . ')',
475       'title[0][value]' => 'choke test',
476     ];
477     $this->drupalPostForm(NULL, $edit, t('Save'));
478     // Delete the referenced node.
479     $node->delete();
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.
507     $node->delete();
508
509     // Set the Paragraph field closed mode to "Preview".
510     $settings = [
511       'edit_mode' => 'closed',
512       'closed_mode' => 'preview',
513     ];
514     $this->setParagraphsWidgetSettings('article', 'field_paragraphs', $settings);
515
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.');
531
532     // Verify that the text displayed is correct when no paragraph has been
533     // added yet.
534     $this->drupalGet('node/add/article');
535     $this->assertText('No Paragraph added yet.');
536
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');
541   }
542
543   /**
544    * Asserts that a select option in the current page is checked.
545    *
546    * @param string $id
547    *   ID of select field to assert.
548    * @param string $option
549    *   Option to assert.
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
558    *   this default.
559    *
560    * @return bool
561    *   TRUE on pass, FALSE on fail.
562    *
563    * @todo Remove function once core issue is resolved: https://www.drupal.org/node/2530092
564    */
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);
568   }
569
570   /**
571    * Helper function for revision counting.
572    */
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);
578   }
579
580 }