You are here

public function ParagraphsDragAndDropModeTest::testChangeParagraphContainerMove in Paragraphs 8

Tests moving a paragraph from one container to another.

File

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

Class

ParagraphsDragAndDropModeTest
Tests the drag and drop mode of paragraphs.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetStable

Code

public function testChangeParagraphContainerMove() {

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

  // Create container that contains the first two 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();

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

  // Ensure that the summary is displayed correctly.
  $this
    ->assertSession()
    ->elementTextContains('css', '.paragraphs-dragdrop-wrapper li:nth-of-type(1)', 'Test text 1');
  $this
    ->assertSession()
    ->elementTextNotContains('css', '.paragraphs-dragdrop-wrapper li:nth-of-type(2)', 'Test text 1');
  $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');

  // Ensure the summary is displayed correctly for the collapsed paragraphs.
  $this
    ->assertSession()
    ->elementTextNotContains('css', '.field--name-field-paragraphs tbody tr:nth-of-type(1) .paragraph-summary', 'Test text 1');
  $this
    ->assertSession()
    ->elementTextContains('css', '.field--name-field-paragraphs tbody tr:nth-of-type(2) .paragraph-summary', 'Test text 1');

  // Ensure that the summary was updated correctly when going back to drag and
  // drop mode.
  $this
    ->submitForm([], 'Drag & drop');
  $this
    ->assertSession()
    ->elementTextNotContains('css', '.paragraphs-dragdrop-wrapper li:nth-of-type(1)', 'Test text 1');
  $this
    ->assertSession()
    ->elementTextContains('css', '.paragraphs-dragdrop-wrapper li:nth-of-type(2)', 'Test text 1');
  $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')), 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');
}