Resolve #3633954 "Fix channel, escaping and severity handling, and expand test coverage"

Closes #3633954

Problem

LoadTest::testPost() only checked the JSON response, which is built from the request data, so it never verified that anything was logged. That gap hid three bugs in RestfulLogger:

  1. The channel was ignored. channel was returned as dblog_channel, but every entry was logged to restfullogger.
  2. Message and path were escaped twice. Html::escape() ran before the @message/@path placeholders escaped the values again, so a < b showed as a &amp;lt; b.
  3. Severity failed on non-English sites. Severity was compared with the translated RfcLogLevel::getLevels() labels, so on a translated site valid levels fell back to notice.

Changes

Tests (first commit, expected to fail against the current code)

  • tests/src/Kernel/RestfulLoggerResourceTest.php (new). Calls the plugin directly and checks the watchdog table for:
    • the default channel, a custom channel, and rejected channel names (space, markup, over 64 characters, non-string);
    • all 8 PSR-3 levels, upper and mixed case, and missing, invalid and numeric severities;
    • severity on a site where the "Error" label is translated;
    • escaping: raw values are stored, and the displayed message and the response are escaped exactly once.
  • tests/src/Functional/LoadTest.php (expanded):
    • testPost also checks the stored dblog entry.
    • testAccess checks for a 403 for anonymous users, for each missing permission, and for a missing or invalid CSRF token.
    • testValidation checks for a 400 when the message is missing, the path is missing, or the channel is invalid.
    • testHelp checks the help page added in #3602529.
    • The leftover _format=hal_json query is removed.

Fixes (second commit)

  • Channel: messages are logged to the requested channel through logger.factory, and the default is still restfullogger. Channel names must match ^[a-zA-Z0-9_-]{1,64}$ (64 is the watchdog.type column length). Anything else gets a 400 The channel value is invalid.
  • Escaping: the Html::escape() calls are removed, so the values are escaped once, by the placeholders, when displayed.
  • Severity: severity is validated against the PSR-3 LogLevel constants with a strict comparison, independent of the site language.
  • README: the allowed channel and severity values are documented, and the restfulloger typo is fixed.

API changes

  • A channel in the request now changes the channel the entry is logged to. Requests with an invalid channel name get a 400 instead of a 200.
  • dblog_message in the response is no longer double-escaped.
  • RestfulLogger::__construct() takes a new trailing LoggerChannelFactoryInterface $logger_factory argument. This only affects code that subclasses the plugin.

Testing

The pipeline runs on Drupal 10, 11 and 12 (previous and next major are opted in), and the new tests run on all three.

Edited by Ryan McVeigh

Merge request reports

Loading
Loading