Commit acad6dac authored by catch's avatar catch
Browse files

Issue #3255637 by Liam Morland, 3li, joegraduate, rivimey, alexpott, mondrake,...

Issue #3255637 by Liam Morland, 3li, joegraduate, rivimey, alexpott, mondrake, ravi.shankar, hswong3i, ameymudras, longwave, Anchal_gupta, neclimdul, Martijn de Wit, sgourebi, pradhumanjainOSL, gambry, kim.pepper: Deprecate NULL values in Html::escape(), ::decodeEntities(), and FormattableMarkup::placeholderFormat() to make it easier to upgrade to PHP 8

(cherry picked from commit 7430cc3c)
parent 3d4a9c12
Loading
Loading
Loading
Loading
+13 −5
Original line number Diff line number Diff line
@@ -131,11 +131,11 @@ public function jsonSerialize() {
   * @param array $args
   *   An associative array of replacements. Each array key should be the same
   *   as a placeholder in $string. The corresponding value should be a string
   *   or an object that implements
   *   \Drupal\Component\Render\MarkupInterface. The value replaces the
   *   placeholder in $string. Sanitization and formatting will be done before
   *   replacement. The type of sanitization and formatting depends on the first
   *   character of the key:
   *   or an object that implements \Drupal\Component\Render\MarkupInterface.
   *   Null args[] values are deprecated in Drupal 9.5 and will fail in
   *   Drupal 11.0. The value replaces the placeholder in $string. Sanitization
   *   and formatting will be done before replacement. The type of sanitization
   *   and formatting depends on the first character of the key:
   *   - @variable: When the placeholder replacement value is:
   *     - A string, the replaced value in the returned string will be sanitized
   *       using \Drupal\Component\Utility\Html::escape().
@@ -196,6 +196,14 @@ public function jsonSerialize() {
  protected static function placeholderFormat($string, array $args) {
    // Transform arguments before inserting them.
    foreach ($args as $key => $value) {
      if (is_null($value)) {
        // It's probably a bug to provide a null value for the placeholder arg,
        // and in D11 this will no longer be allowed. When this trigger_error
        // is removed, also remove isset $value checks inside the switch{}
        // below.
        @trigger_error(sprintf('Deprecated NULL placeholder value for key (%s) in: "%s". This will throw a PHP error in drupal:11.0.0. See https://www.drupal.org/node/3318826', (string) $key, (string) $string), E_USER_DEPRECATED);
        $value = '';
      }
      switch ($key[0]) {
        case '@':
          // Escape if the value is not an object from a class that implements
+10 −2
Original line number Diff line number Diff line
@@ -382,7 +382,11 @@ public static function escapeCdataElement(\DOMNode $node, $comment_start = '//',
   * @see html_entity_decode()
   * @see \Drupal\Component\Utility\Html::escape()
   */
  public static function decodeEntities($text) {
  public static function decodeEntities($text): string {
    if (is_null($text)) {
      @trigger_error('Passing NULL to ' . __METHOD__ . ' is deprecated in drupal:9.5.0 and will trigger a PHP error from drupal:11.0.0. Pass a string instead. See https://www.drupal.org/node/3318826', E_USER_DEPRECATED);
      return '';
    }
    return html_entity_decode($text, ENT_QUOTES, 'UTF-8');
  }

@@ -420,7 +424,11 @@ public static function decodeEntities($text) {
   *
   * @ingroup sanitization
   */
  public static function escape($text) {
  public static function escape($text): string {
    if (is_null($text)) {
      @trigger_error('Passing NULL to ' . __METHOD__ . ' is deprecated in drupal:9.5.0 and will trigger a PHP error from drupal:11.0.0. Pass a string instead. See https://www.drupal.org/node/3318826', E_USER_DEPRECATED);
      return '';
    }
    return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  }

+23 −0
Original line number Diff line number Diff line
@@ -52,6 +52,29 @@ public function testCount() {
    $this->assertEquals(strlen($string), $formattable_string->count());
  }

  /**
   * @covers ::__toString
   * @dataProvider providerTestNullPlaceholder
   * @group legacy
   */
  public function testNullPlaceholder(string $expected, string $string, array $arguments, string $expected_deprecation): void {
    $this->expectDeprecation($expected_deprecation);
    $this->assertEquals($expected, (string) new FormattableMarkup($string, $arguments));
  }

  /**
   * Data provider for FormattableMarkupTest::testNullPlaceholder().
   *
   * @return array
   */
  public function providerTestNullPlaceholder() {
    return [
      ['', '@empty', ['@empty' => NULL], 'Deprecated NULL placeholder value for key (@empty) in: "@empty". This will throw a PHP error in drupal:11.0.0. See https://www.drupal.org/node/3318826'],
      ['', ':empty', [':empty' => NULL], 'Deprecated NULL placeholder value for key (:empty) in: ":empty". This will throw a PHP error in drupal:11.0.0. See https://www.drupal.org/node/3318826'],
      ['<em class="placeholder"></em>', '%empty', ['%empty' => NULL], 'Deprecated NULL placeholder value for key (%%empty) in: "%%empty". This will throw a PHP error in drupal:11.0.0. See https://www.drupal.org/node/3318826'],
    ];
  }

  /**
   * Custom error handler that saves the last error.
   *
+16 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Random;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;

/**
 * Tests \Drupal\Component\Utility\Html.
@@ -17,6 +18,8 @@
 */
class HtmlTest extends TestCase {

  use ExpectDeprecationTrait;

  /**
   * {@inheritdoc}
   */
@@ -405,6 +408,19 @@ public function providerTestTransformRootRelativeUrlsToAbsoluteAssertion() {
    ];
  }

  /**
   * Test deprecations.
   *
   * @group legacy
   */
  public function testDeprecations(): void {
    $this->expectDeprecation('Passing NULL to Drupal\Component\Utility\Html::decodeEntities is deprecated in drupal:9.5.0 and will trigger a PHP error from drupal:11.0.0. Pass a string instead. See https://www.drupal.org/node/3318826');
    $this->assertSame('', Html::decodeEntities(NULL));

    $this->expectDeprecation('Passing NULL to Drupal\Component\Utility\Html::escape is deprecated in drupal:9.5.0 and will trigger a PHP error from drupal:11.0.0. Pass a string instead. See https://www.drupal.org/node/3318826');
    $this->assertSame('', Html::escape(NULL));
  }

}

/**