3 namespace Drupal\Tests\Component\Render;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Tests\UnitTestCase;
9 * Tests the TranslatableMarkup class.
11 * @coversDefaultClass \Drupal\Component\Render\FormattableMarkup
14 class FormattableMarkupTest extends UnitTestCase {
17 * The error message of the last error in the error handler.
21 protected $lastErrorMessage;
24 * The error number of the last error in the error handler.
28 protected $lastErrorNumber;
31 * @covers ::__toString
32 * @covers ::jsonSerialize
34 public function testToString() {
35 $string = 'Can I please have a @replacement';
36 $formattable_string = new FormattableMarkup($string, ['@replacement' => 'kitten']);
37 $text = (string) $formattable_string;
38 $this->assertEquals('Can I please have a kitten', $text);
39 $text = $formattable_string->jsonSerialize();
40 $this->assertEquals('Can I please have a kitten', $text);
46 public function testCount() {
47 $string = 'Can I please have a @replacement';
48 $formattable_string = new FormattableMarkup($string, ['@replacement' => 'kitten']);
49 $this->assertEquals(strlen($string), $formattable_string->count());
53 * Custom error handler that saves the last error.
55 * We need this custom error handler because we cannot rely on the error to
56 * exception conversion as __toString is never allowed to leak any kind of
59 * @param int $error_number
61 * @param string $error_message
64 public function errorHandler($error_number, $error_message) {
65 $this->lastErrorNumber = $error_number;
66 $this->lastErrorMessage = $error_message;
70 * @covers ::__toString
71 * @dataProvider providerTestUnexpectedPlaceholder
73 public function testUnexpectedPlaceholder($string, $arguments, $error_number, $error_message) {
74 // We set a custom error handler because of https://github.com/sebastianbergmann/phpunit/issues/487
75 set_error_handler([$this, 'errorHandler']);
76 // We want this to trigger an error.
77 $markup = new FormattableMarkup($string, $arguments);
78 // Cast it to a string which will generate the errors.
79 $output = (string) $markup;
80 restore_error_handler();
81 // The string should not change.
82 $this->assertEquals($string, $output);
83 $this->assertEquals($error_number, $this->lastErrorNumber);
84 $this->assertEquals($error_message, $this->lastErrorMessage);
88 * Data provider for FormattableMarkupTest::testUnexpectedPlaceholder().
92 public function providerTestUnexpectedPlaceholder() {
94 ['Non alpha starting character: ~placeholder', ['~placeholder' => 'replaced'], E_USER_ERROR, 'Invalid placeholder (~placeholder) in string: Non alpha starting character: ~placeholder'],
95 ['Alpha starting character: placeholder', ['placeholder' => 'replaced'], E_USER_DEPRECATED, 'Invalid placeholder (placeholder) in string: Alpha starting character: placeholder'],
96 // Ensure that where the placeholder is located in the the string is
98 ['placeholder', ['placeholder' => 'replaced'], E_USER_DEPRECATED, 'Invalid placeholder (placeholder) in string: placeholder'],