Tests: Move assertQueryTrue() closer to the other custom assertions in WP_UnitTestCase_Base.

Follow-up to [109/tests], [26005].

See #55652.

git-svn-id: https://develop.svn.wordpress.org/trunk@53545 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-06-21 11:12:02 +00:00
parent 40c4f11a81
commit 53372fd5be

View File

@ -879,6 +879,82 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
}
}
/**
* Checks each of the WP_Query is_* functions/properties against expected boolean value.
*
* Any properties that are listed by name as parameters will be expected to be true; all others are
* expected to be false. For example, assertQueryTrue( 'is_single', 'is_feed' ) means is_single()
* and is_feed() must be true and everything else must be false to pass.
*
* @since 2.5.0
* @since 3.8.0 Moved from `Tests_Query_Conditionals` to `WP_UnitTestCase`.
* @since 5.3.0 Formalized the existing `...$prop` parameter by adding it
* to the function signature.
*
* @param string ...$prop Any number of WP_Query properties that are expected to be true for the current request.
*/
public function assertQueryTrue( ...$prop ) {
global $wp_query;
$all = array(
'is_404',
'is_admin',
'is_archive',
'is_attachment',
'is_author',
'is_category',
'is_comment_feed',
'is_date',
'is_day',
'is_embed',
'is_feed',
'is_front_page',
'is_home',
'is_privacy_policy',
'is_month',
'is_page',
'is_paged',
'is_post_type_archive',
'is_posts_page',
'is_preview',
'is_robots',
'is_favicon',
'is_search',
'is_single',
'is_singular',
'is_tag',
'is_tax',
'is_time',
'is_trackback',
'is_year',
);
foreach ( $prop as $true_thing ) {
$this->assertContains( $true_thing, $all, "Unknown conditional: {$true_thing}." );
}
$passed = true;
$message = '';
foreach ( $all as $query_thing ) {
$result = is_callable( $query_thing ) ? call_user_func( $query_thing ) : $wp_query->$query_thing;
if ( in_array( $query_thing, $prop, true ) ) {
if ( ! $result ) {
$message .= $query_thing . ' is false but is expected to be true. ' . PHP_EOL;
$passed = false;
}
} elseif ( $result ) {
$message .= $query_thing . ' is true but is expected to be false. ' . PHP_EOL;
$passed = false;
}
}
if ( ! $passed ) {
$this->fail( $message );
}
}
/**
* Helper function to convert a single-level array containing text strings to a named data provider.
*
@ -1105,82 +1181,6 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
return tempnam( $tmp_dir, 'wpunit' );
}
/**
* Checks each of the WP_Query is_* functions/properties against expected boolean value.
*
* Any properties that are listed by name as parameters will be expected to be true; all others are
* expected to be false. For example, assertQueryTrue( 'is_single', 'is_feed' ) means is_single()
* and is_feed() must be true and everything else must be false to pass.
*
* @since 2.5.0
* @since 3.8.0 Moved from `Tests_Query_Conditionals` to `WP_UnitTestCase`.
* @since 5.3.0 Formalized the existing `...$prop` parameter by adding it
* to the function signature.
*
* @param string ...$prop Any number of WP_Query properties that are expected to be true for the current request.
*/
public function assertQueryTrue( ...$prop ) {
global $wp_query;
$all = array(
'is_404',
'is_admin',
'is_archive',
'is_attachment',
'is_author',
'is_category',
'is_comment_feed',
'is_date',
'is_day',
'is_embed',
'is_feed',
'is_front_page',
'is_home',
'is_privacy_policy',
'is_month',
'is_page',
'is_paged',
'is_post_type_archive',
'is_posts_page',
'is_preview',
'is_robots',
'is_favicon',
'is_search',
'is_single',
'is_singular',
'is_tag',
'is_tax',
'is_time',
'is_trackback',
'is_year',
);
foreach ( $prop as $true_thing ) {
$this->assertContains( $true_thing, $all, "Unknown conditional: {$true_thing}." );
}
$passed = true;
$message = '';
foreach ( $all as $query_thing ) {
$result = is_callable( $query_thing ) ? call_user_func( $query_thing ) : $wp_query->$query_thing;
if ( in_array( $query_thing, $prop, true ) ) {
if ( ! $result ) {
$message .= $query_thing . ' is false but is expected to be true. ' . PHP_EOL;
$passed = false;
}
} elseif ( $result ) {
$message .= $query_thing . ' is true but is expected to be false. ' . PHP_EOL;
$passed = false;
}
}
if ( ! $passed ) {
$this->fail( $message );
}
}
/**
* Selectively deletes a file.
*