diff --git a/core/lib/Drupal/Component/Utility/PlaceholderTrait.php b/core/lib/Drupal/Component/Utility/PlaceholderTrait.php index ce49f8a..a95cd40 100644 --- a/core/lib/Drupal/Component/Utility/PlaceholderTrait.php +++ b/core/lib/Drupal/Component/Utility/PlaceholderTrait.php @@ -107,7 +107,7 @@ protected static function placeholderFormat($string, array $args, &$safe = TRUE) break; default: - throw new \InvalidArgumentException('Invalid placeholder'); + trigger_error('Invalid placeholder', E_USER_ERROR); break; } } diff --git a/core/tests/Drupal/Tests/Component/Utility/SafeMarkupTest.php b/core/tests/Drupal/Tests/Component/Utility/SafeMarkupTest.php index 9468fe7..6e3da90 100644 --- a/core/tests/Drupal/Tests/Component/Utility/SafeMarkupTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/SafeMarkupTest.php @@ -22,6 +22,20 @@ class SafeMarkupTest extends UnitTestCase { /** + * The error message of the last error in the error handler. + * + * @var string + */ + protected $lastErrorMessage; + + /** + * The error number of the last error in the error handler. + * + * @var int + */ + protected $lastErrorNumber; + + /** * {@inheritdoc} */ protected function tearDown() { @@ -192,15 +206,38 @@ function providerCheckPlain() { } /** + * Custom error handler that saves the last error. + * + * We need this custom error handler because we cannot rely on the error to + * exception conversion as __toString is never allowed to leak any kind of + * exception. + * + * @param int $error_number + * The error number. + * @param string $error_message + * The error message. + */ + public function errorHandler($error_number, $error_message) { + $this->lastErrorNumber = $error_number; + $this->lastErrorMessage = $error_message; + } + + /** * String formatting with SafeMarkup::format() and an unsupported placeholder. * * When you call SafeMarkup::format() with an unsupported placeholder, an * InvalidArgumentException should be thrown. - * - * @expectedException \InvalidArgumentException */ public function testUnexpectedFormat() { - SafeMarkup::format('Broken placeholder: ~placeholder', ['~placeholder' => 'broken'])->__toString(); + + // We set a custom error handler because of https://github.com/sebastianbergmann/phpunit/issues/487 + set_error_handler([$this, 'errorHandler']); + // We want this to trigger an error. + $error = SafeMarkup::format('Broken placeholder: ~placeholder', ['~placeholder' => 'broken'])->__toString(); + restore_error_handler(); + + $this->assertEquals(E_USER_ERROR, $this->lastErrorNumber); + $this->assertEquals('Invalid placeholder', $this->lastErrorMessage); } /**