FormattableMarkupTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Drupal\Tests\Component\Render;
  3. use Drupal\Component\Render\FormattableMarkup;
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * Tests the TranslatableMarkup class.
  7. *
  8. * @coversDefaultClass \Drupal\Component\Render\FormattableMarkup
  9. * @group utility
  10. */
  11. class FormattableMarkupTest extends TestCase {
  12. /**
  13. * The error message of the last error in the error handler.
  14. *
  15. * @var string
  16. */
  17. protected $lastErrorMessage;
  18. /**
  19. * The error number of the last error in the error handler.
  20. *
  21. * @var int
  22. */
  23. protected $lastErrorNumber;
  24. /**
  25. * @covers ::__toString
  26. * @covers ::jsonSerialize
  27. */
  28. public function testToString() {
  29. $string = 'Can I please have a @replacement';
  30. $formattable_string = new FormattableMarkup($string, ['@replacement' => 'kitten']);
  31. $text = (string) $formattable_string;
  32. $this->assertEquals('Can I please have a kitten', $text);
  33. $text = $formattable_string->jsonSerialize();
  34. $this->assertEquals('Can I please have a kitten', $text);
  35. }
  36. /**
  37. * @covers ::count
  38. */
  39. public function testCount() {
  40. $string = 'Can I please have a @replacement';
  41. $formattable_string = new FormattableMarkup($string, ['@replacement' => 'kitten']);
  42. $this->assertEquals(strlen($string), $formattable_string->count());
  43. }
  44. /**
  45. * Custom error handler that saves the last error.
  46. *
  47. * We need this custom error handler because we cannot rely on the error to
  48. * exception conversion as __toString is never allowed to leak any kind of
  49. * exception.
  50. *
  51. * @param int $error_number
  52. * The error number.
  53. * @param string $error_message
  54. * The error message.
  55. */
  56. public function errorHandler($error_number, $error_message) {
  57. $this->lastErrorNumber = $error_number;
  58. $this->lastErrorMessage = $error_message;
  59. }
  60. /**
  61. * @covers ::__toString
  62. * @dataProvider providerTestUnexpectedPlaceholder
  63. */
  64. public function testUnexpectedPlaceholder($string, $arguments, $error_number, $error_message) {
  65. // We set a custom error handler because of https://github.com/sebastianbergmann/phpunit/issues/487
  66. set_error_handler([$this, 'errorHandler']);
  67. // We want this to trigger an error.
  68. $markup = new FormattableMarkup($string, $arguments);
  69. // Cast it to a string which will generate the errors.
  70. $output = (string) $markup;
  71. restore_error_handler();
  72. // The string should not change.
  73. $this->assertEquals($string, $output);
  74. $this->assertEquals($error_number, $this->lastErrorNumber);
  75. $this->assertEquals($error_message, $this->lastErrorMessage);
  76. }
  77. /**
  78. * Data provider for FormattableMarkupTest::testUnexpectedPlaceholder().
  79. *
  80. * @return array
  81. */
  82. public function providerTestUnexpectedPlaceholder() {
  83. return [
  84. ['Non alpha starting character: ~placeholder', ['~placeholder' => 'replaced'], E_USER_ERROR, 'Invalid placeholder (~placeholder) in string: Non alpha starting character: ~placeholder'],
  85. ['Alpha starting character: placeholder', ['placeholder' => 'replaced'], E_USER_DEPRECATED, 'Invalid placeholder (placeholder) in string: Alpha starting character: placeholder'],
  86. // Ensure that where the placeholder is located in the the string is
  87. // irrelevant.
  88. ['placeholder', ['placeholder' => 'replaced'], E_USER_DEPRECATED, 'Invalid placeholder (placeholder) in string: placeholder'],
  89. ];
  90. }
  91. }