function LinkFieldTest::testNoLinkUri

Same name in other branches
  1. 9 core/modules/link/tests/src/Functional/LinkFieldTest.php \Drupal\Tests\link\Functional\LinkFieldTest::testNoLinkUri()
  2. 8.9.x core/modules/link/tests/src/Functional/LinkFieldTest.php \Drupal\Tests\link\Functional\LinkFieldTest::testNoLinkUri()
  3. 11.x core/modules/link/tests/src/Functional/LinkFieldTest.php \Drupal\Tests\link\Functional\LinkFieldTest::testNoLinkUri()

Tests <nolink> and <none> as link uri.

File

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

Class

LinkFieldTest
Tests link field widgets and formatters.

Namespace

Drupal\Tests\link\Functional

Code

public function testNoLinkUri() : void {
    $field_name = $this->randomMachineName();
    $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' => LinkItemInterface::LINK_INTERNAL,
        ],
    ])
        ->save();
    $this->container
        ->get('entity_type.manager')
        ->getStorage('entity_form_display')
        ->load('entity_test.entity_test.default')
        ->setComponent($field_name, [
        'type' => 'link_default',
    ])
        ->save();
    EntityViewDisplay::create([
        'targetEntityType' => 'entity_test',
        'bundle' => 'entity_test',
        'mode' => 'full',
        'status' => TRUE,
    ])->setComponent($field_name, [
        'type' => 'link',
    ])
        ->save();
    // Test a link with <nolink> uri.
    $edit = [
        "{$field_name}[0][title]" => 'Title, no link',
        "{$field_name}[0][uri]" => '<nolink>',
    ];
    $this->drupalGet('/entity_test/add');
    $this->submitForm($edit, 'Save');
    preg_match('|entity_test/manage/(\\d+)|', $this->getUrl(), $match);
    $id = $match[1];
    $output = $this->renderTestEntity($id);
    $expected_link = (string) $this->container
        ->get('link_generator')
        ->generate('Title, no link', Url::fromUri('route:<nolink>'));
    $this->assertStringContainsString($expected_link, $output);
    // Test a link with <none> uri.
    $edit = [
        "{$field_name}[0][title]" => 'Title, none',
        "{$field_name}[0][uri]" => '<none>',
    ];
    $this->drupalGet('/entity_test/add');
    $this->submitForm($edit, 'Save');
    preg_match('|entity_test/manage/(\\d+)|', $this->getUrl(), $match);
    $id = $match[1];
    $output = $this->renderTestEntity($id);
    $expected_link = (string) $this->container
        ->get('link_generator')
        ->generate('Title, none', Url::fromUri('route:<none>'));
    $this->assertStringContainsString($expected_link, $output);
    // Test a link with a <button> uri.
    $edit = [
        "{$field_name}[0][title]" => 'Title, button',
        "{$field_name}[0][uri]" => '<button>',
    ];
    $this->drupalGet('/entity_test/add');
    $this->submitForm($edit, 'Save');
    preg_match('|entity_test/manage/(\\d+)|', $this->getUrl(), $match);
    $id = $match[1];
    $output = $this->renderTestEntity($id);
    $expected_link = (string) $this->container
        ->get('link_generator')
        ->generate('Title, button', Url::fromUri('route:<button>'));
    $this->assertStringContainsString($expected_link, $output);
}

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