function LinkFieldTest::testLinkFormatter in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/link/src/Tests/LinkFieldTest.php \Drupal\link\Tests\LinkFieldTest::testLinkFormatter()
Tests the default 'link' formatter.
File
- core/
modules/ link/ src/ Tests/ LinkFieldTest.php, line 332 - Contains \Drupal\link\Tests\LinkFieldTest.
Class
- LinkFieldTest
- Tests link field widgets and formatters.
Namespace
Drupal\link\TestsCode
function testLinkFormatter() {
$field_name = Unicode::strtolower($this
->randomMachineName());
// Create a field with settings to validate.
$this->fieldStorage = entity_create('field_storage_config', array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'link',
'cardinality' => 2,
));
$this->fieldStorage
->save();
entity_create('field_config', array(
'field_storage' => $this->fieldStorage,
'label' => 'Read more about this entity',
'bundle' => 'entity_test',
'settings' => array(
'title' => DRUPAL_OPTIONAL,
'link_type' => LinkItemInterface::LINK_GENERIC,
),
))
->save();
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($field_name, array(
'type' => 'link_default',
))
->save();
$display_options = array(
'type' => 'link',
'label' => 'hidden',
);
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($field_name, $display_options)
->save();
// Create an entity with two link field values:
// - The first field item uses a URL only.
// - The second field item uses a URL and link text.
// For consistency in assertion code below, the URL is assigned to the title
// variable for the first field.
$this
->drupalGet('entity_test/add');
$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(
"{$field_name}[0][uri]" => $url1,
// Note that $title1 is not submitted.
"{$field_name}[0][title]" => '',
"{$field_name}[1][uri]" => $url2,
"{$field_name}[1][title]" => $title2,
);
// Assert label is shown.
$this
->assertText('Read more about this entity');
$this
->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
$id = $match[1];
$this
->assertText(t('entity_test @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
// the link generator 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' => FALSE,
),
array(
'url_only' => FALSE,
'url_plain' => TRUE,
),
array(
'url_only' => TRUE,
),
array(
'url_only' => TRUE,
'url_plain' => TRUE,
),
),
);
foreach ($options as $setting => $values) {
foreach ($values as $new_value) {
// Update the field formatter settings.
if (!is_array($new_value)) {
$display_options['settings'] = array(
$setting => $new_value,
);
}
else {
$display_options['settings'] = $new_value;
}
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($field_name, $display_options)
->save();
$this
->renderTestEntity($id);
switch ($setting) {
case 'trim_length':
$url = $url1;
$title = isset($new_value) ? Unicode::truncate($title1, $new_value, FALSE, TRUE) : $title1;
$this
->assertRaw('<a href="' . Html::escape($url) . '">' . Html::escape($title) . '</a>');
$url = $url2;
$title = isset($new_value) ? Unicode::truncate($title2, $new_value, FALSE, TRUE) : $title2;
$this
->assertRaw('<a href="' . Html::escape($url) . '">' . Html::escape($title) . '</a>');
break;
case 'rel':
$rel = isset($new_value) ? ' rel="' . $new_value . '"' : '';
$this
->assertRaw('<a href="' . Html::escape($url1) . '"' . $rel . '>' . Html::escape($title1) . '</a>');
$this
->assertRaw('<a href="' . Html::escape($url2) . '"' . $rel . '>' . Html::escape($title2) . '</a>');
break;
case 'target':
$target = isset($new_value) ? ' target="' . $new_value . '"' : '';
$this
->assertRaw('<a href="' . Html::escape($url1) . '"' . $target . '>' . Html::escape($title1) . '</a>');
$this
->assertRaw('<a href="' . Html::escape($url2) . '"' . $target . '>' . Html::escape($title2) . '</a>');
break;
case 'url_only':
// In this case, $new_value is an array.
if (!$new_value['url_only']) {
$this
->assertRaw('<a href="' . Html::escape($url1) . '">' . Html::escape($title1) . '</a>');
$this
->assertRaw('<a href="' . Html::escape($url2) . '">' . Html::escape($title2) . '</a>');
}
else {
if (empty($new_value['url_plain'])) {
$this
->assertRaw('<a href="' . Html::escape($url1) . '">' . Html::escape($url1) . '</a>');
$this
->assertRaw('<a href="' . Html::escape($url2) . '">' . Html::escape($url2) . '</a>');
}
else {
$this
->assertNoRaw('<a href="' . Html::escape($url1) . '">' . Html::escape($url1) . '</a>');
$this
->assertNoRaw('<a href="' . Html::escape($url2) . '">' . Html::escape($url2) . '</a>');
$this
->assertEscaped($url1);
$this
->assertEscaped($url2);
}
}
break;
}
}
}
}