914b98f054bbb3a4efc54e5d30a3a4eb1de1df6d
[yaffs-website] / web / modules / contrib / paragraphs / src / Tests / Experimental / ParagraphsExperimentalEditModesTest.php
1 <?php
2
3 namespace Drupal\paragraphs\Tests\Experimental;
4
5 use Drupal\field_ui\Tests\FieldUiTestTrait;
6
7 /**
8  * Tests paragraphs edit modes.
9  *
10  * @group paragraphs
11  */
12 class ParagraphsExperimentalEditModesTest extends ParagraphsExperimentalTestBase {
13
14   use FieldUiTestTrait;
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = [
22     'image',
23   ];
24
25   /**
26    * Tests the collapsed summary of paragraphs.
27    */
28   public function testCollapsedSummary() {
29     $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
30     $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
31
32     // Add a Paragraph type.
33     $paragraph_type = 'image_text_paragraph';
34     $this->addParagraphsType($paragraph_type);
35     $this->addParagraphsType('text');
36     static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'image', 'Image', 'image', [], ['settings[alt_field_required]' => FALSE]);
37     static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
38
39     // Add a user Paragraph Type
40     $paragraph_type = 'user_paragraph';
41     $this->addParagraphsType($paragraph_type);
42     static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'user', 'User', 'entity_reference', ['settings[target_type]' => 'user'], []);
43
44     // Set edit mode to closed.
45     $this->drupalGet('admin/structure/types/manage/paragraphed_test/form-display');
46     $this->drupalPostAjaxForm(NULL, [], "field_paragraphs_settings_edit");
47     $edit = ['fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'closed'];
48     $this->drupalPostForm(NULL, $edit, t('Save'));
49     // Add a paragraph.
50     $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_image_text_paragraph_add_more');
51
52     $text = 'Trust me I am an image';
53     file_put_contents('temporary://myImage1.jpg', $text);
54
55     // Create a node with an image and text.
56     $edit = [
57       'title[0][value]' => 'Test article',
58       'field_paragraphs[0][subform][field_text][0][value]' => 'text_summary',
59       'files[field_paragraphs_0_subform_field_image_0]' => drupal_realpath('temporary://myImage1.jpg'),
60     ];
61     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
62     $this->clickLink(t('Edit'));
63     $this->drupalPostForm(NULL, [], t('Add user_paragraph'));
64     $edit = [
65       'field_paragraphs[1][subform][field_user][0][target_id]' => $this->admin_user->label() . ' (' . $this->admin_user->id() . ')',
66     ];
67     $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
68
69     // Assert the summary is correctly generated.
70     $this->clickLink(t('Edit'));
71     $this->assertRaw('<div class="paragraphs-collapsed-description">myImage1.jpg, text_summary');
72     $this->assertRaw('<div class="paragraphs-collapsed-description">' . $this->admin_user->label());
73
74     // Edit and remove alternative text.
75     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
76     $edit = [
77       'field_paragraphs[0][subform][field_image][0][alt]' => 'alternative_text_summary',
78       'field_paragraphs[0][subform][field_image][0][width]' => 300,
79       'field_paragraphs[0][subform][field_image][0][height]' => 300,
80     ];
81     $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_0_collapse');
82     // Assert the summary is correctly generated.
83     $this->assertRaw('<div class="paragraphs-collapsed-description">alternative_text_summary, text_summary');
84
85     // Remove image.
86     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
87     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_subform_field_image_0_remove_button');
88     $this->drupalPostForm(NULL, [], t('Save and keep published'));
89
90     // Assert the summary is correctly generated.
91     $this->clickLink(t('Edit'));
92     $this->assertRaw('<div class="paragraphs-collapsed-description">text_summary');
93
94     $this->addParagraphsType('nested_paragraph');
95     static::fieldUIAddNewField('admin/structure/paragraphs_type/nested_paragraph', 'nested_content', 'Nested Content', 'entity_reference_revisions', ['settings[target_type]' => 'paragraph'], []);
96     $this->drupalGet('admin/structure/paragraphs_type/nested_paragraph/form-display');
97     $this->drupalPostForm(NULL, ['fields[field_nested_content][type]' => 'entity_reference_paragraphs'], t('Save'));
98
99     $test_user = $this->drupalCreateUser([]);
100
101     $this->drupalGet('node/add/paragraphed_test');
102     $this->drupalPostForm(NULL, NULL, t('Add nested_paragraph'));
103     $this->drupalPostAjaxForm(NULL, NULL, t('field_paragraphs_0_subform_field_nested_content_user_paragraph_add_more'));
104     $this->drupalPostForm(NULL, [
105       'title[0][value]' => 'Node title',
106       'field_paragraphs[0][subform][field_nested_content][0][subform][field_user][0][target_id]' => $test_user->label() . ' (' . $test_user->id() . ')',
107     ], t('Save and publish'));
108
109     // Create an orphaned ER field item by deleting the target entity.
110     $test_user->delete();
111
112     $nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['title' => 'Node title']);
113     $this->drupalGet('node/' . current($nodes)->id() . '/edit');
114     $this->drupalPostAjaxForm(NULL, [], t('field_paragraphs_0_edit'));
115     $this->drupalPostAjaxForm(NULL, [], t('field_paragraphs_0_collapse'));
116     $this->assertResponse(200);
117   }
118
119 }