Make WordPress Core

Changeset 54365


Ignore:
Timestamp:
10/01/2022 03:47:13 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Tests: Ensure prerequisites are met for draft length tests in Tests_L10n.

These three tests for wp_dashboard_recent_drafts() would run into a PHP 8.1 "passing null to non-nullable" deprecation for the call to ltrim() when the result of get_edit_post_link() is passed to esc_url().

Setting a deprecation expectation would not solve this as the returned value would still not match the expected value(s).

The recent drafts list is only displayed on the Dashboard screen for users with the edit_posts capability. By setting the current user to Editor, the prerequisites for wp_dashboard_recent_drafts() are met, which means the deprecation notice is avoided and the assertions will succeed.

This commit addresses a few errors in the test suite along the lines of:

1) Tests_L10n::test_length_of_draft_should_be_counted_by_words
ltrim(): Passing null to parameter #1 ($string) of type string is deprecated

/var/www/src/wp-includes/formatting.php:4376
/var/www/src/wp-admin/includes/dashboard.php:657
/var/www/tests/phpunit/tests/l10n.php:449
/var/www/vendor/bin/phpunit:123

Follow-up to [45505], [52253], [52259].

Props jrf, desrosj, SergeyBiryukov.
See #56681, #55652, #55656.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/l10n.php

    r54088 r54365  
    436436        switch_to_locale( 'en_US' );
    437437
     438        /*
     439         * The recent drafts list is only displayed on the Dashboard screen for users
     440         * with the 'edit_posts' capability.
     441         *
     442         * This means the current user needs to be set to Editor as a prerequisite
     443         * for the call to the wp_dashboard_recent_drafts() function.
     444         *
     445         * This allows the subsequent call to get_edit_post_link() to work as expected
     446         * and return a string instead of null, which would otherwise cause a PHP 8.1
     447         * "passing null to non-nullable" deprecation notice.
     448         */
     449        wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
     450
    438451        $args = array(
    439452            'post_content' => $this->long_text,
     
    462475        switch_to_locale( 'ja_JP' );
    463476
     477        /*
     478         * The recent drafts list is only displayed on the Dashboard screen for users
     479         * with the 'edit_posts' capability.
     480         *
     481         * This means the current user needs to be set to Editor as a prerequisite
     482         * for the call to the wp_dashboard_recent_drafts() function.
     483         *
     484         * This allows the subsequent call to get_edit_post_link() to work as expected
     485         * and return a string instead of null, which would otherwise cause a PHP 8.1
     486         * "passing null to non-nullable" deprecation notice.
     487         */
     488        wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
     489
    464490        $args = array(
    465491            'post_content' => $this->long_text,
     
    487513
    488514        switch_to_locale( 'ja_JP' );
     515
     516        /*
     517         * The recent drafts list is only displayed on the Dashboard screen for users
     518         * with the 'edit_posts' capability.
     519         *
     520         * This means the current user needs to be set to Editor as a prerequisite
     521         * for the call to the wp_dashboard_recent_drafts() function.
     522         *
     523         * This allows the subsequent call to get_edit_post_link() to work as expected
     524         * and return a string instead of null, which would otherwise cause a PHP 8.1
     525         * "passing null to non-nullable" deprecation notice.
     526         */
     527        wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
    489528
    490529        $args = array(
Note: See TracChangeset for help on using the changeset viewer.