Commit 5c0ebaa7 authored by jcfiala's avatar jcfiala
Browse files

Issue #1148864 by jcfiala: Re-enabled the ability to turn off url validation.

parent ef2fb062
......@@ -49,6 +49,7 @@ function link_field_info() {
'display' => array(
'url_cutoff' => 80,
),
'validate_url' => 1,
),
'default_widget' => 'link_field',
'default_formatter' => 'link_default',
......@@ -63,6 +64,13 @@ function link_field_instance_settings_form($field, $instance) {
$form = array(
'#element_validate' => array('link_field_settings_form_validate'),
);
$form['validate_url'] = array(
'#type' => 'checkbox',
'#title' => t('Validate URL'),
'#default_value' => isset($instance['settings']['validate_url']) && ($instance['settings']['validate_url'] !== '') ? $instance['settings']['validate_url'] : TRUE,
'#description' => t('If checked, the URL field will be verified as a valid URL during validation.'),
);
$form['url'] = array(
'#type' => 'checkbox',
......@@ -201,8 +209,14 @@ function link_field_load($entity_type, $entities, $field, $instances, $langcode,
*/
function link_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
$optional_field_found = FALSE;
foreach ($items as $delta => $value) {
_link_validate($items[$delta], $delta, $field, $entity, $instance, $optional_field_found);
if ($instance['settings']['validate_url'] !== 0 || is_null($instance['settings']['validate_url']) || !isset($instance['settings']['validate_url'])) {
foreach ($items as $delta => $value) {
_link_validate($items[$delta], $delta, $field, $entity, $instance, $optional_field_found);
}
}
if ($instance['settings']['url'] == 'optional' && $instance['settings']['title'] == 'optional' && $instance['required'] && !$optional_field_found) {
form_set_error($field['field_name'] .'][0][title', t('At least one title or URL must be entered.'));
}
}
......@@ -359,10 +373,13 @@ function _link_sanitize(&$item, $delta, &$field, $instance, &$node) {
}
$type = link_validate_url($item['url']);
// If we can't determine the type of url, and we've been told not to validate it,
// then we assume it's a LINK_EXTERNAL type for later processing. #357604
if ($type == FALSE && $instance['settings']['validate_url'] === 0) {
$type = LINK_EXTERNAL;
}
$url = link_cleanup_url($item['url']);
//dpm(array($type, $url));
// Separate out the anchor if any.
if (strpos($url, '#') !== FALSE) {
$item['fragment'] = substr($url, strpos($url, '#') + 1);
......@@ -375,6 +392,7 @@ function _link_sanitize(&$item, $delta, &$field, $instance, &$node) {
$item['query'] = $query_array;
$url = substr($url, 0, strpos($url, '?'));
}
$item['url'] = check_plain($url);
// Create a shortened URL for display.
......
......@@ -68,112 +68,142 @@ class LinkValidateTest extends LinkValidateTestCase {
/**
* Test if we're stopped from posting a bad url on default validation.
*/
function x_test_link_validate_bad_url_validate_default() {
$this->acquireContentTypes(1);
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' => 1,
);
function test_link_validate_bad_url_validate_default() {
$account = $this->drupalCreateUser(array('administer content types',
'administer nodes',
'administer filters',
'access content',
'create page content',
'access administration pages'));
$this->drupalLogin($account);
$field = $this->createField($field_settings, 0);
//$this->pass('<pre>'. print_r($field, TRUE) .'</pre>');
$field_db_info = content_database_info($field);
// 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'));
$this->acquireNodes(2);
// Is field created?
$this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
node_types_rebuild();
menu_rebuild();
$node = node_load($this->nodes[0]->nid);
// 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');
$this->drupalGet('node/'. $this->nodes[0]->nid);
$edit = array();
$edit[$field['field_name'] .'[0][url]'] = 'edik:naw';
$edit = array(
'title' => 'Simple Title',
$field_name .'[und][0][url]' => 'edik:naw',
);
$this->drupalPost('node/'. $this->nodes[0]->nid .'/edit', $edit, t('Save'));
//$this->pass($this->content);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertText(t('Not a valid URL.'));
// Make sure we get a new version!
$node = node_load($this->nodes[0]->nid, NULL, TRUE);
$this->assertNotEqual('edik:naw', $node->{$field['field_name']}[0]['url']);
}
/**
* Test if we're stopped from posting a bad url with validation on.
*/
function x_test_link_validate_bad_url_validate_on() {
$this->acquireContentTypes(1);
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' => 1,
);
function test_link_validate_bad_url_validate_on() {
$account = $this->drupalCreateUser(array('administer content types',
'administer nodes',
'administer filters',
'access content',
'create page content',
'access administration pages'));
$this->drupalLogin($account);
$field = $this->createField($field_settings, 0);
//$this->pass('<pre>'. print_r($field, TRUE) .'</pre>');
$field_db_info = content_database_info($field);
// 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'));
$this->acquireNodes(2);
// Is field created?
$this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
node_types_rebuild();
menu_rebuild();
$node = node_load($this->nodes[0]->nid);
// 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');
$this->drupalGet('node/'. $this->nodes[0]->nid);
$edit = array();
$edit[$field['field_name'] .'[0][url]'] = 'edik:naw';
$edit = array(
'title' => 'Simple Title',
$field_name .'[und][0][url]' => 'edik:naw',
);
$this->drupalPost('node/'. $this->nodes[0]->nid .'/edit', $edit, t('Save'));
//$this->pass($this->content);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertText(t('Not a valid URL.'));
// Make sure we get a new version!
$node = node_load($this->nodes[0]->nid, NULL, TRUE);
$this->assertNotEqual('edik:naw', $node->{$field['field_name']}[0]['url']);
}
/**
* Test if we can post a bad url if the validation is expressly turned off.
*/
function x_test_link_validate_bad_url_validate_off() {
$this->acquireContentTypes(1);
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,
);
function test_link_validate_bad_url_validate_off() {
$account = $this->drupalCreateUser(array('administer content types',
'administer nodes',
'administer filters',
'access content',
'create page content',
'access administration pages'));
$this->drupalLogin($account);
$field = $this->createField($field_settings, 0);
//$this->pass('<pre>'. print_r($field, TRUE) .'</pre>');
$field_db_info = content_database_info($field);
// 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('<pre>'. print_r($instance_details, TRUE) .'</pre>');
$this->fail('<pre>'. print_r(unserialize($instance_details->data), TRUE) .'</pre>');*/
$this->acquireNodes(2);
// Is field created?
$this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
node_types_rebuild();
menu_rebuild();
$node = node_load($this->nodes[0]->nid);
// 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');
$this->drupalGet('node/'. $this->nodes[0]->nid);
$edit = array();
$edit[$field['field_name'] .'[0][url]'] = 'edik:naw';
$edit = array(
'title' => 'Simple Title',
$field_name .'[und][0][url]' => 'edik:naw',
);
$this->drupalPost('node/'. $this->nodes[0]->nid .'/edit', $edit, t('Save'));
//$this->pass($this->content);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertNoText(t('Not a valid URL.'));
// Make sure we get a new version!
$node = node_load($this->nodes[0]->nid, NULL, TRUE);
$this->assertEqual('edik:naw', $node->{$field['field_name']}[0]['url']);
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment