Commit f846c6c1 authored by webchick's avatar webchick
Browse files

Issue #501434 by sun, Dave Reid: Added Link field type into core.

parent a24390f2
......@@ -91,6 +91,7 @@ Drupal 8.0, xxxx-xx-xx (development version)
* Reworked Gettext PO support to use pluggable read/write handlers.
* Added language select form element in the Form API.
- Added E-mail field type to core.
- Added Link field type to core.
Drupal 7.0, 2011-01-05
----------------------
......
<?php
/**
* @file
* Contains Drupal\link\Tests\LinkFieldTest.
*/
namespace Drupal\link\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests link field widgets and formatters.
*/
class LinkFieldTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field_test', 'link');
public static function getInfo() {
return array(
'name' => 'Link field',
'description' => 'Tests link field widgets and formatters.',
'group' => 'Field types',
);
}
function setUp() {
parent::setUp();
$this->web_user = $this->drupalCreateUser(array(
'access field_test content',
'administer field_test content',
));
$this->drupalLogin($this->web_user);
}
/**
* Tests link field URL validation.
*/
function testURLValidation() {
// Create a field with settings to validate.
$this->field = array(
'field_name' => drupal_strtolower($this->randomName()),
'type' => 'link',
);
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'settings' => array(
'title' => DRUPAL_DISABLED,
),
'widget' => array(
'type' => 'link_default',
),
'display' => array(
'full' => array(
'type' => 'link',
),
),
);
field_create_instance($this->instance);
$langcode = LANGUAGE_NOT_SPECIFIED;
// Display creation form.
$this->drupalGet('test-entity/add/test_bundle');
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][url]", '', 'Link URL field is displayed');
// Verify that a valid URL can be submitted.
$value = 'http://www.example.com/';
$edit = array(
"{$this->field['field_name']}[$langcode][0][url]" => $value,
);
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)));
$this->assertRaw($value);
// Verify that invalid URLs cannot be submitted.
$wrong_entries = array(
// Missing protcol
'not-an-url',
// Invalid protocol
'invalid://not-a-valid-protocol',
// Missing host name
'http://',
);
$this->drupalGet('test-entity/add/test_bundle');
foreach ($wrong_entries as $invalid_value) {
$edit = array(
"{$this->field['field_name']}[$langcode][0][url]" => $invalid_value,
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertText(t('The URL @url is not valid.', array('@url' => $invalid_value)));
}
}
/**
* Tests the title settings of a link field.
*/
function testLinkTitle() {
// Create a field with settings to validate.
$this->field = array(
'field_name' => drupal_strtolower($this->randomName()),
'type' => 'link',
);
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'settings' => array(
'title' => DRUPAL_OPTIONAL,
),
'widget' => array(
'type' => 'link_default',
),
'display' => array(
'full' => array(
'type' => 'link',
'label' => 'hidden',
),
),
);
field_create_instance($this->instance);
$langcode = LANGUAGE_NOT_SPECIFIED;
// Verify that the title field works according to the field setting.
foreach (array(DRUPAL_DISABLED, DRUPAL_REQUIRED, DRUPAL_OPTIONAL) as $title_setting) {
// Update the title field setting.
$this->instance['settings']['title'] = $title_setting;
field_update_instance($this->instance);
// Display creation form.
$this->drupalGet('test-entity/add/test_bundle');
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][url]", '', 'URL field found.');
if ($title_setting === DRUPAL_DISABLED) {
$this->assertNoFieldByName("{$this->field['field_name']}[$langcode][0][title]", '', 'Title field not found.');
}
else {
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][title]", '', 'Title field found.');
if ($title_setting === DRUPAL_REQUIRED) {
// Verify that the title is required, if the URL is non-empty.
$edit = array(
"{$this->field['field_name']}[$langcode][0][url]" => 'http://www.example.com',
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertText(t('!name field is required.', array('!name' => t('Title'))));
// Verify that the title is not required, if the URL is empty.
$edit = array(
"{$this->field['field_name']}[$langcode][0][url]" => '',
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertNoText(t('!name field is required.', array('!name' => t('Title'))));
// Verify that a URL and title meets requirements.
$this->drupalGet('test-entity/add/test_bundle');
$edit = array(
"{$this->field['field_name']}[$langcode][0][url]" => 'http://www.example.com',
"{$this->field['field_name']}[$langcode][0][title]" => 'Example',
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertNoText(t('!name field is required.', array('!name' => t('Title'))));
}
}
}
// Verify that a link without title is rendered using the URL as link text.
$value = 'http://www.example.com/';
$edit = array(
"{$this->field['field_name']}[$langcode][0][url]" => $value,
"{$this->field['field_name']}[$langcode][0][title]" => '',
);
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)));
$this->renderTestEntity($id);
$expected_link = l($value, $value);
$this->assertRaw($expected_link);
// Verify that a link with title is rendered using the title as link text.
$title = $this->randomName();
$edit = array(
"{$this->field['field_name']}[$langcode][0][title]" => $title,
);
$this->drupalPost("test-entity/manage/$id/edit", $edit, t('Save'));
$this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)));
$this->renderTestEntity($id);
$expected_link = l($title, $value);
$this->assertRaw($expected_link);
}
/**
* Tests the default 'link' formatter.
*/
function testLinkFormatter() {
// Create a field with settings to validate.
$this->field = array(
'field_name' => drupal_strtolower($this->randomName()),
'type' => 'link',
'cardinality' => 2,
);
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'settings' => array(
'title' => DRUPAL_OPTIONAL,
),
'widget' => array(
'type' => 'link_default',
),
'display' => array(
'full' => array(
'type' => 'link',
'label' => 'hidden',
),
),
);
field_create_instance($this->instance);
$langcode = LANGUAGE_NOT_SPECIFIED;
// Create an entity with two link field values:
// - The first field item uses a URL only.
// - The second field item uses a URL and title.
// For consistency in assertion code below, the URL is assigned to the title
// variable for the first field.
$this->drupalGet('test-entity/add/test_bundle');
$url1 = 'http://www.example.com/content/articles/archive?author=John&year=2012#com';
$url2 = 'http://www.example.org/content/articles/archive?author=John&year=2012#org';
$title1 = $url1;
// Intentionally contains an ampersand that needs sanitization on output.
$title2 = 'A very long & strange example title that could break the nice layout of the site';
$edit = array(
"{$this->field['field_name']}[$langcode][0][url]" => $url1,
// Note that $title1 is not submitted.
"{$this->field['field_name']}[$langcode][0][title]" => '',
"{$this->field['field_name']}[$langcode][1][url]" => $url2,
"{$this->field['field_name']}[$langcode][1][title]" => $title2,
);
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)));
// Verify that the link is output according to the formatter settings.
// Not using generatePermutations(), since that leads to 32 cases, which
// would not test actual link field formatter functionality but rather
// theme_link() and options/attributes. Only 'url_plain' has a dependency on
// 'url_only', so we have a total of ~10 cases.
$options = array(
'trim_length' => array(NULL, 6),
'rel' => array(NULL, 'nofollow'),
'target' => array(NULL, '_blank'),
'url_only' => array(
array('url_only' => array(FALSE)),
array('url_only' => array(FALSE), 'url_plain' => TRUE),
array('url_only' => array(TRUE)),
array('url_only' => array(TRUE), 'url_plain' => TRUE),
),
);
foreach ($options as $setting => $values) {
foreach ($values as $new_value) {
// Update the field formatter settings.
if (!is_array($new_value)) {
$this->instance['display']['full']['settings'] = array($setting => $new_value);
}
else {
$this->instance['display']['full']['settings'] = $new_value;
}
field_update_instance($this->instance);
$this->renderTestEntity($id);
switch ($setting) {
case 'trim_length':
$url = $url1;
$title = isset($new_value) ? truncate_utf8($title1, $new_value, FALSE, TRUE) : $title1;
$this->assertRaw('<a href="' . check_plain($url) . '">' . check_plain($title) . '</a>');
$url = $url2;
$title = isset($new_value) ? truncate_utf8($title2, $new_value, FALSE, TRUE) : $title2;
$this->assertRaw('<a href="' . check_plain($url) . '">' . check_plain($title) . '</a>');
break;
case 'rel':
$rel = isset($new_value) ? ' rel="' . $new_value . '"' : '';
$this->assertRaw('<a href="' . check_plain($url1) . '"' . $rel . '>' . check_plain($title1) . '</a>');
$this->assertRaw('<a href="' . check_plain($url2) . '"' . $rel . '>' . check_plain($title2) . '</a>');
break;
case 'target':
$target = isset($new_value) ? ' target="' . $new_value . '"' : '';
$this->assertRaw('<a href="' . check_plain($url1) . '"' . $target . '>' . check_plain($title1) . '</a>');
$this->assertRaw('<a href="' . check_plain($url2) . '"' . $target . '>' . check_plain($title2) . '</a>');
break;
case 'url_only':
// In this case, $new_value is an array.
if (!$new_value['url_only']) {
$this->assertRaw('<a href="' . check_plain($url1) . '">' . check_plain($title1) . '</a>');
$this->assertRaw('<a href="' . check_plain($url2) . '">' . check_plain($title2) . '</a>');
}
else {
if (empty($new_value['url_plain'])) {
$this->assertRaw('<a href="' . check_plain($url1) . '">' . check_plain($url1) . '</a>');
$this->assertRaw('<a href="' . check_plain($url2) . '">' . check_plain($url2) . '</a>');
}
else {
$this->assertNoRaw('<a href="' . check_plain($url1) . '">' . check_plain($url1) . '</a>');
$this->assertNoRaw('<a href="' . check_plain($url2) . '">' . check_plain($url2) . '</a>');
$this->assertRaw(check_plain($url1));
$this->assertRaw(check_plain($url2));
}
}
break;
}
}
}
}
/**
* Tests the 'link_separate' formatter.
*
* This test is mostly the same as testLinkFormatter(), but they cannot be
* merged, since they involve different configuration and output.
*/
function testLinkSeparateFormatter() {
// Create a field with settings to validate.
$this->field = array(
'field_name' => drupal_strtolower($this->randomName()),
'type' => 'link',
'cardinality' => 2,
);
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'settings' => array(
'title' => DRUPAL_OPTIONAL,
),
'widget' => array(
'type' => 'link_default',
),
'display' => array(
'full' => array(
'type' => 'link_separate',
'label' => 'hidden',
),
),
);
field_create_instance($this->instance);
$langcode = LANGUAGE_NOT_SPECIFIED;
// Create an entity with two link field values:
// - The first field item uses a URL only.
// - The second field item uses a URL and title.
// For consistency in assertion code below, the URL is assigned to the title
// variable for the first field.
$this->drupalGet('test-entity/add/test_bundle');
$url1 = 'http://www.example.com/content/articles/archive?author=John&year=2012#com';
$url2 = 'http://www.example.org/content/articles/archive?author=John&year=2012#org';
// Intentionally contains an ampersand that needs sanitization on output.
$title2 = 'A very long & strange example title that could break the nice layout of the site';
$edit = array(
"{$this->field['field_name']}[$langcode][0][url]" => $url1,
"{$this->field['field_name']}[$langcode][1][url]" => $url2,
"{$this->field['field_name']}[$langcode][1][title]" => $title2,
);
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)));
// Verify that the link is output according to the formatter settings.
$options = array(
'trim_length' => array(NULL, 6),
'rel' => array(NULL, 'nofollow'),
'target' => array(NULL, '_blank'),
);
foreach ($options as $setting => $values) {
foreach ($values as $new_value) {
// Update the field formatter settings.
$this->instance['display']['full']['settings'] = array($setting => $new_value);
field_update_instance($this->instance);
$this->renderTestEntity($id);
switch ($setting) {
case 'trim_length':
$url = $url1;
$url_title = isset($new_value) ? truncate_utf8($url, $new_value, FALSE, TRUE) : $url;
$expected = '<div class="link-item">';
$expected .= '<div class="link-url"><a href="' . check_plain($url) . '">' . check_plain($url_title) . '</a></div>';
$expected .= '</div>';
$this->assertRaw($expected);
$url = $url2;
$url_title = isset($new_value) ? truncate_utf8($url, $new_value, FALSE, TRUE) : $url;
$title = isset($new_value) ? truncate_utf8($title2, $new_value, FALSE, TRUE) : $title2;
$expected = '<div class="link-item">';
$expected .= '<div class="link-title">' . check_plain($title) . '</div>';
$expected .= '<div class="link-url"><a href="' . check_plain($url) . '">' . check_plain($url_title) . '</a></div>';
$expected .= '</div>';
$this->assertRaw($expected);
break;
case 'rel':
$rel = isset($new_value) ? ' rel="' . $new_value . '"' : '';
$this->assertRaw('<div class="link-url"><a href="' . check_plain($url1) . '"' . $rel . '>' . check_plain($url1) . '</a></div>');
$this->assertRaw('<div class="link-url"><a href="' . check_plain($url2) . '"' . $rel . '>' . check_plain($url2) . '</a></div>');
break;
case 'target':
$target = isset($new_value) ? ' target="' . $new_value . '"' : '';
$this->assertRaw('<div class="link-url"><a href="' . check_plain($url1) . '"' . $target . '>' . check_plain($url1) . '</a></div>');
$this->assertRaw('<div class="link-url"><a href="' . check_plain($url2) . '"' . $target . '>' . check_plain($url2) . '</a></div>');
break;
}
}
}
}
/**
* Renders a test_entity and sets the output in the internal browser.
*
* @param int $id
* The test_entity ID to render.
* @param string $view_mode
* (optional) The view mode to use for rendering.
* @param bool $reset
* (optional) Whether to reset the test_entity controller cache. Defaults to
* TRUE to simplify testing.
*/
protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) {
if ($reset) {
entity_get_controller('test_entity')->resetCache(array($id));
}
$entity = field_test_entity_test_load($id);
field_attach_prepare_view('test_entity', array($entity->id() => $entity), $view_mode);
$entity->content = field_attach_view('test_entity', $entity, $view_mode);
$output = drupal_render($entity->content);
$this->drupalSetContent($output);
$this->verbose($output);
}
}
<?php
/**
* @file
* Contains Drupal\link\Tests\LinkFieldUITest.
*/
namespace Drupal\link\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests link field UI functionality.
*/
class LinkFieldUITest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node', 'link', 'field_ui');
public static function getInfo() {
return array(
'name' => 'Link field UI',
'description' => 'Tests link field UI functionality.',
'group' => 'Field types',
);
}
function setUp() {
parent::setUp();
$this->web_user = $this->drupalCreateUser(array('administer content types'));
$this->drupalLogin($this->web_user);
}
/**
* Tests that link field UI functionality does not generate warnings.
*/
function testFieldUI() {
// Add a content type.
$type = $this->drupalCreateContentType();
$type_path = 'admin/structure/types/manage/' . $type->type;
// Add a link field to the newly-created type.
$label = $this->randomName();
$field_name = drupal_strtolower($label);
$edit = array(
'fields[_add_new_field][label]' => $label,
'fields[_add_new_field][field_name]' => $field_name,
'fields[_add_new_field][type]' => 'link',
'fields[_add_new_field][widget_type]' => 'link_default',
);
$this->drupalPost("$type_path/fields", $edit, t('Save'));
// Proceed to the Edit (field instance settings) page.
$this->drupalPost(NULL, array(), t('Save field settings'));
// Proceed to the Manage fields overview page.
$this->drupalPost(NULL, array(), t('Save settings'));
// Load the formatter page to check that the settings summary does not
// generate warnings.
// @todo Mess with the formatter settings a bit here.
$this->drupalGet("$type_path/display");
$this->assertText(t('Link text trimmed to @limit characters', array('@limit' => 80)));
}
}
name = Link
description = Provides a simple link field type.
core = 8.x
package = Core
version = VERSION
dependencies[] = field
<?php
/**
* @file
* Contains install, update, and uninstall functionality for the Link module.
*/
/**
* Implements hook_field_schema().
*/
function link_field_schema($field) {
$schema['columns']['url'] = array(
'description' => 'The URL of the link.',
'type' => 'varchar',
'length' => 2048,
'not null' => FALSE,
);
$schema['columns']['title'] = array(
'description' => 'The link text.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
);
$schema['columns']['attributes'] = array(
'description' => 'Serialized array of attributes for the link.',
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
'serialize' => TRUE,
);
return $schema;