1 theme.test ThemeFunctionsTestCase::testLinks()

Tests theme_links().

File

core/modules/simpletest/tests/theme.test, line 340
Tests for the theme API.

Class

ThemeFunctionsTestCase
Tests for common theme functions.

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 home 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 ('').
  $_GET['q'] = config_get('system.core', 'site_frontpage');

  // Verify that a list of links is properly rendered.
  $variables = array();
  $variables['attributes'] = array('id' => 'some-links');
  $variables['links'] = array(
    'a link' => array(
      'title' => 'A <link>',
      'href' => 'a/link',
    ),
    'plain text' => array(
      'title' => 'Plain "text"',
    ),
    'front page' => array(
      'title' => 'Home page',
      'href' => '<front>',
    ),
  );

  $expected_links = '';
  $expected_links .= '<ul id="some-links">';
  $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"><span>' . check_plain('Plain "text"') . '</span></li>';
  $expected_links .= '<li class="front-page odd last active"><a href="' . url('<front>') . '" class="active" aria-current="page">' . check_plain('Home 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);
}