Tests: Use a data provider in the test for conditional tags returning early if $wp_query is not set.

Follow-up to [53395], [53396].

Props peterwilsoncc.
See #55104.

git-svn-id: https://develop.svn.wordpress.org/trunk@53400 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-05-16 14:09:41 +00:00
parent 0cd735bc01
commit cfc71d580f

View File

@ -1618,28 +1618,46 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
/**
* @ticket 55104
*
* @dataProvider data_conditional_tags_trigger_doing_it_wrong_and_return_false_if_wp_query_is_not_set
*
* @param string $function_name The name of the function to test.
*/
public function test_conditional_tags_trigger_doing_it_wrong_and_return_false_if_wp_query_is_not_set() {
public function test_conditional_tags_trigger_doing_it_wrong_and_return_false_if_wp_query_is_not_set( $function_name ) {
unset( $GLOBALS['wp_query'] );
$functions = get_class_methods( 'WP_Query' );
foreach ( $functions as $function_name ) {
// Only test `is_*()` conditional tags.
if ( ! str_starts_with( $function_name, 'is_' ) ) {
continue;
}
if ( 'is_comments_popup' === $function_name ) {
// `is_comments_popup()` is deprecated as of WP 4.5.
$this->setExpectedDeprecated( $function_name );
} else {
// All the other functions should throw a `_doing_it_wrong()` notice.
$this->setExpectedIncorrectUsage( $function_name );
}
$this->assertFalse( call_user_func( $function_name ) );
if ( 'is_comments_popup' === $function_name ) {
// `is_comments_popup()` is deprecated as of WP 4.5.
$this->setExpectedDeprecated( $function_name );
} else {
// All the other functions should throw a `_doing_it_wrong()` notice.
$this->setExpectedIncorrectUsage( $function_name );
}
$this->assertFalse( call_user_func( $function_name ) );
}
/**
* Data provider.
*/
public function data_conditional_tags_trigger_doing_it_wrong_and_return_false_if_wp_query_is_not_set() {
// Get the list of `is_*()` conditional tags.
$functions = array_filter(
get_class_methods( 'WP_Query' ),
static function( $function_name ) {
return str_starts_with( $function_name, 'is_' );
}
);
// Wrap each function name in an array.
$functions = array_map(
static function( $function_name ) {
return array( $function_name );
},
$functions
);
return $functions;
}
}