Newer
Older
Alex Pott
committed
<?php
/**
* @file
* Contains Drupal\system\Tests\Common\RenderElementTypesTest.
*/
namespace Drupal\system\Tests\Common;
use Drupal\Component\Utility\String;
Angie Byron
committed
use Drupal\simpletest\DrupalUnitTestBase;
Alex Pott
committed
/**
* Tests the markup of core render element types passed to drupal_render().
*/
Angie Byron
committed
class RenderElementTypesTest extends DrupalUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('system');
Alex Pott
committed
public static function getInfo() {
return array(
'name' => 'Render element types',
'description' => 'Tests the markup of core render element types passed to drupal_render().',
'group' => 'Common',
);
}
Angie Byron
committed
protected function setUp() {
parent::setUp();
$this->installConfig(array('system'));
$this->container->get('theme_handler')->enable(array('stark'));
}
Alex Pott
committed
/**
* Asserts that an array of elements is rendered properly.
*
* @param array $elements
Alex Pott
committed
* The render element array to test.
* @param string $expected_html
* The expected markup.
* @param string $message
* Assertion message.
Alex Pott
committed
*/
Alex Pott
committed
protected function assertElements(array $elements, $expected_html, $message) {
$actual_html = drupal_render($elements);
Alex Pott
committed
$out = '<table><tr>';
$out .= '<td valign="top"><pre>' . String::checkPlain($expected_html) . '</pre></td>';
$out .= '<td valign="top"><pre>' . String::checkPlain($actual_html) . '</pre></td>';
$out .= '</tr></table>';
$this->verbose($out);
Alex Pott
committed
$this->assertIdentical($actual_html, $expected_html, String::checkPlain($message));
Alex Pott
committed
}
/**
* Tests system #type 'container'.
*/
function testContainer() {
Alex Pott
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' => 'bar',
Alex Pott
committed
),
Alex Pott
committed
), '<div class="bar">foo</div>' . "\n", "#type 'container' with a class HTML attribute");
Alex Pott
committed
Alex Pott
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 auto-closure meta tag generation.
$this->assertElements(array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'name' => 'description',
'content' => 'Drupal test',
Alex Pott
committed
), '<meta name="description" content="Drupal test" />' . "\n", "#type 'html_tag' auto-closure meta tag generation");
// Test title tag generation.
$this->assertElements(array(
'#type' => 'html_tag',
'#tag' => 'title',
'#value' => 'title test',
), "<title>title test</title>\n", "#type 'html_tag' title tag generation");
}