function HtmlTest::testHtmlGetUniqueIdWithAjaxIds

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/Tests/Component/Utility/HtmlTest.php \Drupal\Tests\Component\Utility\HtmlTest::testHtmlGetUniqueIdWithAjaxIds()
  2. 10 core/tests/Drupal/Tests/Component/Utility/HtmlTest.php \Drupal\Tests\Component\Utility\HtmlTest::testHtmlGetUniqueIdWithAjaxIds()
  3. 11.x core/tests/Drupal/Tests/Component/Utility/HtmlTest.php \Drupal\Tests\Component\Utility\HtmlTest::testHtmlGetUniqueIdWithAjaxIds()

Tests the Html::getUniqueId() method.

@dataProvider providerTestHtmlGetUniqueIdWithAjaxIds

@covers ::getUniqueId

Parameters

string $expected: The expected result.

string $source: The string being transformed to an ID.

File

core/tests/Drupal/Tests/Component/Utility/HtmlTest.php, line 162

Class

HtmlTest
Tests <a href="/api/drupal/core%21lib%21Drupal%21Component%21Utility%21Html.php/class/Html/9" title="Provides DOMDocument helpers for parsing and serializing HTML strings." class="local">\Drupal\Component\Utility\Html</a>.

Namespace

Drupal\Tests\Component\Utility

Code

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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.