3 namespace Drupal\paragraphs\Tests\Classic;
5 use Drupal\field_ui\Tests\FieldUiTestTrait;
8 * Tests paragraphs widget buttons.
12 class ParagraphsWidgetButtonsTest extends ParagraphsTestBase {
17 * Tests the widget buttons of paragraphs.
19 public function testWidgetButtons() {
20 $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
22 $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
23 // Add a Paragraph type.
24 $paragraph_type = 'text_paragraph';
25 $this->addParagraphsType($paragraph_type);
26 $this->addParagraphsType('text');
28 // Add a text field to the text_paragraph type.
29 static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
30 $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_text_paragraph_add_more');
32 // Create a node with a Paragraph.
33 $text = 'recognizable_text';
35 'title[0][value]' => 'paragraphs_mode_test',
36 'field_paragraphs[0][subform][field_text][0][value]' => $text,
38 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_paragraph_add_more');
39 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
40 $node = $this->drupalGetNodeByTitle('paragraphs_mode_test');
42 // Test the 'Open' mode.
43 $this->drupalGet('node/' . $node->id() . '/edit');
44 $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', $text);
45 $this->drupalPostForm(NULL, [], t('Save and keep published'));
46 $this->assertText($text);
48 // Test the 'Closed' mode.
49 $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
50 $this->drupalGet('node/' . $node->id() . '/edit');
51 // Click "Edit" button.
52 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
53 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_1_edit');
54 $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', $text);
55 $closed_mode_text = 'closed_mode_text';
56 // Click "Collapse" button on both paragraphs.
57 $edit = ['field_paragraphs[0][subform][field_text][0][value]' => $closed_mode_text];
58 $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_0_collapse');
59 $edit = ['field_paragraphs[1][subform][field_text][0][value]' => $closed_mode_text];
60 $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_1_collapse');
61 // Verify that we have warning message for each paragraph.
62 $this->assertNoUniqueText('You have unsaved changes on this Paragraph item.');
63 $this->assertRaw('<div class="paragraphs-collapsed-description">' . $closed_mode_text);
64 $this->drupalPostForm(NULL, [], t('Save and keep published'));
65 $this->assertText('paragraphed_test ' . $node->label() . ' has been updated.');
66 $this->assertText($closed_mode_text);
68 // Test the 'Preview' mode.
69 $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'preview');
70 $this->drupalGet('node/' . $node->id() . '/edit');
71 // Click "Edit" button.
72 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
73 $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', $closed_mode_text);
74 $preview_mode_text = 'preview_mode_text';
75 $edit = ['field_paragraphs[0][subform][field_text][0][value]' => $preview_mode_text];
76 // Click "Collapse" button.
77 $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_0_collapse');
78 $this->assertText('You have unsaved changes on this Paragraph item.');
79 $this->assertText($preview_mode_text);
80 $this->drupalPostForm(NULL, [], t('Save and keep published'));
81 $this->assertText('paragraphed_test ' . $node->label() . ' has been updated.');
82 $this->assertText($preview_mode_text);
84 // Test the remove/restore function.
85 $this->drupalGet('node/' . $node->id() . '/edit');
86 $this->assertText($preview_mode_text);
87 // Click "Remove" button.
88 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_remove');
89 $this->assertText('Deleted Paragraph: text_paragraph');
90 // Click "Restore" button.
91 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_restore');
92 $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', $preview_mode_text);
93 $restore_text = 'restore_text';
94 $edit = ['field_paragraphs[0][subform][field_text][0][value]' => $restore_text];
95 $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
96 $this->assertText('paragraphed_test ' . $node->label() . ' has been updated.');
97 $this->assertText($restore_text);
99 // Test the remove/confirm remove function.
100 $this->drupalGet('node/' . $node->id() . '/edit');
101 $this->assertText($restore_text);
102 // Click "Remove" button.
103 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_remove');
104 $this->assertText('Deleted Paragraph: text_paragraph');
105 // Click "Confirm Removal" button.
106 $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_confirm_remove');
107 $this->drupalPostForm(NULL, [], t('Save and keep published'));
108 $this->assertText('paragraphed_test ' . $node->label() . ' has been updated.');
109 $this->assertNoText($restore_text);