You are here

public function ParagraphsWidgetButtonsTest::testButtonsVisibility in Paragraphs 8

Tests if buttons are present for each widget mode.

File

tests/src/Functional/WidgetStable/ParagraphsWidgetButtonsTest.php, line 118

Class

ParagraphsWidgetButtonsTest
Tests paragraphs stable widget buttons.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetStable

Code

public function testButtonsVisibility() {
  $this
    ->addParagraphedContentType('paragraphed_test');
  $this
    ->loginAsAdmin([
    'create paragraphed_test content',
    'edit any paragraphed_test content',
    'administer content translation',
    'administer languages',
    'create content translations',
    'translate any entity',
  ]);

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

  // Add a text field to the text_paragraph type.
  static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
  $edit = [
    'fields[field_paragraphs][type]' => 'paragraphs',
  ];
  $this
    ->drupalGet('admin/structure/types/manage/paragraphed_test/form-display');
  $this
    ->submitForm($edit, 'Save');
  $this
    ->drupalGet('node/add/paragraphed_test');
  $this
    ->submitForm([], 'field_paragraphs_text_paragraph_add_more');

  // Create a node with a Paragraph.
  $text = 'recognizable_text';
  $edit = [
    'title[0][value]' => 'paragraphs_mode_test',
    'field_paragraphs[0][subform][field_text][0][value]' => $text,
  ];
  $this
    ->submitForm([], 'field_paragraphs_text_paragraph_add_more');
  $this
    ->submitForm($edit, 'Save');
  $node = $this
    ->drupalGetNodeByTitle('paragraphs_mode_test');

  // Checking visible buttons on "Open" mode.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_0_collapse');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_0_remove');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_0_duplicate');

  // Checking visible buttons on "Closed" mode.
  $this
    ->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_0_edit');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_0_remove');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_0_duplicate');

  // Checking visible buttons on "Preview" mode.
  $this
    ->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_0_edit');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_0_remove');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_0_duplicate');

  // Checking always show collapse and edit actions.
  $this
    ->addParagraphsType('nested_paragraph');
  static::fieldUIAddNewField('admin/structure/paragraphs_type/nested_paragraph', 'nested', 'Nested', 'field_ui:entity_reference_revisions:paragraph', [
    'settings[target_type]' => 'paragraph',
    'cardinality' => '-1',
  ], []);
  $this
    ->drupalGet('admin/structure/paragraphs_type/nested_paragraph/form-display');
  $edit = [
    'fields[field_nested][type]' => 'paragraphs',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->submitForm([], 'field_paragraphs_nested_paragraph_add_more');
  $this
    ->submitForm([], 'field_paragraphs_2_subform_field_nested_nested_paragraph_add_more');

  // Collapse is present on each nesting level.
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_2_collapse');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_2_subform_field_nested_0_collapse');

  // Tests hook_paragraphs_widget_actions_alter.
  $this
    ->drupalGet('node/add/paragraphed_test');
  $this
    ->submitForm([], 'Add text');
  $this
    ->assertSession()
    ->buttonNotExists('edit-field-paragraphs-0-top-links-test-button');
  \Drupal::state()
    ->set('paragraphs_test_dropbutton', TRUE);
  $this
    ->drupalGet('node/add/paragraphed_test');
  $this
    ->submitForm([], 'Add text');
  $this
    ->assertSession()
    ->buttonNotExists('edit-field-paragraphs-0-top-links-test-button');
  ConfigurableLanguage::createFromLangcode('sr')
    ->save();

  // Enable translation for test content.
  $this
    ->drupalGet('admin/config/regional/content-language');
  $edit = [
    'entity_types[node]' => TRUE,
    'settings[node][paragraphed_test][translatable]' => TRUE,
  ];
  $this
    ->submitForm($edit, 'Save configuration');

  // Check that operation is hidden during translation.
  $this
    ->drupalGet('sr/node/' . $node
    ->id() . '/translations/add/en/sr');
  $this
    ->assertSession()
    ->buttonNotExists('edit-field-paragraphs-1-top-actions-dropdown-actions-test-button');

  // Check that "Duplicate" is hidden during translation.
  $this
    ->assertSession()
    ->buttonNotExists('field_paragraphs_0_duplicate');
  $this
    ->assertSession()
    ->responseNotContains('value="Duplicate"');
}