Problem/Motivation

Status

This is a follow-up to #500866: [META] remove t() from assert message. The changes in that issue have already been accepted and committed to 8.0.x.

pass() and fail() are simple wrappers for assert(), and as such all the discussion in the meta issue about removing t() from assert messages applies directly to pass() and fail(). The meta issue did not explicitly address pass() and fail() - as a result, some of the referenced patches fixed the use of t() in pass() and fail() but many did not. This issue intends to clean up those missed cases (43 by my count) where t() is still used in pass() and fail().

Details

A brief recap of the meta issue:

  • The $message parameter of SimpleTest assertions (e.g., DrupalTestCase::assertTrue()) is a string displayed only in the administrative UI or on the commandline following test runs, and on testbot.
  • This parameter is never translated, so using t() on it is needless overhead.
  • We have other instances in core where string function parameters should not be wrapped in t(), e.g. watchdog().
  • When the assertion message is a plain string, t() should simply be omitted.
  • When the assertion message contains placeholders for variables, SafeMarkup::format() should be used instead.

Proposed resolution

  • Strip t() from plain-string assertion messages that are still using it.
    • $this->fail(t('My custom message here'));
      $this->pass(t('My custom message here'));
      

      Becomes:

      $this->fail('My custom message here');
      $this->pass('My custom message here');
      
    • Assertion messages that have placeholders or variables will be converted to SafeMarkup::format().
    • Other uses of t() in automated tests are not changed.

Comments

TR created an issue. See original summary.

tr’s picture

Status: Active » Needs review
StatusFileSize
new15.47 KB

The patch ...

geertvd’s picture

Status: Needs review » Needs work

Just 1 nitpick:

+++ b/core/modules/simpletest/src/Tests/SimpleTestTest.php
@@ -189,7 +190,7 @@ function stubTest() {
+    $this->pass(SafeMarkup::format('Test ID is @id.', array('@id' => $this->testId)));

We could just concatenate that string so we don't have to use SafeMarkup::format() in a test.

tr’s picture

Status: Needs work » Needs review
StatusFileSize
new15.43 KB

Changed
$this->pass(SafeMarkup::format('Test ID is @id.', array('@id' => $this->testId)));
to
$this->pass('Test ID is ' . $this->testId);

Status: Needs review » Needs work

The last submitted patch, 4: 2555145-4.patch, failed testing.

tr’s picture

Status: Needs work » Needs review
StatusFileSize
new15.43 KB

Changed
$this->pass('Test ID is ' . $this->testId);
to
$this->pass('Test ID is ' . $this->testId . '.');

TR queued 6: 2555145-6.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 6: 2555145-6.patch, failed testing.

tr’s picture

Assigned: Unassigned » tr
Status: Needs work » Needs review
StatusFileSize
new15.44 KB

Rerolled patch against latest HEAD. No code changes, just offsets in the patch.

tr’s picture

StatusFileSize
new15.26 KB

Re-roll against current head.

tr’s picture

Still applies, still passes tests ...

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mile23’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

Won't apply to 8.1.x.

kostyashupenko’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new16.29 KB

Reroll of #10

mile23’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/field/tests/src/Kernel/FieldCrudTest.php
    @@ -113,21 +113,21 @@ function testCreateField() {
    -      FieldConfig::create($this->fieldDefinition)->save();
    -      $this->fail(t('Cannot create two fields with the same field / bundle combination.'));
    +      entity_create('field_config', $this->fieldDefinition)->save();
    +      $this->fail('Cannot create two fields with the same field / bundle combination.');
    ...
    -      FieldConfig::create($this->fieldDefinition)->save();
    -      $this->fail(t('Cannot create a field with a non-existing storage.'));
    +      entity_create('field_config', $this->fieldDefinition)->save();
    +      $this->fail('Cannot create a field with a non-existing storage.');
    
    +++ b/core/modules/field/tests/src/Kernel/FieldStorageCrudTest.php
    @@ -77,11 +77,11 @@ function testCreate() {
    -      FieldStorageConfig::create($field_storage_definition)->save();
    -      $this->fail(t('Cannot create two fields with the same name.'));
    +      entity_create('field_storage_config', $field_storage_definition)->save();
    +      $this->fail('Cannot create two fields with the same name.');
    
    @@ -90,11 +90,11 @@ function testCreate() {
    -      FieldStorageConfig::create($field_storage_definition)->save();
    -      $this->fail(t('Cannot create a field with no type.'));
    +      entity_create('field_storage_config', $field_storage_definition)->save();
    +      $this->fail('Cannot create a field with no type.');
    
    @@ -103,11 +103,11 @@ function testCreate() {
    -      FieldStorageConfig::create($field_storage_definition)->save();
    -      $this->fail(t('Cannot create an unnamed field.'));
    +      entity_create('field_storage_config', $field_storage_definition)->save();
    +      $this->fail('Cannot create an unnamed field.');
    
    @@ -129,11 +129,11 @@ function testCreate() {
    -      FieldStorageConfig::create($field_storage_definition)->save();
    -      $this->fail(t('Cannot create a field with a name starting with a digit.'));
    +      entity_create('field_storage_config', $field_storage_definition)->save();
    +      $this->fail('Cannot create a field with a name starting with a digit.');
    
    @@ -143,11 +143,11 @@ function testCreate() {
    -      FieldStorageConfig::create($field_storage_definition)->save();
    -      $this->fail(t('Cannot create a field with a name containing an illegal character.'));
    +      entity_create('field_storage_config', $field_storage_definition)->save();
    +      $this->fail('Cannot create a field with a name containing an illegal character.');
    
    @@ -157,11 +157,11 @@ function testCreate() {
    -      FieldStorageConfig::create($field_storage_definition)->save();
    -      $this->fail(t('Cannot create a field with a name longer than 32 characters.'));
    +      entity_create('field_storage_config', $field_storage_definition)->save();
    +      $this->fail('Cannot create a field with a name longer than 32 characters.');
    
    @@ -172,11 +172,11 @@ function testCreate() {
    -      FieldStorageConfig::create($field_storage_definition)->save();
    -      $this->fail(t('Cannot create a field bearing the name of an entity key.'));
    +      entity_create('field_storage_config', $field_storage_definition)->save();
    +      $this->fail('Cannot create a field bearing the name of an entity key.');
    
    +++ b/core/modules/node/src/Tests/NodeCreationTest.php
    @@ -86,11 +86,11 @@ function testFailedPageCreation() {
    -      Node::create($edit)->save();
    -      $this->fail(t('Expected exception has not been thrown.'));
    +      entity_create('node', $edit)->save();
    +      $this->fail('Expected exception has not been thrown.');
    

    entity_create() and so forth have been deprecated, and so we want to keep the EntityType::create() methods in place.

  2. +++ b/core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php
    @@ -120,10 +120,10 @@ function testDependencyResolution() {
    -    catch (MissingDependencyException $e) {
    -      $this->pass(t('ModuleInstaller::install() throws an exception if dependencies are missing.'));
    +    catch (\Drupal\Core\Extension\MissingDependencyException $e) {
    +      $this->pass('ModuleInstaller::install() throws an exception if dependencies are missing.');
    

    We don't need the fully qualified class name here for the exception. MissingDependencyException has a use statement at the top of the file.

  3. +++ b/core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php
    @@ -297,7 +297,7 @@ function testTransactionWithDdlStatement() {
             // @TODO: an exception should be triggered here, but is not, because
             // "ROLLBACK" fails silently in MySQL if there is no transaction active.
    -        // $this->fail(t('Rolling back a transaction containing DDL should fail.'));
    +        // $this->fail('Rolling back a transaction containing DDL should fail.');
    

    Fixing the commented code! :-)

    I couldn't find an issue for the @todo anywhere, so I made one: #2736777: MySQL on PHP 8 now errors when committing or rolling back when there is no active transaction

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

tr’s picture

Version: 8.6.x-dev » 8.8.x-dev
Component: simpletest.module » phpunit
Status: Needs work » Needs review
StatusFileSize
new15.58 KB

The Re-roll in #14 introduced lots of regressions - instead of being a simple re-roll to match changes in core, it REVERSED the core changes to make the patch apply.

Here is a correct re-roll of #10 so that it applies to the current HEAD. None of the concerns pointed out in #15 are applicable any more, as they were all the result of the reverted core changes in #14.

The only changes to the patch #10 are:
1) Line numbers / context lines corrected to agree with HEAD
2) Test file names corrected to agree with HEAD

mile23’s picture

#15.3 still applies... Edit the @todo so it points to the issue.

tr’s picture

StatusFileSize
new15.77 KB

OK. I read #15.3 as a statement that you created an issue for something you noticed. I don't see where you requested modification of the @todo.

I changed the @todo to reference the newly-created issue. Seems a little out of scope though ...

oriol_e9g’s picture

Status: Needs review » Reviewed & tested by the community

I'm not sure with the change done in #6 IMO for consistency with core we should use:

$this->pass(new FormattableMarkup('Test ID is @id.', ['@id' => $this->testId]));

The performance impact is low and the print is safely by default; But the patch is fine and secure with or without the formattable markup :)

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 23: 2555145-23.patch, failed testing. View results

oriol_e9g’s picture

Status: Needs work » Reviewed & tested by the community

Random js test fail.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Less strings for translators to translate - nice and less t() seems a good idea.

Committed b644848 and pushed to 8.8.x. Thanks!

  • alexpott committed b644848 on 8.8.x
    Issue #2555145 by TR, kostyashupenko, Mile23, oriol_e9g, geertvd: Remove...

Status: Fixed » Closed (fixed)

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