You are here

public function LocaleStringIsSafeTest::testLocaleStringIsSafe in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/locale/tests/src/Kernel/LocaleStringIsSafeTest.php \Drupal\Tests\locale\Kernel\LocaleStringIsSafeTest::testLocaleStringIsSafe()
  2. 9 core/modules/locale/tests/src/Kernel/LocaleStringIsSafeTest.php \Drupal\Tests\locale\Kernel\LocaleStringIsSafeTest::testLocaleStringIsSafe()

Tests for locale_string_is_safe().

File

core/modules/locale/tests/src/Kernel/LocaleStringIsSafeTest.php, line 24

Class

LocaleStringIsSafeTest
Tests locale translation safe string handling.

Namespace

Drupal\Tests\locale\Kernel

Code

public function testLocaleStringIsSafe() {

  // Check a translatable string without HTML.
  $string = 'Hello world!';
  $result = locale_string_is_safe($string);
  $this
    ->assertTrue($result);

  // Check a translatable string which includes trustable HTML.
  $string = 'Hello <strong>world</strong>!';
  $result = locale_string_is_safe($string);
  $this
    ->assertTrue($result);

  // Check an untranslatable string which includes untrustable HTML (according
  // to the locale_string_is_safe() function definition).
  $string = 'Hello <img src="world.png" alt="world" />!';
  $result = locale_string_is_safe($string);
  $this
    ->assertFalse($result);

  // Check a translatable string which includes a token in an href attribute.
  $string = 'Hi <a href="[current-user:url]">user</a>';
  $result = locale_string_is_safe($string);
  $this
    ->assertTrue($result);
}