You are here

function LinkUITest::testLinkCreate in Link 6.2

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

File

tests/link.crud_browser.test, line 49
Testing CRUD API in the browser.

Class

LinkUITest
Testing that users can not input bad URLs or labels

Code

function testLinkCreate() {

  //libxml_use_internal_errors(true);
  $account = $this
    ->drupalCreateUser(array(
    'administer content types',
    'access content',
    'create page content',
  ));
  $this
    ->drupalLogin($account);

  // create field
  $name = strtolower($this
    ->randomName());
  $edit = array(
    '_add_new_field[label]' => $name,
    '_add_new_field[field_name]' => $name,
    '_add_new_field[type]' => 'link',
    '_add_new_field[widget_type]' => 'link',
  );
  $this
    ->drupalPost('admin/content/node-type/page/fields', $edit, t('Save'));
  $this
    ->drupalPost(NULL, array(
    'validate_url' => TRUE,
  ), t('Save field settings'));

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

  // create page form
  $this
    ->drupalGet('node/add/page');
  $field_name = 'field_' . $name;
  $this
    ->assertField($field_name . '[0][title]', 'Title found');
  $this
    ->assertField($field_name . '[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,
    ),
  );
  foreach ($input_test_cases as $input) {
    $this
      ->drupalLogin($account);
    $this
      ->drupalGet('node/add/page');
    $edit = array(
      'title' => $input['label'],
      $field_name . '[0][title]' => $input['label'],
      $field_name . '[0][url]' => $input['href'],
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    if ($input['type'] == self::LINK_INPUT_TYPE_BAD_URL) {
      $this
        ->assertRaw(t('Not a valid URL.'), 'Not a valid URL: ' . $input['href']);
      continue;
    }
    else {
      $this
        ->assertRaw(t('@type %title has been created.', array(
        '@type' => 'Page',
        '%title' => $edit['title'],
      )), 'Page created: ' . $input['href']);
    }
    $url = $this
      ->getUrl();

    // change to anonym user
    $this
      ->drupalLogout();
    $this
      ->drupalGet($url);

    //debug($this);

    // 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));
    $path = '//a[@href="' . $input['href'] . '" and text()="' . $input['label'] . '"]';

    //$this->pass(htmlentities($path));
    $elements = $this
      ->xpath($path);
    libxml_use_internal_errors(FALSE);
    $this
      ->assertIdentical(isset($elements[0]), $input['type'] == self::LINK_INPUT_TYPE_GOOD, $input['msg']);
  }

  //libxml_use_internal_errors(FALSE);
}