Problem/Motivation

There is no need to use t() in tests, unless we're testing translations, however in core we do not follow this consistently, which does not set a good example for new contributions.

In #3133726: [meta] Remove usage of t() in tests not testing translation we identified there are severals of calls to t() in calls to assertTrue() and assertFalse() and that removing all these in one go seems to be a suitable way of attacking this problem.

Proposed resolution

Identify and remove all calls to t() wrapped in calls to assertTrue() and assertFalse(), except those used by translation-related code (if any).

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

Hardik_Patel_12 created an issue. See original summary.

hardik_patel_12’s picture

Status: Active » Needs review
StatusFileSize
new9 KB

Kindly review a patch.

hardik_patel_12’s picture

Issue tags: +Deprecated assertions
mondrake’s picture

Issue tags: -Deprecated assertions
longwave’s picture

Status: Needs review » Needs work

The patch looks pretty good but there are a number of calls to t() in assertTrue() in core/modules/language/tests/src/Functional/LanguageSwitchingTest.php which can also be removed here.

ravi.shankar’s picture

Assigned: Unassigned » ravi.shankar

Will work on this.

ravi.shankar’s picture

Assigned: ravi.shankar » Unassigned
Status: Needs work » Needs review
StatusFileSize
new20.15 KB
new11.16 KB

Addressed comment #5.

longwave’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/comment/tests/src/Functional/CommentInterfaceTest.php
    @@ -101,7 +101,7 @@ public function testCommentInterface() {
    -    $this->assertTrue($comment->getAuthorName() == t('Anonymous') && $comment->getOwnerId() == 0, 'Comment author successfully changed to anonymous.');
    +    $this->assertTrue($comment->getAuthorName() == 'Anonymous' && $comment->getOwnerId() == 0, 'Comment author successfully changed to anonymous.');
    

    This should be two separate assertions, maybe considered out of scope but unsure when else we would fix this.

  2. +++ b/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php
    @@ -316,17 +316,17 @@ protected function doTestLanguageLinkActiveClassAuthenticated() {
    -    $this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode has the correct attributes that will allow the drupal.active-link library to mark it as active.', [':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode]));
    +    $this->assertTrue(isset($links[0]), 'A link generated by :function to the current :language page with langcode :langcode has the correct attributes that will allow the drupal.active-link library to mark it as active.', [':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode]);
    

    The placeholders need removing, the strings can be directly inserted.

  3. +++ b/core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php
    @@ -379,7 +379,7 @@ public function testFieldUpdateFailure() {
    -      $this->assertTrue($schema->tableExists($table_name), t('Table %table exists.', ['%table' => $table_name]));
    +      $this->assertTrue($schema->tableExists($table_name), 'Table %table exists.', ['%table' => $table_name]);
    

    Same here.

  4. +++ b/core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php
    @@ -405,8 +405,8 @@ public function testFieldUpdateIndexesWithData() {
    -      $this->assertFalse(Database::getConnection()->schema()->indexExists($table, 'value'), t("No index named value exists in @table", ['@table' => $table]));
    -      $this->assertFalse(Database::getConnection()->schema()->indexExists($table, 'value_format'), t("No index named value_format exists in @table", ['@table' => $table]));
    +      $this->assertFalse(Database::getConnection()->schema()->indexExists($table, 'value'), "No index named value exists in @table", ['@table' => $table]);
    +      $this->assertFalse(Database::getConnection()->schema()->indexExists($table, 'value_format'), "No index named value_format exists in @table", ['@table' => $table]);
    

    And here, etc

  5. +++ b/core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php
    @@ -51,7 +51,7 @@ public function testFileCheckLocalDirectoryHandling() {
    -    $this->assertTrue($file_system->mkdir($child_path, 0775, TRUE), t('No error reported when creating new local directories.'), 'File');
    +    $this->assertTrue($file_system->mkdir($child_path, 0775, TRUE), 'No error reported when creating new local directories.', 'File');
    

    The 'File' parameter can be removed entirely as well while we are here.

deepak goyal’s picture

I am working on this

deepak goyal’s picture

Status: Needs work » Needs review
StatusFileSize
new21.02 KB
new19.94 KB

Hi @longwave Made changes as you suggested please review.

longwave’s picture

Status: Needs review » Needs work
+++ b/core/modules/comment/tests/src/Functional/CommentInterfaceTest.php
@@ -101,7 +101,10 @@ public function testCommentInterface() {
+    $this->assertTrue($comment->getAuthorName() == 'Anonymous', 'Comment
+    author successfully changed to anonymous.');
+    $this->assertTrue($comment->getOwnerId() == 0, 'Comment author
+    successfully changed to anonymous.');
 

+++ b/core/modules/field/tests/src/Functional/FormTest.php
@@ -266,7 +266,8 @@ public function testFieldFormUnlimited() {
+    $this->assertTrue(isset($elements[0]), 'aria-describedby attribute is
+    properly placed on multiple value widgets.');

+++ b/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php
@@ -272,12 +272,14 @@ public function testLanguageBodyClass() {
+    $this->assertTrue(isset($class[0]), 'The path-admin class appears on
+    default language.');
...
+    $this->assertTrue(isset($class[0]), 'The path-admin class same as on
+    default language.');

All these (and more) need unwrapping so they are on a single line.

suresh prabhu parkala’s picture

Status: Needs work » Needs review
StatusFileSize
new19.22 KB
new19 KB

Please review!

paulocs’s picture

Assigned: Unassigned » paulocs
Status: Needs review » Needs work
Issue tags: +Needs reroll

Patch needs re-roll.
I will do it.

paulocs’s picture

Status: Needs work » Needs review
StatusFileSize
new18.24 KB

New patch.

Status: Needs review » Needs work

The last submitted patch, 14: 3153150-14.patch, failed testing. View results

paulocs’s picture

Assigned: paulocs » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new1.37 KB
new18.23 KB

Fixing errors.

longwave’s picture

Status: Needs review » Needs work

One more change to go:

core/modules/search/tests/src/Functional/SearchNodeUpdateAndDeletionTest.php
121:    $this->assertFalse($search_index_dataset, t('Node info successfully removed from search_index'));
suresh prabhu parkala’s picture

Status: Needs work » Needs review
StatusFileSize
new19.06 KB
new681 bytes

Updated patch. Please review!

paulocs’s picture

Status: Needs review » Reviewed & tested by the community

Patch #18 looks good to me. I did not find any assertTrue() or assertFalse() with a t() call inside.

Cheers, Paulo.

  • catch committed 3ab889a on 9.1.x
    Issue #3153150 by Suresh Prabhu Parkala, paulocs, ravi.shankar, Deepak...
catch’s picture

Status: Reviewed & tested by the community » Fixed
+++ b/core/modules/comment/tests/src/Functional/CommentInterfaceTest.php
@@ -101,7 +101,8 @@ public function testCommentInterface() {
     $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['uid' => '']);
-    $this->assertTrue($comment->getAuthorName() == t('Anonymous') && $comment->getOwnerId() == 0, 'Comment author successfully changed to anonymous.');
+    $this->assertTrue($comment->getAuthorName() == 'Anonymous', 'Comment author successfully changed to anonymous.');
+    $this->assertTrue($comment->getOwnerId() == 0, 'Comment author successfully changed to anonymous.');
 

These could change to assertSame(), then we'd be able to remove the assertion message too. Out of scope here but could be done in a follow-up.

Committed 3ab889a and pushed to 9.1.x. Thanks!

Status: Fixed » Closed (fixed)

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