class HtmlTest in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/tests/Drupal/Tests/Component/Utility/HtmlTest.php \Drupal\Tests\Component\Utility\HtmlTest
Tests \Drupal\Component\Utility\Html.
@group Common
@coversDefaultClass \Drupal\Component\Utility\Html
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\Component\Utility\HtmlTest
Expanded class hierarchy of HtmlTest
File
- core/
tests/ Drupal/ Tests/ Component/ Utility/ HtmlTest.php, line 20 - Contains \Drupal\Tests\Component\Utility\HtmlTest.
Namespace
Drupal\Tests\Component\UtilityView source
class HtmlTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$property = new \ReflectionProperty('Drupal\\Component\\Utility\\Html', 'seenIdsInit');
$property
->setAccessible(TRUE);
$property
->setValue(NULL);
}
/**
* Tests the Html::cleanCssIdentifier() method.
*
* @param string $expected
* The expected result.
* @param string $source
* The string being transformed to an ID.
* @param array|null $filter
* (optional) An array of string replacements to use on the identifier. If
* NULL, no filter will be passed and a default will be used.
*
* @dataProvider providerTestCleanCssIdentifier
*
* @covers ::cleanCssIdentifier
*/
public function testCleanCssIdentifier($expected, $source, $filter = NULL) {
if ($filter !== NULL) {
$this
->assertSame($expected, Html::cleanCssIdentifier($source, $filter));
}
else {
$this
->assertSame($expected, Html::cleanCssIdentifier($source));
}
}
/**
* Provides test data for testCleanCssIdentifier().
*
* @return array
* Test data.
*/
public function providerTestCleanCssIdentifier() {
$id1 = 'abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789';
$id2 = '¡¢£¤¥';
$id3 = 'css__identifier__with__double__underscores';
return array(
// Verify that no valid ASCII characters are stripped from the identifier.
array(
$id1,
$id1,
array(),
),
// Verify that valid UTF-8 characters are not stripped from the identifier.
array(
$id2,
$id2,
array(),
),
// Verify that invalid characters (including non-breaking space) are stripped from the identifier.
array(
$id3,
$id3,
),
// Verify that double underscores are not stripped from the identifier.
array(
'invalididentifier',
'invalid !"#$%&\'()*+,./:;<=>?@[\\]^`{|}~ identifier',
array(),
),
// Verify that an identifier starting with a digit is replaced.
array(
'_cssidentifier',
'1cssidentifier',
array(),
),
// Verify that an identifier starting with a hyphen followed by a digit is
// replaced.
array(
'__cssidentifier',
'-1cssidentifier',
array(),
),
// Verify that an identifier starting with two hyphens is replaced.
array(
'__cssidentifier',
'--cssidentifier',
array(),
),
// Verify that passing double underscores as a filter is processed.
array(
'_cssidentifier',
'__cssidentifier',
array(
'__' => '_',
),
),
);
}
/**
* Tests that Html::getClass() cleans the class name properly.
*
* @coversDefaultClass ::getClass
*/
public function testHtmlClass() {
// Verify Drupal coding standards are enforced.
$this
->assertSame(Html::getClass('CLASS NAME_[Ü]'), 'class-name--ü', 'Enforce Drupal coding standards.');
}
/**
* Tests the Html::getUniqueId() method.
*
* @param string $expected
* The expected result.
* @param string $source
* The string being transformed to an ID.
* @param bool $reset
* (optional) If TRUE, reset the list of seen IDs. Defaults to FALSE.
*
* @dataProvider providerTestHtmlGetUniqueId
*
* @covers ::getUniqueId
*/
public function testHtmlGetUniqueId($expected, $source, $reset = FALSE) {
if ($reset) {
Html::resetSeenIds();
}
$this
->assertSame($expected, Html::getUniqueId($source));
}
/**
* Provides test data for testHtmlGetId().
*
* @return array
* Test data.
*/
public function providerTestHtmlGetUniqueId() {
$id = 'abcdefghijklmnopqrstuvwxyz-0123456789';
return array(
// Verify that letters, digits, and hyphens are not stripped from the ID.
array(
$id,
$id,
),
// Verify that invalid characters are stripped from the ID.
array(
'invalididentifier',
'invalid,./:@\\^`{Üidentifier',
),
// Verify Drupal coding standards are enforced.
array(
'id-name-1',
'ID NAME_[1]',
),
// Verify that a repeated ID is made unique.
array(
'test-unique-id',
'test-unique-id',
TRUE,
),
array(
'test-unique-id--2',
'test-unique-id',
),
array(
'test-unique-id--3',
'test-unique-id',
),
);
}
/**
* Tests the Html::getUniqueId() method.
*
* @param string $expected
* The expected result.
* @param string $source
* The string being transformed to an ID.
*
* @dataProvider providerTestHtmlGetUniqueIdWithAjaxIds
*
* @covers ::getUniqueId
*/
public function testHtmlGetUniqueIdWithAjaxIds($expected, $source) {
Html::setIsAjax(TRUE);
$id = Html::getUniqueId($source);
// Note, we truncate two hyphens at the end.
// @see \Drupal\Component\Utility\Html::getId()
if (strpos($source, '--') !== FALSE) {
$random_suffix = substr($id, strlen($source) + 1);
}
else {
$random_suffix = substr($id, strlen($source) + 2);
}
$expected = $expected . $random_suffix;
$this
->assertSame($expected, $id);
}
/**
* Provides test data for testHtmlGetId().
*
* @return array
* Test data.
*/
public function providerTestHtmlGetUniqueIdWithAjaxIds() {
return array(
array(
'test-unique-id1--',
'test-unique-id1',
),
// Note, we truncate two hyphens at the end.
// @see \Drupal\Component\Utility\Html::getId()
array(
'test-unique-id1---',
'test-unique-id1--',
),
array(
'test-unique-id2--',
'test-unique-id2',
),
);
}
/**
* Tests the Html::getUniqueId() method.
*
* @param string $expected
* The expected result.
* @param string $source
* The string being transformed to an ID.
*
* @dataProvider providerTestHtmlGetId
*
* @covers ::getId
*/
public function testHtmlGetId($expected, $source) {
Html::setIsAjax(FALSE);
$this
->assertSame($expected, Html::getId($source));
}
/**
* Provides test data for testHtmlGetId().
*
* @return array
* Test data.
*/
public function providerTestHtmlGetId() {
$id = 'abcdefghijklmnopqrstuvwxyz-0123456789';
return array(
// Verify that letters, digits, and hyphens are not stripped from the ID.
array(
$id,
$id,
),
// Verify that invalid characters are stripped from the ID.
array(
'invalididentifier',
'invalid,./:@\\^`{Üidentifier',
),
// Verify Drupal coding standards are enforced.
array(
'id-name-1',
'ID NAME_[1]',
),
// Verify that a repeated ID is made unique.
array(
'test-unique-id',
'test-unique-id',
),
array(
'test-unique-id',
'test-unique-id',
),
);
}
/**
* Tests Html::decodeEntities().
*
* @dataProvider providerDecodeEntities
* @covers ::decodeEntities
*/
public function testDecodeEntities($text, $expected) {
$this
->assertEquals($expected, Html::decodeEntities($text));
}
/**
* Data provider for testDecodeEntities().
*
* @see testDecodeEntities()
*/
public function providerDecodeEntities() {
return array(
array(
'Drupal',
'Drupal',
),
array(
'<script>',
'<script>',
),
array(
'<script>',
'<script>',
),
array(
'<script>',
'<script>',
),
array(
'&lt;script&gt;',
'<script>',
),
array(
'"',
'"',
),
array(
'"',
'"',
),
array(
'&#34;',
'"',
),
array(
'"',
'"',
),
array(
'&quot;',
'"',
),
array(
"'",
"'",
),
array(
''',
"'",
),
array(
'&#39;',
''',
),
array(
'©',
'©',
),
array(
'©',
'©',
),
array(
'©',
'©',
),
array(
'→',
'→',
),
array(
'→',
'→',
),
array(
'➼',
'➼',
),
array(
'➼',
'➼',
),
array(
'€',
'€',
),
);
}
/**
* Tests Html::escape().
*
* @dataProvider providerEscape
* @covers ::escape
*/
public function testEscape($expected, $text) {
$this
->assertEquals($expected, Html::escape($text));
}
/**
* Data provider for testEscape().
*
* @see testEscape()
*/
public function providerEscape() {
return array(
array(
'Drupal',
'Drupal',
),
array(
'<script>',
'<script>',
),
array(
'&lt;script&gt;',
'<script>',
),
array(
'&#34;',
'"',
),
array(
'"',
'"',
),
array(
'&quot;',
'"',
),
array(
''',
"'",
),
array(
'&#039;',
''',
),
array(
'©',
'©',
),
array(
'→',
'→',
),
array(
'➼',
'➼',
),
array(
'€',
'€',
),
array(
'Drup�al',
"",
),
);
}
/**
* Tests relationship between escaping and decoding HTML entities.
*
* @covers ::decodeEntities
* @covers ::escape
*/
public function testDecodeEntitiesAndEscape() {
$string = "<em>répété</em>";
$escaped = Html::escape($string);
$this
->assertSame('<em>répét&eacute;</em>', $escaped);
$decoded = Html::decodeEntities($escaped);
$this
->assertSame('<em>répété</em>', $decoded);
$decoded = Html::decodeEntities($decoded);
$this
->assertSame('<em>répété</em>', $decoded);
$escaped = Html::escape($decoded);
$this
->assertSame('<em>répété</em>', $escaped);
}
/**
* Tests Html::serialize().
*
* Resolves an issue by where an empty DOMDocument object sent to serialization would
* cause errors in getElementsByTagName() in the serialization function.
*
* @covers ::serialize
*/
public function testSerialize() {
$document = new \DOMDocument();
$result = Html::serialize($document);
$this
->assertSame('', $result);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
HtmlTest:: |
public | function | Data provider for testDecodeEntities(). | |
HtmlTest:: |
public | function | Data provider for testEscape(). | |
HtmlTest:: |
public | function | Provides test data for testCleanCssIdentifier(). | |
HtmlTest:: |
public | function | Provides test data for testHtmlGetId(). | |
HtmlTest:: |
public | function | Provides test data for testHtmlGetId(). | |
HtmlTest:: |
public | function | Provides test data for testHtmlGetId(). | |
HtmlTest:: |
protected | function |
Overrides UnitTestCase:: |
|
HtmlTest:: |
public | function | Tests the Html::cleanCssIdentifier() method. | |
HtmlTest:: |
public | function | Tests Html::decodeEntities(). | |
HtmlTest:: |
public | function | Tests relationship between escaping and decoding HTML entities. | |
HtmlTest:: |
public | function | Tests Html::escape(). | |
HtmlTest:: |
public | function | Tests that Html::getClass() cleans the class name properly. | |
HtmlTest:: |
public | function | Tests the Html::getUniqueId() method. | |
HtmlTest:: |
public | function | Tests the Html::getUniqueId() method. | |
HtmlTest:: |
public | function | Tests the Html::getUniqueId() method. | |
HtmlTest:: |
public | function | Tests Html::serialize(). | |
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. |