Update Composer, update everything

This commit is contained in:
Oliver Davies 2018-11-23 12:29:20 +00:00
parent ea3e94409f
commit dda5c284b6
19527 changed files with 1135420 additions and 351004 deletions

View file

@ -5,4 +5,4 @@ core: 8.x
package: Field types
version: VERSION
dependencies:
- field
- drupal:field

View file

@ -32,7 +32,7 @@ class LinkItem extends FieldItemBase implements LinkItemInterface {
public static function defaultFieldSettings() {
return [
'title' => DRUPAL_OPTIONAL,
'link_type' => LinkItemInterface::LINK_GENERIC
'link_type' => LinkItemInterface::LINK_GENERIC,
] + parent::defaultFieldSettings();
}
@ -135,7 +135,7 @@ class LinkItem extends FieldItemBase implements LinkItemInterface {
$values['title'] = mt_rand(0, 1) ? $random->sentences(4) : '';
break;
}
$values['uri'] = 'http://www.' . $random->word($domain_length) . '.' . $tlds[mt_rand(0, (sizeof($tlds) - 1))];
$values['uri'] = 'http://www.' . $random->word($domain_length) . '.' . $tlds[mt_rand(0, (count($tlds) - 1))];
}
else {
$values['uri'] = 'base:' . $random->name(mt_rand(1, 64));

View file

@ -101,7 +101,7 @@ class LinkWidget extends WidgetBase {
*/
protected static function getUserEnteredStringAsUri($string) {
// By default, assume the entered string is an URI.
$uri = $string;
$uri = trim($string);
// Detect entity autocomplete string, map to 'entity:' URI.
$entity_id = EntityAutocomplete::extractEntityIdFromAutocompleteInput($string);
@ -151,10 +151,20 @@ class LinkWidget extends WidgetBase {
*/
public static function validateTitleElement(&$element, FormStateInterface $form_state, $form) {
if ($element['uri']['#value'] !== '' && $element['title']['#value'] === '') {
$element['title']['#required'] = TRUE;
// We expect the field name placeholder value to be wrapped in t() here,
// so it won't be escaped again as it's already marked safe.
$form_state->setError($element['title'], t('@name field is required.', ['@name' => $element['title']['#title']]));
$form_state->setError($element['title'], t('@title field is required if there is @uri input.', ['@title' => $element['title']['#title'], '@uri' => $element['uri']['#title']]));
}
}
/**
* Form element validation handler for the 'title' element.
*
* Requires the URL value if a link title was filled in.
*/
public static function validateTitleNoLink(&$element, FormStateInterface $form_state, $form) {
if ($element['uri']['#value'] === '' && $element['title']['#value'] !== '') {
$form_state->setError($element['uri'], t('The @uri field is required when the @title field is specified.', ['@title' => $element['title']['#title'], '@uri' => $element['uri']['#title']]));
}
}
@ -176,6 +186,7 @@ class LinkWidget extends WidgetBase {
'#element_validate' => [[get_called_class(), 'validateUriElement']],
'#maxlength' => 2048,
'#required' => $element['#required'],
'#link_type' => $this->getFieldSetting('link_type'),
];
// If the field is configured to support internal links, it cannot use the
@ -217,12 +228,39 @@ class LinkWidget extends WidgetBase {
'#default_value' => isset($items[$delta]->title) ? $items[$delta]->title : NULL,
'#maxlength' => 255,
'#access' => $this->getFieldSetting('title') != DRUPAL_DISABLED,
'#required' => $this->getFieldSetting('title') === DRUPAL_REQUIRED && $element['#required'],
];
// Post-process the title field to make it conditionally required if URL is
// non-empty. Omit the validation on the field edit form, since the field
// settings cannot be saved otherwise.
if (!$this->isDefaultValueWidget($form_state) && $this->getFieldSetting('title') == DRUPAL_REQUIRED) {
//
// Validate that title field is filled out (regardless of uri) when it is a
// required field.
if (!$this->isDefaultValueWidget($form_state) && $this->getFieldSetting('title') === DRUPAL_REQUIRED) {
$element['#element_validate'][] = [get_called_class(), 'validateTitleElement'];
$element['#element_validate'][] = [get_called_class(), 'validateTitleNoLink'];
if (!$element['title']['#required']) {
// Make title required on the front-end when URI filled-in.
$field_name = $this->fieldDefinition->getName();
$parents = $element['#field_parents'];
$parents[] = $field_name;
$selector = $root = array_shift($parents);
if ($parents) {
$selector = $root . '[' . implode('][', $parents) . ']';
}
$element['title']['#states']['required'] = [
':input[name="' . $selector . '[' . $delta . '][uri]"]' => ['filled' => TRUE],
];
}
}
// Ensure that a URI is always entered when an optional title field is
// submitted.
if (!$this->isDefaultValueWidget($form_state) && $this->getFieldSetting('title') == DRUPAL_OPTIONAL) {
$element['#element_validate'][] = [get_called_class(), 'validateTitleNoLink'];
}
// Exposing the attributes array in the widget is left for alternate and more
@ -240,6 +278,29 @@ class LinkWidget extends WidgetBase {
// title of the 'uri' element.
if ($this->getFieldSetting('title') == DRUPAL_DISABLED) {
$element['uri']['#title'] = $element['#title'];
// By default the field description is added to the title field. Since
// the title field is disabled, we add the description, if given, to the
// uri element instead.
if (!empty($element['#description'])) {
if (empty($element['uri']['#description'])) {
$element['uri']['#description'] = $element['#description'];
}
else {
// If we have the description of the type of field together with
// the user provided description, we want to make a distinction
// between "core help text" and "user entered help text". To make
// this distinction more clear, we put them in an unordered list.
$element['uri']['#description'] = [
'#theme' => 'item_list',
'#items' => [
// Assume the user-specified description has the most relevance,
// so place it first.
$element['#description'],
$element['uri']['#description'],
],
];
}
}
}
// Otherwise wrap everything in a details element.
else {
@ -337,7 +398,6 @@ class LinkWidget extends WidgetBase {
return $values;
}
/**
* {@inheritdoc}
*

View file

@ -30,7 +30,7 @@ class LinkNotExistingInternalConstraintValidator extends ConstraintValidator {
if ($url->isRouted()) {
$allowed = TRUE;
try {
$url->toString();
$url->toString(TRUE);
}
// The following exceptions are all possible during URL generation, and
// should be considered as disallowed URLs.

View file

@ -0,0 +1,57 @@
<?php
namespace Drupal\link\Plugin\migrate\cckfield;
@trigger_error('LinkField is deprecated in Drupal 8.3.x and will be be removed before Drupal 9.0.x. Use \Drupal\link\Plugin\migrate\field\d6\LinkField instead.', E_USER_DEPRECATED);
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate_drupal\Plugin\migrate\cckfield\CckFieldPluginBase;
/**
* @MigrateCckField(
* id = "link",
* core = {6},
* type_map = {
* "link_field" = "link"
* },
* source_module = "link",
* destination_module = "link"
* )
*
* @deprecated in Drupal 8.3.x and will be removed in Drupal 9.0.x. Use
* \Drupal\link\Plugin\migrate\field\d6\LinkField instead.
*
* @see https://www.drupal.org/node/2751897
*/
class LinkField extends CckFieldPluginBase {
/**
* {@inheritdoc}
*/
public function getFieldFormatterMap() {
// See d6_field_formatter_settings.yml and FieldPluginBase
// alterFieldFormatterMigration().
return [
'default' => 'link',
'plain' => 'link',
'absolute' => 'link',
'title_plain' => 'link',
'url' => 'link',
'short' => 'link',
'label' => 'link',
'separate' => 'link_separate',
];
}
/**
* {@inheritdoc}
*/
public function processCckFieldValues(MigrationInterface $migration, $field_name, $data) {
$process = [
'plugin' => 'd6_cck_link',
'source' => $field_name,
];
$migration->mergeProcessOfProperty($field_name, $process);
}
}

View file

@ -0,0 +1,57 @@
<?php
namespace Drupal\link\Plugin\migrate\cckfield\d7;
@trigger_error('LinkField is deprecated in Drupal 8.3.x and will be be removed before Drupal 9.0.x. Use \Drupal\link\Plugin\migrate\field\d7\LinkField instead.', E_USER_DEPRECATED);
use Drupal\link\Plugin\migrate\cckfield\LinkField as D6LinkField;
use Drupal\migrate\Plugin\MigrationInterface;
/**
* @MigrateCckField(
* id = "link_field",
* core = {7},
* type_map = {
* "link_field" = "link"
* },
* source_module = "link",
* destination_module = "link"
* )
*
* This plugin provides the exact same functionality as the Drupal 6 "link"
* plugin with the exception that the plugin ID "link_field" is used in the
* field type map.
*
* @deprecated in Drupal 8.3.x, to be removed before Drupal 9.0.x. Use
* \Drupal\link\Plugin\migrate\field\d7\LinkField instead.
*
* @see https://www.drupal.org/node/2751897
*/
class LinkField extends D6LinkField {
/**
* {@inheritdoc}
*/
public function getFieldWidgetMap() {
// By default, use the plugin ID for the widget types.
return ['link_field' => 'link_default'];
}
/**
* @inheritdoc}
*/
public function alterFieldInstanceMigration(MigrationInterface $migration) {
$process = [
'plugin' => 'static_map',
'source' => 'settings/title',
'bypass' => TRUE,
'map' => [
'disabled' => DRUPAL_DISABLED,
'optional' => DRUPAL_OPTIONAL,
'required' => DRUPAL_REQUIRED,
],
];
$migration->mergeProcessOfProperty('settings/title', $process);
}
}

View file

@ -11,7 +11,9 @@ use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
* core = {6},
* type_map = {
* "link_field" = "link"
* }
* },
* source_module = "link",
* destination_module = "link"
* )
*/
class LinkField extends FieldPluginBase {
@ -21,7 +23,7 @@ class LinkField extends FieldPluginBase {
*/
public function getFieldFormatterMap() {
// See d6_field_formatter_settings.yml and FieldPluginBase
// processFieldFormatter().
// alterFieldFormatterMigration().
return [
'default' => 'link',
'plain' => 'link',
@ -37,9 +39,9 @@ class LinkField extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function processFieldValues(MigrationInterface $migration, $field_name, $data) {
public function defineValueProcessPipeline(MigrationInterface $migration, $field_name, $data) {
$process = [
'plugin' => 'd6_field_link',
'plugin' => 'field_link',
'source' => $field_name,
];
$migration->mergeProcessOfProperty($field_name, $process);

View file

@ -11,7 +11,9 @@ use Drupal\migrate\Plugin\MigrationInterface;
* core = {7},
* type_map = {
* "link_field" = "link"
* }
* },
* source_module = "link",
* destination_module = "link"
* )
*
* This plugin provides the exact same functionality as the Drupal 6 "link"
@ -20,6 +22,15 @@ use Drupal\migrate\Plugin\MigrationInterface;
*/
class LinkField extends D6LinkField {
/**
* {@inheritdoc}
*/
public function getFieldFormatterMap() {
return [
'link_default' => 'link',
];
}
/**
* {@inheritdoc}
*/
@ -31,10 +42,10 @@ class LinkField extends D6LinkField {
/**
* {@inheritdoc}
*/
public function processFieldInstance(MigrationInterface $migration) {
public function alterFieldInstanceMigration(MigrationInterface $migration) {
$process = [
'plugin' => 'static_map',
'source' => 'instance_settings/title',
'source' => 'settings/title',
'bypass' => TRUE,
'map' => [
'disabled' => DRUPAL_DISABLED,

View file

@ -0,0 +1,133 @@
<?php
namespace Drupal\link\Plugin\migrate\process;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* Transform a pre-Drupal 8 formatted link for use in Drupal 8.
*
* Previous to Drupal 8, URLs didn't need to have a URI scheme assigned. The
* contrib link module would auto-prefix the URL with a URI scheme. A link in
* Drupal 8 has more validation and external links must include the URI scheme.
* All external URIs need to be converted to use a URI scheme.
*
* Available configuration keys
* - uri_scheme: (optional) The URI scheme prefix to use for URLs without a
* scheme. Defaults to 'http://', which was the default in Drupal 6 and
* Drupal 7.
*
* Examples:
*
* Consider a link field migration, where you want to use https:// as the
* prefix:
*
* @code
* process:
* field_link:
* plugin: field_link
* uri_scheme: 'https://'
* source: field_link
* @endcode
*
* @MigrateProcessPlugin(
* id = "field_link"
* )
*/
class FieldLink extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
$configuration += ['uri_scheme' => 'http://'];
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* Turn a Drupal 6/7 URI into a Drupal 8-compatible format.
*
* @param string $uri
* The 'url' value from Drupal 6/7.
*
* @return string
* The Drupal 8-compatible URI.
*
* @see \Drupal\link\Plugin\Field\FieldWidget\LinkWidget::getUserEnteredStringAsUri()
*/
protected function canonicalizeUri($uri) {
// If we already have a scheme, we're fine.
if (empty($uri) || parse_url($uri, PHP_URL_SCHEME)) {
return $uri;
}
// Remove the <front> component of the URL.
if (strpos($uri, '<front>') === 0) {
$uri = substr($uri, strlen('<front>'));
}
else {
// List of unicode-encoded characters that were allowed in URLs,
// according to link module in Drupal 7. Every character between &#x00BF;
// and &#x00FF; (except × &#x00D7; and ÷ &#x00F7;) with the addition of
// &#x0152;, &#x0153; and &#x0178;.
// @see http://cgit.drupalcode.org/link/tree/link.module?h=7.x-1.5-beta2#n1382
$link_ichars = '¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿŒœŸ';
// Pattern specific to internal links.
$internal_pattern = "/^(?:[a-z0-9" . $link_ichars . "_\-+\[\] ]+)";
$directories = "(?:\/[a-z0-9" . $link_ichars . "_\-\.~+%=&,$'#!():;*@\[\]]*)*";
// Yes, four backslashes == a single backslash.
$query = "(?:\/?\?([?a-z0-9" . $link_ichars . "+_|\-\.~\/\\\\%=&,$'():;*@\[\]{} ]*))";
$anchor = "(?:#[a-z0-9" . $link_ichars . "_\-\.~+%=&,$'():;*@\[\]\/\?]*)";
// The rest of the path for a standard URL.
$end = $directories . '?' . $query . '?' . $anchor . '?' . '$/i';
if (!preg_match($internal_pattern . $end, $uri)) {
$link_domains = '[a-z][a-z0-9-]{1,62}';
// Starting a parenthesis group with (?: means that it is grouped, but is not captured
$authentication = "(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=" . $link_ichars . "]|%[0-9a-f]{2})+(?::(?:[\w" . $link_ichars . "\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})*)?)?@)";
$domain = '(?:(?:[a-z0-9' . $link_ichars . ']([a-z0-9' . $link_ichars . '\-_\[\]])*)(\.(([a-z0-9' . $link_ichars . '\-_\[\]])+\.)*(' . $link_domains . '|[a-z]{2}))?)';
$ipv4 = '(?:[0-9]{1,3}(\.[0-9]{1,3}){3})';
$ipv6 = '(?:[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7})';
$port = '(?::([0-9]{1,5}))';
// Pattern specific to external links.
$external_pattern = '/^' . $authentication . '?(' . $domain . '|' . $ipv4 . '|' . $ipv6 . ' |localhost)' . $port . '?';
if (preg_match($external_pattern . $end, $uri)) {
return $this->configuration['uri_scheme'] . $uri;
}
}
}
// Add the internal: scheme and ensure a leading slash.
return 'internal:/' . ltrim($uri, '/');
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$attributes = unserialize($value['attributes']);
// Drupal 6/7 link attributes might be double serialized.
if (!is_array($attributes)) {
$attributes = unserialize($attributes);
}
if (!$attributes) {
$attributes = [];
}
// Massage the values into the correct form for the link.
$route['uri'] = $this->canonicalizeUri($value['url']);
$route['options']['attributes'] = $attributes;
$route['title'] = $value['title'];
return $route;
}
}

View file

@ -2,9 +2,9 @@
namespace Drupal\link\Plugin\migrate\process\d6;
@trigger_error('CckLink is deprecated in Drupal 8.3.x and will be removed before
Drupal 9.0.x. Use \Drupal\link\Plugin\migrate\process\d6\FieldLink instead.',
E_USER_DEPRECATED);
use Drupal\link\Plugin\migrate\process\FieldLink;
@trigger_error('CckLink is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.x. Use \Drupal\link\Plugin\migrate\process\FieldLink instead.', E_USER_DEPRECATED);
/**
* @MigrateProcessPlugin(
@ -12,6 +12,6 @@ E_USER_DEPRECATED);
* )
*
* @deprecated in Drupal 8.3.x, to be removed before Drupal 9.0.x. Use
* \Drupal\link\Plugin\migrate\process\d6\FieldLink instead.
* \Drupal\link\Plugin\migrate\process\FieldLink instead.
*/
class CckLink extends FieldLink { }
class CckLink extends FieldLink {}

View file

@ -2,85 +2,16 @@
namespace Drupal\link\Plugin\migrate\process\d6;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\link\Plugin\migrate\process\FieldLink as GeneralPurposeFieldLink;
@trigger_error('FieldLink is deprecated in Drupal 8.4.x and will be removed before Drupal 9.0.x. Use \Drupal\link\Plugin\migrate\process\FieldLink instead.', E_USER_DEPRECATED);
/**
* @MigrateProcessPlugin(
* id = "d6_field_link"
* )
*
* @deprecated in Drupal 8.4.x, to be removed before Drupal 9.0.x. Use
* \Drupal\link\Plugin\migrate\process\FieldLink instead.
*/
class FieldLink extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migration = $migration;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration
);
}
/**
* Turn a Drupal 6 URI into a Drupal 8-compatible format.
*
* @param string $uri
* The 'url' value from Drupal 6.
*
* @return string
* The Drupal 8-compatible URI.
*
* @see \Drupal\link\Plugin\Field\FieldWidget\LinkWidget::getUserEnteredStringAsUri()
*/
protected function canonicalizeUri($uri) {
// If we already have a scheme, we're fine.
if (empty($uri) || !is_null(parse_url($uri, PHP_URL_SCHEME))) {
return $uri;
}
// Remove the <front> component of the URL.
if (strpos($uri, '<front>') === 0) {
$uri = substr($uri, strlen('<front>'));
}
// Add the internal: scheme and ensure a leading slash.
return 'internal:/' . ltrim($uri, '/');
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$attributes = unserialize($value['attributes']);
// Drupal 6 link attributes might be double serialized.
if (!is_array($attributes)) {
$attributes = unserialize($attributes);
}
if (!$attributes) {
$attributes = [];
}
// Massage the values into the correct form for the link.
$route['uri'] = $this->canonicalizeUri($value['url']);
$route['options']['attributes'] = $attributes;
$route['title'] = $value['title'];
return $route;
}
}
class FieldLink extends GeneralPurposeFieldLink {}

View file

@ -0,0 +1,8 @@
name: Link test base field
description: Tests link field as an optional base field
type: module
core: 8.x
hidden: true
dependencies:
- drupal:link
- drupal:entity_test

View file

@ -0,0 +1,35 @@
<?php
/**
* @file
* Contains main module functions.
*/
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\link\LinkItemInterface;
/**
* Implements hook_entity_base_field_info().
*/
function link_test_base_field_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type->id() === 'entity_test') {
$fields['links'] = BaseFieldDefinition::create('link')
->setLabel(t('Links'))
->setRevisionable(TRUE)
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setDescription(t('Add links to the entity.'))
->setRequired(FALSE)
->setSettings([
'link_type' => LinkItemInterface::LINK_GENERIC,
'title' => DRUPAL_REQUIRED,
])
->setDisplayOptions('form', [
'type' => 'link_default',
'weight' => 49,
]);
}
return $fields;
}

View file

@ -5,6 +5,6 @@ package: Testing
version: VERSION
core: 8.x
dependencies:
- node
- views
- link
- drupal:node
- drupal:views
- drupal:link

View file

@ -24,7 +24,12 @@ class LinkFieldTest extends BrowserTestBase {
*
* @var array
*/
public static $modules = ['entity_test', 'link', 'node'];
public static $modules = [
'entity_test',
'link',
'node',
'link_test_base_field',
];
/**
* A field to use in this test class.
@ -54,7 +59,7 @@ class LinkFieldTest extends BrowserTestBase {
* Tests link field URL validation.
*/
public function testURLValidation() {
$field_name = Unicode::strtolower($this->randomMachineName());
$field_name = mb_strtolower($this->randomMachineName());
// Create a field with settings to validate.
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
@ -116,6 +121,10 @@ class LinkFieldTest extends BrowserTestBase {
'/?example=llama' => '&lt;front&gt;?example=llama',
'/#example' => '&lt;front&gt;#example',
// Trailing spaces should be ignored.
'/ ' => '&lt;front&gt;',
'/path with spaces ' => '/path with spaces',
// @todo '<front>' is valid input for BC reasons, may be removed by
// https://www.drupal.org/node/2421941
'<front>' => '&lt;front&gt;',
@ -195,7 +204,7 @@ class LinkFieldTest extends BrowserTestBase {
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);
$this->assertRaw('"' . $string . '"');
}
}
@ -221,7 +230,7 @@ class LinkFieldTest extends BrowserTestBase {
* Tests the link title settings of a link field.
*/
public function testLinkTitle() {
$field_name = Unicode::strtolower($this->randomMachineName());
$field_name = mb_strtolower($this->randomMachineName());
// Create a field with settings to validate.
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
@ -276,13 +285,21 @@ class LinkFieldTest extends BrowserTestBase {
$this->assertRaw('placeholder="Enter the text for this link"');
$this->assertFieldByName("{$field_name}[0][title]", '', 'Link text field found.');
if ($title_setting === DRUPAL_OPTIONAL) {
// Verify that the URL is required, if the link text is non-empty.
$edit = [
"{$field_name}[0][title]" => 'Example',
];
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertText(t('The URL field is required when the @title field is specified.', ['@title' => t('Link text')]));
}
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')]));
$this->assertText(t('@title field is required if there is @uri input.', ['@title' => t('Link text'), '@uri' => t('URL')]));
// Verify that the link text is not required, if the URL is empty.
$edit = [
@ -335,7 +352,7 @@ class LinkFieldTest extends BrowserTestBase {
* Tests the default 'link' formatter.
*/
public function testLinkFormatter() {
$field_name = Unicode::strtolower($this->randomMachineName());
$field_name = mb_strtolower($this->randomMachineName());
// Create a field with settings to validate.
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
@ -490,7 +507,7 @@ class LinkFieldTest extends BrowserTestBase {
* merged, since they involve different configuration and output.
*/
public function testLinkSeparateFormatter() {
$field_name = Unicode::strtolower($this->randomMachineName());
$field_name = mb_strtolower($this->randomMachineName());
// Create a field with settings to validate.
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
@ -606,6 +623,48 @@ class LinkFieldTest extends BrowserTestBase {
}
}
/**
* Test '#link_type' property exists on 'link_default' widget.
*
* Make sure the 'link_default' widget exposes a '#link_type' property on
* its element. Modules can use it to understand if a text form element is
* a link and also which LinkItemInterface::LINK_* is (EXTERNAL, GENERIC,
* INTERNAL).
*/
public function testLinkTypeOnLinkWidget() {
$link_type = LinkItemInterface::LINK_EXTERNAL;
$field_name = mb_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' => 1,
]);
$this->fieldStorage->save();
FieldConfig::create([
'field_storage' => $this->fieldStorage,
'label' => 'Read more about this entity',
'bundle' => 'entity_test',
'settings' => [
'title' => DRUPAL_OPTIONAL,
'link_type' => $link_type,
],
])->save();
$this->container->get('entity.manager')
->getStorage('entity_form_display')
->load('entity_test.entity_test.default')
->setComponent($field_name, [
'type' => 'link_default',
])
->save();
$form = \Drupal::service('entity.form_builder')->getForm(EntityTest::create());
$this->assertEqual($form[$field_name]['widget'][0]['uri']['#link_type'], $link_type);
}
/**
* Tests editing a link to a non-node entity.

View file

@ -2,10 +2,13 @@
namespace Drupal\Tests\link\Functional;
use Drupal\Component\Utility\Unicode;
use Drupal\field_ui\Tests\FieldUiTestTrait;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\link\LinkItemInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
/**
* Tests link field UI functionality.
@ -30,14 +33,37 @@ class LinkFieldUITest extends BrowserTestBase {
*/
protected $adminUser;
/**
* A user that should see the help texts.
*
* @var \Drupal\user\Entity\User
*/
protected $helpTextUser;
/**
* The first content type to add fields to.
*
* @var \Drupal\node\Entity\NodeType
*/
protected $firstContentType;
/**
* The second content type to add fields to.
*
* @var \Drupal\node\Entity\NodeType
*/
protected $secondContentType;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->firstContentType = $this->drupalCreateContentType();
$this->secondContentType = $this->drupalCreateContentType();
$this->adminUser = $this->drupalCreateUser(['administer content types', 'administer node fields', 'administer node display']);
$this->drupalLogin($this->adminUser);
$this->helpTextUser = $this->drupalCreateUser(['create ' . $this->secondContentType->id() . ' content']);
$this->drupalPlaceBlock('system_breadcrumb_block');
}
@ -45,16 +71,91 @@ class LinkFieldUITest extends BrowserTestBase {
* 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();
foreach ($this->providerTestFieldUI() as $item) {
list($cardinality, $link_type, $title, $label, $field_name) = $item;
$this->runFieldUIItem($cardinality, $link_type, $title, $label, $field_name);
}
}
/**
* Provides test data for ::testFieldUI().
*/
protected function providerTestFieldUI() {
// There are many combinations of field settings: where the description
// should show: variation on internal, external, both; cardinality (where
// the fieldset is hidden or used); and link text shown (required or
// optional) or disabled. There are two descriptions: field and URL help
// text.
$cardinalities = [1, 2];
$title_settings = [
DRUPAL_DISABLED,
DRUPAL_OPTIONAL,
];
$link_types = [
LinkItemInterface::LINK_EXTERNAL,
LinkItemInterface::LINK_GENERIC,
LinkItemInterface::LINK_INTERNAL,
];
// Test all variations of link types on all cardinalities.
foreach ($cardinalities as $cardinality) {
foreach ($link_types as $link_type) {
// Now, test this with both the title enabled and disabled.
foreach ($title_settings as $title_setting) {
// Test both empty and non-empty labels.
foreach ([TRUE, FALSE] as $label_provided) {
// Generate a unique machine name for the field so it can be
// identified in the test.
$id = implode('_', [
'link',
$cardinality,
$link_type,
$title_setting,
(int) $label_provided,
]);
// Use a unique label that contains some HTML.
$label = '<img src="http://example.com">' . $id;
yield [
$cardinality,
$link_type,
$title_setting,
$label_provided ? $label : '',
$id,
];
}
}
}
}
}
/**
* Tests one link field UI item.
*
* @param int $cardinality
* The field cardinality.
* @param int $link_type
* Determine if the link is external, internal or both.
* @param int $title
* Determine if the field will display the link text field.
* @param string $label
* The field label.
* @param string $field_name
* The unique machine name for the field.
*/
public function runFieldUIItem($cardinality, $link_type, $title, $label, $field_name) {
$this->drupalLogin($this->adminUser);
$type_path = 'admin/structure/types/manage/' . $this->firstContentType->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');
$field_label = str_replace('_', ' ', $field_name);
$description = 'link field description';
$field_edit = [
'description' => $description,
];
$this->fieldUIAddNewField($type_path, $field_name, $field_label, 'link', [], $field_edit);
// Load the formatter page to check that the settings summary does not
// generate warnings.
@ -62,31 +163,94 @@ class LinkFieldUITest extends BrowserTestBase {
$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']));
$storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'link',
'cardinality' => $cardinality,
]);
$storage->save();
FieldConfig::create([
'field_storage' => $storage,
'label' => $label,
'bundle' => $this->secondContentType->id(),
'settings' => [
'title' => $title,
'link_type' => $link_type,
],
])->save();
// Make the fields visible in the form display.
$form_display_id = implode('.', ['node', $this->secondContentType->id(), 'default']);
$form_display = EntityFormDisplay::load($form_display_id);
$form_display->setComponent($field_name, ['region' => 'content']);
$form_display->save();
// Log in a user that is allowed to create this content type, see if
// the user can see the expected help text.
$this->drupalLogin($this->helpTextUser);
$add_path = 'node/add/' . $this->secondContentType->id();
$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);
$expected_help_texts = [
LinkItemInterface::LINK_EXTERNAL => 'This must be an external URL such as <em class="placeholder">http://example.com</em>.',
LinkItemInterface::LINK_GENERIC => '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>. Enter <em class="placeholder">&lt;front&gt;</em> to link to the front page.',
LinkItemInterface::LINK_INTERNAL => rtrim(\Drupal::url('<front>', [], ['absolute' => TRUE]), '/'),
];
// Add a different content type.
$type = $this->drupalCreateContentType();
$type_path = 'admin/structure/types/manage/' . $type->id();
$add_path = 'node/add/' . $type->id();
// Check that the help texts we assume should be there, is there.
$this->assertFieldContainsRawText($field_name, $expected_help_texts[$link_type]);
if ($link_type === LinkItemInterface::LINK_INTERNAL) {
// Internal links have no "system" description. Test that none
// of the other help texts show here.
$this->assertNoFieldContainsRawText($field_name, $expected_help_texts[LinkItemInterface::LINK_EXTERNAL]);
$this->assertNoFieldContainsRawText($field_name, $expected_help_texts[LinkItemInterface::LINK_GENERIC]);
}
// Also assert that the description we made is here, no matter what the
// cardinality or link setting.
if (!empty($label)) {
$this->assertFieldContainsRawText($field_name, $label);
}
}
// 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);
/**
* Checks that given field contains the given raw text.
*
* @param string $field_name
* The name of the field to check.
* @param string $text
* The text to check.
*/
protected function assertFieldContainsRawText($field_name, $text) {
$this->assertTrue((bool) preg_match('/' . preg_quote($text, '/') . '/ui', $this->getFieldHtml($field_name)));
}
// 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>.');
/**
* Checks that given field does not contain the given raw text.
*
* @param string $field_name
* The name of the field to check.
* @param string $text
* The text to check.
*/
protected function assertNoFieldContainsRawText($field_name, $text) {
$this->assertFalse((bool) preg_match('/' . preg_quote($text, '/') . '/ui', $this->getFieldHtml($field_name)));
}
/**
* Returns the raw HTML for the given field.
*
* @param $field_name
* The name of the field for which to return the HTML.
*
* @return string
* The raw HTML.
*/
protected function getFieldHtml($field_name) {
$css_id = Html::cleanCssIdentifier('edit-' . $field_name . '-wrapper');
return $this->xpath('//*[@id=:id]', [':id' => $css_id])[0]->getHtml();
}
}

View file

@ -1,10 +1,10 @@
<?php
namespace Drupal\link\Tests\Views;
namespace Drupal\Tests\link\Functional\Views;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\views\Tests\ViewTestBase;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
/**
@ -38,14 +38,14 @@ class LinkViewsTokensTest extends ViewTestBase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
ViewTestData::createTestViews(get_class($this), ['link_test_views']);
// Create Basic page node type.
$this->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page'
'name' => 'Basic page',
]);
// Create a field.

View file

@ -0,0 +1,83 @@
<?php
namespace Drupal\Tests\link\Kernel;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\link\LinkItemInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
/**
* Tests link field serialization.
*
* @group link
*/
class LinkItemSerializationTest extends FieldKernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['link', 'serialization'];
/**
* The serializer service.
*
* @var \Symfony\Component\Serializer\SerializerInterface
*/
protected $serializer;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('user');
$this->serializer = \Drupal::service('serializer');
// Create a generic, external, and internal link fields for validation.
FieldStorageConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'field_test',
'type' => 'link',
])->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'field_test',
'bundle' => 'entity_test',
'settings' => ['link_type' => LinkItemInterface::LINK_GENERIC],
])->save();
}
/**
* Tests the serialization.
*/
public function testLinkSerialization() {
// Create entity.
$entity = EntityTest::create();
$url = 'https://www.drupal.org?test_param=test_value';
$parsed_url = UrlHelper::parse($url);
$title = $this->randomMachineName();
$class = $this->randomMachineName();
$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', ['class' => $class]);
$entity->save();
$serialized = $this->serializer->serialize($entity, 'json');
$deserialized = $this->serializer->deserialize($serialized, EntityTest::class, 'json');
$options_expected = [
'query' => $parsed_url['query'],
'attributes' => ['class' => $class],
];
$this->assertSame($options_expected, $deserialized->field_test->options);
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Drupal\Tests\link\Kernel\Plugin\migrate\cckfield\d7;
/**
* @group link
* @group legacy
*/
class LinkCckDeprecationTest extends LinkCckTest {
/**
* @expectedDeprecation Deprecated in Drupal 8.6.0, to be removed before Drupal 9.0.0. Use alterFieldInstanceMigration() instead. See https://www.drupal.org/node/2944598.
*/
public function testAlterFieldInstanceMigration($method = 'processFieldInstance') {
parent::testAlterFieldInstanceMigration($method);
}
}

View file

@ -0,0 +1,73 @@
<?php
namespace Drupal\Tests\link\Kernel\Plugin\migrate\cckfield\d7;
use Drupal\KernelTests\KernelTestBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\link\Plugin\migrate\cckfield\d7\LinkField;
use Prophecy\Argument;
/**
* @coversDefaultClass \Drupal\link\Plugin\migrate\cckfield\d7\LinkField
* @group link
* @group legacy
*/
class LinkCckTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['system'];
/**
* @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface
*/
protected $plugin;
/**
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->plugin = new LinkField([], 'link', []);
$migration = $this->prophesize(MigrationInterface::class);
// The plugin's alterFieldInstanceMigration() method will call
// mergeProcessOfProperty() and return nothing. So, in order to examine the
// process pipeline created by the plugin, we need to ensure that
// getProcess() always returns the last input to mergeProcessOfProperty().
$migration->mergeProcessOfProperty(Argument::type('string'), Argument::type('array'))
->will(function ($arguments) use ($migration) {
$migration->getProcess()->willReturn($arguments[1]);
});
$this->migration = $migration->reveal();
}
/**
* @covers ::alterFieldInstanceMigration
*/
public function testAlterFieldInstanceMigration($method = 'alterFieldInstanceMigration') {
$this->plugin->$method($this->migration);
$expected = [
'plugin' => 'static_map',
'source' => 'settings/title',
'bypass' => TRUE,
'map' => [
'disabled' => DRUPAL_DISABLED,
'optional' => DRUPAL_OPTIONAL,
'required' => DRUPAL_REQUIRED,
],
];
$this->assertSame($expected, $this->migration->getProcess());
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Drupal\Tests\link\Kernel\Plugin\migrate\field\d7;
/**
* @group legacy
* @group link
*/
class LinkFieldLegacyTest extends LinkFieldTest {
/**
* @expectedDeprecation Deprecated in Drupal 8.6.0, to be removed before Drupal 9.0.0. Use alterFieldInstanceMigration() instead. See https://www.drupal.org/node/2944598.
*/
public function testAlterFieldInstanceMigration($method = 'processFieldInstance') {
parent::testAlterFieldInstanceMigration($method);
}
}

View file

@ -0,0 +1,72 @@
<?php
namespace Drupal\Tests\link\Kernel\Plugin\migrate\field\d7;
use Drupal\KernelTests\KernelTestBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\link\Plugin\migrate\field\d7\LinkField;
use Prophecy\Argument;
/**
* @coversDefaultClass \Drupal\link\Plugin\migrate\field\d7\LinkField
* @group link
*/
class LinkFieldTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['system'];
/**
* @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface
*/
protected $plugin;
/**
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->plugin = new LinkField([], 'link', []);
$migration = $this->prophesize(MigrationInterface::class);
// The plugin's alterFieldInstanceMigration() method will call
// mergeProcessOfProperty() and return nothing. So, in order to examine the
// process pipeline created by the plugin, we need to ensure that
// getProcess() always returns the last input to mergeProcessOfProperty().
$migration->mergeProcessOfProperty(Argument::type('string'), Argument::type('array'))
->will(function ($arguments) use ($migration) {
$migration->getProcess()->willReturn($arguments[1]);
});
$this->migration = $migration->reveal();
}
/**
* @covers ::alterFieldInstanceMigration
*/
public function testAlterFieldInstanceMigration($method = 'alterFieldInstanceMigration') {
$this->plugin->$method($this->migration);
$expected = [
'plugin' => 'static_map',
'source' => 'settings/title',
'bypass' => TRUE,
'map' => [
'disabled' => DRUPAL_DISABLED,
'optional' => DRUPAL_OPTIONAL,
'required' => DRUPAL_REQUIRED,
],
];
$this->assertSame($expected, $this->migration->getProcess());
}
}

View file

@ -31,7 +31,6 @@ class LinkNotExistingInternalConstraintValidatorTest extends UnitTestCase {
->method('addViolation');
}
$constraint = new LinkNotExistingInternalConstraint();
$validator = new LinkNotExistingInternalConstraintValidator();

View file

@ -0,0 +1,61 @@
<?php
namespace Drupal\Tests\link\Unit\Plugin\migrate\cckfield;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\link\Plugin\migrate\cckfield\LinkField;
use Prophecy\Argument;
/**
* @coversDefaultClass \Drupal\link\Plugin\migrate\cckfield\LinkField
* @group link
* @group legacy
*/
class LinkCckTest extends UnitTestCase {
/**
* @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface
*/
protected $plugin;
/**
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->plugin = new LinkField([], 'link', []);
$migration = $this->prophesize(MigrationInterface::class);
// The plugin's processCckFieldValues() method will call
// mergeProcessOfProperty() and return nothing. So, in order to examine the
// process pipeline created by the plugin, we need to ensure that
// getProcess() always returns the last input to mergeProcessOfProperty().
$migration->mergeProcessOfProperty(Argument::type('string'), Argument::type('array'))
->will(function ($arguments) use ($migration) {
$migration->getProcess()->willReturn($arguments[1]);
});
$this->migration = $migration->reveal();
}
/**
* @covers ::processCckFieldValues
* @expectedDeprecation LinkField is deprecated in Drupal 8.3.x and will be be removed before Drupal 9.0.x. Use \Drupal\link\Plugin\migrate\field\d6\LinkField instead.
*/
public function testProcessCckFieldValues() {
$this->plugin->processCckFieldValues($this->migration, 'somefieldname', []);
$expected = [
'plugin' => 'd6_cck_link',
'source' => 'somefieldname',
];
$this->assertSame($expected, $this->migration->getProcess());
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Drupal\Tests\link\Unit\Plugin\migrate\field\d6;
/**
* @group legacy
* @group link
*/
class LinkFieldLegacyTest extends LinkFieldTest {
/**
* @expectedDeprecation Deprecated in Drupal 8.6.0, to be removed before Drupal 9.0.0. Use defineValueProcessPipeline() instead. See https://www.drupal.org/node/2944598.
*/
public function testDefineValueProcessPipeline($method = 'processFieldValues') {
parent::testDefineValueProcessPipeline($method);
}
}

View file

@ -0,0 +1,59 @@
<?php
namespace Drupal\Tests\link\Unit\Plugin\migrate\field\d6;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\link\Plugin\migrate\field\d6\LinkField;
use Prophecy\Argument;
/**
* @coversDefaultClass \Drupal\link\Plugin\migrate\field\d6\LinkField
* @group link
*/
class LinkFieldTest extends UnitTestCase {
/**
* @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface
*/
protected $plugin;
/**
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->plugin = new LinkField([], 'link', []);
$migration = $this->prophesize(MigrationInterface::class);
// The plugin's defineValueProcessPipeline() method will call
// mergeProcessOfProperty() and return nothing. So, in order to examine the
// process pipeline created by the plugin, we need to ensure that
// getProcess() always returns the last input to mergeProcessOfProperty().
$migration->mergeProcessOfProperty(Argument::type('string'), Argument::type('array'))
->will(function ($arguments) use ($migration) {
$migration->getProcess()->willReturn($arguments[1]);
});
$this->migration = $migration->reveal();
}
/**
* @covers ::defineValueProcessPipeline
*/
public function testDefineValueProcessPipeline($method = 'defineValueProcessPipeline') {
$this->plugin->$method($this->migration, 'somefieldname', []);
$expected = [
'plugin' => 'field_link',
'source' => 'somefieldname',
];
$this->assertSame($expected, $this->migration->getProcess());
}
}

View file

@ -0,0 +1,92 @@
<?php
namespace Drupal\Tests\link\Unit\Plugin\migrate\process;
use Drupal\link\Plugin\migrate\process\FieldLink;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\Tests\UnitTestCase;
/**
* @group Link
*/
class FieldLinkTest extends UnitTestCase {
/**
* Test the url transformations in the FieldLink process plugin.
*
* @dataProvider canonicalizeUriDataProvider
*/
public function testCanonicalizeUri($url, $expected, $configuration = []) {
$link_plugin = new FieldLink($configuration, '', [], $this->getMock(MigrationInterface::class));
$transformed = $link_plugin->transform([
'url' => $url,
'title' => '',
'attributes' => serialize([]),
], $this->getMock(MigrateExecutableInterface::class), $this->getMockBuilder(Row::class)->disableOriginalConstructor()->getMock(), NULL);
$this->assertEquals($expected, $transformed['uri']);
}
/**
* Data provider for testCanonicalizeUri.
*/
public function canonicalizeUriDataProvider() {
return [
'Simple front-page' => [
'<front>',
'internal:/',
],
'Front page with query' => [
'<front>?query=1',
'internal:/?query=1',
],
'No leading forward slash' => [
'node/10',
'internal:/node/10',
],
'Leading forward slash' => [
'/node/10',
'internal:/node/10',
],
'Existing scheme' => [
'scheme:test',
'scheme:test',
],
'Absolute URL with protocol prefix' => [
'http://www.google.com',
'http://www.google.com',
],
'Absolute URL without protocol prefix' => [
'www.yahoo.com',
'http://www.yahoo.com',
],
'Absolute URL without protocol prefix nor www' => [
'yahoo.com',
'https://yahoo.com',
['uri_scheme' => 'https://'],
],
'Absolute URL with non-standard characters' => [
'http://www.ßÀÑÐ¥ƒå¢ë.com',
'http://www.ßÀÑÐ¥ƒå¢ë.com',
],
'Absolute URL with non-standard characters, without protocol prefix' => [
'www.ÐØÑ¢åþë.com',
'http://www.ÐØÑ¢åþë.com',
],
'Absolute URL with non-standard top level domain' => [
'http://www.example.xxx',
'http://www.example.xxx',
],
'Internal link with fragment' => [
'/node/10#top',
'internal:/node/10#top',
],
'External link with fragment' => [
'http://www.example.com/page#links',
'http://www.example.com/page#links',
],
];
}
}

View file

@ -7,6 +7,7 @@ use Drupal\Tests\UnitTestCase;
/**
* @group Link
* @group legacy
*/
class FieldLinkTest extends UnitTestCase {