Chris@0: assertThemeOutput('item_list', $variables, $expected, 'Empty %callback generates no output.'); Chris@0: Chris@0: // Verify that empty items with title produce no output. Chris@0: $variables = []; Chris@0: $variables['title'] = 'Some title'; Chris@0: $expected = ''; Chris@0: $this->assertThemeOutput('item_list', $variables, $expected, 'Empty %callback with title generates no output.'); Chris@0: Chris@0: // Verify that empty items produce the empty string. Chris@0: $variables = []; Chris@0: $variables['empty'] = 'No items found.'; Chris@0: $expected = '
No items found.
'; Chris@0: $this->assertThemeOutput('item_list', $variables, $expected, 'Empty %callback generates empty string.'); Chris@0: Chris@0: // Verify that empty items produce the empty string with title. Chris@0: $variables = []; Chris@0: $variables['title'] = 'Some title'; Chris@0: $variables['empty'] = 'No items found.'; Chris@0: $expected = '

Some title

No items found.
'; Chris@0: $this->assertThemeOutput('item_list', $variables, $expected, 'Empty %callback generates empty string with title.'); Chris@0: Chris@0: // Verify that title set to 0 is output. Chris@0: $variables = []; Chris@0: $variables['title'] = 0; Chris@0: $variables['empty'] = 'No items found.'; Chris@0: $expected = '

0

No items found.
'; Chris@0: $this->assertThemeOutput('item_list', $variables, $expected, '%callback with title set to 0 generates a title.'); Chris@0: Chris@0: // Verify that title set to a render array is output. Chris@0: $variables = []; Chris@0: $variables['title'] = [ Chris@0: '#markup' => 'Render array', Chris@0: ]; Chris@0: $variables['empty'] = 'No items found.'; Chris@0: $expected = '

Render array

No items found.
'; Chris@0: $this->assertThemeOutput('item_list', $variables, $expected, '%callback with title set to a render array generates a title.'); Chris@0: Chris@0: // Verify that empty text is not displayed when there are list items. Chris@0: $variables = []; Chris@0: $variables['title'] = 'Some title'; Chris@0: $variables['empty'] = 'No items found.'; Chris@0: $variables['items'] = ['Un', 'Deux', 'Trois']; Chris@0: $expected = '

Some title

'; Chris@0: $this->assertThemeOutput('item_list', $variables, $expected, '%callback does not print empty text when there are list items.'); Chris@0: Chris@0: // Verify nested item lists. Chris@0: $variables = []; Chris@0: $variables['title'] = 'Some title'; Chris@0: $variables['attributes'] = [ Chris@0: 'id' => 'parentlist', Chris@0: ]; Chris@0: $variables['items'] = [ Chris@0: // A plain string value forms an own item. Chris@0: 'a', Chris@0: // Items can be fully-fledged render arrays with their own attributes. Chris@0: [ Chris@0: '#wrapper_attributes' => [ Chris@0: 'id' => 'item-id-b', Chris@0: ], Chris@0: '#markup' => 'b', Chris@0: 'childlist' => [ Chris@0: '#theme' => 'item_list', Chris@0: '#attributes' => ['id' => 'blist'], Chris@0: '#list_type' => 'ol', Chris@0: '#items' => [ Chris@0: 'ba', Chris@0: [ Chris@0: '#markup' => 'bb', Chris@0: '#wrapper_attributes' => ['class' => ['item-class-bb']], Chris@0: ], Chris@0: ], Chris@0: ], Chris@0: ], Chris@0: // However, items can also be child #items. Chris@0: [ Chris@0: '#markup' => 'c', Chris@0: 'childlist' => [ Chris@0: '#attributes' => ['id' => 'clist'], Chris@0: 'ca', Chris@0: [ Chris@0: '#markup' => 'cb', Chris@0: '#wrapper_attributes' => ['class' => ['item-class-cb']], Chris@0: 'children' => [ Chris@0: 'cba', Chris@0: 'cbb', Chris@0: ], Chris@0: ], Chris@0: 'cc', Chris@0: ], Chris@0: ], Chris@0: // Use #markup to be able to specify #wrapper_attributes. Chris@0: [ Chris@0: '#markup' => 'd', Chris@0: '#wrapper_attributes' => ['id' => 'item-id-d'], Chris@0: ], Chris@0: // An empty item with attributes. Chris@0: [ Chris@0: '#wrapper_attributes' => ['id' => 'item-id-e'], Chris@0: ], Chris@0: // Lastly, another plain string item. Chris@0: 'f', Chris@0: ]; Chris@0: Chris@0: $inner_b = '
    '; Chris@0: $inner_b .= '
  1. ba
  2. '; Chris@0: $inner_b .= '
  3. bb
  4. '; Chris@0: $inner_b .= '
