Follow-up to #2527360: Review all usages of Xss::filter(), Xss::filterAdmin(), and Html::escape()

We're using SafeMarkup::format in tests. We should not as this pollutes the safe string list and is irrelevant.

Where we are using it in $this->assertRaw() we should just convert it to a string. Where we are using it in an assertion message we should just convert it to a string with the variables inserted like "This is a $lol test".

CommentFileSizeAuthor
#78 interdiff_70-78.txt4.74 KBmohrerao
#78 2549805-78.patch26.63 KBmohrerao
#70 2549805-70.patch26.52 KBshalinigaur
#68 2549805-68.patch123.97 KBmrinalini9
#66 2549805-66.patch123.98 KBmrinalini9
#55 2549805-55-D8.patch41.03 KBmohit1604
#55 interdiff.txt8.88 KBmohit1604
#51 2549805_51.patch39.67 KBMile23
#43 2549805_38.patch34.56 KBMile23
#38 2549805_38.patch34.56 KBMile23
#35 2527360-35.patch34.36 KBlokapujya
#35 2527360-35-interdiff.txt3.71 KBlokapujya
#31 2549805-31-interdiff.txt15.7 KBlokapujya
#31 2549805-31.patch34.35 KBlokapujya
#30 2549805-30.patch19.12 KBlokapujya
#30 2549805-30-interdiff.txt553 byteslokapujya
#28 2549805-28-interdiff.txt112.13 KBlokapujya
#28 2549805-28.patch19.38 KBlokapujya
#20 interdiff-2549805-20.txt3.56 KBlokapujya
#20 2549805-20.patch134.31 KBlokapujya
#18 interdiff-2549805-18.txt15.23 KBlokapujya
#18 2549805-18.patch134.29 KBlokapujya
#12 2549805-12.patch125.85 KBlokapujya
#7 2549805-7.patch131.8 KBgeertvd
#7 interdiff-2549805-5-7.txt4.85 KBgeertvd
#5 2549805-5.patch130.53 KBgeertvd
#4 2549805.4.patch1.76 KBalexpott
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alexpott created an issue. See original summary.

xjm’s picture

Title: Remove all usage of SafeMarkup::format and format_string in tests apart from explicit tests of those methods » Remove all usage of SafeMarkup::format() and format_string() in tests apart from explicit tests of those methods
alexpott’s picture

      $text = SafeMarkup::format('<b>‹</b> @label', array('@label' => $previous->label()));
      $this->assertRaw(\Drupal::l($text, $url), 'Previous page link found.');

So how would be convert this? From BookTest.php

alexpott’s picture

Status: Active » Needs review
FileSize
1.76 KB

A couple of example conversions. Placeholders are going to be annoying.

geertvd’s picture

Just posting my progress, curious if I missed any placeholders.
This is still unfinished.

Status: Needs review » Needs work

The last submitted patch, 5: 2549805-5.patch, failed testing.

geertvd’s picture

Status: Needs work » Needs review
FileSize
4.85 KB
131.8 KB

This should fix the test fails for now.
Still a work in progress, anyone feel free to work on this, I'll assign myself when i'm doing so.

geertvd’s picture

So how would be convert this? From BookTest.php

