You are here

function RenderElementTypesTest::testSystemCompactLink 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::testSystemCompactLink()

Tests system #type 'system_compact_link'.

File

core/modules/system/src/Tests/Common/RenderElementTypesTest.php, line 206
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 testSystemCompactLink() {
  $elements = array(
    array(
      'name' => "#type 'system_compact_link' when admin compact mode is off",
      'value' => array(
        '#type' => 'system_compact_link',
      ),
      'expected' => '//div[@class="compact-link"]/a[contains(@href, "admin/compact/on?") and text()="Hide descriptions"]',
    ),
    array(
      'name' => "#type 'system_compact_link' when adding extra attributes",
      'value' => array(
        '#type' => 'system_compact_link',
        '#attributes' => array(
          'class' => array(
            'kittens-rule',
          ),
        ),
      ),
      'expected' => '//div[@class="compact-link"]/a[contains(@href, "admin/compact/on?") and @class="kittens-rule" and text()="Hide descriptions"]',
    ),
  );
  foreach ($elements as $element) {
    $xml = new \SimpleXMLElement(\Drupal::service('renderer')
      ->renderRoot($element['value']));
    $result = $xml
      ->xpath($element['expected']);
    $this
      ->assertTrue($result, '"' . $element['name'] . '" is rendered correctly by drupal_render().');
  }

  // Set admin compact mode on for additional tests.
  \Drupal::request()->cookies
    ->set('Drupal_visitor_admin_compact_mode', TRUE);
  $element = array(
    'name' => "#type 'system_compact_link' when admin compact mode is on",
    'value' => array(
      '#type' => 'system_compact_link',
    ),
    'expected' => '//div[@class="compact-link"]/a[contains(@href, "admin/compact?") and text()="Show descriptions"]',
  );
  $xml = new \SimpleXMLElement(\Drupal::service('renderer')
    ->renderRoot($element['value']));
  $result = $xml
    ->xpath($element['expected']);
  $this
    ->assertTrue($result, '"' . $element['name'] . '" is rendered correctly by drupal_render().');
}