1 link.ui.test LinkUITest::testCRUDCreateFieldWithLinkType()

Tests the link type: "internal", "external", and "both" settings.

File

core/modules/link/tests/link.ui.test, line 304
Testing CRUD API in the browser.

Class

LinkUITest
Testing that users can not input bad URLs or labels

Code

function testCRUDCreateFieldWithLinkType() {
  $name = strtolower($this->randomName());
  $edit = array(
    'fields[_add_new_field][label]' => $name,
    'fields[_add_new_field][field_name]' => $name,
    'fields[_add_new_field][type]' => 'link_field',
    'fields[_add_new_field][widget_type]' => 'link_field',
  );
  $this->backdropPost('admin/structure/types/manage/page/fields', $edit, t('Save'));

  // Enable multiple values. Use the other defaults, which should use a link
  // type of "both".
  $edit = array(
    'field[cardinality]' => '-1',
  );
  $this->backdropPost(NULL, $edit, t('Save settings'));

  field_info_cache_clear();
  $instances = field_info_instances('node', 'page');

  $instance = $instances['field_' . $name];
  $this->assertEqual($instance['settings']['type'], 'both', 'Link type set to "both" by default.');

  // Create nodes with both internal and external values.
  $field_name = 'field_' . $name;
  $this->backdropGet('node/add/page');

  // Check that autocomplete is enabled on internal/external link fields.
  $this->assertFieldByXPath("//input[@name='${field_name}[und][0][url]' and contains(@class, 'form-autocomplete')]", NULL, 'Autocomplete is enabled on the link URL field.');

  // Save both an internal and external value.
  $edit = array(
    'title' => 'Internal and external test',
    $field_name . '[und][0][title]' => 'Example External URL',
    $field_name . '[und][0][url]' => 'http://www.example.com/',
  );
  $this->backdropPost(NULL, $edit, t('Add another'));
  $edit = array(
    $field_name . '[und][1][title]' => 'Example Internal URL',
    $field_name . '[und][1][url]' => '<front>',
  );
  $this->backdropPost(NULL, $edit, t('Save'));

  // Check that both internal and external values are output correctly.
  $this->assertElementByXPath('//a[contains(@href, :path) and text()=:title]', array(
    ':path' => 'http://www.example.com/',
    ':title' => 'Example External URL',
  ), 'External link saved correctly.');
  $this->assertElementByXPath('//a[contains(@href, :path) and text()=:title]', array(
    ':path' => url('<front>'),
    ':title' => 'Example Internal URL',
  ), 'Internal link saved correctly.');

  // Set the field to be internal only.
  $edit = array(
    'instance[settings][type]' => 'internal',
  );
  $this->backdropPost('admin/structure/types/manage/page/fields/' . $field_name, $edit, t('Save settings'));

  // Check that autocomplete is enabled on internal/external link fields.
  $this->backdropGet('node/add/page');
  $this->assertFieldByXPath("//input[@name='${field_name}[und][0][url]' and contains(@class, 'form-autocomplete')]", NULL, 'Autocomplete is enabled on the link URL field.');

  // Confirm an external value is not allowed.
  $edit = array(
    'title' => 'Internal only test',
    $field_name . '[und][0][title]' => 'Example External URL',
    $field_name . '[und][0][url]' => 'https://example.com',
  );
  $this->backdropPost(NULL, $edit, t('Save'));
  $this->assertRaw(t('Only internal URLs are allowed in %field.', array('%field' => $name)));

  // Submit internal link values.
  $edit = array(
    $field_name . '[und][0][title]' => 'Example Front Page Link',
    $field_name . '[und][0][url]' => '<front>',
  );
  $this->backdropPost(NULL, $edit, t('Save'));
  $this->assertElementByXPath('//a[contains(@href, :path) and text()=:title]', array(
    ':path' => url('<front>'),
    ':title' => 'Example Front Page Link',
  ), 'Internal link saved correctly.');

  // Set the field to be external only.
  $edit = array(
    'instance[settings][type]' => 'external',
  );
  $this->backdropPost('admin/structure/types/manage/page/fields/' . $field_name, $edit, t('Save settings'));

  // Check that autocomplete is disabled on external link fields.
  $this->backdropGet('node/add/page');
  $this->assertNoFieldByXPath("//input[@name='${field_name}[und][0][url]' and contains(@class, 'form-autocomplete')]", NULL, 'Autocomplete is enabled on the link URL field.');

  // Confirm an internal value is not allowed.
  $edit = array(
    'title' => 'External only test',
    $field_name . '[und][0][title]' => 'Example Internal URL',
    $field_name . '[und][0][url]' => 'node/1',
  );
  $this->backdropPost(NULL, $edit, t('Save'));
  $this->assertRaw(t('Only external URLs are allowed in %field.', array('%field' => $name)));

  // Submit external link values.
  $edit = array(
    $field_name . '[und][0][title]' => 'Example External Link',
    $field_name . '[und][0][url]' => 'https://example.com',
  );
  $this->backdropPost(NULL, $edit, t('Save'));
  $this->assertElementByXPath('//a[contains(@href, :path) and text()=:title]', array(
    ':path' => url('https://example.com'),
    ':title' => 'Example External Link',
  ), 'External link saved correctly.');
}