$url, 'title' => $title, 'attributes' => $attributes, ); } /** * Takes a url, and sees if it can validate that the url is valid. */ protected function link_test_validate_url($url) { $field_name = $this->createLinkField(); $label = $this->randomName(); $settings = array( 'title' => $label, $field_name => array( LANGUAGE_NONE=> array( array( 'title' => $label, 'url' => $url, ) ), ), ); $node = $this->drupalCreateNode($settings); $this->assertNotNull($node, ' has been created.', 'Node created'); $this->assertEqual($url, $node->{$field_name}[LANGUAGE_NONE][0]['url']); } } class LinkValidateTest extends LinkValidateTestCase { public static function getInfo() { return array( 'name' => 'Link Validation Tests', 'description' => 'Tests the field validation.', 'group' => 'Link', ); } function test_link_validate_basic_url() { $this->link_test_validate_url('http://www.example.com'); } /** * Test if we're stopped from posting a bad url on default validation. */ function test_link_validate_bad_url_validate_default() { $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(); // create page form $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'); $edit = array( 'title' => 'Simple Title', $field_name . '[und][0][url]' => 'edik:naw', ); $this->drupalPost(NULL, $edit, t('Save')); $this->assertText(t('The value @value provided for @field is not a valid URL.', array('@value' => 'edik:naw', '@field' => $name))); } /** * Test if we're stopped from posting a bad url with validation on. */ function test_link_validate_bad_url_validate_on() { $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('instance[settings][validate_url]' => TRUE), t('Save settings')); // Is field created? $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added'); node_types_rebuild(); menu_rebuild(); // create page form $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'); $edit = array( 'title' => 'Simple Title', $field_name . '[und][0][url]' => 'edik:naw', ); $this->drupalPost(NULL, $edit, t('Save')); $this->assertText(t('The value @value provided for @field is not a valid URL.', array('@field' => $name, '@value' => 'edik:naw'))); } /** * Test if we can post a bad url if the validation is expressly turned off. */ function test_link_validate_bad_url_validate_off() { $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('instance[settings][validate_url]' => FALSE), t('Save settings')); /*$instance_details = db_query("SELECT * FROM {field_config_instance} WHERE field_name = :field_name AND bundle = 'page'", array(':field_name' => 'field_'. $name))->fetchObject(); $this->fail('
'. print_r($instance_details, TRUE) .''); $this->fail('
'. print_r(unserialize($instance_details->data), TRUE) .'');*/ // Is field created? $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added'); node_types_rebuild(); menu_rebuild(); // create page form $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'); $edit = array( 'title' => 'Simple Title', $field_name . '[und][0][url]' => 'edik:naw', ); $this->drupalPost(NULL, $edit, t('Save')); $this->assertNoText(t('The value %value provided for %field is not a valid URL.', array('%field' => $name, '%value' => 'edik:naw'))); } /** * Test if a bad url can sneak through un-filtered if we play with the validation... */ function x_test_link_validate_switching_between_validation_status() { $this->acquireContentTypes(1); $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); variable_set('node_options_' . $this->content_types[0]->name, array('status', 'promote')); $field_settings = array( 'type' => 'link', 'widget_type' => 'link', 'type_name' => $this->content_types[0]->name, 'attributes' => array(), // <-- This is needed or we have an error 'validate_url' => 0, ); $field = $this->createField($field_settings, 0); //$this->fail('
'. print_r($field, TRUE) .''); $field_db_info = content_database_info($field); $this->acquireNodes(2); $node = node_load($this->nodes[0]->nid); $this->drupalGet('node/' . $this->nodes[0]->nid); $edit = array(); $title = $this->randomName(); $url = 'javascript:alert("http://example.com/' . $this->randomName() . '")'; $edit[$field['field_name'] . '[0][url]'] = $url; $edit[$field['field_name'] . '[0][title]'] = $title; $this->drupalPost('node/' . $this->nodes[0]->nid . '/edit', $edit, t('Save')); //$this->pass($this->content); $this->assertNoText(t('The value %value provided for %field is not a valid URL.', array('%field' => $name, '%value' => trim($url)))); // Make sure we get a new version! $node = node_load($this->nodes[0]->nid, NULL, TRUE); $this->assertEqual($url, $node->{$field['field_name']}[0]['url']); $this->drupalGet('node/' . $node->nid); $this->assertNoRaw($url, 'Make sure Javascript does not display.'); // Turn the array validation back _on_. $edit = array('validate_url' => TRUE); $node_type_link = str_replace('_', '-', $node->type); //$this->drupalGet('admin/content/node-type/'. $node_type_link .'/fields'); ///'. $field['field_name']); //$this->fail($this->content); $this->drupalPost('admin/content/node-type/' . $node_type_link . '/fields/' . $field['field_name'], $edit, t('Save field settings')); $this->drupalGet('node/' . $node->nid); // This actually works because the display_url goes through the core // url() function. But we should have a test that makes sure it continues // to work. $this->assertNoRaw($url, 'Make sure Javascript does not display.'); //$this->fail($this->content); } // Validate that '