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

Test that the Views autocomplete widget validates the value properly.

File

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

Class

EntityReferenceFormTestCase
Test for Entity Reference form.

Code

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

  $field_name = 'field_' . $type_referenced->type;
  $field = array(
    'field_name' => $field_name,
    'settings' => array(
      "handler" => "views",
      "target_type" => "node",
      "handler_settings" => array(
        "view" => array(
          "view_name" => "test_entityreference_select_node",
          "display_name" => "entityreference_1",
          "args" => array(),
        ),
        "behaviors" => array(
          "views-select-list" => array(
            "status" => 0,
          ),
        ),
      ),
    ),
  );
  $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('No items found for ' . $field_name, 
  'No validation error occurs for a valid title.');

  $title_invalid = $this->randomName();
  $this->postNodeFormWithEntityReference($type_referencing, $field_name, $title_invalid);
  $this->assertText('No items found for ' . $field_name, 
  '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('Too many items found for ' . $field_name . '.', 
  '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 items found for ' . $field_name . ':', 
  'A validation error occurs for a title shared by several nodes.');
}