'; Chris@0: Chris@0: $inner_cb = '
'; Chris@0: Chris@0: $inner_c = '
'; Chris@0: Chris@0: $expected = '
'; Chris@0: $expected .= '

Some title

'; Chris@0: $expected .= '
'; Chris@0: Chris@0: $this->assertThemeOutput('item_list', $variables, $expected); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests links.html.twig. Chris@0: */ Chris@0: public function testLinks() { Chris@0: // Turn off the query for the Chris@0: // \Drupal\Core\Utility\LinkGeneratorInterface::generate() method to compare Chris@0: // the active link correctly. Chris@0: $original_query = \Drupal::request()->query->all(); Chris@0: \Drupal::request()->query->replace([]); Chris@0: // Verify that empty variables produce no output. Chris@0: $variables = []; Chris@0: $expected = ''; Chris@0: $this->assertThemeOutput('links', $variables, $expected, 'Empty %callback generates no output.'); Chris@0: Chris@0: $variables = []; Chris@0: $variables['heading'] = 'Some title'; Chris@0: $expected = ''; Chris@0: $this->assertThemeOutput('links', $variables, $expected, 'Empty %callback with heading generates no output.'); Chris@0: Chris@0: // Verify that a list of links is properly rendered. Chris@0: $variables = []; Chris@0: $variables['attributes'] = ['id' => 'somelinks']; Chris@0: $variables['links'] = [ Chris@0: 'a link' => [ Chris@0: 'title' => 'A ', Chris@0: 'url' => Url::fromUri('base:a/link'), Chris@0: ], Chris@0: 'plain text' => [ Chris@0: 'title' => 'Plain "text"', Chris@0: ], Chris@0: 'html text' => [ Chris@0: 'title' => SafeMarkup::format('@text', ['@text' => 'potentially unsafe text that be escaped']), Chris@0: ], Chris@0: 'front page' => [ Chris@0: 'title' => 'Front page', Chris@0: 'url' => Url::fromRoute(''), Chris@0: ], Chris@0: 'router-test' => [ Chris@0: 'title' => 'Test route', Chris@0: 'url' => Url::fromRoute('router_test.1'), Chris@0: ], Chris@0: 'query-test' => [ Chris@0: 'title' => 'Query test route', Chris@0: 'url' => Url::fromRoute('router_test.1'), Chris@0: 'query' => [ Chris@0: 'key' => 'value', Chris@0: ] Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $expected_links = ''; Chris@0: $expected_links .= ''; Chris@0: Chris@0: // Verify that passing a string as heading works. Chris@0: $variables['heading'] = 'Links heading'; Chris@0: $expected_heading = '

Links heading

'; Chris@0: $expected = $expected_heading . $expected_links; Chris@0: $this->assertThemeOutput('links', $variables, $expected); Chris@0: Chris@0: // Restore the original request's query. Chris@0: \Drupal::request()->query->replace($original_query); Chris@0: Chris@0: // Verify that passing an array as heading works (core support). Chris@0: $variables['heading'] = [ Chris@0: 'text' => 'Links heading', Chris@0: 'level' => 'h3', Chris@0: 'attributes' => ['class' => ['heading']], Chris@0: ]; Chris@0: $expected_heading = '

Links heading

'; Chris@0: $expected = $expected_heading . $expected_links; Chris@0: $this->assertThemeOutput('links', $variables, $expected); Chris@0: Chris@0: // Verify that passing attributes for the heading works. Chris@0: $variables['heading'] = ['text' => 'Links heading', 'level' => 'h3', 'attributes' => ['id' => 'heading']]; Chris@0: $expected_heading = '

Links heading

