class HtmlTagTest in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/tests/Drupal/Tests/Core/Render/Element/HtmlTagTest.php \Drupal\Tests\Core\Render\Element\HtmlTagTest
@coversDefaultClass \Drupal\Core\Render\Element\HtmlTag @group Render
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\Core\Render\Element\HtmlTagTest
Expanded class hierarchy of HtmlTagTest
File
- core/
tests/ Drupal/ Tests/ Core/ Render/ Element/ HtmlTagTest.php, line 18 - Contains \Drupal\Tests\Core\Render\Element\HtmlTagTest.
Namespace
Drupal\Tests\Core\Render\ElementView source
class HtmlTagTest extends UnitTestCase {
/**
* @covers ::getInfo
*/
public function testGetInfo() {
$htmlTag = new HtmlTag(array(), 'test', 'test');
$info = $htmlTag
->getInfo();
$this
->assertArrayHasKey('#pre_render', $info);
$this
->assertArrayHasKey('#attributes', $info);
$this
->assertArrayHasKey('#value', $info);
}
/**
* @covers ::preRenderHtmlTag
* @dataProvider providerPreRenderHtmlTag
*/
public function testPreRenderHtmlTag($element, $expected) {
$result = HtmlTag::preRenderHtmlTag($element);
$this
->assertArrayHasKey('#markup', $result);
$this
->assertEquals($expected, $result['#markup']);
}
/**
* Data provider for preRenderHtmlTag test.
*/
public function providerPreRenderHtmlTag() {
$tags = array();
// Value prefix/suffix.
$element = array(
'#value' => 'value',
'#tag' => 'p',
);
$tags[] = array(
$element,
'<p>value</p>' . "\n",
);
// Normal element without a value should not result in a void element.
$element = array(
'#tag' => 'p',
'#value' => NULL,
);
$tags[] = array(
$element,
"<p></p>\n",
);
// A void element.
$element = array(
'#tag' => 'br',
);
$tags[] = array(
$element,
"<br />\n",
);
// Attributes.
$element = array(
'#tag' => 'div',
'#attributes' => array(
'class' => 'test',
'id' => 'id',
),
'#value' => 'value',
);
$tags[] = array(
$element,
'<div class="test" id="id">value</div>' . "\n",
);
// No script tags.
$element['#noscript'] = TRUE;
$tags[] = array(
$element,
'<noscript><div class="test" id="id">value</div>' . "\n" . '</noscript>',
);
// Ensure that #tag is sanitised.
$element = array(
'#tag' => 'p><script>alert()</script><p',
'#value' => 'value',
);
$tags[] = array(
$element,
"<p><script>alert()</script><p>value</p><script>alert()</script><p>\n",
);
// Ensure that #value is not filtered if it is marked as safe.
$element = array(
'#tag' => 'p',
'#value' => Markup::create('<script>value</script>'),
);
$tags[] = array(
$element,
"<p><script>value</script></p>\n",
);
// Ensure that #value is filtered if it is not safe.
$element = array(
'#tag' => 'p',
'#value' => '<script>value</script>',
);
$tags[] = array(
$element,
"<p>value</p>\n",
);
return $tags;
}
/**
* @covers ::preRenderConditionalComments
* @dataProvider providerPreRenderConditionalComments
*/
public function testPreRenderConditionalComments($element, $expected, $set_safe = FALSE) {
if ($set_safe) {
$element['#prefix'] = Markup::create($element['#prefix']);
$element['#suffix'] = Markup::create($element['#suffix']);
}
$this
->assertEquals($expected, HtmlTag::preRenderConditionalComments($element));
}
/**
* Data provider for conditional comments test.
*/
public function providerPreRenderConditionalComments() {
// No browser specification.
$element = array(
'#tag' => 'link',
);
$tags[] = array(
$element,
$element,
);
// Specify all browsers.
$element['#browsers'] = array(
'IE' => TRUE,
'!IE' => TRUE,
);
$tags[] = array(
$element,
$element,
);
// All IE.
$element = array(
'#tag' => 'link',
'#browsers' => array(
'IE' => TRUE,
'!IE' => FALSE,
),
);
$expected = $element;
$expected['#prefix'] = "\n<!--[if IE]>\n";
$expected['#suffix'] = "<![endif]-->\n";
$tags[] = array(
$element,
$expected,
);
// Exclude IE.
$element = array(
'#tag' => 'link',
'#browsers' => array(
'IE' => FALSE,
),
);
$expected = $element;
$expected['#prefix'] = "\n<!--[if !IE]><!-->\n";
$expected['#suffix'] = "<!--<![endif]-->\n";
$tags[] = array(
$element,
$expected,
);
// IE gt 8
$element = array(
'#tag' => 'link',
'#browsers' => array(
'IE' => 'gt IE 8',
),
);
$expected = $element;
$expected['#prefix'] = "\n<!--[if gt IE 8]><!-->\n";
$expected['#suffix'] = "<!--<![endif]-->\n";
$tags[] = array(
$element,
$expected,
);
// Prefix and suffix filtering if not safe.
$element = array(
'#tag' => 'link',
'#browsers' => array(
'IE' => FALSE,
),
'#prefix' => '<blink>prefix</blink>',
'#suffix' => '<blink>suffix</blink>',
);
$expected = $element;
$expected['#prefix'] = "\n<!--[if !IE]><!-->\nprefix";
$expected['#suffix'] = "suffix<!--<![endif]-->\n";
$tags[] = array(
$element,
$expected,
);
// Prefix and suffix filtering if marked as safe. This has to come after the
// previous test case.
$expected['#prefix'] = "\n<!--[if !IE]><!-->\n<blink>prefix</blink>";
$expected['#suffix'] = "<blink>suffix</blink><!--<![endif]-->\n";
$tags[] = array(
$element,
$expected,
TRUE,
);
return $tags;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
HtmlTagTest:: |
public | function | Data provider for conditional comments test. | |
HtmlTagTest:: |
public | function | Data provider for preRenderHtmlTag test. | |
HtmlTagTest:: |
public | function | @covers ::getInfo | |
HtmlTagTest:: |
public | function | @covers ::preRenderConditionalComments @dataProvider providerPreRenderConditionalComments | |
HtmlTagTest:: |
public | function | @covers ::preRenderHtmlTag @dataProvider providerPreRenderHtmlTag | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed in array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 259 |