1 theme.test ThemeFunctionsTestCase::testItemList()

Tests theme_item_list().

File

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

Class

ThemeFunctionsTestCase
Tests for common theme functions.

Code

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

  // Verify that empty items produce the empty string.
  $variables = array();
  $variables['empty'] = 'No items found.';
  $expected = '<div class="item-list">No items found.</div>';
  $this->assertThemeOutput('item_list', $variables, $expected, 'Empty %callback generates empty string.');
  // Verify that empty items produce the empty string with title.
  $variables = array();
  $variables['title'] = 'Some title';
  $variables['empty'] = 'No items found.';
  $expected = '<div class="item-list"><h3>Some title</h3>No items found.</div>';
  $this->assertThemeOutput('item_list', $variables, $expected, 'Empty %callback generates empty string with title.');

  // Verify that additional attributes can be added to the wrapper div.
  $attribute_name = $this->randomName();
  $attribute_value = $this->randomName();
  $another_css_class = $this->randomName();
  $variables = array();
  $variables['empty'] = 'No items found.';
  $variables['wrapper_attributes'] = array(
    $attribute_name => $attribute_value,
    'class' => array($another_css_class),
  );
  $expected = '<div ' . $attribute_name . '="' . $attribute_value . '" class="' . $another_css_class . ' item-list">No items found.</div>';
  $this->assertThemeOutput('item_list', $variables, $expected, 'Additional attributes added to the wrapper div.');

  // Verify nested item lists.
  $variables = array();
  $variables['title'] = 'Some title';
  $variables['attributes'] = array(
    'id' => 'parent-list',
  );
  $variables['items'] = array(
    'a',
    array(
      'data' => 'b',
      'children' => array(
        'c',
        // Nested children may use additional attributes.
        array(
          'data' => 'd',
          'class' => array('dee'),
        ),
        // Any string key is treated as child list attribute.
        'id' => 'child-list',
      ),
      // Any other keys are treated as item attributes.
      'id' => 'bee',
    ),
    array(
      'data' => 'e',
      'id' => 'E',
    ),
  );
  $inner = '<div class="item-list"><ul id="child-list">';
  $inner .= '<li class="odd first">c</li>';
  $inner .= '<li class="dee even last">d</li>';
  $inner .= '</ul></div>';

  $expected = '<div class="item-list">';
  $expected .= '<h3>Some title</h3>';
  $expected .= '<ul id="parent-list">';
  $expected .= '<li class="odd first">a</li>';
  $expected .= '<li id="bee" class="even">b' . $inner . '</li>';
  $expected .= '<li id="E" class="odd last">e</li>';
  $expected .= '</ul></div>';

  $this->assertThemeOutput('item_list', $variables, $expected);
}