public function ParagraphsWidgetButtonsTest::testClosedExtendNestedEditMode

Tests the "Closed, show nested" edit mode.

File

paragraphs/tests/src/Functional/ParagraphsWidgetButtonsTest.php, line 205

Class

ParagraphsWidgetButtonsTest
Tests paragraphs stable widget buttons.

Namespace

Drupal\Tests\paragraphs\Functional

Code

public function testClosedExtendNestedEditMode() {
  $this
    ->addParagraphedContentType('paragraphed_test');
  $permissions = [
    'administer content types',
    'administer node fields',
    'administer paragraphs types',
    'administer node form display',
    'administer paragraph fields',
    'administer paragraph form display',
    'create paragraphed_test content',
    'edit any paragraphed_test content',
  ];
  $this
    ->loginAsAdmin($permissions, TRUE);

  // Add a container Paragraph type.
  $this
    ->addParagraphsType('container_paragraph');
  $this
    ->addParagraphsField('container_paragraph', 'field_paragraphs', 'paragraph', 'paragraphs');

  // Set the edit mode to "Closed".
  $settings = [
    'edit_mode' => 'closed',
    'closed_mode' => 'summary',
  ];
  $this
    ->setParagraphsWidgetSettings('container_paragraph', 'field_paragraphs', $settings, 'paragraphs', 'paragraph');

  // Add a text Paragraph type.
  $this
    ->addParagraphsType('text_paragraph');
  $this
    ->addFieldtoParagraphType('text_paragraph', 'field_text', 'text_long');

  // Set the edit mode to "Closed, show nested".
  $settings = [
    'edit_mode' => 'closed_expand_nested',
    'closed_mode' => 'summary',
  ];
  $this
    ->setParagraphsWidgetSettings('paragraphed_test', 'field_paragraphs', $settings);

  // Check that the paragraphs field uses the stable widget on the
  // paragraphed_test content type.
  $this
    ->drupalGet('admin/structure/types/manage/paragraphed_test/form-display');
  $option = $this
    ->assertSession()
    ->optionExists('fields[field_paragraphs][type]', 'paragraphs');
  $this
    ->assertTrue($option
    ->isSelected());

  // Check if the edit mode is set to "Closed, show nested".
  $this
    ->assertSession()
    ->pageTextContains('Edit mode: Closed, show nested');

  // Check that the paragraphs field uses the stable widget on the
  // container_paragraph paragraph type.
  $this
    ->drupalGet('admin/structure/paragraphs_type/container_paragraph/form-display');
  $option = $this
    ->assertSession()
    ->optionExists('fields[field_paragraphs][type]', 'paragraphs');
  $this
    ->assertTrue($option
    ->isSelected());

  // Check if the edit mode is set to "Closed".
  $this
    ->assertSession()
    ->pageTextContains('Edit mode: Closed');

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

  // Create a container paragraph referencing to the text paragraph.
  $paragraph_1 = Paragraph::create([
    'type' => 'container_paragraph',
    'field_paragraphs' => [
      $text_paragraph_1,
    ],
  ]);
  $paragraph_1
    ->save();

  // Create a second text paragraph.
  $text_paragraph_2 = Paragraph::create([
    'type' => 'text_paragraph',
    'field_text' => [
      'value' => 'Test text 2',
      'format' => 'plain_text',
    ],
  ]);
  $text_paragraph_2
    ->save();

  // Create a second container paragraph referencing to the second text paragraph
  // and the first container paragraph.
  $paragraph_2 = Paragraph::create([
    'type' => 'container_paragraph',
    'field_paragraphs' => [
      $text_paragraph_2,
      $paragraph_1,
    ],
  ]);
  $paragraph_2
    ->save();

  // Create a third text paragraph.
  $text_paragraph_3 = Paragraph::create([
    'type' => 'text_paragraph',
    'field_text' => [
      'value' => 'Test text 3',
      'format' => 'plain_text',
    ],
  ]);
  $text_paragraph_3
    ->save();

  // Create a node referencing to the second container paragraph and the third
  // text paragraph.
  $node = Node::create([
    'type' => 'paragraphed_test',
    'title' => 'Paragraphs Test',
    'field_paragraphs' => [
      $paragraph_2,
      $text_paragraph_3,
    ],
  ]);
  $node
    ->save();

  // Edit the test node.
  $this
    ->drupalGet('/node/' . $node
    ->id() . '/edit');

  // Check if the top level container paragraph is open and the text paragraph
  // is closed.
  $this
    ->checkParagraphInMode('field_paragraphs_0', 'edit');
  $this
    ->checkParagraphInMode('field_paragraphs_1', 'closed');

  // Check if the nested paragraphs are closed.
  $this
    ->checkParagraphInMode('field_paragraphs_0_subform_field_paragraphs_0', 'closed');
  $this
    ->checkParagraphInMode('field_paragraphs_0_subform_field_paragraphs_1', 'closed');

  // Change the edit mode to "Closed, show nested" on the container_paragraph type.
  $settings = [
    'edit_mode' => 'closed_expand_nested',
  ];

  // Check if the edit mode is changed.
  $this
    ->setParagraphsWidgetSettings('container_paragraph', 'field_paragraphs', $settings, 'paragraphs', 'paragraph');
  $this
    ->drupalGet('admin/structure/paragraphs_type/container_paragraph/form-display');
  $this
    ->assertSession()
    ->pageTextContains('Edit mode: Closed, show nested');

  // Edit the test node agian.
  $this
    ->drupalGet('/node/' . $node
    ->id() . '/edit');

  // Check if the nested container paragraph is open after the change.
  $this
    ->checkParagraphInMode('field_paragraphs_0_subform_field_paragraphs_1', 'edit');
}