1 entityreference.form.test public EntityReferenceFormTestCase::testAutocompleteValidationWithGenericSelectionHandler()

Test that the Generic autocomplete widget validates the value properly.

File

core/modules/entityreference/tests/entityreference.form.test, line 30
Contains EntityReferenceFormTestCase.

Class

EntityReferenceFormTestCase
Test for Entity Reference form.

Code

public function testAutocompleteValidationWithGenericSelectionHandler() {
  $type_referenced = $this->backdropCreateContentType();
  $type_referencing = $this->backdropCreateContentType();

  $field_name = 'field_' . $type_referenced->type;
  $field = array(
    'field_name' => $field_name,
    'settings' => array(
      'handler' => 'base',
      'target_type' => 'node',
      'handler_settings' => array(
        'target_bundles' => array($type_referenced->type),
      ),
    ),
  );
  $field_instance = array(
    'field_name' => $field_name,
    'bundle' => $type_referencing->type,
  );
  $this->createEntityReferenceFieldForNode($field, $field_instance);

  $node = $this->backdropCreateNode(array('type' => $type_referenced->type));
  $title_valid = $node->title;
  $this->postNodeFormWithEntityReference($type_referencing, $field_name, $title_valid);
  $this->assertNoText('There are no entities matching "' . $title_valid . '"', 
  'No validation error occurs for a valid title.');

  $title_invalid = $this->randomName();
  $this->postNodeFormWithEntityReference($type_referencing, $field_name, $title_invalid);
  $this->assertText('There are no entities matching "' . $title_invalid . '"', 
  'A validation error occurs for an invalid title.');

  $title_many_nodes_has = $this->randomName();
  for ($i = 0; $i < 6; $i++) {
    $node = $this->backdropCreateNode(array(
      'type' => $type_referenced->type,
      'title' => $title_many_nodes_has,
    ));
  }
  $this->postNodeFormWithEntityReference($type_referencing, $field_name, $title_many_nodes_has);
  $this->assertText('Many entities are called ' . $title_many_nodes_has . '.', 
  'A validation error occurs for a title shared by too many nodes.');

  $title_several_nodes_has = $this->randomName();
  for ($i = 0; $i < 2; $i++) {
    $node = $this->backdropCreateNode(array(
      'type' => $type_referenced->type,
      'title' => $title_several_nodes_has,
    ));
  }
  $this->postNodeFormWithEntityReference($type_referencing, $field_name, $title_several_nodes_has);
  $this->assertText('Multiple entities match this reference; ', 
  'A validation error occurs for a title shared by several nodes.');
}