function LinkFieldTest::testLinkSeparateFormatter in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/link/src/Tests/LinkFieldTest.php \Drupal\link\Tests\LinkFieldTest::testLinkSeparateFormatter()
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.
File
- core/
modules/ link/ src/ Tests/ LinkFieldTest.php, line 472 - Contains \Drupal\link\Tests\LinkFieldTest.
Class
- LinkFieldTest
- Tests link field widgets and formatters.
Namespace
Drupal\link\TestsCode
function testLinkSeparateFormatter() {
$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,
'bundle' => 'entity_test',
'settings' => array(
'title' => DRUPAL_OPTIONAL,
'link_type' => LinkItemInterface::LINK_GENERIC,
),
))
->save();
$display_options = array(
'type' => 'link_separate',
'label' => 'hidden',
);
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($field_name, array(
'type' => 'link_default',
))
->save();
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';
// 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,
"{$field_name}[1][uri]" => $url2,
"{$field_name}[1][title]" => $title2,
);
$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.
$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.
$display_options['settings'] = array(
$setting => $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;
$url_title = isset($new_value) ? Unicode::truncate($url, $new_value, FALSE, TRUE) : $url;
$expected = '<div class="link-item">';
$expected .= '<div class="link-url"><a href="' . Html::escape($url) . '">' . Html::escape($url_title) . '</a></div>';
$expected .= '</div>';
$this
->assertRaw($expected);
$url = $url2;
$url_title = isset($new_value) ? Unicode::truncate($url, $new_value, FALSE, TRUE) : $url;
$title = isset($new_value) ? Unicode::truncate($title2, $new_value, FALSE, TRUE) : $title2;
$expected = '<div class="link-item">';
$expected .= '<div class="link-title">' . Html::escape($title) . '</div>';
$expected .= '<div class="link-url"><a href="' . Html::escape($url) . '">' . Html::escape($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="' . Html::escape($url1) . '"' . $rel . '>' . Html::escape($url1) . '</a></div>');
$this
->assertRaw('<div class="link-url"><a href="' . Html::escape($url2) . '"' . $rel . '>' . Html::escape($url2) . '</a></div>');
break;
case 'target':
$target = isset($new_value) ? ' target="' . $new_value . '"' : '';
$this
->assertRaw('<div class="link-url"><a href="' . Html::escape($url1) . '"' . $target . '>' . Html::escape($url1) . '</a></div>');
$this
->assertRaw('<div class="link-url"><a href="' . Html::escape($url2) . '"' . $target . '>' . Html::escape($url2) . '</a></div>');
break;
}
}
}
}