+++ b/core/modules/book/src/Tests/BookTest.php
@@ -266,8 +265,9 @@ function checkBookNode(EntityInterface $node, $nodes, $previous = FALSE, $up = F
-      $text = "<b>‹</b> {$previous->label()}";
-      $this->assertRaw(\Drupal::l($text, $url), 'Previous page link found.');
+      $unrendered_text = ['#markup' => '<b>‹</b> ' . $previous->label()];
+      $rendered_text = \Drupal::service('renderer')->renderRoot($unrendered_text);
+      $this->assertRaw(\Drupal::l($rendered_text, $url), 'Previous page link found.');

@alexpott, i fixed that like this, does that seem acceptable?

lauriii queued 7: 2549805-7.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 7: 2549805-7.patch, failed testing.

joelpittet’s picture

Issue tags: +Needs reroll

Looks like it needs a re-roll.

lokapujya’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
125.85 KB

Rerolled.

lokapujya’s picture

@@ -166,21 +166,21 @@ protected function doTestMultilingualProperties($entity_type) {
+    $this->assertEqual($default_langcode, $field->getLangcode(), "$entity_type: The field object has the expect langcode.");

s/expect/expected/g

Tempted to fix this here. Is it allowed or out of scope?

Status: Needs review » Needs work

The last submitted patch, 12: 2549805-12.patch, failed testing.

joelpittet’s picture

In scope imo. It's in a changed hunk so won't effect other patches more than what we are changing.

geertvd’s picture

Just pointing out that there still are a lot of occurrences of SafeMarkup::format() and format_string() that need replacing. My patch in #7 was just a work in progress.

joelpittet’s picture

Issue tags: +rc eligible

Since this is improving only tests. this can be done during RC. https://www.drupal.org/core/d8-allowed-changes#rc

lokapujya’s picture

Status: Needs work » Needs review
FileSize
134.29 KB
15.23 KB

Fixed errors, fixed typos mentioned in #13, and removed a few more usages.

Status: Needs review » Needs work

The last submitted patch, 18: 2549805-18.patch, failed testing.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
134.31 KB
3.56 KB

Fix some mistakes that I added in #18.

lokapujya’s picture

1.) Some of these strings are displayed in the browser when viewing test results. Why wouldn't those messages need to use SafeMarkup? Are they getting escaped later? I think they are auto-escpaped, but just making sure.
2.) Maybe we can split this issue up since there are over 100 instances.

DuaelFr’s picture

Thank you very mush @lokapujya!

To anwser your questions I think that variables in tests can be considered as safe as they are never taken from user inputs. Even if one of these variables were not safe, tests are not likely run from the UI on a production server so there would be no impact to an XSS injection in the tests results.

About the size of this patch I think that core maintainers would like to have some smaller changes to review but I don't know how they'd like them to be splitted. One should ask them what they prefer.

Finally, I have a remark about some changes introduced by your patch. For example in:

+++ b/core/modules/aggregator/src/Tests/AggregatorRenderingTest.php
@@ -66,7 +64,7 @@ public function testBlockLinks() {
-    $this->assert(isset($links[0]), format_string('Link to href %href found.', array('%href' => $href)));
+    $this->assert(isset($links[0]), "Link to href $href found.");

The fact is that %href get transformed in something like <em>$href</em>. So, your patch make us loose something. As <em>something</em> is painful while reading tests results in command line, I'd suggest to add double quotes around the variable to keep the value separated from the other part of the message:

"Link to href \"$href\" found."
lokapujya’s picture

Thanks for reviewing!

The fact is that %href get transformed in something like $href. So, your patch make us loose something.

Good point. Maybe the <em> is important for messages?

As <em>something</em> is painful while reading tests results in command line

I don't know if we should worry about this part. One could argue that is command line's problem. Command line could eventually support interpreting <em> tag.

mpdonadio’s picture

Why don't we add a trait onto WebTestbase for a ::format() method that essentially does the same thing, but w/o the safe string handling? That should allow for nearly one-to-one replacement in tests?

DuaelFr’s picture

@mpdonadio I like your idea. Ready to provide a patch? :)

mpdonadio’s picture

Assigned: Unassigned » mpdonadio

Sure. Probably not tomorrow, but I'll have something ready on Monday or Tuesday.

xjm’s picture

So the current patch is changing both assertions themselves, and assertion messages. These are two separate scopes actually and I think we should split them into two separate issues. The former (removing it from non-messages) is important to make sure our tests are testing the right thing. The latter (removing format_string() from assertion messages) does not affect the integrity of the test and is a separate discussion. At first @alexpott had suggested doing so, but then said once "actually SimpleTest result display sanitization works differently than I thought" and so said it would need to be considered more carefully. There may be a different issue for that somewhere.

Also, I'm not actually in favor of adding additional assertion methods for the string formatting.

Finally, we should actually consider first off which of these assertion messages are unneeded. Often with an assertText() or assertRaw() the custom assertion message is not actually adding any additional information.

So my suggestion would be:

  1. One patch (this issue) that removes any format_string() and friends that are not on the message parameter.
  2. Take what's left over, and look at what messages are simply redundant that also use format_string(), and just remove the parameter so only the default message is used.
  3. Take what's left over from this patch after that, and then look into whether the message needs to be sanitized or not for output.
lokapujya’s picture

reroll + beginning #27-1. So, this only includes what we already discovered in the previous patch. Many more may have been added.

