Problem/Motivation

There is no need to use t() in tests, unless we're testing translations, however in core we do not follow this consistently, which does not set a good example for new contributions.

In #3133726: [meta] Remove usage of t() in tests not testing translation we identified there are hundreds of calls to t() in calls to assert(No)Raw() and that removing all these in one go seems to be a suitable way of attacking this problem.

Proposed resolution

  • Add a deprecation triggered when passing an object as the argument to assert(No)Raw() calls. To be decided whether to leave it in permanently or only for the sake of finding the calls to correct.
  • Identify and remove all calls to t() wrapped in calls to assert(No)Raw().

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3170396

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mondrake created an issue. See original summary.

mondrake’s picture

Status: Active » Needs review
StatusFileSize
new1.77 KB

Kick-off patch, with just a deprecation error triggered when an object is passed as a $raw argument. This would give us the compass to steer through the conversions.

For discussion: shall we leave the deprecation in, or only during the conversion to be removed before commit? While we are saying that calling t() is legitimate in translation tests, IMHO those cases are limited enough to expect that developers could cast the output of t() to a string before putting that in the assertion.

Status: Needs review » Needs work

The last submitted patch, 2: 3170396-2.patch, failed testing. View results

mondrake’s picture

Assigned: Unassigned » mondrake

I'll start doing some.

mondrake’s picture

Assigned: mondrake » Unassigned
StatusFileSize
new18.84 KB
longwave’s picture

If we forced tests to cast to string, once we have done all these conversions we could deprecate and remove MarkupInterfaceComparator if we wanted. Is it better DX to allow people to pass MarkupInterface though? Not sure.

mondrake’s picture

Assigned: Unassigned » mondrake

#6 is a fair point in general, OTOH there is no comparator kicking in here specifically, since the assertion is eventually perfomed by WebAssert::response[Not]Contains which is just doing the casting to string itself.

#5 failure is telling us that we probably need to allow FormattableMarkup though:

+++ b/core/modules/action/tests/src/Functional/ConfigurationTest.php
@@ -83,7 +83,7 @@ public function testActionConfiguration() {
-    $this->assertRaw(t('The action %action has been deleted.', ['%action' => $new_action_label]));
+    $this->assertRaw("The action $new_action_label has been deleted.");

This is failing because the real string is

The action <em class="placeholder">$new_action_label</em> has been deleted.

but that seems to me too much to add all that boilerplate. So maybe here a reasonable compromise could be to switch to

-    $this->assertRaw(new FormattableMarkup('The action %action has been deleted.', ['%action' => $new_action_label]));

Thoughts?

Working on an updated patch.

mondrake’s picture

StatusFileSize
new17.57 KB
longwave’s picture

Re #7 I guess the question is: what is the intent of the test? We obviously care that the message text is correct. But do we care about the markup around the message, or is that incidental? Does testing the <em> tag serve any purpose?

If we only care about the message itself we should consider switching to assertText().

mondrake’s picture

#9 very good point... in fact I was also thinking lots of these assertions should use assertText (or more appropriately, WebAssert::pageTextContains).

Point is: we do that here straight, or we do an intermediate step to clean up here and then do the association to the appropriate WebAssert method in the parent issue?

longwave’s picture

I suspect we should do it elsewhere, as we know we need to get rid of t(), but we probably need core committer approval to remove the testing of markup.

Incidentally there are 11 matches of assertRaw.*<em class= calls already. Maybe it's best to convert the existing uses here to HTML strings (instead of using FormattableMarkup), and then open a followup to decide whether we should switch assertRaw('some <em>text</em>') to pageTextContains('some text')?

mondrake’s picture

StatusFileSize
new20.35 KB

Still trying to figure out the proper direction for this patch.

Here:

  • ::assertRaw calls that were using t() to workaround the preparation of a matchable string with markup for use by assertRaw, but are really meant to check correctness of the human readable text, are converted to $this->assertSession()->pageTextContains() with concatenated strings as input (so that they can match the page text as returned by the getText Mink method)
  • tests that are really using t() to translate an input string are kept as assertRaw calls, but with the argument explicitly cast to string to prevent the deprecation trigger
  • ... just for a limited number of cases to get feedback ...
mondrake’s picture

StatusFileSize
new32.01 KB
new11.66 KB

Some more.

mondrake’s picture

Assigned: mondrake » Unassigned

@longwave sorry #12 was xposted with #11 and I did not notice until I came back for #13. IMHO #12 and #13 are in the right direction, because I don't see a purpose to test the <em> markup, the use of t() in many cases seems just a shortcut. Maybe we should ask for feedback to core committers in Slack?

longwave’s picture

Yeah, the more I look at the work you've done so far here, the more I think I was wrong in #11 and we should just go straight to pageTextContains() in almost all cases.

mondrake’s picture

StatusFileSize
new33.39 KB
new59.3 KB

Some progress. I am starting to think we should not have a permanent deprecation trigger here: FormattableMarkup may be returned legitimately from underlying methods tested. So leaving ATM the deprecation trigger only TranslatableMarkup, however, there are cases where instead of t() there is use of new FormattableMarkup with the same intent of formatting a string within the test for the purpose of using it in an assert, which is what we are trying to get rid of. OTOH I agree casting t() to string explicitly in translation related testing is annoying. A bit messy.

mondrake’s picture

StatusFileSize
new3.98 KB
new59.74 KB

Trying to fix failures in the work done so far - which pertains to test groups starting with a-c. I'll pause here. We need feedback if this is the right direction and whether it would be ok to proceed with a single patch, which will be rather large eventually.

longwave’s picture

I think this is the right tag to get feedback here.

mondrake’s picture

StatusFileSize
new63.11 KB
new7.34 KB

Some fixes.

ravi.shankar’s picture

Assigned: Unassigned » ravi.shankar
ravi.shankar’s picture

Assigned: ravi.shankar » Unassigned
ravi.shankar’s picture

Assigned: Unassigned » ravi.shankar

Working on this.

longwave’s picture

@ravi.shankar Please see #17 and other comments in this issue - we want feedback from core committers as to whether this approach is OK before continuing with this patch.

mondrake’s picture

Assigned: ravi.shankar » Unassigned
Status: Needs work » Postponed (maintainer needs more info)

#23 yes... maybe Postponed is a better status right now.

ravi.shankar’s picture

@longwave Thanks

Let's wait for feedback.

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.

mondrake’s picture

Changed issue to MR workflow.

mondrake’s picture

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.

mondrake’s picture

Status: Postponed (maintainer needs more info) » Needs work

Rerolled.

mondrake’s picture

Rerolled. Don't think we need release manager review ATM.

mondrake’s picture

Status: Needs work » Needs review

Green. Ready for review

longwave’s picture

Status: Needs review » Needs work

Great work - there has obviously been a lot of effort put into this and the new assertions are all so much cleaner.

A couple of places we can improve and a couple of questions but otherwise this looks almost ready to go.

mondrake’s picture

Status: Needs work » Needs review

Thanks for review. Done the changes suggested, and removed the deprecation triggered when passing a Translatable object as the argument to assert(No)Raw() calls - that's not strictly necessary, it was useful to spot the changes to be done.

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Looks good, thanks!

mondrake’s picture

Status: Reviewed & tested by the community » Needs work

Few fixes needed.

mondrake’s picture

Assigned: Unassigned » mondrake
mondrake’s picture

Assigned: mondrake » Unassigned
Status: Needs work » Needs review

fixed

longwave’s picture

Status: Needs review » Reviewed & tested by the community

RTBC for real this time :)

  • catch committed 89da8c8 on 9.3.x
    Issue #3170396 by mondrake, longwave: Remove uses of t() in assert(No)...
catch’s picture

Title: Remove uses of t() in assert(No)Raw() calls » [backport] Remove uses of t() and switch to pageTextContains() in assert(No)Raw() calls
Version: 9.3.x-dev » 9.2.x-dev
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Agreed with the approach of switching to ::pageTextContains(), updating the issue title accordingly ;)

Committed/pushed to 9.3.x thanks!

Moving to 9.2.x for backport - this needs a re-roll/rebase there.

ankithashetty made their first commit to this issue’s fork.

ankithashetty’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll

Re-rolled against 9.2.x branch, thanks!

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Checked the reroll with diff -u <(curl https://git.drupalcode.org/project/drupal/-/merge_requests/301.diff) <(curl https://git.drupalcode.org/project/drupal/-/merge_requests/1025.diff) and the minor differences all look correct to me where things have changed slightly between 9.2.x and 9.3.x.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Needs another reroll for BlockTest.

ankithashetty’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll

Rebased the MR, thanks!

  • catch committed 18ab5c0 on 9.2.x
    Issue #3170396 by mondrake, ankithashetty, longwave, catch: [backport]...
catch’s picture

Status: Needs review » Fixed

If it's a straight rebase for a small conflict, it's OK to set the issue back to RTBC (one of the few cases you can mark your own patch RTBC). Committed/pushed to 9.2.x, thanks!

mondrake’s picture

Title: [backport] Remove uses of t() and switch to pageTextContains() in assert(No)Raw() calls » Remove uses of t() and switch to pageTextContains() in assert(No)Raw() calls

Status: Fixed » Closed (fixed)

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