Make WordPress Core

Changeset 59052


Ignore:
Timestamp:
09/18/2024 02:53:32 PM (8 months ago)
Author:
hellofromTonya
Message:

Tests: Fix implicitly nullable parameters in Tests_HtmlApi_WpHtmlProcessorComments.

PHP 8.4 deprecates implicitly nullable parameters, i.e. typed parameters with a null default value, which are not explicitly declared as nullable.

The Tests_HtmlApi_WpHtmlProcessorComments test class contains one problematic parameter in the test_comment_processing() method declaration.

While this could be fixed by adding the nullability operator, the type declarations in the test method is removed instead, including other type declarations for this method and the second test method, which were not affected by the deprecation.

The reason for this is quite straight-forward: using type declarations in tests is bad practice and inhibits defense-in-depth type testing.

Using type declarations in tests prevents being able to test the "code under test" with unexpected input types as the values with unexpected (scalar) types will be juggled to the expected type between the data provider and the test method and the _real_ data value would therefore never reach the method under test.

The knock-on effects of this are:

  • That the input handling of the "code under test" can not be safeguarded, whether this input handling is done via in-function type checking or via a type declaration in the "code under test".
  • That if such "unexpected data type" tests are added to the data provider, they will silently pass (due to the type being juggled before reaching the "code under test"), giving a false sense of security, while in actual fact, these data sets would not be testing anything at all and if, for instance, the type declaration in the "code under test" would be removed, these tests would still pass, while by rights they should start failing.

Also note that this problem would only be exacerbated if the file would be put under strict_types.

Ref: https://wiki.php.net/rfc/deprecate-implicitly-nullable-types

Follow-up to [58734].

Props jrf.
See #62061.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/html-api/wpHtmlProcessorComments.php

    r58734 r59052  
    2020     * @dataProvider data_comments
    2121     */
    22     public function test_comment_processing( string $html, string $expected_comment_type, string $expected_modifiable_text, string $expected_tag = null ) {
     22    public function test_comment_processing( $html, $expected_comment_type, $expected_modifiable_text, $expected_tag = null ) {
    2323        $processor = WP_HTML_Processor::create_fragment( $html );
    2424        $processor->next_token();
     
    5454     * @dataProvider data_funky_comments
    5555     */
    56     public function test_funky_comment( string $html, string $expected_modifiable_text ) {
     56    public function test_funky_comment( $html, $expected_modifiable_text ) {
    5757        $processor = WP_HTML_Processor::create_fragment( $html );
    5858        $processor->next_token();
Note: See TracChangeset for help on using the changeset viewer.