Remove uses of FormattableMarkup in kernel test assertions

Created on 10 August 2021, over 3 years ago
Updated 30 November 2024, 5 months ago

Problem/Motivation

There is no need to use FormattableMarkup to produce message strings in PHPUnit assertions; it is cleaner and easier in almost all cases to use simple string interpolation instead.

Steps to reproduce

Proposed resolution

Replace all uses of FormattableMarkup in kernel test assertions with either plain string interpolation, or remove the assertion message entirely when the assertion is obvious and the message does not add any context.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Needs work

Version

11.0 πŸ”₯

Component

phpunit

Created by

πŸ‡¬πŸ‡§United Kingdom longwave UK

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

Merge Requests

Comments & Activities

Not all content is available!

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

  • πŸ‡·πŸ‡ΊRussia zniki.ru

    nikolay shapovalov β†’ made their first commit to this issue’s fork.

  • Pipeline finished with Success
    5 months ago
    Total: 916s
    #355075
  • Pipeline finished with Failed
    5 months ago
    Total: 147s
    #355087
  • πŸ‡·πŸ‡ΊRussia zniki.ru

    I was not able to fix merge conflicts, and create new MR.
    Used changes from Dave's MR.

    Current status:

    grep FormattableMarkup -r -l | grep Kernel
    
    tests/Drupal/KernelTests/Core/Routing/ExceptionHandlingTest.php (single comment)
    tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php (markup tests)
    tests/Drupal/KernelTests/Core/Test/Comparator/MarkupInterfaceComparatorTest.php (markup tests)
    tests/Drupal/KernelTests/AssertContentTrait.php (not sure to fix)
    tests/Drupal/KernelTests/Component/Render/FormattableMarkupKernelTest.php (test FormattableMarkup)
    modules/system/tests/src/Kernel/Theme/FunctionsTest.php (not sure to fix)
    modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php (don't know how to fix)
    

    Todo:
    Decide if should be removed from

    • tests/Drupal/KernelTests/AssertContentTrait.php
    • modules/system/tests/src/Kernel/Theme/FunctionsTest.php

    And fix, I don't have a clue what will be the best way, looking for your suggestions.

    • modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php
  • Pipeline finished with Success
    5 months ago
    Total: 2274s
    #355090
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Tried comparing the original MR against 9.3.x, which has more changes. Checking the file changes missing it appears the FormattableMarkup() has already been done.

    But also notice that a number of the tests are now using HTML:escape which didn't see in the original MR either?

    Think the issue summary should be updated if the scope is being altered from the original.

  • πŸ‡·πŸ‡ΊRussia zniki.ru

    Thanks a lot.
    You are right, code base changed significantly.
    I used changes made by @longwave, but also add some fixes from my side.
    Scope was not changed because there was no direct tasks, I added grep command to get current list of Tests files that used FormattableMarkup.

    Set status to NW, because I have no solution for SmartDefaultSettingsTest.php .

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Since those tests don't appear to be testing translation or anything could the parts just be added to make a single string to compare?

  • πŸ‡·πŸ‡ΊRussia zniki.ru

    Or we can just compare message string and variable array without convert it to string itself.

          $variables = unserialize($log->variables);
          $message = strtr($log->message, $variables);
          $db_logs[$type_to_status[$log->severity]][] = (string) $message;
    

    Then we don't even need to unserialize variables.

  • Pipeline finished with Failed
    5 months ago
    Total: 86s
    #356672
  • πŸ‡·πŸ‡ΊRussia zniki.ru

    I was able to remove FormattableMarkup from SmartDefaultSettingsTest.php.
    IS updated. MR updated.
    Does anyone have idea what should be done with these files:

    • tests/Drupal/KernelTests/AssertContentTrait.php
    • modules/system/tests/src/Kernel/Theme/FunctionsTest.php
  • Pipeline finished with Success
    5 months ago
    Total: 4980s
    #356675
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    I glanced at the MR and the size is well past that recommended in the Merge Request size β†’ section of the scope guidelines. I suggest that this be split into smaller issues that are easier to review. Say, creating an MR for a group of changes that are implementing the same style of fix. I also noticed what could be inconsistent use of sprintf in the MR, that is, sometimes it is used and sometimes it is not. Can this be explained.

  • Pipeline finished with Canceled
    5 months ago
    Total: 483s
    #357335
  • Pipeline finished with Success
    5 months ago
    Total: 1540s
    #357325
  • Pipeline finished with Success
    5 months ago
    Total: 534s
    #357342
  • πŸ‡·πŸ‡ΊRussia zniki.ru

    Thanks for your feedback.
    I removed sprintf, and keep it only at core/modules/migrate/tests/src/Kernel/MigrateEntityContentValidationTest.php to keep code style consistent.
    I create 2 new MRs and move changes from first MR.

    1. MR 10434 - Changes in FileManagedUnitTestBase doesn't look obvious.
    2. MR 10433 - Changes at data provider for SmartDefaultSettingsTest is huge.

    Should I create new issue for each MR, or it's possible to keep several MRs at current issue?

  • Pipeline finished with Success
    5 months ago
    Total: 953s
    #357341
  • Pipeline finished with Canceled
    5 months ago
    Total: 238s
    #357701
  • Pipeline finished with Success
    5 months ago
    Total: 507s
    #357705
  • Pipeline finished with Canceled
    5 months ago
    Total: 84s
    #357735
  • Pipeline finished with Success
    5 months ago
    Total: 502s
    #357736
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    With regards to #19 and (hopefully) to help answer #22. New tickets should be created. For each issue it should have 1 MR to review to make it super clear what's being fixed. Sometimes reviewers or committers may need the top level view of things and if multiple MRs it could be hard to review.

    Let me know if that makes sense.

  • πŸ‡·πŸ‡ΊRussia zniki.ru

    Create 2 new child issues

    1. πŸ“Œ Remove FormattableMarkup from SmartDefaultSettingsTest Active
    2. πŸ“Œ Remove FormattableMarkup from FileManagedUnitTestBase Active

    Close 2 MRs. Update IS.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thanks for breaking out, these looks like good replacements.

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Left some comments on the MR, we can simplify the date formatter test bit more now - there's some dead code

  • Pipeline finished with Canceled
    5 months ago
    Total: 127s
    #373916
  • Pipeline finished with Failed
    5 months ago
    Total: 627s
    #373917
  • Pipeline finished with Failed
    5 months ago
    Total: 101s
    #373927
  • Pipeline finished with Failed
    5 months ago
    Total: 101s
    #373937
  • Pipeline finished with Failed
    5 months ago
    Total: 101s
    #373943
  • Pipeline finished with Canceled
    5 months ago
    Total: 71s
    #373959
  • Pipeline finished with Success
    5 months ago
    Total: 757s
    #373963
  • πŸ‡·πŸ‡ΊRussia zniki.ru

    Thanks for review. I simplified the date formatter test. And apllied one more suggestion.
    Changes at πŸ“Œ Fix 'Drupal.Semantics.FunctionT.NotLiteralString' coding standard Active caused MR conflict, I fixed conflict.

    Ready for one more review round.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Feedback appears to be addressed.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Step 3 of the remaining tasks has not been resolved yet. I didn't see discussion of those files in the comments, apologies if I missed that. Do they need a separate issue or can that be discussed here? I am not sure.

    The title here should probably change to something like "Remove remaining uses of FormattableMarkup in kernel test assertions" to help indicate this is not all Kernel tests tests.

    I also think it would be helpful to those who come after us to add a class comment in these files to explicitly state why FormattableMarkup is used in those tests. That can be a separate issue. If the text of the comment is supplied that would be a suitable novice issue.

    • tests/Drupal/KernelTests/Core/Routing/ExceptionHandlingTest.php (single comment)
    • tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php (markup tests)
    • tests/Drupal/KernelTests/Core/Test/Comparator/MarkupInterfaceComparatorTest.php (markup tests)
    • tests/Drupal/KernelTests/Component/Render/FormattableMarkupKernelTest.php (test FormattableMarkup)
  • πŸ‡·πŸ‡ΊRussia zniki.ru

    Thanks for the feedback.
    +1 for new title, so I update the title.
    I believe we still need to decide, if listed tests still need FormattableMarkup, before we can proceed with MR review.
    I already made my suggestion, because I create this list, and think we can keep it there. I am looking for someone to also check these files and provide their feedback.

    I like the idea to add comments to files that will use FormattableMarkup, explaining the reason. This can be done at follow up.
    But decision about keeping or removing should be done at this issue.

    Not sure what status to set for now, leave it NW.

  • πŸ‡·πŸ‡ΊRussia zniki.ru

    Set to NR to get some feedback.

  • Pipeline finished with Canceled
    4 months ago
    Total: 130s
    #385243
  • πŸ‡·πŸ‡ΊRussia zniki.ru

    I made changes to ConfigImporterMissingContentTest::log().

  • Pipeline finished with Failed
    4 months ago
    Total: 542s
    #385244
  • Pipeline finished with Failed
    4 months ago
    Total: 1381s
    #385252
  • Pipeline finished with Canceled
    4 months ago
    Total: 350s
    #385285
  • Pipeline finished with Success
    4 months ago
    Total: 418s
    #385297
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Appears to need rebase

    If you are another contributor eager to jump in, please allow the previous poster at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!

  • Pipeline finished with Failed
    3 months ago
    Total: 113s
    #416108
  • πŸ‡·πŸ‡ΊRussia zniki.ru

    Rebased.

  • Pipeline finished with Failed
    3 months ago
    Total: 493s
    #416111
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Believe feedback has been addressed.

  • πŸ‡¬πŸ‡§United Kingdom catch

    @smustgrave's feedback in #14 wasn't addressed afaict, it's not clear why Html::escape() is used in some of the assertions and that seems unnecessary.

  • πŸ‡·πŸ‡ΊRussia zniki.ru

    @catch HTML::escape used only for variables with special characters.

  • Pipeline finished with Success
    3 months ago
    Total: 359s
    #421672
  • The Needs Review Queue Bot β†’ tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Rebased and grep currently shows

    $ grep FormattableMarkup -r -l | grep Kernel
    core/tests/Drupal/KernelTests/AssertContentTrait.php
    core/tests/Drupal/KernelTests/Component/Render/FormattableMarkupKernelTest.php
    core/tests/Drupal/KernelTests/Core/Test/Comparator/MarkupInterfaceComparatorTest.php
    core/tests/Drupal/KernelTests/Core/Routing/ExceptionHandlingTest.php
    core/tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php
    core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php
    

    And modified to remove the files where FormattableMarkup is to remain;

    $ grep FormattableMarkup -r -l | grep -v tests/Drupal/KernelTests/Core/Routing/ExceptionHandlingTest.php | grep -v tests/Drupal/KernelTe
    sts/Core/Theme/TwigMarkupInterfaceTest.php | grep -v tests/Drupal/KernelTests/Core/Test/Comparator/MarkupInterfaceComparatorTest.php | grep -v tests/Drupal/KernelTests/Component/Render/FormattableMarkupKernelTest.ph | grep Kernel
    core/tests/Drupal/KernelTests/AssertContentTrait.php
    core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    For consideration, I removed the use of FormattableMarkup to build the message in AssertContentTrait.php. This is only done for methods that are not deprecated.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    appears to have merge conflicts

    If you are another contributor eager to jump in, please allow the previous poster at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Thanks for the bump.

    Rebased with several conflicts in several files

  • πŸ‡·πŸ‡ΊRussia zniki.ru

    Found one issue in the MR. Please fix.

  • πŸ‡·πŸ‡ΊRussia zniki.ru

    Thanks, LGTM.

  • Pipeline finished with Success
    23 days ago
    Total: 484s
    #474932
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    Committed aeccef9 and pushed to 11.x. Thanks!

  • Status changed to Fixed 23 days ago
    • longwave β†’ committed aeccef91 on 11.x
      Issue #3227598 by nikolay shapovalov, longwave, quietone, smustgrave,...
  • πŸ‡¬πŸ‡§United Kingdom longwave UK
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024