Chris@0: getMockBuilder('\Drupal\Component\Render\MarkupInterface')->getMock(); Chris@0: $this->assertTrue(SafeMarkup::isSafe($safe_string)); Chris@0: $string_object = new SafeMarkupTestString('test'); Chris@0: $this->assertFalse(SafeMarkup::isSafe($string_object)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests SafeMarkup::checkPlain(). Chris@0: * Chris@0: * @dataProvider providerCheckPlain Chris@0: * @covers ::checkPlain Chris@17: * @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. Chris@0: * Chris@0: * @param string $text Chris@0: * The text to provide to SafeMarkup::checkPlain(). Chris@0: * @param string $expected Chris@0: * The expected output from the function. Chris@0: * @param string $message Chris@0: * The message to provide as output for the test. Chris@0: */ Chris@0: public function testCheckPlain($text, $expected, $message) { Chris@0: $result = SafeMarkup::checkPlain($text); Chris@0: $this->assertTrue($result instanceof HtmlEscapedText); Chris@0: $this->assertEquals($expected, $result, $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests Drupal\Component\Render\HtmlEscapedText. Chris@0: * Chris@0: * Verifies that the result of SafeMarkup::checkPlain() is the same as using Chris@0: * HtmlEscapedText directly. Chris@0: * Chris@0: * @dataProvider providerCheckPlain Chris@0: * Chris@0: * @param string $text Chris@0: * The text to provide to the HtmlEscapedText constructor. Chris@0: * @param string $expected Chris@0: * The expected output from the function. Chris@0: * @param string $message Chris@0: * The message to provide as output for the test. Chris@0: */ Chris@0: public function testHtmlEscapedText($text, $expected, $message) { Chris@0: $result = new HtmlEscapedText($text); Chris@0: $this->assertEquals($expected, $result, $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Data provider for testCheckPlain() and testEscapeString(). Chris@0: * Chris@0: * @see testCheckPlain() Chris@0: */ Chris@0: public function providerCheckPlain() { Chris@0: // Checks that invalid multi-byte sequences are escaped. Chris@0: $tests[] = ["Foo\xC0barbaz", 'Foo�barbaz', 'Escapes invalid sequence "Foo\xC0barbaz"']; Chris@0: $tests[] = ["\xc2\"", '�"', 'Escapes invalid sequence "\xc2\""']; Chris@0: $tests[] = ["Fooÿñ", "Fooÿñ", 'Does not escape valid sequence "Fooÿñ"']; Chris@0: Chris@0: // Checks that special characters are escaped. Chris@0: $tests[] = [SafeMarkupTestMarkup::create(""], 'Hey hey', '', TRUE]; Chris@0: return $tests; Chris@0: } Chris@0: Chris@0: } Chris@0: Chris@0: class SafeMarkupTestString { Chris@0: Chris@0: protected $string; Chris@0: Chris@0: public function __construct($string) { Chris@0: $this->string = $string; Chris@0: } Chris@0: Chris@0: public function __toString() { Chris@0: return $this->string; Chris@0: } Chris@0: Chris@0: } Chris@0: Chris@0: /** Chris@0: * Marks an object's __toString() method as returning markup. Chris@0: */ Chris@0: class SafeMarkupTestMarkup implements MarkupInterface { Chris@0: use MarkupTrait; Chris@0: Chris@0: }