You are here

public function ParagraphsDuplicateFeatureTest::testDuplicateButtonWithNesting in Paragraphs 8

Tests duplicate paragraph feature with nested paragraphs.

File

tests/src/Functional/WidgetStable/ParagraphsDuplicateFeatureTest.php, line 121

Class

ParagraphsDuplicateFeatureTest
Tests paragraphs duplicate feature.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetStable

Code

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

  // Add nested Paragraph type.
  $nested_paragraph_type = 'nested_paragraph';
  $this
    ->addParagraphsType($nested_paragraph_type);

  // Add text Paragraph type.
  $paragraph_type = 'text';
  $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', [], []);

  // Add a ERR paragraph field to the nested_paragraph type.
  static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $nested_paragraph_type, 'nested', 'Nested', 'field_ui:entity_reference_revisions:paragraph', [
    'settings[target_type]' => 'paragraph',
    'cardinality' => '-1',
  ], []);
  $this
    ->drupalGet('node/add/paragraphed_test');
  $this
    ->submitForm([], 'field_paragraphs_nested_paragraph_add_more');

  // Create a node with a Paragraph.
  $edit = [
    'title[0][value]' => 'paragraphs_mode_test',
  ];
  $this
    ->submitForm($edit, 'Save');
  $node = $this
    ->drupalGetNodeByTitle('paragraphs_mode_test');

  // Add a text field to nested paragraph.
  $text = 'recognizable_text';
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->submitForm([], 'field_paragraphs_0_subform_field_nested_text_add_more');
  $edit = [
    'field_paragraphs[0][subform][field_nested][0][subform][field_text][0][value]' => $text,
  ];
  $this
    ->submitForm($edit, 'Save');

  // Switch mode to closed.
  $this
    ->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');

  // Click "Duplicate" button.
  $this
    ->submitForm([], 'field_paragraphs_0_duplicate');
  $this
    ->submitForm([], 'field_paragraphs_0_edit');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[0][subform][field_nested][0][subform][field_text][0][value]', $text);
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[1][subform][field_nested][0][subform][field_text][0][value]', $text);

  // Change the text paragraph value of duplicated nested paragraph.
  $second_paragraph_text = 'duplicated_text';
  $edit = [
    'field_paragraphs[1][subform][field_nested][0][subform][field_text][0][value]' => $second_paragraph_text,
  ];

  // Save and check if the changed text paragraph value of the duplicated
  // paragraph is not the same as in the original paragraph.
  $this
    ->submitForm($edit, 'Save');
  $page_text = $this
    ->getSession()
    ->getPage()
    ->getText();
  $text_nr_found = substr_count($page_text, $text);
  $this
    ->assertSame(1, $text_nr_found);
  $page_text = $this
    ->getSession()
    ->getPage()
    ->getText();
  $second_paragraph_text_nr_found = substr_count($page_text, $second_paragraph_text);
  $this
    ->assertSame(1, $second_paragraph_text_nr_found);
}