public function ParagraphsUiTest::testEmptyRequiredField

Same name in this branch
  1. 8.x-1.x paragraphs/tests/src/Functional/WidgetStable/ParagraphsUiTest.php \Drupal\Tests\paragraphs\Functional\WidgetStable\ParagraphsUiTest::testEmptyRequiredField()
  2. 8.x-1.x paragraphs/tests/src/Functional/WidgetLegacy/ParagraphsUiTest.php \Drupal\Tests\paragraphs\Functional\WidgetLegacy\ParagraphsUiTest::testEmptyRequiredField()

Tests displaying an error message a required paragraph field that is empty.

File

paragraphs/tests/src/Functional/WidgetStable/ParagraphsUiTest.php, line 72

Class

ParagraphsUiTest
Tests the Paragraphs user interface.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetStable

Code

public function testEmptyRequiredField() {
  $admin_user = $this
    ->drupalCreateUser([
    'administer node fields',
    'administer paragraph form display',
    'administer node form display',
    'create paragraphed_content_demo content',
    'edit any paragraphed_content_demo content',
  ]);
  $this
    ->drupalLogin($admin_user);

  // Add required field to paragraphed content type.
  $bundle_path = 'admin/structure/types/manage/paragraphed_content_demo';
  $field_title = 'Content Test';
  $field_type = 'field_ui:entity_reference_revisions:paragraph';
  $field_edit = [
    'required' => TRUE,
  ];
  $this
    ->fieldUIAddNewField($bundle_path, 'content', $field_title, $field_type, [], $field_edit);
  $form_display_edit = [
    'fields[field_content][type]' => 'paragraphs',
  ];
  $this
    ->drupalGet($bundle_path . '/form-display');
  $this
    ->submitForm($form_display_edit, 'Save');

  // Attempt to create a paragraphed node with an empty required field.
  $title = 'Empty';
  $this
    ->drupalGet('node/add/paragraphed_content_demo');
  $this
    ->submitForm([
    'title[0][value]' => $title,
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains($field_title . ' field is required');

  // Attempt to create a paragraphed node with only a paragraph in the
  // "remove" mode in the required field.
  $title = 'Remove all items';
  $this
    ->drupalGet('node/add/paragraphed_content_demo');
  $this
    ->submitForm([], 'field_content_text_image_add_more');
  $this
    ->submitForm([], 'field_content_0_remove');
  $this
    ->assertSession()
    ->pageTextNotContains($field_title . ' field is required');
  $this
    ->submitForm([
    'title[0][value]' => $title,
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains($field_title . ' field is required');

  // Attempt to create a paragraphed node with a valid paragraph and a
  // removed paragraph.
  $title = 'Valid Removal';
  $this
    ->drupalGet('node/add/paragraphed_content_demo');
  $this
    ->submitForm([], 'field_content_text_image_add_more');
  $this
    ->submitForm([], 'field_content_text_image_add_more');
  $this
    ->submitForm([], 'field_content_1_remove');
  $this
    ->assertSession()
    ->pageTextNotContains($field_title . ' field is required');
  $this
    ->submitForm([
    'title[0][value]' => $title,
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextNotContains($field_title . ' field is required');
}