function LocaleStringIsSafeTest::testLocaleStringIsSafe

Same name in other branches
  1. 7.x modules/locale/locale.test \LocaleStringIsSafeTest::testLocaleStringIsSafe()
  2. 9 core/modules/locale/tests/src/Kernel/LocaleStringIsSafeTest.php \Drupal\Tests\locale\Kernel\LocaleStringIsSafeTest::testLocaleStringIsSafe()
  3. 10 core/modules/locale/tests/src/Kernel/LocaleStringIsSafeTest.php \Drupal\Tests\locale\Kernel\LocaleStringIsSafeTest::testLocaleStringIsSafe()
  4. 11.x 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);
}

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