getMockBuilder('\Drupal\Component\Render\MarkupInterface')->getMock(); $this->assertTrue(SafeMarkup::isSafe($safe_string)); $string_object = new SafeMarkupTestString('test'); $this->assertFalse(SafeMarkup::isSafe($string_object)); } /** * Tests SafeMarkup::checkPlain(). * * @dataProvider providerCheckPlain * @covers ::checkPlain * @expectedDeprecation SafeMarkup::checkPlain() is scheduled for removal in Drupal 9.0.0. Rely on Twig's auto-escaping feature, or use the @link theme_render #plain_text @endlink key when constructing a render array that contains plain text in order to use the renderer's auto-escaping feature. If neither of these are possible, \Drupal\Component\Utility\Html::escape() can be used in places where explicit escaping is needed. See https://www.drupal.org/node/2549395. * * @param string $text * The text to provide to SafeMarkup::checkPlain(). * @param string $expected * The expected output from the function. * @param string $message * The message to provide as output for the test. */ public function testCheckPlain($text, $expected, $message) { $result = SafeMarkup::checkPlain($text); $this->assertTrue($result instanceof HtmlEscapedText); $this->assertEquals($expected, $result, $message); } /** * Tests Drupal\Component\Render\HtmlEscapedText. * * Verifies that the result of SafeMarkup::checkPlain() is the same as using * HtmlEscapedText directly. * * @dataProvider providerCheckPlain * * @param string $text * The text to provide to the HtmlEscapedText constructor. * @param string $expected * The expected output from the function. * @param string $message * The message to provide as output for the test. */ public function testHtmlEscapedText($text, $expected, $message) { $result = new HtmlEscapedText($text); $this->assertEquals($expected, $result, $message); } /** * Data provider for testCheckPlain() and testEscapeString(). * * @see testCheckPlain() */ public function providerCheckPlain() { // Checks that invalid multi-byte sequences are escaped. $tests[] = ["Foo\xC0barbaz", 'Foo�barbaz', 'Escapes invalid sequence "Foo\xC0barbaz"']; $tests[] = ["\xc2\"", '�"', 'Escapes invalid sequence "\xc2\""']; $tests[] = ["Fooÿñ", "Fooÿñ", 'Does not escape valid sequence "Fooÿñ"']; // Checks that special characters are escaped. $tests[] = [SafeMarkupTestMarkup::create(""], 'Hey hey', '', TRUE]; return $tests; } } class SafeMarkupTestString { protected $string; public function __construct($string) { $this->string = $string; } public function __toString() { return $this->string; } } /** * Marks an object's __toString() method as returning markup. */ class SafeMarkupTestMarkup implements MarkupInterface { use MarkupTrait; }