public function FunctionsTest::testIndexedKeyedLinks 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::testIndexedKeyedLinks()
- 10 core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php \Drupal\Tests\system\Kernel\Theme\FunctionsTest::testIndexedKeyedLinks()
Tests links.html.twig using links with indexed keys.
File
- core/
modules/ system/ tests/ src/ Kernel/ Theme/ FunctionsTest.php, line 302
Class
- FunctionsTest
- Tests for common theme functions.
Namespace
Drupal\Tests\system\Kernel\ThemeCode
public function testIndexedKeyedLinks() {
// Turn off the query for the
// \Drupal\Core\Utility\LinkGeneratorInterface::generate() method to compare
// the active link correctly.
$original_query = \Drupal::request()->query
->all();
\Drupal::request()->query
->replace([]);
// Verify that empty variables produce no output.
$variables = [];
$expected = '';
$this
->assertThemeOutput('links', $variables, $expected, 'Empty %callback generates no output.');
$variables = [];
$variables['heading'] = 'Some title';
$expected = '';
$this
->assertThemeOutput('links', $variables, $expected, 'Empty %callback with heading generates no output.');
// Verify that a list of links is properly rendered.
$variables = [];
$variables['attributes'] = [
'id' => 'somelinks',
];
$variables['links'] = [
[
'title' => 'A <link>',
'url' => Url::fromUri('base:a/link'),
],
[
'title' => 'Plain "text"',
],
[
'title' => new FormattableMarkup('<span class="unescaped">@text</span>', [
'@text' => 'potentially unsafe text that <should> be escaped',
]),
],
[
'title' => 'Front page',
'url' => Url::fromRoute('<front>'),
],
[
'title' => 'Test route',
'url' => Url::fromRoute('router_test.1'),
],
[
'title' => 'Query test route',
'url' => Url::fromRoute('router_test.1'),
'query' => [
'key' => 'value',
],
],
];
$expected_links = '';
$expected_links .= '<ul id="somelinks">';
$expected_links .= '<li><a href="' . Url::fromUri('base:a/link')
->toString() . '">' . Html::escape('A <link>') . '</a></li>';
$expected_links .= '<li>' . Html::escape('Plain "text"') . '</li>';
$expected_links .= '<li><span class="unescaped">' . Html::escape('potentially unsafe text that <should> be escaped') . '</span></li>';
$expected_links .= '<li><a href="' . Url::fromRoute('<front>')
->toString() . '">' . Html::escape('Front page') . '</a></li>';
$expected_links .= '<li><a href="' . \Drupal::urlGenerator()
->generate('router_test.1') . '">' . Html::escape('Test route') . '</a></li>';
$query = [
'key' => 'value',
];
$expected_links .= '<li><a href="' . \Drupal::urlGenerator()
->generate('router_test.1', $query) . '">' . Html::escape('Query test route') . '</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);
// Restore the original request's query.
\Drupal::request()->query
->replace($original_query);
// Verify that passing an array as heading works (core support).
$variables['heading'] = [
'text' => 'Links heading',
'level' => 'h3',
'attributes' => [
'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'] = [
'text' => 'Links heading',
'level' => 'h3',
'attributes' => [
'id' => 'heading',
],
];
$expected_heading = '<h3 id="heading">Links heading</h3>';
$expected = $expected_heading . $expected_links;
$this
->assertThemeOutput('links', $variables, $expected);
// Verify that passing attributes for the links work.
$variables['links'][1]['attributes'] = [
'class' => [
'a/class',
],
];
$expected_links = '';
$expected_links .= '<ul id="somelinks">';
$expected_links .= '<li><a href="' . Url::fromUri('base:a/link')
->toString() . '">' . Html::escape('A <link>') . '</a></li>';
$expected_links .= '<li><span class="a/class">' . Html::escape('Plain "text"') . '</span></li>';
$expected_links .= '<li><span class="unescaped">' . Html::escape('potentially unsafe text that <should> be escaped') . '</span></li>';
$expected_links .= '<li><a href="' . Url::fromRoute('<front>')
->toString() . '">' . Html::escape('Front page') . '</a></li>';
$expected_links .= '<li><a href="' . \Drupal::urlGenerator()
->generate('router_test.1') . '">' . Html::escape('Test route') . '</a></li>';
$query = [
'key' => 'value',
];
$expected_links .= '<li><a href="' . \Drupal::urlGenerator()
->generate('router_test.1', $query) . '">' . Html::escape('Query test route') . '</a></li>';
$expected_links .= '</ul>';
$expected = $expected_heading . $expected_links;
$this
->assertThemeOutput('links', $variables, $expected);
// Verify the data- attributes for setting the "active" class on links.
\Drupal::currentUser()
->setAccount(new UserSession([
'uid' => 1,
]));
$variables['set_active_class'] = TRUE;
$expected_links = '';
$expected_links .= '<ul id="somelinks">';
$expected_links .= '<li><a href="' . Url::fromUri('base:a/link')
->toString() . '">' . Html::escape('A <link>') . '</a></li>';
$expected_links .= '<li><span class="a/class">' . Html::escape('Plain "text"') . '</span></li>';
$expected_links .= '<li><span class="unescaped">' . Html::escape('potentially unsafe text that <should> be escaped') . '</span></li>';
$expected_links .= '<li data-drupal-link-system-path="<front>"><a href="' . Url::fromRoute('<front>')
->toString() . '" data-drupal-link-system-path="<front>">' . Html::escape('Front page') . '</a></li>';
$expected_links .= '<li data-drupal-link-system-path="router_test/test1"><a href="' . \Drupal::urlGenerator()
->generate('router_test.1') . '" data-drupal-link-system-path="router_test/test1">' . Html::escape('Test route') . '</a></li>';
$query = [
'key' => 'value',
];
$encoded_query = Html::escape(Json::encode($query));
$expected_links .= '<li data-drupal-link-query="' . $encoded_query . '" data-drupal-link-system-path="router_test/test1"><a href="' . \Drupal::urlGenerator()
->generate('router_test.1', $query) . '" data-drupal-link-query="' . $encoded_query . '" data-drupal-link-system-path="router_test/test1">' . Html::escape('Query test route') . '</a></li>';
$expected_links .= '</ul>';
$expected = $expected_heading . $expected_links;
$this
->assertThemeOutput('links', $variables, $expected);
}