function LinkFieldWidgetTest::testFieldDescription

Verifies that the widget elements have the expected descriptions.

File

core/modules/link/tests/src/Kernel/LinkFieldWidgetTest.php, line 99

Class

LinkFieldWidgetTest
Tests link field widgets.

Namespace

Drupal\Tests\link\Kernel

Code

public function testFieldDescription() : void {
  $formBuilder = $this->container
    ->get('entity.form_builder');
  $linkTypes = [
    LinkItemInterface::LINK_INTERNAL => 'This must be an internal path such as /node/add. You can also start typing the title of a piece of content to select it. Enter <front> to link to the front page. Enter <nolink> to display link text only. Enter <button> to display keyboard-accessible link text only.',
    LinkItemInterface::LINK_EXTERNAL => 'This must be an external URL such as https://example.com.',
    LinkItemInterface::LINK_GENERIC => 'Start typing the title of a piece of content to select it. You can also enter an internal path such as /node/add or an external URL such as https://example.com. Enter <front> to link to the front page. Enter <nolink> to display link text only. Enter <button> to display keyboard-accessible link text only.',
  ];
  foreach ($linkTypes as $linkType => $typeDescription) {
    // Create two fields: one with a title subfield and one without. There are
    // different description visibility rules for these cases.
    $fieldWithTitle = $this->entitySetUp($linkType, LinkTitleVisibility::Optional);
    $fieldWithoutTitle = $this->entitySetUp($linkType, LinkTitleVisibility::Disabled);
    $form = $formBuilder->getForm(EntityTest::create());
    // Instances with a title subfield should have the type description
    // describing the uri subfield. The field help text should describe the
    // fieldset.
    $this->assertEquals($typeDescription, strip_tags((string) $form[$fieldWithTitle]['widget'][0]['uri']['#description']));
    $this->assertEquals($this->fieldDescription, $form[$fieldWithTitle]['widget']['#description']);
    // Instances without a title subfield should have both descriptions
    // describing the uri subfield in an item_list
    $this->assertEquals('item_list', $form[$fieldWithoutTitle]['widget'][0]['uri']['#description']['#theme']);
    $expectedDescriptions = [
      $this->fieldDescription,
      $typeDescription,
    ];
    $this->assertEquals($expectedDescriptions, $form[$fieldWithoutTitle]['widget'][0]['uri']['#description']['#items']);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.