';
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
Un
Deux
Trois
';
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: $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 = '' . "\n";
Chris@0: $this->assertThemeOutput('image', $variables, $expected);
Chris@0: }
Chris@0:
Chris@0: }