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 [ // Verify that no valid ASCII characters are stripped from the identifier. [$id1, $id1, []], // Verify that valid UTF-8 characters are not stripped from the identifier. [$id2, $id2, []], // Verify that double underscores are not stripped from the identifier. [$id3, $id3], // Verify that invalid characters (including non-breaking space) are // stripped from the identifier. ['invalididentifier', 'invalid !"#$%&\'()*+,./:;<=>?@[\\]^`{|}~ identifier', []], // Verify that an identifier starting with a digit is replaced. ['_cssidentifier', '1cssidentifier', []], // Verify that an identifier starting with a hyphen followed by a digit is // replaced. ['__cssidentifier', '-1cssidentifier', []], // Verify that an identifier starting with two hyphens is replaced. ['__cssidentifier', '--cssidentifier', []], // Verify that passing double underscores as a filter is processed. ['_cssidentifier', '__cssidentifier', ['__' => '_']], ]; } /** * Tests that Html::getClass() cleans the class name properly. * * @coversDefaultClass ::getClass */ public function testHtmlClass() { // Verify Drupal coding standards are enforced. $this->assertSame('class-name--ü', Html::getClass('CLASS NAME_[Ü]'), 'Enforce Drupal coding standards.'); // Test Html::getClass() handles Drupal\Component\Render\MarkupInterface // input. $markup = HtmlTestMarkup::create('CLASS_FROM_OBJECT'); $this->assertSame('class-from-object', Html::getClass($markup), 'Markup object is converted to CSS class.'); } /** * 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 [ // Verify that letters, digits, and hyphens are not stripped from the ID. [$id, $id], // Verify that invalid characters are stripped from the ID. ['invalididentifier', 'invalid,./:@\\^`{Üidentifier'], // Verify Drupal coding standards are enforced. ['id-name-1', 'ID NAME_[1]'], // Verify that a repeated ID is made unique. ['test-unique-id', 'test-unique-id', TRUE], ['test-unique-id--2', 'test-unique-id'], ['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 [ ['test-unique-id1--', 'test-unique-id1'], // Note, we truncate two hyphens at the end. // @see \Drupal\Component\Utility\Html::getId() ['test-unique-id1---', 'test-unique-id1--'], ['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 [ // Verify that letters, digits, and hyphens are not stripped from the ID. [$id, $id], // Verify that invalid characters are stripped from the ID. ['invalididentifier', 'invalid,./:@\\^`{Üidentifier'], // Verify Drupal coding standards are enforced. ['id-name-1', 'ID NAME_[1]'], // Verify that a repeated ID is made unique. ['test-unique-id', 'test-unique-id'], ['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 [ ['Drupal', 'Drupal'], ['