Move `Tests_Query_Conditionals::assertQueryTrue()` to `WP_UnitTestCase`. It should be available to all unit test classes. The conditionals class is huge, other classes are necessary for better coverage.

git-svn-id: https://develop.svn.wordpress.org/trunk@26005 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2013-11-04 21:55:12 +00:00
parent ecbcfc5a20
commit 2f38d2efc6
2 changed files with 44 additions and 44 deletions

View File

@ -278,4 +278,48 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
$tmp_dir = realpath( $dir );
return tempnam( $tmp_dir, 'wpunit' );
}
/**
* Check 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; any 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.
*
* @param string $prop,... Any number of WP_Query properties that are expected to be true for the current request.
*/
function assertQueryTrue(/* ... */) {
global $wp_query;
$all = array(
'is_single', 'is_preview', 'is_page', 'is_archive', 'is_date', 'is_year', 'is_month', 'is_day', 'is_time',
'is_author', 'is_category', 'is_tag', 'is_tax', 'is_search', 'is_feed', 'is_comment_feed', 'is_trackback',
'is_home', 'is_404', 'is_comments_popup', 'is_paged', 'is_admin', 'is_attachment', 'is_singular', 'is_robots',
'is_posts_page', 'is_post_type_archive',
);
$true = func_get_args();
$passed = true;
$not_false = $not_true = array(); // properties that were not set to expected values
foreach ( $all as $query_thing ) {
$result = is_callable( $query_thing ) ? call_user_func( $query_thing ) : $wp_query->$query_thing;
if ( in_array( $query_thing, $true ) ) {
if ( ! $result ) {
array_push( $not_true, $query_thing );
$passed = false;
}
} else if ( $result ) {
array_push( $not_false, $query_thing );
$passed = false;
}
}
$message = '';
if ( count($not_true) )
$message .= implode( $not_true, ', ' ) . ' should be true. ';
if ( count($not_false) )
$message .= implode( $not_false, ', ' ) . ' should be false.';
$this->assertTrue( $passed, $message );
}
}

View File

@ -34,50 +34,6 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
$GLOBALS['wp_rewrite']->init();
}
/**
* Check 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; any 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.
*
* @param string $prop,... Any number of WP_Query properties that are expected to be true for the current request.
*/
function assertQueryTrue(/* ... */) {
global $wp_query;
$all = array(
'is_single', 'is_preview', 'is_page', 'is_archive', 'is_date', 'is_year', 'is_month', 'is_day', 'is_time',
'is_author', 'is_category', 'is_tag', 'is_tax', 'is_search', 'is_feed', 'is_comment_feed', 'is_trackback',
'is_home', 'is_404', 'is_comments_popup', 'is_paged', 'is_admin', 'is_attachment', 'is_singular', 'is_robots',
'is_posts_page', 'is_post_type_archive',
);
$true = func_get_args();
$passed = true;
$not_false = $not_true = array(); // properties that were not set to expected values
foreach ( $all as $query_thing ) {
$result = is_callable( $query_thing ) ? call_user_func( $query_thing ) : $wp_query->$query_thing;
if ( in_array( $query_thing, $true ) ) {
if ( ! $result ) {
array_push( $not_true, $query_thing );
$passed = false;
}
} else if ( $result ) {
array_push( $not_false, $query_thing );
$passed = false;
}
}
$message = '';
if ( count($not_true) )
$message .= implode( $not_true, ', ' ) . ' should be true. ';
if ( count($not_false) )
$message .= implode( $not_false, ', ' ) . ' should be false.';
$this->assertTrue( $passed, $message );
}
function test_home() {
$this->go_to('/');
$this->assertQueryTrue('is_home');