Problem/Motivation

Spotted in #3176200: Remove more uses of t() in assertNoText() we have assertions like this:

core/modules/views_ui/tests/src/Functional/FieldUITest.php
35:    $this->assertNoText('Views test: Name [' . t('hidden') . ']');

The call to t() does nothing (except in the special case that translations are being tested).

Steps to reproduce

Proposed resolution

Remove the calls to t() and use plain strings where possible.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

longwave created an issue. See original summary.

longwave’s picture

Status: Active » Needs review
FileSize
17.42 KB
longwave’s picture

mondrake’s picture

Status: Needs review » Needs work
+++ b/core/modules/system/tests/src/Functional/Form/ElementsLabelsTest.php
@@ -91,9 +91,9 @@ public function testFormLabels() {
-    $this->assertEqual($elements[0]->getAttribute('title'), 'Checkboxes test' . ' (' . t('Required') . ')', 'Title attribute found.');
+    $this->assertEqual($elements[0]->getAttribute('title'), 'Checkboxes test' . ' (Required)', 'Title attribute found.');
...
-    $this->assertEqual($elements[0]->getAttribute('title'), 'Radios test' . ' (' . t('Required') . ')', 'Title attribute found.');
+    $this->assertEqual($elements[0]->getAttribute('title'), 'Radios test' . ' (Required)', 'Title attribute found.');

Looks like we can have just a string here for expected, further avoiding concatenation. Also maybe since we touch here we can switch to assertEquals and remove usage of deprecated assertEqual.

longwave’s picture

I also missed quite a few

core/modules/field_ui/tests/src/Functional/ManageDisplayTest.php:    $this->verbose(t('Rendered node - view mode: @view_mode', ['@view_mode' => $view_mode]) . '<hr />' . $output);
core/modules/update/tests/src/Functional/UpdateContribTest.php:    $this->assertRaw('<div class="project-update__status">' . t('Failed to get available update data'));
core/modules/update/tests/src/Functional/UpdateCoreTest.php:    $this->assertNoRaw('<li>' . t('There is a security update available for your version of Drupal.'));
core/modules/locale/tests/src/Functional/LocaleUpdateInterfaceTest.php:    $update_button = $this->xpath('//input[@type="submit"][@value="' . t('Update translations') . '"]');
core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php:    return t('Save') . $this->getFormSubmitSuffix($entity, $langcode);
core/modules/file/tests/src/Functional/SaveUploadTest.php:    $message = t('Only files with the following extensions are allowed:') . ' <em class="placeholder">' . $extensions . '</em>';
core/modules/file/tests/src/Functional/SaveUploadTest.php:    $message = t('For security reasons, your upload has been renamed to') . ' <em class="placeholder">' . $this->phpfile->filename . '_.txt' . '</em>';
core/modules/file/tests/src/Functional/SaveUploadTest.php:    $message = t('For security reasons, your upload has been renamed to') . ' <em class="placeholder">' . $this->phpfile->filename . '_.txt' . '</em>';
core/modules/file/tests/src/Functional/SaveUploadTest.php:    $message = t('Only files with the following extensions are allowed:') . ' <em class="placeholder">' . $edit['extensions'] . '</em>';
core/modules/file/tests/src/Functional/SaveUploadTest.php:    $message = t('Only files with the following extensions are allowed:') . ' <em class="placeholder">' . $edit['extensions'] . '</em>';
core/modules/file/tests/src/Functional/SaveUploadFormTest.php:    $message = t('Only files with the following extensions are allowed:') . ' <em class="placeholder">' . $extensions . '</em>';
core/modules/file/tests/src/Functional/SaveUploadFormTest.php:    $message = t('For security reasons, your upload has been renamed to') . ' <em class="placeholder">' . $this->phpfile->filename . '_.txt' . '</em>';
core/modules/file/tests/src/Functional/SaveUploadFormTest.php:    $this->assertRaw(t('One or more files could not be uploaded.') . '<div class="item-list">');
core/modules/views/tests/src/Kernel/ModuleTest.php:    $description_top = '<p>' . t('The handler for this item is broken or missing. The following details are available:') . '</p>';
core/modules/views/tests/src/Kernel/ModuleTest.php:    $description_bottom = '<p>' . t('Enabling the appropriate module may solve this issue. Otherwise, check to see if there is a module update available.') . '</p>';
core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php:    $expected = t('- Restricted access -') . ' (' . $this->referencedEntities[0]->id() . ')';
core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php:    $expected .= ', ' . t('- Restricted access -') . ' (' . $this->referencedEntities[1]->id() . ')';
ravi.shankar’s picture

Assigned: Unassigned » ravi.shankar
ravi.shankar’s picture

Assigned: ravi.shankar » Unassigned
Status: Needs work » Needs review
FileSize
28.6 KB
12.84 KB

I have addressed comments #4 and #5, please review.

longwave’s picture

  1. +++ b/core/modules/file/tests/src/Functional/SaveUploadFormTest.php
    @@ -166,7 +166,7 @@ public function testHandleExtension() {
    +    $message = 'Only files with the following extensions are allowed: <em class="placeholder">' . $extensions . '</em>';
         $this->assertRaw($message);
    

    I think we should inline the variable in this test and SaveUploadTest.

  2. +++ b/core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php
    @@ -336,10 +336,10 @@ public function testEntityAutocompleteAccess() {
    +    $expected = '- Restricted access - (' . $this->referencedEntities[0]->id() . ')';
         $this->assertEqual($form['single_access']['#value'], $expected);
    ...
    +    $expected .= ', - Restricted access - (' . $this->referencedEntities[1]->id() . ')';
         $this->assertEqual($form['tags_access']['#value'], $expected);
    

    Again we can just inline the variable here.

ravi.shankar’s picture

Made changes as per comment #8, please review.

longwave’s picture

Status: Needs review » Needs work

There are more instances of:

     $this->assertRaw($message);

I think we should inline all the ones changed in SaveUploadTest in this patch.

mondrake’s picture

+++ b/core/modules/system/tests/src/Functional/Form/ElementsLabelsTest.php
@@ -91,9 +91,9 @@ public function testFormLabels() {
-    $this->assertEqual($elements[0]->getAttribute('title'), 'Checkboxes test' . ' (' . t('Required') . ')', 'Title attribute found.');
+    $this->assertEquals($elements[0]->getAttribute('title'), 'Checkboxes test (Required)', 'Title attribute found.');
     $elements = $this->xpath('//div[@id="edit-form-radios-title-attribute"]');
-    $this->assertEqual($elements[0]->getAttribute('title'), 'Radios test' . ' (' . t('Required') . ')', 'Title attribute found.');
+    $this->assertEquals($elements[0]->getAttribute('title'), 'Radios test (Required)', 'Title attribute found.');

Please, either leave $this->assertEqual, or when converting to $this->assertEquals, note that the expected and the actual arguments should be swapped.

mondrake’s picture

IDK, but in a small patch like this, IMHO we should also convert assertText and assertRaw (that are deprecated since D 8.2.0!) to their WebAssert equivalents. The conversion of those will be tough and big, so whatever we can do to anticipate some conversions, will help later. My opinion though: please let's discuss before jumping to code.

longwave’s picture

Re #12 I also think doing those conversions here is worthwhile, I guess this is a small enough patch to try it here and see if core committers agree? If we do do this, we should only touch lines that would be touched by the patch anyway, no matter how tempting it is to scope creep into the next line even if it seems trivial.

mondrake’s picture

+1 for #13!

longwave’s picture

Status: Needs work » Needs review
FileSize
30.02 KB

Addressed #10-#13. No interdiff as pretty much every line has changed again.

I think I am perhaps taking liberties with this, but it's a good a time as any to do it:

     // Make sure duplicate messages don't appear on Update status pages.
     $this->drupalGet('admin/reports/status');
-    // We're expecting "There is a security update..." inside the status report
-    // itself, but the message from
-    // \Drupal\Core\Messenger\MessengerInterface::addStatus() appears as an li
-    // so we can prefix with that and search for the raw HTML.
-    $this->assertNoRaw('<li>' . t('There is a security update available for your version of Drupal.'));
+    $this->assertSession()->pageTextContainsOnce('There is a security update available for your version of Drupal.');
mondrake’s picture

Status: Needs review » Needs work
+++ b/core/modules/system/tests/src/Functional/Form/ElementsLabelsTest.php
@@ -91,9 +91,9 @@ public function testFormLabels() {
     $elements = $this->xpath('//div[@id="edit-form-checkboxes-title-attribute"]');
-    $this->assertEqual($elements[0]->getAttribute('title'), 'Checkboxes test' . ' (' . t('Required') . ')', 'Title attribute found.');
+    $this->assertEquals('Checkboxes test (Required)', $elements[0]->getAttribute('title'));
     $elements = $this->xpath('//div[@id="edit-form-radios-title-attribute"]');
-    $this->assertEqual($elements[0]->getAttribute('title'), 'Radios test' . ' (' . t('Required') . ')', 'Title attribute found.');
+    $this->assertEquals('Radios test (Required)', $elements[0]->getAttribute('title'));
 
     $elements = $this->xpath('//fieldset[@id="edit-form-checkboxes-title-invisible--wrapper"]/legend/span[contains(@class, "visually-hidden")]');

I think we can use WebAssert methods here like elementExists and elementAttributeContains

+++ b/core/modules/field_ui/tests/src/Functional/ManageDisplayTest.php
@@ -274,7 +274,7 @@ public function assertNodeViewTextHelper(EntityInterface $node, $view_mode, $tex
     $output = (string) \Drupal::service('renderer')->renderRoot($element);
-    $this->verbose(t('Rendered node - view mode: @view_mode', ['@view_mode' => $view_mode]) . '<hr />' . $output);
+    $this->verbose('Rendered node - view mode: ' . $view_mode . '<hr />' . $output);
 

verbose is actually a no-op in PHPUnit, in other issues we just removed calls to it

Pooja Ganjage’s picture

Hi,

Creating a patch as suggested in #16 comment.

Please review the patch.

Thanks.

longwave’s picture

Status: Needs work » Needs review
FileSize
2.16 KB
36.74 KB

#17 does not apply.

Addressed #16 from #15.

longwave’s picture

FileSize
30.13 KB
2.16 KB

Uploaded the wrong files, let's try again.

mondrake’s picture

Status: Needs review » Reviewed & tested by the community

Thank you

  • catch committed 9e8c051 on 9.2.x
    Issue #3184493 by longwave, ravi.shankar, Pooja Ganjage, mondrake:...

  • catch committed 6d4eefc on 9.1.x
    Issue #3184493 by longwave, ravi.shankar, Pooja Ganjage, mondrake:...
catch’s picture

Version: 9.2.x-dev » 9.1.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 9.2.x and cherry-picked to 9.1.x, thanks!

Status: Fixed » Closed (fixed)

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