Problem/Motivation

When switching to PHP 8.1 this notice when visiting admin/reports/monitoring this notice appears:

"Deprecated function: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\Component\Utility\Html::escape() (line 424 of /var/www/html/web/core/lib/Drupal/Component/Utility/Hp)"

Steps to reproduce

Visit admin/reports/monitoring within a PHP 8.1 environment.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork monitoring-3277649

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Primsi created an issue. See original summary.

primsi’s picture

Status: Active » Needs review
StatusFileSize
new870 bytes

This patch gets around this problem, but I am not sure if we should try to approach this in a different way.

Status: Needs review » Needs work

The last submitted patch, 2: monitoring-htmlspecialchars-deprecation-3277649-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

berdir’s picture

Fine with that. Instead of ??, we can also just use a (string) cast, possibly only on the placeholder array.

test fail is likely not related.

berdir’s picture

I noticed in #3277425: PHP 8.1 compatiblity in metatag integration that PHP 8.1 deprecation messages are suppressed, at least in kernel tests.

Running kernel test with that enabled gives me this:

  113x: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated
    23x in MonitoringCoreKernelTest::testCronRunAllEnabledSensors from Drupal\Tests\monitoring\Kernel
    18x in MonitoringCoreKernelTest::testCoreRequirementsSensorPlugin from Drupal\Tests\monitoring\Kernel
    16x in MonitoringCoreKernelTest::testGitDirtyTreeSensorPlugin from Drupal\Tests\monitoring\Kernel
    10x in MonitoringCoreKernelTest::testDatabaseAggregator from Drupal\Tests\monitoring\Kernel
    9x in MonitoringRedirect404SensorTest::testRedirect404Sensor from Drupal\Tests\monitoring\Kernel
    8x in MonitoringCoreKernelTest::testDefaultNodeTypeSensors from Drupal\Tests\monitoring\Kernel
    6x in MonitoringCoreKernelTest::testDblog404SensorPlugin from Drupal\Tests\monitoring\Kernel
    6x in MonitoringResponseTimeSensorTest::testResponseTimeSensorPlugin from Drupal\Tests\monitoring\Kernel
    3x in MonitoringCoreKernelTest::testImageMissingStyleSensorPlugin from Drupal\Tests\monitoring\Kernel
    3x in MonitoringCoreKernelTest::testUserFailedLoginsSensorPlugin from Drupal\Tests\monitoring\Kernel
    3x in MonitoringDiskUsageSensorTest::testDiskUsageSensorPlugin from Drupal\Tests\monitoring\Kernel
    3x in MonitoringSystemLoadSensorTest::testSystemLoadSensorPlugin from Drupal\Tests\monitoring\Kernel
    2x in MonitoringApiTest::testLogging from Drupal\Tests\monitoring\Kernel
    1x in MonitoringApiTest::testAPI from Drupal\Tests\monitoring\Kernel
    1x in MonitoringCoreKernelTest::testDatabaseAggregatorSensorPluginDblog from Drupal\Tests\monitoring\Kernel
    1x in MonitoringCoreKernelTest::testDatabaseAggregatorSensorPluginUserLogout from Drupal\Tests\monitoring\Kernel

  3x: trim(): Passing null to parameter #1 ($string) of type string is deprecated
    3x in MonitoringSystemMemoryPluginTest::testSystemMemorySensorPlugin from Drupal\Tests\monitoring\Kernel

  2x: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated
    2x in MonitoringCoreKernelTest::testCronRunAllEnabledSensors from Drupal\Tests\monitoring\Kernel

  1x: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated
    1x in MonitoringCoreKernelTest::testImageMissingStyleSensorPlugin from Drupal\Tests\monitoring\Kernel

The patch fixes some of these but not all.

berdir’s picture

The test fails can be fixed by adding "$this->assertSession()->elementExists('css', '#result')->click();", it looks like the details element needs to be opened first as it only sees visible text, must have changed at some point.

fine to just add them in here IMHO.

primsi’s picture

Fixed most of the deprecation notices, except those in Drupal\Tests\monitoring\Kernel\MonitoringCoreKernelTest::testImageMissingStyleSensorPlugin which we talked about in chat.

primsi’s picture

Status: Needs work » Needs review

Checking if the functional test passes.

Status: Needs review » Needs work

The last submitted patch, 7: monitoring-htmlspecialchars-deprecation-3277649-7.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

berdir’s picture

Opened #3279289: file_requirements() can trigger a PHP 8.1 deprecation notice when called without a SERVER_SOFTWARE server env variable for the file_requirements() issue.

Something I recently found is that you can define the deprecation configuration as a regular expression:

<env name="SYMFONY_DEPRECATIONS_HELPER" value="/.*strpos.*/"/>

Then, for kernel tests, combine that with protected $runTestInSeparateProcess = FALSE

This will immediately stop the test on a deprecation notice and print a full backtrace.

But nothing gave me anything useful in this specific case. Then I realized that we're inside a twig render process and the rendering captured output and put it inside the rendered content. And in the case above, it just eat it and then died.

So I printed out the rendered output, which gave me this:

  string(56) "Records in dblog limited to :limit records. :parent_info"
array(2) {
  [":limit"]=>

This I tracked down to \Drupal\monitoring\Plugin\monitoring\SensorPlugin\WatchdogAggregatorSensorPlugin::verboseResultHistory

So the limit that it's trying to print is a config that's not imported in the test. Importing dblog config fixes it.

But there's also a real bug here and that's using :placeholder for something that''s not actually an attribute, should be @ for both. Fixed, also fixed the other case in the JS test that needs this and improved some sleeps().

cilefen’s picture

Title: Deprication notice for htmlspecialchars with php 8.1 » Deprecation notice for htmlspecialchars with php 8.1

maxilein made their first commit to this issue’s fork.

  • Berdir committed 302ad11 on 8.x-1.x
    Issue #3277649 by Primsi, Berdir, maxilein: Deprecation notice for...
berdir’s picture

Status: Needs review » Fixed

Committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.