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.

CommentFileSizeAuthor
#56 interdiff.3101247.50-56.txt4.26 KBlongwave
#56 3101247-56.patch32.32 KBlongwave
#55 interdiff_50-55.txt2.34 KBsaurabh-2k17
#55 deprecated_asserthelpertrait-3101247-55.patch34.87 KBsaurabh-2k17
#50 3101247-50.patch34.53 KBlongwave
#48 interdiff.3101247.44-48.txt4.04 KBlongwave
#48 3101247-48.patch34.57 KBlongwave
#44 3101247-43.patch37.88 KBmondrake
#44 interdiff_42-43.txt6.95 KBmondrake
#42 interdiff_39-42.txt6.56 KBmondrake
#42 3101247-42.patch37.49 KBmondrake
#39 3101247-39.patch35.2 KBmondrake
#39 interdiff_38-39.txt1.03 KBmondrake
#38 3101247-38.patch35.18 KBmondrake
#38 interdiff_37-38.txt1.91 KBmondrake
#37 3101247-37.patch35.42 KBmondrake
#36 interdiff_28-35.txt3.48 KBmondrake
#35 3101247-35.patch35.43 KBmondrake
#28 interdiff_25-28.txt2.03 KBmondrake
#28 3101247-28.patch34.2 KBmondrake
#25 3101247-25.patch34.2 KBmondrake
#25 interdiff_23-25.txt1.42 KBmondrake
#23 interdiff_22-23.txt4.21 KBmondrake
#23 3101247-23.patch32.78 KBmondrake
#22 3101247-22.patch32.21 KBmondrake
#17 interdiff.3101247.11-17.txt2.22 KBlongwave
#17 3101247-17.patch34.52 KBlongwave
#11 3101247-12.patch32.45 KBmondrake
#11 interdiff_11-12.txt1.13 KBmondrake
#10 interdiff_8-11.txt12.08 KBmondrake
#10 3101247-11.patch31.32 KBmondrake
#8 interdiff_4-8.txt13.38 KBmondrake
#8 3101247-8.patch20.17 KBmondrake
#4 interdiff_2-4.txt4.7 KBmondrake
#4 3101247-4.patch15.57 KBmondrake
#2 3101247-2.patch15.6 KBmondrake
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

longwave created an issue. See original summary.

mondrake’s picture

Status: Active » Needs review
FileSize
15.6 KB

Just a starter, out of curiosity.

Status: Needs review » Needs work

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

mondrake’s picture

Status: Needs work » Needs review
FileSize
15.57 KB
4.7 KB

assertIdentical (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.

longwave’s picture

I think switching to assertEquals is fine, as we were casting them before - they were equal but not strictly identical, really.

Status: Needs review » Needs work

The last submitted patch, 4: 3101247-4.patch, failed testing. View results

mondrake’s picture

Assigned: Unassigned » mondrake

#5 OK, let's see what happens if we try to remove castSafeStrings from all things KernelTests.

mondrake’s picture

Assigned: mondrake » Unassigned
Status: Needs work » Needs review
FileSize
20.17 KB
13.38 KB

Status: Needs review » Needs work

The last submitted patch, 8: 3101247-8.patch, failed testing. View results

mondrake’s picture

Status: Needs work » Needs review
FileSize
31.32 KB
12.08 KB
mondrake’s picture

Trying to find a way to allow MarkupInterfaceComparator work in unit test context.

The last submitted patch, 10: 3101247-11.patch, failed testing. View results

Status: Needs review » Needs work

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

mondrake’s picture

IMO 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.

longwave’s picture

As Simpletest is going away in #3075490: Move simpletest module to contrib maybe #14 is OK? Or we postpone until Simpletest is removed?

mondrake’s picture

Status: Needs work » Postponed
Related issues: +#3075490: Move simpletest module to contrib

Let's wait until the situation of Simpletest removal is more clear.

longwave’s picture

If/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?

jhedstrom’s picture

+++ 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...

mondrake’s picture

Version: 8.9.x-dev » 9.1.x-dev
mondrake’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
mondrake’s picture

Assigned: Unassigned » mondrake
mondrake’s picture

Rerolled, simpletest related hunks no longer needed.

mondrake’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll +Needs change record
FileSize
32.78 KB
4.21 KB

Fixed 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.

Status: Needs review » Needs work

The last submitted patch, 23: 3101247-23.patch, failed testing. View results

mondrake’s picture

Status: Needs work » Needs review
FileSize
1.42 KB
34.2 KB

Converted a new test class that was introduced in the meantime.

longwave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs change record

Added 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.

longwave’s picture

Status: Reviewed & tested by the community » Needs work

Actually, needs work to get the change record link into the patch :)

mondrake’s picture

Status: Needs work » Needs review
FileSize
34.2 KB
2.03 KB

Added links to CR.

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Looks great!

mondrake’s picture

