Fix strict type errors in test traits

Created on 8 November 2023, 8 months ago
Updated 15 November 2023, 8 months ago

Problem/Motivation

This is a child issue of 📌 Add declare(strict_types=1) to all tests Needs work . 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

📌 Task
Status

Fixed

Version

10.1

Component
PHPUnit 

Last updated about 8 hours ago

Created by

🇦🇺Australia mstrelan

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @mstrelan
  • @mstrelan opened merge request.
  • Status changed to Needs review 8 months ago
  • Status changed to Needs work 8 months ago
  • 🇺🇸United States smustgrave

    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
    
    
  • Status changed to RTBC 8 months ago
  • 🇺🇸United States smustgrave

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

  • Status changed to Needs review 8 months ago
  • 🇦🇺Australia mstrelan

    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.

  • Status changed to RTBC 8 months ago
  • 🇦🇺Australia mstrelan

    Did not mean to change the status

  • Status changed to Needs work 8 months ago
  • 🇺🇸United States xjm

    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!

  • Status changed to Needs review 8 months ago
  • 🇦🇺Australia mstrelan

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

  • Status changed to Needs work 8 months ago
  • 🇺🇸United States xjm

    @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 out of scope.

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

  • Status changed to Needs review 8 months ago
  • 🇦🇺Australia mstrelan

    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.

  • Status changed to RTBC 8 months ago
  • 🇺🇸United States smustgrave

    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...
  • 🇺🇸United States xjm

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

  • Status changed to Fixed 8 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024