You are here

function FunctionsTest::testItemList in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Theme/FunctionsTest.php \Drupal\system\Tests\Theme\FunctionsTest::testItemList()

Tests item-list.html.twig.

File

core/modules/system/src/Tests/Theme/FunctionsTest.php, line 34
Contains \Drupal\system\Tests\Theme\FunctionsTest.

Class

FunctionsTest
Tests for common theme functions.

Namespace

Drupal\system\Tests\Theme

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 title set to 0 is output.
  $variables = array();
  $variables['title'] = 0;
  $variables['empty'] = 'No items found.';
  $expected = '<div class="item-list"><h3>0</h3>No items found.</div>';
  $this
    ->assertThemeOutput('item_list', $variables, $expected, '%callback with title set to 0 generates a title.');

  // Verify that title set to a render array is output.
  $variables = array();
  $variables['title'] = array(
    '#markup' => '<span>Render array</span>',
  );
  $variables['empty'] = 'No items found.';
  $expected = '<div class="item-list"><h3><span>Render array</span></h3>No items found.</div>';
  $this
    ->assertThemeOutput('item_list', $variables, $expected, '%callback with title set to a render array generates a title.');

  // Verify that empty text is not displayed when there are list items.
  $variables = array();
  $variables['title'] = 'Some title';
  $variables['empty'] = 'No items found.';
  $variables['items'] = array(
    'Un',
    'Deux',
    'Trois',
  );
  $expected = '<div class="item-list"><h3>Some title</h3><ul><li>Un</li><li>Deux</li><li>Trois</li></ul></div>';
  $this
    ->assertThemeOutput('item_list', $variables, $expected, '%callback does not print empty text when there are list items.');

  // Verify nested item lists.
  $variables = array();
  $variables['title'] = 'Some title';
  $variables['attributes'] = array(
    'id' => 'parentlist',
  );
  $variables['items'] = array(
    // A plain string value forms an own item.
    'a',
    // Items can be fully-fledged render arrays with their own attributes.
    array(
      '#wrapper_attributes' => array(
        'id' => 'item-id-b',
      ),
      '#markup' => 'b',
      'childlist' => array(
        '#theme' => 'item_list',
        '#attributes' => array(
          'id' => 'blist',
        ),
        '#list_type' => 'ol',
        '#items' => array(
          'ba',
          array(
            '#markup' => 'bb',
            '#wrapper_attributes' => array(
              'class' => array(
                'item-class-bb',
              ),
            ),
          ),
        ),
      ),
    ),
    // However, items can also be child #items.
    array(
      '#markup' => 'c',
      'childlist' => array(
        '#attributes' => array(
          'id' => 'clist',
        ),
        'ca',
        array(
          '#markup' => 'cb',
          '#wrapper_attributes' => array(
            'class' => array(
              'item-class-cb',
            ),
          ),
          'children' => array(
            'cba',
            'cbb',
          ),
        ),
        'cc',
      ),
    ),
    // Use #markup to be able to specify #wrapper_attributes.
    array(
      '#markup' => 'd',
      '#wrapper_attributes' => array(
        'id' => 'item-id-d',
      ),
    ),
    // An empty item with attributes.
    array(
      '#wrapper_attributes' => array(
        'id' => 'item-id-e',
      ),
    ),
    // Lastly, another plain string item.
    'f',
  );
  $inner_b = '<div class="item-list"><ol id="blist">';
  $inner_b .= '<li>ba</li>';
  $inner_b .= '<li class="item-class-bb">bb</li>';
  $inner_b .= '</ol></div>';
  $inner_cb = '<div class="item-list"><ul>';
  $inner_cb .= '<li>cba</li>';
  $inner_cb .= '<li>cbb</li>';
  $inner_cb .= '</ul></div>';
  $inner_c = '<div class="item-list"><ul id="clist">';
  $inner_c .= '<li>ca</li>';
  $inner_c .= '<li class="item-class-cb">cb' . $inner_cb . '</li>';
  $inner_c .= '<li>cc</li>';
  $inner_c .= '</ul></div>';
  $expected = '<div class="item-list">';
  $expected .= '<h3>Some title</h3>';
  $expected .= '<ul id="parentlist">';
  $expected .= '<li>a</li>';
  $expected .= '<li id="item-id-b">b' . $inner_b . '</li>';
  $expected .= '<li>c' . $inner_c . '</li>';
  $expected .= '<li id="item-id-d">d</li>';
  $expected .= '<li id="item-id-e"></li>';
  $expected .= '<li>f</li>';
  $expected .= '</ul></div>';
  $this
    ->assertThemeOutput('item_list', $variables, $expected);
}