From bae6e1735bd8c782f6e265ed70b14aa48e140316 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 1 Oct 2022 15:47:13 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@54365 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/l10n.php | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/phpunit/tests/l10n.php b/tests/phpunit/tests/l10n.php index 3d23dd7f20d4d..1c69fb4c23bee 100644 --- a/tests/phpunit/tests/l10n.php +++ b/tests/phpunit/tests/l10n.php @@ -435,6 +435,19 @@ public function test_length_of_draft_should_be_counted_by_words() { switch_to_locale( 'en_US' ); + /* + * The recent drafts list is only displayed on the Dashboard screen for users + * with the 'edit_posts' capability. + * + * This means the current user needs to be set to Editor as a prerequisite + * for the call to the wp_dashboard_recent_drafts() function. + * + * This allows the subsequent call to get_edit_post_link() to work as expected + * and return a string instead of null, which would otherwise cause a PHP 8.1 + * "passing null to non-nullable" deprecation notice. + */ + wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) ); + $args = array( 'post_content' => $this->long_text, 'post_excerpt' => '', @@ -461,6 +474,19 @@ public function test_length_of_draft_should_be_counted_by_chars() { switch_to_locale( 'ja_JP' ); + /* + * The recent drafts list is only displayed on the Dashboard screen for users + * with the 'edit_posts' capability. + * + * This means the current user needs to be set to Editor as a prerequisite + * for the call to the wp_dashboard_recent_drafts() function. + * + * This allows the subsequent call to get_edit_post_link() to work as expected + * and return a string instead of null, which would otherwise cause a PHP 8.1 + * "passing null to non-nullable" deprecation notice. + */ + wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) ); + $args = array( 'post_content' => $this->long_text, 'post_excerpt' => '', @@ -487,6 +513,19 @@ public function test_length_of_draft_should_be_counted_by_chars_in_japanese() { switch_to_locale( 'ja_JP' ); + /* + * The recent drafts list is only displayed on the Dashboard screen for users + * with the 'edit_posts' capability. + * + * This means the current user needs to be set to Editor as a prerequisite + * for the call to the wp_dashboard_recent_drafts() function. + * + * This allows the subsequent call to get_edit_post_link() to work as expected + * and return a string instead of null, which would otherwise cause a PHP 8.1 + * "passing null to non-nullable" deprecation notice. + */ + wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) ); + $args = array( 'post_content' => str_repeat( 'あ', 200 ), 'post_excerpt' => '',