public function ParagraphsDragAndDropModeTest::testChangeParagraphMoveAllFromTopLevelContainer

Tests emptying a top level container.

File

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

Class

ParagraphsDragAndDropModeTest
Tests the drag and drop mode of paragraphs.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetStable

Code

public function testChangeParagraphMoveAllFromTopLevelContainer() {

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

  // Add test node with paragraph container.
  $node = Node::create([
    'type' => 'paragraphed_test',
    'title' => 'Paragraphs Test',
    'field_paragraphs' => [
      $paragraph,
    ],
  ]);
  $node
    ->save();

  // Check that the parent of the second text paragraph is the paragraph
  // container.
  $text_paragraph_2 = Paragraph::load($text_paragraph_2
    ->id());
  $this
    ->assertEquals($text_paragraph_2
    ->get('parent_id')->value, $paragraph
    ->id());
  $this
    ->assertEquals($text_paragraph_2
    ->get('parent_type')->value, 'paragraph');
  $this
    ->drupalGet('/node/' . $node
    ->id() . '/edit');
  $this
    ->submitForm([], 'Drag & drop');
  $assert_session = $this
    ->assertSession();
  $assert_session
    ->hiddenFieldValueEquals('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_path]', 'field_paragraphs][0][paragraphs_container_paragraphs');
  $assert_session
    ->hiddenFieldValueEquals('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][1][_path]', 'field_paragraphs][0][paragraphs_container_paragraphs');

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

  // Check the new structure of the node and its paragraphs.
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache();
  \Drupal::entityTypeManager()
    ->getStorage('paragraph')
    ->resetCache();
  $node = Node::load($node
    ->id());
  $this
    ->assertEquals(3, count($node
    ->get('field_paragraphs')));
  $this
    ->assertEquals($node
    ->get('field_paragraphs')
    ->get(0)->target_id, $text_paragraph_1
    ->id());
  $text_paragraph_1 = $node
    ->get('field_paragraphs')
    ->get(0)->entity;
  $this
    ->assertEquals('node', $text_paragraph_1
    ->get('parent_type')->value);
  $this
    ->assertEquals($node
    ->id(), $text_paragraph_1
    ->get('parent_id')->value);
  $this
    ->assertEquals($node
    ->get('field_paragraphs')
    ->get(1)->target_id, $text_paragraph_2
    ->id());
  $text_paragraph_2 = $node
    ->get('field_paragraphs')
    ->get(1)->entity;
  $this
    ->assertEquals('node', $text_paragraph_2
    ->get('parent_type')->value);
  $this
    ->assertEquals($node
    ->id(), $text_paragraph_2
    ->get('parent_id')->value);
  $this
    ->assertEquals($node
    ->get('field_paragraphs')
    ->get(2)->target_id, $paragraph
    ->id());
  $paragraph = $node
    ->get('field_paragraphs')
    ->get(2)->entity;
  $this
    ->assertEquals('node', $paragraph
    ->get('parent_type')->value);
  $this
    ->assertEquals($node
    ->id(), $paragraph
    ->get('parent_id')->value);
  $this
    ->assertEquals(0, count($paragraph
    ->get('paragraphs_container_paragraphs')));
}