public function ParagraphsDragAndDropModeTest::testChangeParagraphMoveAfterDelete

Tests deleting a paragraph and after that enable the drag and drop mode.

File

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

Class

ParagraphsDragAndDropModeTest
Tests the drag and drop mode of paragraphs.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetStable

Code

public function testChangeParagraphMoveAfterDelete() {

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

  // Create 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 text paragraphs.
  $paragraph = Paragraph::create([
    'type' => 'paragraphs_container',
    'paragraphs_container_paragraphs' => [
      $text_paragraph_1,
    ],
  ]);
  $paragraph
    ->save();

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

  // Create the node with two containers and the second text in the middle.
  $node = Node::create([
    'type' => 'paragraphed_test',
    'title' => 'Paragraphs Test',
    'field_paragraphs' => [
      $paragraph,
      $text_paragraph_2,
      $paragraph_1,
    ],
  ]);
  $node
    ->save();
  $this
    ->drupalGet('/node/' . $node
    ->id() . '/edit');

  // Delete the first container, move the text 2 paragraph into the second
  // container.
  $this
    ->getSession()
    ->getPage()
    ->pressButton('field_paragraphs_0_remove');
  $this
    ->submitForm([], 'Drag & drop');
  $assert_session = $this
    ->assertSession();
  $assert_session
    ->pageTextNotContains('Test text 1');
  $assert_session
    ->pageTextContains('Test text 2');

  // Change the path of the text 2 paragraph to the empty container as its
  // parent.
  $assert_session
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][_path]')
    ->setValue('field_paragraphs][0][paragraphs_container_paragraphs');
  $assert_session
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][1][_weight]')
    ->setValue(0);
  $this
    ->submitForm([], 'Complete drag & drop');
  $this
    ->submitForm([], 'Save');

  // Check that the parent of the text paragraph is the second paragraph
  // container.
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache();
  \Drupal::entityTypeManager()
    ->getStorage('paragraph')
    ->resetCache();
  $node = Node::load($node
    ->id());
  $this
    ->assertEquals(count($node
    ->get('field_paragraphs')), 1);
  $this
    ->assertEquals($node
    ->get('field_paragraphs')
    ->get(0)->target_id, $paragraph_1
    ->id());
  $paragraph_1 = $node
    ->get('field_paragraphs')
    ->get(0)->entity;
  $this
    ->assertEquals(count($paragraph_1
    ->get('paragraphs_container_paragraphs')), 1);
  $this
    ->assertEquals($paragraph_1
    ->get('paragraphs_container_paragraphs')
    ->get(0)->target_id, $text_paragraph_2
    ->id());
  $text_paragraph_2 = $paragraph_1
    ->get('paragraphs_container_paragraphs')->entity;
  $this
    ->assertEquals($text_paragraph_2
    ->get('parent_id')->value, $paragraph_1
    ->id());
  $this
    ->assertEquals($text_paragraph_2
    ->get('parent_type')->value, 'paragraph');
}