public function ParagraphsDragAndDropModeTest::testMultipleChangesParagraphs

Tests drag and drop mode with multiple changes on the paragraphs.

File

paragraphs/tests/src/Functional/WidgetStable/ParagraphsDragAndDropModeTest.php, line 262

Class

ParagraphsDragAndDropModeTest
Tests the drag and drop mode of paragraphs.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetStable

Code

public function testMultipleChangesParagraphs() {

  // Create text paragraph.
  $text_paragraph_1 = Paragraph::create([
    'type' => 'text',
    'field_text' => [
      'value' => 'Test text 1',
      'format' => 'plain_text',
    ],
  ]);
  $text_paragraph_1
    ->save();

  // Create a second text paragraph.
  $text_paragraph_2 = Paragraph::create([
    'type' => 'text',
    'field_text' => [
      'value' => 'Test text 2.',
      'format' => 'plain_text',
    ],
  ]);
  $text_paragraph_2
    ->save();

  // Create container that contains the first two text paragraphs.
  $paragraph_1 = Paragraph::create([
    'title' => 'Test Paragraph 1',
    'type' => 'paragraphs_container',
    'paragraphs_container_paragraphs' => [
      $text_paragraph_1,
      $text_paragraph_2,
    ],
  ]);
  $paragraph_1
    ->save();

  // Create another text paragraph.
  $text_paragraph_3 = Paragraph::create([
    'type' => 'text',
    'field_text' => [
      'value' => 'Test text 3.',
      'format' => 'plain_text',
    ],
  ]);
  $text_paragraph_3
    ->save();

  // Create a container that contains the third text paragraph.
  $paragraph_2 = Paragraph::create([
    'type' => 'paragraphs_container',
    'paragraphs_container_paragraphs' => [
      $text_paragraph_3,
    ],
  ]);
  $paragraph_2
    ->save();

  // Create a container that contains the second paragraph.
  $paragraph_3 = Paragraph::create([
    'type' => 'paragraphs_container',
    'paragraphs_container_paragraphs' => [
      $paragraph_2,
    ],
  ]);
  $paragraph_3
    ->save();

  // Create an empty container paragraph.
  $paragraph_4 = Paragraph::create([
    'type' => 'paragraphs_container',
    'paragraphs_container_paragraphs' => [],
  ]);
  $paragraph_4
    ->save();

  // Create a node with the structure of three nested paragraphs, first
  // paragraph with two text paragraphs, second paragraph with a nested
  // paragraph containing a text paragraph and the third empty paragraph.
  $node = Node::create([
    'type' => 'paragraphed_test',
    'title' => 'Paragraphs Test',
    'field_paragraphs' => [
      $paragraph_1,
      $paragraph_3,
      $paragraph_4,
    ],
  ]);
  $node
    ->save();

  // Edit the node.
  $this
    ->drupalGet('/node/' . $node
    ->id() . '/edit');
  $this
    ->getSession()
    ->getPage()
    ->findButton('field_paragraphs_2_edit')
    ->press();
  $this
    ->getSession()
    ->getPage()
    ->findButton('field_paragraphs_2_subform_paragraphs_container_paragraphs_text_add_more')
    ->press();
  $edit = [
    'field_paragraphs[2][subform][paragraphs_container_paragraphs][0][subform][field_text][0][value]' => 'new paragraph',
  ];
  $this
    ->submitForm($edit, 'Drag & drop');

  // Change the structure of the node, third text paragraph goes to first
  // container, the first text paragraph goes to the second container (child
  // of third container) and the third container goes to the fourth container.
  // This also affects weights and paths of child and related paragraphs.
  $assert_session = $this
    ->assertSession();
  $assert_session
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][1][dragdrop][paragraphs_container_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_path]')
    ->setValue('field_paragraphs][0][paragraphs_container_paragraphs');
  $assert_session
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_path]')
    ->setValue('field_paragraphs][1][paragraphs_container_paragraphs][0][paragraphs_container_paragraphs][0][paragraphs_container_paragraphs');
  $assert_session
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][1][dragdrop][paragraphs_container_paragraphs][list][0][_path]')
    ->setValue('field_paragraphs][1][paragraphs_container_paragraphs][0][paragraphs_container_paragraphs');
  $assert_session
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][1][_path]')
    ->setValue('field_paragraphs][1][paragraphs_container_paragraphs');
  $assert_session
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][2][dragdrop][paragraphs_container_paragraphs][list][0][_path]')
    ->setValue('field_paragraphs][1][paragraphs_container_paragraphs');
  $assert_session
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][1][_weight]')
    ->setValue(0);
  $assert_session
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][2][dragdrop][paragraphs_container_paragraphs][list][0][_weight]')
    ->setValue(1);
  $assert_session
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][2][_weight]')
    ->setValue(1);

  // Save immediately, without separately confirming the widget changes.
  $this
    ->submitForm([], 'Save');

  // Reset the cache to make sure that the loaded parents are the new ones.
  \Drupal::entityTypeManager()
    ->getStorage('paragraph')
    ->resetCache();

  // Assert the new parents of the text paragraphs.
  $text_paragraph_1 = Paragraph::load($text_paragraph_1
    ->id());
  $this
    ->assertEquals($text_paragraph_1
    ->get('parent_id')->value, $paragraph_2
    ->id());
  $this
    ->assertEquals($text_paragraph_1
    ->get('parent_type')->value, 'paragraph');
  $text_paragraph_3 = Paragraph::load($text_paragraph_3
    ->id());
  $this
    ->assertEquals($text_paragraph_3
    ->get('parent_id')->value, $paragraph_1
    ->id());
  $this
    ->assertEquals($text_paragraph_3
    ->get('parent_type')->value, 'paragraph');

  // Assert the new parent of the container.
  $paragraph_3 = Paragraph::load($paragraph_3
    ->id());
  $this
    ->assertEquals($paragraph_3
    ->get('parent_id')->value, $paragraph_4
    ->id());
  $this
    ->assertEquals($paragraph_3
    ->get('parent_type')->value, 'paragraph');
}