You are here

public function ParagraphsWidgetButtonsTest::testButtonsVisibilityException in Paragraphs 8

Tests buttons visibility exception.

File

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

Class

ParagraphsWidgetButtonsTest
Tests paragraphs stable widget buttons.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetStable

Code

public function testButtonsVisibilityException() {

  // Hide the button if field is required, cardinality is one and just one
  // paragraph type is allowed.
  $content_type_name = 'paragraphed_test';
  $paragraphs_field_name = 'field_paragraphs';
  $widget_type = 'paragraphs';
  $entity_type = 'node';

  // Create the content type.
  $node_type = NodeType::create([
    'type' => $content_type_name,
    'name' => $content_type_name,
  ]);
  $node_type
    ->save();
  $field_storage = FieldStorageConfig::loadByName($content_type_name, $paragraphs_field_name);
  if (!$field_storage) {

    // Add a paragraphs field.
    $field_storage = FieldStorageConfig::create([
      'field_name' => $paragraphs_field_name,
      'entity_type' => $entity_type,
      'type' => 'entity_reference_revisions',
      'cardinality' => '1',
      'settings' => [
        'target_type' => 'paragraph',
      ],
    ]);
    $field_storage
      ->save();
  }
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => $content_type_name,
    'required' => TRUE,
    'settings' => [
      'handler' => 'default:paragraph',
      'handler_settings' => [
        'target_bundles' => NULL,
      ],
    ],
  ]);
  $field
    ->save();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');
  $form_display = $display_repository
    ->getFormDisplay($entity_type, $content_type_name)
    ->setComponent($paragraphs_field_name, [
    'type' => $widget_type,
  ]);
  $form_display
    ->save();
  $view_display = $display_repository
    ->getViewDisplay($entity_type, $content_type_name)
    ->setComponent($paragraphs_field_name, [
    'type' => 'entity_reference_revisions_entity_view',
  ]);
  $view_display
    ->save();
  $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);

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

  // Checking hidden button on "Open" mode.
  $this
    ->drupalGet('node/add/paragraphed_test');
  $this
    ->assertSession()
    ->buttonNotExists('field_paragraphs_0_remove');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[0][subform][field_text][0][value]', '');

  // Checking hidden button on "Closed" mode.
  $this
    ->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
  $this
    ->drupalGet('node/add/paragraphed_test');
  $this
    ->assertSession()
    ->buttonNotExists('field_paragraphs_0_remove');
  $this
    ->assertSession()
    ->fieldValueEquals('field_paragraphs[0][subform][field_text][0][value]', '');

  // Checking that the "Duplicate" button is not shown when cardinality is 1.
  $this
    ->drupalGet('node/add/paragraphed_test');
  $this
    ->assertSession()
    ->buttonNotExists('field_paragraphs_0_duplicate');
}