| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 | <?php/** * @file * Testing CRUD API in the browser. *//** * Testing that users can not input bad URLs or labels */class LinkUITest extends DrupalWebTestcase {  /**   * Link supposed to be good   */  const LINK_INPUT_TYPE_GOOD = 0;  /**   * Link supposed to have a bad title   */  const LINK_INPUT_TYPE_BAD_TITLE = 1;  /**   * Link supposed to have a bad URL   */  const LINK_INPUT_TYPE_BAD_URL = 2;  public static function getInfo() {    return array(      'name' => 'Link CRUD - browser test',      'description' => 'Tests the field CRUD (create, read, update, delete) API 2.',      'group' => 'Link',    );  }  function setUp() {    parent::setUp('field_ui', 'link');  }  /**   * Creates a link field for the "page" type and creates a page with a link.   */  function testLinkCreate() {    //libxml_use_internal_errors(true);    $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));    $perms = array_keys($perms[array_search('administrator', user_roles())]);    $admin = $this->drupalCreateUser($perms);    $this->drupalLogin($admin);    // create field    $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->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));    $this->drupalPost(NULL, array(), t('Save field settings'));    $this->drupalPost(NULL, array(), t('Save settings'));    // Is field created?    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');    node_types_rebuild();    menu_rebuild();    $permission = 'create page content';    $this->checkPermissions(array($permission), TRUE);    // create page form    //$this->drupalGet('node/add');    $this->drupalGet('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->drupalLogin($admin);      $this->drupalGet('node/add/page');      $edit = array(        'title' => $input['label'],        $field_name . '[und][0][title]' => $input['label'],        $field_name . '[und][0][url]' => $input['href'],      );      $this->drupalPost(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']);      }      $url = $this->getUrl();      // change to Anonymous 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));      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']);    }    //libxml_use_internal_errors(FALSE);  }  /**   * Testing that if you use <strong> in a static title for your link, that the   * title actually displays <strong>.   */  function testStaticLinkCreate() {    $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));    $perms = array_keys($perms[array_search('administrator', user_roles())]);    $admin = $this->drupalCreateUser($perms);    $this->drupalLogin($admin);    // create field    $name = strtolower($this->randomName());    $field_name = 'field_' . $name;    $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->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));    $this->drupalPost(NULL, array(), t('Save field settings'));    $this->drupalPost(NULL, array(      'instance[settings][title]' => 'value',      'instance[settings][title_value]' => '<strong>' . $name . '</strong>'), t('Save settings'));    // Is field created?    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');    // create page form    $this->drupalGet('node/add/page');    $this->assertField($field_name . '[und][0][url]', 'URL found');    $input = array(      'href' => 'http://example.com/' . $this->randomName()    );    $edit = array(      'title' => $name,      $field_name . '[und][0][url]' => $input['href'],    );    $this->drupalPost(NULL, $edit, t('Save'));    $url = $this->getUrl();    // change to anonymous user    $this->drupalLogout();    $this->drupalGet($url);    $this->assertRaw(l('<strong>' . $name . '</strong>', $input['href'], array('html' => TRUE)));  }    /**   * Testing that if you have the title but no url, the title is not sanitized twice.   */  function testCRUDTitleOnlyTitleNoLink() {    $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));    $perms = array_keys($perms[array_search('administrator', user_roles())]);    $admin = $this->drupalCreateUser($perms);    $this->drupalLogin($admin);    // create field    $name = strtolower($this->randomName());    $field_name = 'field_' . $name;    $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->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));    $this->drupalPost(NULL, array(), t('Save field settings'));    $this->drupalPost(NULL, array(      'instance[settings][url]' => 1,    ), t('Save settings'));    // Is field created?    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');        // create page form    $this->drupalGet('node/add/page');    $this->assertField($field_name . '[und][0][url]', 'URL found');    $input = array(      'title' => 'This & That',      'href' => '',    );    $edit = array(      'title' => $name,      $field_name . '[und][0][title]' => $input['title'],      $field_name . '[und][0][url]' => $input['href'],    );    $this->drupalPost(NULL, $edit, t('Save'));    $url = $this->getUrl();        // change to anonymous user    $this->drupalLogout();    $this->drupalGet($url);    $this->assertRaw('This & That');  }  /**   * If we're creating a new field and just hit 'save' on the default options, we want to make   * sure they are set to the expected results.   */  function testCRUDCreateFieldDefaults() {    $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));    $perms = array_keys($perms[array_search('administrator', user_roles())]);    $admin = $this->drupalCreateUser($perms);    $this->drupalLogin($admin);    // create field    $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->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));    $this->drupalPost(NULL, array(), t('Save field settings'));    $this->drupalPost(NULL, array(), t('Save settings'));    // Is field created?    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');    node_types_rebuild();    menu_rebuild();    _field_info_collate_fields(TRUE);    $instances = field_info_instances('node', 'page');    $instance = $instances['field_' . $name];    $this->assertFalse($instance['required'], 'Make sure field is not required.');    $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');    $this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');    $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');    $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');    $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');    $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');    $this->assertFalse($instance['settings']['attributes']['class'], 'By default, no class should be set.');    $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');  }    /**   * If we're creating a new field and just hit 'save' on the default options, we want to make   * sure they are set to the expected results.   */  function testCRUDCreateFieldWithClass() {    $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));    $perms = array_keys($perms[array_search('administrator', user_roles())]);    $admin = $this->drupalCreateUser($perms);    $this->drupalLogin($admin);    // create field    $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->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));    $this->drupalPost(NULL, array(), t('Save field settings'));    $link_class_name = 'basic-link-' . strtolower($this->randomName());    $edit = array(      'instance[settings][attributes][class]' => $link_class_name,    );    $this->drupalPost(NULL, $edit, t('Save settings'));    // Is field created?    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');    node_types_rebuild();    menu_rebuild();    _field_info_collate_fields(TRUE);    $instances = field_info_instances('node', 'page');    $instance = $instances['field_' . $name];    $this->assertFalse($instance['required'], 'Make sure field is not required.');    $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');    $this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');    $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');    $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');    $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');    $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');    $this->assertEqual($instance['settings']['attributes']['class'], $link_class_name, 'One class should be set.');    $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');        // Now, let's create a node with this field and make sure the link shows up:    // create page form    $field_name = 'field_' . $name;    $this->drupalGet('node/add/page');    $this->assertField($field_name . '[und][0][url]', 'URL found');    $input = array(      'title' => 'This & That',      'href' => 'http://www.example.com/',    );    $edit = array(      'title' => $field_name,      $field_name . '[und][0][title]' => $input['title'],      $field_name . '[und][0][url]' => $input['href'],    );    $this->drupalPost(NULL, $edit, t('Save'));    $url = $this->getUrl();        // change to anonymous user    $this->drupalLogout();    $this->drupalGet($url);    $this->assertRaw('This & That');    $this->assertPattern('|class\s?=\s?"' . $link_class_name . '"|', "Class $link_class_name exists on page.");  }/**   * If we're creating a new field and just hit 'save' on the default options, we want to make   * sure they are set to the expected results.   */  function testCRUDCreateFieldWithTwoClasses() {    $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));    $perms = array_keys($perms[array_search('administrator', user_roles())]);    $admin = $this->drupalCreateUser($perms);    $this->drupalLogin($admin);    // create field    $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->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));    $this->drupalPost(NULL, array(), t('Save field settings'));    $link_class_name = 'basic-link ' . strtoupper($this->randomName());    $edit = array(      'instance[settings][attributes][class]' => $link_class_name,    );    $this->drupalPost(NULL, $edit, t('Save settings'));    // Is field created?    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');    node_types_rebuild();    menu_rebuild();    _field_info_collate_fields(TRUE);    $instances = field_info_instances('node', 'page');    $instance = $instances['field_' . $name];    $this->assertFalse($instance['required'], 'Make sure field is not required.');    $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');    $this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');    $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');    $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');    $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');    $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');    $this->assertEqual($instance['settings']['attributes']['class'], $link_class_name, 'Two classes should be set.');    $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');        // Now, let's create a node with this field and make sure the link shows up:    // create page form    $field_name = 'field_' . $name;    $this->drupalGet('node/add/page');    $this->assertField($field_name . '[und][0][url]', 'URL found');    $input = array(      'title' => 'This & That',      'href' => 'http://www.example.com/',    );    $edit = array(      'title' => $field_name,      $field_name . '[und][0][title]' => $input['title'],      $field_name . '[und][0][url]' => $input['href'],    );    $this->drupalPost(NULL, $edit, t('Save'));    $url = $this->getUrl();        // change to anonymous user    $this->drupalLogout();    $this->drupalGet($url);    $this->assertRaw('This & That');    $this->assertPattern('|class\s?=\s?"' . $link_class_name . '"|', "Classes $link_class_name exist on page.");  }}
 |