'; Chris@0: $expected = $expected_heading . $expected_links; Chris@0: $this->assertThemeOutput('links', $variables, $expected); Chris@0: Chris@0: // Verify that passing attributes for the links work. Chris@0: $variables['links']['plain text']['attributes'] = [ Chris@0: 'class' => ['a/class'], Chris@0: ]; Chris@0: $expected_links = ''; Chris@0: $expected_links .= ''; Chris@0: $expected = $expected_heading . $expected_links; Chris@0: $this->assertThemeOutput('links', $variables, $expected); Chris@0: Chris@0: // Verify the data- attributes for setting the "active" class on links. Chris@0: \Drupal::currentUser()->setAccount(new UserSession(['uid' => 1])); Chris@0: $variables['set_active_class'] = TRUE; Chris@0: $expected_links = ''; Chris@0: $expected_links .= ''; Chris@0: $expected = $expected_heading . $expected_links; Chris@0: $this->assertThemeOutput('links', $variables, $expected); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests links.html.twig using links with indexed keys. Chris@0: */ Chris@0: public function testIndexedKeyedLinks() { Chris@0: // Turn off the query for the Chris@0: // \Drupal\Core\Utility\LinkGeneratorInterface::generate() method to compare Chris@0: // the active link correctly. Chris@0: $original_query = \Drupal::request()->query->all(); Chris@0: \Drupal::request()->query->replace([]); Chris@0: // Verify that empty variables produce no output. Chris@0: $variables = []; Chris@0: $expected = ''; Chris@0: $this->assertThemeOutput('links', $variables, $expected, 'Empty %callback generates no output.'); Chris@0: Chris@0: $variables = []; Chris@0: $variables['heading'] = 'Some title'; Chris@0: $expected = ''; Chris@0: $this->assertThemeOutput('links', $variables, $expected, 'Empty %callback with heading generates no output.'); Chris@0: Chris@0: // Verify that a list of links is properly rendered. Chris@0: $variables = []; Chris@0: $variables['attributes'] = ['id' => 'somelinks']; Chris@0: $variables['links'] = [ Chris@0: [ Chris@0: 'title' => 'A ', Chris@0: 'url' => Url::fromUri('base:a/link'), Chris@0: ], Chris@0: [ Chris@0: 'title' => 'Plain "text"', Chris@0: ], Chris@0: [ Chris@0: 'title' => SafeMarkup::format('@text', ['@text' => 'potentially unsafe text that be escaped']), Chris@0: ], Chris@0: [ Chris@0: 'title' => 'Front page', Chris@0: 'url' => Url::fromRoute(''), Chris@0: ], Chris@0: [ Chris@0: 'title' => 'Test route', Chris@0: 'url' => Url::fromRoute('router_test.1'), Chris@0: ], Chris@0: [ Chris@0: 'title' => 'Query test route', Chris@0: 'url' => Url::fromRoute('router_test.1'), Chris@0: 'query' => [ Chris@0: 'key' => 'value', Chris@0: ] Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $expected_links = ''; Chris@0: $expected_links .= ''; Chris@0: Chris@0: // Verify that passing a string as heading works. Chris@0: $variables['heading'] = 'Links heading'; Chris@0: $expected_heading = '

Links heading

'; Chris@0: $expected = $expected_heading . $expected_links; Chris@0: $this->assertThemeOutput('links', $variables, $expected); Chris@0: Chris@0: // Restore the original request's query. Chris@0: \Drupal::request()->query->replace($original_query); Chris@0: Chris@0: // Verify that passing an array as heading works (core support). Chris@0: $variables['heading'] = [ Chris@0: 'text' => 'Links heading', Chris@0: 'level' => 'h3', Chris@0: 'attributes' => ['class' => ['heading']], Chris@0: ]; Chris@0: $expected_heading = '

Links heading

'; Chris@0: $expected = $expected_heading . $expected_links; Chris@0: $this->assertThemeOutput('links', $variables, $expected); Chris@0: Chris@0: // Verify that passing attributes for the heading works. Chris@0: $variables['heading'] = ['text' => 'Links heading', 'level' => 'h3', 'attributes' => ['id' => 'heading']]; Chris@0: $expected_heading = '

Links heading

