Loading
Sign in before continuing.
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:
- The channel was ignored.
channelwas returned asdblog_channel, but every entry was logged torestfullogger. - Message and path were escaped twice.
Html::escape()ran before the@message/@pathplaceholders escaped the values again, soa < bshowed asa &lt; b. - Severity failed on non-English sites. Severity was compared with the translated
RfcLogLevel::getLevels()labels, so on a translated site valid levels fell back tonotice.
Changes
Tests (first commit, expected to fail against the current code)
tests/src/Kernel/RestfulLoggerResourceTest.php(new). Calls the plugin directly and checks thewatchdogtable 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):testPostalso checks the stored dblog entry.testAccesschecks for a 403 for anonymous users, for each missing permission, and for a missing or invalid CSRF token.testValidationchecks for a 400 when the message is missing, the path is missing, or the channel is invalid.testHelpchecks the help page added in #3602529.- The leftover
_format=hal_jsonquery is removed.
Fixes (second commit)
- Channel: messages are logged to the requested channel through
logger.factory, and the default is stillrestfullogger. Channel names must match^[a-zA-Z0-9_-]{1,64}$(64 is thewatchdog.typecolumn length). Anything else gets a400 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
LogLevelconstants with a strict comparison, independent of the site language. - README: the allowed
channelandseverityvalues are documented, and therestfullogertypo is fixed.
API changes
- A
channelin 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_messagein the response is no longer double-escaped.RestfulLogger::__construct()takes a new trailingLoggerChannelFactoryInterface $logger_factoryargument. 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