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

Creates a link field for the "page" type and creates a page with a link.

File

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

Class

LinkUITest
Testing that users can not input bad URLs or labels

Code

function testLinkCreate() {
  $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'));
  $this->backdropPost(NULL, array(), t('Save settings'));

  // Is field created?
  $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');

  $permission = 'create page content';
  $this->checkPermissions(array($permission), TRUE);

  $this->backdropGet('node/add/page');
  $field_name = 'field_' . $name;
  $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
  $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');

  $input_test_cases = array(
    array(
      'href' => 'http://example.com/' . $this->randomName(),
      'label' => $this->randomName(),
      'msg' => 'Link found',
      'type' => self::LINK_INPUT_TYPE_GOOD
    ),
    array(
      'href' => 'http://example.com/' . $this->randomName(),
      'label' => $this->randomName() . '<script>alert("hi");</script>',
      'msg' => 'js label',
      'type' => self::LINK_INPUT_TYPE_BAD_TITLE
    ),
    array(
      'href' => 'http://example.com/' . $this->randomName(),
      'label' => $this->randomName() . '<script src="http://devil.site.com"></script>',
      'msg' => 'js label',
      'type' => self::LINK_INPUT_TYPE_BAD_TITLE
    ),
    array(
      'href' => 'http://example.com/' . $this->randomName(),
      'label' => $this->randomName() . '" onmouseover="alert(\'hi\')',
      'msg' => 'js label',
      'type' => self::LINK_INPUT_TYPE_BAD_TITLE
    ),
    array(
      'href' => 'http://example.com/' . $this->randomName(),
      'label' => $this->randomName() . '\' onmouseover="alert(\'hi\')',
      'msg' => 'js label',
      'type' => self::LINK_INPUT_TYPE_BAD_TITLE
    ),
    array(
      'href' => 'javascript:alert("http://example.com/' . $this->randomName() . '")',
      'label' => $this->randomName(),
      'msg' => 'js url',
      'type' => self::LINK_INPUT_TYPE_BAD_URL
    ),
    array(
      'href' => 'http://ecs-es.kelkoo.es/ctl/go/sitesearchGo?.ts=1338833010331&.sig=qP9GXeEFH6syBzwmzYkxmsvp1EI-',
      'label' => 'http://ecs-es.kelkoo.es/ctl/go/sitesearchGo?.ts=1338833010331&.sig=qP9GXeEFH6syBzwmzYkxmsvp1EI-',
      'msg' => 'Url with . in querystring',
      'type' => self::LINK_INPUT_TYPE_GOOD,
    ),
  );
  $test_case = array(
    'href' => 'www.example.com/' . $this->randomName(),
    'label' => $this->randomName(),
    'msg' => 'Link found',
    'type' => self::LINK_INPUT_TYPE_GOOD,
  );
  $test_case['expected_href'] = 'http://' . $test_case['href'];
  $input_test_cases[] = $test_case;

  foreach ($input_test_cases as $input) {
    $this->backdropLogin($this->web_user);
    $this->backdropGet('node/add/page');

    $edit = array(
      'title' => $input['label'],
      $field_name . '[und][0][title]' => $input['label'],
      $field_name . '[und][0][url]' => $input['href'],
    );
    $this->backdropPost(NULL, $edit, t('Save'));
    if ($input['type'] == self::LINK_INPUT_TYPE_BAD_URL) {
      $this->assertRaw(t('The value %value provided for %field is not a valid URL.', array('%field' => $name, '%value' => trim($input['href']))), 'Not a valid URL: ' . $input['href']);
      continue;
    }
    else {
      $this->assertRaw(' ' . t('has been created.', array('@type' => 'Basic Page', '%title' => $edit['title'])), 'Page created: ' . $input['href']);
    }

    // If simpletest starts using something to override the error system, this
    ///will flag us and let us know it's broken.
    $this->assertFalse(libxml_use_internal_errors(TRUE));
    if (isset($input['expected_href'])) {
      $path = '//a[@href="' . $input['expected_href'] . '" and text()="' . $input['label'] . '"]';
    }
    else {
      $path = '//a[@href="' . $input['href'] . '" and text()="' . $input['label'] . '"]';
    }
    $elements = $this->xpath($path);
    libxml_use_internal_errors(FALSE);
    $this->assertIdentical(isset($elements[0]), $input['type'] == self::LINK_INPUT_TYPE_GOOD, $input['msg']);
  }
}