You are here

public function HtmlTest::testHtmlGetUniqueIdWithAjaxIds in Drupal 8

Same name and namespace in other branches
  1. 9 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 157

Class

HtmlTest
Tests \Drupal\Component\Utility\Html.

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