function RenderElementTypesTest::testLink

Tests system #type 'link'.

File

core/tests/Drupal/KernelTests/Core/Render/Element/RenderElementTypesTest.php, line 243

Class

RenderElementTypesTest
Tests the rendered markup of core render element types.

Namespace

Drupal\KernelTests\Core\Render\Element

Code

public function testLink() : void {
  $elements = [
    [
      'name' => "#type 'link' simple anchor tag",
      'value' => [
        '#type' => 'link',
        '#title' => 'title',
        '#url' => Url::fromUri('https://www.drupal.org'),
      ],
      'expected' => '//a[@href="https://www.drupal.org" and text()="title"]',
    ],
    [
      'name' => "#type 'link' anchor tag with extra classes in ['#attributes']",
      'value' => [
        '#type' => 'link',
        '#title' => 'title',
        '#url' => Url::fromUri('https://www.drupal.org'),
        '#attributes' => [
          'class' => [
            'attributes-class',
          ],
        ],
        '#options' => [],
      ],
      'expected' => '//a[@href="https://www.drupal.org" and @class="attributes-class" and text()="title"]',
    ],
    [
      'name' => "#type 'link' anchor tag with extra classes in ['#options']['attributes']",
      'value' => [
        '#type' => 'link',
        '#title' => 'title',
        '#url' => Url::fromUri('https://www.drupal.org'),
        '#attributes' => [],
        '#options' => [
          'attributes' => [
            'class' => [
              'options-attributes-class',
            ],
          ],
        ],
      ],
      'expected' => '//a[@href="https://www.drupal.org" and @class="options-attributes-class" and text()="title"]',
    ],
    [
      'name' => "#type 'link' anchor tag with extra classes in both ['#attributes'] and ['#options']['attributes']",
      'value' => [
        '#type' => 'link',
        '#title' => 'title',
        '#url' => Url::fromUri('https://www.drupal.org'),
        '#attributes' => [
          'class' => [
            'attributes-class',
          ],
        ],
        '#options' => [
          'attributes' => [
            'class' => [
              'options-attributes-class',
            ],
          ],
        ],
      ],
      'expected' => '//a[@href="https://www.drupal.org" and @class="options-attributes-class attributes-class" and text()="title"]',
    ],
    [
      'name' => "#type 'link' anchor tag with extra classes in both ['#attributes'] and ['#options']['attributes'] as strings",
      'value' => [
        '#type' => 'link',
        '#title' => 'title',
        '#url' => Url::fromUri('https://www.drupal.org'),
        '#attributes' => [
          'class' => 'attributes-class',
        ],
        '#options' => [
          'attributes' => [
            'class' => 'options-attributes-class',
          ],
        ],
      ],
      'expected' => '//a[@href="https://www.drupal.org" and contains(@class,"options-attributes-class") and contains(@class,"attributes-class") and text()="title"]',
    ],
    [
      'name' => "#type 'link' anchor tag with extra classes in Url object ['#options']['attributes'] which are ignored",
      'value' => [
        '#type' => 'link',
        '#title' => 'title',
        '#url' => Url::fromUri('https://www.drupal.org')->setOption('attributes', [
          'class' => 'url-options-attributes-class',
        ]),
        '#attributes' => [],
        '#options' => [],
      ],
      'expected' => '//a[@href="https://www.drupal.org" and not(@class) and text()="title"]',
    ],
  ];
  foreach ($elements as $element) {
    $xml = new \SimpleXMLElement((string) \Drupal::service('renderer')->renderRoot($element['value']));
    $result = $xml->xpath($element['expected']);
    $this->assertNotEmpty($result, '"' . $element['name'] . '" input rendered correctly.');
  }
}

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