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 toassert(No)Raw().
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-3170396
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:
- 3170396-9.2.x
changes, plain diff MR !1025
- 3170396-remove-uses-of
changes, plain diff MR !301
Comments
Comment #2
mondrakeKick-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.
Comment #4
mondrakeI'll start doing some.
Comment #5
mondrakeComment #6
longwaveIf 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.
Comment #7
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:
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
Thoughts?
Working on an updated patch.
Comment #8
mondrakeComment #9
longwaveRe #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().Comment #10
mondrake#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?
Comment #11
longwaveI 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 switchassertRaw('some <em>text</em>')topageTextContains('some text')?Comment #12
mondrakeStill trying to figure out the proper direction for this patch.
Here:
::assertRawcalls 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)Comment #13
mondrakeSome more.
Comment #14
mondrake@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?Comment #15
longwaveYeah, 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.Comment #16
mondrakeSome 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 ofnew FormattableMarkupwith 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.Comment #17
mondrakeTrying 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.
Comment #18
longwaveI think this is the right tag to get feedback here.
Comment #19
mondrakeSome fixes.
Comment #20
ravi.shankar commentedComment #21
ravi.shankar commentedComment #22
ravi.shankar commentedWorking on this.
Comment #23
longwave@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.
Comment #24
mondrake#23 yes... maybe Postponed is a better status right now.
Comment #25
ravi.shankar commented@longwave Thanks
Let's wait for feedback.
Comment #28
mondrakeChanged issue to MR workflow.
Comment #29
mondrakeComment #31
mondrakeRerolled.
Comment #32
mondrakeRerolled. Don't think we need release manager review ATM.
Comment #33
mondrakeGreen. Ready for review
Comment #34
longwaveGreat 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.
Comment #35
mondrakeThanks 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.
Comment #36
longwaveLooks good, thanks!
Comment #37
mondrakeFew fixes needed.
Comment #38
mondrakeComment #39
mondrakefixed
Comment #40
longwaveRTBC for real this time :)
Comment #42
catchAgreed 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.
Comment #46
ankithashettyRe-rolled against 9.2.x branch, thanks!
Comment #47
longwaveChecked 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.Comment #48
catchNeeds another reroll for BlockTest.
Comment #49
ankithashettyRebased the MR, thanks!
Comment #51
catchIf 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!
Comment #52
mondrake