diff --git a/core/modules/link/src/Tests/LinkFieldTest.php b/core/modules/link/src/Tests/LinkFieldTest.php index bfca381..c369716 100644 --- a/core/modules/link/src/Tests/LinkFieldTest.php +++ b/core/modules/link/src/Tests/LinkFieldTest.php @@ -597,9 +597,9 @@ protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) { } /** - * Tests if the link text is required. + * Tests if the URL and the link text are both required. */ - function testRequiredLinkText() { + function testRequiredURLAndLinkText() { $field_name = Unicode::strtolower($this->randomMachineName()); // Create a field with settings to validate. FieldStorageConfig::create([ @@ -635,4 +635,44 @@ function testRequiredLinkText() { $this->assertEqual(count($result), 1, "Link text is marked as required when the link field is set to required."); } + /** + * Tests if the URL is not required, but link text is required. + */ + function testRequiredLinkTextOnly() { + $field_name = Unicode::strtolower($this->randomMachineName()); + // Create a field with settings to validate. + FieldStorageConfig::create([ + 'field_name' => $field_name, + 'entity_type' => 'entity_test', + 'type' => 'link', + ])->save(); + FieldConfig::create([ + 'field_name' => $field_name, + 'entity_type' => 'entity_test', + 'bundle' => 'entity_test', + 'label' => 'Read more about this entity', + 'required' => FALSE, + 'settings' => [ + 'title' => DRUPAL_REQUIRED, + 'link_type' => LinkItemInterface::LINK_GENERIC, + ], + ])->save(); + EntityFormDisplay::load('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(); + + // Display creation form. + $edit = ["{$field_name}[0][uri]" => 'http://www.example.com']; + $this->drupalPostForm('entity_test/add', $edit, t('Save')); + + $result = $this->xpath('//label[contains(@class, :class) and contains(text(), :text)]', [':class' => 'form-required', ':text' => 'Link text']); + $this->assertEqual(count($result), 1, "Link text is marked as required when the link field is set to required."); + } + }