function FunctionsTest::testItemList

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php \Drupal\Tests\system\Kernel\Theme\FunctionsTest::testItemList()
  2. 8.9.x core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php \Drupal\Tests\system\Kernel\Theme\FunctionsTest::testItemList()
  3. 11.x core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php \Drupal\Tests\system\Kernel\Theme\FunctionsTest::testItemList()

Tests item-list.html.twig.

File

core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php, line 41

Class

FunctionsTest
Tests for common theme functions.

Namespace

Drupal\Tests\system\Kernel\Theme

Code

public function testItemList() {
    // Verify that empty items produce no output.
    $variables = [];
    $expected = '';
    $this->assertThemeOutput('item_list', $variables, $expected, 'Empty %callback generates no output.');
    // Verify that empty items with title produce no output.
    $variables = [];
    $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 = [];
    $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 = [];
    $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 = [];
    $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 = [];
    $variables['title'] = [
        '#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 = [];
    $variables['title'] = 'Some title';
    $variables['empty'] = 'No items found.';
    $variables['items'] = [
        '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 = [];
    $variables['title'] = 'Some title';
    $variables['attributes'] = [
        'id' => 'parent-list',
    ];
    $variables['items'] = [
        // A plain string value forms an own item.
'a',
        // Items can be fully-fledged render arrays with their own attributes.
[
            '#wrapper_attributes' => [
                'id' => 'item-id-b',
            ],
            '#markup' => 'b',
            'child_list' => [
                '#theme' => 'item_list',
                '#attributes' => [
                    'id' => 'b_list',
                ],
                '#list_type' => 'ol',
                '#items' => [
                    'ba',
                    [
                        '#markup' => 'bb',
                        '#wrapper_attributes' => [
                            'class' => [
                                'item-class-bb',
                            ],
                        ],
                    ],
                ],
            ],
        ],
        // However, items can also be child #items.
[
            '#markup' => 'c',
            'child_list' => [
                '#attributes' => [
                    'id' => 'c-list',
                ],
                'ca',
                [
                    '#markup' => 'cb',
                    '#wrapper_attributes' => [
                        'class' => [
                            'item-class-cb',
                        ],
                    ],
                    'children' => [
                        'cba',
                        'cbb',
                    ],
                ],
                'cc',
            ],
        ],
        // Use #markup to be able to specify #wrapper_attributes.
[
            '#markup' => 'd',
            '#wrapper_attributes' => [
                'id' => 'item-id-d',
            ],
        ],
        // An empty item with attributes.
[
            '#wrapper_attributes' => [
                'id' => 'item-id-e',
            ],
        ],
        // Lastly, another plain string item.
'f',
    ];
    $inner_b = '<div class="item-list"><ol id="b_list">';
    $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="c-list">';
    $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="parent-list">';
    $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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.