only in patch2: unchanged: --- a/core/modules/link/tests/src/Functional/LinkFieldUITest.php +++ b/core/modules/link/tests/src/Functional/LinkFieldUITest.php @@ -5,8 +5,6 @@ use Drupal\Core\Url; 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; @@ -42,18 +40,11 @@ class LinkFieldUITest extends BrowserTestBase { protected $helpTextUser; /** - * The first content type to add fields to. + * The 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; + protected $contentType; /** * {@inheritdoc} @@ -61,10 +52,9 @@ class LinkFieldUITest extends BrowserTestBase { protected function setUp() { parent::setUp(); - $this->firstContentType = $this->drupalCreateContentType(); - $this->secondContentType = $this->drupalCreateContentType(); + $this->contentType = $this->drupalCreateContentType(); $this->adminUser = $this->drupalCreateUser(['administer content types', 'administer node fields', 'administer node display']); - $this->helpTextUser = $this->drupalCreateUser(['create ' . $this->secondContentType->id() . ' content']); + $this->helpTextUser = $this->drupalCreateUser(['create ' . $this->contentType->id() . ' content']); $this->drupalPlaceBlock('system_breadcrumb_block'); } @@ -73,8 +63,8 @@ protected function setUp() { */ public function testFieldUI() { foreach ($this->providerTestFieldUI() as $item) { - list($cardinality, $link_type, $title, $label, $field_name) = $item; - $this->runFieldUIItem($cardinality, $link_type, $title, $label, $field_name); + list($cardinality, $link_type, $title, $label, $field_name, $default_uri) = $item; + $this->runFieldUIItem($cardinality, $link_type, $title, $label, $field_name, $default_uri); } } @@ -93,14 +83,14 @@ protected function providerTestFieldUI() { DRUPAL_OPTIONAL, ]; $link_types = [ - LinkItemInterface::LINK_EXTERNAL, - LinkItemInterface::LINK_GENERIC, - LinkItemInterface::LINK_INTERNAL, + LinkItemInterface::LINK_EXTERNAL => 'http://drupal.org', + 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) { + foreach ($link_types as $link_type => $default_uri) { // Now, test this with both the title enabled and disabled. foreach ($title_settings as $title_setting) { // Test both empty and non-empty labels. @@ -124,6 +114,7 @@ protected function providerTestFieldUI() { $title_setting, $label_provided ? $label : '', $id, + $default_uri, ]; } } @@ -144,19 +135,27 @@ protected function providerTestFieldUI() { * The field label. * @param string $field_name * The unique machine name for the field. + * @param string $default_uri + * The default URI value. */ - public function runFieldUIItem($cardinality, $link_type, $title, $label, $field_name) { + public function runFieldUIItem($cardinality, $link_type, $title, $label, $field_name, $default_uri) { $this->drupalLogin($this->adminUser); - $type_path = 'admin/structure/types/manage/' . $this->firstContentType->id(); + $type_path = 'admin/structure/types/manage/' . $this->contentType->id(); - // Add a link field to the newly-created type. It defaults to allowing both - // internal and external links. - $field_label = str_replace('_', ' ', $field_name); + // Add a link field to the newly-created type. $description = 'link field description'; $field_edit = [ 'description' => $description, + 'settings[link_type]' => (int) $link_type, ]; - $this->fieldUIAddNewField($type_path, $field_name, $field_label, 'link', [], $field_edit); + if (!empty($default_uri)) { + $field_edit['default_value_input[field_' . $field_name . '][0][uri]'] = $default_uri; + $field_edit['default_value_input[field_' . $field_name . '][0][title]'] = 'Default title'; + } + $storage_edit = [ + 'cardinality_number' => $cardinality, + ]; + $this->fieldUIAddNewField($type_path, $field_name, $label, 'link', $storage_edit, $field_edit); // Load the formatter page to check that the settings summary does not // generate warnings. @@ -164,26 +163,8 @@ public function runFieldUIItem($cardinality, $link_type, $title, $label, $field_ $this->drupalGet("$type_path/display"); $this->assertText(t('Link text trimmed to @limit characters', ['@limit' => 80])); - $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_id = implode('.', ['node', $this->contentType->id(), 'default']); $form_display = EntityFormDisplay::load($form_display_id); $form_display->setComponent($field_name, ['region' => 'content']); $form_display->save(); @@ -192,7 +173,7 @@ public function runFieldUIItem($cardinality, $link_type, $title, $label, $field_ // the user can see the expected help text. $this->drupalLogin($this->helpTextUser); - $add_path = 'node/add/' . $this->secondContentType->id(); + $add_path = 'node/add/' . $this->contentType->id(); $this->drupalGet($add_path); $expected_help_texts = [ @@ -214,6 +195,9 @@ public function runFieldUIItem($cardinality, $link_type, $title, $label, $field_ if (!empty($label)) { $this->assertFieldContainsRawText($field_name, $label); } + + // Test the default field value is used as expected. + $this->assertSession()->fieldValueEquals('field_' . $field_name . '[0][uri]', $default_uri); } /** @@ -250,7 +234,7 @@ protected function assertNoFieldContainsRawText($field_name, $text) { * The raw HTML. */ protected function getFieldHtml($field_name) { - $css_id = Html::cleanCssIdentifier('edit-' . $field_name . '-wrapper'); + $css_id = Html::cleanCssIdentifier('edit-field-' . $field_name . '-wrapper'); return $this->xpath('//*[@id=:id]', [':id' => $css_id])[0]->getHtml(); }