You are here

public function TextareaWidgetTest::testValidation in Typed Data API enhancements 8

@covers ::form @covers ::flagViolations

Overrides FormWidgetBrowserTestBase::testValidation

File

tests/src/Functional/TypedDataFormWidget/TextareaWidgetTest.php, line 86

Class

TextareaWidgetTest
Class TextInputWidgetTest.

Namespace

Drupal\Tests\typed_data\Functional\TypedDataFormWidget

Code

public function testValidation() {
  $context_definition = ContextDefinition::create('text')
    ->setLabel('Test text area')
    ->setDescription('Enter text, minimum 40 characters.');

  // Omitting the 'allowEmptyString' argument in Symfony 4+ (which is used in
  // Drupal 9.0+) gives a deprecation warning, but this option does not exist
  // in Symfony 3.4 (which is used in Drupal 8.8 and 8.9).
  // @see https://www.drupal.org/project/typed_data/issues/3161000
  if (version_compare(\Drupal::VERSION, '9.0', '>=')) {
    $context_definition
      ->addConstraint('Length', [
      'min' => 40,
      'allowEmptyString' => FALSE,
    ]);
  }
  else {
    $context_definition
      ->addConstraint('Length', [
      'min' => 40,
    ]);
  }
  $this->container
    ->get('state')
    ->set('typed_data_widgets.definition', $context_definition);
  $this
    ->drupalLogin($this
    ->createUser([], NULL, TRUE));
  $path = 'admin/config/user-interface/typed-data-widgets/' . $this->widget
    ->getPluginId();
  $this
    ->drupalGet($path);

  // Try to save with text that is too short.
  $this
    ->fillField('data[value]', $this
    ->randomString(20));
  $this
    ->pressButton('Submit');

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();
  $assert
    ->fieldExists('data[value]')
    ->hasClass('error');

  // Make sure the changes have not been saved.
  $this
    ->drupalGet($path);
  $assert
    ->fieldValueEquals('data[value]', $context_definition
    ->getDefaultValue());
}