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.

Comments

jhodgdon created an issue. See original summary.

amber himes matz’s picture

Title: Add additional tests for help topic sysntax » Add additional tests for help topic syntax

Fixed typo in issue title.

andypost’s picture

Status: Active » Needs review
StatusFileSize
new1.13 KB

I think kind of it should be used

jhodgdon’s picture

Status: Needs review » Needs work

I don't think that is quite right:

     // Verify that if we remove all the translated text, whitespace, and
     // HTML tags, there is nothing left (that is, all text is translated).
     $text = preg_replace('|\{\% trans \%\}.*\{\% endtrans \%\}|sU', '', $body);
+    $result = locale_string_is_safe($text);
+    $this->assertTrue($result, 'Topic ' . $id . 'Twig file has all translatable strings exportable');

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. :)

jhodgdon’s picture

Issue summary: View changes

Adding some more examples to issue summary of bad HTML syntax.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

jhodgdon’s picture

StatusFileSize
new2.88 KB

Here'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.

jhodgdon’s picture

Status: Needs work » Needs review
StatusFileSize
new12.15 KB
new1.24 KB
new13.17 KB

Here'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).

jhodgdon’s picture

StatusFileSize
new13.17 KB

The test result showed a coding standards error in this one line:

catch (ExpectationFailedException|AssertionFailedError $e) {

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.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

I think it's ok to rtbc as nothing to add

jhodgdon’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new3.05 KB
new16.17 KB

Well, 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!

Status: Needs review » Needs work

The last submitted patch, 12: 3090257-12.patch, failed testing. View results

jhodgdon’s picture

Status: Needs work » Needs review
StatusFileSize
new2.02 KB
new16.41 KB

Stray DL tag.

Status: Needs review » Needs work

The last submitted patch, 14: 3090257-14.patch, failed testing. View results

jhodgdon’s picture

Status: Needs work » Needs review
StatusFileSize
new16.28 KB

Typo in that last patch. This line wasn't supposed to be changed:

----
+--

Sheesh. Trying once more...

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Thank you

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 3434dee and pushed to 9.2.x. Thanks!

This looks really useful.

  • alexpott committed 3434dee on 9.2.x
    Issue #3090257 by jhodgdon, andypost: Add additional tests for help...
jhodgdon’s picture

THANK 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. :)

penyaskito’s picture

Should 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)

jhodgdon’s picture

Yes, 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.

Status: Fixed » Closed (fixed)

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