You are here

public function ContentEntityFormFieldValidationFilteringTest::testFieldWidgetsWithLimitedValidationErrors in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/FunctionalTests/Entity/ContentEntityFormFieldValidationFilteringTest.php \Drupal\FunctionalTests\Entity\ContentEntityFormFieldValidationFilteringTest::testFieldWidgetsWithLimitedValidationErrors()

Tests field widgets with #limit_validation_errors.

File

core/tests/Drupal/FunctionalTests/Entity/ContentEntityFormFieldValidationFilteringTest.php, line 131

Class

ContentEntityFormFieldValidationFilteringTest
Tests field validation filtering on content entity forms.

Namespace

Drupal\FunctionalTests\Entity

Code

public function testFieldWidgetsWithLimitedValidationErrors() {
  $assert_session = $this
    ->assertSession();
  $this
    ->drupalGet($this->entityTypeId . '/add');

  // The 'Test multiple' field is the only multi-valued field in the form, so
  // try to add a new item for it. This tests the '#limit_validation_errors'
  // property set by \Drupal\Core\Field\WidgetBase::formMultipleElements().
  $assert_session
    ->elementsCount('css', 'div#edit-test-multiple-wrapper div.form-type-textfield input', 1);
  $this
    ->drupalPostForm(NULL, [], 'Add another item');
  $assert_session
    ->elementsCount('css', 'div#edit-test-multiple-wrapper div.form-type-textfield input', 2);

  // Now try to upload a file. This tests the '#limit_validation_errors'
  // property set by
  // \Drupal\file\Plugin\Field\FieldWidget\FileWidget::process().
  $text_file = current($this
    ->getTestFiles('text'));
  $edit = [
    'files[test_file_0]' => \Drupal::service('file_system')
      ->realpath($text_file->uri),
  ];
  $assert_session
    ->elementNotExists('css', 'input#edit-test-file-0-remove-button');
  $this
    ->drupalPostForm(NULL, $edit, 'Upload');
  $assert_session
    ->elementExists('css', 'input#edit-test-file-0-remove-button');

  // Make the 'Test multiple' field required and check that adding another
  // item throws a validation error.
  $field_config = FieldConfig::loadByName($this->entityTypeId, $this->entityTypeId, $this->fieldNameMultiple);
  $field_config
    ->setRequired(TRUE);
  $field_config
    ->save();
  $this
    ->drupalPostForm($this->entityTypeId . '/add', [], 'Add another item');
  $assert_session
    ->pageTextContains('Test multiple (value 1) field is required.');

  // Check that saving the form without entering any value for the required
  // field still throws the proper validation errors.
  $this
    ->drupalPostForm(NULL, [], 'Save');
  $assert_session
    ->pageTextContains('Test single field is required.');
  $assert_session
    ->pageTextContains('Test multiple (value 1) field is required.');
}