You are here

public function ParagraphsDragAndDropModeTest::testChangeParagraphMoveBeforeReorder in Paragraphs 8

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

File

tests/src/Functional/WidgetStable/ParagraphsDragAndDropModeTest.php, line 501

Class

ParagraphsDragAndDropModeTest
Tests the drag and drop mode of paragraphs.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetStable

Code

public function testChangeParagraphMoveBeforeReorder() {

  // 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');

  // Move the second text below the container.
  $edit = [
    'field_paragraphs[1][_weight]' => 2,
    'field_paragraphs[2][_weight]' => 1,
  ];
  $this
    ->submitForm($edit, 'Drag & drop');

  // Change the path of the text paragraph to the empty container as its
  // parent.
  $this
    ->assertSession()
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_path]')
    ->setValue('field_paragraphs][1][paragraphs_container_paragraphs');
  $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')), 3);
  $this
    ->assertEquals($node
    ->get('field_paragraphs')
    ->get(0)->target_id, $paragraph
    ->id());
  $this
    ->assertEquals($node
    ->get('field_paragraphs')
    ->get(1)->target_id, $paragraph_1
    ->id());
  $this
    ->assertEquals($node
    ->get('field_paragraphs')
    ->get(2)->target_id, $text_paragraph_2
    ->id());
  $paragraph = $node
    ->get('field_paragraphs')
    ->get(0)->entity;
  $this
    ->assertEquals(count($paragraph
    ->get('paragraphs_container_paragraphs')), 0);
  $paragraph_1 = $node
    ->get('field_paragraphs')
    ->get(1)->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_1
    ->id());
  $text_paragraph_1 = $paragraph_1
    ->get('paragraphs_container_paragraphs')->entity;
  $this
    ->assertEquals($text_paragraph_1
    ->get('parent_id')->value, $paragraph_1
    ->id());
  $this
    ->assertEquals($text_paragraph_1
    ->get('parent_type')->value, 'paragraph');
}