public function ParagraphsDragAndDropModeTest::testChangeParagraphParentWeight

Tests moving a paragraph from a container to top-level.

File

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

Class

ParagraphsDragAndDropModeTest
Tests the drag and drop mode of paragraphs.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetStable

Code

public function testChangeParagraphParentWeight() {

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

  // Add test content 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 the first text paragraph to the node as its parent.
  // This also requires an update of the path of the second paragraph in the
  // container as that moves down as well as the weight to prevent multiple
  // identical weights.
  $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][1][paragraphs_container_paragraphs');
  $assert_session
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][_weight]')
    ->setValue(1);
  $assert_session
    ->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][1][_weight]')
    ->setValue(0);
  $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(count($node
    ->get('field_paragraphs')), 2);
  $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($text_paragraph_1
    ->get('parent_id')->value, $node
    ->id());
  $this
    ->assertEquals($text_paragraph_1
    ->get('parent_type')->value, 'node');
  $this
    ->assertEquals($node
    ->get('field_paragraphs')
    ->get(1)->target_id, $paragraph
    ->id());
  $paragraph = $node
    ->get('field_paragraphs')
    ->get(1)->entity;
  $this
    ->assertEquals($paragraph
    ->get('parent_id')->value, $node
    ->id());
  $this
    ->assertEquals($paragraph
    ->get('parent_type')->value, 'node');
  $this
    ->assertEquals(count($paragraph
    ->get('paragraphs_container_paragraphs')), 1);
  $this
    ->assertEquals($paragraph
    ->get('paragraphs_container_paragraphs')->target_id, $text_paragraph_2
    ->id());
  $text_paragraph_2 = $paragraph
    ->get('paragraphs_container_paragraphs')->entity;
  $this
    ->assertEquals($text_paragraph_2
    ->get('parent_id')->value, $paragraph
    ->id());
  $this
    ->assertEquals($text_paragraph_2
    ->get('parent_type')->value, 'paragraph');

  // If the library does not exist, test that the button is not visible
  // without forcing it. This can not be tested if the library exists.
  // @todo: Implement a library alter in a test module to do this?
  $library_discovery = \Drupal::service('library.discovery');
  $library = $library_discovery
    ->getLibraryByName('paragraphs', 'paragraphs-dragdrop');
  if (!$library) {
    \Drupal::state()
      ->set('paragraphs_test_dragdrop_force_show', FALSE);
    $this
      ->drupalGet('/node/' . $node
      ->id() . '/edit');
    $this
      ->assertSession()
      ->buttonNotExists('Drag & drop');
  }
}