5eb2a03698d52d75d13ff39edda0533728f9e906
[yaffs-website] / web / modules / contrib / paragraphs / src / Tests / Experimental / ParagraphsExperimentalDuplicateFeatureTest.php
1 <?php
2
3 namespace Drupal\paragraphs\Tests\Experimental;
4
5 /**
6  * Tests paragraphs duplicate feature.
7  *
8  * @group paragraphs
9  */
10 class ParagraphsExperimentalDuplicateFeatureTest extends ParagraphsExperimentalTestBase {
11
12   public static $modules = [
13     'node',
14     'paragraphs',
15     'field',
16     'field_ui',
17     'block',
18     'paragraphs_test',
19   ];
20
21   /**
22    * Tests duplicate paragraph feature.
23    */
24   public function testDuplicateButton() {
25     $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
26
27     $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
28     // Add a Paragraph type.
29     $paragraph_type = 'text_paragraph';
30     $this->addParagraphsType($paragraph_type);
31     $this->addParagraphsType('text');
32
33     // Add a text field to the text_paragraph type.
34     static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
35     $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_text_paragraph_add_more');
36
37     // Create a node with a Paragraph.
38     $text = 'recognizable_text';
39     $edit = [
40       'title[0][value]' => 'paragraphs_mode_test',
41       'field_paragraphs[0][subform][field_text][0][value]' => $text,
42     ];
43     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
44     $node = $this->drupalGetNodeByTitle('paragraphs_mode_test');
45
46     // Change edit mode to "closed".
47     $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
48     $this->drupalGet('node/' . $node->id() . '/edit');
49
50     // Click "Duplicate" button.
51     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_duplicate');
52     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
53     $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', $text);
54     $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', $text);
55
56     // Save and check if both paragraphs are present.
57     $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
58     $this->assertNoUniqueText($text);
59   }
60
61   /**
62    * Tests duplicate paragraph feature with nested paragraphs.
63    */
64   public function testDuplicateButtonWithNesting() {
65     $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
66
67     $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
68     // Add nested Paragraph type.
69     $nested_paragraph_type = 'nested_paragraph';
70     $this->addParagraphsType($nested_paragraph_type);
71     // Add text Paragraph type.
72     $paragraph_type = 'text';
73     $this->addParagraphsType($paragraph_type);
74
75     // Add a text field to the text_paragraph type.
76     static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
77
78     // Add a ERR paragraph field to the nested_paragraph type.
79     static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $nested_paragraph_type, 'nested', 'Nested', 'field_ui:entity_reference_revisions:paragraph', [
80       'settings[target_type]' => 'paragraph',
81       'cardinality' => '-1',
82     ], []);
83     $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_nested_paragraph_add_more');
84
85     // Create a node with a Paragraph.
86     $edit = [
87       'title[0][value]' => 'paragraphs_mode_test',
88     ];
89     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
90     $node = $this->drupalGetNodeByTitle('paragraphs_mode_test');
91
92     // Add a text field to nested paragraph.
93     $text = 'recognizable_text';
94     $this->drupalPostAjaxForm('node/' . $node->id() . '/edit', [], 'field_paragraphs_0_subform_field_nested_text_add_more');
95     $edit = [
96       'field_paragraphs[0][subform][field_nested][0][subform][field_text][0][value]' => $text,
97     ];
98     $this->drupalPostForm(NULL, $edit, 'Save and keep published');
99
100     // Switch mode to closed.
101     $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
102     $this->drupalGet('node/' . $node->id() . '/edit');
103
104     // Click "Duplicate" button.
105     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_duplicate');
106     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
107     $this->assertFieldByName('field_paragraphs[0][subform][field_nested][0][subform][field_text][0][value]', $text);
108     $this->assertFieldByName('field_paragraphs[1][subform][field_nested][0][subform][field_text][0][value]', $text);
109
110     // Change the text paragraph value of duplicated nested paragraph.
111     $second_paragraph_text = 'duplicated_text';
112     $edit = [
113       'field_paragraphs[1][subform][field_nested][0][subform][field_text][0][value]' => $second_paragraph_text,
114     ];
115
116     // Save and check if the changed text paragraph value of the duplicated
117     // paragraph is not the same as in the original paragraph.
118     $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
119     $this->assertUniqueText($text);
120     $this->assertUniqueText($second_paragraph_text);
121   }
122
123 }