function LinkFieldTest::doTestLinkTitle

Same name and namespace in other branches
  1. 10 core/modules/link/tests/src/Functional/LinkFieldTest.php \Drupal\Tests\link\Functional\LinkFieldTest::doTestLinkTitle()
  2. 11.x core/modules/link/tests/src/Functional/LinkFieldTest.php \Drupal\Tests\link\Functional\LinkFieldTest::doTestLinkTitle()

Tests the link title settings of a link field.

1 call to LinkFieldTest::doTestLinkTitle()
LinkFieldTest::testLinkField in core/modules/link/tests/src/Functional/LinkFieldTest.php
Tests the functionality and rendering of the link field.

File

core/modules/link/tests/src/Functional/LinkFieldTest.php, line 272

Class

LinkFieldTest
Tests link field widgets and formatters.

Namespace

Drupal\Tests\link\Functional

Code

protected function doTestLinkTitle() {
    $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',
    ]);
    $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();
    
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    $display_repository->getFormDisplay('entity_test', 'entity_test')
        ->setComponent($field_name, [
        'type' => 'link_default',
        'settings' => [
            'placeholder_url' => 'http://example.com',
            'placeholder_title' => 'Enter the text for this link',
        ],
    ])
        ->save();
    $display_repository->getViewDisplay('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->assertSession()
            ->pageTextContains('Read more about this entity');
        $this->assertSession()
            ->fieldValueEquals("{$field_name}[0][uri]", '');
        $this->assertSession()
            ->responseContains('placeholder="http://example.com"');
        if ($title_setting === DRUPAL_DISABLED) {
            $this->assertSession()
                ->fieldNotExists("{$field_name}[0][title]");
            $this->assertSession()
                ->responseNotContains('placeholder="Enter the text for this link"');
        }
        else {
            $this->assertSession()
                ->responseContains('placeholder="Enter the text for this link"');
            $this->assertSession()
                ->fieldValueEquals("{$field_name}[0][title]", '');
            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->submitForm($edit, 'Save');
                $this->assertSession()
                    ->statusMessageContains('The URL field is required when the Link text field is specified.', 'error');
            }
            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->submitForm($edit, 'Save');
                $this->assertSession()
                    ->statusMessageContains('Link text field is required if there is URL input.', 'error');
                // Verify that the link text is not required, if the URL is empty.
                $edit = [
                    "{$field_name}[0][uri]" => '',
                ];
                $this->submitForm($edit, 'Save');
                $this->assertSession()
                    ->statusMessageNotContains('Link text field is required.');
                // 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->submitForm($edit, 'Save');
                $this->assertSession()
                    ->statusMessageNotContains('Link text field is required.');
            }
        }
    }
    // 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->submitForm($edit, 'Save');
    preg_match('|entity_test/manage/(\\d+)|', $this->getUrl(), $match);
    $id = $match[1];
    $this->assertSession()
        ->statusMessageContains('entity_test ' . $id . ' has been created.', 'status');
    $output = $this->renderTestEntity($id);
    $expected_link = (string) Link::fromTextAndUrl($value, Url::fromUri($value))->toString();
    $this->assertStringContainsString($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->drupalGet("entity_test/manage/{$id}/edit");
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->statusMessageContains('entity_test ' . $id . ' has been updated.', 'status');
    $output = $this->renderTestEntity($id);
    $expected_link = (string) Link::fromTextAndUrl($title, Url::fromUri($value))->toString();
    $this->assertStringContainsString($expected_link, $output);
}

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