'; Chris@0: $expected = $expected_heading . $expected_links; Chris@0: $this->assertThemeOutput('links', $variables, $expected); Chris@0: Chris@0: // Verify that passing attributes for the links work. Chris@0: $variables['links'][1]['attributes'] = [ Chris@0: 'class' => ['a/class'], Chris@0: ]; Chris@0: $expected_links = ''; Chris@0: $expected_links .= ''; Chris@0: $expected = $expected_heading . $expected_links; Chris@0: $this->assertThemeOutput('links', $variables, $expected); Chris@0: Chris@0: // Verify the data- attributes for setting the "active" class on links. Chris@0: \Drupal::currentUser()->setAccount(new UserSession(['uid' => 1])); Chris@0: $variables['set_active_class'] = TRUE; Chris@0: $expected_links = ''; Chris@0: $expected_links .= ''; Chris@0: $expected = $expected_heading . $expected_links; Chris@0: $this->assertThemeOutput('links', $variables, $expected); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test the use of drupal_pre_render_links() on a nested array of links. Chris@0: */ Chris@0: public function testDrupalPreRenderLinks() { Chris@0: // Define the base array to be rendered, containing a variety of different Chris@0: // kinds of links. Chris@0: $base_array = [ Chris@0: '#theme' => 'links', Chris@0: '#pre_render' => ['drupal_pre_render_links'], Chris@0: '#links' => [ Chris@0: 'parent_link' => [ Chris@0: 'title' => 'Parent link original', Chris@0: 'url' => Url::fromRoute('router_test.1'), Chris@0: ], Chris@0: ], Chris@0: 'first_child' => [ Chris@0: '#theme' => 'links', Chris@0: '#links' => [ Chris@0: // This should be rendered if 'first_child' is rendered separately, Chris@0: // but ignored if the parent is being rendered (since it duplicates Chris@0: // one of the parent's links). Chris@0: 'parent_link' => [ Chris@0: 'title' => 'Parent link copy', Chris@0: 'url' => Url::fromRoute('router_test.6'), Chris@0: ], Chris@0: // This should always be rendered. Chris@0: 'first_child_link' => [ Chris@0: 'title' => 'First child link', Chris@0: 'url' => Url::fromRoute('router_test.7'), Chris@0: ], Chris@0: ], Chris@0: ], Chris@0: // This should always be rendered as part of the parent. Chris@0: 'second_child' => [ Chris@0: '#theme' => 'links', Chris@0: '#links' => [ Chris@0: 'second_child_link' => [ Chris@0: 'title' => 'Second child link', Chris@0: 'url' => Url::fromRoute('router_test.8'), Chris@0: ], Chris@0: ], Chris@0: ], Chris@0: // This should never be rendered, since the user does not have access to Chris@0: // it. Chris@0: 'third_child' => [ Chris@0: '#theme' => 'links', Chris@0: '#links' => [ Chris@0: 'third_child_link' => [ Chris@0: 'title' => 'Third child link', Chris@0: 'url' => Url::fromRoute('router_test.9'), Chris@0: ], Chris@0: ], Chris@0: '#access' => FALSE, Chris@0: ], Chris@0: ]; Chris@0: Chris@0: // Start with a fresh copy of the base array, and try rendering the entire Chris@0: // thing. We expect a single
    with appropriate links contained within Chris@0: // it. Chris@0: $render_array = $base_array; Chris@0: $html = \Drupal::service('renderer')->renderRoot($render_array); Chris@0: $dom = new \DOMDocument(); Chris@0: $dom->loadHTML($html); Chris@0: $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered HTML.'); Chris@0: $list_elements = $dom->getElementsByTagName('li'); Chris@0: $this->assertEqual($list_elements->length, 3, 'Three "li" tags found in the rendered HTML.'); Chris@0: $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link original', 'First expected link found.'); Chris@0: $this->assertEqual($list_elements->item(1)->nodeValue, 'First child link', 'Second expected link found.'); Chris@0: $this->assertEqual($list_elements->item(2)->nodeValue, 'Second child link', 'Third expected link found.'); Chris@0: $this->assertIdentical(strpos($html, 'Parent link copy'), FALSE, '"Parent link copy" link not found.'); Chris@0: $this->assertIdentical(strpos($html, 'Third child link'), FALSE, '"Third child link" link not found.'); Chris@0: Chris@0: // Now render 'first_child', followed by the rest of the links, and make Chris@0: // sure we get two separate
      's with the appropriate links contained Chris@0: // within each. Chris@0: $render_array = $base_array; Chris@0: $child_html = \Drupal::service('renderer')->renderRoot($render_array['first_child']); Chris@0: $parent_html = \Drupal::service('renderer')->renderRoot($render_array); Chris@0: // First check the child HTML. Chris@0: $dom = new \DOMDocument(); Chris@0: $dom->loadHTML($child_html); Chris@0: $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered child HTML.'); Chris@0: $list_elements = $dom->getElementsByTagName('li'); Chris@0: $this->assertEqual($list_elements->length, 2, 'Two "li" tags found in the rendered child HTML.'); Chris@0: $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link copy', 'First expected link found.'); Chris@0: $this->assertEqual($list_elements->item(1)->nodeValue, 'First child link', 'Second expected link found.'); Chris@0: // Then check the parent HTML. Chris@0: $dom = new \DOMDocument(); Chris@0: $dom->loadHTML($parent_html); Chris@0: $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered parent HTML.'); Chris@0: $list_elements = $dom->getElementsByTagName('li'); Chris@0: $this->assertEqual($list_elements->length, 2, 'Two "li" tags found in the rendered parent HTML.'); Chris@0: $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link original', 'First expected link found.'); Chris@0: $this->assertEqual($list_elements->item(1)->nodeValue, 'Second child link', 'Second expected link found.'); Chris@0: $this->assertIdentical(strpos($parent_html, 'First child link'), FALSE, '"First child link" link not found.'); Chris@0: $this->assertIdentical(strpos($parent_html, 'Third child link'), FALSE, '"Third child link" link not found.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests theme_image(). Chris@0: */ Chris@0: public function testImage() { Chris@0: // Test that data URIs work with theme_image(). Chris@0: $variables = []; Chris@0: $variables['uri'] = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='; Chris@0: $variables['alt'] = 'Data URI image of a red dot'; Chris@0: $expected = 'Data URI image of a red dot' . "\n"; Chris@0: $this->assertThemeOutput('image', $variables, $expected); Chris@0: } Chris@0: Chris@0: }