Assigned: mondrake » Unassigned
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php
@@ -187,7 +188,14 @@ public function getArguments() {
-      $this->translatedMarkup = $this->getStringTranslation()->translateString($this);
+      try {
+        $this->translatedMarkup = $this->getStringTranslation()->translateString($this);
+      }
+      catch (ContainerNotInitializedException $e) {
+        // In unit tests, there is no container initialized. In that case, we
+        // just return the untranslated string.
+        $this->translatedMarkup = $this->string;
+      }

This 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.

mondrake’s picture

Assigned: Unassigned » mondrake

Maybe we can just fetch the untranslated string in the comparator if casting to string errors out.

alexpott’s picture

@mondrake which test requires the code in #31?

alexpott’s picture

And more pertinently why are things not breaking now?

mondrake’s picture

Status: Needs work » Needs review
FileSize
35.43 KB

#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.

mondrake’s picture

FileSize
3.48 KB

The interdiff.

mondrake’s picture

mondrake’s picture

So #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.

mondrake’s picture

Status: Needs review » Needs work

The last submitted patch, 39: 3101247-39.patch, failed testing. View results

mondrake’s picture

So #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.

mondrake’s picture

Status: Needs work » Needs review
FileSize
37.49 KB
6.56 KB

Let's see if this is the right thread - passing $this->getStringTranslationStub() to TranslatableMarkup 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.

alexpott’s picture

+++ b/core/tests/Drupal/Tests/UnitTestCase.php
@@ -40,6 +41,10 @@
+
+    // Allow tests to compare MarkupInterface objects via assertEquals().
+    $this->registerComparator(new MarkupInterfaceComparator());

Ah 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.

mondrake’s picture

See this maybe?

mondrake’s picture

So #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 :)

mondrake’s picture

Status: Needs review » Needs work
mondrake’s picture

Assigned: mondrake » Unassigned
longwave’s picture

Status: Needs work » Needs review
FileSize
34.57 KB
4.04 KB

Can 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.

mondrake’s picture

Neat, thanks @longwave! +1 for RTBC, but cannot myself.

longwave’s picture

Rerolled. Unsure who else will review this so if @mondrake agrees can we RTBC this between us?

mondrake’s picture

Status: Needs review » Reviewed & tested by the community

RTBC this between us?

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

mondrake’s picture

BTW - net of deprecation code, I believe this can be backported up to D8.9 if required.

xjm’s picture

Title: Deprecate AssertHelperTrait::castSafeStrings in favour of MarkupInterfaceComparator » Deprecate AssertHelperTrait::castSafeStrings() in favour of MarkupInterfaceComparator
Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/tests/Drupal/Tests/AssertHelperTrait.php
    @@ -17,8 +17,14 @@
    +   * @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. There is no
    +   *   replacement, just use assertEquals in tests.
    

    The 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 ().

  2. +++ b/core/tests/Drupal/Tests/AssertHelperTrait.php
    @@ -17,8 +17,14 @@
    +    @trigger_error('AssertHelperTrait::castSafeStrings is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. There is no replacement, just use assertEquals in tests. See https://www.drupal.org/node/3123638', E_USER_DEPRECATED);
    
    +++ b/core/tests/Drupal/Tests/AssertHelperTraitTest.php
    @@ -8,12 +8,14 @@
    +   * @expectDeprecation AssertHelperTrait::castSafeStrings is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. There is no replacement, just use assertEquals in tests. See https://www.drupal.org/node/3123638
    

    Also, both method names need parens here. (And of course these should be updated to match the docblock.)

  3. +++ b/core/tests/Drupal/Tests/Core/Batch/BatchBuilderTest.php
    @@ -90,10 +90,10 @@ public function testSetInitMessage() {
    -      ->setProgressMessage(new TranslatableMarkup('Batch in progress...'))
    +      ->setProgressMessage(new TranslatableMarkup('Batch in progress...', [], [], $this->getStringTranslationStub()))
    
    @@ -101,10 +101,10 @@ public function testSetProgressMessage() {
    -      ->setErrorMessage(new TranslatableMarkup('Oops. An error has occurred :('))
    +      ->setErrorMessage(new TranslatableMarkup('Oops. An error has occurred :(', [], [], $this->getStringTranslationStub()))
    

    wat

    I see #44 - #48 are about this; maybe we can add an inline comment?

saurabh-2k17’s picture

Assigned: Unassigned » saurabh-2k17
saurabh-2k17’s picture

Assigned: saurabh-2k17 » Unassigned
Status: Needs work » Needs review
FileSize
34.87 KB
2.34 KB

Hi @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,

longwave’s picture

Reviving 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.

mondrake’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Deprecated assertions

Thank you, points from #53 addressed.

  • catch committed 15de078 on 9.1.x
    Issue #3101247 by mondrake, longwave, Saurabh_sgh, alexpott, jhedstrom,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed 15de078 and pushed to 9.1.x. Thanks!

Status: Fixed » Closed (fixed)

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