Problem/Motivation

This is a child issue of #3376057: [META] Add declare(strict_types=1) to all tests. After adding enabling strict types to all tests there were around 3000 errors. Fixing them all in one issue will lead to an enormous merge request that's difficult to review, as per the issue scope guidelines.

Steps to reproduce

Create the rector.php file:

<?php

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;

return static function (RectorConfig $rectorConfig): void {
  $rectorConfig->rule(DeclareStrictTypesRector::class);
};

Process all test traits:

composer require rector/rector --dev
php ./vendor/bin/rector process core/tests/Drupal/Tests/Traits
find core/tests/Drupal/Tests -type f -name "*Trait.php" -maxdepth 1 | xargs php ./vendor/bin/rector process
find core -type d -path "*/tests/src/Traits" | xargs php ./vendor/bin/rector process

Proposed resolution

Fix all strict type issues in test traits
Follow up [@todo create follow up issue]

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3399992

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mstrelan created an issue. See original summary.

mstrelan’s picture

Issue summary: View changes

mstrelan’s picture

Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Ran the script in the issue summary and got a different result

        modified:   core/modules/basic_auth/tests/src/Traits/BasicAuthTestTrait.php
        modified:   core/modules/block/tests/src/Traits/BlockCreationTrait.php
        modified:   core/modules/ckeditor5/tests/src/Traits/CKEditor5TestTrait.php
        modified:   core/modules/ckeditor5/tests/src/Traits/PrivateMethodUnitTestTrait.php
        modified:   core/modules/ckeditor5/tests/src/Traits/SynchronizeCsrfTokenSeedTrait.php
        modified:   core/modules/config/tests/src/Traits/AssertConfigEntityImportTrait.php
        modified:   core/modules/content_moderation/tests/src/Traits/ContentModerationTestTrait.php
        modified:   core/modules/field/tests/src/Traits/EntityReferenceTestTrait.php
        modified:   core/modules/field_ui/tests/src/Traits/FieldUiJSTestTrait.php
        modified:   core/modules/field_ui/tests/src/Traits/FieldUiTestTrait.php
        modified:   core/modules/jsonapi/tests/src/Traits/CommonCollectionFilterAccessTestPatternsTrait.php
        modified:   core/modules/media/tests/src/Traits/MediaTypeCreationTrait.php
        modified:   core/modules/media/tests/src/Traits/OEmbedTestTrait.php
        modified:   core/modules/menu_ui/tests/src/Traits/MenuUiTrait.php
        modified:   core/modules/migrate_drupal/tests/src/Traits/CreateMigrationsTrait.php
        modified:   core/modules/migrate_drupal/tests/src/Traits/CreateTestContentEntitiesTrait.php
        modified:   core/modules/migrate_drupal/tests/src/Traits/FieldDiscoveryTestTrait.php
        modified:   core/modules/migrate_drupal/tests/src/Traits/NodeMigrateTypeTestTrait.php
        modified:   core/modules/migrate_drupal/tests/src/Traits/ValidateMigrationStateTestTrait.php
        modified:   core/modules/node/tests/src/Traits/ContentTypeCreationTrait.php
        modified:   core/modules/node/tests/src/Traits/NodeCreationTrait.php
        modified:   core/modules/system/tests/src/Traits/OffCanvasTestTrait.php
        modified:   core/modules/system/tests/src/Traits/TestTrait.php
        modified:   core/modules/taxonomy/tests/src/Traits/TaxonomyTestTrait.php
        modified:   core/modules/user/tests/src/Traits/UserCreationTrait.php
        modified:   core/modules/views/tests/src/Traits/ViewsLoggerTestTrait.php
        modified:   core/tests/Drupal/Tests/ApiRequestTrait.php
        modified:   core/tests/Drupal/Tests/BrowserHtmlDebugTrait.php
        modified:   core/tests/Drupal/Tests/ConfigTestTrait.php
        modified:   core/tests/Drupal/Tests/EntityViewTrait.php
        modified:   core/tests/Drupal/Tests/ExtensionListTestTrait.php
        modified:   core/tests/Drupal/Tests/PhpUnitCompatibilityTrait.php
        modified:   core/tests/Drupal/Tests/RandomGeneratorTrait.php
        modified:   core/tests/Drupal/Tests/RequirementsPageTrait.php
        modified:   core/tests/Drupal/Tests/SchemaCheckTestTrait.php
        modified:   core/tests/Drupal/Tests/SessionTestTrait.php
        modified:   core/tests/Drupal/Tests/TestFileCreationTrait.php
        modified:   core/tests/Drupal/Tests/TestRequirementsTrait.php
        modified:   core/tests/Drupal/Tests/Traits/Core/Config/SchemaConfigListenerTestTrait.php
        modified:   core/tests/Drupal/Tests/Traits/Core/CronRunTrait.php
        modified:   core/tests/Drupal/Tests/Traits/Core/GeneratePermutationsTrait.php
        modified:   core/tests/Drupal/Tests/Traits/Core/Image/ToolkitTestTrait.php
        modified:   core/tests/Drupal/Tests/Traits/Core/PathAliasTestTrait.php
        modified:   core/tests/Drupal/Tests/Traits/PhpUnitWarnings.php
        modified:   core/tests/Drupal/Tests/UiHelperTrait.php
        modified:   core/tests/Drupal/Tests/UpdatePathTestTrait.php
        modified:   core/tests/Drupal/Tests/WaitTerminateTestTrait.php
        modified:   core/tests/Drupal/Tests/XdebugRequestTrait.php

