You are here

public function ParagraphsHeaderActionsTest::testHeaderActions in Paragraphs 8

Tests header actions.

File

tests/src/Functional/WidgetStable/ParagraphsHeaderActionsTest.php, line 27

Class

ParagraphsHeaderActionsTest
Tests collapse all button.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetStable

Code

public function testHeaderActions() {

  // To test with a single header action, ensure the drag and drop action is
  // shown, even without the library.
  \Drupal::state()
    ->set('paragraphs_test_dragdrop_force_show', TRUE);
  $this
    ->addParagraphedContentType('paragraphed_test');
  $this
    ->loginAsAdmin([
    'create paragraphed_test content',
    'edit any paragraphed_test content',
  ]);

  // Add a Paragraph type.
  $paragraph_type = 'text_paragraph';
  $this
    ->addParagraphsType($paragraph_type);

  // Add a text field to the text_paragraph type.
  static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);

  // Add 2 paragraphs and check for Collapse/Edit all button.
  $this
    ->drupalGet('node/add/paragraphed_test');
  $this
    ->assertSession()
    ->responseNotContains('field_paragraphs_collapse_all');
  $this
    ->assertSession()
    ->responseNotContains('field_paragraphs_edit_all');
  $this
    ->assertSession()
    ->responseContains('field_paragraphs_dragdrop_mode');

  // Ensure there is only a single table row.
  $table_rows = $this
    ->xpath('//table[contains(@class, :class)]/tbody/tr', [
    ':class' => 'field-multiple-table',
  ]);
  $this
    ->assertEquals(1, count($table_rows));

  // Add second paragraph and check for Collapse/Edit all button.
  $this
    ->submitForm([], 'field_paragraphs_text_paragraph_add_more');
  $this
    ->assertSession()
    ->responseContains('field_paragraphs_collapse_all');
  $this
    ->assertSession()
    ->responseContains('field_paragraphs_edit_all');
  $edit = [
    'field_paragraphs[0][subform][field_text][0][value]' => 'First text',
    'field_paragraphs[1][subform][field_text][0][value]' => 'Second text',
  ];
  $this
    ->submitForm($edit, 'Collapse all');

  // Checks that after collapsing all we can edit again these paragraphs.
  $this
    ->assertSession()
    ->responseContains('field_paragraphs_0_edit');
  $this
    ->assertSession()
    ->responseContains('field_paragraphs_1_edit');

  // Test Edit all button.
  $this
    ->submitForm([], 'field_paragraphs_edit_all');
  $this
    ->assertSession()
    ->responseContains('field_paragraphs_0_collapse');
  $this
    ->assertSession()
    ->responseContains('field_paragraphs_1_collapse');
  $edit = [
    'title[0][value]' => 'Test',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('paragraphed_test Test has been created.');
  $node = $this
    ->getNodeByTitle('Test');
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->clickLink('Edit');
  $this
    ->assertSession()
    ->pageTextNotContains('No Paragraph added yet.');

  // Add and remove another paragraph.
  $this
    ->submitForm([], 'field_paragraphs_text_paragraph_add_more');
  $edit = [
    'field_paragraphs[2][subform][field_text][0][value]' => 'Third text',
  ];
  $this
    ->submitForm($edit, 'field_paragraphs_2_remove');

  // Check that pressing "Collapse all" does not restore the removed
  // paragraph.
  $this
    ->submitForm([], 'field_paragraphs_edit_all');
  $this
    ->assertSession()
    ->pageTextContains('First text');
  $this
    ->assertSession()
    ->pageTextContains('Second text');
  $this
    ->assertSession()
    ->pageTextNotContains('Third text');

  // Check that pressing "Edit all" does not restore the removed paragraph,
  // either.
  $this
    ->submitForm([], 'field_paragraphs_collapse_all');
  $this
    ->assertSession()
    ->pageTextContains('First text');
  $this
    ->assertSession()
    ->pageTextContains('Second text');
  $this
    ->assertSession()
    ->pageTextNotContains('Third text');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_collapse_all');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_edit_all');
  $this
    ->submitForm([], 'Save');

  // Check that the drag and drop button is present when there is a paragraph
  // and that it is not shown when the paragraph is deleted.
  $this
    ->drupalGet('node/add/paragraphed_test');
  $this
    ->assertSession()
    ->responseContains('name="field_paragraphs_dragdrop_mode"');
  $this
    ->submitForm([], 'field_paragraphs_0_remove');
  $this
    ->assertSession()
    ->responseNotContains('name="field_paragraphs_dragdrop_mode"');

  // Disable show multiple actions.
  $this
    ->drupalGet('admin/structure/types/manage/paragraphed_test/form-display');
  $this
    ->submitForm([], 'field_paragraphs_settings_edit');
  $this
    ->submitForm([
    'fields[field_paragraphs][settings_edit_form][settings][features][collapse_edit_all]' => FALSE,
  ], 'Update');
  $this
    ->submitForm([], 'Save');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');

  // Check that the collapse/edit all actions are not present.
  $this
    ->assertSession()
    ->buttonNotExists('field_paragraphs_collapse_all');
  $this
    ->assertSession()
    ->buttonNotExists('field_paragraphs_edit_all');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[0][subform][field_text][0][value]', 'First text');

  // Enable show "Collapse / Edit all" actions.
  $this
    ->drupalGet('admin/structure/types/manage/paragraphed_test/form-display');
  $this
    ->submitForm([], 'field_paragraphs_settings_edit');
  $this
    ->submitForm([
    'fields[field_paragraphs][settings_edit_form][settings][features][collapse_edit_all]' => TRUE,
  ], 'Update');
  $this
    ->submitForm([], 'Save');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');

  // Check that the collapse/edit all actions are present.
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_collapse_all');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_edit_all');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[0][subform][field_text][0][value]', 'First text');
}