Strip HTML tags when using assertEquals() to compare markup

Created on 20 June 2020, over 4 years ago
Updated 7 February 2023, almost 2 years ago

Problem/Motivation

In PHPUnit tests, we can use assertEquals() to compare MarkupInterface objects via the MarkupInterfaceComparator. Often, we only care about the text of the markup, and not the specific HTML tags.

Proposed resolution

This issue changes MarkupInterfaceComparator so it strips tags before comparing; this way only the text of the markup is considered for equality.

To provide a backward compatibility safety net, if both the expected and actual values contain markup (that is, the test is sensitive to the full markup and not just the text), a deprecation is issued informing the user that they should explicitly cast to string and use assertSame() instead.

Remaining tasks

User interface changes

None

API changes

In tests, assertEquals() will issue a deprecation if both the expected and actual values contain markup. Tests that are markup-sensitive should use assertSame() instead.

Data model changes

None

Release notes snippet

📌 Task
Status

Fixed

Version

10.1

Component
PHPUnit 

Last updated about 1 hour ago

Created by

🇮🇳India Hardik_Patel_12 India

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Issue was unassigned.
  • Status changed to Needs review almost 2 years ago
  • Status changed to RTBC almost 2 years ago
  • 🇺🇸United States smustgrave

    Changes look good.

  • 🇬🇧United Kingdom longwave UK

    Looks great - one nitpick that could go either way:

    +++ b/core/modules/field/tests/src/Kernel/EntityReference/Views/SelectionTest.php
    @@ -132,7 +133,10 @@ public function testSelectionHandler() {
    +      $subresults_formatted = array_map(function (MarkupInterface $markup): string {
    +        return (string) $markup;
    +      }, $subresults);
    +      $filtered_rendered_results_formatted += $subresults_formatted;
    

    I think it's somewhat less verbose to just use strval()?

    $filtered_rendered_results_formatted += array_map('strval', $subresults_formatted);
    

    or if you want to be more strictly typed

    $filtered_rendered_results_formatted += array_map(fn(MarkupInterface $markup) => (string) $markup, $subresults_formatted);
    
  • 🇬🇧United Kingdom longwave UK

    ...so while this solves the issue of assertEquals() comparing markup, it no longer solves the actual issue of removing t() in assertEquals() - there are a whole bunch of cases left:

    $ rg 'assertEquals.*\Wt\('|sort
    core/modules/contact/tests/src/Functional/ContactSitewideTest.php:    $this->assertEquals(t('[@label] @subject', ['@label' => $label, '@subject' => $edit['subject[0][value]']]), $mail['subject']);
    core/modules/field/tests/src/Kernel/FieldCrudTest.php:    $this->assertEquals(t('%name does not accept the value @value.', ['%name' => $field_name, '@value' => -2]), $violations[0]->getMessage());
    core/modules/field/tests/src/Kernel/FieldCrudTest.php:    $this->assertEquals(t('This value should be between %min and %max.', ['%min' => 0, '%max' => 32]), $violations[0]->getMessage());
    core/modules/field/tests/src/Kernel/FieldCrudTest.php:    $this->assertEquals(t('This value should be between %min and %max.', ['%min' => 0, '%max' => 32]), $violations[1]->getMessage());
    core/modules/field/tests/src/Kernel/FieldValidationTest.php:    $this->assertEquals(t('%name: this field cannot hold more than @count values.', ['%name' => $this->fieldTestData->field->getLabel(), '@count' => $cardinality]), $violations[0]->getMessage());
    core/modules/file/tests/src/Kernel/SaveTest.php:    $this->assertEquals(t('The file %value already exists. Enter a unique file URI.', ['%value' => $uppercase_file_duplicate->getFileUri()]), $violations[0]->getMessage());
    core/modules/forum/tests/src/Kernel/ForumValidationTest.php:    $this->assertEquals(t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', ['%forum' => $container->label()]), $violations[0]->getMessage());
    ...
    
  • 🇺🇸United States xjm

    So the title and IS don't seem to be describing what's actually being done in this issue.

    The scope of the parent meta was intended to be removing unnecessary direct uses of t() and friends in test fixture data. However, this issue's primary scope seems to be that of converting assertEquals() calls to assertSame() (and therefore adding a string cast on passed-in markup objects, where t() is already not called directly in the assertions).

    If the goal is to remove use of assertEquals() with translatable strings and markup objects, then the IS should be completely rewritten and the issue retitled.

    Thanks for working on this!

  • Status changed to Needs work almost 2 years ago
  • 🇺🇸United States smustgrave

    With such a large number of changes would it make sense to breakup?

  • 🇬🇧United Kingdom longwave UK

    I don't think it can be broken up easily but we might want to spin off a child issue for the changes to MarkupInterfaceComparator and then come back here to fix the assertEquals() calls.

  • 🇮🇹Italy mondrake 🇮🇹

    Addressed #75.

  • Status changed to Needs review almost 2 years ago
  • 🇺🇸United States xjm

    @smustgrave, actually this issue is about the perfect size for a mostly 1:1 replacement on a single line.

    FWIW re: #77, I chatted with @longwave about this. The current scope is fine if we update the title and IS, but we would need a followup for the original scope?

  • Status changed to Needs work almost 2 years ago
  • 🇮🇹Italy mondrake 🇮🇹

    #80 xposted with #76, reset status to NW.

  • 🇬🇧United Kingdom longwave UK

    Opened 📌 Remove remaining uses of t() in assertEquals() calls Fixed to handle the remainder, and retitled and updated the IS of this issue.

    The change record still needs work.

  • Status changed to Needs review almost 2 years ago
  • 🇮🇹Italy mondrake 🇮🇹

    Fleshed the CR.

  • Status changed to RTBC almost 2 years ago
  • 🇺🇸United States smustgrave

    Reviewed the change record and looks great. The examples were super helpful

  • 🇬🇧United Kingdom longwave UK

    RTBC +1, the change record is nice and clear to me.

  • 🇬🇧United Kingdom catch

    +1 from me.

    • longwave committed c187cc63 on 10.1.x
      Issue #3153468 by mondrake, mohrerao, mallezie, smustgrave, meena.bisht...
  • Status changed to Fixed almost 2 years ago
  • 🇬🇧United Kingdom longwave UK

    Committed and pushed c187cc6339 to 10.1.x. Thanks!

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

Production build 0.71.5 2024