You are here

public function ParagraphsDragAndDropModeTest::testChangeParagraphMoveAllFromNestedContainer in Paragraphs 8

Tests emptying a nested container.

File

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

Class

ParagraphsDragAndDropModeTest
Tests the drag and drop mode of paragraphs.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetStable

Code

public function testChangeParagraphMoveAllFromNestedContainer() {

  // 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 a nested container that contains the two text paragraphs.
  $nested_container = Paragraph::create([
    'type' => 'paragraphs_container',
    'paragraphs_container_paragraphs' => [
      $text_paragraph_1,
      $text_paragraph_2,
    ],
  ]);
  $nested_container
    ->save();

  // Create a container that contains the first two text paragraphs.
  $container = Paragraph::create([
    'type' => 'paragraphs_container',
    'paragraphs_container_paragraphs' => [
      $nested_container,
    ],
  ]);
  $container
    ->save();

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

  // Check that the parent of the second text paragraph is the nested
  // container.
  $text_paragraph_2 = Paragraph::load($text_paragraph_2
    ->id());
  $this
    ->assertEquals($text_paragraph_2
    ->get('parent_id')->value, $nested_container
    ->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][dragdrop][paragraphs_container_paragraphs][list][0][_path]', 'field_paragraphs][0][paragraphs_container_paragraphs][0][paragraphs_container_paragraphs');
  $assert_session
    ->hiddenFieldValueEquals('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][1][_path]', 'field_paragraphs][0][paragraphs_container_paragraphs][0][paragraphs_container_paragraphs');

  // Change the path of both text paragraphs to the top container as their
  // parent with the nested container in the middle.
  $assert_session
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][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][dragdrop][paragraphs_container_paragraphs][list][1][_path]')
    ->setValue('field_paragraphs][0][paragraphs_container_paragraphs');
  $assert_session
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_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][0][_weight]')
    ->setValue(1);
  $assert_session
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][1][_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(1, count($node
    ->get('field_paragraphs')));
  $this
    ->assertEquals($container
    ->id(), $node
    ->get('field_paragraphs')
    ->get(0)->target_id);
  $container = $node
    ->get('field_paragraphs')
    ->get(0)->entity;
  $this
    ->assertEquals('node', $container
    ->get('parent_type')->value);
  $this
    ->assertEquals($node
    ->id(), $container
    ->get('parent_id')->value);
  $this
    ->assertEquals(3, count($container
    ->get('paragraphs_container_paragraphs')));
  $this
    ->assertEquals($text_paragraph_1
    ->id(), $container
    ->get('paragraphs_container_paragraphs')
    ->get(0)->target_id);
  $text_paragraph_1 = $container
    ->get('paragraphs_container_paragraphs')
    ->get(0)->entity;
  $this
    ->assertEquals('paragraph', $text_paragraph_1
    ->get('parent_type')->value);
  $this
    ->assertEquals($container
    ->id(), $text_paragraph_1
    ->get('parent_id')->value);
  $this
    ->assertEquals($nested_container
    ->id(), $container
    ->get('paragraphs_container_paragraphs')
    ->get(1)->target_id);
  $nested_container = $container
    ->get('paragraphs_container_paragraphs')
    ->get(1)->entity;
  $this
    ->assertEquals('paragraph', $nested_container
    ->get('parent_type')->value);
  $this
    ->assertEquals($container
    ->id(), $nested_container
    ->get('parent_id')->value);
  $this
    ->assertEquals(count($nested_container
    ->get('paragraphs_container_paragraphs')), 0);
  $this
    ->assertEquals($text_paragraph_2
    ->id(), $container
    ->get('paragraphs_container_paragraphs')
    ->get(2)->target_id);
  $text_paragraph_2 = $container
    ->get('paragraphs_container_paragraphs')
    ->get(2)->entity;
  $this
    ->assertEquals('paragraph', $text_paragraph_2
    ->get('parent_type')->value);
  $this
    ->assertEquals($container
    ->id(), $text_paragraph_2
    ->get('parent_id')->value);
}