Update core 8.3.0
This commit is contained in:
parent
da7a7918f8
commit
cd7a898e66
6144 changed files with 132297 additions and 87747 deletions
639
web/core/modules/link/tests/src/Functional/LinkFieldTest.php
Normal file
639
web/core/modules/link/tests/src/Functional/LinkFieldTest.php
Normal file
|
@ -0,0 +1,639 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\link\Functional;
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\link\LinkItemInterface;
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
|
||||
/**
|
||||
* Tests link field widgets and formatters.
|
||||
*
|
||||
* @group link
|
||||
*/
|
||||
class LinkFieldTest extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['entity_test', 'link', 'node'];
|
||||
|
||||
/**
|
||||
* A field to use in this test class.
|
||||
*
|
||||
* @var \Drupal\field\Entity\FieldStorageConfig
|
||||
*/
|
||||
protected $fieldStorage;
|
||||
|
||||
/**
|
||||
* The instance used in this test class.
|
||||
*
|
||||
* @var \Drupal\field\Entity\FieldConfig
|
||||
*/
|
||||
protected $field;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->drupalLogin($this->drupalCreateUser([
|
||||
'view test entity',
|
||||
'administer entity_test content',
|
||||
'link to any page',
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests link field URL validation.
|
||||
*/
|
||||
public function testURLValidation() {
|
||||
$field_name = Unicode::strtolower($this->randomMachineName());
|
||||
// Create a field with settings to validate.
|
||||
$this->fieldStorage = FieldStorageConfig::create([
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'link',
|
||||
]);
|
||||
$this->fieldStorage->save();
|
||||
$this->field = FieldConfig::create([
|
||||
'field_storage' => $this->fieldStorage,
|
||||
'bundle' => 'entity_test',
|
||||
'settings' => [
|
||||
'title' => DRUPAL_DISABLED,
|
||||
'link_type' => LinkItemInterface::LINK_GENERIC,
|
||||
],
|
||||
]);
|
||||
$this->field->save();
|
||||
entity_get_form_display('entity_test', 'entity_test', 'default')
|
||||
->setComponent($field_name, [
|
||||
'type' => 'link_default',
|
||||
'settings' => [
|
||||
'placeholder_url' => 'http://example.com',
|
||||
],
|
||||
])
|
||||
->save();
|
||||
entity_get_display('entity_test', 'entity_test', 'full')
|
||||
->setComponent($field_name, [
|
||||
'type' => 'link',
|
||||
])
|
||||
->save();
|
||||
|
||||
// Display creation form.
|
||||
$this->drupalGet('entity_test/add');
|
||||
$this->assertFieldByName("{$field_name}[0][uri]", '', 'Link URL field is displayed');
|
||||
$this->assertRaw('placeholder="http://example.com"');
|
||||
|
||||
// Create a path alias.
|
||||
\Drupal::service('path.alias_storage')->save('/admin', '/a/path/alias');
|
||||
|
||||
// Create a node to test the link widget.
|
||||
$node = $this->drupalCreateNode();
|
||||
|
||||
// Create an entity with restricted view access.
|
||||
$entity_test_no_label_access = EntityTest::create([
|
||||
'name' => 'forbid_access',
|
||||
]);
|
||||
$entity_test_no_label_access->save();
|
||||
|
||||
// Define some valid URLs (keys are the entered values, values are the
|
||||
// strings displayed to the user).
|
||||
$valid_external_entries = [
|
||||
'http://www.example.com/' => 'http://www.example.com/',
|
||||
// Strings within parenthesis without leading space char.
|
||||
'http://www.example.com/strings_(string_within_parenthesis)' => 'http://www.example.com/strings_(string_within_parenthesis)',
|
||||
// Numbers within parenthesis without leading space char.
|
||||
'http://www.example.com/numbers_(9999)' => 'http://www.example.com/numbers_(9999)',
|
||||
];
|
||||
$valid_internal_entries = [
|
||||
'/entity_test/add' => '/entity_test/add',
|
||||
'/a/path/alias' => '/a/path/alias',
|
||||
|
||||
// Front page, with query string and fragment.
|
||||
'/' => '<front>',
|
||||
'/?example=llama' => '<front>?example=llama',
|
||||
'/#example' => '<front>#example',
|
||||
|
||||
// @todo '<front>' is valid input for BC reasons, may be removed by
|
||||
// https://www.drupal.org/node/2421941
|
||||
'<front>' => '<front>',
|
||||
'<front>#example' => '<front>#example',
|
||||
'<front>?example=llama' => '<front>?example=llama',
|
||||
|
||||
// Query string and fragment.
|
||||
'?example=llama' => '?example=llama',
|
||||
'#example' => '#example',
|
||||
|
||||
// Entity reference autocomplete value.
|
||||
$node->label() . ' (1)' => $node->label() . ' (1)',
|
||||
// Entity URI displayed as ER autocomplete value when displayed in a form.
|
||||
'entity:node/1' => $node->label() . ' (1)',
|
||||
// URI for an entity that exists, but is not accessible by the user.
|
||||
'entity:entity_test/' . $entity_test_no_label_access->id() => '- Restricted access - (' . $entity_test_no_label_access->id() . ')',
|
||||
// URI for an entity that doesn't exist, but with a valid ID.
|
||||
'entity:user/999999' => 'entity:user/999999',
|
||||
];
|
||||
|
||||
// Define some invalid URLs.
|
||||
$validation_error_1 = "The path '@link_path' is invalid.";
|
||||
$validation_error_2 = 'Manually entered paths should start with /, ? or #.';
|
||||
$validation_error_3 = "The path '@link_path' is inaccessible.";
|
||||
$invalid_external_entries = [
|
||||
// Invalid protocol
|
||||
'invalid://not-a-valid-protocol' => $validation_error_1,
|
||||
// Missing host name
|
||||
'http://' => $validation_error_1,
|
||||
];
|
||||
$invalid_internal_entries = [
|
||||
'no-leading-slash' => $validation_error_2,
|
||||
'entity:non_existing_entity_type/yar' => $validation_error_1,
|
||||
// URI for an entity that doesn't exist, with an invalid ID.
|
||||
'entity:user/invalid-parameter' => $validation_error_1,
|
||||
];
|
||||
|
||||
// Test external and internal URLs for 'link_type' = LinkItemInterface::LINK_GENERIC.
|
||||
$this->assertValidEntries($field_name, $valid_external_entries + $valid_internal_entries);
|
||||
$this->assertInvalidEntries($field_name, $invalid_external_entries + $invalid_internal_entries);
|
||||
|
||||
// Test external URLs for 'link_type' = LinkItemInterface::LINK_EXTERNAL.
|
||||
$this->field->setSetting('link_type', LinkItemInterface::LINK_EXTERNAL);
|
||||
$this->field->save();
|
||||
$this->assertValidEntries($field_name, $valid_external_entries);
|
||||
$this->assertInvalidEntries($field_name, $valid_internal_entries + $invalid_external_entries);
|
||||
|
||||
// Test external URLs for 'link_type' = LinkItemInterface::LINK_INTERNAL.
|
||||
$this->field->setSetting('link_type', LinkItemInterface::LINK_INTERNAL);
|
||||
$this->field->save();
|
||||
$this->assertValidEntries($field_name, $valid_internal_entries);
|
||||
$this->assertInvalidEntries($field_name, $valid_external_entries + $invalid_internal_entries);
|
||||
|
||||
// Ensure that users with 'link to any page', don't apply access checking.
|
||||
$this->drupalLogin($this->drupalCreateUser([
|
||||
'view test entity',
|
||||
'administer entity_test content',
|
||||
]));
|
||||
$this->assertValidEntries($field_name, ['/entity_test/add' => '/entity_test/add']);
|
||||
$this->assertInValidEntries($field_name, ['/admin' => $validation_error_3]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that valid URLs can be submitted.
|
||||
*
|
||||
* @param string $field_name
|
||||
* The field name.
|
||||
* @param array $valid_entries
|
||||
* An array of valid URL entries.
|
||||
*/
|
||||
protected function assertValidEntries($field_name, array $valid_entries) {
|
||||
foreach ($valid_entries as $uri => $string) {
|
||||
$edit = [
|
||||
"{$field_name}[0][uri]" => $uri,
|
||||
];
|
||||
$this->drupalPostForm('entity_test/add', $edit, t('Save'));
|
||||
preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
|
||||
$id = $match[1];
|
||||
$this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
|
||||
$this->assertRaw($string);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that invalid URLs cannot be submitted.
|
||||
*
|
||||
* @param string $field_name
|
||||
* The field name.
|
||||
* @param array $invalid_entries
|
||||
* An array of invalid URL entries.
|
||||
*/
|
||||
protected function assertInvalidEntries($field_name, array $invalid_entries) {
|
||||
foreach ($invalid_entries as $invalid_value => $error_message) {
|
||||
$edit = [
|
||||
"{$field_name}[0][uri]" => $invalid_value,
|
||||
];
|
||||
$this->drupalPostForm('entity_test/add', $edit, t('Save'));
|
||||
$this->assertText(t($error_message, ['@link_path' => $invalid_value]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the link title settings of a link field.
|
||||
*/
|
||||
public function testLinkTitle() {
|
||||
$field_name = Unicode::strtolower($this->randomMachineName());
|
||||
// Create a field with settings to validate.
|
||||
$this->fieldStorage = FieldStorageConfig::create([
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'link',
|
||||
]);
|
||||
$this->fieldStorage->save();
|
||||
$this->field = FieldConfig::create([
|
||||
'field_storage' => $this->fieldStorage,
|
||||
'bundle' => 'entity_test',
|
||||
'label' => 'Read more about this entity',
|
||||
'settings' => [
|
||||
'title' => DRUPAL_OPTIONAL,
|
||||
'link_type' => LinkItemInterface::LINK_GENERIC,
|
||||
],
|
||||
]);
|
||||
$this->field->save();
|
||||
entity_get_form_display('entity_test', 'entity_test', 'default')
|
||||
->setComponent($field_name, [
|
||||
'type' => 'link_default',
|
||||
'settings' => [
|
||||
'placeholder_url' => 'http://example.com',
|
||||
'placeholder_title' => 'Enter the text for this link',
|
||||
],
|
||||
])
|
||||
->save();
|
||||
entity_get_display('entity_test', 'entity_test', 'full')
|
||||
->setComponent($field_name, [
|
||||
'type' => 'link',
|
||||
'label' => 'hidden',
|
||||
])
|
||||
->save();
|
||||
|
||||
// Verify that the link text field works according to the field setting.
|
||||
foreach ([DRUPAL_DISABLED, DRUPAL_REQUIRED, DRUPAL_OPTIONAL] as $title_setting) {
|
||||
// Update the link title field setting.
|
||||
$this->field->setSetting('title', $title_setting);
|
||||
$this->field->save();
|
||||
|
||||
// Display creation form.
|
||||
$this->drupalGet('entity_test/add');
|
||||
// Assert label is shown.
|
||||
$this->assertText('Read more about this entity');
|
||||
$this->assertFieldByName("{$field_name}[0][uri]", '', 'URL field found.');
|
||||
$this->assertRaw('placeholder="http://example.com"');
|
||||
|
||||
if ($title_setting === DRUPAL_DISABLED) {
|
||||
$this->assertNoFieldByName("{$field_name}[0][title]", '', 'Link text field not found.');
|
||||
$this->assertNoRaw('placeholder="Enter the text for this link"');
|
||||
}
|
||||
else {
|
||||
$this->assertRaw('placeholder="Enter the text for this link"');
|
||||
|
||||
$this->assertFieldByName("{$field_name}[0][title]", '', 'Link text field found.');
|
||||
if ($title_setting === DRUPAL_REQUIRED) {
|
||||
// Verify that the link text is required, if the URL is non-empty.
|
||||
$edit = [
|
||||
"{$field_name}[0][uri]" => 'http://www.example.com',
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
$this->assertText(t('@name field is required.', ['@name' => t('Link text')]));
|
||||
|
||||
// Verify that the link text is not required, if the URL is empty.
|
||||
$edit = [
|
||||
"{$field_name}[0][uri]" => '',
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
$this->assertNoText(t('@name field is required.', ['@name' => t('Link text')]));
|
||||
|
||||
// Verify that a URL and link text meets requirements.
|
||||
$this->drupalGet('entity_test/add');
|
||||
$edit = [
|
||||
"{$field_name}[0][uri]" => 'http://www.example.com',
|
||||
"{$field_name}[0][title]" => 'Example',
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
$this->assertNoText(t('@name field is required.', ['@name' => t('Link text')]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Verify that a link without link text is rendered using the URL as text.
|
||||
$value = 'http://www.example.com/';
|
||||
$edit = [
|
||||
"{$field_name}[0][uri]" => $value,
|
||||
"{$field_name}[0][title]" => '',
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
|
||||
$id = $match[1];
|
||||
$this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
|
||||
|
||||
$output = $this->renderTestEntity($id);
|
||||
$expected_link = (string) \Drupal::l($value, Url::fromUri($value));
|
||||
$this->assertContains($expected_link, $output);
|
||||
|
||||
// Verify that a link with text is rendered using the link text.
|
||||
$title = $this->randomMachineName();
|
||||
$edit = [
|
||||
"{$field_name}[0][title]" => $title,
|
||||
];
|
||||
$this->drupalPostForm("entity_test/manage/$id/edit", $edit, t('Save'));
|
||||
$this->assertText(t('entity_test @id has been updated.', ['@id' => $id]));
|
||||
|
||||
$output = $this->renderTestEntity($id);
|
||||
$expected_link = (string) \Drupal::l($title, Url::fromUri($value));
|
||||
$this->assertContains($expected_link, $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the default 'link' formatter.
|
||||
*/
|
||||
public function testLinkFormatter() {
|
||||
$field_name = Unicode::strtolower($this->randomMachineName());
|
||||
// Create a field with settings to validate.
|
||||
$this->fieldStorage = FieldStorageConfig::create([
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'link',
|
||||
'cardinality' => 3,
|
||||
]);
|
||||
$this->fieldStorage->save();
|
||||
FieldConfig::create([
|
||||
'field_storage' => $this->fieldStorage,
|
||||
'label' => 'Read more about this entity',
|
||||
'bundle' => 'entity_test',
|
||||
'settings' => [
|
||||
'title' => DRUPAL_OPTIONAL,
|
||||
'link_type' => LinkItemInterface::LINK_GENERIC,
|
||||
],
|
||||
])->save();
|
||||
entity_get_form_display('entity_test', 'entity_test', 'default')
|
||||
->setComponent($field_name, [
|
||||
'type' => 'link_default',
|
||||
])
|
||||
->save();
|
||||
$display_options = [
|
||||
'type' => 'link',
|
||||
'label' => 'hidden',
|
||||
];
|
||||
entity_get_display('entity_test', 'entity_test', 'full')
|
||||
->setComponent($field_name, $display_options)
|
||||
->save();
|
||||
|
||||
// Create an entity with three link field values:
|
||||
// - The first field item uses a URL only.
|
||||
// - The second field item uses a URL and link text.
|
||||
// - The third field item uses a fragment-only URL with 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';
|
||||
$url3 = '#net';
|
||||
$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';
|
||||
$title3 = 'Fragment only';
|
||||
$edit = [
|
||||
"{$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,
|
||||
"{$field_name}[2][uri]" => $url3,
|
||||
"{$field_name}[2][title]" => $title3,
|
||||
];
|
||||
// Assert label is shown.
|
||||
$this->assertText('Read more about this entity');
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
|
||||
$id = $match[1];
|
||||
$this->assertText(t('entity_test @id has been created.', ['@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 = [
|
||||
'trim_length' => [NULL, 6],
|
||||
'rel' => [NULL, 'nofollow'],
|
||||
'target' => [NULL, '_blank'],
|
||||
'url_only' => [
|
||||
['url_only' => FALSE],
|
||||
['url_only' => FALSE, 'url_plain' => TRUE],
|
||||
['url_only' => TRUE],
|
||||
['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'] = [$setting => $new_value];
|
||||
}
|
||||
else {
|
||||
$display_options['settings'] = $new_value;
|
||||
}
|
||||
entity_get_display('entity_test', 'entity_test', 'full')
|
||||
->setComponent($field_name, $display_options)
|
||||
->save();
|
||||
|
||||
$output = $this->renderTestEntity($id);
|
||||
switch ($setting) {
|
||||
case 'trim_length':
|
||||
$url = $url1;
|
||||
$title = isset($new_value) ? Unicode::truncate($title1, $new_value, FALSE, TRUE) : $title1;
|
||||
$this->assertContains('<a href="' . Html::escape($url) . '">' . Html::escape($title) . '</a>', $output);
|
||||
|
||||
$url = $url2;
|
||||
$title = isset($new_value) ? Unicode::truncate($title2, $new_value, FALSE, TRUE) : $title2;
|
||||
$this->assertContains('<a href="' . Html::escape($url) . '">' . Html::escape($title) . '</a>', $output);
|
||||
|
||||
$url = $url3;
|
||||
$title = isset($new_value) ? Unicode::truncate($title3, $new_value, FALSE, TRUE) : $title3;
|
||||
$this->assertContains('<a href="' . Html::escape($url) . '">' . Html::escape($title) . '</a>', $output);
|
||||
break;
|
||||
|
||||
case 'rel':
|
||||
$rel = isset($new_value) ? ' rel="' . $new_value . '"' : '';
|
||||
$this->assertContains('<a href="' . Html::escape($url1) . '"' . $rel . '>' . Html::escape($title1) . '</a>', $output);
|
||||
$this->assertContains('<a href="' . Html::escape($url2) . '"' . $rel . '>' . Html::escape($title2) . '</a>', $output);
|
||||
$this->assertContains('<a href="' . Html::escape($url3) . '"' . $rel . '>' . Html::escape($title3) . '</a>', $output);
|
||||
break;
|
||||
|
||||
case 'target':
|
||||
$target = isset($new_value) ? ' target="' . $new_value . '"' : '';
|
||||
$this->assertContains('<a href="' . Html::escape($url1) . '"' . $target . '>' . Html::escape($title1) . '</a>', $output);
|
||||
$this->assertContains('<a href="' . Html::escape($url2) . '"' . $target . '>' . Html::escape($title2) . '</a>', $output);
|
||||
$this->assertContains('<a href="' . Html::escape($url3) . '"' . $target . '>' . Html::escape($title3) . '</a>', $output);
|
||||
break;
|
||||
|
||||
case 'url_only':
|
||||
// In this case, $new_value is an array.
|
||||
if (!$new_value['url_only']) {
|
||||
$this->assertContains('<a href="' . Html::escape($url1) . '">' . Html::escape($title1) . '</a>', $output);
|
||||
$this->assertContains('<a href="' . Html::escape($url2) . '">' . Html::escape($title2) . '</a>', $output);
|
||||
$this->assertContains('<a href="' . Html::escape($url3) . '">' . Html::escape($title3) . '</a>', $output);
|
||||
}
|
||||
else {
|
||||
if (empty($new_value['url_plain'])) {
|
||||
$this->assertContains('<a href="' . Html::escape($url1) . '">' . Html::escape($url1) . '</a>', $output);
|
||||
$this->assertContains('<a href="' . Html::escape($url2) . '">' . Html::escape($url2) . '</a>', $output);
|
||||
$this->assertContains('<a href="' . Html::escape($url3) . '">' . Html::escape($url3) . '</a>', $output);
|
||||
}
|
||||
else {
|
||||
$this->assertNotContains('<a href="' . Html::escape($url1) . '">' . Html::escape($url1) . '</a>', $output);
|
||||
$this->assertNotContains('<a href="' . Html::escape($url2) . '">' . Html::escape($url2) . '</a>', $output);
|
||||
$this->assertNotContains('<a href="' . Html::escape($url3) . '">' . Html::escape($url3) . '</a>', $output);
|
||||
$this->assertContains(Html::escape($url1), $output);
|
||||
$this->assertContains(Html::escape($url2), $output);
|
||||
$this->assertContains(Html::escape($url3), $output);
|
||||
}
|
||||
}
|
||||
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.
|
||||
*/
|
||||
public function testLinkSeparateFormatter() {
|
||||
$field_name = Unicode::strtolower($this->randomMachineName());
|
||||
// Create a field with settings to validate.
|
||||
$this->fieldStorage = FieldStorageConfig::create([
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'link',
|
||||
'cardinality' => 3,
|
||||
]);
|
||||
$this->fieldStorage->save();
|
||||
FieldConfig::create([
|
||||
'field_storage' => $this->fieldStorage,
|
||||
'bundle' => 'entity_test',
|
||||
'settings' => [
|
||||
'title' => DRUPAL_OPTIONAL,
|
||||
'link_type' => LinkItemInterface::LINK_GENERIC,
|
||||
],
|
||||
])->save();
|
||||
$display_options = [
|
||||
'type' => 'link_separate',
|
||||
'label' => 'hidden',
|
||||
];
|
||||
entity_get_form_display('entity_test', 'entity_test', 'default')
|
||||
->setComponent($field_name, [
|
||||
'type' => 'link_default',
|
||||
])
|
||||
->save();
|
||||
entity_get_display('entity_test', 'entity_test', 'full')
|
||||
->setComponent($field_name, $display_options)
|
||||
->save();
|
||||
|
||||
// Create an entity with three link field values:
|
||||
// - The first field item uses a URL only.
|
||||
// - The second field item uses a URL and link text.
|
||||
// - The third field item uses a fragment-only URL with 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';
|
||||
$url3 = '#net';
|
||||
// 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';
|
||||
$title3 = 'Fragment only';
|
||||
$edit = [
|
||||
"{$field_name}[0][uri]" => $url1,
|
||||
"{$field_name}[1][uri]" => $url2,
|
||||
"{$field_name}[1][title]" => $title2,
|
||||
"{$field_name}[2][uri]" => $url3,
|
||||
"{$field_name}[2][title]" => $title3,
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
|
||||
$id = $match[1];
|
||||
$this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
|
||||
|
||||
// Verify that the link is output according to the formatter settings.
|
||||
$options = [
|
||||
'trim_length' => [NULL, 6],
|
||||
'rel' => [NULL, 'nofollow'],
|
||||
'target' => [NULL, '_blank'],
|
||||
];
|
||||
foreach ($options as $setting => $values) {
|
||||
foreach ($values as $new_value) {
|
||||
// Update the field formatter settings.
|
||||
$display_options['settings'] = [$setting => $new_value];
|
||||
entity_get_display('entity_test', 'entity_test', 'full')
|
||||
->setComponent($field_name, $display_options)
|
||||
->save();
|
||||
|
||||
$output = $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->assertContains($expected, $output);
|
||||
|
||||
$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->assertContains($expected, $output);
|
||||
|
||||
$url = $url3;
|
||||
$url_title = isset($new_value) ? Unicode::truncate($url, $new_value, FALSE, TRUE) : $url;
|
||||
$title = isset($new_value) ? Unicode::truncate($title3, $new_value, FALSE, TRUE) : $title3;
|
||||
$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->assertContains($expected, $output);
|
||||
break;
|
||||
|
||||
case 'rel':
|
||||
$rel = isset($new_value) ? ' rel="' . $new_value . '"' : '';
|
||||
$this->assertContains('<div class="link-url"><a href="' . Html::escape($url1) . '"' . $rel . '>' . Html::escape($url1) . '</a></div>', $output);
|
||||
$this->assertContains('<div class="link-url"><a href="' . Html::escape($url2) . '"' . $rel . '>' . Html::escape($url2) . '</a></div>', $output);
|
||||
$this->assertContains('<div class="link-url"><a href="' . Html::escape($url3) . '"' . $rel . '>' . Html::escape($url3) . '</a></div>', $output);
|
||||
break;
|
||||
|
||||
case 'target':
|
||||
$target = isset($new_value) ? ' target="' . $new_value . '"' : '';
|
||||
$this->assertContains('<div class="link-url"><a href="' . Html::escape($url1) . '"' . $target . '>' . Html::escape($url1) . '</a></div>', $output);
|
||||
$this->assertContains('<div class="link-url"><a href="' . Html::escape($url2) . '"' . $target . '>' . Html::escape($url2) . '</a></div>', $output);
|
||||
$this->assertContains('<div class="link-url"><a href="' . Html::escape($url3) . '"' . $target . '>' . Html::escape($url3) . '</a></div>', $output);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a test_entity and returns the output.
|
||||
*
|
||||
* @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 entity_test storage cache. Defaults to
|
||||
* TRUE to simplify testing.
|
||||
*
|
||||
* @return string
|
||||
* The rendered HTML output.
|
||||
*/
|
||||
protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) {
|
||||
if ($reset) {
|
||||
$this->container->get('entity.manager')->getStorage('entity_test')->resetCache([$id]);
|
||||
}
|
||||
$entity = EntityTest::load($id);
|
||||
$display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), $view_mode);
|
||||
$content = $display->build($entity);
|
||||
$output = \Drupal::service('renderer')->renderRoot($content);
|
||||
$output = (string) $output;
|
||||
$this->verbose($output);
|
||||
return $output;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\link\Functional;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\field_ui\Tests\FieldUiTestTrait;
|
||||
use Drupal\link\LinkItemInterface;
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
|
||||
/**
|
||||
* Tests link field UI functionality.
|
||||
*
|
||||
* @group link
|
||||
*/
|
||||
class LinkFieldUITest extends BrowserTestBase {
|
||||
|
||||
use FieldUiTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['node', 'link', 'field_ui', 'block'];
|
||||
|
||||
/**
|
||||
* A user that can edit content types.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->adminUser = $this->drupalCreateUser(['administer content types', 'administer node fields', 'administer node display']);
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->drupalPlaceBlock('system_breadcrumb_block');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the link field UI.
|
||||
*/
|
||||
public function testFieldUI() {
|
||||
// Add a content type.
|
||||
$type = $this->drupalCreateContentType();
|
||||
$type_path = 'admin/structure/types/manage/' . $type->id();
|
||||
$add_path = 'node/add/' . $type->id();
|
||||
|
||||
// Add a link field to the newly-created type. It defaults to allowing both
|
||||
// internal and external links.
|
||||
$label = $this->randomMachineName();
|
||||
$field_name = Unicode::strtolower($label);
|
||||
$this->fieldUIAddNewField($type_path, $field_name, $label, 'link');
|
||||
|
||||
// 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', ['@limit' => 80]));
|
||||
|
||||
// Test the help text displays when the link field allows both internal and
|
||||
// external links.
|
||||
$this->drupalLogin($this->drupalCreateUser(['create ' . $type->id() . ' content']));
|
||||
$this->drupalGet($add_path);
|
||||
$this->assertRaw('You can also enter an internal path such as <em class="placeholder">/node/add</em> or an external URL such as <em class="placeholder">http://example.com</em>.');
|
||||
|
||||
// Log in an admin to set up the next content type.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Add a different content type.
|
||||
$type = $this->drupalCreateContentType();
|
||||
$type_path = 'admin/structure/types/manage/' . $type->id();
|
||||
$add_path = 'node/add/' . $type->id();
|
||||
|
||||
// Add a link field to the newly-created type. Specify it must allow
|
||||
// external only links.
|
||||
$label = $this->randomMachineName();
|
||||
$field_name = Unicode::strtolower($label);
|
||||
$field_edit = ['settings[link_type]' => LinkItemInterface::LINK_EXTERNAL];
|
||||
$this->fieldUIAddNewField($type_path, $field_name, $label, 'link', [], $field_edit);
|
||||
|
||||
// Test the help text displays when link allows only external links.
|
||||
$this->drupalLogin($this->drupalCreateUser(['create ' . $type->id() . ' content']));
|
||||
$this->drupalGet($add_path);
|
||||
$this->assertRaw('This must be an external URL such as <em class="placeholder">http://example.com</em>.');
|
||||
}
|
||||
|
||||
}
|
|
@ -24,7 +24,7 @@ class LinkItemTest extends FieldKernelTestBase {
|
|||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('link');
|
||||
public static $modules = ['link'];
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
@ -78,7 +78,7 @@ class LinkItemTest extends FieldKernelTestBase {
|
|||
$entity->field_test->uri = $parsed_url['path'];
|
||||
$entity->field_test->title = $title;
|
||||
$entity->field_test->first()->get('options')->set('query', $parsed_url['query']);
|
||||
$entity->field_test->first()->get('options')->set('attributes', array('class' => $class));
|
||||
$entity->field_test->first()->get('options')->set('attributes', ['class' => $class]);
|
||||
$this->assertEquals([
|
||||
'query' => $parsed_url['query'],
|
||||
'attributes' => [
|
||||
|
@ -118,7 +118,7 @@ class LinkItemTest extends FieldKernelTestBase {
|
|||
$entity->field_test->uri = $new_url;
|
||||
$entity->field_test->title = $new_title;
|
||||
$entity->field_test->first()->get('options')->set('query', NULL);
|
||||
$entity->field_test->first()->get('options')->set('attributes', array('class' => $new_class));
|
||||
$entity->field_test->first()->get('options')->set('attributes', ['class' => $new_class]);
|
||||
$this->assertEqual($entity->field_test->uri, $new_url);
|
||||
$this->assertEqual($entity->field_test->title, $new_title);
|
||||
$this->assertEqual($entity->field_test->options['attributes']['class'], $new_class);
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace Drupal\Tests\link\Unit\Plugin\Validation\Constraint;
|
|||
use Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraint;
|
||||
use Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
/**
|
||||
* Tests the LinkAccessConstraintValidator validator.
|
||||
|
@ -29,7 +30,7 @@ class LinkAccessConstraintValidatorTest extends UnitTestCase {
|
|||
* @dataProvider providerValidate
|
||||
*/
|
||||
public function testValidate($value, $user, $valid) {
|
||||
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
|
||||
$context = $this->getMock(ExecutionContextInterface::class);
|
||||
|
||||
if ($valid) {
|
||||
$context->expects($this->never())
|
||||
|
|
|
@ -7,6 +7,7 @@ use Drupal\Core\Url;
|
|||
use Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraint;
|
||||
use Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator
|
||||
|
@ -19,7 +20,7 @@ class LinkExternalProtocolsConstraintValidatorTest extends UnitTestCase {
|
|||
* @dataProvider providerValidate
|
||||
*/
|
||||
public function testValidate($value, $valid) {
|
||||
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
|
||||
$context = $this->getMock(ExecutionContextInterface::class);
|
||||
|
||||
if ($valid) {
|
||||
$context->expects($this->never())
|
||||
|
@ -77,7 +78,7 @@ class LinkExternalProtocolsConstraintValidatorTest extends UnitTestCase {
|
|||
->method('getUrl')
|
||||
->willThrowException(new \InvalidArgumentException());
|
||||
|
||||
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
|
||||
$context = $this->getMock(ExecutionContextInterface::class);
|
||||
$context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
|
@ -97,7 +98,7 @@ class LinkExternalProtocolsConstraintValidatorTest extends UnitTestCase {
|
|||
->method('getUrl')
|
||||
->willReturn(Url::fromRoute('example.test'));
|
||||
|
||||
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
|
||||
$context = $this->getMock(ExecutionContextInterface::class);
|
||||
$context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ use Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraint;
|
|||
use Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraintValidator;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraintValidator
|
||||
|
@ -19,7 +20,7 @@ class LinkNotExistingInternalConstraintValidatorTest extends UnitTestCase {
|
|||
* @dataProvider providerValidate
|
||||
*/
|
||||
public function testValidate($value, $valid) {
|
||||
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
|
||||
$context = $this->getMock(ExecutionContextInterface::class);
|
||||
|
||||
if ($valid) {
|
||||
$context->expects($this->never())
|
||||
|
@ -94,7 +95,7 @@ class LinkNotExistingInternalConstraintValidatorTest extends UnitTestCase {
|
|||
->method('getUrl')
|
||||
->willThrowException(new \InvalidArgumentException());
|
||||
|
||||
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
|
||||
$context = $this->getMock(ExecutionContextInterface::class);
|
||||
$context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
|
|
Reference in a new issue