public function FunctionsTest::testLinks 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::testLinks()
- 10 core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php \Drupal\Tests\system\Kernel\Theme\FunctionsTest::testLinks()
Tests links.html.twig.
File
- core/
modules/ system/ tests/ src/ Kernel/ Theme/ FunctionsTest.php, line 181
Class
- FunctionsTest
- Tests for common theme functions.
Namespace
Drupal\Tests\system\Kernel\ThemeCode
public function testLinks() {
// 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'] = [
'a link' => [
'title' => 'A <link>',
'url' => Url::fromUri('base:a/link'),
],
'plain text' => [
'title' => 'Plain "text"',
],
'html text' => [
'title' => new FormattableMarkup('<span class="unescaped">@text</span>', [
'@text' => 'potentially unsafe text that <should> be escaped',
]),
],
'front page' => [
'title' => 'Front page',
'url' => Url::fromRoute('<front>'),
],
'router-test' => [
'title' => 'Test route',
'url' => Url::fromRoute('router_test.1'),
],
'query-test' => [
'title' => 'Query test route',
'url' => Url::fromRoute('router_test.1'),
'query' => [
'key' => 'value',
],
],
];
$expected_links = '';
$expected_links .= '<ul id="somelinks">';
$expected_links .= '<li class="a-link"><a href="' . Url::fromUri('base:a/link')
->toString() . '">' . Html::escape('A <link>') . '</a></li>';
$expected_links .= '<li class="plain-text">' . Html::escape('Plain "text"') . '</li>';
$expected_links .= '<li class="html-text"><span class="unescaped">' . Html::escape('potentially unsafe text that <should> be escaped') . '</span></li>';
$expected_links .= '<li class="front-page"><a href="' . Url::fromRoute('<front>')
->toString() . '">' . Html::escape('Front page') . '</a></li>';
$expected_links .= '<li class="router-test"><a href="' . \Drupal::urlGenerator()
->generate('router_test.1') . '">' . Html::escape('Test route') . '</a></li>';
$query = [
'key' => 'value',
];
$expected_links .= '<li class="query-test"><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']['plain text']['attributes'] = [
'class' => [
'a/class',
],
];
$expected_links = '';
$expected_links .= '<ul id="somelinks">';
$expected_links .= '<li class="a-link"><a href="' . Url::fromUri('base:a/link')
->toString() . '">' . Html::escape('A <link>') . '</a></li>';
$expected_links .= '<li class="plain-text"><span class="a/class">' . Html::escape('Plain "text"') . '</span></li>';
$expected_links .= '<li class="html-text"><span class="unescaped">' . Html::escape('potentially unsafe text that <should> be escaped') . '</span></li>';
$expected_links .= '<li class="front-page"><a href="' . Url::fromRoute('<front>')
->toString() . '">' . Html::escape('Front page') . '</a></li>';
$expected_links .= '<li class="router-test"><a href="' . \Drupal::urlGenerator()
->generate('router_test.1') . '">' . Html::escape('Test route') . '</a></li>';
$query = [
'key' => 'value',
];
$expected_links .= '<li class="query-test"><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 class="a-link"><a href="' . Url::fromUri('base:a/link')
->toString() . '">' . Html::escape('A <link>') . '</a></li>';
$expected_links .= '<li class="plain-text"><span class="a/class">' . Html::escape('Plain "text"') . '</span></li>';
$expected_links .= '<li class="html-text"><span class="unescaped">' . Html::escape('potentially unsafe text that <should> be escaped') . '</span></li>';
$expected_links .= '<li data-drupal-link-system-path="<front>" class="front-page"><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" class="router-test"><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" class="query-test"><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);
}