function UrlTest::testLinkAttributes

Same name in other branches
  1. 9 core/modules/system/tests/src/Kernel/Common/UrlTest.php \Drupal\Tests\system\Kernel\Common\UrlTest::testLinkAttributes()
  2. 8.9.x core/modules/system/tests/src/Functional/Common/UrlTest.php \Drupal\Tests\system\Functional\Common\UrlTest::testLinkAttributes()
  3. 11.x core/modules/system/tests/src/Kernel/Common/UrlTest.php \Drupal\Tests\system\Kernel\Common\UrlTest::testLinkAttributes()

Tests that default and custom attributes are handled correctly on links.

File

core/modules/system/tests/src/Kernel/Common/UrlTest.php, line 82

Class

UrlTest
Tests the Url object.

Namespace

Drupal\Tests\system\Kernel\Common

Code

public function testLinkAttributes() : void {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container
        ->get('renderer');
    // Test that hreflang is added when a link has a known language.
    $language = new Language([
        'id' => 'fr',
        'name' => 'French',
    ]);
    $hreflang_link = [
        '#type' => 'link',
        '#options' => [
            'language' => $language,
        ],
        '#url' => Url::fromUri('https://www.drupal.org'),
        '#title' => 'bar',
    ];
    $langcode = $language->getId();
    // Test that the default hreflang handling for links does not override a
    // hreflang attribute explicitly set in the render array.
    $hreflang_override_link = $hreflang_link;
    $hreflang_override_link['#options']['attributes']['hreflang'] = 'foo';
    $rendered = (string) $renderer->renderRoot($hreflang_link);
    $this->assertTrue($this->hasAttribute('hreflang', $rendered, $langcode), "hreflang attribute with value {$langcode} is present on a rendered link when langcode is provided in the render array.");
    $rendered = (string) $renderer->renderRoot($hreflang_override_link);
    $this->assertTrue($this->hasAttribute('hreflang', $rendered, 'foo'), 'hreflang attribute with value foo is present on a rendered link when @hreflang is provided in the render array.');
    // Test adding a custom class in links produced by
    // \Drupal\Core\Utility\LinkGeneratorInterface::generate() and #type 'link'.
    // Test the link generator.
    $class_l = $this->randomMachineName();
    $link_l = (string) Link::fromTextAndUrl($this->randomMachineName(), Url::fromRoute('common_test.destination', [], [
        'attributes' => [
            'class' => [
                $class_l,
            ],
        ],
    ]))
        ->toString();
    $this->assertTrue($this->hasAttribute('class', $link_l, $class_l), "Custom class {$class_l} is present on link when requested by Link::toString()");
    // Test #type.
    $class_theme = $this->randomMachineName();
    $type_link = [
        '#type' => 'link',
        '#title' => $this->randomMachineName(),
        '#url' => Url::fromRoute('common_test.destination'),
        '#options' => [
            'attributes' => [
                'class' => [
                    $class_theme,
                ],
            ],
        ],
    ];
    $link_theme = (string) $renderer->renderRoot($type_link);
    $this->assertTrue($this->hasAttribute('class', $link_theme, $class_theme), "Custom class {$class_theme} is present on link when requested by #type");
}

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