You are here

public function EntityFormTest::testValidationHandlers in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/Entity/EntityFormTest.php \Drupal\Tests\system\Functional\Entity\EntityFormTest::testValidationHandlers()

Checks that validation handlers works as expected.

File

core/modules/system/tests/src/Functional/Entity/EntityFormTest.php, line 202

Class

EntityFormTest
Tests the entity form.

Namespace

Drupal\Tests\system\Functional\Entity

Code

public function testValidationHandlers() {

  /** @var \Drupal\Core\State\StateInterface $state */
  $state = $this->container
    ->get('state');

  // Check that from-level validation handlers can be defined and can alter
  // the form array.
  $state
    ->set('entity_test.form.validate.test', 'form-level');
  $this
    ->drupalGet('entity_test/add');
  $this
    ->submitForm([], 'Save');
  $this
    ->assertTrue($state
    ->get('entity_test.form.validate.result'), 'Form-level validation handlers behave correctly.');

  // Check that defining a button-level validation handler causes an exception
  // to be thrown.
  $state
    ->set('entity_test.form.validate.test', 'button-level');
  $this
    ->drupalGet('entity_test/add');
  $this
    ->submitForm([], 'Save');
  $this
    ->assertEquals('Drupal\\Core\\Entity\\EntityStorageException: Entity validation was skipped.', $state
    ->get('entity_test.form.save.exception'), 'Button-level validation handlers behave correctly.');
}