You are here

public function EntityAutocompleteElementFormTest::testInvalidEntityAutocompleteElement in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/Element/EntityAutocompleteElementFormTest.php \Drupal\system\Tests\Entity\Element\EntityAutocompleteElementFormTest::testInvalidEntityAutocompleteElement()

Tests invalid entries in the EntityAutocomplete Form API element.

File

core/modules/system/src/Tests/Entity/Element/EntityAutocompleteElementFormTest.php, line 244
Contains \Drupal\system\Tests\Entity\Element\EntityAutocompleteElementFormTest.

Class

EntityAutocompleteElementFormTest
Tests the EntityAutocomplete Form API element.

Namespace

Drupal\system\Tests\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
    ->assertEqual(count($form_state
    ->getErrors()), 1);
  $this
    ->assertEqual($form_state
    ->getErrors()['single'], t('There are no entities matching "%value".', array(
    '%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
    ->assertEqual(count($form_state
    ->getErrors()), 1);
  $this
    ->assertEqual($form_state
    ->getErrors()['single'], t('The referenced entity (%type: %id) does not exist.', array(
    '%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
    ->assertEqual(count($form_state
    ->getErrors()), 1);
  $this
    ->assertEqual($form_state
    ->getErrors()['single_no_validate'], t('There are no entities matching "%value".', array(
    '%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
    ->assertEqual(count($form_state
    ->getErrors()), 0);
}