Upgraded imagemagick and manually altered pdf to image module to handle changes....
[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');
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
32     // Add a text field to the text_paragraph type.
33     static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
34     $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_text_paragraph_add_more');
35     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_text_paragraph_add_more');
36
37     // Create a node with a Paragraph.
38     $text_01 = 'recognizable_text_01';
39     $text_02 = 'recognizable_text_02';
40     $edit = [
41       'title[0][value]' => 'paragraphs_mode_test',
42       'field_paragraphs[0][subform][field_text][0][value]' => 'A',
43       'field_paragraphs[1][subform][field_text][0][value]' => 'B',
44       'field_paragraphs[2][subform][field_text][0][value]' => 'C',
45     ];
46     $this->drupalPostForm(NULL, $edit, t('Save'));
47     $node = $this->drupalGetNodeByTitle('paragraphs_mode_test');
48
49     $this->drupalGet('node/' . $node->id() . '/edit');
50
51     // Click "Duplicate" button on A and move C to the first position.
52     $edit = ['field_paragraphs[2][_weight]' => -1];
53     $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_0_duplicate');
54     $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'A');
55     $this->assertFieldByName('field_paragraphs[0][_weight]', 1);
56     $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', 'B');
57     $this->assertFieldByName('field_paragraphs[1][_weight]', 3);
58     $this->assertFieldByName('field_paragraphs[2][subform][field_text][0][value]', 'C');
59     $this->assertFieldByName('field_paragraphs[2][_weight]', 0);
60     $this->assertFieldByName('field_paragraphs[3][subform][field_text][0][value]', 'A');
61     $this->assertFieldByName('field_paragraphs[3][_weight]', 2);
62
63     // Move C after the A's and save.
64     $edit = [
65       'field_paragraphs[0][_weight]' => -2,
66       'field_paragraphs[1][_weight]' => 2,
67       'field_paragraphs[2][_weight]' => 1,
68       'field_paragraphs[3][_weight]' => -1,
69     ];
70
71     // Save and check if all paragraphs are present in the correct order.
72     $this->drupalPostForm(NULL, $edit, t('Save'));
73     $this->drupalGet('node/' . $node->id() . '/edit');
74     $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'A');
75     $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', 'A');
76     $this->assertFieldByName('field_paragraphs[2][subform][field_text][0][value]', 'C');
77     $this->assertFieldByName('field_paragraphs[3][subform][field_text][0][value]', 'B');
78
79     // Delete the second A, then duplicate C.
80     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_1_remove');
81     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_2_duplicate');
82     $this->drupalPostForm(NULL, [], t('Save'));
83
84     $this->drupalGet('node/' . $node->id() . '/edit');
85     $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'A');
86     $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', 'C');
87     $this->assertFieldByName('field_paragraphs[2][subform][field_text][0][value]', 'C');
88     $this->assertFieldByName('field_paragraphs[3][subform][field_text][0][value]', 'B');
89   }
90
91   /**
92    * Tests duplicate paragraph feature with nested paragraphs.
93    */
94   public function testDuplicateButtonWithNesting() {
95     $this->addParagraphedContentType('paragraphed_test');
96
97     $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
98     // Add nested Paragraph type.
99     $nested_paragraph_type = 'nested_paragraph';
100     $this->addParagraphsType($nested_paragraph_type);
101     // Add text Paragraph type.
102     $paragraph_type = 'text';
103     $this->addParagraphsType($paragraph_type);
104
105     // Add a text field to the text_paragraph type.
106     static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
107
108     // Add a ERR paragraph field to the nested_paragraph type.
109     static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $nested_paragraph_type, 'nested', 'Nested', 'field_ui:entity_reference_revisions:paragraph', [
110       'settings[target_type]' => 'paragraph',
111       'cardinality' => '-1',
112     ], []);
113     $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_nested_paragraph_add_more');
114
115     // Create a node with a Paragraph.
116     $edit = [
117       'title[0][value]' => 'paragraphs_mode_test',
118     ];
119     $this->drupalPostForm(NULL, $edit, t('Save'));
120     $node = $this->drupalGetNodeByTitle('paragraphs_mode_test');
121
122     // Add a text field to nested paragraph.
123     $text = 'recognizable_text';
124     $this->drupalPostAjaxForm('node/' . $node->id() . '/edit', [], 'field_paragraphs_0_subform_field_nested_text_add_more');
125     $edit = [
126       'field_paragraphs[0][subform][field_nested][0][subform][field_text][0][value]' => $text,
127     ];
128     $this->drupalPostForm(NULL, $edit, t('Save'));
129
130     // Switch mode to closed.
131     $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
132     $this->drupalGet('node/' . $node->id() . '/edit');
133
134     // Click "Duplicate" button.
135     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_duplicate');
136     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
137     $this->assertFieldByName('field_paragraphs[0][subform][field_nested][0][subform][field_text][0][value]', $text);
138     $this->assertFieldByName('field_paragraphs[1][subform][field_nested][0][subform][field_text][0][value]', $text);
139
140     // Change the text paragraph value of duplicated nested paragraph.
141     $second_paragraph_text = 'duplicated_text';
142     $edit = [
143       'field_paragraphs[1][subform][field_nested][0][subform][field_text][0][value]' => $second_paragraph_text,
144     ];
145
146     // Save and check if the changed text paragraph value of the duplicated
147     // paragraph is not the same as in the original paragraph.
148     $this->drupalPostForm(NULL, $edit, t('Save'));
149     $this->assertUniqueText($text);
150     $this->assertUniqueText($second_paragraph_text);
151   }
152
153 }