smustgrave’s picture

Status: Needs work » Reviewed & tested by the community

Actually think I misunderstood the ticket. As far as your comments think refactoring any trait/test could be it's own ticket.

mstrelan’s picture

Status: Reviewed & tested by the community » Needs review

Just to clarify, pipeline 45766 demonstrates all the errors and fails when declare(strict_types=1) is added to all traits. The following commits then fix up the issues. Finally the initial commit is reverted so we have a reviewable MR. As discussed in #3397890-11: Fix strict type errors in unit tests we decided that splitting the fixes and adding the declaration should be separated. We need a follow up issue here for adding the declaration (see the todo in the IS), which can just be a cherry-pick of the first commit.

mstrelan’s picture

Status: Needs review » Reviewed & tested by the community

Did not mean to change the status

xjm’s picture

Status: Reviewed & tested by the community » Needs work

Under our test coding standards, it's preferred to use string literals when asserting test data. Neither FormattableMarkup() nor sprintf() should be used. (We've had massive cleanups previously to eliminate the usage of both in tests.) Thanks!

mstrelan’s picture

Status: Needs work » Needs review

@xjm these are not in the actual assertions but in the $message argument.

xjm’s picture

@mstrelan, it goes for all arguments of assertions including the message. Actually even more so for the message; we stripped string formatting off that eight years back, whereas we did so only 3-5 years ago for asserted values.

What would be best would be to use the literal values rather than unnecessary uses of randomly generated test data, but that's possibly out of scope. So, in the meanwhile, double-quoted strings. (Or this issue is small enough that I would also embrace going a step further and using literal fixture values for those few tests.)

Adding the related policy issue which apparently never got closed out and added to docs, but still applies.

mstrelan’s picture

Status: Needs work » Needs review

Not trying to argue here as I'm not bothered either way. Wondering what you mean "we stripped string formatting off that eight years back" since this and all the other related issues are removing usage of FormattableMarkup.

Regardless, I've converted these to string interpolation and it's back to NR.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Seems replacement of sprintf has been done.

  • xjm committed c304b092 on 11.x
    Issue #3399992 by mstrelan, smustgrave, xjm: Fix strict type errors in...

  • xjm committed b6052c19 on 10.2.x
    Issue #3399992 by mstrelan, smustgrave, xjm: Fix strict type errors in...

  • xjm committed 67343dfe on 10.1.x
    Issue #3399992 by mstrelan, smustgrave, xjm: Fix strict type errors in...
xjm’s picture

Version: 11.x-dev » 10.1.x-dev
Status: Reviewed & tested by the community » Fixed

Looks great now. So much cleaner. Committed to 11.x, 10.2.x, and 10.1.x as a test improvement. Thanks!

Status: Fixed » Closed (fixed)

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