- Issue created by @larowlan
- Assigned to royalpinto007
- Status changed to Needs work
almost 2 years ago 9:10am 7 February 2023 - @royalpinto007 opened merge request.
- Status changed to Needs review
almost 2 years ago 12:43pm 7 February 2023 - ๐ฎ๐ณIndia royalpinto007
I just wanted to inform you that I have removed the use of FormattableMarkup in log messages and have instead passed the message as the first parameter and the parameters as the second. This change ensures a more consistent and efficient logging system. Let me know if there are any questions or concerns.
- ๐ซ๐ทFrance andypost
probably it needs some test or runtime assertion to catch it
- First commit to issue fork.
- Issue was unassigned.
- ๐ช๐ธSpain gxleano Cรกceres
Hi @royalpinto007!
I've added the string messages with parameters into
$this->t()
function. It seems like if we use it directly into the loggers, it will break the tests. - Status changed to Needs work
almost 2 years ago 7:00pm 7 February 2023 - ๐ฌ๐งUnited Kingdom longwave UK
The logger methods should take an untranslated string as the first argument and the context array as the second argument; the untranslated string and context is stored, and translation is handled at display time, so the logs can be viewed in different languages.
- ๐ช๐ธSpain gxleano Cรกceres
Thanks @longwave for the feedback!
It seems like this approach is currently used in CKEditor5.
The only
logger
method that is not being passed theFormattableMarkup
, is using it.See: https://git.drupalcode.org/project/drupal/-/blob/9.5.0/core/modules/cked...
- ๐ช๐ธSpain gxleano Cรกceres
To give more context on this, I was debugging the
test()
inSmartDefaultSettingsTest
class, and when we use thelogger
method passing the string as the first argument and the context as second, the context arguments are not being transformed into the string when they are being called in the tests.This makes me think that by not using the
FormattableMarkup
, the messages are being stored in thewatchdog
table as untransformed strings. But I don't really understand why... - ๐ช๐ธSpain fjgarlin
You are right @gxleano, the messages are stored as they are, without the replacements applied. See the "log" method here: https://api.drupal.org/api/drupal/core%21modules%21dblog%21src%21Logger%...
I think, in this case, the test might need to be updated too as part of this issue, as it assumes that the stored value in DB has the replacements, and that's not correct. The test checks the array entries "expected_db_logs" with the context already replaced.
So, the way I see it, the initial fix from @royalpinto007 was valid, but it also needed to have the tests updated. Maybe you want to give it a go @gxleano? Note that the $this->t commit will need to be reverted.
- ๐ช๐ธSpain gxleano Cรกceres
Thanks @fjgarlin for the feedback!
Sure, I will try to rework the "expected_db_logs" and revert
$this->t()
changes. - ๐ง๐ชBelgium wim leers Ghent ๐ง๐ช๐ช๐บ
Woah, great catch! Looks like this is getting close ๐
- Status changed to Needs review
almost 2 years ago 8:43pm 8 February 2023 - ๐ช๐ธSpain gxleano Cรกceres
I've updated the kernel test in order to do it compatible with the
logger
context. - Status changed to Needs work
almost 2 years ago 9:05am 9 February 2023 - ๐ฌ๐งUnited Kingdom longwave UK
I think the test cases should stay the same, otherwise we are losing coverage of the contextual values; the test should format the message and then check that the final string is as expected.
- ๐ช๐ธSpain fjgarlin
the test should format the message and then check that the final string is as expected.
@longwave - in this part of the test https://git.drupalcode.org/project/drupal/-/blob/8aaf79d53b2251e8bd3d9b2... we are storing the "message" column as it is in the database, without replacements.
Wouldn't it make sense to compare the same thing (aka without the replacements applied) in the tests?
Otherwise, I guess we'd need to apply the replacements in those lines (getting the variables), and then leave the strings in the test unchanged. Is that the approach that you suggest?
- ๐ฌ๐งUnited Kingdom longwave UK
Yes, I mean to apply the replacements - the select statement already reads the
variables
column, but then does nothing with it. - First commit to issue fork.
- ๐ช๐ธSpain gxleano Cรกceres
@longwave
I've tried to apply the replacements with the
variables
column, but there is a problem. The values coming from thevariables
column are different than the provided data in the test.For instance:
- value coming from variables: Basic HTML
- expected value: Basic HTML
So, the test are trying to compare the next:
--- Expected
The CKEditor 5 migration enabled the following plugins to support tags that are allowed by the Basic HTML text format: Code (for tags: <code>). The text format must be saved to make these changes active.+++ Actual (With replacements applied)
The CKEditor 5 migration enabled the following plugins to support tags that are allowed by the Basic HTML text format: Code (for tags:). The text format must be saved to make these changes active.
- ๐ช๐ธSpain fjgarlin
Which code are you using to do the replacements?
- ๐ช๐ธSpain gxleano Cรกceres
$db_logged = $this ->database ->select('watchdog', 'w') ->fields('w', ['message', 'variables', 'severity']) ->condition('type', 'ckeditor5') ->orderBy('wid') ->execute() ->fetchAll(); $type_to_status = [ 6 => 'status', 4 => 'warning', ]; $db_logs = []; foreach ($db_logged as $log) { $message = $log->message; $variables = unserialize($log->variables); foreach ($variables as $key => $value) { if (str_contains($message, $key)) { $message = str_replace($key, $value, $message); } } $db_logs[$type_to_status[$log->severity]][] = $message; }
- ๐ช๐ธSpain gxleano Cรกceres
But on my eyes, it seems to be normal, because of it is storing the format name, without html.
'%text_format' => $editor->getFilterFormat()->get('name'),
- ๐ช๐ธSpain fjgarlin
Try something like this better (not tested):
$variables = unserialize($log->variables); $message = new FormattableMarkup($log->message, $variables);
You'll need to add
use Drupal\Component\Render\FormattableMarkup;
to the top of the test file.% or @ have different meanings when replacing, and the class should take care of it, instead of doing a "str_replace".
- ๐ช๐ธSpain gxleano Cรกceres
Thanks @fjgarlin for the hint.
I've refactored this piece of code and test are passing now.
$variables = unserialize($log->variables); $message = new FormattableMarkup($log->message, $variables); $db_logs[$type_to_status[$log->severity]][] = (string) $message;
- ๐ช๐ธSpain fjgarlin
Great to hear that! Things are looking better and better.
You might need to change your editor autosaving/autoformatting to _not_ change the other parts of the code that are not affected / relevant that were triggering the coding standards. Ideally, the MR should only reflect the changes needed to fix the issue.
Is the
(string)
casting needed for the tests to pass? - ๐ช๐ธSpain gxleano Cรกceres
I have reverted the unnecessary changes.
And yes, the casting
(string)
is necessary to make the test pass. - ๐ช๐ธSpain fjgarlin
Awesome, tests are running as we type and the MR looks much cleaner now. Great work!
- Status changed to Needs review
almost 2 years ago 9:58pm 9 February 2023 - Status changed to RTBC
almost 2 years ago 9:27am 10 February 2023 - ๐ช๐ธSpain fjgarlin
I checked the code and it all looks good, and also tested everything manually via Drupalpod as follows:
- I uninstalled ckeditor5, enabled ckeditor.
- Then installed again ckeditor5 and assigned it to a text format (to trigger all the logging messages).
- Went to the log messages section and the messages showed up as expected, with the replacements properly parsed.See the two images below:
Marking this as RTBC.
-
larowlan โ
committed 9fbbf6e5 on 10.1.x
Issue #3339400 by gxleano, royalpinto007, VladimirAus, fjgarlin,...
-
larowlan โ
committed 9fbbf6e5 on 10.1.x
-
larowlan โ
committed eda47b52 on 10.0.x
Issue #3339400 by gxleano, royalpinto007, VladimirAus, fjgarlin,...
-
larowlan โ
committed eda47b52 on 10.0.x
-
larowlan โ
committed f065974b on 9.5.x
Issue #3339400 by gxleano, royalpinto007, VladimirAus, fjgarlin,...
-
larowlan โ
committed f065974b on 9.5.x
- Status changed to Fixed
almost 2 years ago 10:15pm 10 February 2023 - ๐ฆ๐บAustralia larowlan ๐ฆ๐บ๐.au GMT+10
Thanks folks, committed to 10.1.x and backported to 10.0.x and 9.5.x for consistency as the risk of disruption is low.
Automatically closed - issue fixed for 2 weeks with no activity.
- Status changed to Fixed
over 1 year ago 10:58am 11 April 2023 - ๐บ๐ธUnited States bnjmnm Ann Arbor, MI
Adjusting issue credit as credit is not provided for button-click rebases without additional contribution.