Remove all usage of FormattableMarkup in tests apart from explicit tests of that API

Created on 11 August 2015, almost 10 years ago
Updated 25 August 2024, 10 months ago

Follow-up to #2527360: Review all usages of Xss::filter(), Xss::filterAdmin(), and Html::escape()

We're using SafeMarkup::format in tests. We should not as this pollutes the safe string list and is irrelevant.

Where we are using it in $this->assertRaw() we should just convert it to a string. Where we are using it in an assertion message we should just convert it to a string with the variables inserted like "This is a $lol test".

📌 Task
Status

Closed: duplicate

Version

11.0 🔥

Component
PHPUnit 

Last updated 1 day ago

Created by

🇬🇧United Kingdom alexpott 🇪🇺🌍

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

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.

  • 🇳🇿New Zealand quietone

    Closing as a duplicate of 📌 [meta] Remove usage of t() in tests not testing translation Active and moving credit.

  • 🇳🇿New Zealand quietone

    I think I should not have closed this. I was probably focused on t() at the time but today I see uses of FormattableMarkup in tests where it could be removed. And I guess I didn't read #74.

    Sorry folks.

  • Status changed to Active 7 months ago
  • 🇷🇺Russia zniki.ru

    Thanks everyone for your effort.

    It looks like title and summary are different, as already mentioned at #51, and it's extremely difficult to understand what is happening here/
    As I understand original issue was "Remove all usage of SafeMarkup::format() and format_string() in tests apart from explicit tests of those methods".

    I checked patch #78, at the top we remove t() function.

    My suggestion:
    revert issue title to original value and close this issue, and create new one with focus to remove FormattableMarkup.

    In order to provide code contribution is better to use child issue #3227598: Remove uses of FormattableMarkup in kernel test assertions .

  • 🇳🇿New Zealand quietone

    Since SafeMarkup was replaced by FormattableMarkup I think we can continue with this issue. There is already a child issue here for Kernel tests. Another one is needed for Functional tests.

    As I understand #51, the follow up was to manage the number of changes. Since then, a separate issue has been created for Kernel test, which will help manage the scale of the changes. So, I don't think the tag is necessary any more.

  • 🇳🇿New Zealand quietone

    The current child issues have been fixed. So, time for a last check for any remaining occurrences that need to be changed.
    There are 128 usages of FormattableMarkup in tests in 32 files, according to results from the following.

    git grep FormattableMarkup | grep Test.php: | wc -l
    git grep FormattableMarkup | grep Test.php: | awk -F: '{print $1}' | sort -u | nl

    Expanding the search to 5 lines before and after results in 12 instances to check.

    $ git grep -A5 -B5 FormattableMarkup | grep Test.php: | grep -i assert | nl
         1  core/modules/comment/tests/src/Functional/CommentAdminTest.php:    $this->assertSession()->responseContains(new FormattableMarkup('@label (Original translation) - <em>The following comment translations will be deleted:</em>', ['@label' => $comment1->label()]));
         2  core/modules/comment/tests/src/Functional/CommentAdminTest.php:    $this->assertSession()->responseContains(new FormattableMarkup('@label (Original translation) - <em>The following comment translations will be deleted:</em>', ['@label' => $comment2->label()]));
         3  core/modules/contact/tests/src/Functional/ContactPersonalTest.php:    $this->assertSession()->responseContains(new FormattableMarkup('@sender_name (@sender_email) sent @recipient_name an email.', $placeholders));
         4  core/modules/field/tests/src/Functional/FunctionalString/StringFieldTest.php:    $this->assertSession()->responseContains(new FormattableMarkup('placeholder="A placeholder on @widget_type"', ['@widget_type' => $widget_type]));
         5  core/modules/system/tests/src/Functional/Form/FormTest.php:            $this->assertSession()->responseContains(new FormattableMarkup($message, $placeholders));
         6  core/modules/system/tests/src/Functional/Form/FormTest.php:            $this->assertSession()->responseNotContains(new FormattableMarkup($message, $placeholders));
         7  core/modules/taxonomy/tests/src/Functional/VocabularyUiTest.php:    $this->assertSession()->responseContains(new FormattableMarkup('Are you sure you want to delete the vocabulary %name?', ['%name' => $edit['name']]));
         8  core/modules/taxonomy/tests/src/Functional/VocabularyUiTest.php:    $this->assertSession()->responseContains(new FormattableMarkup('Deleted vocabulary %name.', ['%name' => $edit['name']]));
         9  core/modules/views/tests/src/Functional/Plugin/DisplayTest.php:    $this->assertEquals(new FormattableMarkup('The %relationship_name relationship used in %handler_type %handler is not present in the %display_name display.', ['%relationship_name' => 'uid', '%handler_type' => 'field', '%handler' => 'User: Last login', '%display_name' => 'Default']), $errors['default'][0]);
        10  core/modules/views/tests/src/Functional/Plugin/DisplayTest.php:    $this->assertEquals(new FormattableMarkup('The %relationship_name relationship used in %handler_type %handler is not present in the %display_name display.', ['%relationship_name' => 'uid', '%handler_type' => 'field', '%handler' => 'User: Created', '%display_name' => 'Default']), $errors['default'][1]);
        11  core/tests/Drupal/KernelTests/Component/Render/FormattableMarkupKernelTest.php:    $this->assertSame($expected, (string) new FormattableMarkup($string, $args));
        12  core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php:  public function testIsStringAssertionWithFormattableMarkup(): void {
    
  • 🇳🇿New Zealand quietone

    Searching for remaining usages with the following. I then struck out the ones that are testing the API

    $ git grep FormattableMarkup | grep -v "Test\.php:\s*\* " | grep Test.php: | awk -F: '{print $1}' | sort -u | nl
         1  core/modules/system/tests/src/Functional/System/ErrorHandlerTest.php
         2  core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php
         3  <del>core/modules/user/tests/modules/user_hooks_test/src/Hook/UserHooksTest.php</del>
         4  core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php
         5 <del> core/tests/Drupal/KernelTests/Component/Render/FormattableMarkupKernelTest.php</del>
         6  core/tests/Drupal/KernelTests/Core/Routing/ExceptionHandlingTest.php
         7  core/tests/Drupal/KernelTests/Core/Test/Comparator/MarkupInterfaceComparatorTest.php
         8  <del>core/tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php</del>
         9  <del>core/tests/Drupal/Tests/Component/Render/FormattableMarkupTest.php</del>
        10  <del>core/tests/Drupal/Tests/Component/Render/PlainTextOutputTest.php</del>
        11  core/tests/Drupal/Tests/Core/Config/Checkpoint/LinearHistoryTest.php
        12  <del>core/tests/Drupal/Tests/Core/Logger/LogMessageParserTest.php</del>
        13  core/tests/Drupal/Tests/Core/Mail/Plugin/Mail/SymfonyMailerTest.php
        14  <del>core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php</del>
    
  • Merge request !12434unnecessary uses of FormattableMarkup → (Open) created by quietone
  • Pipeline finished with Failed
    7 days ago
    Total: 221s
    #527817
  • 🇳🇿New Zealand quietone

    to check

    1. core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php
    2. core/tests/Drupal/Tests/Core/Logger/LogMessageParserTest.php
  • Pipeline finished with Failed
    7 days ago
    Total: 2628s
    #527833
  • 🇳🇿New Zealand quietone

    There is one failing test, \Drupal\FunctionalTests\Bootstrap\UncaughtExceptionTest::testUncaughtFatalError, which passes locally for different versions of PHP. Not sure why it is failing here though.

    Setting to NR because the existing changes can be reviewed as well as checking if there are other instances that need to change.

Production build 0.71.5 2024