Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
In #3082340: Replace assertEquals() overrides in PHPUnit Kernel, Functional and FunctionalJavascript tests with a MarkupInterfaceComparator implementation we extended PHPUnit to allow us to compare MarkupInterface objects with strings. AssertHelperTrait::castSafeStrings is a helper that does something very similar, we can attempt to deprecate it and use direct comparisons instead to make tests easier to read and write.
Comment | File | Size | Author |
---|---|---|---|
#56 | interdiff.3101247.50-56.txt | 4.26 KB | longwave |
#56 | 3101247-56.patch | 32.32 KB | longwave |
Comments
Comment #2
mondrakeJust a starter, out of curiosity.
Comment #4
mondrakeassertIdentical (or, actually, assertSame) will fail when comparing arrays that have on one side strings and on the other MarkupInterface objects, since they are not 'identical'. I wonder whether we should be really be strict in the tests failing in #2, or using assertEquals that will let the new comparator kick in would be sufficient.
Comment #5
longwaveI think switching to assertEquals is fine, as we were casting them before - they were equal but not strictly identical, really.
Comment #7
mondrake#5 OK, let's see what happens if we try to remove castSafeStrings from all things KernelTests.
Comment #8
mondrakeComment #10
mondrakeComment #11
mondrakeTrying to find a way to allow MarkupInterfaceComparator work in unit test context.
Comment #14
mondrakeIMO we would have to un-deprecate simpletest's AssertHelperTrait and copy back the castSafeStrings method to it, to get tests pass. Simpletest can't work with the comparator, so we can't have simpletest's and phpunit's castSafeString both deprecated. Possibly, we also need to have a simpletest's dedicated assertTitle method, since in the trait we need to remove the castSafeString method which will work for phpunit but not for simpletest.
Comment #15
longwaveAs Simpletest is going away in #3075490: Move simpletest module to contrib maybe #14 is OK? Or we postpone until Simpletest is removed?
Comment #16
mondrakeLet's wait until the situation of Simpletest removal is more clear.
Comment #17
longwaveIf/when Simpletest moves to contrib it's going to have to deal with this one way or another, so let's just un-deprecate the helper now?
Or we could put castSafeStrings() directly in Drupal\simpletest\TestBase?
Comment #18
jhedstrom+++ b/core/modules/simpletest/src/AssertHelperTrait.php
@@ -2,20 +2,32 @@
-@trigger_error(__NAMESPACE__ . '\AssertHelperTrait is deprecated in Drupal 8.4.x. Will be removed before Drupal 9.0.0. Use Drupal\Tests\AssertHelperTrait instead. See https://www.drupal.org/node/2884454.', E_USER_DEPRECATED);
-
-use Drupal\Tests\AssertHelperTrait as BaseAssertHelperTrait;
Since simpletest hasn't been moved to contrib, I think it's still ok to deprecate the helper trait entirely, and remove all usages in core, then when it does go to contrib, that trait just doesn't need to go along with it...
Comment #19
mondrakeComment #20
mondrakeComment #21
mondrakeComment #22
mondrakeRerolled, simpletest related hunks no longer needed.
Comment #23
mondrakeFixed deprecation messages, and removed sorting of array before comparing with assertEquals that is not necessary in PHPUnit.
Needs a CR and then referencing it here.
Comment #25
mondrakeConverted a new test class that was introduced in the meantime.
Comment #26
longwaveAdded draft change record: https://www.drupal.org/node/3123638
Marking as RTBC as this looks good to me, and the part I worked on has now been removed.
Comment #27
longwaveActually, needs work to get the change record link into the patch :)
Comment #28
mondrakeAdded links to CR.
Comment #29
longwaveLooks great!
Comment #30
mondrakeComment #31
alexpottThis feels like we're overstepping. This could hide errors from trying to translate a string too early - ie. prior to the container being booted. With this change something that would cause an error now won't. And I'm not convinced this is the correct behaviour. I can remember times when errors in too translation has caused us to make necessary and good changes to code.
Comment #32
mondrakeMaybe we can just fetch the untranslated string in the comparator if casting to string errors out.
Comment #33
alexpott@mondrake which test requires the code in #31?
Comment #34
alexpottAnd more pertinently why are things not breaking now?
Comment #35
mondrake#33 It was failing in #10 with a few Unit tests. And re. #34 why, I do not know, good question, will check.
Patch here doing #32.
Comment #36
mondrakeThe interdiff.
Comment #37
mondrakeRerolled
Comment #38
mondrakeSo #37 is stiil presenting the same failures of #10 - seem to be related to the ToStringTrait that lets the script die if an exception is thrown during __toString.
Comment #39
mondrakeSorry.
Comment #41
mondrakeSo #37 is certainly better than #39... and shows the same 2 failures of #10. Maybe the question is then why BatchBuilderTest and TableSelectTest fail but other unit tests that still rely on TranslatableMarkup do not.
Comment #42
mondrakeLet's see if this is the right thread - passing
$this->getStringTranslationStub()
toTranslatableMarkup
instances like other unit tests do. The reason why this was passing before (I think) is that now the comparator will always try to convert to strings both actual and expected.Comment #43
alexpottAh I see where the fails are coming from. I'm not sure about this because it's introducing an interesting dependency on the container. This is going to affect contrib unit tests massively.
Comment #44
mondrakeSee this maybe?
Comment #45
mondrakeSo #44 does not work, either, because
assertSame
on objects require expected and actual to be the same object (obviously), but the sense of the test here is actually to check if the translated string is equal - which clashes with the fact that either the service container is not available, or that we cannot inject the stub string translation service on the SUT objects. Out of ideas at the moment.@alexpott re #43, trying to follow-up on your point in #3082340-37: Replace assertEquals() overrides in PHPUnit Kernel, Functional and FunctionalJavascript tests with a MarkupInterfaceComparator implementation :)
Comment #46
mondrakeComment #47
mondrakeComment #48
longwaveCan we just do this? We don't need the comparator to compare two TranslatableMarkup objects using assertEquals(), we only need it when they are different types.
Comment #49
mondrakeNeat, thanks @longwave! +1 for RTBC, but cannot myself.
Comment #50
longwaveRerolled. Unsure who else will review this so if @mondrake agrees can we RTBC this between us?
Comment #51
mondrakeThanks, so it's going to be RTBU :) then.
LGTM, here we are PHPunitizing more the test codebase by removing usage of a trait that was needed in Simpletest to ensure equality comparison between strings when MarkupInterface objects are involved. In PHPUnit we can use comparators instead and one was introduced in D8.9 specifically for the task - thus making the trait redundant.
Comment #52
mondrakeBTW - net of deprecation code, I believe this can be backported up to D8.9 if required.
Comment #53
xjmThe title of this issue made me go, "What? No..." until I read the IS also. Let's mention in the deprecation message that the reason there's no replacement is that
MarkupInterface
objects can now be compared with strings by default in assertions. (Or however we want to word it; suggestions welcome.)Note that the current message is also comma-spliced. (The comma should be a semicolon, unless you are Jane Austen.) And the method name should have
()
.Also, both method names need parens here. (And of course these should be updated to match the docblock.)
wat
I see #44 - #48 are about this; maybe we can add an inline comment?
Comment #54
saurabh-2k17 CreditAttribution: saurabh-2k17 at Srijan | A Material+ Company for Drupal India Association commentedComment #55
saurabh-2k17 CreditAttribution: saurabh-2k17 at Srijan | A Material+ Company for Drupal India Association commentedHi @xjm,
I have tried fixing the issues which you pointed except the last one, it was not clear for me that what kind of inline comment i should add. Rest two points are covered. Please take a look :)
Thanks,
Comment #56
longwaveReviving this. #55 isn't quite right as far as English grammar goes, so I redid the deprecation message.
Also we don't need to change BatchBuilderTest at all now, from what I can figure locally.
Interdiff from #50.
Comment #57
mondrakeThank you, points from #53 addressed.
Comment #59
catchCommitted 15de078 and pushed to 9.1.x. Thanks!