public function ParagraphsDuplicateFeatureTest::testDuplicateButton

Tests duplicate paragraph feature.

File

paragraphs/tests/src/Functional/WidgetStable/ParagraphsDuplicateFeatureTest.php, line 26

Class

ParagraphsDuplicateFeatureTest
Tests paragraphs duplicate feature.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetStable

Code

public function testDuplicateButton() {
  $this
    ->addParagraphedContentType('paragraphed_test');
  $this
    ->loginAsAdmin([
    'create paragraphed_test content',
    'edit any paragraphed_test content',
  ]);

  // Add a Paragraph type.
  $paragraph_type = 'text_paragraph';
  $this
    ->addParagraphsType($paragraph_type);

  // Add a text field to the text_paragraph type.
  static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
  $this
    ->drupalGet('node/add/paragraphed_test');
  $this
    ->submitForm([], 'field_paragraphs_text_paragraph_add_more');
  $this
    ->submitForm([], 'field_paragraphs_text_paragraph_add_more');

  // Create a node with a Paragraph.
  $edit = [
    'title[0][value]' => 'paragraphs_mode_test',
    'field_paragraphs[0][subform][field_text][0][value]' => 'A',
    'field_paragraphs[1][subform][field_text][0][value]' => 'B',
    'field_paragraphs[2][subform][field_text][0][value]' => 'C',
  ];
  $this
    ->submitForm($edit, 'Save');
  $node = $this
    ->drupalGetNodeByTitle('paragraphs_mode_test');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');

  // Click "Duplicate" button on A and move C to the first position.
  $edit = [
    'field_paragraphs[2][_weight]' => -1,
  ];
  $this
    ->submitForm($edit, 'field_paragraphs_0_duplicate');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[0][subform][field_text][0][value]', 'A');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[0][_weight]', 1);
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[1][subform][field_text][0][value]', 'B');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[1][_weight]', 3);
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[2][subform][field_text][0][value]', 'C');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[2][_weight]', 0);
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[3][subform][field_text][0][value]', 'A');
  $this
    ->assertSession()
    ->fieldValueEquals('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
    ->submitForm($edit, 'Save');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[0][subform][field_text][0][value]', 'A');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[1][subform][field_text][0][value]', 'A');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[2][subform][field_text][0][value]', 'C');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[3][subform][field_text][0][value]', 'B');

  // Delete the second A, then duplicate C.
  $this
    ->submitForm([], 'field_paragraphs_1_remove');
  $this
    ->submitForm([], 'field_paragraphs_2_duplicate');
  $this
    ->submitForm([], 'Save');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[0][subform][field_text][0][value]', 'A');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[1][subform][field_text][0][value]', 'C');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[2][subform][field_text][0][value]', 'C');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[3][subform][field_text][0][value]', 'B');

  // Check that the duplicate action is present.
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_0_duplicate');

  // Disable show duplicate action.
  $this
    ->drupalGet('admin/structure/types/manage/paragraphed_test/form-display');
  $this
    ->assertSession()
    ->pageTextContains('Features: Duplicate, Collapse / Edit all');
  $this
    ->submitForm([], 'field_paragraphs_settings_edit');
  $this
    ->submitForm([
    'fields[field_paragraphs][settings_edit_form][settings][features][duplicate]' => FALSE,
  ], 'Update');
  $this
    ->assertSession()
    ->pageTextContains('Features: Collapse / Edit all');
  $this
    ->submitForm([], 'Save');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');

  // Check that the duplicate action is not present.
  $this
    ->assertSession()
    ->buttonNotExists('field_paragraphs_0_duplicate');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[0][subform][field_text][0][value]', 'A');

  // Enable show duplicate action.
  $this
    ->drupalGet('admin/structure/types/manage/paragraphed_test/form-display');
  $this
    ->assertSession()
    ->pageTextContains('Features: Collapse / Edit all');
  $this
    ->submitForm([], 'field_paragraphs_settings_edit');
  $this
    ->submitForm([
    'fields[field_paragraphs][settings_edit_form][settings][features][duplicate]' => TRUE,
  ], 'Update');
  $this
    ->assertSession()
    ->pageTextContains('Features: Duplicate, Collapse / Edit all');
  $this
    ->submitForm([], 'Save');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');

  // Check that the duplicate action is present.
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_0_duplicate');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[0][subform][field_text][0][value]', 'A');
}