You are here

function RenderElementTypesTest::testMoreLink in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Common/RenderElementTypesTest.php \Drupal\system\Tests\Common\RenderElementTypesTest::testMoreLink()

Tests system #type 'more_link'.

File

core/modules/system/src/Tests/Common/RenderElementTypesTest.php, line 126
Contains \Drupal\system\Tests\Common\RenderElementTypesTest.

Class

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

Namespace

Drupal\system\Tests\Common

Code

function testMoreLink() {
  $elements = array(
    array(
      'name' => "#type 'more_link' anchor tag generation without extra classes",
      'value' => array(
        '#type' => 'more_link',
        '#url' => Url::fromUri('https://www.drupal.org'),
      ),
      'expected' => '//div[@class="more-link"]/a[@href="https://www.drupal.org" and text()="More"]',
    ),
    array(
      'name' => "#type 'more_link' anchor tag generation with different link text",
      'value' => array(
        '#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"]',
    ),
    array(
      'name' => "#type 'more_link' anchor tag generation with attributes on wrapper",
      'value' => array(
        '#type' => 'more_link',
        '#url' => Url::fromUri('https://www.drupal.org'),
        '#theme_wrappers' => array(
          'container' => array(
            '#attributes' => array(
              'title' => 'description',
              'class' => array(
                '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"]',
    ),
    array(
      'name' => "#type 'more_link' anchor tag with a relative path",
      'value' => array(
        '#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"]',
    ),
    array(
      'name' => "#type 'more_link' anchor tag with a route",
      'value' => array(
        '#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"]',
    ),
    array(
      'name' => "#type 'more_link' anchor tag with an absolute path",
      'value' => array(
        '#type' => 'more_link',
        '#url' => Url::fromRoute('system.admin_content'),
        '#options' => array(
          'absolute' => TRUE,
        ),
      ),
      'expected' => '//div[@class="more-link"]/a[@href="' . Url::fromRoute('system.admin_content')
        ->setAbsolute()
        ->toString() . '" and text()="More"]',
    ),
    array(
      'name' => "#type 'more_link' anchor tag to the front page",
      'value' => array(
        '#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
      ->assertTrue($result, '"' . $element['name'] . '" input rendered correctly by drupal_render().');
  }
}