- // Click "Duplicate" button.
- $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_duplicate');
- $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
- $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', $text);
- $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', $text);
+ // Click "Duplicate" button on A and move C to the first position.
+ $edit = ['field_paragraphs[2][_weight]' => -1];
+ $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_0_duplicate');
+ $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'A');
+ $this->assertFieldByName('field_paragraphs[0][_weight]', 1);
+ $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', 'B');
+ $this->assertFieldByName('field_paragraphs[1][_weight]', 3);
+ $this->assertFieldByName('field_paragraphs[2][subform][field_text][0][value]', 'C');
+ $this->assertFieldByName('field_paragraphs[2][_weight]', 0);
+ $this->assertFieldByName('field_paragraphs[3][subform][field_text][0][value]', 'A');
+ $this->assertFieldByName('field_paragraphs[3][_weight]', 2);
+
+ // Move C after the A's and save.
+ $edit = [
+ 'field_paragraphs[0][_weight]' => -2,
+ 'field_paragraphs[1][_weight]' => 2,
+ 'field_paragraphs[2][_weight]' => 1,
+ 'field_paragraphs[3][_weight]' => -1,
+ ];
+
+ // Save and check if all paragraphs are present in the correct order.
+ $this->drupalPostForm(NULL, $edit, t('Save'));
+ $this->drupalGet('node/' . $node->id() . '/edit');
+ $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'A');
+ $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', 'A');
+ $this->assertFieldByName('field_paragraphs[2][subform][field_text][0][value]', 'C');
+ $this->assertFieldByName('field_paragraphs[3][subform][field_text][0][value]', 'B');