You are here

public function ParagraphsDragAndDropModeTest::testChangeParagraphContainerMultipleFields in Paragraphs 8

Tests that a separate field is not affected by reordering one field.

File

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

Class

ParagraphsDragAndDropModeTest
Tests the drag and drop mode of paragraphs.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetStable

Code

public function testChangeParagraphContainerMultipleFields() {
  $this
    ->addParagraphsField('paragraphed_test', 'field_paragraphs_second', 'node');

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

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

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

  // Create a container paragraph for the second field.
  $paragraph_second = Paragraph::create([
    'type' => 'paragraphs_container',
    'paragraphs_container_paragraphs' => [
      $text_paragraph_2,
    ],
  ]);
  $paragraph_second
    ->save();

  // Add test content with paragraph container and the third text paragraph.
  $node = Node::create([
    'type' => 'paragraphed_test',
    'title' => 'Paragraphs Test',
    'field_paragraphs' => [
      $paragraph,
      $paragraph_1,
    ],
    'field_paragraphs_second' => [
      $paragraph_second,
    ],
  ]);
  $node
    ->save();

  // Change the path of the text paragraph to the empty container as its
  // parent.
  $this
    ->drupalGet('/node/' . $node
    ->id() . '/edit');
  $this
    ->submitForm([], 'Drag & drop');

  // Make sure that the second paragraph field is still displayed normally by
  // checking that it displays the edit button, as it is closed by default.
  // @todo: Introduce a global drag and drop mode?
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_second_0_subform_paragraphs_container_paragraphs_0_edit');

  // 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([], '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')), 2);
  $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());
  $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');

  // Assert the second field.
  $this
    ->assertEquals(count($node
    ->get('field_paragraphs_second')), 1);
  $this
    ->assertEquals($node
    ->get('field_paragraphs_second')
    ->get(0)->target_id, $paragraph_second
    ->id());
  $paragraph_second = $node
    ->get('field_paragraphs_second')
    ->get(0)->entity;
  $this
    ->assertEquals(count($paragraph_second
    ->get('paragraphs_container_paragraphs')), 1);
  $this
    ->assertEquals($paragraph_second
    ->get('paragraphs_container_paragraphs')
    ->get(0)->target_id, $text_paragraph_2
    ->id());
}