You are here

public function EntityAutocompleteElementFormTest::testInvalidEntityAutocompleteElement in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php \Drupal\KernelTests\Core\Entity\Element\EntityAutocompleteElementFormTest::testInvalidEntityAutocompleteElement()
  2. 9 core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php \Drupal\KernelTests\Core\Entity\Element\EntityAutocompleteElementFormTest::testInvalidEntityAutocompleteElement()

Tests invalid entries in the EntityAutocomplete Form API element.

File

core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php, line 285

Class

EntityAutocompleteElementFormTest
Tests the EntityAutocomplete Form API element.

Namespace

Drupal\KernelTests\Core\Entity\Element

Code

public function testInvalidEntityAutocompleteElement() {
  $form_builder = $this->container
    ->get('form_builder');

  // Test 'single' with an entity label that doesn't exist
  $form_state = (new FormState())
    ->setValues([
    'single' => 'single - non-existent label',
  ]);
  $form_builder
    ->submitForm($this, $form_state);
  $this
    ->assertCount(1, $form_state
    ->getErrors());
  $this
    ->assertEquals(t('There are no test entity entities matching "%value".', [
    '%value' => 'single - non-existent label',
  ]), $form_state
    ->getErrors()['single']);

  // Test 'single' with an entity ID that doesn't exist.
  $form_state = (new FormState())
    ->setValues([
    'single' => 'single - non-existent label (42)',
  ]);
  $form_builder
    ->submitForm($this, $form_state);
  $this
    ->assertCount(1, $form_state
    ->getErrors());
  $this
    ->assertEquals(t('The referenced entity (%type: %id) does not exist.', [
    '%type' => 'entity_test',
    '%id' => 42,
  ]), $form_state
    ->getErrors()['single']);

  // Do the same tests as above but on an element with '#validate_reference'
  // set to FALSE.
  $form_state = (new FormState())
    ->setValues([
    'single_no_validate' => 'single - non-existent label',
    'single_autocreate_no_validate' => 'single - autocreate non-existent label',
  ]);
  $form_builder
    ->submitForm($this, $form_state);

  // The element without 'autocreate' support still has to emit a warning when
  // the input doesn't end with an entity ID enclosed in parentheses.
  $this
    ->assertCount(1, $form_state
    ->getErrors());
  $this
    ->assertEquals(t('There are no test entity entities matching "%value".', [
    '%value' => 'single - non-existent label',
  ]), $form_state
    ->getErrors()['single_no_validate']);
  $form_state = (new FormState())
    ->setValues([
    'single_no_validate' => 'single - non-existent label (42)',
    'single_autocreate_no_validate' => 'single - autocreate non-existent label (43)',
  ]);
  $form_builder
    ->submitForm($this, $form_state);

  // The input is complete (i.e. contains an entity ID at the end), no errors
  // are triggered.
  $this
    ->assertCount(0, $form_state
    ->getErrors());
}