From 400982add83a10618cb2f84ecc7d1ca35b2a76e2 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Mon, 1 Nov 2021 22:22:49 +0000 Subject: [PATCH] Build/Test Tools: Ignore "null to nullable" deprecations for select tests. Adds an expectation for PHP 8.1 "passing null to non-nullable" deprecation notice to select tests where the deprecation is generated by one of the functions in the `wp-includes/formatting.php` file, either via a filter hook callback or by a direct call. Instead of haphazardly fixing these issues exposed by the tests, a more structural and all-encompassing solution for input validation should be architected and implemented as otherwise, we'll keep running into similar issues time and again with each new PHP version. To discourage people from "fixing" these issues now anyway, this commit "hides" nearly all of these issues from the test runs. Once a more structural solution is designed, these tests and the underlying functions causing the deprecation notices should be revisited and the structural solution put in place. Includes a few minor other tweaks to select tests: * Removing a stray `return` (twice) from assertion statements. * Removing calls to `ob_*()` functions in favour of letting PHPUnit manage the output catching. This prevents warnings along the lines of `Test code or tested code did not (only) close its own output buffers`. Props jrf, hellofromTonya. See #53635. git-svn-id: https://develop.svn.wordpress.org/trunk@51968 602fd350-edb4-49c9-b593-d223f7449a82 --- .../phpunit/tests/admin/includesListTable.php | 16 +- tests/phpunit/tests/admin/includesPost.php | 60 ++++++ tests/phpunit/tests/comment-submission.php | 57 ++++++ tests/phpunit/tests/date/xmlrpc.php | 10 + tests/phpunit/tests/dependencies/scripts.php | 36 +++- .../tests/formatting/wpRelNofollow.php | 45 ++++- tests/phpunit/tests/formatting/wpRelUgc.php | 45 ++++- .../tests/formatting/wpTrimExcerpt.php | 10 + .../phpunit/tests/term/wpGenerateTagCloud.php | 10 + tests/phpunit/tests/xmlrpc/mw/editPost.php | 10 + tests/phpunit/tests/xmlrpc/mw/newPost.php | 80 ++++++++ tests/phpunit/tests/xmlrpc/wp/editPost.php | 20 ++ .../phpunit/tests/xmlrpc/wp/getRevisions.php | 10 + tests/phpunit/tests/xmlrpc/wp/newPost.php | 190 ++++++++++++++++++ 14 files changed, 589 insertions(+), 10 deletions(-) diff --git a/tests/phpunit/tests/admin/includesListTable.php b/tests/phpunit/tests/admin/includesListTable.php index ac2e153e2f..af589dcfe6 100644 --- a/tests/phpunit/tests/admin/includesListTable.php +++ b/tests/phpunit/tests/admin/includesListTable.php @@ -185,6 +185,16 @@ class Tests_Admin_IncludesListTable extends WP_UnitTestCase { * @param array $expected_ids Expected IDs of pages returned. */ protected function _test_list_hierarchical_page( array $args, array $expected_ids ) { + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } + $matches = array(); $_REQUEST['paged'] = $args['paged']; @@ -205,12 +215,14 @@ class Tests_Admin_IncludesListTable extends WP_UnitTestCase { $args['posts_per_archive_page'] = -1; } + // Effectively ignore the output until retrieving it later via `getActualOutput()`. + $this->expectOutputRegex( '`.`' ); + $pages = new WP_Query( $args ); - ob_start(); $this->table->set_hierarchical_display( true ); $this->table->display_rows( $pages->posts ); - $output = ob_get_clean(); + $output = $this->getActualOutput(); // Clean up. unset( $_REQUEST['paged'] ); diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index 8c5013279e..02c40d9071 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -876,6 +876,16 @@ class Tests_Admin_IncludesPost extends WP_UnitTestCase { * @ticket 37406 */ public function test_post_exists_should_support_post_type() { + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } + $title = 'Foo Bar'; $post_type = 'page'; $post_id = self::factory()->post->create( @@ -893,6 +903,16 @@ class Tests_Admin_IncludesPost extends WP_UnitTestCase { * @ticket 37406 */ public function test_post_exists_should_not_match_a_page_for_post() { + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } + $title = 'Foo Bar'; $post_type = 'page'; $post_id = self::factory()->post->create( @@ -910,6 +930,16 @@ class Tests_Admin_IncludesPost extends WP_UnitTestCase { * @ticket 34012 */ public function test_post_exists_should_support_post_status() { + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } + $title = 'Foo Bar'; $post_type = 'post'; $post_status = 'publish'; @@ -930,6 +960,16 @@ class Tests_Admin_IncludesPost extends WP_UnitTestCase { * @ticket 34012 */ public function test_post_exists_should_support_post_type_status_combined() { + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } + $title = 'Foo Bar'; $post_type = 'post'; $post_status = 'publish'; @@ -949,6 +989,16 @@ class Tests_Admin_IncludesPost extends WP_UnitTestCase { * @ticket 34012 */ public function test_post_exists_should_only_match_correct_post_status() { + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } + $title = 'Foo Bar'; $post_type = 'post'; $post_status = 'draft'; @@ -968,6 +1018,16 @@ class Tests_Admin_IncludesPost extends WP_UnitTestCase { * @ticket 34012 */ public function test_post_exists_should_not_match_invalid_post_type_and_status_combined() { + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } + $title = 'Foo Bar'; $post_type = 'post'; $post_status = 'publish'; diff --git a/tests/phpunit/tests/comment-submission.php b/tests/phpunit/tests/comment-submission.php index 3e6c7ab1c2..2cd5d8473a 100644 --- a/tests/phpunit/tests/comment-submission.php +++ b/tests/phpunit/tests/comment-submission.php @@ -200,6 +200,15 @@ class Tests_Comment_Submission extends WP_UnitTestCase { } public function test_submitting_comment_to_password_protected_post_succeeds() { + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } $password = 'password'; $hasher = new PasswordHash( 8, true ); @@ -282,6 +291,15 @@ class Tests_Comment_Submission extends WP_UnitTestCase { * @group slashes */ public function test_submitting_comment_handles_slashes_correctly_handles_slashes() { + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } $data = array( 'comment_post_ID' => self::$post->ID, @@ -429,6 +447,15 @@ class Tests_Comment_Submission extends WP_UnitTestCase { } public function test_anonymous_user_cannot_comment_unfiltered_html() { + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } $data = array( 'comment_post_ID' => self::$post->ID, @@ -719,6 +746,16 @@ class Tests_Comment_Submission extends WP_UnitTestCase { * @ticket 49236 */ public function test_submitting_comment_with_empty_type_results_in_correct_type() { + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } + $data = array( 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment', @@ -802,6 +839,16 @@ class Tests_Comment_Submission extends WP_UnitTestCase { * @ticket 36901 */ public function test_submitting_duplicate_comments() { + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } + $data = array( 'comment_post_ID' => self::$post->ID, 'comment' => 'Did I say that?', @@ -818,6 +865,16 @@ class Tests_Comment_Submission extends WP_UnitTestCase { * @ticket 36901 */ public function test_comments_flood() { + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } + $data = array( 'comment_post_ID' => self::$post->ID, 'comment' => 'Did I say that?', diff --git a/tests/phpunit/tests/date/xmlrpc.php b/tests/phpunit/tests/date/xmlrpc.php index 6fe96618bc..b2275403d8 100644 --- a/tests/phpunit/tests/date/xmlrpc.php +++ b/tests/phpunit/tests/date/xmlrpc.php @@ -14,6 +14,16 @@ class Tests_Date_XMLRPC extends WP_XMLRPC_UnitTestCase { * @covers wp_xmlrpc_server::mw_newPost */ public function test_date_new_post() { + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } + $timezone = 'Europe/Kiev'; update_option( 'timezone_string', $timezone ); diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 2863a5ec9b..90394fab30 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -721,6 +721,16 @@ JS; $wp_scripts->base_url = ''; $wp_scripts->do_concat = true; + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } + $ver = get_bloginfo( 'version' ); $suffix = wp_scripts_get_suffix(); $expected = "\n"; @@ -746,8 +756,12 @@ JS; wp_enqueue_script( 'test-example2', 'http://example2.com', array( 'wp-a11y' ), null ); wp_add_inline_script( 'test-example2', 'console.log("after");', 'after' ); - $print_scripts = get_echo( 'wp_print_scripts' ); - $print_scripts .= get_echo( '_print_scripts' ); + // Effectively ignore the output until retrieving it later via `getActualOutput()`. + $this->expectOutputRegex( '`.`' ); + + wp_print_scripts(); + _print_scripts(); + $print_scripts = $this->getActualOutput(); /* * We've replaced wp-a11y.js with @wordpress/a11y package (see #45066), @@ -776,6 +790,16 @@ JS; $wp_scripts->base_url = ''; $wp_scripts->do_concat = true; + if ( PHP_VERSION_ID >= 80100 ) { + /* + * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in + * via hooked in filter functions until a more structural solution to the + * "missing input validation" conundrum has been architected and implemented. + */ + $this->expectDeprecation(); + $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); + } + $expected_tail = "\n"; $expected_tail .= "