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;
}
}
}
}