Problem/Motivation
On #3072312: Review/fix/delete existing help topics, we came up with a few additional tests that would be useful for help topic syntax:
a) Make sure that all of the translated text is importable into POTX. The test for that can be seen in
https://git.drupalcode.org/project/drupal/blob/HEAD/core/modules/locale/...
// Skip this string unless it passes a check for dangerous code.
if (!locale_string_is_safe($translation)) {
So basically, we should test whether the translated text in the Twig file returns TRUE from locale_string_is_safe(). And there should be a test case that fails this validation.
b) Verify that the translated text contains valid HTML. And there should be a test case that fails this validation. I suggest a test case that looks something like this:
{% trans %}<a href="/foo">Text here{% endtrans %}</a>
That is, the open A tag is within %trans, while the close A tag is outside of the %trans.
We should also have a few more examples of bad syntax, such as an open A tag without a close A tag (seen in a recent patch and found through manual review/test).
Proposed resolution
Make a patch to HelpTopicsSyntaxTest that does this.
Remaining tasks
Patch including tests.
User interface changes
None, this just adds tests.
API changes
None.
Data model changes
None.
Release notes snippet
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #16 | 3090257-16.patch | 16.28 KB | jhodgdon |
| #14 | 3090257-14.patch | 16.41 KB | jhodgdon |
| #14 | interdiff-12-14.txt | 2.02 KB | jhodgdon |
| #12 | interdiff.txt | 3.05 KB | jhodgdon |
Comments
Comment #2
amber himes matzFixed typo in issue title.
Comment #3
andypostI think kind of it should be used
Comment #4
jhodgdonI don't think that is quite right:
The line above the 2 lines you added removes all the translated text. So... it is opposite of what we want to test -- you are checking that the text that is *outside* the translated text is safe. Oops.
This is why we need a new test case to test the test. :)
Comment #5
jhodgdonAdding some more examples to issue summary of bad HTML syntax.
Comment #8
jhodgdonHere's a new patch for this, which I expect to fail... I need to upload it here and then fire up my dev machine so I can work on it and test as I go.
Comment #9
jhodgdonHere's a patch that passes locally. What it does:
a) Tests that the translated strings work in locale_string_is_safe().
b) Fixes the HTML syntax check so that it is more rigorous:
- Uses loadXML instead of loadHTML so that it validates the XML syntax (not against a DTD but at least that open/close tags match)
- Removes the translated strings and validates again so that it verifies we don't have situations like an opening tag outside and closing tag inside a single translated string
c) Adds some additional test cases to validate that the tests are able to catch invalid HTML
d) Adds a line to the test of the bad topics so it makes sure an exception/fail was actually encountered.
e) Hm.... just realized I could also validate that each translated string is separately valid HTML. So, there are 2 interdiffs here for the above and for this change.
f) [edit: added this] The additional tests in this patch found HTML syntax problems in 5 existing topics (fixes for those are included in the patch, because the tests failed locally without those fixes).
Comment #10
jhodgdonThe test result showed a coding standards error in this one line:
Needs spaces before/after the |
Here's a new patch that does this. I edited the patch file directly so no interdiff, hope that is OK.
Comment #11
andypostI think it's ok to rtbc as nothing to add
Comment #12
jhodgdonWell, since the time I made the previous patch and now, some new topics were committed that these tests found errors in. Here's a new patch and interdiff. I am not on my dev machine so I'm not sure the tests will pass, but I did fix two HTML validation problems in the migrate.overview topic.... let's see what the bot says. And hopefully get this back to RTBC and committed before we commit any more topics with errors!
Comment #14
jhodgdonStray DL tag.
Comment #16
jhodgdonTypo in that last patch. This line wasn't supposed to be changed:
Sheesh. Trying once more...
Comment #17
andypostThank you
Comment #18
alexpottCommitted 3434dee and pushed to 9.2.x. Thanks!
This looks really useful.
Comment #20
jhodgdonTHANK YOU for committing this! It will indeed be very useful for preventing problems, as you can see by all the HTML syntax errors it caught that I had to put in the patch so the tests would pass. :)
Comment #21
penyaskitoShould this be backported to 9.1.x? There are some languages where there are translations already, and some users confused by this (as per #3190153: Malformed strings break translation imports)
Comment #22
jhodgdonYes, it should be backported to 9.1, along with
#3047711: Convert file, image, media, media_library, responsive_image module hook_help() to topic(s)
#3073476: Convert migrate, migrate_drupal, migrate_drupal_multilingual, migrate_drupal_ui module hook_help() to topic(s)
The policy is apparently that patches on experimental modules get backported. I have a Slack thread about those other two issues; I'll add this to the thread.