function FunctionsTest::testLinks

Tests theme_links().

File

drupal/core/modules/system/lib/Drupal/system/Tests/Theme/FunctionsTest.php, line 130
Definition of Drupal\system\Tests\Theme\FunctionsTest.

Class

FunctionsTest
Tests for common theme functions.

Namespace

Drupal\system\Tests\Theme

Code

function testLinks() {

  // Verify that empty variables produce no output.
  $variables = array();
  $expected = '';
  $this
    ->assertThemeOutput('links', $variables, $expected, 'Empty %callback generates no output.');
  $variables = array();
  $variables['heading'] = 'Some title';
  $expected = '';
  $this
    ->assertThemeOutput('links', $variables, $expected, 'Empty %callback with heading generates no output.');

  // Set the current path to the front page path.
  // Required to verify the "active" class in expected links below, and
  // because the current path is different when running tests manually via
  // simpletest.module ('batch') and via the testing framework ('').
  _current_path(config('system.site')
    ->get('page.front'));

  // Verify that a list of links is properly rendered.
  $variables = array();
  $variables['attributes'] = array(
    'id' => 'somelinks',
  );
  $variables['links'] = array(
    'a link' => array(
      'title' => 'A <link>',
      'href' => 'a/link',
    ),
    'plain text' => array(
      'title' => 'Plain "text"',
    ),
    'front page' => array(
      'title' => 'Front page',
      'href' => '<front>',
    ),
  );
  $expected_links = '';
  $expected_links .= '<ul id="somelinks">';
  $expected_links .= '<li class="a-link odd first"><a href="' . url('a/link') . '">' . check_plain('A <link>') . '</a></li>';
  $expected_links .= '<li class="plain-text even">' . check_plain('Plain "text"') . '</li>';
  $expected_links .= '<li class="front-page odd last active"><a href="' . url('<front>') . '" class="active">' . check_plain('Front page') . '</a></li>';
  $expected_links .= '</ul>';

  // Verify that passing a string as heading works.
  $variables['heading'] = 'Links heading';
  $expected_heading = '<h2>Links heading</h2>';
  $expected = $expected_heading . $expected_links;
  $this
    ->assertThemeOutput('links', $variables, $expected);

  // Verify that passing an array as heading works (core support).
  $variables['heading'] = array(
    'text' => 'Links heading',
    'level' => 'h3',
    'class' => 'heading',
  );
  $expected_heading = '<h3 class="heading">Links heading</h3>';
  $expected = $expected_heading . $expected_links;
  $this
    ->assertThemeOutput('links', $variables, $expected);

  // Verify that passing attributes for the heading works.
  $variables['heading'] = array(
    'text' => 'Links heading',
    'level' => 'h3',
    'attributes' => array(
      'id' => 'heading',
    ),
  );
  $expected_heading = '<h3 id="heading">Links heading</h3>';
  $expected = $expected_heading . $expected_links;
  $this
    ->assertThemeOutput('links', $variables, $expected);

  // Verify that passing attributes for the links work.
  $variables['links']['a link']['attributes'] = array(
    'class' => array(
      'a/class',
    ),
  );
  $variables['links']['plain text']['attributes'] = array(
    'class' => array(
      'a/class',
    ),
  );
  $expected_links = '';
  $expected_links .= '<ul id="somelinks">';
  $expected_links .= '<li class="a-link odd first"><a href="' . url('a/link') . '" class="a/class">' . check_plain('A <link>') . '</a></li>';
  $expected_links .= '<li class="plain-text even"><span class="a/class">' . check_plain('Plain "text"') . '</span></li>';
  $expected_links .= '<li class="front-page odd last active"><a href="' . url('<front>') . '" class="active">' . check_plain('Front page') . '</a></li>';
  $expected_links .= '</ul>';
  $expected = $expected_heading . $expected_links;
  $this
    ->assertThemeOutput('links', $variables, $expected);
}