Newer
Older
Alex Pott
committed
<?php
namespace Drupal\system\Tests\Common;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
Alex Pott
committed
use Drupal\simpletest\KernelTestBase;
Alex Pott
committed
/**
* Tests the markup of core render element types passed to drupal_render().
*
* @group Common
Alex Pott
committed
*/
Alex Pott
committed
class RenderElementTypesTest extends KernelTestBase {
Angie Byron
committed
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('system', 'router_test');
Angie Byron
committed
protected function setUp() {
parent::setUp();
$this->installConfig(array('system'));
\Drupal::service('router.builder')->rebuild();
Angie Byron
committed
}
Alex Pott
committed
/**
* Asserts that an array of elements is rendered properly.
*
* @param array $elements
Angie Byron
committed
* The render element array to test.
* @param string $expected_html
* The expected markup.
* @param string $message
* Assertion message.
Alex Pott
committed
*/
Angie Byron
committed
protected function assertElements(array $elements, $expected_html, $message) {
$actual_html = (string) \Drupal::service('renderer')->renderRoot($elements);
Angie Byron
committed
$out = '<table><tr>';
$out .= '<td valign="top"><pre>' . Html::escape($expected_html) . '</pre></td>';
$out .= '<td valign="top"><pre>' . Html::escape($actual_html) . '</pre></td>';
Angie Byron
committed
$out .= '</tr></table>';
$this->verbose($out);
$this->assertIdentical($actual_html, $expected_html, Html::escape($message));
Alex Pott
committed
}
/**
* Tests system #type 'container'.
*/
function testContainer() {
Angie Byron
committed
// Basic container with no attributes.
$this->assertElements(array(
'#type' => 'container',
'#markup' => 'foo',
), "<div>foo</div>\n", "#type 'container' with no HTML attributes");
// Container with a class.
$this->assertElements(array(
'#type' => 'container',
'#markup' => 'foo',
'#attributes' => array(
'class' => array('bar'),
Alex Pott
committed
),
Angie Byron
committed
), '<div class="bar">foo</div>' . "\n", "#type 'container' with a class HTML attribute");
Angie Byron
committed
Angie Byron
committed
// Container with children.
$this->assertElements(array(
'#type' => 'container',
'child' => array(
'#markup' => 'foo',
),
), "<div>foo</div>\n", "#type 'container' with child elements");
Alex Pott
committed
}
/**
* Tests system #type 'html_tag'.
*/
function testHtmlTag() {
Alex Pott
committed
// Test void element.
Angie Byron
committed
$this->assertElements(array(
'#type' => 'html_tag',
'#tag' => 'meta',
Alex Pott
committed
'#value' => 'ignored',
Angie Byron
committed
'#attributes' => array(
'name' => 'description',
'content' => 'Drupal test',
Alex Pott
committed
), '<meta name="description" content="Drupal test" />' . "\n", "#type 'html_tag', void element renders properly");
Angie Byron
committed
Alex Pott
committed
// Test non-void element.
Angie Byron
committed
$this->assertElements(array(
'#type' => 'html_tag',
Alex Pott
committed
'#tag' => 'section',
'#value' => 'value',
'#attributes' => array(
'class' => array('unicorns'),
),
), '<section class="unicorns">value</section>' . "\n", "#type 'html_tag', non-void element renders properly");
Alex Pott
committed
// Test empty void element tag.
$this->assertElements(array(
'#type' => 'html_tag',
'#tag' => 'link',
), "<link />\n", "#type 'html_tag' empty void element renders properly");
// Test empty non-void element tag.
$this->assertElements(array(
'#type' => 'html_tag',
'#tag' => 'section',
), "<section></section>\n", "#type 'html_tag' empty non-void element renders properly");
}
/**
* Tests system #type 'more_link'.
*/
function testMoreLink() {
$elements = array(
array(
'name' => "#type 'more_link' anchor tag generation without extra classes",
'value' => array(
'#type' => 'more_link',
Jess
committed
'#url' => Url::fromUri('https://www.drupal.org'),
),
Jess
committed
'expected' => '//div[@class="more-link"]/a[@href="https://www.drupal.org" and text()="More"]',
),
array(
'name' => "#type 'more_link' anchor tag generation with different link text",
'value' => array(
'#type' => 'more_link',
Jess
committed
'#url' => Url::fromUri('https://www.drupal.org'),
'#title' => 'More Titles',
),
Jess
committed
'expected' => '//div[@class="more-link"]/a[@href="https://www.drupal.org" and text()="More Titles"]',
),
array(
'name' => "#type 'more_link' anchor tag generation with attributes on wrapper",
'value' => array(
'#type' => 'more_link',
Jess
committed
'#url' => Url::fromUri('https://www.drupal.org'),
'#theme_wrappers' => array(
'container' => array(
'#attributes' => array(
'title' => 'description',
'class' => array('more-link', 'drupal', 'test'),
),
),
),
),
Jess
committed
'expected' => '//div[@title="description" and contains(@class, "more-link") and contains(@class, "drupal") and contains(@class, "test")]/a[@href="https://www.drupal.org" and text()="More"]',
),
array(
'name' => "#type 'more_link' anchor tag with a relative path",
'value' => array(
'#type' => 'more_link',
'#url' => Url::fromRoute('router_test.1'),
),
'expected' => '//div[@class="more-link"]/a[@href="' . Url::fromRoute('router_test.1')->toString() . '" and text()="More"]',
),
array(
'name' => "#type 'more_link' anchor tag with a route",
'value' => array(
'#type' => 'more_link',
'#url' => Url::fromRoute('router_test.1'),
),
'expected' => '//div[@class="more-link"]/a[@href="' . \Drupal::urlGenerator()->generate('router_test.1') . '" and text()="More"]',
),
array(
'name' => "#type 'more_link' anchor tag with an absolute path",
'value' => array(
'#type' => 'more_link',
'#url' => Url::fromRoute('system.admin_content'),
'#options' => array('absolute' => TRUE),
),
'expected' => '//div[@class="more-link"]/a[@href="' . Url::fromRoute('system.admin_content')->setAbsolute()->toString() . '" and text()="More"]',
),
array(
'name' => "#type 'more_link' anchor tag to the front page",
'value' => array(
'#type' => 'more_link',
'#url' => Url::fromRoute('<front>'),
),
'expected' => '//div[@class="more-link"]/a[@href="' . Url::fromRoute('<front>')->toString() . '" and text()="More"]',
),
);
foreach($elements as $element) {
$xml = new \SimpleXMLElement(\Drupal::service('renderer')->renderRoot($element['value']));
$result = $xml->xpath($element['expected']);
$this->assertTrue($result, '"' . $element['name'] . '" input rendered correctly by drupal_render().');
}
}
/**
* Tests system #type 'system_compact_link'.
*/
function testSystemCompactLink() {
$elements = array(
array(
'name' => "#type 'system_compact_link' when admin compact mode is off",
'value' => array(
'#type' => 'system_compact_link',
),
'expected' => '//div[@class="compact-link"]/a[contains(@href, "admin/compact/on?") and text()="Hide descriptions"]',
),
array(
'name' => "#type 'system_compact_link' when adding extra attributes",
'value' => array(
'#type' => 'system_compact_link',
'#attributes' => array(
'class' => array('kittens-rule'),
),
),
'expected' => '//div[@class="compact-link"]/a[contains(@href, "admin/compact/on?") and @class="kittens-rule" and text()="Hide descriptions"]',
),
);
foreach ($elements as $element) {
$xml = new \SimpleXMLElement(\Drupal::service('renderer')->renderRoot($element['value']));
$result = $xml->xpath($element['expected']);
$this->assertTrue($result, '"' . $element['name'] . '" is rendered correctly by drupal_render().');
}
// Set admin compact mode on for additional tests.
\Drupal::request()->cookies->set('Drupal_visitor_admin_compact_mode', TRUE);
$element = array(
'name' => "#type 'system_compact_link' when admin compact mode is on",
'value' => array(
'#type' => 'system_compact_link',
),
'expected' => '//div[@class="compact-link"]/a[contains(@href, "admin/compact?") and text()="Show descriptions"]',
);
$xml = new \SimpleXMLElement(\Drupal::service('renderer')->renderRoot($element['value']));
$result = $xml->xpath($element['expected']);
$this->assertTrue($result, '"' . $element['name'] . '" is rendered correctly by drupal_render().');
}