public function FunctionsTest::testItemList in Drupal 8
Same name and namespace in other branches
- 9 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\ThemeCode
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' => 'parentlist',
];
$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',
'childlist' => [
'#theme' => 'item_list',
'#attributes' => [
'id' => 'blist',
],
'#list_type' => 'ol',
'#items' => [
'ba',
[
'#markup' => 'bb',
'#wrapper_attributes' => [
'class' => [
'item-class-bb',
],
],
],
],
],
],
// However, items can also be child #items.
[
'#markup' => 'c',
'childlist' => [
'#attributes' => [
'id' => 'clist',
],
'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="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);
}