You are here

public function RenderElementTypesTest::testMoreLink in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Render/Element/RenderElementTypesTest.php \Drupal\KernelTests\Core\Render\Element\RenderElementTypesTest::testMoreLink()

Tests system #type 'more_link'.

File

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

Class

RenderElementTypesTest
Tests the markup of core render element types passed to drupal_render().

Namespace

Drupal\KernelTests\Core\Render\Element

Code

public function testMoreLink() {
  $elements = [
    [
      'name' => "#type 'more_link' anchor tag generation without extra classes",
      'value' => [
        '#type' => 'more_link',
        '#url' => Url::fromUri('https://www.drupal.org'),
      ],
      'expected' => '//div[@class="more-link"]/a[@href="https://www.drupal.org" and text()="More"]',
    ],
    [
      'name' => "#type 'more_link' anchor tag generation with different link text",
      'value' => [
        '#type' => 'more_link',
        '#url' => Url::fromUri('https://www.drupal.org'),
        '#title' => 'More Titles',
      ],
      'expected' => '//div[@class="more-link"]/a[@href="https://www.drupal.org" and text()="More Titles"]',
    ],
    [
      'name' => "#type 'more_link' anchor tag generation with attributes on wrapper",
      'value' => [
        '#type' => 'more_link',
        '#url' => Url::fromUri('https://www.drupal.org'),
        '#theme_wrappers' => [
          'container' => [
            '#attributes' => [
              'title' => 'description',
              'class' => [
                'more-link',
                'drupal',
                'test',
              ],
            ],
          ],
        ],
      ],
      'expected' => '//div[@title="description" and contains(@class, "more-link") and contains(@class, "drupal") and contains(@class, "test")]/a[@href="https://www.drupal.org" and text()="More"]',
    ],
    [
      'name' => "#type 'more_link' anchor tag with a relative path",
      'value' => [
        '#type' => 'more_link',
        '#url' => Url::fromRoute('router_test.1'),
      ],
      'expected' => '//div[@class="more-link"]/a[@href="' . Url::fromRoute('router_test.1')
        ->toString() . '" and text()="More"]',
    ],
    [
      'name' => "#type 'more_link' anchor tag with a route",
      'value' => [
        '#type' => 'more_link',
        '#url' => Url::fromRoute('router_test.1'),
      ],
      'expected' => '//div[@class="more-link"]/a[@href="' . \Drupal::urlGenerator()
        ->generate('router_test.1') . '" and text()="More"]',
    ],
    [
      'name' => "#type 'more_link' anchor tag with an absolute path",
      'value' => [
        '#type' => 'more_link',
        '#url' => Url::fromRoute('system.admin_content'),
        '#options' => [
          'absolute' => TRUE,
        ],
      ],
      'expected' => '//div[@class="more-link"]/a[@href="' . Url::fromRoute('system.admin_content')
        ->setAbsolute()
        ->toString() . '" and text()="More"]',
    ],
    [
      'name' => "#type 'more_link' anchor tag to the front page",
      'value' => [
        '#type' => 'more_link',
        '#url' => Url::fromRoute('<front>'),
      ],
      'expected' => '//div[@class="more-link"]/a[@href="' . Url::fromRoute('<front>')
        ->toString() . '" and text()="More"]',
    ],
  ];
  foreach ($elements as $element) {
    $xml = new \SimpleXMLElement(\Drupal::service('renderer')
      ->renderRoot($element['value']));
    $result = $xml
      ->xpath($element['expected']);
    $this
      ->assertNotEmpty($result, '"' . $element['name'] . '" input rendered correctly by drupal_render().');
  }
}