function FunctionsTest::testDrupalPreRenderLinks in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/src/Tests/Theme/FunctionsTest.php \Drupal\system\Tests\Theme\FunctionsTest::testDrupalPreRenderLinks()
Test the use of drupal_pre_render_links() on a nested array of links.
File
- core/
modules/ system/ src/ Tests/ Theme/ FunctionsTest.php, line 295 - Contains \Drupal\system\Tests\Theme\FunctionsTest.
Class
- FunctionsTest
- Tests for common theme functions.
Namespace
Drupal\system\Tests\ThemeCode
function testDrupalPreRenderLinks() {
// Define the base array to be rendered, containing a variety of different
// kinds of links.
$base_array = array(
'#theme' => 'links',
'#pre_render' => array(
'drupal_pre_render_links',
),
'#links' => array(
'parent_link' => array(
'title' => 'Parent link original',
'url' => Url::fromRoute('router_test.1'),
),
),
'first_child' => array(
'#theme' => 'links',
'#links' => array(
// This should be rendered if 'first_child' is rendered separately,
// but ignored if the parent is being rendered (since it duplicates
// one of the parent's links).
'parent_link' => array(
'title' => 'Parent link copy',
'url' => Url::fromRoute('router_test.6'),
),
// This should always be rendered.
'first_child_link' => array(
'title' => 'First child link',
'url' => Url::fromRoute('router_test.7'),
),
),
),
// This should always be rendered as part of the parent.
'second_child' => array(
'#theme' => 'links',
'#links' => array(
'second_child_link' => array(
'title' => 'Second child link',
'url' => Url::fromRoute('router_test.8'),
),
),
),
// This should never be rendered, since the user does not have access to
// it.
'third_child' => array(
'#theme' => 'links',
'#links' => array(
'third_child_link' => array(
'title' => 'Third child link',
'url' => Url::fromRoute('router_test.9'),
),
),
'#access' => FALSE,
),
);
// Start with a fresh copy of the base array, and try rendering the entire
// thing. We expect a single <ul> with appropriate links contained within
// it.
$render_array = $base_array;
$html = \Drupal::service('renderer')
->renderRoot($render_array);
$dom = new \DOMDocument();
$dom
->loadHTML($html);
$this
->assertEqual($dom
->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered HTML.');
$list_elements = $dom
->getElementsByTagName('li');
$this
->assertEqual($list_elements->length, 3, 'Three "li" tags found in the rendered HTML.');
$this
->assertEqual($list_elements
->item(0)->nodeValue, 'Parent link original', 'First expected link found.');
$this
->assertEqual($list_elements
->item(1)->nodeValue, 'First child link', 'Second expected link found.');
$this
->assertEqual($list_elements
->item(2)->nodeValue, 'Second child link', 'Third expected link found.');
$this
->assertIdentical(strpos($html, 'Parent link copy'), FALSE, '"Parent link copy" link not found.');
$this
->assertIdentical(strpos($html, 'Third child link'), FALSE, '"Third child link" link not found.');
// Now render 'first_child', followed by the rest of the links, and make
// sure we get two separate <ul>'s with the appropriate links contained
// within each.
$render_array = $base_array;
$child_html = \Drupal::service('renderer')
->renderRoot($render_array['first_child']);
$parent_html = \Drupal::service('renderer')
->renderRoot($render_array);
// First check the child HTML.
$dom = new \DOMDocument();
$dom
->loadHTML($child_html);
$this
->assertEqual($dom
->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered child HTML.');
$list_elements = $dom
->getElementsByTagName('li');
$this
->assertEqual($list_elements->length, 2, 'Two "li" tags found in the rendered child HTML.');
$this
->assertEqual($list_elements
->item(0)->nodeValue, 'Parent link copy', 'First expected link found.');
$this
->assertEqual($list_elements
->item(1)->nodeValue, 'First child link', 'Second expected link found.');
// Then check the parent HTML.
$dom = new \DOMDocument();
$dom
->loadHTML($parent_html);
$this
->assertEqual($dom
->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered parent HTML.');
$list_elements = $dom
->getElementsByTagName('li');
$this
->assertEqual($list_elements->length, 2, 'Two "li" tags found in the rendered parent HTML.');
$this
->assertEqual($list_elements
->item(0)->nodeValue, 'Parent link original', 'First expected link found.');
$this
->assertEqual($list_elements
->item(1)->nodeValue, 'Second child link', 'Second expected link found.');
$this
->assertIdentical(strpos($parent_html, 'First child link'), FALSE, '"First child link" link not found.');
$this
->assertIdentical(strpos($parent_html, 'Third child link'), FALSE, '"Third child link" link not found.');
}