Status: Needs review » Needs work

The last submitted patch, 28: 2549805-28.patch, failed testing.

lokapujya’s picture

Assigned: mpdonadio » Unassigned
Status: Needs work » Needs review
FileSize
553 bytes
19.12 KB
lokapujya’s picture

I think I've identified all of them. Left some as @todos. Do we still want to do this? If so, then if anyone wants to finish it off, go ahead.

Status: Needs review » Needs work

The last submitted patch, 31: 2549805-31.patch, failed testing.

lokapujya’s picture

+++ b/core/modules/contact/src/Tests/ContactSitewideTest.php
@@ -130,7 +130,7 @@ function testSiteWideContact() {
-    $this->assertText(format_string('Machine-readable name cannot be longer than @max characters but is currently @exceeded characters long.', array('@max' => $max_length, '@exceeded' => $max_length_exceeded)));
+    $this->assertText('Machine-readable name cannot be longer than ' . $max_length . ' characters but is currently @' . $max_length_exceeded . ' characters long.');

remove the @

dawehner’s picture

Status: Needs work » Needs review

Just some quick feedback.

+++ b/core/modules/book/src/Tests/BookTest.php
@@ -271,8 +271,9 @@ function checkBookNode(EntityInterface $node, $nodes, $previous = FALSE, $up = F
+      $unrendered_text = ['#markup' => '<b>‹</b> ' . $previous->label()];
+      $rendered_text = \Drupal::service('renderer')->renderRoot($unrendered_text);

a renderPlain would be enough here. In general it feels weird though that we require to do that, but yeah this more or less how its expected to work.

lokapujya’s picture

I feel responsible for fixing the errors.

lokapujya’s picture

What would be the ideal branch to commit this?

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Mile23’s picture

Version: 8.1.x-dev » 8.2.x-dev
Issue tags: -rc eligible +@deprecated
FileSize
34.56 KB

Rerolled to 8.2.x.

@xjm in #27 might disagree, but we could preserve the effort here by reviewing and committing, add a follow-up to split things out for assertion strings vs. message strings, and maybe even scope for format_string() only, and one for SafeMarkup::format().

After this patch, NetBeans says: Found 581 matches of format_string in 158 files and Found 179 matches of SafeMarkup::format in 63 files. That's searching through files that end in *Test.php.

Status: Needs review » Needs work

The last submitted patch, 38: 2549805_38.patch, failed testing.

joelpittet’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 38: 2549805_38.patch, failed testing.

joelpittet’s picture

Custom test seemed to pass but it's stuck. @Mile23 mind uploading that again?

Mile23’s picture

I see this in the jenkins log:

23:52:14 Completed publish:gather_artifacts
23:52:14 Executing publish:junit_xmlformat
23:52:16 Reformatted test results written to /var/lib/drupalci/web/jenkins-default-179672/artifacts/xml/testresults.xml
23:52:16 Completed publish:junit_xmlformat
23:52:16 Completed publish
23:52:17 Checking console output
23:52:17 Recording test results
23:52:18 Finished: SUCCESS

But I'll re-up anyway.

Mile23’s picture

Status: Needs work » Needs review

Ewps.

Mile23’s picture

Version: 8.2.x-dev » 8.3.x-dev

Patch still applies to 8.3.x. Re-running tests.

lokapujya’s picture

The patch in @28 basically filters the changes down to what was suggested in the #27.1 comment. So patch #20 has a lot more conversions in it.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joelpittet’s picture

Status: Needs review » Needs work
Mile23’s picture

Status: Needs work » Needs review
FileSize
39.67 KB

Reroll of #43.

There are still a lot more usages. We might finish up here with the remainder of either SafeMarkup or format_string, and then do a follow-up for the other one. For the sake of reviewability.

format_string() in *Test.php: 156 files.

SafeMarkup::format() in *Test.php: 63 files.

Let's finish out SafeMarkup::format() here when this patch passes.

Mile23’s picture

Status: Needs review » Needs work

The last submitted patch, 51: 2549805_51.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mohit1604’s picture

Assigned: Unassigned » mohit1604
mohit1604’s picture

Assigned: mohit1604 » Unassigned
Status: Needs work » Needs review
FileSize
8.88 KB
41.03 KB

Status: Needs review » Needs work

The last submitted patch, 55: 2549805-55-D8.patch, failed testing. View results

alexpott’s picture

Some of this is going to be done automatically by #2958429: Properly deprecate SafeMarkup::format() - and some of this is going to be done by #2228741: Replace calls to format_string() with Drupal\Component\Render\FormattableMarkup. I think we should postpone on those two issue so that this issue only has to deal with FormattableMarkup usage in tests and not with any deprecated code.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

It could be closed, no more usage found in core

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

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). 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: 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.

xjm’s picture

Title: Remove all usage of SafeMarkup::format() and format_string() in tests apart from explicit tests of those methods » Remove all usage of FormattableMarkup in tests apart from explicit tests of those methods

@andypost Well, no, the original scope wasn't only about removing deprecated API use; it was the followup to getting rid of t() in tests. The equivalent of the safe string list is still being polluted by whatever's left in tests.

xjm’s picture

Title: Remove all usage of FormattableMarkup in tests apart from explicit tests of those methods » Remove all usage of FormattableMarkup in tests apart from explicit tests of that API
mrinalini9’s picture

Assigned: Unassigned » mrinalini9
mrinalini9’s picture

Assigned: mrinalini9 » Unassigned
Status: Needs work » Needs review
FileSize
123.98 KB

Rerolled patch for 9.1.x and removed FormattableMarkup and t() from tests, please review.

mrinalini9’s picture

Assigned: Unassigned » mrinalini9
Status: Needs review » Needs work
mrinalini9’s picture

Assigned: mrinalini9 » Unassigned
Status: Needs work » Needs review
FileSize
123.97 KB

Fixed PHPLint issues in #66, please review.

shalinigaur’s picture

Assigned: Unassigned » shalinigaur
shalinigaur’s picture

Assigned: shalinigaur » Unassigned
FileSize
26.52 KB

Status: Needs review » Needs work

The last submitted patch, 70: 2549805-70.patch, failed testing. View results

mohrerao’s picture

Assigned: Unassigned » mohrerao

Working on failed tests

mohrerao’s picture

1. Ran grep -r "FormattableMarkup" * and could find FormattableMarkup still in many places.
2. Also found t() being used in many asserts.

longwave’s picture

We have a separate meta for t() as there are thousands of these to replace. This issue should be for FormattableMarkup only.

mohrerao’s picture

@longwave in #63, it was requested for.

mohrerao’s picture

Assigned: mohrerao » Unassigned
Status: Needs work » Needs review
mohrerao’s picture

Status: Needs review » Needs work

Moved to back to needs work and working on failed tests.

mohrerao’s picture

Fixed failing tests in #70. Also removes t() from many places.
I second #74. It would be better to have a separate issue from removal of t().

longwave’s picture

That issue already exists: #3133726: [meta] Remove usage of t() in tests not testing translation

There are 933 instances of "new FormattableMarkup" in tests, is this too much to fix in one issue? As with the t() issue, should we convert this to a meta and try and break this down into smaller patches that are easier to write and review?

mohrerao’s picture

@longwave, I think it would be better not to pollute the scope of this issue. We can group all issues for removal of t() under one meta and then address it from there. Also want to know @xjm's view on this as she mentioned that the scope also includes removal of t() in #63.

mohrerao’s picture

Status: Needs work » Needs review

Moving to Needs review and expecting comments form @longwave and @xjm

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.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

longwave’s picture

We are almost done with removing t() in tests, there are two child issues of #3133726: [meta] Remove usage of t() in tests not testing translation open and then we should be done.

There are roughly just under 1000 references to FormattableMarkup in tests:

$ rg FormattableMarkup|grep -i test|wc -l
995

Some of those will be use statements and comments, or tests of FormattableMarkup itself. If we cut this down into just assertions there are about 600:

$ rg assert.*Formattable|wc -l
599

If we then break those down into kernel and functional tests, those seem manageable amounts to do in separate child issues:

$ rg assert.*Formattable|grep Kernel|wc -l
359

$ rg assert.*Formattable|grep Functional|wc -l
235

Does that seem OK as a rescoping of this problem? We can then clean up any final leftovers in this issue.

mondrake’s picture

Component: simpletest.module » phpunit

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

joachim’s picture

Status: Needs review » Needs work

Based on #84, it looks like this needs work?

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.