1 entityreference.admin.test public EntityReferenceAdminTestCase::testFieldAdminHandler()

File

core/modules/entityreference/tests/entityreference.admin.test, line 57
Contains EntityReferenceHandlersTestCase

Class

EntityReferenceAdminTestCase
Test for Entity Reference admin UI.

Code

public function testFieldAdminHandler() {
  $bundle_path = 'admin/structure/types/manage/' . $this->hyphen_type;

  // First step: 'Add new field' on the 'Manage fields' page.
  $this->backdropPost($bundle_path . '/fields', array(
    'fields[_add_new_field][label]' => 'Test label',
    'fields[_add_new_field][field_name]' => 'test',
    'fields[_add_new_field][type]' => 'entityreference',
    'fields[_add_new_field][widget_type]' => 'entityreference_autocomplete',
  ), t('Save'));

  // Node should be selected by default.
  $this->assertFieldByName('field[settings][target_type]', 'node');
  // The base handler should be selected by default.
  $this->assertFieldByName('field[settings][handler]', 'base');

  // The base handler settings should be displayed.
  $entity_type = 'node';
  $entity_info = entity_get_info($entity_type);
  foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
    $this->assertFieldByName('field[settings][handler_settings][target_bundles][' . $bundle_name . ']');
  }

  // Test the sort settings.
  $options = array('none', 'property', 'field');
  $this->assertFieldSelectOptions('field[settings][handler_settings][sort][type]', $options);
  // Option 0: no sort.
  $this->assertFieldByName('field[settings][handler_settings][sort][type]', 'none');
  $this->assertNoFieldByName('field[settings][handler_settings][sort][property]');
  $this->assertNoFieldByName('field[settings][handler_settings][sort][field]');
  $this->assertNoFieldByName('field[settings][handler_settings][sort][direction]');
  // Option 1: sort by property.
  $this->backdropPostAJAX(NULL, array('field[settings][handler_settings][sort][type]' => 'property'), 'field[settings][handler_settings][sort][type]');
  $this->assertFieldByName('field[settings][handler_settings][sort][property]', '');
  $this->assertNoFieldByName('field[settings][handler_settings][sort][field]');
  $this->assertFieldByName('field[settings][handler_settings][sort][direction]', 'ASC');
  // Option 2: sort by field.
  $this->backdropPostAJAX(NULL, array('field[settings][handler_settings][sort][type]' => 'field'), 'field[settings][handler_settings][sort][type]');
  $this->assertNoFieldByName('field[settings][handler_settings][sort][property]');
  $this->assertFieldByName('field[settings][handler_settings][sort][field]', '');
  $this->assertFieldByName('field[settings][handler_settings][sort][direction]', 'ASC');
  // Set back to no sort.
  $this->backdropPostAJAX(NULL, array('field[settings][handler_settings][sort][type]' => 'none'), 'field[settings][handler_settings][sort][type]');

  // Second step: 'Instance settings' form.
  $this->backdropPost(NULL, array(), t('Save field settings'));

  // Third step: confirm.
  $this->backdropPost(NULL, array(), t('Save settings'));

  // Check that the field appears in the overview form.
  $this->assertFieldByXPath('//input[@name="fields[field_test][parent_wrapper][hidden_name]"]', 'field_test', 'Field was created and appears in the overview page.');
}