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 setLabel() and setDescription() 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 setLabel() and setDescription(), 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
new8.31 KB

Kindly review a new patch.

hardik_patel_12’s picture

Issue tags: +Deprecated assertions
mondrake’s picture

Issue tags: -Deprecated assertions
siddhant.bhosale’s picture

Assigned: Unassigned » siddhant.bhosale
siddhant.bhosale’s picture

Assigned: siddhant.bhosale » Unassigned
Status: Needs review » Reviewed & tested by the community

Hi, the patch applies cleanly, and also the tests are run successfully.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

find ./core -type f -name "*Test*.php" -exec grep -E 'setLabel.*t\(' {} + Tue 14 Jul 10:22:53 2020
./core/modules/comment/tests/modules/comment_base_field_test/src/Entity/CommentTestBaseField.php: ->setLabel(t('A comment field'))
./core/modules/language/tests/language_test/src/Entity/NoLanguageEntityTest.php: ->setLabel(t('ID'))
./core/modules/language/tests/language_test/src/Entity/NoLanguageEntityTest.php: ->setLabel(t('UUID'))
./core/modules/field/tests/modules/field_test/src/Plugin/Field/FieldType/TestObjectItem.php: ->setLabel(t('Value'))
./core/modules/field/tests/modules/field_test/src/Plugin/Field/FieldType/TestItem.php: ->setLabel(t('Test integer value'))
./core/modules/system/tests/modules/entity_test_revlog/src/Entity/EntityTestWithRevisionLog.php: ->setLabel(t('Name'))
./core/modules/system/tests/modules/entity_test_update/src/Plugin/Field/FieldType/MultiValueTestItem.php: ->setLabel(t('First value'));
./core/modules/system/tests/modules/entity_test_update/src/Plugin/Field/FieldType/MultiValueTestItem.php: ->setLabel(t('Second value'));
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestDefaultValue.php: ->setLabel(t('Some custom description'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestFieldMethods.php: ->setLabel(t('Test field method invocation order.'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMapField.php: ->setLabel(t('Data'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestConstraintViolation.php: ->setLabel(t('Test field'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulRev.php: ->setLabel(t('Non translatable'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestBaseFieldDisplay.php: ->setLabel(t('Field with no display'));
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestBaseFieldDisplay.php: ->setLabel(t('Field with configurable display'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestBaseFieldDisplay.php: ->setLabel(t('Field with non-configurable display'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestBaseFieldDisplay.php: ->setLabel(t('A field with multiple values'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulRevChanged.php: ->setLabel(t('Revision ID'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulRevChanged.php: ->setLabel(t('Revision translation affected'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestStringId.php: ->setLabel(t('ID'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestConstraints.php: ->setLabel(t('Changed'));
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulChanged.php: ->setLabel(t('Changed'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulChanged.php: ->setLabel(t('Non translatable'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulDefaultValue.php: ->setLabel(t('Some custom description'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestWithBundle.php: ->setLabel(t('Name'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestRev.php: ->setLabel(t('Non Revisionable Field'))
./core/modules/system/tests/src/Functional/Entity/Traits/EntityDefinitionTestTrait.php: $definitions['new_base_field']->setLabel(t('A new base field'));
./core/modules/system/tests/src/Functional/Entity/Traits/EntityDefinitionTestTrait.php: ->setLabel(t('A new long-named base field'))
./core/modules/system/tests/src/Functional/Entity/Traits/EntityDefinitionTestTrait.php: ->setLabel(t('A new revisionable base field'))
./core/modules/system/tests/src/Functional/Entity/Traits/EntityDefinitionTestTrait.php: ->setLabel(t('A new bundle field'))

And

find ./core -type f -name "*Test*.php" -exec grep -E 'setDescription.*t\(' {} +                                                                                                                                                           783ms  Tue 14 Jul 10:23:17 2020
./core/modules/language/tests/language_test/src/Entity/NoLanguageEntityTest.php:      ->setDescription(t('The ID of the test entity.'))
./core/modules/language/tests/language_test/src/Entity/NoLanguageEntityTest.php:      ->setDescription(t('The UUID of the test entity.'))
./core/modules/system/tests/modules/entity_test_revlog/src/Entity/EntityTestWithRevisionLog.php:      ->setDescription(t('The name of the test entity.'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMapField.php:      ->setDescription(t('A serialized array of additional data.'));
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulRev.php:      ->setDescription(t('A non-translatable string field'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulRevChanged.php:      ->setDescription(t('The version id of the test entity.'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulRevChanged.php:      ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestStringId.php:      ->setDescription(t('The ID of the test entity.'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulChanged.php:      ->setDescription(t('The time that the entity was last edited.'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulChanged.php:      ->setDescription(t('A non-translatable string field'));
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestWithBundle.php:      ->setDescription(t('The name of the test entity.'))
./core/modules/system/tests/modules/entity_test/src/Entity/EntityTestRev.php:      ->setDescription(t('A non-revisionable test field.'))

I think instances where this is needed should be commented and we should also take the opportunity to consider whether the test code in entity_test should use t() too.

snehalgaikwad’s picture

Status: Needs work » Needs review
StatusFileSize
new29.14 KB
paulocs’s picture

StatusFileSize
new38.22 KB

Replace t() in some more places that was missing.

paulocs’s picture

StatusFileSize
new7.5 KB

Interdiff #8 and #9

longwave’s picture

Status: Needs review » Reviewed & tested by the community

I reviewed the patch with git diff --color-words and only t() wrappers are removed.

I also checked for missed items with rg 'setLabel\(t\('|rg -i test and rg 'setDescription\(t\('|rg -i test and didn't find any.

Therefore this is RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 9: 3153168-9.patch, failed testing. View results

longwave’s picture

Status: Needs work » Reviewed & tested by the community
alexpott’s picture

So we have #3157938: Use t() for #title and #description in tests and test modules which is going in the other direction. Not sure what's correct here. I discovered recently that the PO extractor ignores all of these strings so maybe the argument that we should be consistent with real runtime code in tests and set a good example for copy and paste.

alexpott’s picture

FWIW I don't think this t() usage is the same as remove t() from an assertion message. I'm not really sure this should be part of #3133726: [meta] Remove usage of t() in tests not testing translation

andypost’s picture

-1 here because I often using test modules to show examples of how something could be used

Instead of that I'd removed t() from all entity classes (baseFieldDefinitions() at least because having t() here really confusing).
Moreover both methods setLabel() and setDescription() could check is passed in string or translatable object and in case of strings wrap it into translatable class... but it will need to tune potx to extract this strings...

longwave’s picture

> Moreover both methods setLabel() and setDescription() could check is passed in string or translatable object

Maybe we should go further and only allow TranslatableMarkup to be passed in? ie. add a deprecation warning if it's a string and then use a typehint in D10?

andypost’s picture

++ to #17 and set proper type to arguments in d10

longwave’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.64 KB

Let's try #17 though I think this is going to fail horribly somewhere, we might need a way to bypass this in some cases.

Status: Needs review » Needs work

The last submitted patch, 19: 3153168-test-only.patch, failed testing. View results

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Component: phpunit » other
Issue tags: +Coding standards

Tagging for coding standards and moving to the 'other' component where such issues live.

chaitanyadessai’s picture

Status: Needs work » Needs review
StatusFileSize
new64.98 KB

Removed t() calls from setLabel() and setDescription().

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative
StatusFileSize
new165.13 KB
new143.36 KB

@chaitanyadessai please include an interdiff with your patches

Doing a search for ->setLabel(t(
Showed more instances from test files I don't think are related to revisions

set label

Same for setDescription

description.

adeshsharma’s picture

Status: Needs work » Needs review
StatusFileSize
new96.65 KB
new32.98 KB

Removed t calls.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Seems all instances have been removed. So guess we weren't testing translation of either of those. Should we open a follow up to add testing?

longwave’s picture

Status: Reviewed & tested by the community » Needs work

This still needs discussion as to whether we should actually do it, as per #14 through #18.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

longwave’s picture

Status: Needs work » Needs review

Thinking this through some more after working on the final t() removals in tests in #3410128: Remove remaining unnecessary uses of t() in tests, I now think this is won't fix - we need test coverage of TranslatableMarkup passed through to various common APIs like render, form and field API and test coverage should provide good examples to use as well.

Marking "needs review" for this comment, hiding all other patches.

longwave’s picture

Explaining a bit further:

In real-world implementations of setLabel() and similar methods we use t(). By removing t() in tests we are not really testing the same way a real-world implementation would, if we only used raw strings in tests then how would be sure that the code actually works with translatable strings?

Also, tests are sometimes used as examples of features and the code is copy-pasted and then edited and used for real. If we drop t() here we risk people copy-pasting and not following the best practice of adding translatable labels and descriptions.

It's similar to when we have a test form. In most test form implementations we still wrap form element labels in t() because that's what a real world implementation of a form should do.

smustgrave’s picture

Status: Needs review » Closed (won't fix)

Explanation makes sense, anyone really disagrees please reopne.