function EntityAutocompleteElementFormTest::testInvalidEntityAutocompleteElement

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php \Drupal\KernelTests\Core\Entity\Element\EntityAutocompleteElementFormTest::testInvalidEntityAutocompleteElement()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php \Drupal\KernelTests\Core\Entity\Element\EntityAutocompleteElementFormTest::testInvalidEntityAutocompleteElement()
  3. 11.x 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 274

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 a 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->assertEqual($form_state->getErrors()['single'], t('There are no entities matching "%value".', [
        '%value' => 'single - non-existent label',
    ]));
    // Test 'single' with a 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->assertEqual($form_state->getErrors()['single'], t('The referenced entity (%type: %id) does not exist.', [
        '%type' => 'entity_test',
        '%id' => 42,
    ]));
    // 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->assertEqual($form_state->getErrors()['single_no_validate'], t('There are no entities matching "%value".', [
        '%value' => 'single - non-existent label',
    ]));
    $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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.