Code is Poetry.

WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.



git-svn-id: https://develop.svn.wordpress.org/trunk@42343 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast
2017-11-30 23:09:33 +00:00
parent ec6a089f98
commit 8f95800d52
1103 changed files with 105978 additions and 78184 deletions
+16 -15
View File
@@ -13,8 +13,9 @@ if ( class_exists( 'PHPUnit\Runner\Version' ) ) {
$config_file_path = dirname( dirname( __FILE__ ) );
if ( ! file_exists( $config_file_path . '/wp-tests-config.php' ) ) {
// Support the config file from the root of the develop repository.
if ( basename( $config_file_path ) === 'phpunit' && basename( dirname( $config_file_path ) ) === 'tests' )
if ( basename( $config_file_path ) === 'phpunit' && basename( dirname( $config_file_path ) ) === 'tests' ) {
$config_file_path = dirname( dirname( $config_file_path ) );
}
}
$config_file_path .= '/wp-tests-config.php';
@@ -38,8 +39,9 @@ define( 'DIR_TESTDATA', dirname( __FILE__ ) . '/../data' );
define( 'WP_LANG_DIR', DIR_TESTDATA . '/languages' );
if ( ! defined( 'WP_TESTS_FORCE_KNOWN_BUGS' ) )
if ( ! defined( 'WP_TESTS_FORCE_KNOWN_BUGS' ) ) {
define( 'WP_TESTS_FORCE_KNOWN_BUGS', false );
}
// Cron tries to make an HTTP request to the blog, which always fails, because tests are run in CLI mode only
define( 'DISABLE_WP_CRON', true );
@@ -53,7 +55,7 @@ $PHP_SELF = $GLOBALS['PHP_SELF'] = $_SERVER['PHP_SELF'] = '/index.php';
// Should we run in multisite mode?
$multisite = '1' == getenv( 'WP_MULTISITE' );
$multisite = $multisite || ( defined( 'WP_TESTS_MULTISITE') && WP_TESTS_MULTISITE );
$multisite = $multisite || ( defined( 'WP_TESTS_MULTISITE' ) && WP_TESTS_MULTISITE );
$multisite = $multisite || ( defined( 'MULTISITE' ) && MULTISITE );
// Override the PHPMailer
@@ -71,12 +73,12 @@ if ( 0 !== $retval ) {
}
if ( $multisite ) {
echo "Running as multisite..." . PHP_EOL;
echo 'Running as multisite...' . PHP_EOL;
defined( 'MULTISITE' ) or define( 'MULTISITE', true );
defined( 'SUBDOMAIN_INSTALL' ) or define( 'SUBDOMAIN_INSTALL', false );
$GLOBALS['base'] = '/';
} else {
echo "Running as single site... To run multisite, use -c tests/phpunit/multisite.xml" . PHP_EOL;
echo 'Running as single site... To run multisite, use -c tests/phpunit/multisite.xml' . PHP_EOL;
}
unset( $multisite );
@@ -86,14 +88,14 @@ tests_add_filter( 'wp_die_handler', '_wp_die_handler_filter' );
// Preset WordPress options defined in bootstrap file.
// Used to activate themes, plugins, as well as other settings.
if(isset($GLOBALS['wp_tests_options'])) {
if ( isset( $GLOBALS['wp_tests_options'] ) ) {
function wp_tests_options( $value ) {
$key = substr( current_filter(), strlen( 'pre_option_' ) );
return $GLOBALS['wp_tests_options'][$key];
return $GLOBALS['wp_tests_options'][ $key ];
}
foreach ( array_keys( $GLOBALS['wp_tests_options'] ) as $key ) {
tests_add_filter( 'pre_option_'.$key, 'wp_tests_options' );
tests_add_filter( 'pre_option_' . $key, 'wp_tests_options' );
}
}
@@ -142,8 +144,7 @@ class WP_PHPUnit_Util_Getopt extends PHPUnit_Util_Getopt {
if ( strlen( $arg ) > 1 && $arg[0] === '-' && $arg[1] === '-' ) {
PHPUnit_Util_Getopt::parseLongOption( substr( $arg, 2 ), $this->longOptions, $options, $argv );
}
}
catch ( PHPUnit_Framework_Exception $e ) {
} catch ( PHPUnit_Framework_Exception $e ) {
// Enforcing recognized arguments or correctly formed arguments is
// not really the concern here.
continue;
@@ -151,19 +152,19 @@ class WP_PHPUnit_Util_Getopt extends PHPUnit_Util_Getopt {
}
$skipped_groups = array(
'ajax' => true,
'ms-files' => true,
'ajax' => true,
'ms-files' => true,
'external-http' => true,
);
foreach ( $options as $option ) {
switch ( $option[0] ) {
case '--exclude-group' :
case '--exclude-group':
foreach ( $skipped_groups as $group_name => $skipped ) {
$skipped_groups[ $group_name ] = false;
}
continue 2;
case '--group' :
case '--group':
$groups = explode( ',', $option[1] );
foreach ( $groups as $group ) {
if ( is_numeric( $group ) || preg_match( '/^(UT|Plugin)\d+$/', $group ) ) {
@@ -191,6 +192,6 @@ class WP_PHPUnit_Util_Getopt extends PHPUnit_Util_Getopt {
echo 'If this changeset includes changes to HTTP, make sure there are no timeouts.' . PHP_EOL;
echo PHP_EOL;
}
}
}
}
new WP_PHPUnit_Util_Getopt( $_SERVER['argv'] );
@@ -17,44 +17,47 @@ class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
function create_object( $args, $legacy_parent = 0, $legacy_args = array() ) {
// Backward compatibility for legacy argument format.
if ( is_string( $args ) ) {
$file = $args;
$args = $legacy_args;
$file = $args;
$args = $legacy_args;
$args['post_parent'] = $legacy_parent;
$args['file'] = $file;
$args['file'] = $file;
}
$r = array_merge( array(
'file' => '',
'post_parent' => 0,
), $args );
$r = array_merge(
array(
'file' => '',
'post_parent' => 0,
), $args
);
return wp_insert_attachment( $r, $r['file'], $r['post_parent'] );
}
function create_upload_object( $file, $parent = 0 ) {
$contents = file_get_contents($file);
$upload = wp_upload_bits(basename($file), null, $contents);
$contents = file_get_contents( $file );
$upload = wp_upload_bits( basename( $file ), null, $contents );
$type = '';
if ( ! empty($upload['type']) ) {
if ( ! empty( $upload['type'] ) ) {
$type = $upload['type'];
} else {
$mime = wp_check_filetype( $upload['file'] );
if ($mime)
if ( $mime ) {
$type = $mime['type'];
}
}
$attachment = array(
'post_title' => basename( $upload['file'] ),
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $parent,
'post_title' => basename( $upload['file'] ),
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $parent,
'post_mime_type' => $type,
'guid' => $upload[ 'url' ],
'guid' => $upload['url'],
);
// Save the data
$id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent );
$id = wp_insert_attachment( $attachment, $upload['file'], $parent );
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
return $id;
@@ -16,20 +16,20 @@ class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
global $current_site, $base;
parent::__construct( $factory );
$this->default_generation_definitions = array(
'domain' => $current_site->domain,
'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
'domain' => $current_site->domain,
'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
'site_id' => $current_site->id,
);
}
function create_object( $args ) {
global $wpdb;
$meta = isset( $args['meta'] ) ? $args['meta'] : array( 'public' => 1 );
$meta = isset( $args['meta'] ) ? $args['meta'] : array( 'public' => 1 );
$user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
// temp tables will trigger db errors when we attempt to reference them as new temp tables
$suppress = $wpdb->suppress_errors();
$blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
$blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
$wpdb->suppress_errors( $suppress );
// Tell WP we're done installing.
@@ -18,7 +18,7 @@ class WP_UnitTest_Factory_For_Bookmark extends WP_UnitTest_Factory_For_Thing {
parent::__construct( $factory );
$this->default_generation_definitions = array(
'link_name' => new WP_UnitTest_Generator_Sequence( 'Bookmark name %s' ),
'link_url' => new WP_UnitTest_Generator_Sequence( 'Bookmark URL %s' ),
'link_url' => new WP_UnitTest_Generator_Sequence( 'Bookmark URL %s' ),
);
}
@@ -15,10 +15,10 @@ class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
function __construct( $factory = null ) {
parent::__construct( $factory );
$this->default_generation_definitions = array(
'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
'comment_approved' => 1,
'comment_content' => 'This is a comment'
'comment_approved' => 1,
'comment_content' => 'This is a comment',
);
}
@@ -15,10 +15,10 @@ class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
function __construct( $factory = null ) {
parent::__construct( $factory );
$this->default_generation_definitions = array(
'domain' => WP_TESTS_DOMAIN,
'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
'domain' => WP_TESTS_DOMAIN,
'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
'subdomain_install' => false,
);
}
@@ -15,11 +15,11 @@ class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
function __construct( $factory = null ) {
parent::__construct( $factory );
$this->default_generation_definitions = array(
'post_status' => 'publish',
'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
'post_status' => 'publish',
'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
'post_type' => 'post'
'post_type' => 'post',
);
}
@@ -16,26 +16,28 @@ class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
function __construct( $factory = null, $taxonomy = null ) {
parent::__construct( $factory );
$this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
$this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
$this->default_generation_definitions = array(
'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
'taxonomy' => $this->taxonomy,
'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
'taxonomy' => $this->taxonomy,
'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
);
}
function create_object( $args ) {
$args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
$args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
$term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
if ( is_wp_error( $term_id_pair ) )
if ( is_wp_error( $term_id_pair ) ) {
return $term_id_pair;
}
return $term_id_pair['term_id'];
}
function update_object( $term, $fields ) {
$fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
if ( is_object( $term ) )
if ( is_object( $term ) ) {
$taxonomy = $term->taxonomy;
}
$term_id_pair = wp_update_term( $term, $taxonomy, $fields );
return $term_id_pair['term_id'];
}
@@ -48,7 +50,7 @@ class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
* @return array|null|WP_Error|WP_Term
*/
function create_and_get( $args = array(), $generation_definitions = null ) {
$term_id = $this->create( $args, $generation_definitions );
$term_id = $this->create( $args, $generation_definitions );
$taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
return get_term( $term_id, $taxonomy );
}
@@ -17,7 +17,7 @@ abstract class WP_UnitTest_Factory_For_Thing {
* {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
*/
function __construct( $factory, $default_generation_definitions = array() ) {
$this->factory = $factory;
$this->factory = $factory;
$this->default_generation_definitions = $default_generation_definitions;
}
@@ -25,19 +25,22 @@ abstract class WP_UnitTest_Factory_For_Thing {
abstract function update_object( $object, $fields );
function create( $args = array(), $generation_definitions = null ) {
if ( is_null( $generation_definitions ) )
if ( is_null( $generation_definitions ) ) {
$generation_definitions = $this->default_generation_definitions;
}
$generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
$created = $this->create_object( $generated_args );
if ( !$created || is_wp_error( $created ) )
$created = $this->create_object( $generated_args );
if ( ! $created || is_wp_error( $created ) ) {
return $created;
}
if ( $callbacks ) {
$updated_fields = $this->apply_callbacks( $callbacks, $created );
$save_result = $this->update_object( $created, $updated_fields );
if ( !$save_result || is_wp_error( $save_result ) )
$save_result = $this->update_object( $created, $updated_fields );
if ( ! $save_result || is_wp_error( $save_result ) ) {
return $save_result;
}
}
return $created;
}
@@ -59,20 +62,21 @@ abstract class WP_UnitTest_Factory_For_Thing {
function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
$callbacks = array();
if ( is_null( $generation_definitions ) )
if ( is_null( $generation_definitions ) ) {
$generation_definitions = $this->default_generation_definitions;
}
// Use the same incrementor for all fields belonging to this object.
$gen = new WP_UnitTest_Generator_Sequence();
$incr = $gen->get_incr();
foreach( array_keys( $generation_definitions ) as $field_name ) {
if ( !isset( $args[$field_name] ) ) {
$generator = $generation_definitions[$field_name];
foreach ( array_keys( $generation_definitions ) as $field_name ) {
if ( ! isset( $args[ $field_name ] ) ) {
$generator = $generation_definitions[ $field_name ];
if ( is_scalar( $generator ) ) {
$args[$field_name] = $generator;
$args[ $field_name ] = $generator;
} elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
$callbacks[$field_name] = $generator;
$callbacks[ $field_name ] = $generator;
} elseif ( is_object( $generator ) ) {
$args[ $field_name ] = sprintf( $generator->get_template_string(), $incr );
} else {
@@ -86,8 +90,8 @@ abstract class WP_UnitTest_Factory_For_Thing {
function apply_callbacks( $callbacks, $created ) {
$updated_fields = array();
foreach( $callbacks as $field_name => $generator ) {
$updated_fields[$field_name] = $generator->call( $created );
foreach ( $callbacks as $field_name => $generator ) {
$updated_fields[ $field_name ] = $generator->call( $created );
}
return $updated_fields;
}
@@ -96,12 +100,12 @@ abstract class WP_UnitTest_Factory_For_Thing {
return new WP_UnitTest_Factory_Callback_After_Create( $function );
}
function addslashes_deep($value) {
function addslashes_deep( $value ) {
if ( is_array( $value ) ) {
$value = array_map( array( $this, 'addslashes_deep' ), $value );
} elseif ( is_object( $value ) ) {
$vars = get_object_vars( $value );
foreach ($vars as $key=>$data) {
foreach ( $vars as $key => $data ) {
$value->{$key} = $this->addslashes_deep( $data );
}
} elseif ( is_string( $value ) ) {
@@ -16,7 +16,7 @@ class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
parent::__construct( $factory );
$this->default_generation_definitions = array(
'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
'user_pass' => 'password',
'user_pass' => 'password',
'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
);
}
@@ -59,16 +59,16 @@ class WP_UnitTest_Factory {
public $network;
function __construct() {
$this->post = new WP_UnitTest_Factory_For_Post( $this );
$this->post = new WP_UnitTest_Factory_For_Post( $this );
$this->attachment = new WP_UnitTest_Factory_For_Attachment( $this );
$this->comment = new WP_UnitTest_Factory_For_Comment( $this );
$this->user = new WP_UnitTest_Factory_For_User( $this );
$this->term = new WP_UnitTest_Factory_For_Term( $this );
$this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
$this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
$this->bookmark = new WP_UnitTest_Factory_For_Bookmark( $this );
$this->comment = new WP_UnitTest_Factory_For_Comment( $this );
$this->user = new WP_UnitTest_Factory_For_User( $this );
$this->term = new WP_UnitTest_Factory_For_Term( $this );
$this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
$this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
$this->bookmark = new WP_UnitTest_Factory_For_Bookmark( $this );
if ( is_multisite() ) {
$this->blog = new WP_UnitTest_Factory_For_Blog( $this );
$this->blog = new WP_UnitTest_Factory_For_Blog( $this );
$this->network = new WP_UnitTest_Factory_For_Network( $this );
}
}
@@ -16,7 +16,7 @@ class WP_UnitTest_Generator_Sequence {
}
function next() {
$generated = sprintf( $this->template_string , $this->next );
$generated = sprintf( $this->template_string, $this->next );
$this->next++;
return $generated;
}
+23 -19
View File
@@ -20,34 +20,38 @@ function tests_reset__SERVER() {
}
// For adding hooks before loading WP
function tests_add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
function tests_add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
global $wp_filter;
if ( function_exists( 'add_filter' ) ) {
add_filter( $tag, $function_to_add, $priority, $accepted_args );
} else {
$idx = _test_filter_build_unique_id($tag, $function_to_add, $priority);
$wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
$idx = _test_filter_build_unique_id( $tag, $function_to_add, $priority );
$wp_filter[ $tag ][ $priority ][ $idx ] = array(
'function' => $function_to_add,
'accepted_args' => $accepted_args,
);
}
return true;
}
function _test_filter_build_unique_id($tag, $function, $priority) {
if ( is_string($function) )
function _test_filter_build_unique_id( $tag, $function, $priority ) {
if ( is_string( $function ) ) {
return $function;
}
if ( is_object($function) ) {
if ( is_object( $function ) ) {
// Closures are currently implemented as objects
$function = array( $function, '' );
} else {
$function = (array) $function;
}
if (is_object($function[0]) ) {
return spl_object_hash($function[0]) . $function[1];
} else if ( is_string($function[0]) ) {
if ( is_object( $function[0] ) ) {
return spl_object_hash( $function[0] ) . $function[1];
} elseif ( is_string( $function[0] ) ) {
// Static Calling
return $function[0].$function[1];
return $function[0] . $function[1];
}
}
@@ -60,14 +64,14 @@ function _delete_all_data() {
$wpdb->comments,
$wpdb->commentmeta,
$wpdb->term_relationships,
$wpdb->termmeta
$wpdb->termmeta,
) as $table ) {
$wpdb->query( "DELETE FROM {$table}" );
}
foreach ( array(
$wpdb->terms,
$wpdb->term_taxonomy
$wpdb->term_taxonomy,
) as $table ) {
$wpdb->query( "DELETE FROM {$table} WHERE term_id != 1" );
}
@@ -96,8 +100,8 @@ function _delete_all_posts() {
}
function _wp_die_handler( $message, $title = '', $args = array() ) {
if ( !$GLOBALS['_wp_die_disabled'] ) {
_wp_die_handler_txt( $message, $title, $args);
if ( ! $GLOBALS['_wp_die_disabled'] ) {
_wp_die_handler_txt( $message, $title, $args );
} else {
//Ignore at our peril
}
@@ -125,7 +129,7 @@ function _wp_die_handler_txt( $message, $title, $args ) {
echo "Title : $title\n";
if ( ! empty( $args ) ) {
echo "Args: \n";
foreach( $args as $k => $v ){
foreach ( $args as $k => $v ) {
echo "\t $k : $v\n";
}
}
@@ -137,7 +141,7 @@ function _wp_die_handler_exit( $message, $title, $args ) {
echo "Title : $title\n";
if ( ! empty( $args ) ) {
echo "Args: \n";
foreach( $args as $k => $v ){
foreach ( $args as $k => $v ) {
echo "\t $k : $v\n";
}
}
@@ -163,8 +167,8 @@ function _upload_dir_no_subdir( $uploads ) {
$subdir = $uploads['subdir'];
$uploads['subdir'] = '';
$uploads['path'] = str_replace( $subdir, '', $uploads['path'] );
$uploads['url'] = str_replace( $subdir, '', $uploads['url'] );
$uploads['path'] = str_replace( $subdir, '', $uploads['path'] );
$uploads['url'] = str_replace( $subdir, '', $uploads['url'] );
return $uploads;
}
@@ -173,7 +177,7 @@ function _upload_dir_no_subdir( $uploads ) {
* Helper used with the `upload_dir` filter to set https upload URL.
*/
function _upload_dir_https( $uploads ) {
$uploads['url'] = str_replace( 'http://', 'https://', $uploads['url'] );
$uploads['url'] = str_replace( 'http://', 'https://', $uploads['url'] );
$uploads['baseurl'] = str_replace( 'http://', 'https://', $uploads['baseurl'] );
return $uploads;
+8 -7
View File
@@ -7,7 +7,7 @@
error_reporting( E_ALL & ~E_DEPRECATED & ~E_STRICT );
$config_file_path = $argv[1];
$multisite = ! empty( $argv[2] );
$multisite = ! empty( $argv[2] );
define( 'WP_INSTALLING', true );
require_once $config_file_path;
@@ -47,9 +47,9 @@ if ( version_compare( $wpdb->db_version(), '5.5.3', '>=' ) ) {
}
$wpdb->select( DB_NAME, $wpdb->dbh );
echo "Installing..." . PHP_EOL;
echo 'Installing...' . PHP_EOL;
$wpdb->query( "SET foreign_key_checks = 0" );
$wpdb->query( 'SET foreign_key_checks = 0' );
foreach ( $wpdb->tables() as $table => $prefixed_table ) {
$wpdb->query( "DROP TABLE IF EXISTS $prefixed_table" );
}
@@ -58,10 +58,11 @@ foreach ( $wpdb->tables( 'ms_global' ) as $table => $prefixed_table ) {
$wpdb->query( "DROP TABLE IF EXISTS $prefixed_table" );
// We need to create references to ms global tables.
if ( $multisite )
if ( $multisite ) {
$wpdb->$table = $prefixed_table;
}
}
$wpdb->query( "SET foreign_key_checks = 1" );
$wpdb->query( 'SET foreign_key_checks = 1' );
// Prefill a permalink structure so that WP doesn't try to determine one itself.
add_action( 'populate_options', '_set_default_permalink_structure_for_tests' );
@@ -75,11 +76,11 @@ if ( ! is_multisite() ) {
remove_action( 'populate_options', '_set_default_permalink_structure_for_tests' );
if ( $multisite ) {
echo "Installing network..." . PHP_EOL;
echo 'Installing network...' . PHP_EOL;
define( 'WP_INSTALLING_NETWORK', true );
$title = WP_TESTS_TITLE . ' Network';
$title = WP_TESTS_TITLE . ' Network';
$subdomain_install = false;
install_network();
+45 -30
View File
@@ -10,8 +10,8 @@ class WP_Filesystem_MockFS extends WP_Filesystem_Base {
private $fs_map = array();
public $verbose = false; // Enable to debug WP_Filesystem_Base::find_folder() / etc.
public $errors = array();
public $method = 'MockFS';
public $errors = array();
public $method = 'MockFS';
function __construct() {}
@@ -21,13 +21,15 @@ class WP_Filesystem_MockFS extends WP_Filesystem_Base {
// Copy of core's function, but accepts a path.
function abspath( $path = false ) {
if ( ! $path )
if ( ! $path ) {
$path = ABSPATH;
}
$folder = $this->find_folder( $path );
// Perhaps the FTP folder is rooted at the WordPress installation, Check for wp-includes folder in root, Could have some false positives, but rare.
if ( ! $folder && $this->is_dir('/wp-includes') )
if ( ! $folder && $this->is_dir( '/wp-includes' ) ) {
$folder = '/';
}
return $folder;
}
@@ -38,12 +40,12 @@ class WP_Filesystem_MockFS extends WP_Filesystem_Base {
* Can also be passed the initial filesystem to be setup which is passed to self::setfs()
*/
function init( $paths = '', $home_dir = '/' ) {
$this->fs = new MockFS_Directory_Node( '/' );
$this->fs = new MockFS_Directory_Node( '/' );
$this->fs_map = array(
'/' => $this->fs,
);
$this->cache = array(); // Used by find_folder() and friends
$this->cwd = isset( $this->fs_map[ $home_dir ] ) ? $this->fs_map[ $home_dir ] : '/';
$this->cache = array(); // Used by find_folder() and friends
$this->cwd = isset( $this->fs_map[ $home_dir ] ) ? $this->fs_map[ $home_dir ] : '/';
$this->setfs( $paths );
}
@@ -51,21 +53,24 @@ class WP_Filesystem_MockFS extends WP_Filesystem_Base {
* "Bulk Loads" a filesystem into the internal virtual filesystem
*/
function setfs( $paths ) {
if ( ! is_array($paths) )
if ( ! is_array( $paths ) ) {
$paths = explode( "\n", $paths );
}
$paths = array_filter( array_map( 'trim', $paths ) );
foreach ( $paths as $path ) {
// Allow for comments
if ( '#' == $path[0] )
if ( '#' == $path[0] ) {
continue;
}
// Directories
if ( '/' == $path[ strlen($path) -1 ] )
if ( '/' == $path[ strlen( $path ) - 1 ] ) {
$this->mkdir( $path );
else // Files (with dummy content for now)
} else { // Files (with dummy content for now)
$this->put_contents( $path, 'This is a test file' );
}
}
}
@@ -95,32 +100,35 @@ class WP_Filesystem_MockFS extends WP_Filesystem_Base {
$dirname = str_replace( '\\', '/', dirname( $path ) );
$this->mkdir( $dirname );
$parent_node = $this->locate_parent_node( $path );
if ( ! $parent_node )
if ( ! $parent_node ) {
return false;
}
}
$node = new MockFS_Directory_Node( $path );
$parent_node->children[ $node->name ] = $node;
$this->fs_map[ $path ] = $node;
$this->fs_map[ $path ] = $node;
return true;
}
function put_contents( $path, $contents = '', $mode = null ) {
if ( ! $this->is_dir( dirname( $path ) ) )
if ( ! $this->is_dir( dirname( $path ) ) ) {
$this->mkdir( dirname( $path ) );
}
$parent = $this->locate_parent_node( $path );
$parent = $this->locate_parent_node( $path );
$new_file = new MockFS_File_Node( $path, $contents );
$parent->children[ $new_file->name ] = $new_file;
$this->fs_map[ $path ] = $new_file;
$this->fs_map[ $path ] = $new_file;
}
function get_contents( $file ) {
if ( ! $this->is_file( $file ) )
if ( ! $this->is_file( $file ) ) {
return false;
}
return $this->fs_map[ $file ]->contents;
}
@@ -129,8 +137,9 @@ class WP_Filesystem_MockFS extends WP_Filesystem_Base {
}
function chdir( $path ) {
if ( ! isset( $this->fs_map[ $path ] ) )
if ( ! isset( $this->fs_map[ $path ] ) ) {
return false;
}
$this->cwd = $this->fs_map[ $path ];
return true;
@@ -152,38 +161,44 @@ class WP_Filesystem_MockFS extends WP_Filesystem_Base {
function dirlist( $path = '.', $include_hidden = true, $recursive = false ) {
if ( empty( $path ) || '.' == $path )
if ( empty( $path ) || '.' == $path ) {
$path = $this->cwd();
}
if ( ! $this->exists( $path ) )
if ( ! $this->exists( $path ) ) {
return false;
}
$limit_file = false;
if ( $this->is_file( $path ) ) {
$limit_file = $this->locate_node( $path )->name;
$path = dirname( $path ) . '/';
$path = dirname( $path ) . '/';
}
$ret = array();
foreach ( $this->fs_map[ $path ]->children as $entry ) {
if ( '.' == $entry->name || '..' == $entry->name )
if ( '.' == $entry->name || '..' == $entry->name ) {
continue;
}
if ( ! $include_hidden && '.' == $entry->name )
if ( ! $include_hidden && '.' == $entry->name ) {
continue;
}
if ( $limit_file && $entry->name != $limit_file )
if ( $limit_file && $entry->name != $limit_file ) {
continue;
}
$struc = array();
$struc = array();
$struc['name'] = $entry->name;
$struc['type'] = $entry->type;
if ( 'd' == $struc['type'] ) {
if ( $recursive )
if ( $recursive ) {
$struc['files'] = $this->dirlist( trailingslashit( $path ) . trailingslashit( $struc['name'] ), $include_hidden, $recursive );
else
} else {
$struc['files'] = array();
}
}
$ret[ $entry->name ] = $struc;
@@ -213,16 +228,16 @@ class MockFS_Node {
}
class MockFS_Directory_Node extends MockFS_Node {
public $type = 'd';
public $type = 'd';
public $children = array(); // The child nodes of this directory
}
class MockFS_File_Node extends MockFS_Node {
public $type = 'f';
public $type = 'f';
public $contents = ''; // The contents of the file
function __construct( $path, $contents = '' ) {
parent::__construct( $path );
$this->contents = $contents;
}
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
<?php
if (class_exists( 'WP_Image_Editor' ) ) :
if ( class_exists( 'WP_Image_Editor' ) ) :
class WP_Image_Editor_Mock extends WP_Image_Editor {
+2 -2
View File
@@ -53,10 +53,10 @@ class MockPHPMailer extends PHPMailer {
*/
public function get_recipient( $address_type, $mock_sent_index = 0, $recipient_index = 0 ) {
$retval = false;
$mock = $this->get_sent( $mock_sent_index );
$mock = $this->get_sent( $mock_sent_index );
if ( $mock ) {
if ( isset( $mock->{$address_type}[ $recipient_index ] ) ) {
$address_index = $mock->{$address_type}[ $recipient_index ];
$address_index = $mock->{$address_type}[ $recipient_index ];
$recipient_data = array(
'address' => ( isset( $address_index[0] ) && ! empty( $address_index[0] ) ) ? $address_index[0] : 'No address set',
'name' => ( isset( $address_index[1] ) && ! empty( $address_index[1] ) ) ? $address_index[1] : 'No name set',
+189 -133
View File
@@ -306,10 +306,11 @@ function wp_cache_flush( $delay = 0 ) {
function wp_cache_get( $key, $group = '', $force = false, &$found = null, $cache_cb = null, &$cas_token = null ) {
global $wp_object_cache;
if ( func_num_args() > 4 )
if ( func_num_args() > 4 ) {
return $wp_object_cache->get( $key, $group, $force, $found, '', false, $cache_cb, $cas_token );
else
} else {
return $wp_object_cache->get( $key, $group, $force, $found );
}
}
/**
@@ -331,13 +332,14 @@ function wp_cache_get( $key, $group = '', $force = false, &$found = null, $cache
* @param null|float $cas_token The variable to store the CAS token in.
* @return bool|mixed Cached object value.
*/
function wp_cache_get_by_key( $server_key, $key, $group = '', $force = false, &$found = null, $cache_cb = NULL, &$cas_token = NULL ) {
function wp_cache_get_by_key( $server_key, $key, $group = '', $force = false, &$found = null, $cache_cb = null, &$cas_token = null ) {
global $wp_object_cache;
if ( func_num_args() > 5 )
if ( func_num_args() > 5 ) {
return $wp_object_cache->getByKey( $server_key, $key, $group, $force, $found, $cache_cb, $cas_token );
else
} else {
return $wp_object_cache->getByKey( $server_key, $key, $group, $force, $found );
}
}
/**
@@ -351,7 +353,7 @@ function wp_cache_get_by_key( $server_key, $key, $group = '', $force = false, &$
* @param null $value_cb The result callback or NULL.
* @return bool Returns TRUE on success or FALSE on failure.
*/
function wp_cache_get_delayed( $keys, $groups = '', $with_cas = false, $value_cb = NULL ) {
function wp_cache_get_delayed( $keys, $groups = '', $with_cas = false, $value_cb = null ) {
global $wp_object_cache;
return $wp_object_cache->getDelayed( $keys, $groups, $with_cas, $value_cb );
}
@@ -368,7 +370,7 @@ function wp_cache_get_delayed( $keys, $groups = '', $with_cas = false, $value_cb
* @param null $value_cb The result callback or NULL.
* @return bool Returns TRUE on success or FALSE on failure.
*/
function wp_cache_get_delayed_by_key( $server_key, $keys, $groups = '', $with_cas = false, $value_cb = NULL ) {
function wp_cache_get_delayed_by_key( $server_key, $keys, $groups = '', $with_cas = false, $value_cb = null ) {
global $wp_object_cache;
return $wp_object_cache->getDelayedByKey( $server_key, $keys, $groups, $with_cas, $value_cb );
}
@@ -386,13 +388,14 @@ function wp_cache_get_delayed_by_key( $server_key, $keys, $groups = '', $with_ca
* @param int $flags The flags for the get operation.
* @return bool|array Returns the array of found items or FALSE on failure.
*/
function wp_cache_get_multi( $keys, $groups = '', &$cas_tokens = NULL, $flags = NULL ) {
function wp_cache_get_multi( $keys, $groups = '', &$cas_tokens = null, $flags = null ) {
global $wp_object_cache;
if ( func_num_args() > 2 )
if ( func_num_args() > 2 ) {
return $wp_object_cache->getMulti( $keys, $groups, '', $cas_tokens, $flags );
else
} else {
return $wp_object_cache->getMulti( $keys, $groups );
}
}
/**
@@ -409,13 +412,14 @@ function wp_cache_get_multi( $keys, $groups = '', &$cas_tokens = NULL, $flags =
* @param int $flags The flags for the get operation.
* @return bool|array Returns the array of found items or FALSE on failure.
*/
function wp_cache_get_multi_by_key( $server_key, $keys, $groups = '', &$cas_tokens = NULL, $flags = NULL ) {
function wp_cache_get_multi_by_key( $server_key, $keys, $groups = '', &$cas_tokens = null, $flags = null ) {
global $wp_object_cache;
if ( func_num_args() > 3 )
if ( func_num_args() > 3 ) {
return $wp_object_cache->getMultiByKey( $server_key, $keys, $groups, $cas_tokens, $flags );
else
} else {
return $wp_object_cache->getMultiByKey( $server_key, $keys, $groups );
}
}
/**
@@ -818,18 +822,20 @@ class WP_Object_Cache {
*
* @param null $persistent_id To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance.
*/
public function __construct( $persistent_id = NULL ) {
public function __construct( $persistent_id = null ) {
global $memcached_servers, $blog_id, $table_prefix;
if ( is_null( $persistent_id ) || ! is_string( $persistent_id ) )
if ( is_null( $persistent_id ) || ! is_string( $persistent_id ) ) {
$this->m = new Memcached();
else
} else {
$this->m = new Memcached( $persistent_id );
}
if ( isset( $memcached_servers ) )
if ( isset( $memcached_servers ) ) {
$this->servers = $memcached_servers;
else
} else {
$this->servers = array( array( '127.0.0.1', 11211 ) );
}
$this->addServers( $this->servers );
@@ -837,13 +843,14 @@ class WP_Object_Cache {
* This approach is borrowed from Sivel and Boren. Use the salt for easy cache invalidation and for
* multi single WP installations on the same server.
*/
if ( ! defined( 'WP_CACHE_KEY_SALT' ) )
if ( ! defined( 'WP_CACHE_KEY_SALT' ) ) {
define( 'WP_CACHE_KEY_SALT', '' );
}
// Assign global and blog prefixes for use with keys
if ( function_exists( 'is_multisite' ) ) {
$this->global_prefix = ( is_multisite() || defined( 'CUSTOM_USER_TABLE' ) && defined( 'CUSTOM_USER_META_TABLE' ) ) ? '' : $table_prefix;
$this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':';
$this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':';
}
// Setup cacheable values for handling expiration times
@@ -885,8 +892,9 @@ class WP_Object_Cache {
if ( in_array( $group, $this->no_mc_groups ) ) {
// Add does not set the value if the key exists; mimic that here
if ( isset( $this->cache[$derived_key] ) )
if ( isset( $this->cache[ $derived_key ] ) ) {
return false;
}
$this->add_to_internal_cache( $derived_key, $value );
@@ -894,14 +902,16 @@ class WP_Object_Cache {
}
// Save to Memcached
if ( $byKey )
if ( $byKey ) {
$result = $this->m->addByKey( $server_key, $derived_key, $value, $expiration );
else
} else {
$result = $this->m->add( $derived_key, $value, $expiration );
}
// Store in runtime cache if add was successful
if ( Memcached::RES_SUCCESS === $this->getResultCode() )
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->add_to_internal_cache( $derived_key, $value );
}
return $result;
}
@@ -938,8 +948,8 @@ class WP_Object_Cache {
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function addServer( $host, $port, $weight = 0 ) {
$host = is_string( $host ) ? $host : '127.0.0.1';
$port = is_numeric( $port ) && $port > 0 ? $port : 11211;
$host = is_string( $host ) ? $host : '127.0.0.1';
$port = is_numeric( $port ) && $port > 0 ? $port : 11211;
$weight = is_numeric( $weight ) && $weight > 0 ? $weight : 1;
return $this->m->addServer( $host, $port, $weight );
@@ -957,8 +967,9 @@ class WP_Object_Cache {
* @return bool True on success; false on failure.
*/
public function addServers( $servers ) {
if ( ! is_object( $this->m ) )
if ( ! is_object( $this->m ) ) {
return false;
}
return $this->m->addServers( $servers );
}
@@ -983,30 +994,33 @@ class WP_Object_Cache {
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function append( $key, $value, $group = 'default', $server_key = '', $byKey = false ) {
if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) )
if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) ) {
return false;
}
$derived_key = $this->buildKey( $key, $group );
// If group is a non-Memcached group, append to runtime cache value, not Memcached
if ( in_array( $group, $this->no_mc_groups ) ) {
if ( ! isset( $this->cache[$derived_key] ) )
if ( ! isset( $this->cache[ $derived_key ] ) ) {
return false;
}
$combined = $this->combine_values( $this->cache[$derived_key], $value, 'app' );
$combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'app' );
$this->add_to_internal_cache( $derived_key, $combined );
return true;
}
// Append to Memcached value
if ( $byKey )
if ( $byKey ) {
$result = $this->m->appendByKey( $server_key, $derived_key, $value );
else
} else {
$result = $this->m->append( $derived_key, $value );
}
// Store in runtime cache if add was successful
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$combined = $this->combine_values( $this->cache[$derived_key], $value, 'app' );
$combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'app' );
$this->add_to_internal_cache( $derived_key, $combined );
}
@@ -1067,14 +1081,16 @@ class WP_Object_Cache {
}
// Save to Memcached
if ( $byKey )
if ( $byKey ) {
$result = $this->m->casByKey( $cas_token, $server_key, $derived_key, $value, $expiration );
else
} else {
$result = $this->m->cas( $cas_token, $derived_key, $value, $expiration );
}
// Store in runtime cache if cas was successful
if ( Memcached::RES_SUCCESS === $this->getResultCode() )
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->add_to_internal_cache( $derived_key, $value );
}
return $result;
}
@@ -1116,19 +1132,21 @@ class WP_Object_Cache {
if ( in_array( $group, $this->no_mc_groups ) ) {
// Only decrement if the key already exists and value is 0 or greater (mimics memcached behavior)
if ( isset( $this->cache[$derived_key] ) && $this->cache[$derived_key] >= 0 ) {
if ( isset( $this->cache[ $derived_key ] ) && $this->cache[ $derived_key ] >= 0 ) {
// If numeric, subtract; otherwise, consider it 0 and do nothing
if ( is_numeric( $this->cache[$derived_key] ) )
$this->cache[$derived_key] -= (int) $offset;
else
$this->cache[$derived_key] = 0;
if ( is_numeric( $this->cache[ $derived_key ] ) ) {
$this->cache[ $derived_key ] -= (int) $offset;
} else {
$this->cache[ $derived_key ] = 0;
}
// Returned value cannot be less than 0
if ( $this->cache[$derived_key] < 0 )
$this->cache[$derived_key] = 0;
if ( $this->cache[ $derived_key ] < 0 ) {
$this->cache[ $derived_key ] = 0;
}
return $this->cache[$derived_key];
return $this->cache[ $derived_key ];
} else {
return false;
}
@@ -1136,8 +1154,9 @@ class WP_Object_Cache {
$result = $this->m->decrement( $derived_key, $offset );
if ( Memcached::RES_SUCCESS === $this->getResultCode() )
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->add_to_internal_cache( $derived_key, $result );
}
return $result;
}
@@ -1179,19 +1198,22 @@ class WP_Object_Cache {
// Remove from no_mc_groups array
if ( in_array( $group, $this->no_mc_groups ) ) {
if ( isset( $this->cache[$derived_key] ) )
unset( $this->cache[$derived_key] );
if ( isset( $this->cache[ $derived_key ] ) ) {
unset( $this->cache[ $derived_key ] );
}
return true;
}
if ( $byKey )
if ( $byKey ) {
$result = $this->m->deleteByKey( $server_key, $derived_key, $time );
else
} else {
$result = $this->m->delete( $derived_key, $time );
}
if ( Memcached::RES_SUCCESS === $this->getResultCode() )
unset( $this->cache[$derived_key] );
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
unset( $this->cache[ $derived_key ] );
}
return $result;
}
@@ -1235,7 +1257,7 @@ class WP_Object_Cache {
* @return array|bool Returns the results or FALSE on failure.
*/
public function fetchAll() {
return $this->m->fetchAll();
return $this->m->fetchAll();
}
/**
@@ -1250,8 +1272,9 @@ class WP_Object_Cache {
$result = $this->m->flush( $delay );
// Only reset the runtime cache if memcached was properly flushed
if ( Memcached::RES_SUCCESS === $this->getResultCode() )
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->cache = array();
}
return $result;
}
@@ -1280,7 +1303,7 @@ class WP_Object_Cache {
* @param null|float $cas_token The variable to store the CAS token in.
* @return bool|mixed Cached object value.
*/
public function get( $key, $group = 'default', $force = false, &$found = null, $server_key = '', $byKey = false, $cache_cb = NULL, &$cas_token = NULL ) {
public function get( $key, $group = 'default', $force = false, &$found = null, $server_key = '', $byKey = false, $cache_cb = null, &$cas_token = null ) {
$derived_key = $this->buildKey( $key, $group );
// Assume object is not found
@@ -1288,21 +1311,23 @@ class WP_Object_Cache {
// If either $cache_db, or $cas_token is set, must hit Memcached and bypass runtime cache
if ( func_num_args() > 6 && ! in_array( $group, $this->no_mc_groups ) ) {
if ( $byKey )
if ( $byKey ) {
$value = $this->m->getByKey( $server_key, $derived_key, $cache_cb, $cas_token );
else
} else {
$value = $this->m->get( $derived_key, $cache_cb, $cas_token );
}
} else {
if ( isset( $this->cache[$derived_key] ) ) {
if ( isset( $this->cache[ $derived_key ] ) ) {
$found = true;
return is_object( $this->cache[$derived_key] ) ? clone $this->cache[$derived_key] : $this->cache[$derived_key];
return is_object( $this->cache[ $derived_key ] ) ? clone $this->cache[ $derived_key ] : $this->cache[ $derived_key ];
} elseif ( in_array( $group, $this->no_mc_groups ) ) {
return false;
} else {
if ( $byKey )
if ( $byKey ) {
$value = $this->m->getByKey( $server_key, $derived_key );
else
} else {
$value = $this->m->get( $derived_key );
}
}
}
@@ -1337,15 +1362,16 @@ class WP_Object_Cache {
* @param null|float $cas_token The variable to store the CAS token in.
* @return bool|mixed Cached object value.
*/
public function getByKey( $server_key, $key, $group = 'default', $force = false, &$found = null, $cache_cb = NULL, &$cas_token = NULL ) {
public function getByKey( $server_key, $key, $group = 'default', $force = false, &$found = null, $cache_cb = null, &$cas_token = null ) {
/**
* Need to be careful how "get" is called. If you send $cache_cb, and $cas_token, it will hit memcached.
* Only send those args if they were sent to this function.
*/
if ( func_num_args() > 5 )
if ( func_num_args() > 5 ) {
return $this->get( $key, $group, $force, $found, $server_key, true, $cache_cb, $cas_token );
else
} else {
return $this->get( $key, $group, $force, $found, $server_key, true );
}
}
/**
@@ -1359,7 +1385,7 @@ class WP_Object_Cache {
* @param null $value_cb The result callback or NULL.
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function getDelayed( $keys, $groups = 'default', $with_cas = false, $value_cb = NULL ) {
public function getDelayed( $keys, $groups = 'default', $with_cas = false, $value_cb = null ) {
$derived_keys = $this->buildKeys( $keys, $groups );
return $this->m->getDelayed( $derived_keys, $with_cas, $value_cb );
}
@@ -1376,7 +1402,7 @@ class WP_Object_Cache {
* @param null $value_cb The result callback or NULL.
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function getDelayedByKey( $server_key, $keys, $groups = 'default', $with_cas = false, $value_cb = NULL ) {
public function getDelayedByKey( $server_key, $keys, $groups = 'default', $with_cas = false, $value_cb = null ) {
$derived_keys = $this->buildKeys( $keys, $groups );
return $this->m->getDelayedByKey( $server_key, $derived_keys, $with_cas, $value_cb );
}
@@ -1395,7 +1421,7 @@ class WP_Object_Cache {
* @param int $flags The flags for the get operation.
* @return bool|array Returns the array of found items or FALSE on failure.
*/
public function getMulti( $keys, $groups = 'default', $server_key = '', &$cas_tokens = NULL, $flags = NULL ) {
public function getMulti( $keys, $groups = 'default', $server_key = '', &$cas_tokens = null, $flags = null ) {
$derived_keys = $this->buildKeys( $keys, $groups );
/**
@@ -1404,41 +1430,46 @@ class WP_Object_Cache {
* flags; however, if the groups of groups contains a no_mc_group, this is bypassed.
*/
if ( func_num_args() > 3 && ! $this->contains_no_mc_group( $groups ) ) {
if ( ! empty( $server_key ) )
if ( ! empty( $server_key ) ) {
$values = $this->m->getMultiByKey( $server_key, $derived_keys, $cas_tokens, $flags );
else
} else {
$values = $this->m->getMulti( $derived_keys, $cas_tokens, $flags );
}
} else {
$values = array();
$values = array();
$need_to_get = array();
// Pull out values from runtime cache, or mark for retrieval
foreach ( $derived_keys as $key ) {
if ( isset( $this->cache[$key] ) )
$values[$key] = $this->cache[$key];
else
$need_to_get[$key] = $key;
if ( isset( $this->cache[ $key ] ) ) {
$values[ $key ] = $this->cache[ $key ];
} else {
$need_to_get[ $key ] = $key;
}
}
// Get those keys not found in the runtime cache
if ( ! empty( $need_to_get ) ) {
if ( ! empty( $server_key ) )
if ( ! empty( $server_key ) ) {
$result = $this->m->getMultiByKey( $server_key, array_keys( $need_to_get ) );
else
} else {
$result = $this->m->getMulti( array_keys( $need_to_get ) );
}
}
// Merge with values found in runtime cache
if ( isset( $result ) && Memcached::RES_SUCCESS === $this->getResultCode() )
if ( isset( $result ) && Memcached::RES_SUCCESS === $this->getResultCode() ) {
$values = array_merge( $values, $result );
}
// If order should be preserved, reorder now
if ( ! empty( $need_to_get ) && $flags === Memcached::GET_PRESERVE_ORDER ) {
$ordered_values = array();
foreach ( $derived_keys as $key ) {
if ( isset( $values[$key] ) )
$ordered_values[$key] = $values[$key];
if ( isset( $values[ $key ] ) ) {
$ordered_values[ $key ] = $values[ $key ];
}
}
$values = $ordered_values;
@@ -1466,15 +1497,16 @@ class WP_Object_Cache {
* @param int $flags The flags for the get operation.
* @return bool|array Returns the array of found items or FALSE on failure.
*/
public function getMultiByKey( $server_key, $keys, $groups = 'default', &$cas_tokens = NULL, $flags = NULL ) {
public function getMultiByKey( $server_key, $keys, $groups = 'default', &$cas_tokens = null, $flags = null ) {
/**
* Need to be careful how "getMulti" is called. If you send $cache_cb, and $cas_token, it will hit memcached.
* Only send those args if they were sent to this function.
*/
if ( func_num_args() > 3 )
if ( func_num_args() > 3 ) {
return $this->getMulti( $keys, $groups, $server_key, $cas_tokens, $flags );
else
} else {
return $this->getMulti( $keys, $groups, $server_key );
}
}
/**
@@ -1497,7 +1529,7 @@ class WP_Object_Cache {
* @return int Result code of the last Memcached operation.
*/
public function getResultCode() {
return $this->m->getResultCode();
return $this->m->getResultCode();
}
/**
@@ -1508,7 +1540,7 @@ class WP_Object_Cache {
* @return string Message describing the result of the last Memcached operation.
*/
public function getResultMessage() {
return $this->m->getResultMessage();
return $this->m->getResultMessage();
}
/**
@@ -1535,7 +1567,7 @@ class WP_Object_Cache {
}
/**
* Get server pool statistics.
* Get server pool statistics.
*
* @link http://www.php.net/manual/en/memcached.getstats.php
*
@@ -1573,19 +1605,21 @@ class WP_Object_Cache {
if ( in_array( $group, $this->no_mc_groups ) ) {
// Only increment if the key already exists and the number is currently 0 or greater (mimics memcached behavior)
if ( isset( $this->cache[$derived_key] ) && $this->cache[$derived_key] >= 0 ) {
if ( isset( $this->cache[ $derived_key ] ) && $this->cache[ $derived_key ] >= 0 ) {
// If numeric, add; otherwise, consider it 0 and do nothing
if ( is_numeric( $this->cache[$derived_key] ) )
$this->cache[$derived_key] += (int) $offset;
else
$this->cache[$derived_key] = 0;
if ( is_numeric( $this->cache[ $derived_key ] ) ) {
$this->cache[ $derived_key ] += (int) $offset;
} else {
$this->cache[ $derived_key ] = 0;
}
// Returned value cannot be less than 0
if ( $this->cache[$derived_key] < 0 )
$this->cache[$derived_key] = 0;
if ( $this->cache[ $derived_key ] < 0 ) {
$this->cache[ $derived_key ] = 0;
}
return $this->cache[$derived_key];
return $this->cache[ $derived_key ];
} else {
return false;
}
@@ -1593,8 +1627,9 @@ class WP_Object_Cache {
$result = $this->m->increment( $derived_key, $offset );
if ( Memcached::RES_SUCCESS === $this->getResultCode() )
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->add_to_internal_cache( $derived_key, $result );
}
return $result;
}
@@ -1636,30 +1671,33 @@ class WP_Object_Cache {
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function prepend( $key, $value, $group = 'default', $server_key = '', $byKey = false ) {
if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) )
if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) ) {
return false;
}
$derived_key = $this->buildKey( $key, $group );
// If group is a non-Memcached group, prepend to runtime cache value, not Memcached
if ( in_array( $group, $this->no_mc_groups ) ) {
if ( ! isset( $this->cache[$derived_key] ) )
if ( ! isset( $this->cache[ $derived_key ] ) ) {
return false;
}
$combined = $this->combine_values( $this->cache[$derived_key], $value, 'pre' );
$combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'pre' );
$this->add_to_internal_cache( $derived_key, $combined );
return true;
}
// Append to Memcached value
if ( $byKey )
if ( $byKey ) {
$result = $this->m->prependByKey( $server_key, $derived_key, $value );
else
} else {
$result = $this->m->prepend( $derived_key, $value );
}
// Store in runtime cache if add was successful
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$combined = $this->combine_values( $this->cache[$derived_key], $value, 'pre' );
$combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'pre' );
$this->add_to_internal_cache( $derived_key, $combined );
}
@@ -1713,22 +1751,25 @@ class WP_Object_Cache {
if ( in_array( $group, $this->no_mc_groups ) ) {
// Replace won't save unless the key already exists; mimic this behavior here
if ( ! isset( $this->cache[$derived_key] ) )
if ( ! isset( $this->cache[ $derived_key ] ) ) {
return false;
}
$this->cache[$derived_key] = $value;
$this->cache[ $derived_key ] = $value;
return true;
}
// Save to Memcached
if ( $byKey )
if ( $byKey ) {
$result = $this->m->replaceByKey( $server_key, $derived_key, $value, $expiration );
else
} else {
$result = $this->m->replace( $derived_key, $value, $expiration );
}
// Store in runtime cache if add was successful
if ( Memcached::RES_SUCCESS === $this->getResultCode() )
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->add_to_internal_cache( $derived_key, $value );
}
return $result;
}
@@ -1785,8 +1826,9 @@ class WP_Object_Cache {
}
// Store in runtime cache if add was successful
if ( Memcached::RES_SUCCESS === $this->getResultCode() )
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->add_to_internal_cache( $derived_key, $value );
}
return $result;
}
@@ -1842,19 +1884,21 @@ class WP_Object_Cache {
// If group is a non-Memcached group, save to runtime cache, not Memcached
if ( in_array( $key_pieces[1], $this->no_mc_groups ) ) {
$this->add_to_internal_cache( $derived_key, $value );
unset( $derived_items[$derived_key] );
unset( $derived_items[ $derived_key ] );
}
}
// Save to memcached
if ( $byKey )
if ( $byKey ) {
$result = $this->m->setMultiByKey( $server_key, $derived_items, $expiration );
else
} else {
$result = $this->m->setMulti( $derived_items, $expiration );
}
// Store in runtime cache if add was successful
if ( Memcached::RES_SUCCESS === $this->getResultCode() )
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->cache = array_merge( $this->cache, $derived_items );
}
return $result;
}
@@ -1904,13 +1948,15 @@ class WP_Object_Cache {
* @return string
*/
public function buildKey( $key, $group = 'default' ) {
if ( empty( $group ) )
if ( empty( $group ) ) {
$group = 'default';
}
if ( false !== array_search( $group, $this->global_groups ) )
if ( false !== array_search( $group, $this->global_groups ) ) {
$prefix = $this->global_prefix;
else
} else {
$prefix = $this->blog_prefix;
}
return preg_replace( '/\s+/', '', WP_CACHE_KEY_SALT . "$prefix$group:$key" );
}
@@ -1935,27 +1981,30 @@ class WP_Object_Cache {
$derived_keys = array();
// If strings sent, convert to arrays for proper handling
if ( ! is_array( $groups ) )
if ( ! is_array( $groups ) ) {
$groups = (array) $groups;
}
if ( ! is_array( $keys ) )
if ( ! is_array( $keys ) ) {
$keys = (array) $keys;
}
// If we have equal numbers of keys and groups, merge $keys[n] and $group[n]
if ( count( $keys ) == count( $groups ) ) {
for ( $i = 0; $i < count( $keys ); $i++ ) {
$derived_keys[] = $this->buildKey( $keys[$i], $groups[$i] );
$derived_keys[] = $this->buildKey( $keys[ $i ], $groups[ $i ] );
}
// If more keys are received than groups, merge $keys[n] and $group[n] until no more group are left; remaining groups are 'default'
// If more keys are received than groups, merge $keys[n] and $group[n] until no more group are left; remaining groups are 'default'
} elseif ( count( $keys ) > count( $groups ) ) {
for ( $i = 0; $i < count( $keys ); $i++ ) {
if ( isset( $groups[$i] ) )
$derived_keys[] = $this->buildKey( $keys[$i], $groups[$i] );
elseif ( count( $groups ) == 1 )
$derived_keys[] = $this->buildKey( $keys[$i], $groups[0] );
else
$derived_keys[] = $this->buildKey( $keys[$i], 'default' );
if ( isset( $groups[ $i ] ) ) {
$derived_keys[] = $this->buildKey( $keys[ $i ], $groups[ $i ] );
} elseif ( count( $groups ) == 1 ) {
$derived_keys[] = $this->buildKey( $keys[ $i ], $groups[0] );
} else {
$derived_keys[] = $this->buildKey( $keys[ $i ], 'default' );
}
}
}
@@ -1996,10 +2045,11 @@ class WP_Object_Cache {
$type = gettype( $original );
// Combine the values based on direction of the "pend"
if ( 'pre' == $direction )
if ( 'pre' == $direction ) {
$combined = $pended . $original;
else
} else {
$combined = $original . $pended;
}
// Cast type of combined value
settype( $combined, $type );
@@ -2018,7 +2068,7 @@ class WP_Object_Cache {
$value = clone $value;
}
$this->cache[$derived_key] = $value;
$this->cache[ $derived_key ] = $value;
}
/**
@@ -2028,15 +2078,18 @@ class WP_Object_Cache {
* @return bool True if a no_mc_group is present; false if a no_mc_group is not present.
*/
public function contains_no_mc_group( $groups ) {
if ( is_scalar( $groups ) )
if ( is_scalar( $groups ) ) {
return in_array( $groups, $this->no_mc_groups );
}
if ( ! is_array( $groups ) )
if ( ! is_array( $groups ) ) {
return false;
}
foreach ( $groups as $group ) {
if ( in_array( $group, $this->no_mc_groups ) )
if ( in_array( $group, $this->no_mc_groups ) ) {
return true;
}
}
return false;
@@ -2052,10 +2105,11 @@ class WP_Object_Cache {
* @return void
*/
public function add_global_groups( $groups ) {
if ( ! is_array( $groups ) )
if ( ! is_array( $groups ) ) {
$groups = (array) $groups;
}
$this->global_groups = array_merge( $this->global_groups, $groups);
$this->global_groups = array_merge( $this->global_groups, $groups );
$this->global_groups = array_unique( $this->global_groups );
}
@@ -2069,8 +2123,9 @@ class WP_Object_Cache {
* @return void
*/
public function add_non_persistent_groups( $groups ) {
if ( ! is_array( $groups ) )
if ( ! is_array( $groups ) ) {
$groups = (array) $groups;
}
$this->no_mc_groups = array_merge( $this->no_mc_groups, $groups );
$this->no_mc_groups = array_unique( $this->no_mc_groups );
@@ -2086,8 +2141,9 @@ class WP_Object_Cache {
public function get_from_runtime_cache( $key, $group ) {
$derived_key = $this->buildKey( $key, $group );
if ( isset( $this->cache[$derived_key] ) )
return $this->cache[$derived_key];
if ( isset( $this->cache[ $derived_key ] ) ) {
return $this->cache[ $derived_key ];
}
return false;
}
+11 -11
View File
@@ -2,18 +2,18 @@
if ( class_exists( 'PHPUnit\Runner\Version' ) && version_compare( PHPUnit\Runner\Version::id(), '6.0', '>=' ) ) {
class_alias( 'PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase' );
class_alias( 'PHPUnit\Framework\Exception', 'PHPUnit_Framework_Exception' );
class_alias( 'PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase' );
class_alias( 'PHPUnit\Framework\Exception', 'PHPUnit_Framework_Exception' );
class_alias( 'PHPUnit\Framework\ExpectationFailedException', 'PHPUnit_Framework_ExpectationFailedException' );
class_alias( 'PHPUnit\Framework\Error\Notice', 'PHPUnit_Framework_Error_Notice' );
class_alias( 'PHPUnit\Framework\Error\Warning', 'PHPUnit_Framework_Error_Warning' );
class_alias( 'PHPUnit\Framework\Test', 'PHPUnit_Framework_Test' );
class_alias( 'PHPUnit\Framework\Warning', 'PHPUnit_Framework_Warning' );
class_alias( 'PHPUnit\Framework\AssertionFailedError', 'PHPUnit_Framework_AssertionFailedError' );
class_alias( 'PHPUnit\Framework\TestSuite', 'PHPUnit_Framework_TestSuite' );
class_alias( 'PHPUnit\Framework\TestListener', 'PHPUnit_Framework_TestListener' );
class_alias( 'PHPUnit\Util\GlobalState', 'PHPUnit_Util_GlobalState' );
class_alias( 'PHPUnit\Util\Getopt', 'PHPUnit_Util_Getopt' );
class_alias( 'PHPUnit\Framework\Error\Notice', 'PHPUnit_Framework_Error_Notice' );
class_alias( 'PHPUnit\Framework\Error\Warning', 'PHPUnit_Framework_Error_Warning' );
class_alias( 'PHPUnit\Framework\Test', 'PHPUnit_Framework_Test' );
class_alias( 'PHPUnit\Framework\Warning', 'PHPUnit_Framework_Warning' );
class_alias( 'PHPUnit\Framework\AssertionFailedError', 'PHPUnit_Framework_AssertionFailedError' );
class_alias( 'PHPUnit\Framework\TestSuite', 'PHPUnit_Framework_TestSuite' );
class_alias( 'PHPUnit\Framework\TestListener', 'PHPUnit_Framework_TestListener' );
class_alias( 'PHPUnit\Util\GlobalState', 'PHPUnit_Util_GlobalState' );
class_alias( 'PHPUnit\Util\Getopt', 'PHPUnit_Util_Getopt' );
class PHPUnit_Util_Test extends PHPUnit\Util\Test {
@@ -8,7 +8,7 @@
*/
function tests_make_plural_form_function( $nplurals, $expression ) {
$expression = str_replace( 'n', '$n', $expression );
$func_body = "
$func_body = "
\$index = (int)($expression);
return (\$index < $nplurals)? \$index : $nplurals - 1;";
+263 -283
View File
@@ -4,323 +4,303 @@
* A PHPUnit TestListener that exposes your slowest running tests by outputting
* results directly to the console.
*/
class SpeedTrapListener implements PHPUnit_Framework_TestListener
{
/**
* Internal tracking for test suites.
*
* Increments as more suites are run, then decremented as they finish. All
* suites have been run when returns to 0.
*
* @var integer
*/
protected $suites = 0;
class SpeedTrapListener implements PHPUnit_Framework_TestListener {
/**
* Time in milliseconds at which a test will be considered "slow" and be
* reported by this listener.
*
* @var int
*/
protected $slowThreshold;
/**
* Internal tracking for test suites.
*
* Increments as more suites are run, then decremented as they finish. All
* suites have been run when returns to 0.
*
* @var integer
*/
protected $suites = 0;
/**
* Number of tests to report on for slowness.
*
* @var int
*/
protected $reportLength;
/**
* Time in milliseconds at which a test will be considered "slow" and be
* reported by this listener.
*
* @var int
*/
protected $slowThreshold;
/**
* Collection of slow tests.
*
* @var array
*/
protected $slow = array();
/**
* Number of tests to report on for slowness.
*
* @var int
*/
protected $reportLength;
/**
* Construct a new instance.
*
* @param array $options
*/
public function __construct(array $options = array())
{
$this->loadOptions($options);
}
/**
* Collection of slow tests.
*
* @var array
*/
protected $slow = array();
/**
* An error occurred.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
*/
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
/**
* Construct a new instance.
*
* @param array $options
*/
public function __construct( array $options = array() ) {
$this->loadOptions( $options );
}
/**
* A warning occurred.
*
* @param PHPUnit_Framework_Test $test
* @param PHPUnit_Framework_Warning $e
* @param float $time
* @since Method available since Release 5.1.0
*/
public function addWarning(PHPUnit_Framework_Test $test, PHPUnit_Framework_Warning $e, $time)
{
}
/**
* An error occurred.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
*/
public function addError( PHPUnit_Framework_Test $test, Exception $e, $time ) {
}
/**
* A failure occurred.
*
* @param PHPUnit_Framework_Test $test
* @param PHPUnit_Framework_AssertionFailedError $e
* @param float $time
*/
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
{
}
/**
* A warning occurred.
*
* @param PHPUnit_Framework_Test $test
* @param PHPUnit_Framework_Warning $e
* @param float $time
* @since Method available since Release 5.1.0
*/
public function addWarning( PHPUnit_Framework_Test $test, PHPUnit_Framework_Warning $e, $time ) {
}
/**
* Incomplete test.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
*/
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
/**
* A failure occurred.
*
* @param PHPUnit_Framework_Test $test
* @param PHPUnit_Framework_AssertionFailedError $e
* @param float $time
*/
public function addFailure( PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time ) {
}
/**
* Risky test.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
* @since Method available since Release 4.0.0
*/
public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
/**
* Incomplete test.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
*/
public function addIncompleteTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
}
/**
* Skipped test.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
*/
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
/**
* Risky test.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
* @since Method available since Release 4.0.0
*/
public function addRiskyTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
}
/**
* A test started.
*
* @param PHPUnit_Framework_Test $test
*/
public function startTest(PHPUnit_Framework_Test $test)
{
}
/**
* Skipped test.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
*/
public function addSkippedTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
}
/**
* A test ended.
*
* @param PHPUnit_Framework_Test $test
* @param float $time
*/
public function endTest(PHPUnit_Framework_Test $test, $time)
{
if (!$test instanceof PHPUnit_Framework_TestCase) return;
/**
* A test started.
*
* @param PHPUnit_Framework_Test $test
*/
public function startTest( PHPUnit_Framework_Test $test ) {
}
$time = $this->toMilliseconds($time);
$threshold = $this->getSlowThreshold($test);
/**
* A test ended.
*
* @param PHPUnit_Framework_Test $test
* @param float $time
*/
public function endTest( PHPUnit_Framework_Test $test, $time ) {
if ( ! $test instanceof PHPUnit_Framework_TestCase ) {
return;
}
if ($this->isSlow($time, $threshold)) {
$this->addSlowTest($test, $time);
}
}
$time = $this->toMilliseconds( $time );
$threshold = $this->getSlowThreshold( $test );
/**
* A test suite started.
*
* @param PHPUnit_Framework_TestSuite $suite
*/
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
{
$this->suites++;
}
if ( $this->isSlow( $time, $threshold ) ) {
$this->addSlowTest( $test, $time );
}
}
/**
* A test suite ended.
*
* @param PHPUnit_Framework_TestSuite $suite
*/
public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
{
$this->suites--;
/**
* A test suite started.
*
* @param PHPUnit_Framework_TestSuite $suite
*/
public function startTestSuite( PHPUnit_Framework_TestSuite $suite ) {
$this->suites++;
}
if (0 === $this->suites && $this->hasSlowTests()) {
arsort($this->slow); // Sort longest running tests to the top
/**
* A test suite ended.
*
* @param PHPUnit_Framework_TestSuite $suite
*/
public function endTestSuite( PHPUnit_Framework_TestSuite $suite ) {
$this->suites--;
$this->renderHeader();
$this->renderBody();
$this->renderFooter();
}
}
if ( 0 === $this->suites && $this->hasSlowTests() ) {
arsort( $this->slow ); // Sort longest running tests to the top
/**
* Whether the given test execution time is considered slow.
*
* @param int $time Test execution time in milliseconds
* @param int $slowThreshold Test execution time at which a test should be considered slow (milliseconds)
* @return bool
*/
protected function isSlow($time, $slowThreshold)
{
return $time >= $slowThreshold;
}
$this->renderHeader();
$this->renderBody();
$this->renderFooter();
}
}
/**
* Stores a test as slow.
*
* @param PHPUnit_Framework_TestCase $test
* @param int $time Test execution time in milliseconds
*/
protected function addSlowTest(PHPUnit_Framework_TestCase $test, $time)
{
$label = $this->makeLabel($test);
/**
* Whether the given test execution time is considered slow.
*
* @param int $time Test execution time in milliseconds
* @param int $slowThreshold Test execution time at which a test should be considered slow (milliseconds)
* @return bool
*/
protected function isSlow( $time, $slowThreshold ) {
return $time >= $slowThreshold;
}
$this->slow[$label] = $time;
}
/**
* Stores a test as slow.
*
* @param PHPUnit_Framework_TestCase $test
* @param int $time Test execution time in milliseconds
*/
protected function addSlowTest( PHPUnit_Framework_TestCase $test, $time ) {
$label = $this->makeLabel( $test );
/**
* Whether at least one test has been considered slow.
*
* @return bool
*/
protected function hasSlowTests()
{
return !empty($this->slow);
}
$this->slow[ $label ] = $time;
}
/**
* Convert PHPUnit's reported test time (microseconds) to milliseconds.
*
* @param float $time
* @return int
*/
protected function toMilliseconds($time)
{
return (int) round($time * 1000);
}
/**
* Whether at least one test has been considered slow.
*
* @return bool
*/
protected function hasSlowTests() {
return ! empty( $this->slow );
}
/**
* Label for describing a test.
*
* @param PHPUnit_Framework_TestCase $test
* @return string
*/
protected function makeLabel(PHPUnit_Framework_TestCase $test)
{
return sprintf('%s:%s', get_class($test), $test->getName());
}
/**
* Convert PHPUnit's reported test time (microseconds) to milliseconds.
*
* @param float $time
* @return int
*/
protected function toMilliseconds( $time ) {
return (int) round( $time * 1000 );
}
/**
* Calculate number of slow tests to report about.
*
* @return int
*/
protected function getReportLength()
{
return min(count($this->slow), $this->reportLength);
}
/**
* Label for describing a test.
*
* @param PHPUnit_Framework_TestCase $test
* @return string
*/
protected function makeLabel( PHPUnit_Framework_TestCase $test ) {
return sprintf( '%s:%s', get_class( $test ), $test->getName() );
}
/**
* Find how many slow tests occurred that won't be shown due to list length.
*
* @return int Number of hidden slow tests
*/
protected function getHiddenCount()
{
$total = count($this->slow);
$showing = $this->getReportLength($this->slow);
/**
* Calculate number of slow tests to report about.
*
* @return int
*/
protected function getReportLength() {
return min( count( $this->slow ), $this->reportLength );
}
$hidden = 0;
if ($total > $showing) {
$hidden = $total - $showing;
}
/**
* Find how many slow tests occurred that won't be shown due to list length.
*
* @return int Number of hidden slow tests
*/
protected function getHiddenCount() {
$total = count( $this->slow );
$showing = $this->getReportLength( $this->slow );
return $hidden;
}
$hidden = 0;
if ( $total > $showing ) {
$hidden = $total - $showing;
}
/**
* Renders slow test report header.
*/
protected function renderHeader()
{
echo sprintf("\n\nYou should really fix these slow tests (>%sms)...\n", $this->slowThreshold);
}
return $hidden;
}
/**
* Renders slow test report body.
*/
protected function renderBody()
{
$slowTests = $this->slow;
/**
* Renders slow test report header.
*/
protected function renderHeader() {
echo sprintf( "\n\nYou should really fix these slow tests (>%sms)...\n", $this->slowThreshold );
}
$length = $this->getReportLength($slowTests);
for ($i = 1; $i <= $length; ++$i) {
$label = key($slowTests);
$time = array_shift($slowTests);
/**
* Renders slow test report body.
*/
protected function renderBody() {
$slowTests = $this->slow;
echo sprintf(" %s. %sms to run %s\n", $i, $time, $label);
}
}
$length = $this->getReportLength( $slowTests );
for ( $i = 1; $i <= $length; ++$i ) {
$label = key( $slowTests );
$time = array_shift( $slowTests );
/**
* Renders slow test report footer.
*/
protected function renderFooter()
{
if ($hidden = $this->getHiddenCount($this->slow)) {
echo sprintf("...and there %s %s more above your threshold hidden from view", $hidden == 1 ? 'is' : 'are', $hidden);
}
}
echo sprintf( " %s. %sms to run %s\n", $i, $time, $label );
}
}
/**
* Populate options into class internals.
*
* @param array $options
*/
protected function loadOptions(array $options)
{
$this->slowThreshold = isset($options['slowThreshold']) ? $options['slowThreshold'] : 500;
$this->reportLength = isset($options['reportLength']) ? $options['reportLength'] : 10;
}
/**
* Renders slow test report footer.
*/
protected function renderFooter() {
if ( $hidden = $this->getHiddenCount( $this->slow ) ) {
echo sprintf( '...and there %s %s more above your threshold hidden from view', $hidden == 1 ? 'is' : 'are', $hidden );
}
}
/**
* Get slow test threshold for given test. A TestCase can override the
* suite-wide slow threshold by using the annotation @slowThreshold with
* the threshold value in milliseconds.
*
* The following test will only be considered slow when its execution time
* reaches 5000ms (5 seconds):
*
* <code>
* @slowThreshold 5000
* public function testLongRunningProcess() {}
* </code>
*
* @param PHPUnit_Framework_TestCase $test
* @return int
*/
protected function getSlowThreshold(PHPUnit_Framework_TestCase $test)
{
$ann = $test->getAnnotations();
/**
* Populate options into class internals.
*
* @param array $options
*/
protected function loadOptions( array $options ) {
$this->slowThreshold = isset( $options['slowThreshold'] ) ? $options['slowThreshold'] : 500;
$this->reportLength = isset( $options['reportLength'] ) ? $options['reportLength'] : 10;
}
return isset($ann['method']['slowThreshold'][0]) ? $ann['method']['slowThreshold'][0] : $this->slowThreshold;
}
/**
* Get slow test threshold for given test. A TestCase can override the
* suite-wide slow threshold by using the annotation @slowThreshold with
* the threshold value in milliseconds.
*
* The following test will only be considered slow when its execution time
* reaches 5000ms (5 seconds):
*
* <code>
*
* @slowThreshold 5000
* public function testLongRunningProcess() {}
* </code>
*
* @param PHPUnit_Framework_TestCase $test
* @return int
*/
protected function getSlowThreshold( PHPUnit_Framework_TestCase $test ) {
$ann = $test->getAnnotations();
return isset( $ann['method']['slowThreshold'][0] ) ? $ann['method']['slowThreshold'][0] : $this->slowThreshold;
}
}
+4 -4
View File
@@ -2,9 +2,9 @@
class Spy_REST_Server extends WP_REST_Server {
public $sent_headers = array();
public $sent_body = '';
public $last_request = null;
public $sent_headers = array();
public $sent_body = '';
public $last_request = null;
public $override_by_default = false;
/**
@@ -63,7 +63,7 @@ class Spy_REST_Server extends WP_REST_Server {
public function serve_request( $path = null ) {
ob_start();
$result = parent::serve_request( $path );
$result = parent::serve_request( $path );
$this->sent_body = ob_get_clean();
return $result;
}
+92 -23
View File
@@ -18,44 +18,107 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
/**
* Last AJAX response. This is set via echo -or- wp_die.
*
* @var string
*/
protected $_last_response = '';
/**
* List of ajax actions called via GET
*
* @var array
*/
protected static $_core_actions_get = array(
'fetch-list', 'ajax-tag-search', 'wp-compression-test', 'imgedit-preview', 'oembed-cache',
'autocomplete-user', 'dashboard-widgets', 'logged-in',
'fetch-list',
'ajax-tag-search',
'wp-compression-test',
'imgedit-preview',
'oembed-cache',
'autocomplete-user',
'dashboard-widgets',
'logged-in',
);
/**
* Saved error reporting level
*
* @var int
*/
protected $_error_level = 0;
/**
* List of ajax actions called via POST
*
* @var array
*/
protected static $_core_actions_post = array(
'oembed_cache', 'image-editor', 'delete-comment', 'delete-tag', 'delete-link',
'delete-meta', 'delete-post', 'trash-post', 'untrash-post', 'delete-page', 'dim-comment',
'add-link-category', 'add-tag', 'get-tagcloud', 'get-comments', 'replyto-comment',
'edit-comment', 'add-menu-item', 'add-meta', 'add-user', 'closed-postboxes',
'hidden-columns', 'update-welcome-panel', 'menu-get-metabox', 'wp-link-ajax',
'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink',
'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post',
'wp-remove-post-lock', 'dismiss-wp-pointer', 'send-attachment-to-editor', 'heartbeat', 'nopriv_heartbeat', 'get-revision-diffs',
'save-user-color-scheme', 'update-widget', 'query-themes', 'parse-embed', 'set-attachment-thumbnail',
'parse-media-shortcode', 'destroy-sessions', 'install-plugin', 'update-plugin', 'press-this-save-post',
'press-this-add-category', 'crop-image', 'generate-password', 'save-wporg-username', 'delete-plugin',
'search-plugins', 'search-install-plugins', 'activate-plugin', 'update-theme', 'delete-theme',
'install-theme', 'get-post-thumbnail-html',
'oembed_cache',
'image-editor',
'delete-comment',
'delete-tag',
'delete-link',
'delete-meta',
'delete-post',
'trash-post',
'untrash-post',
'delete-page',
'dim-comment',
'add-link-category',
'add-tag',
'get-tagcloud',
'get-comments',
'replyto-comment',
'edit-comment',
'add-menu-item',
'add-meta',
'add-user',
'closed-postboxes',
'hidden-columns',
'update-welcome-panel',
'menu-get-metabox',
'wp-link-ajax',
'menu-locations-save',
'menu-quick-search',
'meta-box-order',
'get-permalink',
'sample-permalink',
'inline-save',
'inline-save-tax',
'find_posts',
'widgets-order',
'save-widget',
'set-post-thumbnail',
'date_format',
'time_format',
'wp-fullscreen-save-post',
'wp-remove-post-lock',
'dismiss-wp-pointer',
'send-attachment-to-editor',
'heartbeat',
'nopriv_heartbeat',
'get-revision-diffs',
'save-user-color-scheme',
'update-widget',
'query-themes',
'parse-embed',
'set-attachment-thumbnail',
'parse-media-shortcode',
'destroy-sessions',
'install-plugin',
'update-plugin',
'press-this-save-post',
'press-this-add-category',
'crop-image',
'generate-password',
'save-wporg-username',
'delete-plugin',
'search-plugins',
'search-install-plugins',
'activate-plugin',
'update-theme',
'delete-theme',
'install-theme',
'get-post-thumbnail-html',
);
public static function setUpBeforeClass() {
@@ -103,7 +166,7 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
public function tearDown() {
parent::tearDown();
$_POST = array();
$_GET = array();
$_GET = array();
unset( $GLOBALS['post'] );
unset( $GLOBALS['comment'] );
remove_filter( 'wp_die_ajax_handler', array( $this, 'getDieHandler' ), 1, 1 );
@@ -117,13 +180,15 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
*/
public function logout() {
unset( $GLOBALS['current_user'] );
$cookies = array(AUTH_COOKIE, SECURE_AUTH_COOKIE, LOGGED_IN_COOKIE, USER_COOKIE, PASS_COOKIE);
foreach ( $cookies as $c )
unset( $_COOKIE[$c] );
$cookies = array( AUTH_COOKIE, SECURE_AUTH_COOKIE, LOGGED_IN_COOKIE, USER_COOKIE, PASS_COOKIE );
foreach ( $cookies as $c ) {
unset( $_COOKIE[ $c ] );
}
}
/**
* Return our callback handler
*
* @return callback
*/
public function getDieHandler() {
@@ -143,6 +208,7 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
* <code>
* $this->setExpectedException( 'WPAjaxDieContinueException', 'something contained in $message' );
* </code>
*
* @param string $message
*/
public function dieHandler( $message ) {
@@ -162,19 +228,21 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
/**
* Switch between user roles
* E.g. administrator, editor, author, contributor, subscriber
*
* @param string $role
*/
protected function _setRole( $role ) {
$post = $_POST;
$post = $_POST;
$user_id = self::factory()->user->create( array( 'role' => $role ) );
wp_set_current_user( $user_id );
$_POST = array_merge($_POST, $post);
$_POST = array_merge( $_POST, $post );
}
/**
* Mimic the ajax handling of admin-ajax.php
* Capture the output via output buffering, and if there is any, store
* it in $this->_last_response.
*
* @param string $action
*/
protected function _handleAjax( $action ) {
@@ -194,7 +262,8 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
// Save the output
$buffer = ob_get_clean();
if ( !empty( $buffer ) )
if ( ! empty( $buffer ) ) {
$this->_last_response = $buffer;
}
}
}
+157 -74
View File
@@ -3,10 +3,10 @@
class WP_Canonical_UnitTestCase extends WP_UnitTestCase {
static $old_current_user;
static $author_id;
static $post_ids = array();
static $post_ids = array();
static $comment_ids = array();
static $term_ids = array();
static $terms = array();
static $term_ids = array();
static $terms = array();
static $old_options = array();
/**
@@ -43,7 +43,7 @@ class WP_Canonical_UnitTestCase extends WP_UnitTestCase {
*/
public static function generate_shared_fixtures( $factory ) {
self::$old_current_user = get_current_user_id();
self::$author_id = $factory->user->create( array( 'user_login' => 'canonical-author' ) );
self::$author_id = $factory->user->create( array( 'user_login' => 'canonical-author' ) );
/*
* Also set in self::setUp(), but we must configure here to make sure that
@@ -54,17 +54,43 @@ class WP_Canonical_UnitTestCase extends WP_UnitTestCase {
// Already created by install defaults:
// self::factory()->term->create( array( 'taxonomy' => 'category', 'name' => 'uncategorized' ) );
self::$post_ids[] = $factory->post->create( array( 'import_id' => 587, 'post_title' => 'post-format-test-audio', 'post_date' => '2008-06-02 00:00:00' ) );
self::$post_ids[] = $post_id = $factory->post->create( array( 'post_title' => 'post-format-test-gallery', 'post_date' => '2008-06-10 00:00:00' ) );
self::$post_ids[] = $factory->post->create( array( 'import_id' => 611, 'post_type' => 'attachment', 'post_title' => 'canola2', 'post_parent' => $post_id ) );
self::$post_ids[] = $factory->post->create(
array(
'import_id' => 587,
'post_title' => 'post-format-test-audio',
'post_date' => '2008-06-02 00:00:00',
)
);
self::$post_ids[] = $post_id = $factory->post->create(
array(
'post_title' => 'post-format-test-gallery',
'post_date' => '2008-06-10 00:00:00',
)
);
self::$post_ids[] = $factory->post->create(
array(
'import_id' => 611,
'post_type' => 'attachment',
'post_title' => 'canola2',
'post_parent' => $post_id,
)
);
self::$post_ids[] = $factory->post->create( array(
'post_title' => 'images-test',
'post_date' => '2008-09-03 00:00:00',
'post_content' => 'Page 1 <!--nextpage--> Page 2 <!--nextpage--> Page 3'
) );
self::$post_ids[] = $factory->post->create(
array(
'post_title' => 'images-test',
'post_date' => '2008-09-03 00:00:00',
'post_content' => 'Page 1 <!--nextpage--> Page 2 <!--nextpage--> Page 3',
)
);
self::$post_ids[] = $post_id = $factory->post->create( array( 'import_id' => 149, 'post_title' => 'comment-test', 'post_date' => '2008-03-03 00:00:00' ) );
self::$post_ids[] = $post_id = $factory->post->create(
array(
'import_id' => 149,
'post_title' => 'comment-test',
'post_date' => '2008-03-03 00:00:00',
)
);
self::$comment_ids = $factory->comment->create_post_comments( $post_id, 15 );
self::$post_ids[] = $factory->post->create( array( 'post_date' => '2008-09-05 00:00:00' ) );
@@ -73,61 +99,114 @@ class WP_Canonical_UnitTestCase extends WP_UnitTestCase {
self::$post_ids[] = $factory->post->create( array( 'import_id' => 1 ) );
self::$post_ids[] = $factory->post->create( array( 'import_id' => 358 ) );
self::$post_ids[] = $factory->post->create( array( 'post_type' => 'page', 'post_title' => 'sample-page' ) );
self::$post_ids[] = $factory->post->create( array( 'post_type' => 'page', 'post_title' => 'about' ) );
self::$post_ids[] = $post_id = $factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
self::$post_ids[] = $factory->post->create(
array( 'import_id' => 144, 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $post_id,
) );
array(
'post_type' => 'page',
'post_title' => 'sample-page',
)
);
self::$post_ids[] = $factory->post->create(
array(
'post_type' => 'page',
'post_title' => 'about',
)
);
self::$post_ids[] = $post_id = $factory->post->create(
array(
'post_type' => 'page',
'post_title' => 'parent-page',
)
);
self::$post_ids[] = $factory->post->create(
array(
'import_id' => 144,
'post_type' => 'page',
'post_title' => 'child-page-1',
'post_parent' => $post_id,
)
);
self::$post_ids[] = $parent_id = $factory->post->create( array(
'post_name' => 'parent',
'post_type' => 'page',
) );
self::$post_ids[] = $child_id_1 = $factory->post->create( array(
'post_name' => 'child1',
'post_type' => 'page',
'post_parent' => $parent_id,
) );
self::$post_ids[] = $child_id_2 = $factory->post->create( array(
'post_name' => 'child2',
'post_type' => 'page',
'post_parent' => $parent_id,
) );
self::$post_ids[] = $grandchild_id_1 = $factory->post->create( array(
'post_name' => 'grandchild',
'post_type' => 'page',
'post_parent' => $child_id_1,
) );
self::$post_ids[] = $grandchild_id_2 = $factory->post->create( array(
'post_name' => 'grandchild',
'post_type' => 'page',
'post_parent' => $child_id_2,
) );
self::$post_ids[] = $parent_id = $factory->post->create(
array(
'post_name' => 'parent',
'post_type' => 'page',
)
);
self::$post_ids[] = $child_id_1 = $factory->post->create(
array(
'post_name' => 'child1',
'post_type' => 'page',
'post_parent' => $parent_id,
)
);
self::$post_ids[] = $child_id_2 = $factory->post->create(
array(
'post_name' => 'child2',
'post_type' => 'page',
'post_parent' => $parent_id,
)
);
self::$post_ids[] = $grandchild_id_1 = $factory->post->create(
array(
'post_name' => 'grandchild',
'post_type' => 'page',
'post_parent' => $child_id_1,
)
);
self::$post_ids[] = $grandchild_id_2 = $factory->post->create(
array(
'post_name' => 'grandchild',
'post_type' => 'page',
'post_parent' => $child_id_2,
)
);
$cat1 = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'parent' ) );
$cat1 = $factory->term->create(
array(
'taxonomy' => 'category',
'name' => 'parent',
)
);
self::$terms['/category/parent/'] = $cat1;
self::$term_ids[ $cat1 ] = 'category';
self::$term_ids[ $cat1 ] = 'category';
$cat2 = $factory->term->create( array(
'taxonomy' => 'category', 'name' => 'child-1', 'parent' => self::$terms['/category/parent/'],
) );
$cat2 = $factory->term->create(
array(
'taxonomy' => 'category',
'name' => 'child-1',
'parent' => self::$terms['/category/parent/'],
)
);
self::$terms['/category/parent/child-1/'] = $cat2;
self::$term_ids[ $cat2 ] = 'category';
self::$term_ids[ $cat2 ] = 'category';
$cat3 = $factory->term->create( array(
'taxonomy' => 'category', 'name' => 'child-2', 'parent' => self::$terms['/category/parent/child-1/'],
) );
$cat3 = $factory->term->create(
array(
'taxonomy' => 'category',
'name' => 'child-2',
'parent' => self::$terms['/category/parent/child-1/'],
)
);
self::$terms['/category/parent/child-1/child-2/'] = $cat3;
self::$term_ids[ $cat3 ] = 'category';
self::$term_ids[ $cat3 ] = 'category';
$cat4 = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) );
$cat4 = $factory->term->create(
array(
'taxonomy' => 'category',
'name' => 'cat-a',
)
);
self::$term_ids[ $cat4 ] = 'category';
$cat5 = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) );
$cat5 = $factory->term->create(
array(
'taxonomy' => 'category',
'name' => 'cat-b',
)
);
self::$term_ids[ $cat5 ] = 'category';
$tag1 = $factory->term->create( array( 'name' => 'post-formats' ) );
$tag1 = $factory->term->create( array( 'name' => 'post-formats' ) );
self::$term_ids[ $tag1 ] = 'post_tag';
}
@@ -137,11 +216,11 @@ class WP_Canonical_UnitTestCase extends WP_UnitTestCase {
* @since 4.1.0
*/
public static function delete_shared_fixtures() {
self::$author_id = null;
self::$post_ids = array();
self::$author_id = null;
self::$post_ids = array();
self::$comment_ids = array();
self::$term_ids = array();
self::$terms = array();
self::$term_ids = array();
self::$terms = array();
}
/**
@@ -157,30 +236,33 @@ class WP_Canonical_UnitTestCase extends WP_UnitTestCase {
public function assertCanonical( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
$this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, (array) $expected_doing_it_wrong );
$ticket_ref = ($ticket > 0) ? 'Ticket #' . $ticket : null;
$ticket_ref = ( $ticket > 0 ) ? 'Ticket #' . $ticket : null;
if ( is_string($expected) )
$expected = array('url' => $expected);
elseif ( is_array($expected) && !isset($expected['url']) && !isset($expected['qv']) )
if ( is_string( $expected ) ) {
$expected = array( 'url' => $expected );
} elseif ( is_array( $expected ) && ! isset( $expected['url'] ) && ! isset( $expected['qv'] ) ) {
$expected = array( 'qv' => $expected );
}
if ( !isset($expected['url']) && !isset($expected['qv']) )
if ( ! isset( $expected['url'] ) && ! isset( $expected['qv'] ) ) {
$this->fail( 'No valid expected output was provided' );
}
$this->go_to( home_url( $test_url ) );
// Does the redirect match what's expected?
$can_url = $this->get_canonical( $test_url );
$parsed_can_url = parse_url($can_url);
$can_url = $this->get_canonical( $test_url );
$parsed_can_url = parse_url( $can_url );
// Just test the Path and Query if present
if ( isset($expected['url']) ) {
$this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref );
if ( isset( $expected['url'] ) ) {
$this->assertEquals( $expected['url'], $parsed_can_url['path'] . ( ! empty( $parsed_can_url['query'] ) ? '?' . $parsed_can_url['query'] : '' ), $ticket_ref );
}
// If the test data doesn't include expected query vars, then we're done here
if ( ! isset($expected['qv']) )
if ( ! isset( $expected['qv'] ) ) {
return;
}
// "make" that the request and check the query is correct
$this->go_to( $can_url );
@@ -188,14 +270,14 @@ class WP_Canonical_UnitTestCase extends WP_UnitTestCase {
// Are all query vars accounted for, And correct?
global $wp;
$query_vars = array_diff($wp->query_vars, $wp->extra_query_vars);
if ( !empty($parsed_can_url['query']) ) {
parse_str($parsed_can_url['query'], $_qv);
$query_vars = array_diff( $wp->query_vars, $wp->extra_query_vars );
if ( ! empty( $parsed_can_url['query'] ) ) {
parse_str( $parsed_can_url['query'], $_qv );
// $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite)
$this->assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref );
$query_vars = array_merge($query_vars, $_qv);
$query_vars = array_merge( $query_vars, $_qv );
}
$this->assertEquals( $expected['qv'], $query_vars );
@@ -213,8 +295,9 @@ class WP_Canonical_UnitTestCase extends WP_UnitTestCase {
$test_url = home_url( $test_url );
$can_url = redirect_canonical( $test_url, false );
if ( ! $can_url )
if ( ! $can_url ) {
return $test_url; // No redirect will take place for this request
}
return $can_url;
}
@@ -159,7 +159,7 @@ abstract class WP_Test_REST_Post_Type_Controller_Testcase extends WP_Test_REST_C
// test links
if ( $links ) {
$links = test_rest_expand_compact_links( $links );
$links = test_rest_expand_compact_links( $links );
$post_type = get_post_type_object( $data['type'] );
$this->assertEquals( $links['self'][0]['href'], rest_url( 'wp/v2/' . $post_type->rest_base . '/' . $data['id'] ) );
$this->assertEquals( $links['collection'][0]['href'], rest_url( 'wp/v2/' . $post_type->rest_base ) );
@@ -216,8 +216,13 @@ abstract class WP_Test_REST_Post_Type_Controller_Testcase extends WP_Test_REST_C
$links = $data['_links'];
foreach ( $links as &$links_array ) {
foreach ( $links_array as &$link ) {
$attributes = array_diff_key( $link, array( 'href' => 1, 'name' => 1 ) );
$link = array_diff_key( $link, $attributes );
$attributes = array_diff_key(
$link, array(
'href' => 1,
'name' => 1,
)
);
$link = array_diff_key( $link, $attributes );
$link['attributes'] = $attributes;
}
}
@@ -278,17 +283,21 @@ abstract class WP_Test_REST_Post_Type_Controller_Testcase extends WP_Test_REST_C
}
protected function set_raw_post_data( $args = array() ) {
return wp_parse_args( $args, $this->set_post_data( array(
'title' => array(
'raw' => 'Post Title',
),
'content' => array(
'raw' => 'Post content',
),
'excerpt' => array(
'raw' => 'Post excerpt',
),
) ) );
return wp_parse_args(
$args, $this->set_post_data(
array(
'title' => array(
'raw' => 'Post Title',
),
'content' => array(
'raw' => 'Post content',
),
'excerpt' => array(
'raw' => 'Post excerpt',
),
)
)
);
}
/**
+11 -9
View File
@@ -1,7 +1,7 @@
<?php
include_once(ABSPATH . 'wp-admin/includes/admin.php');
include_once(ABSPATH . WPINC . '/class-IXR.php');
include_once(ABSPATH . WPINC . '/class-wp-xmlrpc-server.php');
include_once( ABSPATH . 'wp-admin/includes/admin.php' );
include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-xmlrpc-server.php' );
class WP_XMLRPC_UnitTestCase extends WP_UnitTestCase {
protected $myxmlrpcserver;
@@ -23,11 +23,13 @@ class WP_XMLRPC_UnitTestCase extends WP_UnitTestCase {
}
protected function make_user_by_role( $role ) {
return self::factory()->user->create( array(
'user_login' => $role,
'user_pass' => $role,
'role' => $role
));
return self::factory()->user->create(
array(
'user_login' => $role,
'user_pass' => $role,
'role' => $role,
)
);
}
}
}
+82 -66
View File
@@ -14,24 +14,24 @@ require_once dirname( __FILE__ ) . '/trac.php';
*/
class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
protected static $forced_tickets = array();
protected $expected_deprecated = array();
protected $caught_deprecated = array();
protected static $forced_tickets = array();
protected $expected_deprecated = array();
protected $caught_deprecated = array();
protected $expected_doing_it_wrong = array();
protected $caught_doing_it_wrong = array();
protected $caught_doing_it_wrong = array();
protected static $hooks_saved = array();
protected static $ignore_files;
function __isset( $name ) {
return 'factory' === $name;
}
}
function __get( $name ) {
if ( 'factory' === $name ) {
return self::factory();
}
}
}
}
/**
* Fetches the factory object for generating WordPress fixtures.
@@ -55,7 +55,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
$backtrace = debug_backtrace();
// [0] WP_UnitTestCase::get_called_class()
// [1] WP_UnitTestCase::setUpBeforeClass()
if ( 'call_user_func' === $backtrace[2]['function'] ) {
if ( 'call_user_func' === $backtrace[2]['function'] ) {
return $backtrace[2]['args'][0][0];
}
return $backtrace[2]['class'];
@@ -65,9 +65,9 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
global $wpdb;
$wpdb->suppress_errors = false;
$wpdb->show_errors = true;
$wpdb->show_errors = true;
$wpdb->db_connect();
ini_set('display_errors', 1 );
ini_set( 'display_errors', 1 );
parent::setUpBeforeClass();
@@ -100,7 +100,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
}
function setUp() {
set_time_limit(0);
set_time_limit( 0 );
if ( ! self::$ignore_files ) {
self::$ignore_files = $this->scan_user_uploads();
@@ -159,7 +159,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
}
}
$wp_query = new WP_Query();
$wp = new WP();
$wp = new WP();
// Reset globals related to the post loop and `setup_postdata()`.
$post_globals = array( 'post', 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' );
@@ -176,7 +176,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
}
function clean_up_global_scope() {
$_GET = array();
$_GET = array();
$_POST = array();
self::flush_cache();
}
@@ -184,7 +184,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
/**
* Allow tests to be skipped on some automated runs
*
* For test runs on Travis for something other than trunk/master
* For test runs on Travis for something other than trunk/master
* we want to skip tests that only need to run for master.
*/
public function skipOnAutomatedBranches() {
@@ -320,10 +320,10 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
static function flush_cache() {
global $wp_object_cache;
$wp_object_cache->group_ops = array();
$wp_object_cache->stats = array();
$wp_object_cache->group_ops = array();
$wp_object_cache->stats = array();
$wp_object_cache->memcache_debug = array();
$wp_object_cache->cache = array();
$wp_object_cache->cache = array();
if ( method_exists( $wp_object_cache, '__remoteset' ) ) {
$wp_object_cache->__remoteset();
}
@@ -351,14 +351,16 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
}
function _create_temporary_tables( $query ) {
if ( 'CREATE TABLE' === substr( trim( $query ), 0, 12 ) )
if ( 'CREATE TABLE' === substr( trim( $query ), 0, 12 ) ) {
return substr_replace( trim( $query ), 'CREATE TEMPORARY TABLE', 0, 12 );
}
return $query;
}
function _drop_temporary_tables( $query ) {
if ( 'DROP TABLE' === substr( trim( $query ), 0, 10 ) )
if ( 'DROP TABLE' === substr( trim( $query ), 0, 10 ) ) {
return substr_replace( trim( $query ), 'DROP TEMPORARY TABLE', 0, 10 );
}
return $query;
}
@@ -377,10 +379,12 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
function expectDeprecated() {
$annotations = $this->getAnnotations();
foreach ( array( 'class', 'method' ) as $depth ) {
if ( ! empty( $annotations[ $depth ]['expectedDeprecated'] ) )
if ( ! empty( $annotations[ $depth ]['expectedDeprecated'] ) ) {
$this->expected_deprecated = array_merge( $this->expected_deprecated, $annotations[ $depth ]['expectedDeprecated'] );
if ( ! empty( $annotations[ $depth ]['expectedIncorrectUsage'] ) )
}
if ( ! empty( $annotations[ $depth ]['expectedIncorrectUsage'] ) ) {
$this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, $annotations[ $depth ]['expectedIncorrectUsage'] );
}
}
add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) );
add_action( 'deprecated_argument_run', array( $this, 'deprecated_function_run' ) );
@@ -388,8 +392,8 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
add_action( 'doing_it_wrong_run', array( $this, 'doing_it_wrong_run' ) );
add_action( 'deprecated_function_trigger_error', '__return_false' );
add_action( 'deprecated_argument_trigger_error', '__return_false' );
add_action( 'deprecated_hook_trigger_error', '__return_false' );
add_action( 'doing_it_wrong_trigger_error', '__return_false' );
add_action( 'deprecated_hook_trigger_error', '__return_false' );
add_action( 'doing_it_wrong_trigger_error', '__return_false' );
}
function expectedDeprecated() {
@@ -470,13 +474,15 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
}
function deprecated_function_run( $function ) {
if ( ! in_array( $function, $this->caught_deprecated ) )
if ( ! in_array( $function, $this->caught_deprecated ) ) {
$this->caught_deprecated[] = $function;
}
}
function doing_it_wrong_run( $function ) {
if ( ! in_array( $function, $this->caught_doing_it_wrong ) )
if ( ! in_array( $function, $this->caught_doing_it_wrong ) ) {
$this->caught_doing_it_wrong[] = $function;
}
}
function assertWPError( $actual, $message = '' ) {
@@ -502,7 +508,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
}
function assertEqualFields( $object, $fields ) {
foreach( $fields as $field_name => $field_value ) {
foreach ( $fields as $field_name => $field_value ) {
if ( $object->$field_name != $field_value ) {
$this->fail();
}
@@ -534,7 +540,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
$this->assertTrue( is_array( $array ) );
$this->assertNotEmpty( $array );
foreach( $array as $sub_array ) {
foreach ( $array as $sub_array ) {
$this->assertTrue( is_array( $sub_array ) );
$this->assertNotEmpty( $sub_array );
}
@@ -562,16 +568,18 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
// from all over the place (globals, GET, etc), which makes it tricky
// to run them more than once without very carefully clearing everything
$_GET = $_POST = array();
foreach (array('query_string', 'id', 'postdata', 'authordata', 'day', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages', 'pagenow') as $v) {
if ( isset( $GLOBALS[$v] ) ) unset( $GLOBALS[$v] );
foreach ( array( 'query_string', 'id', 'postdata', 'authordata', 'day', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages', 'pagenow' ) as $v ) {
if ( isset( $GLOBALS[ $v ] ) ) {
unset( $GLOBALS[ $v ] );
}
}
$parts = parse_url($url);
if (isset($parts['scheme'])) {
$parts = parse_url( $url );
if ( isset( $parts['scheme'] ) ) {
$req = isset( $parts['path'] ) ? $parts['path'] : '';
if (isset($parts['query'])) {
if ( isset( $parts['query'] ) ) {
$req .= '?' . $parts['query'];
// parse the url query vars into $_GET
parse_str($parts['query'], $_GET);
parse_str( $parts['query'], $_GET );
}
} else {
$req = $url;
@@ -581,23 +589,23 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
}
$_SERVER['REQUEST_URI'] = $req;
unset($_SERVER['PATH_INFO']);
unset( $_SERVER['PATH_INFO'] );
self::flush_cache();
unset($GLOBALS['wp_query'], $GLOBALS['wp_the_query']);
unset( $GLOBALS['wp_query'], $GLOBALS['wp_the_query'] );
$GLOBALS['wp_the_query'] = new WP_Query();
$GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
$GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
$public_query_vars = $GLOBALS['wp']->public_query_vars;
$private_query_vars = $GLOBALS['wp']->private_query_vars;
$GLOBALS['wp'] = new WP();
$GLOBALS['wp'] = new WP();
$GLOBALS['wp']->public_query_vars = $public_query_vars;
$GLOBALS['wp']->private_query_vars = $private_query_vars;
_cleanup_query_vars();
$GLOBALS['wp']->main($parts['query']);
$GLOBALS['wp']->main( $parts['query'] );
}
protected function checkRequirements() {
@@ -619,16 +627,18 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
return;
}
if ( WP_TESTS_FORCE_KNOWN_BUGS )
if ( WP_TESTS_FORCE_KNOWN_BUGS ) {
return;
}
$tickets = PHPUnit_Util_Test::getTickets( get_class( $this ), $this->getName( false ) );
foreach ( $tickets as $ticket ) {
if ( is_numeric( $ticket ) ) {
$this->knownWPBug( $ticket );
} elseif ( 'Plugin' == substr( $ticket, 0, 6 ) ) {
$ticket = substr( $ticket, 6 );
if ( $ticket && is_numeric( $ticket ) )
if ( $ticket && is_numeric( $ticket ) ) {
$this->knownPluginBug( $ticket );
}
}
}
}
@@ -637,10 +647,12 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
* Skips the current test if there is an open WordPress ticket with id $ticket_id
*/
function knownWPBug( $ticket_id ) {
if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets ) )
if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets ) ) {
return;
if ( ! TracTickets::isTracTicketClosed( 'https://core.trac.wordpress.org', $ticket_id ) )
}
if ( ! TracTickets::isTracTicketClosed( 'https://core.trac.wordpress.org', $ticket_id ) ) {
$this->markTestSkipped( sprintf( 'WordPress Ticket #%d is not fixed', $ticket_id ) );
}
}
/**
@@ -654,10 +666,12 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
* Skips the current test if there is an open plugin ticket with id $ticket_id
*/
function knownPluginBug( $ticket_id ) {
if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'Plugin' . $ticket_id, self::$forced_tickets ) )
if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'Plugin' . $ticket_id, self::$forced_tickets ) ) {
return;
if ( ! TracTickets::isTracTicketClosed( 'https://plugins.trac.wordpress.org', $ticket_id ) )
}
if ( ! TracTickets::isTracTicketClosed( 'https://plugins.trac.wordpress.org', $ticket_id ) ) {
$this->markTestSkipped( sprintf( 'WordPress Plugin Ticket #%d is not fixed', $ticket_id ) );
}
}
public static function forceTicket( $ticket ) {
@@ -678,12 +692,13 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
*/
function temp_filename() {
$tmp_dir = '';
$dirs = array( 'TMP', 'TMPDIR', 'TEMP' );
foreach( $dirs as $dir )
if ( isset( $_ENV[$dir] ) && !empty( $_ENV[$dir] ) ) {
$dirs = array( 'TMP', 'TMPDIR', 'TEMP' );
foreach ( $dirs as $dir ) {
if ( isset( $_ENV[ $dir ] ) && ! empty( $_ENV[ $dir ] ) ) {
$tmp_dir = $dir;
break;
}
}
if ( empty( $tmp_dir ) ) {
$tmp_dir = '/tmp';
}
@@ -700,9 +715,9 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
*
* @param string $prop,... Any number of WP_Query properties that are expected to be true for the current request.
*/
function assertQueryTrue(/* ... */) {
function assertQueryTrue() {
global $wp_query;
$all = array(
$all = array(
'is_404',
'is_admin',
'is_archive',
@@ -738,7 +753,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
$this->assertContains( $true_thing, $all, "Unknown conditional: {$true_thing}." );
}
$passed = true;
$passed = true;
$message = '';
foreach ( $all as $query_thing ) {
@@ -747,11 +762,11 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
if ( in_array( $query_thing, $true ) ) {
if ( ! $result ) {
$message .= $query_thing . ' is false but is expected to be true. ' . PHP_EOL;
$passed = false;
$passed = false;
}
} else if ( $result ) {
} elseif ( $result ) {
$message .= $query_thing . ' is true but is expected to be false. ' . PHP_EOL;
$passed = false;
$passed = false;
}
}
@@ -789,7 +804,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
$files = array();
$iterator = new RecursiveDirectoryIterator( $dir );
$objects = new RecursiveIteratorIterator( $iterator );
$objects = new RecursiveIteratorIterator( $iterator );
foreach ( $objects as $name => $object ) {
if ( is_file( $name ) ) {
$files[] = $name;
@@ -806,7 +821,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
}
$uploads = wp_upload_dir();
$files = $this->files_in_dir( $uploads['basedir'] );
$files = $this->files_in_dir( $uploads['basedir'] );
return $files;
}
@@ -835,7 +850,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
/**
* Helper to Convert a microtime string into a float
*/
protected function _microtime_to_float($microtime ){
protected function _microtime_to_float( $microtime ) {
$time_array = explode( ' ', $microtime );
return array_sum( $time_array );
}
@@ -870,27 +885,28 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
$wp_rewrite->flush_rules();
}
function _make_attachment($upload, $parent_post_id = 0) {
function _make_attachment( $upload, $parent_post_id = 0 ) {
$type = '';
if ( !empty($upload['type']) ) {
if ( ! empty( $upload['type'] ) ) {
$type = $upload['type'];
} else {
$mime = wp_check_filetype( $upload['file'] );
if ($mime)
if ( $mime ) {
$type = $mime['type'];
}
}
$attachment = array(
'post_title' => basename( $upload['file'] ),
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $parent_post_id,
'post_title' => basename( $upload['file'] ),
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $parent_post_id,
'post_mime_type' => $type,
'guid' => $upload[ 'url' ],
'guid' => $upload['url'],
);
// Save the data
$id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id );
$id = wp_insert_attachment( $attachment, $upload['file'], $parent_post_id );
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
return $id;
}
@@ -903,7 +919,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
return $wpdb->update(
$wpdb->posts,
array(
'post_modified' => $date,
'post_modified' => $date,
'post_modified_gmt' => $date,
),
array(
+3 -3
View File
@@ -15,12 +15,12 @@ class TracTickets {
*/
public static function isTracTicketClosed( $trac_url, $ticket_id ) {
if ( ! extension_loaded( 'openssl' ) ) {
$trac_url = preg_replace( "/^https:/", "http:", $trac_url );
$trac_url = preg_replace( '/^https:/', 'http:', $trac_url );
}
if ( ! isset( self::$trac_ticket_cache[ $trac_url ] ) ) {
// In case you're running the tests offline, keep track of open tickets.
$file = DIR_TESTDATA . '/.trac-ticket-cache.' . str_replace( array( 'http://', 'https://', '/' ), array( '', '', '-' ), rtrim( $trac_url, '/' ) );
$file = DIR_TESTDATA . '/.trac-ticket-cache.' . str_replace( array( 'http://', 'https://', '/' ), array( '', '', '-' ), rtrim( $trac_url, '/' ) );
$tickets = @file_get_contents( $trac_url . '/query?status=%21closed&format=csv&col=id' );
// Check if our HTTP request failed.
if ( false === $tickets ) {
@@ -37,7 +37,7 @@ class TracTickets {
$tickets = trim( $tickets );
file_put_contents( $file, $tickets );
}
$tickets = explode( "\r\n", $tickets );
$tickets = explode( "\r\n", $tickets );
self::$trac_ticket_cache[ $trac_url ] = $tickets;
}
+242 -185
View File
@@ -2,16 +2,16 @@
// misc help functions and utilities
function rand_str($len=32) {
return substr(md5(uniqid(rand())), 0, $len);
function rand_str( $len = 32 ) {
return substr( md5( uniqid( rand() ) ), 0, $len );
}
function rand_long_str( $length ) {
$chars = 'abcdefghijklmnopqrstuvwxyz';
$chars = 'abcdefghijklmnopqrstuvwxyz';
$string = '';
for ( $i = 0; $i < $length; $i++ ) {
$rand = rand( 0, strlen( $chars ) - 1 );
$rand = rand( 0, strlen( $chars ) - 1 );
$string .= substr( $chars, $rand, 1 );
}
@@ -19,14 +19,16 @@ function rand_long_str( $length ) {
}
// strip leading and trailing whitespace from each line in the string
function strip_ws($txt) {
$lines = explode("\n", $txt);
function strip_ws( $txt ) {
$lines = explode( "\n", $txt );
$result = array();
foreach ($lines as $line)
if (trim($line))
$result[] = trim($line);
foreach ( $lines as $line ) {
if ( trim( $line ) ) {
$result[] = trim( $line );
}
}
return trim(join("\n", $result));
return trim( join( "\n", $result ) );
}
// helper class for testing code that involves actions and filters
@@ -50,57 +52,94 @@ class MockAction {
}
function current_filter() {
if (is_callable('current_filter'))
if ( is_callable( 'current_filter' ) ) {
return current_filter();
}
global $wp_actions;
return end($wp_actions);
return end( $wp_actions );
}
function action($arg) {
if ($this->debug) dmp(__FUNCTION__, $this->current_filter());
$args = func_get_args();
$this->events[] = array('action' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args);
function action( $arg ) {
if ( $this->debug ) {
dmp( __FUNCTION__, $this->current_filter() );
}
$args = func_get_args();
$this->events[] = array(
'action' => __FUNCTION__,
'tag' => $this->current_filter(),
'args' => $args,
);
return $arg;
}
function action2($arg) {
if ($this->debug) dmp(__FUNCTION__, $this->current_filter());
function action2( $arg ) {
if ( $this->debug ) {
dmp( __FUNCTION__, $this->current_filter() );
}
$args = func_get_args();
$this->events[] = array('action' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args);
$args = func_get_args();
$this->events[] = array(
'action' => __FUNCTION__,
'tag' => $this->current_filter(),
'args' => $args,
);
return $arg;
}
function filter($arg) {
if ($this->debug) dmp(__FUNCTION__, $this->current_filter());
function filter( $arg ) {
if ( $this->debug ) {
dmp( __FUNCTION__, $this->current_filter() );
}
$args = func_get_args();
$this->events[] = array('filter' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args);
$args = func_get_args();
$this->events[] = array(
'filter' => __FUNCTION__,
'tag' => $this->current_filter(),
'args' => $args,
);
return $arg;
}
function filter2($arg) {
if ($this->debug) dmp(__FUNCTION__, $this->current_filter());
function filter2( $arg ) {
if ( $this->debug ) {
dmp( __FUNCTION__, $this->current_filter() );
}
$args = func_get_args();
$this->events[] = array('filter' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args);
$args = func_get_args();
$this->events[] = array(
'filter' => __FUNCTION__,
'tag' => $this->current_filter(),
'args' => $args,
);
return $arg;
}
function filter_append($arg) {
if ($this->debug) dmp(__FUNCTION__, $this->current_filter());
function filter_append( $arg ) {
if ( $this->debug ) {
dmp( __FUNCTION__, $this->current_filter() );
}
$args = func_get_args();
$this->events[] = array('filter' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args);
$args = func_get_args();
$this->events[] = array(
'filter' => __FUNCTION__,
'tag' => $this->current_filter(),
'args' => $args,
);
return $arg . '_append';
}
function filterall($tag, $arg=NULL) {
// this one doesn't return the result, so it's safe to use with the new 'all' filter
if ($this->debug) dmp(__FUNCTION__, $this->current_filter());
function filterall( $tag, $arg = null ) {
// this one doesn't return the result, so it's safe to use with the new 'all' filter
if ( $this->debug ) {
dmp( __FUNCTION__, $this->current_filter() );
}
$args = func_get_args();
$this->events[] = array('filter' => __FUNCTION__, 'tag'=>$tag, 'args'=>array_slice($args, 1));
$args = func_get_args();
$this->events[] = array(
'filter' => __FUNCTION__,
'tag' => $tag,
'args' => array_slice( $args, 1 ),
);
}
// return a list of all the actions, tags and args
@@ -109,21 +148,23 @@ if ($this->debug) dmp(__FUNCTION__, $this->current_filter());
}
// return a count of the number of times the action was called since the last reset
function get_call_count($tag='') {
if ($tag) {
function get_call_count( $tag = '' ) {
if ( $tag ) {
$count = 0;
foreach ($this->events as $e)
if ($e['action'] == $tag)
foreach ( $this->events as $e ) {
if ( $e['action'] == $tag ) {
++$count;
}
}
return $count;
}
return count($this->events);
return count( $this->events );
}
// return an array of the tags that triggered calls to this action
function get_tags() {
$out = array();
foreach ($this->events as $e) {
foreach ( $this->events as $e ) {
$out[] = $e['tag'];
}
return $out;
@@ -132,8 +173,9 @@ if ($this->debug) dmp(__FUNCTION__, $this->current_filter());
// return an array of args passed in calls to this action
function get_args() {
$out = array();
foreach ($this->events as $e)
foreach ( $this->events as $e ) {
$out[] = $e['args'];
}
return $out;
}
}
@@ -149,70 +191,76 @@ class testXMLParser {
*/
function __construct( $in ) {
$this->xml = xml_parser_create();
xml_set_object($this->xml, $this);
xml_parser_set_option($this->xml,XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($this->xml, array($this, 'startHandler'), array($this, 'endHandler'));
xml_set_character_data_handler($this->xml, array($this, 'dataHandler'));
$this->parse($in);
xml_set_object( $this->xml, $this );
xml_parser_set_option( $this->xml, XML_OPTION_CASE_FOLDING, 0 );
xml_set_element_handler( $this->xml, array( $this, 'startHandler' ), array( $this, 'endHandler' ) );
xml_set_character_data_handler( $this->xml, array( $this, 'dataHandler' ) );
$this->parse( $in );
}
function parse($in) {
$parse = xml_parse($this->xml, $in, true);
if (!$parse) {
trigger_error(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xml)),
xml_get_current_line_number($this->xml)), E_USER_ERROR);
xml_parser_free($this->xml);
function parse( $in ) {
$parse = xml_parse( $this->xml, $in, true );
if ( ! $parse ) {
trigger_error(
sprintf(
'XML error: %s at line %d',
xml_error_string( xml_get_error_code( $this->xml ) ),
xml_get_current_line_number( $this->xml )
), E_USER_ERROR
);
xml_parser_free( $this->xml );
}
return true;
}
function startHandler($parser, $name, $attributes) {
function startHandler( $parser, $name, $attributes ) {
$data['name'] = $name;
if ($attributes) { $data['attributes'] = $attributes; }
if ( $attributes ) {
$data['attributes'] = $attributes; }
$this->data[] = $data;
}
function dataHandler($parser, $data) {
$index = count($this->data) - 1;
@$this->data[$index]['content'] .= $data;
function dataHandler( $parser, $data ) {
$index = count( $this->data ) - 1;
@$this->data[ $index ]['content'] .= $data;
}
function endHandler($parser, $name) {
if (count($this->data) > 1) {
$data = array_pop($this->data);
$index = count($this->data) - 1;
$this->data[$index]['child'][] = $data;
function endHandler( $parser, $name ) {
if ( count( $this->data ) > 1 ) {
$data = array_pop( $this->data );
$index = count( $this->data ) - 1;
$this->data[ $index ]['child'][] = $data;
}
}
}
function xml_to_array($in) {
$p = new testXMLParser($in);
function xml_to_array( $in ) {
$p = new testXMLParser( $in );
return $p->data;
}
function xml_find($tree /*, $el1, $el2, $el3, .. */) {
$a = func_get_args();
$a = array_slice($a, 1);
$n = count($a);
function xml_find( $tree /*, $el1, $el2, $el3, .. */ ) {
$a = func_get_args();
$a = array_slice( $a, 1 );
$n = count( $a );
$out = array();
if ($n < 1)
if ( $n < 1 ) {
return $out;
}
for ($i=0; $i<count($tree); $i++) {
# echo "checking '{$tree[$i][name]}' == '{$a[0]}'\n";
# var_dump($tree[$i]['name'], $a[0]);
if ($tree[$i]['name'] == $a[0]) {
# echo "n == {$n}\n";
if ($n == 1)
$out[] = $tree[$i];
else {
$subtree =& $tree[$i]['child'];
$call_args = array($subtree);
$call_args = array_merge($call_args, array_slice($a, 1));
$out = array_merge($out, call_user_func_array('xml_find', $call_args));
for ( $i = 0; $i < count( $tree ); $i++ ) {
# echo "checking '{$tree[$i][name]}' == '{$a[0]}'\n";
# var_dump($tree[$i]['name'], $a[0]);
if ( $tree[ $i ]['name'] == $a[0] ) {
# echo "n == {$n}\n";
if ( $n == 1 ) {
$out[] = $tree[ $i ];
} else {
$subtree =& $tree[ $i ]['child'];
$call_args = array( $subtree );
$call_args = array_merge( $call_args, array_slice( $a, 1 ) );
$out = array_merge( $out, call_user_func_array( 'xml_find', $call_args ) );
}
}
}
@@ -220,26 +268,28 @@ function xml_find($tree /*, $el1, $el2, $el3, .. */) {
return $out;
}
function xml_join_atts($atts) {
function xml_join_atts( $atts ) {
$a = array();
foreach ($atts as $k=>$v)
$a[] = $k.'="'.$v.'"';
return join(' ', $a);
foreach ( $atts as $k => $v ) {
$a[] = $k . '="' . $v . '"';
}
return join( ' ', $a );
}
function xml_array_dumbdown(&$data) {
function xml_array_dumbdown( &$data ) {
$out = array();
foreach (array_keys($data) as $i) {
$name = $data[$i]['name'];
if (!empty($data[$i]['attributes']))
$name .= ' '.xml_join_atts($data[$i]['attributes']);
foreach ( array_keys( $data ) as $i ) {
$name = $data[ $i ]['name'];
if ( ! empty( $data[ $i ]['attributes'] ) ) {
$name .= ' ' . xml_join_atts( $data[ $i ]['attributes'] );
}
if (!empty($data[$i]['child'])) {
$out[$name][] = xml_array_dumbdown($data[$i]['child']);
if ( ! empty( $data[ $i ]['child'] ) ) {
$out[ $name ][] = xml_array_dumbdown( $data[ $i ]['child'] );
} else {
$out[ $name ] = $data[ $i ]['content'];
}
else
$out[$name] = $data[$i]['content'];
}
return $out;
@@ -248,41 +298,41 @@ function xml_array_dumbdown(&$data) {
function dmp() {
$args = func_get_args();
foreach ($args as $thing)
echo (is_scalar($thing) ? strval($thing) : var_export($thing, true)), "\n";
foreach ( $args as $thing ) {
echo ( is_scalar( $thing ) ? strval( $thing ) : var_export( $thing, true ) ), "\n";
}
}
function dmp_filter($a) {
dmp($a);
function dmp_filter( $a ) {
dmp( $a );
return $a;
}
function get_echo($callable, $args = array()) {
function get_echo( $callable, $args = array() ) {
ob_start();
call_user_func_array($callable, $args);
call_user_func_array( $callable, $args );
return ob_get_clean();
}
// recursively generate some quick assertEquals tests based on an array
function gen_tests_array($name, $array) {
function gen_tests_array( $name, $array ) {
$out = array();
foreach ($array as $k=>$v) {
if (is_numeric($k))
$index = strval($k);
else
$index = "'".addcslashes($k, "\n\r\t'\\")."'";
foreach ( $array as $k => $v ) {
if ( is_numeric( $k ) ) {
$index = strval( $k );
} else {
$index = "'" . addcslashes( $k, "\n\r\t'\\" ) . "'";
}
if (is_string($v)) {
$out[] = '$this->assertEquals( \'' . addcslashes($v, "\n\r\t'\\") . '\', $'.$name.'['.$index.'] );';
}
elseif (is_numeric($v)) {
$out[] = '$this->assertEquals( ' . $v . ', $'.$name.'['.$index.'] );';
}
elseif (is_array($v)) {
$out[] = gen_tests_array("{$name}[{$index}]", $v);
if ( is_string( $v ) ) {
$out[] = '$this->assertEquals( \'' . addcslashes( $v, "\n\r\t'\\" ) . '\', $' . $name . '[' . $index . '] );';
} elseif ( is_numeric( $v ) ) {
$out[] = '$this->assertEquals( ' . $v . ', $' . $name . '[' . $index . '] );';
} elseif ( is_array( $v ) ) {
$out[] = gen_tests_array( "{$name}[{$index}]", $v );
}
}
return join("\n", $out)."\n";
return join( "\n", $out ) . "\n";
}
/**
@@ -295,33 +345,36 @@ class MockClass {};
*/
function drop_tables() {
global $wpdb;
$tables = $wpdb->get_col('SHOW TABLES;');
foreach ($tables as $table)
$wpdb->query("DROP TABLE IF EXISTS {$table}");
$tables = $wpdb->get_col( 'SHOW TABLES;' );
foreach ( $tables as $table ) {
$wpdb->query( "DROP TABLE IF EXISTS {$table}" );
}
}
function print_backtrace() {
$bt = debug_backtrace();
echo "Backtrace:\n";
$i = 0;
foreach ($bt as $stack) {
echo ++$i, ": ";
if ( isset($stack['class']) )
echo $stack['class'].'::';
if ( isset($stack['function']) )
echo $stack['function'].'() ';
foreach ( $bt as $stack ) {
echo ++$i, ': ';
if ( isset( $stack['class'] ) ) {
echo $stack['class'] . '::';
}
if ( isset( $stack['function'] ) ) {
echo $stack['function'] . '() ';
}
echo "line {$stack[line]} in {$stack[file]}\n";
}
echo "\n";
}
// mask out any input fields matching the given name
function mask_input_value($in, $name='_wpnonce') {
return preg_replace('@<input([^>]*) name="'.preg_quote($name).'"([^>]*) value="[^>]*" />@', '<input$1 name="'.preg_quote($name).'"$2 value="***" />', $in);
function mask_input_value( $in, $name = '_wpnonce' ) {
return preg_replace( '@<input([^>]*) name="' . preg_quote( $name ) . '"([^>]*) value="[^>]*" />@', '<input$1 name="' . preg_quote( $name ) . '"$2 value="***" />', $in );
}
if ( !function_exists( 'str_getcsv' ) ) {
function str_getcsv( $input, $delimiter = ',', $enclosure = '"', $escape = "\\" ) {
if ( ! function_exists( 'str_getcsv' ) ) {
function str_getcsv( $input, $delimiter = ',', $enclosure = '"', $escape = '\\' ) {
$fp = fopen( 'php://temp/', 'r+' );
fputs( $fp, $input );
rewind( $fp );
@@ -355,25 +408,29 @@ function _unregister_post_status( $status ) {
function _cleanup_query_vars() {
// clean out globals to stop them polluting wp and wp_query
foreach ( $GLOBALS['wp']->public_query_vars as $v )
unset( $GLOBALS[$v] );
foreach ( $GLOBALS['wp']->private_query_vars as $v )
unset( $GLOBALS[$v] );
foreach ( get_taxonomies( array() , 'objects' ) as $t ) {
if ( $t->publicly_queryable && ! empty( $t->query_var ) )
$GLOBALS['wp']->add_query_var( $t->query_var );
foreach ( $GLOBALS['wp']->public_query_vars as $v ) {
unset( $GLOBALS[ $v ] );
}
foreach ( get_post_types( array() , 'objects' ) as $t ) {
if ( is_post_type_viewable( $t ) && ! empty( $t->query_var ) )
foreach ( $GLOBALS['wp']->private_query_vars as $v ) {
unset( $GLOBALS[ $v ] );
}
foreach ( get_taxonomies( array(), 'objects' ) as $t ) {
if ( $t->publicly_queryable && ! empty( $t->query_var ) ) {
$GLOBALS['wp']->add_query_var( $t->query_var );
}
}
foreach ( get_post_types( array(), 'objects' ) as $t ) {
if ( is_post_type_viewable( $t ) && ! empty( $t->query_var ) ) {
$GLOBALS['wp']->add_query_var( $t->query_var );
}
}
}
function _clean_term_filters() {
remove_filter( 'get_terms', array( 'Featured_Content', 'hide_featured_term' ), 10, 2 );
remove_filter( 'get_terms', array( 'Featured_Content', 'hide_featured_term' ), 10, 2 );
remove_filter( 'get_the_terms', array( 'Featured_Content', 'hide_the_featured_term' ), 10, 3 );
}
@@ -383,17 +440,17 @@ function _clean_term_filters() {
class wpdb_exposed_methods_for_testing extends wpdb {
public function __construct() {
global $wpdb;
$this->dbh = $wpdb->dbh;
$this->use_mysqli = $wpdb->use_mysqli;
$this->is_mysql = $wpdb->is_mysql;
$this->ready = true;
$this->dbh = $wpdb->dbh;
$this->use_mysqli = $wpdb->use_mysqli;
$this->is_mysql = $wpdb->is_mysql;
$this->ready = true;
$this->field_types = $wpdb->field_types;
$this->charset = $wpdb->charset;
$this->charset = $wpdb->charset;
$this->dbuser = $wpdb->dbuser;
$this->dbuser = $wpdb->dbuser;
$this->dbpassword = $wpdb->dbpassword;
$this->dbname = $wpdb->dbname;
$this->dbhost = $wpdb->dbhost;
$this->dbname = $wpdb->dbname;
$this->dbhost = $wpdb->dbhost;
}
public function __call( $name, $arguments ) {
@@ -408,7 +465,7 @@ class wpdb_exposed_methods_for_testing extends wpdb {
*/
function benchmark_pcre_backtracking( $pattern, $subject, $strategy ) {
$saved_config = ini_get( 'pcre.backtrack_limit' );
// Attempt to prevent PHP crashes. Adjust these lower when needed.
if ( version_compare( phpversion(), '5.4.8', '>' ) ) {
$limit = 1000000;
@@ -417,42 +474,42 @@ function benchmark_pcre_backtracking( $pattern, $subject, $strategy ) {
}
// Start with small numbers, so if a crash is encountered at higher numbers we can still debug the problem.
for( $i = 4; $i <= $limit; $i *= 2 ) {
for ( $i = 4; $i <= $limit; $i *= 2 ) {
ini_set( 'pcre.backtrack_limit', $i );
switch( $strategy ) {
case 'split':
preg_split( $pattern, $subject );
break;
case 'match':
preg_match( $pattern, $subject );
break;
case 'match_all':
$matches = array();
preg_match_all( $pattern, $subject, $matches );
break;
switch ( $strategy ) {
case 'split':
preg_split( $pattern, $subject );
break;
case 'match':
preg_match( $pattern, $subject );
break;
case 'match_all':
$matches = array();
preg_match_all( $pattern, $subject, $matches );
break;
}
ini_set( 'pcre.backtrack_limit', $saved_config );
switch( preg_last_error() ) {
case PREG_NO_ERROR:
return $i;
case PREG_BACKTRACK_LIMIT_ERROR:
continue;
case PREG_RECURSION_LIMIT_ERROR:
trigger_error('PCRE recursion limit encountered before backtrack limit.');
return;
case PREG_BAD_UTF8_ERROR:
trigger_error('UTF-8 error during PCRE benchmark.');
return;
case PREG_INTERNAL_ERROR:
trigger_error('Internal error during PCRE benchmark.');
return;
default:
trigger_error('Unexpected error during PCRE benchmark.');
return;
switch ( preg_last_error() ) {
case PREG_NO_ERROR:
return $i;
case PREG_BACKTRACK_LIMIT_ERROR:
continue;
case PREG_RECURSION_LIMIT_ERROR:
trigger_error( 'PCRE recursion limit encountered before backtrack limit.' );
return;
case PREG_BAD_UTF8_ERROR:
trigger_error( 'UTF-8 error during PCRE benchmark.' );
return;
case PREG_INTERNAL_ERROR:
trigger_error( 'Internal error during PCRE benchmark.' );
return;
default:
trigger_error( 'Unexpected error during PCRE benchmark.' );
return;
}
}
@@ -470,8 +527,8 @@ function test_rest_expand_compact_links( $links ) {
$name = explode( ':', $rel );
$curie = wp_list_filter( $links['curies'], array( 'name' => $name[0] ) );
$full_uri = str_replace( '{rel}', $name[1], $curie[0]['href'] );
$curie = wp_list_filter( $links['curies'], array( 'name' => $name[0] ) );
$full_uri = str_replace( '{rel}', $name[1], $curie[0]['href'] );
$links[ $full_uri ] = $links_array;
unset( $links[ $rel ] );
}
+95 -88
View File
@@ -25,173 +25,180 @@ class WPProfiler {
* PHP5 constructor.
*/
function __construct() {
$this->stack = array();
$this->stack = array();
$this->profile = array();
}
function start($name) {
function start( $name ) {
$time = $this->microtime();
if (!$this->stack) {
if ( ! $this->stack ) {
// log all actions and filters
add_filter('all', array($this, 'log_filter'));
add_filter( 'all', array( $this, 'log_filter' ) );
}
// reset the wpdb queries log, storing it on the profile stack if necessary
global $wpdb;
if ($this->stack) {
$this->stack[count($this->stack)-1]['queries'] = $wpdb->queries;
if ( $this->stack ) {
$this->stack[ count( $this->stack ) - 1 ]['queries'] = $wpdb->queries;
}
$wpdb->queries = array();
global $wp_object_cache;
$this->stack[] = array(
'start' => $time,
'name' => $name,
'cache_cold_hits' => $wp_object_cache->cold_cache_hits,
'cache_warm_hits' => $wp_object_cache->warm_cache_hits,
'cache_misses' => $wp_object_cache->cache_misses,
'cache_dirty_objects' => $this->_dirty_objects_count($wp_object_cache->dirty_objects),
'actions' => array(),
'filters' => array(),
'queries' => array(),
'start' => $time,
'name' => $name,
'cache_cold_hits' => $wp_object_cache->cold_cache_hits,
'cache_warm_hits' => $wp_object_cache->warm_cache_hits,
'cache_misses' => $wp_object_cache->cache_misses,
'cache_dirty_objects' => $this->_dirty_objects_count( $wp_object_cache->dirty_objects ),
'actions' => array(),
'filters' => array(),
'queries' => array(),
);
}
function stop() {
$item = array_pop($this->stack);
$time = $this->microtime($item['start']);
$item = array_pop( $this->stack );
$time = $this->microtime( $item['start'] );
$name = $item['name'];
global $wpdb;
$item['queries'] = $wpdb->queries;
global $wp_object_cache;
$cache_dirty_count = $this->_dirty_objects_count($wp_object_cache->dirty_objects);
$cache_dirty_delta = $this->array_sub($cache_dirty_count, $item['cache_dirty_objects']);
$cache_dirty_count = $this->_dirty_objects_count( $wp_object_cache->dirty_objects );
$cache_dirty_delta = $this->array_sub( $cache_dirty_count, $item['cache_dirty_objects'] );
if (isset($this->profile[$name])) {
$this->profile[$name]['time'] += $time;
$this->profile[$name]['calls'] ++;
$this->profile[$name]['cache_cold_hits'] += ($wp_object_cache->cold_cache_hits - $item['cache_cold_hits']);
$this->profile[$name]['cache_warm_hits'] += ($wp_object_cache->warm_cache_hits - $item['cache_warm_hits']);
$this->profile[$name]['cache_misses'] += ($wp_object_cache->cache_misses - $item['cache_misses']);
$this->profile[$name]['cache_dirty_objects'] = array_add( $this->profile[$name]['cache_dirty_objects'], $cache_dirty_delta) ;
$this->profile[$name]['actions'] = array_add( $this->profile[$name]['actions'], $item['actions'] );
$this->profile[$name]['filters'] = array_add( $this->profile[$name]['filters'], $item['filters'] );
$this->profile[$name]['queries'] = array_add( $this->profile[$name]['queries'], $item['queries'] );
if ( isset( $this->profile[ $name ] ) ) {
$this->profile[ $name ]['time'] += $time;
$this->profile[ $name ]['calls'] ++;
$this->profile[ $name ]['cache_cold_hits'] += ( $wp_object_cache->cold_cache_hits - $item['cache_cold_hits'] );
$this->profile[ $name ]['cache_warm_hits'] += ( $wp_object_cache->warm_cache_hits - $item['cache_warm_hits'] );
$this->profile[ $name ]['cache_misses'] += ( $wp_object_cache->cache_misses - $item['cache_misses'] );
$this->profile[ $name ]['cache_dirty_objects'] = array_add( $this->profile[ $name ]['cache_dirty_objects'], $cache_dirty_delta );
$this->profile[ $name ]['actions'] = array_add( $this->profile[ $name ]['actions'], $item['actions'] );
$this->profile[ $name ]['filters'] = array_add( $this->profile[ $name ]['filters'], $item['filters'] );
$this->profile[ $name ]['queries'] = array_add( $this->profile[ $name ]['queries'], $item['queries'] );
#$this->_query_summary($item['queries'], $this->profile[$name]['queries']);
}
else {
} else {
$queries = array();
$this->_query_summary($item['queries'], $queries);
$this->profile[$name] = array(
'time' => $time,
'calls' => 1,
'cache_cold_hits' => ($wp_object_cache->cold_cache_hits - $item['cache_cold_hits']),
'cache_warm_hits' => ($wp_object_cache->warm_cache_hits - $item['cache_warm_hits']),
'cache_misses' => ($wp_object_cache->cache_misses - $item['cache_misses']),
'cache_dirty_objects' => $cache_dirty_delta,
'actions' => $item['actions'],
'filters' => $item['filters'],
# 'queries' => $item['queries'],
'queries' => $queries,
$this->_query_summary( $item['queries'], $queries );
$this->profile[ $name ] = array(
'time' => $time,
'calls' => 1,
'cache_cold_hits' => ( $wp_object_cache->cold_cache_hits - $item['cache_cold_hits'] ),
'cache_warm_hits' => ( $wp_object_cache->warm_cache_hits - $item['cache_warm_hits'] ),
'cache_misses' => ( $wp_object_cache->cache_misses - $item['cache_misses'] ),
'cache_dirty_objects' => $cache_dirty_delta,
'actions' => $item['actions'],
'filters' => $item['filters'],
# 'queries' => $item['queries'],
'queries' => $queries,
);
}
if (!$this->stack) {
remove_filter('all', array($this, 'log_filter'));
if ( ! $this->stack ) {
remove_filter( 'all', array( $this, 'log_filter' ) );
}
}
function microtime($since = 0.0) {
list($usec, $sec) = explode(' ', microtime());
return (float)$sec + (float)$usec - $since;
function microtime( $since = 0.0 ) {
list($usec, $sec) = explode( ' ', microtime() );
return (float) $sec + (float) $usec - $since;
}
function log_filter($tag) {
if ($this->stack) {
function log_filter( $tag ) {
if ( $this->stack ) {
global $wp_actions;
if ($tag == end($wp_actions))
@$this->stack[count($this->stack)-1]['actions'][$tag] ++;
else
@$this->stack[count($this->stack)-1]['filters'][$tag] ++;
if ( $tag == end( $wp_actions ) ) {
@$this->stack[ count( $this->stack ) - 1 ]['actions'][ $tag ] ++;
} else {
@$this->stack[ count( $this->stack ) - 1 ]['filters'][ $tag ] ++;
}
}
return $arg;
}
function log_action($tag) {
if ($this->stack)
@$this->stack[count($this->stack)-1]['actions'][$tag] ++;
function log_action( $tag ) {
if ( $this->stack ) {
@$this->stack[ count( $this->stack ) - 1 ]['actions'][ $tag ] ++;
}
}
function _current_action() {
global $wp_actions;
return $wp_actions[count($wp_actions)-1];
return $wp_actions[ count( $wp_actions ) - 1 ];
}
function results() {
return $this->profile;
}
function _query_summary($queries, &$out) {
foreach ($queries as $q) {
function _query_summary( $queries, &$out ) {
foreach ( $queries as $q ) {
$sql = $q[0];
$sql = preg_replace('/(WHERE \w+ =) \d+/', '$1 x', $sql);
$sql = preg_replace('/(WHERE \w+ =) \'\[-\w]+\'/', '$1 \'xxx\'', $sql);
$sql = preg_replace( '/(WHERE \w+ =) \d+/', '$1 x', $sql );
$sql = preg_replace( '/(WHERE \w+ =) \'\[-\w]+\'/', '$1 \'xxx\'', $sql );
@$out[$sql] ++;
@$out[ $sql ] ++;
}
asort($out);
asort( $out );
return;
}
function _query_count($queries) {
function _query_count( $queries ) {
// this requires the savequeries patch at https://core.trac.wordpress.org/ticket/5218
$out = array();
foreach ($queries as $q) {
if (empty($q[2]))
foreach ( $queries as $q ) {
if ( empty( $q[2] ) ) {
@$out['unknown'] ++;
else
@$out[$q[2]] ++;
} else {
@$out[ $q[2] ] ++;
}
}
return $out;
}
function _dirty_objects_count($dirty_objects) {
function _dirty_objects_count( $dirty_objects ) {
$out = array();
foreach (array_keys($dirty_objects) as $group)
$out[$group] = count($dirty_objects[$group]);
foreach ( array_keys( $dirty_objects ) as $group ) {
$out[ $group ] = count( $dirty_objects[ $group ] );
}
return $out;
}
function array_add($a, $b) {
function array_add( $a, $b ) {
$out = $a;
foreach (array_keys($b) as $key)
if (array_key_exists($key, $out))
$out[$key] += $b[$key];
else
$out[$key] = $b[$key];
foreach ( array_keys( $b ) as $key ) {
if ( array_key_exists( $key, $out ) ) {
$out[ $key ] += $b[ $key ];
} else {
$out[ $key ] = $b[ $key ];
}
}
return $out;
}
function array_sub($a, $b) {
function array_sub( $a, $b ) {
$out = $a;
foreach (array_keys($b) as $key)
if (array_key_exists($key, $b))
$out[$key] -= $b[$key];
foreach ( array_keys( $b ) as $key ) {
if ( array_key_exists( $key, $b ) ) {
$out[ $key ] -= $b[ $key ];
}
}
return $out;
}
function print_summary() {
$results = $this->results();
printf("\nname calls time action filter warm cold misses dirty\n");
foreach ($results as $name=>$stats) {
printf("%24.24s %6d %6.4f %6d %6d %6d %6d %6d %6d\n", $name, $stats['calls'], $stats['time'], array_sum($stats['actions']), array_sum($stats['filters']), $stats['cache_warm_hits'], $stats['cache_cold_hits'], $stats['cache_misses'], array_sum($stats['cache_dirty_objects']));
printf( "\nname calls time action filter warm cold misses dirty\n" );
foreach ( $results as $name => $stats ) {
printf( "%24.24s %6d %6.4f %6d %6d %6d %6d %6d %6d\n", $name, $stats['calls'], $stats['time'], array_sum( $stats['actions'] ), array_sum( $stats['filters'] ), $stats['cache_warm_hits'], $stats['cache_cold_hits'], $stats['cache_misses'], array_sum( $stats['cache_dirty_objects'] ) );
}
}
}
@@ -199,8 +206,8 @@ class WPProfiler {
global $wppf;
$wppf = new WPProfiler();
function wppf_start($name) {
$GLOBALS['wppf']->start($name);
function wppf_start( $name ) {
$GLOBALS['wppf']->start( $name );
}
function wppf_stop() {
@@ -215,4 +222,4 @@ function wppf_print_summary() {
$GLOBALS['wppf']->print_summary();
}
?>
+132 -123
View File
@@ -8,108 +8,108 @@
class Tests_Actions extends WP_UnitTestCase {
function test_simple_action() {
$a = new MockAction();
$a = new MockAction();
$tag = __FUNCTION__;
add_action($tag, array(&$a, 'action'));
do_action($tag);
add_action( $tag, array( &$a, 'action' ) );
do_action( $tag );
// only one event occurred for the hook, with empty args
$this->assertEquals(1, $a->get_call_count());
$this->assertEquals( 1, $a->get_call_count() );
// only our hook was called
$this->assertEquals(array($tag), $a->get_tags());
$this->assertEquals( array( $tag ), $a->get_tags() );
$argsvar = $a->get_args();
$args = array_pop( $argsvar );
$this->assertEquals(array(''), $args);
$args = array_pop( $argsvar );
$this->assertEquals( array( '' ), $args );
}
function test_remove_action() {
$a = new MockAction();
$a = new MockAction();
$tag = __FUNCTION__;
add_action($tag, array(&$a, 'action'));
do_action($tag);
add_action( $tag, array( &$a, 'action' ) );
do_action( $tag );
// make sure our hook was called correctly
$this->assertEquals(1, $a->get_call_count());
$this->assertEquals(array($tag), $a->get_tags());
$this->assertEquals( 1, $a->get_call_count() );
$this->assertEquals( array( $tag ), $a->get_tags() );
// now remove the action, do it again, and make sure it's not called this time
remove_action($tag, array(&$a, 'action'));
do_action($tag);
$this->assertEquals(1, $a->get_call_count());
$this->assertEquals(array($tag), $a->get_tags());
remove_action( $tag, array( &$a, 'action' ) );
do_action( $tag );
$this->assertEquals( 1, $a->get_call_count() );
$this->assertEquals( array( $tag ), $a->get_tags() );
}
function test_has_action() {
$tag = __FUNCTION__;
$tag = __FUNCTION__;
$func = __FUNCTION__ . '_func';
$this->assertFalse( has_action($tag, $func) );
$this->assertFalse( has_action($tag) );
add_action($tag, $func);
$this->assertEquals( 10, has_action($tag, $func) );
$this->assertTrue( has_action($tag) );
remove_action($tag, $func);
$this->assertFalse( has_action($tag, $func) );
$this->assertFalse( has_action($tag) );
$this->assertFalse( has_action( $tag, $func ) );
$this->assertFalse( has_action( $tag ) );
add_action( $tag, $func );
$this->assertEquals( 10, has_action( $tag, $func ) );
$this->assertTrue( has_action( $tag ) );
remove_action( $tag, $func );
$this->assertFalse( has_action( $tag, $func ) );
$this->assertFalse( has_action( $tag ) );
}
// one tag with multiple actions
function test_multiple_actions() {
$a1 = new MockAction();
$a2 = new MockAction();
$a1 = new MockAction();
$a2 = new MockAction();
$tag = __FUNCTION__;
// add both actions to the hook
add_action($tag, array(&$a1, 'action'));
add_action($tag, array(&$a2, 'action'));
add_action( $tag, array( &$a1, 'action' ) );
add_action( $tag, array( &$a2, 'action' ) );
do_action($tag);
do_action( $tag );
// both actions called once each
$this->assertEquals(1, $a1->get_call_count());
$this->assertEquals(1, $a2->get_call_count());
$this->assertEquals( 1, $a1->get_call_count() );
$this->assertEquals( 1, $a2->get_call_count() );
}
function test_action_args_1() {
$a = new MockAction();
$a = new MockAction();
$tag = __FUNCTION__;
$val = __FUNCTION__ . '_val';
add_action($tag, array(&$a, 'action'));
add_action( $tag, array( &$a, 'action' ) );
// call the action with a single argument
do_action($tag, $val);
do_action( $tag, $val );
$call_count = $a->get_call_count();
$this->assertEquals(1, $call_count);
$this->assertEquals( 1, $call_count );
$argsvar = $a->get_args();
$this->assertEquals( array( $val ), array_pop( $argsvar ) );
}
function test_action_args_2() {
$a1 = new MockAction();
$a2 = new MockAction();
$tag = __FUNCTION__;
$a1 = new MockAction();
$a2 = new MockAction();
$tag = __FUNCTION__;
$val1 = __FUNCTION__ . '_val1';
$val2 = __FUNCTION__ . '_val2';
// a1 accepts two arguments, a2 doesn't
add_action($tag, array(&$a1, 'action'), 10, 2);
add_action($tag, array(&$a2, 'action'));
add_action( $tag, array( &$a1, 'action' ), 10, 2 );
add_action( $tag, array( &$a2, 'action' ) );
// call the action with two arguments
do_action($tag, $val1, $val2);
do_action( $tag, $val1, $val2 );
$call_count = $a1->get_call_count();
// a1 should be called with both args
$this->assertEquals(1, $call_count);
$this->assertEquals( 1, $call_count );
$argsvar1 = $a1->get_args();
$this->assertEquals( array( $val1, $val2 ), array_pop( $argsvar1 ) );
// a2 should be called with one only
$this->assertEquals(1, $a2->get_call_count());
$this->assertEquals( 1, $a2->get_call_count() );
$argsvar2 = $a2->get_args();
$this->assertEquals( array( $val1 ), array_pop( $argsvar2 ) );
}
@@ -122,10 +122,10 @@ class Tests_Actions extends WP_UnitTestCase {
* @ticket 17817
*/
function test_action_args_3() {
$a1 = new MockAction();
$a2 = new MockAction();
$a3 = new MockAction();
$tag = __FUNCTION__;
$a1 = new MockAction();
$a2 = new MockAction();
$a3 = new MockAction();
$tag = __FUNCTION__;
$val1 = __FUNCTION__ . '_val1';
$val2 = __FUNCTION__ . '_val2';
@@ -154,32 +154,32 @@ class Tests_Actions extends WP_UnitTestCase {
}
function test_action_priority() {
$a = new MockAction();
$a = new MockAction();
$tag = __FUNCTION__;
add_action($tag, array(&$a, 'action'), 10);
add_action($tag, array(&$a, 'action2'), 9);
do_action($tag);
add_action( $tag, array( &$a, 'action' ), 10 );
add_action( $tag, array( &$a, 'action2' ), 9 );
do_action( $tag );
// two events, one per action
$this->assertEquals(2, $a->get_call_count());
$this->assertEquals( 2, $a->get_call_count() );
$expected = array (
$expected = array(
// action2 is called first because it has priority 9
array (
array(
'action' => 'action2',
'tag' => $tag,
'args' => array('')
'tag' => $tag,
'args' => array( '' ),
),
// action 1 is called second
array (
array(
'action' => 'action',
'tag' => $tag,
'args' => array('')
'tag' => $tag,
'args' => array( '' ),
),
);
$this->assertEquals($expected, $a->get_events());
$this->assertEquals( $expected, $a->get_events() );
}
function test_did_action() {
@@ -187,79 +187,80 @@ class Tests_Actions extends WP_UnitTestCase {
$tag2 = 'action2';
// do action tag1 but not tag2
do_action($tag1);
$this->assertEquals(1, did_action($tag1));
$this->assertEquals(0, did_action($tag2));
do_action( $tag1 );
$this->assertEquals( 1, did_action( $tag1 ) );
$this->assertEquals( 0, did_action( $tag2 ) );
// do action tag2 a random number of times
$count = rand(0, 10);
for ($i=0; $i<$count; $i++)
do_action($tag2);
$count = rand( 0, 10 );
for ( $i = 0; $i < $count; $i++ ) {
do_action( $tag2 );
}
// tag1's count hasn't changed, tag2 should be correct
$this->assertEquals(1, did_action($tag1));
$this->assertEquals($count, did_action($tag2));
$this->assertEquals( 1, did_action( $tag1 ) );
$this->assertEquals( $count, did_action( $tag2 ) );
}
function test_all_action() {
$a = new MockAction();
$a = new MockAction();
$tag1 = __FUNCTION__ . '_1';
$tag2 = __FUNCTION__ . '_2';
// add an 'all' action
add_action('all', array(&$a, 'action'));
$this->assertEquals(10, has_filter('all', array(&$a, 'action')));
add_action( 'all', array( &$a, 'action' ) );
$this->assertEquals( 10, has_filter( 'all', array( &$a, 'action' ) ) );
// do some actions
do_action($tag1);
do_action($tag2);
do_action($tag1);
do_action($tag1);
do_action( $tag1 );
do_action( $tag2 );
do_action( $tag1 );
do_action( $tag1 );
// our action should have been called once for each tag
$this->assertEquals(4, $a->get_call_count());
$this->assertEquals( 4, $a->get_call_count() );
// only our hook was called
$this->assertEquals(array($tag1, $tag2, $tag1, $tag1), $a->get_tags());
$this->assertEquals( array( $tag1, $tag2, $tag1, $tag1 ), $a->get_tags() );
remove_action('all', array(&$a, 'action'));
$this->assertFalse(has_filter('all', array(&$a, 'action')));
remove_action( 'all', array( &$a, 'action' ) );
$this->assertFalse( has_filter( 'all', array( &$a, 'action' ) ) );
}
function test_remove_all_action() {
$a = new MockAction();
$a = new MockAction();
$tag = __FUNCTION__;
add_action('all', array(&$a, 'action'));
$this->assertEquals(10, has_filter('all', array(&$a, 'action')));
do_action($tag);
add_action( 'all', array( &$a, 'action' ) );
$this->assertEquals( 10, has_filter( 'all', array( &$a, 'action' ) ) );
do_action( $tag );
// make sure our hook was called correctly
$this->assertEquals(1, $a->get_call_count());
$this->assertEquals(array($tag), $a->get_tags());
$this->assertEquals( 1, $a->get_call_count() );
$this->assertEquals( array( $tag ), $a->get_tags() );
// now remove the action, do it again, and make sure it's not called this time
remove_action('all', array(&$a, 'action'));
$this->assertFalse(has_filter('all', array(&$a, 'action')));
do_action($tag);
$this->assertEquals(1, $a->get_call_count());
$this->assertEquals(array($tag), $a->get_tags());
remove_action( 'all', array( &$a, 'action' ) );
$this->assertFalse( has_filter( 'all', array( &$a, 'action' ) ) );
do_action( $tag );
$this->assertEquals( 1, $a->get_call_count() );
$this->assertEquals( array( $tag ), $a->get_tags() );
}
function test_action_ref_array() {
$obj = new stdClass();
$a = new MockAction();
$a = new MockAction();
$tag = __FUNCTION__;
add_action($tag, array(&$a, 'action'));
add_action( $tag, array( &$a, 'action' ) );
do_action_ref_array($tag, array(&$obj));
do_action_ref_array( $tag, array( &$obj ) );
$args = $a->get_args();
$this->assertSame($args[0][0], $obj);
$this->assertSame( $args[0][0], $obj );
// just in case we don't trust assertSame
$obj->foo = true;
$this->assertFalse( empty($args[0][0]->foo) );
$this->assertFalse( empty( $args[0][0]->foo ) );
}
/**
@@ -270,19 +271,22 @@ class Tests_Actions extends WP_UnitTestCase {
$tag = __FUNCTION__;
add_action($tag, array(&$a, 'action'));
add_action( $tag, array( &$a, 'action' ) );
$context = array( 'key1' => 'val1' );
do_action($tag, $context);
do_action( $tag, $context );
$args = $a->get_args();
$this->assertSame($args[0][0], $context);
$this->assertSame( $args[0][0], $context );
$context2 = array( 'key2' => 'val2', 'key3' => 'val3' );
do_action($tag, $context2);
$context2 = array(
'key2' => 'val2',
'key3' => 'val3',
);
do_action( $tag, $context2 );
$args = $a->get_args();
$this->assertSame($args[1][0], $context2);
$this->assertSame( $args[1][0], $context2 );
}
@@ -301,8 +305,8 @@ class Tests_Actions extends WP_UnitTestCase {
*/
function test_action_recursion() {
$tag = __FUNCTION__;
$a = new MockAction();
$b = new MockAction();
$a = new MockAction();
$b = new MockAction();
add_action( $tag, array( $a, 'action' ), 11, 1 );
add_action( $tag, array( $b, 'action' ), 13, 1 );
@@ -328,11 +332,11 @@ class Tests_Actions extends WP_UnitTestCase {
*/
function test_action_callback_manipulation_while_running() {
$tag = __FUNCTION__;
$a = new MockAction();
$b = new MockAction();
$c = new MockAction();
$d = new MockAction();
$e = new MockAction();
$a = new MockAction();
$b = new MockAction();
$c = new MockAction();
$d = new MockAction();
$e = new MockAction();
add_action( $tag, array( $a, 'action' ), 11, 2 );
add_action( $tag, array( $this, 'action_that_manipulates_a_running_hook' ), 12, 2 );
@@ -349,10 +353,10 @@ class Tests_Actions extends WP_UnitTestCase {
}
function action_that_manipulates_a_running_hook( $tag, $mocks ) {
remove_action( $tag, array( $mocks[ 1 ], 'action' ), 12, 2 );
add_action( $tag, array( $mocks[ 2 ], 'action' ), 12, 2 );
add_action( $tag, array( $mocks[ 3 ], 'action' ), 13, 2 );
add_action( $tag, array( $mocks[ 4 ], 'action' ), 10, 2 );
remove_action( $tag, array( $mocks[1], 'action' ), 12, 2 );
add_action( $tag, array( $mocks[2], 'action' ), 12, 2 );
add_action( $tag, array( $mocks[3], 'action' ), 13, 2 );
add_action( $tag, array( $mocks[4], 'action' ), 10, 2 );
}
/**
@@ -363,7 +367,7 @@ class Tests_Actions extends WP_UnitTestCase {
*/
function test_remove_anonymous_callback() {
$tag = __FUNCTION__;
$a = new MockAction();
$a = new MockAction();
add_action( $tag, array( $a, 'action' ), 12, 1 );
$this->assertTrue( has_action( $tag ) );
@@ -373,12 +377,12 @@ class Tests_Actions extends WP_UnitTestCase {
foreach ( $hook as $priority => $filter ) {
foreach ( $filter as $identifier => $function ) {
if ( is_array( $function )
&& is_a( $function['function'][ 0 ], 'MockAction' )
&& 'action' === $function['function'][ 1 ]
&& is_a( $function['function'][0], 'MockAction' )
&& 'action' === $function['function'][1]
) {
remove_filter(
$tag,
array( $function['function'][ 0 ], 'action' ),
array( $function['function'][0], 'action' ),
$priority
);
}
@@ -400,13 +404,18 @@ class Tests_Actions extends WP_UnitTestCase {
add_action( $tag, '__return_null', 11, 1 );
$this->assertTrue( isset( $wp_filter[ $tag ][ 11 ] ) );
$this->assertArrayHasKey( '__return_null', $wp_filter[ $tag ][ 11 ] );
$this->assertTrue( isset( $wp_filter[ $tag ][11] ) );
$this->assertArrayHasKey( '__return_null', $wp_filter[ $tag ][11] );
unset( $wp_filter[ $tag ][ 11 ] );
unset( $wp_filter[ $tag ][11] );
$this->assertFalse( has_action( $tag, '__return_null' ) );
$wp_filter[ $tag ][ 11 ] = array( '__return_null' => array( 'function' => '__return_null', 'accepted_args' => 1 ) );
$wp_filter[ $tag ][11] = array(
'__return_null' => array(
'function' => '__return_null',
'accepted_args' => 1,
),
);
$this->assertEquals( 11, has_action( $tag, '__return_null' ) );
}
@@ -436,7 +445,7 @@ class Tests_Actions extends WP_UnitTestCase {
$wp_current_filter[] = 'testing';
$this->assertTrue( doing_filter() ); // No action is passed in, and a filter is being processed
$this->assertTrue( doing_filter( 'testing') ); // Filter is passed in and is being processed
$this->assertTrue( doing_filter( 'testing' ) ); // Filter is passed in and is being processed
$this->assertFalse( doing_filter( 'something_else' ) ); // Filter is passed in but not being processed
$wp_current_filter = array();
@@ -455,7 +464,7 @@ class Tests_Actions extends WP_UnitTestCase {
$wp_current_filter[] = 'testing';
$this->assertTrue( doing_action() ); // No action is passed in, and a filter is being processed
$this->assertTrue( doing_action( 'testing') ); // Action is passed in and is being processed
$this->assertTrue( doing_action( 'testing' ) ); // Action is passed in and is being processed
$this->assertFalse( doing_action( 'something_else' ) ); // Action is passed in but not being processed
$wp_current_filter = array();
+16 -12
View File
@@ -11,26 +11,30 @@ class Tests_Actions_Closures extends WP_UnitTestCase {
* @ticket 10493
*/
function test_action_closure() {
$tag = 'test_action_closure';
$closure = function($a, $b) { $GLOBALS[$a] = $b;};
add_action($tag, $closure, 10, 2);
$tag = 'test_action_closure';
$closure = function( $a, $b ) {
$GLOBALS[ $a ] = $b;
};
add_action( $tag, $closure, 10, 2 );
$this->assertSame( 10, has_action($tag, $closure) );
$this->assertSame( 10, has_action( $tag, $closure ) );
$context = array( 'val1', 'val2' );
do_action($tag, $context[0], $context[1]);
do_action( $tag, $context[0], $context[1] );
$this->assertSame($GLOBALS[$context[0]], $context[1]);
$this->assertSame( $GLOBALS[ $context[0] ], $context[1] );
$tag2 = 'test_action_closure_2';
$closure2 = function() { $GLOBALS['closure_no_args'] = true;};
add_action($tag2, $closure2);
$tag2 = 'test_action_closure_2';
$closure2 = function() {
$GLOBALS['closure_no_args'] = true;
};
add_action( $tag2, $closure2 );
$this->assertSame( 10, has_action($tag2, $closure2) );
$this->assertSame( 10, has_action( $tag2, $closure2 ) );
do_action($tag2);
do_action( $tag2 );
$this->assertTrue($GLOBALS['closure_no_args']);
$this->assertTrue( $GLOBALS['closure_no_args'] );
remove_action( $tag, $closure );
remove_action( $tag2, $closure2 );
+15 -11
View File
@@ -27,18 +27,22 @@ class Tests_Admin_IncludesComment extends WP_UnitTestCase {
public static function wpSetUpBeforeClass( $factory ) {
self::$post_id = $factory->post->create();
self::$comment_ids[] = $factory->comment->create( array(
'comment_author' => 1,
'comment_date' => '2014-05-06 12:00:00',
'comment_date_gmt' => '2014-05-06 07:00:00',
'comment_post_ID' => self::$post_id,
) );
self::$comment_ids[] = $factory->comment->create(
array(
'comment_author' => 1,
'comment_date' => '2014-05-06 12:00:00',
'comment_date_gmt' => '2014-05-06 07:00:00',
'comment_post_ID' => self::$post_id,
)
);
self::$comment_ids[] = $factory->comment->create( array(
'comment_author' => 2,
'comment_date' => '2004-01-02 12:00:00',
'comment_post_ID' => self::$post_id,
) );
self::$comment_ids[] = $factory->comment->create(
array(
'comment_author' => 2,
'comment_date' => '2004-01-02 12:00:00',
'comment_post_ID' => self::$post_id,
)
);
}
/**
@@ -201,53 +201,55 @@ class Test_WP_Community_Events extends WP_UnitTestCase {
public function _http_request_valid_response() {
return array(
'headers' => '',
'body' => wp_json_encode( array(
'location' => $this->get_user_location(),
'events' => array(
array(
'type' => 'meetup',
'title' => 'Flexbox + CSS Grid: Magic for Responsive Layouts',
'url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/events/236031233/',
'meetup' => 'The East Bay WordPress Meetup Group',
'meetup_url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/',
'date' => date( 'Y-m-d H:i:s', strtotime( 'next Sunday 1pm' ) ),
'location' => array(
'location' => 'Oakland, CA, USA',
'country' => 'us',
'latitude' => 37.808453,
'longitude' => -122.26593,
'body' => wp_json_encode(
array(
'location' => $this->get_user_location(),
'events' => array(
array(
'type' => 'meetup',
'title' => 'Flexbox + CSS Grid: Magic for Responsive Layouts',
'url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/events/236031233/',
'meetup' => 'The East Bay WordPress Meetup Group',
'meetup_url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/',
'date' => date( 'Y-m-d H:i:s', strtotime( 'next Sunday 1pm' ) ),
'location' => array(
'location' => 'Oakland, CA, USA',
'country' => 'us',
'latitude' => 37.808453,
'longitude' => -122.26593,
),
),
array(
'type' => 'meetup',
'title' => 'Part 3- Site Maintenance - Tools to Make It Easy',
'url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/events/237706839/',
'meetup' => 'WordPress Bay Area Foothills Group',
'meetup_url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/',
'date' => date( 'Y-m-d H:i:s', strtotime( 'next Wednesday 1:30pm' ) ),
'location' => array(
'location' => 'Milpitas, CA, USA',
'country' => 'us',
'latitude' => 37.432813,
'longitude' => -121.907095,
),
),
array(
'type' => 'wordcamp',
'title' => 'WordCamp Kansas City',
'url' => 'https://2017.kansascity.wordcamp.org',
'meetup' => null,
'meetup_url' => null,
'date' => date( 'Y-m-d H:i:s', strtotime( 'next Saturday' ) ),
'location' => array(
'location' => 'Kansas City, MO',
'country' => 'US',
'latitude' => 39.0392325,
'longitude' => -94.577076,
),
),
),
array(
'type' => 'meetup',
'title' => 'Part 3- Site Maintenance - Tools to Make It Easy',
'url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/events/237706839/',
'meetup' => 'WordPress Bay Area Foothills Group',
'meetup_url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/',
'date' => date( 'Y-m-d H:i:s', strtotime( 'next Wednesday 1:30pm' ) ),
'location' => array(
'location' => 'Milpitas, CA, USA',
'country' => 'us',
'latitude' => 37.432813,
'longitude' => -121.907095,
),
),
array(
'type' => 'wordcamp',
'title' => 'WordCamp Kansas City',
'url' => 'https://2017.kansascity.wordcamp.org',
'meetup' => null,
'meetup_url' => null,
'date' => date( 'Y-m-d H:i:s', strtotime( 'next Saturday' ) ),
'location' => array(
'location' => 'Kansas City, MO',
'country' => 'US',
'latitude' => 39.0392325,
'longitude' => -94.577076,
),
),
),
) ),
)
),
'response' => array(
'code' => 200,
),
+3 -3
View File
@@ -10,9 +10,9 @@ class Tests_Admin_includesFile extends WP_UnitTestCase {
* @ticket 20449
*/
function test_get_home_path() {
$home = get_option( 'home' );
$home = get_option( 'home' );
$siteurl = get_option( 'siteurl' );
$sfn = $_SERVER['SCRIPT_FILENAME'];
$sfn = $_SERVER['SCRIPT_FILENAME'];
$this->assertEquals( str_replace( '\\', '/', ABSPATH ), get_home_path() );
update_option( 'home', 'http://localhost' );
@@ -31,4 +31,4 @@ class Tests_Admin_includesFile extends WP_UnitTestCase {
update_option( 'siteurl', $siteurl );
$_SERVER['SCRIPT_FILENAME'] = $sfn;
}
}
}
+92 -72
View File
@@ -4,10 +4,10 @@
* @group admin
*/
class Tests_Admin_includesListTable extends WP_UnitTestCase {
protected static $top = array();
protected static $children = array();
protected static $top = array();
protected static $children = array();
protected static $grandchildren = array();
protected static $post_ids = array();
protected static $post_ids = array();
/**
* @var WP_Posts_List_Table
@@ -25,12 +25,14 @@ class Tests_Admin_includesListTable extends WP_UnitTestCase {
// create top level pages
$num_posts = 5;
foreach ( range( 1, $num_posts ) as $i ) {
$p = $factory->post->create_and_get( array(
'post_type' => 'page',
'post_title' => sprintf( 'Top Level Page %d', $i ),
) );
$p = $factory->post->create_and_get(
array(
'post_type' => 'page',
'post_title' => sprintf( 'Top Level Page %d', $i ),
)
);
self::$top[ $i ] = $p;
self::$top[ $i ] = $p;
self::$post_ids[] = $p->ID;
}
@@ -38,14 +40,16 @@ class Tests_Admin_includesListTable extends WP_UnitTestCase {
$num_children = 3;
foreach ( self::$top as $top => $top_page ) {
foreach ( range( 1, $num_children ) as $i ) {
$p = $factory->post->create_and_get( array(
'post_type' => 'page',
'post_parent' => $top_page->ID,
'post_title' => sprintf( 'Child %d', $i ),
) );
$p = $factory->post->create_and_get(
array(
'post_type' => 'page',
'post_parent' => $top_page->ID,
'post_title' => sprintf( 'Child %d', $i ),
)
);
self::$children[ $top ][ $i ] = $p;
self::$post_ids[] = $p->ID;
self::$post_ids[] = $p->ID;
}
}
@@ -54,14 +58,16 @@ class Tests_Admin_includesListTable extends WP_UnitTestCase {
foreach ( range( 3, 4 ) as $top ) {
foreach ( self::$children[ $top ] as $child => $child_page ) {
foreach ( range( 1, $num_grandchildren ) as $i ) {
$p = $factory->post->create_and_get( array(
'post_type' => 'page',
'post_parent' => $child_page->ID,
'post_title' => sprintf( 'Grandchild %d', $i ),
) );
$p = $factory->post->create_and_get(
array(
'post_type' => 'page',
'post_parent' => $child_page->ID,
'post_title' => sprintf( 'Grandchild %d', $i ),
)
);
self::$grandchildren[ $top ][ $child ][ $i ] = $p;
self::$post_ids[] = $p->ID;
self::$post_ids[] = $p->ID;
}
}
}
@@ -71,55 +77,63 @@ class Tests_Admin_includesListTable extends WP_UnitTestCase {
* @ticket 15459
*/
function test_list_hierarchical_pages_first_page() {
$this->_test_list_hierarchical_page( array(
'paged' => 1,
'posts_per_page' => 2,
), array(
self::$top[1]->ID,
self::$children[1][1]->ID,
) );
$this->_test_list_hierarchical_page(
array(
'paged' => 1,
'posts_per_page' => 2,
), array(
self::$top[1]->ID,
self::$children[1][1]->ID,
)
);
}
/**
* @ticket 15459
*/
function test_list_hierarchical_pages_second_page() {
$this->_test_list_hierarchical_page( array(
'paged' => 2,
'posts_per_page' => 2,
), array(
self::$top[1]->ID,
self::$children[1][2]->ID,
self::$children[1][3]->ID,
) );
$this->_test_list_hierarchical_page(
array(
'paged' => 2,
'posts_per_page' => 2,
), array(
self::$top[1]->ID,
self::$children[1][2]->ID,
self::$children[1][3]->ID,
)
);
}
/**
* @ticket 15459
*/
function test_search_hierarchical_pages_first_page() {
$this->_test_list_hierarchical_page( array(
'paged' => 1,
'posts_per_page' => 2,
's' => 'Child',
), array(
self::$children[1][1]->ID,
self::$children[1][2]->ID,
) );
$this->_test_list_hierarchical_page(
array(
'paged' => 1,
'posts_per_page' => 2,
's' => 'Child',
), array(
self::$children[1][1]->ID,
self::$children[1][2]->ID,
)
);
}
/**
* @ticket 15459
*/
function test_search_hierarchical_pages_second_page() {
$this->_test_list_hierarchical_page( array(
'paged' => 2,
'posts_per_page' => 2,
's' => 'Top',
), array(
self::$top[3]->ID,
self::$top[4]->ID,
) );
$this->_test_list_hierarchical_page(
array(
'paged' => 2,
'posts_per_page' => 2,
's' => 'Top',
), array(
self::$top[3]->ID,
self::$top[4]->ID,
)
);
}
/**
@@ -127,15 +141,17 @@ class Tests_Admin_includesListTable extends WP_UnitTestCase {
*/
function test_grandchildren_hierarchical_pages_first_page() {
// page 6 is the first page with grandchildren
$this->_test_list_hierarchical_page( array(
'paged' => 6,
'posts_per_page' => 2,
), array(
self::$top[3]->ID,
self::$children[3][1]->ID,
self::$grandchildren[3][1][1]->ID,
self::$grandchildren[3][1][2]->ID,
) );
$this->_test_list_hierarchical_page(
array(
'paged' => 6,
'posts_per_page' => 2,
), array(
self::$top[3]->ID,
self::$children[3][1]->ID,
self::$grandchildren[3][1][1]->ID,
self::$grandchildren[3][1][2]->ID,
)
);
}
/**
@@ -143,15 +159,17 @@ class Tests_Admin_includesListTable extends WP_UnitTestCase {
*/
function test_grandchildren_hierarchical_pages_second_page() {
// page 7 is the second page with grandchildren
$this->_test_list_hierarchical_page( array(
'paged' => 7,
'posts_per_page' => 2,
), array(
self::$top[3]->ID,
self::$children[3][1]->ID,
self::$grandchildren[3][1][3]->ID,
self::$children[3][2]->ID,
) );
$this->_test_list_hierarchical_page(
array(
'paged' => 7,
'posts_per_page' => 2,
), array(
self::$top[3]->ID,
self::$children[3][1]->ID,
self::$grandchildren[3][1][3]->ID,
self::$children[3][2]->ID,
)
);
}
/**
@@ -166,9 +184,11 @@ class Tests_Admin_includesListTable extends WP_UnitTestCase {
$_REQUEST['paged'] = $args['paged'];
$GLOBALS['per_page'] = $args['posts_per_page'];
$args = array_merge( array(
'post_type' => 'page',
), $args );
$args = array_merge(
array(
'post_type' => 'page',
), $args
);
// Mimic the behaviour of `wp_edit_posts_query()`:
if ( ! isset( $args['orderby'] ) ) {
+2 -1
View File
@@ -19,7 +19,8 @@ class Tests_Admin_includesMisc extends WP_UnitTestCase {
'http://wordpress.org/about/philosophy/#decisions'
=> 'wordpress.org/about/philosophy/#&hellip;', // shorten to 32 if > 35 after cleaning
);
foreach ( $tests as $k => $v )
foreach ( $tests as $k => $v ) {
$this->assertEquals( $v, url_shorten( $k ) );
}
}
}
+29 -29
View File
@@ -8,22 +8,22 @@ class Tests_Admin_includesPlugin extends WP_UnitTestCase {
$data = get_plugin_data( DIR_TESTDATA . '/plugins/hello.php' );
$default_headers = array(
'Name' => 'Hello Dolly',
'Title' => '<a href="http://wordpress.org/#">Hello Dolly</a>',
'PluginURI' => 'http://wordpress.org/#',
'Name' => 'Hello Dolly',
'Title' => '<a href="http://wordpress.org/#">Hello Dolly</a>',
'PluginURI' => 'http://wordpress.org/#',
'Description' => 'This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from Hello, Dolly in the upper right of your admin screen on every page. <cite>By <a href="http://ma.tt/">Matt Mullenweg</a>.</cite>',
'Author' => '<a href="http://ma.tt/">Matt Mullenweg</a>',
'AuthorURI' => 'http://ma.tt/',
'Version' => '1.5.1',
'TextDomain' => 'hello-dolly',
'DomainPath' => ''
'Author' => '<a href="http://ma.tt/">Matt Mullenweg</a>',
'AuthorURI' => 'http://ma.tt/',
'Version' => '1.5.1',
'TextDomain' => 'hello-dolly',
'DomainPath' => '',
);
$this->assertTrue( is_array($data) );
$this->assertTrue( is_array( $data ) );
foreach ($default_headers as $name => $value) {
$this->assertTrue(isset($data[$name]));
$this->assertEquals($value, $data[$name]);
foreach ( $default_headers as $name => $value ) {
$this->assertTrue( isset( $data[ $name ] ) );
$this->assertEquals( $value, $data[ $name ] );
}
}
@@ -41,16 +41,16 @@ class Tests_Admin_includesPlugin extends WP_UnitTestCase {
add_theme_page( 'With Spaces', 'With Spaces', 'manage_options', 'With Spaces', 'mt_tools_page' );
add_pages_page( 'Appending Query Arg', 'Test Pages', 'edit_pages', 'testpages', 'mt_pages_page' );
$expected['testsettings'] = 'http://example.com/wp-admin/options-general.php?page=testsettings';
$expected['testtools'] = 'http://example.com/wp-admin/tools.php?page=testtools';
$expected['testsettings'] = 'http://example.com/wp-admin/options-general.php?page=testsettings';
$expected['testtools'] = 'http://example.com/wp-admin/tools.php?page=testtools';
$expected['mt-top-level-handle'] = 'http://example.com/wp-admin/admin.php?page=mt-top-level-handle';
$expected['sub-page'] = 'http://example.com/wp-admin/admin.php?page=sub-page';
$expected['sub-page2'] = 'http://example.com/wp-admin/admin.php?page=sub-page2';
$expected['not_registered'] = '';
$expected['With Spaces'] = 'http://example.com/wp-admin/themes.php?page=With%20Spaces';
$expected['testpages'] = 'http://example.com/wp-admin/edit.php?post_type=page&#038;page=testpages';
$expected['sub-page'] = 'http://example.com/wp-admin/admin.php?page=sub-page';
$expected['sub-page2'] = 'http://example.com/wp-admin/admin.php?page=sub-page2';
$expected['not_registered'] = '';
$expected['With Spaces'] = 'http://example.com/wp-admin/themes.php?page=With%20Spaces';
$expected['testpages'] = 'http://example.com/wp-admin/edit.php?post_type=page&#038;page=testpages';
foreach ($expected as $name => $value) {
foreach ( $expected as $name => $value ) {
$this->assertEquals( $value, menu_page_url( $name, false ) );
}
@@ -99,14 +99,14 @@ class Tests_Admin_includesPlugin extends WP_UnitTestCase {
public function test_get_plugin_files_folder() {
$plugin_dir = WP_PLUGIN_DIR . '/list_files_test_plugin';
@mkdir( $plugin_dir );
$plugin = $this->_create_plugin(null, 'list_files_test_plugin.php', $plugin_dir );
$plugin = $this->_create_plugin( null, 'list_files_test_plugin.php', $plugin_dir );
$sub_dir = trailingslashit( dirname( $plugin[1] ) ) . 'subdir';
@mkdir( $sub_dir );
@file_put_contents( $sub_dir . '/subfile.php', '<?php // Silence.' );
$plugin_files = get_plugin_files( plugin_basename( $plugin[1] ) );
$expected = array(
$expected = array(
'list_files_test_plugin/list_files_test_plugin.php',
'list_files_test_plugin/subdir/subfile.php',
);
@@ -320,11 +320,11 @@ class Tests_Admin_includesPlugin extends WP_UnitTestCase {
public function test_validate_active_plugins_remove_invalid() {
$plugin = $this->_create_plugin();
activate_plugin( $plugin[ 0 ] );
unlink( $plugin[ 1 ] );
activate_plugin( $plugin[0] );
unlink( $plugin[1] );
$result = validate_active_plugins();
$this->assertTrue( isset( $result[ $plugin[ 0 ] ] ) );
$this->assertTrue( isset( $result[ $plugin[0] ] ) );
}
/**
@@ -347,7 +347,7 @@ class Tests_Admin_includesPlugin extends WP_UnitTestCase {
public function test_is_uninstallable_plugin_true() {
$plugin = $this->_create_plugin();
$uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
$uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
$uninstallable_plugins[ $plugin[0] ] = true;
update_option( 'uninstall_plugins', $uninstallable_plugins );
@@ -430,7 +430,7 @@ class Tests_Admin_includesPlugin extends WP_UnitTestCase {
* @access private
*/
private function _restore_mu_plugins() {
$mu_bu_dir = WP_CONTENT_DIR . '/mu-plugin-backup';
$mu_bu_dir = WP_CONTENT_DIR . '/mu-plugin-backup';
$files_to_move = array();
if ( is_dir( $mu_bu_dir ) && $mu_plugins = opendir( $mu_bu_dir ) ) {
while ( false !== $plugin = readdir( $mu_plugins ) ) {
@@ -464,7 +464,7 @@ class Tests_Admin_includesPlugin extends WP_UnitTestCase {
mkdir( $di_bu_dir );
}
foreach( _get_dropins() as $file_to_move => $v ) {
foreach ( _get_dropins() as $file_to_move => $v ) {
if ( file_exists( WP_CONTENT_DIR . '/' . $file_to_move ) ) {
rename( WP_CONTENT_DIR . '/' . $file_to_move, $di_bu_dir . '/' . $file_to_move );
}
@@ -481,7 +481,7 @@ class Tests_Admin_includesPlugin extends WP_UnitTestCase {
private function _restore_drop_ins() {
$di_bu_dir = WP_CONTENT_DIR . '/drop-ins-backup';
foreach( _get_dropins() as $file_to_move => $v ) {
foreach ( _get_dropins() as $file_to_move => $v ) {
if ( file_exists( $di_bu_dir . '/' . $file_to_move ) ) {
rename( $di_bu_dir . '/' . $file_to_move, WP_CONTENT_DIR . '/' . $file_to_move );
}
+250 -160
View File
@@ -26,9 +26,9 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
wp_set_current_user( self::$contributor_id );
// Create New Draft Post
$_post_data = array();
$_post_data = array();
$_post_data['post_author'] = self::$contributor_id;
$_post_data['post_type'] = 'post';
$_post_data['post_type'] = 'post';
$_post_data['saveasdraft'] = true;
$_results = _wp_translate_postdata( false, $_post_data );
@@ -37,10 +37,10 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
$this->assertEquals( 'draft', $_results['post_status'] );
// Submit Post for Approval
$_post_data = array();
$_post_data = array();
$_post_data['post_author'] = self::$contributor_id;
$_post_data['post_type'] = 'post';
$_post_data['publish'] = true;
$_post_data['post_type'] = 'post';
$_post_data['publish'] = true;
$_results = _wp_translate_postdata( false, $_post_data );
$this->assertNotInstanceOf( 'WP_Error', $_results );
@@ -48,9 +48,9 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
$this->assertEquals( 'pending', $_results['post_status'] );
// Create New Draft Post for another user
$_post_data = array();
$_post_data = array();
$_post_data['post_author'] = self::$editor_id;
$_post_data['post_type'] = 'post';
$_post_data['post_type'] = 'post';
$_post_data['saveasdraft'] = true;
$_results = _wp_translate_postdata( false, $_post_data );
@@ -59,10 +59,10 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
$this->assertEquals( 'Sorry, you are not allowed to create posts as this user.', $_results->get_error_message() );
// Edit Draft Post for another user
$_post_data = array();
$_post_data['post_ID'] = self::factory()->post->create( array( 'post_author' => self::$editor_id ) );
$_post_data = array();
$_post_data['post_ID'] = self::factory()->post->create( array( 'post_author' => self::$editor_id ) );
$_post_data['post_author'] = self::$editor_id;
$_post_data['post_type'] = 'post';
$_post_data['post_type'] = 'post';
$_post_data['post_status'] = 'draft';
$_post_data['saveasdraft'] = true;
@@ -76,9 +76,9 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
wp_set_current_user( self::$editor_id );
// Create New Draft Post
$_post_data = array();
$_post_data = array();
$_post_data['post_author'] = self::$editor_id;
$_post_data['post_type'] = 'post';
$_post_data['post_type'] = 'post';
$_post_data['saveasdraft'] = true;
$_results = _wp_translate_postdata( false, $_post_data );
@@ -87,10 +87,10 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
$this->assertEquals( 'draft', $_results['post_status'] );
// Publish Post
$_post_data = array();
$_post_data = array();
$_post_data['post_author'] = self::$editor_id;
$_post_data['post_type'] = 'post';
$_post_data['publish'] = true;
$_post_data['post_type'] = 'post';
$_post_data['publish'] = true;
$_results = _wp_translate_postdata( false, $_post_data );
$this->assertNotInstanceOf( 'WP_Error', $_results );
@@ -98,9 +98,9 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
$this->assertEquals( 'publish', $_results['post_status'] );
// Create New Draft Post for another user
$_post_data = array();
$_post_data = array();
$_post_data['post_author'] = self::$contributor_id;
$_post_data['post_type'] = 'post';
$_post_data['post_type'] = 'post';
$_post_data['saveasdraft'] = true;
$_results = _wp_translate_postdata( false, $_post_data );
@@ -109,10 +109,10 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
$this->assertEquals( 'draft', $_results['post_status'] );
// Edit Draft Post for another user
$_post_data = array();
$_post_data['post_ID'] = self::factory()->post->create( array( 'post_author' => self::$contributor_id ) );
$_post_data = array();
$_post_data['post_ID'] = self::factory()->post->create( array( 'post_author' => self::$contributor_id ) );
$_post_data['post_author'] = self::$contributor_id;
$_post_data['post_type'] = 'post';
$_post_data['post_type'] = 'post';
$_post_data['post_status'] = 'draft';
$_post_data['saveasdraft'] = true;
@@ -133,9 +133,9 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
$this->assertEquals( 'auto-draft', $post->post_status );
$post_data = array(
'post_title' => 'Post title',
'content' => 'Post content',
'post_type' => 'post',
'post_ID' => $post->ID,
'content' => 'Post content',
'post_type' => 'post',
'post_ID' => $post->ID,
);
edit_post( $post_data );
$this->assertEquals( 'draft', get_post( $post->ID )->post_status );
@@ -148,19 +148,23 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
wp_set_current_user( self::$editor_id );
register_taxonomy( 'wptests_tax', array( 'post' ) );
$t1 = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
'name' => 'foo',
'slug' => 'bar',
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
'name' => 'bar',
'slug' => 'foo',
) );
$t1 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'name' => 'foo',
'slug' => 'bar',
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'name' => 'bar',
'slug' => 'foo',
)
);
$post_data = array(
'post_ID' => self::$post_id,
'post_ID' => self::$post_id,
'tax_input' => array(
'wptests_tax' => 'foo,baz',
),
@@ -184,14 +188,16 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
wp_set_current_user( self::$editor_id );
register_taxonomy( 'wptests_tax', array( 'post' ) );
self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
'name' => 'foo',
'slug' => 'bar',
) );
self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'name' => 'foo',
'slug' => 'bar',
)
);
$post_data = array(
'post_ID' => self::$post_id,
'post_ID' => self::$post_id,
'tax_input' => array(
'wptests_tax' => ' ',
),
@@ -210,27 +216,31 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
public function test_bulk_edit_posts_stomping() {
wp_set_current_user( self::$admin_id );
$post1 = self::factory()->post->create( array(
'post_author' => self::$author_ids[0],
'comment_status' => 'open',
'ping_status' => 'open',
'post_status' => 'publish',
) );
$post1 = self::factory()->post->create(
array(
'post_author' => self::$author_ids[0],
'comment_status' => 'open',
'ping_status' => 'open',
'post_status' => 'publish',
)
);
$post2 = self::factory()->post->create( array(
'post_author' => self::$author_ids[1],
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_status' => 'draft',
) );
$post2 = self::factory()->post->create(
array(
'post_author' => self::$author_ids[1],
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_status' => 'draft',
)
);
$request = array(
'post_type' => 'post',
'post_author' => -1,
'ping_status' => -1,
'comment_status' => -1,
'_status' => -1,
'post' => array( $post1, $post2 ),
'post_type' => 'post',
'post_author' => -1,
'ping_status' => -1,
'comment_status' => -1,
'_status' => -1,
'post' => array( $post1, $post2 ),
);
bulk_edit_posts( $request );
@@ -311,9 +321,15 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
$this->set_permalink_structure( "/$permalink_structure/" );
$future_date = date( 'Y-m-d H:i:s', time() + 100 );
$p = self::factory()->post->create( array( 'post_status' => 'future', 'post_name' => 'foo', 'post_date' => $future_date ) );
$p = self::factory()->post->create(
array(
'post_status' => 'future',
'post_name' => 'foo',
'post_date' => $future_date,
)
);
$found = get_sample_permalink( $p );
$found = get_sample_permalink( $p );
$expected = trailingslashit( home_url( $permalink_structure ) );
$this->assertSame( $expected, $found[0] );
@@ -327,7 +343,13 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
wp_set_current_user( self::$admin_id );
$future_date = date( 'Y-m-d H:i:s', time() + 100 );
$p = self::factory()->post->create( array( 'post_status' => 'future', 'post_name' => 'foo', 'post_date' => $future_date ) );
$p = self::factory()->post->create(
array(
'post_status' => 'future',
'post_name' => 'foo',
'post_date' => $future_date,
)
);
$found = get_sample_permalink_html( $p );
$this->assertContains( 'href="' . get_option( 'home' ) . '/?p=' . $p . '"', $found );
@@ -344,11 +366,17 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
wp_set_current_user( self::$admin_id );
$future_date = date( 'Y-m-d H:i:s', time() + 100 );
$p = self::factory()->post->create( array( 'post_status' => 'future', 'post_name' => 'foo-صورة', 'post_date' => $future_date ) );
$p = self::factory()->post->create(
array(
'post_status' => 'future',
'post_name' => 'foo-صورة',
'post_date' => $future_date,
)
);
$found = get_sample_permalink_html( $p );
$post = get_post( $p );
$this->assertContains( 'href="' . get_option( 'home' ) . "/" . $post->post_name . '/"', $found );
$post = get_post( $p );
$this->assertContains( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found );
$this->assertContains( '>' . urldecode( $post->post_name ) . '<', $found );
}
@@ -360,16 +388,18 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
wp_set_current_user( self::$admin_id );
$p = self::factory()->attachment->create_object( 'صورة.jpg', 0, array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
'post_title' => 'صورة',
'post_status' => 'inherit',
) );
$p = self::factory()->attachment->create_object(
'صورة.jpg', 0, array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
'post_title' => 'صورة',
'post_status' => 'inherit',
)
);
$found = get_sample_permalink_html( $p );
$post = get_post( $p );
$this->assertContains( 'href="' . get_option( 'home' ) . "/" . $post->post_name . '/"', $found );
$post = get_post( $p );
$this->assertContains( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found );
$this->assertContains( '>' . urldecode( get_permalink( $post ) ) . '<', $found );
}
@@ -383,29 +413,45 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
wp_set_current_user( self::$admin_id );
// Published posts should use published permalink
$p = self::factory()->post->create( array( 'post_status' => 'publish', 'post_name' => 'foo-صورة' ) );
$p = self::factory()->post->create(
array(
'post_status' => 'publish',
'post_name' => 'foo-صورة',
)
);
$found = get_sample_permalink_html( $p, null, 'new_slug-صورة' );
$post = get_post( $p );
$found = get_sample_permalink_html( $p, null, 'new_slug-صورة' );
$post = get_post( $p );
$message = 'Published post';
$this->assertContains( 'href="' . get_option( 'home' ) . "/" . $post->post_name . '/"', $found, $message );
$this->assertContains( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found, $message );
$this->assertContains( '>new_slug-صورة<', $found, $message );
// Scheduled posts should use published permalink
$future_date = date( 'Y-m-d H:i:s', time() + 100 );
$p = self::factory()->post->create( array( 'post_status' => 'future', 'post_name' => 'bar-صورة', 'post_date' => $future_date ) );
$p = self::factory()->post->create(
array(
'post_status' => 'future',
'post_name' => 'bar-صورة',
'post_date' => $future_date,
)
);
$found = get_sample_permalink_html( $p, null, 'new_slug-صورة' );
$post = get_post( $p );
$found = get_sample_permalink_html( $p, null, 'new_slug-صورة' );
$post = get_post( $p );
$message = 'Scheduled post';
$this->assertContains( 'href="' . get_option( 'home' ) . "/" . $post->post_name . '/"', $found, $message );
$this->assertContains( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found, $message );
$this->assertContains( '>new_slug-صورة<', $found, $message );
// Draft posts should use preview link
$p = self::factory()->post->create( array( 'post_status' => 'draft', 'post_name' => 'baz-صورة' ) );
$p = self::factory()->post->create(
array(
'post_status' => 'draft',
'post_name' => 'baz-صورة',
)
);
$found = get_sample_permalink_html( $p, null, 'new_slug-صورة' );
$post = get_post( $p );
$found = get_sample_permalink_html( $p, null, 'new_slug-صورة' );
$post = get_post( $p );
$message = 'Draft post';
$preview_link = get_permalink( $post->ID );
@@ -425,10 +471,16 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
wp_set_current_user( self::$admin_id );
$future_date = date( 'Y-m-d H:i:s', time() + 100 );
$p = self::factory()->post->create( array( 'post_status' => 'pending', 'post_name' => '', 'post_date' => $future_date ) );
$p = self::factory()->post->create(
array(
'post_status' => 'pending',
'post_name' => '',
'post_date' => $future_date,
)
);
$found = get_sample_permalink_html( $p );
$post = get_post( $p );
$post = get_post( $p );
$this->assertContains( 'href="' . esc_url( get_preview_post_link( $post ) ), $found );
}
@@ -438,9 +490,11 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
public function test_get_sample_permalink_should_avoid_slugs_that_would_create_clashes_with_year_archives() {
$this->set_permalink_structure( '/%postname%/' );
$p = self::factory()->post->create( array(
'post_name' => '2015',
) );
$p = self::factory()->post->create(
array(
'post_name' => '2015',
)
);
$found = get_sample_permalink( $p );
$this->assertEquals( '2015-2', $found[1] );
@@ -452,9 +506,11 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
public function test_get_sample_permalink_should_allow_yearlike_slugs_if_permastruct_does_not_cause_an_archive_conflict() {
$this->set_permalink_structure( '/%year%/%postname%/' );
$p = self::factory()->post->create( array(
'post_name' => '2015',
) );
$p = self::factory()->post->create(
array(
'post_name' => '2015',
)
);
$found = get_sample_permalink( $p );
$this->assertEquals( '2015', $found[1] );
@@ -466,9 +522,11 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
public function test_get_sample_permalink_should_avoid_slugs_that_would_create_clashes_with_month_archives() {
$this->set_permalink_structure( '/%year%/%postname%/' );
$p = self::factory()->post->create( array(
'post_name' => '11',
) );
$p = self::factory()->post->create(
array(
'post_name' => '11',
)
);
$found = get_sample_permalink( $p );
$this->assertEquals( '11-2', $found[1] );
@@ -480,9 +538,11 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
public function test_get_sample_permalink_should_ignore_potential_month_conflicts_for_invalid_monthnum() {
$this->set_permalink_structure( '/%year%/%postname%/' );
$p = self::factory()->post->create( array(
'post_name' => '13',
) );
$p = self::factory()->post->create(
array(
'post_name' => '13',
)
);
$found = get_sample_permalink( $p );
$this->assertEquals( '13', $found[1] );
@@ -494,9 +554,11 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
public function test_get_sample_permalink_should_avoid_slugs_that_would_create_clashes_with_day_archives() {
$this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
$p = self::factory()->post->create( array(
'post_name' => '30',
) );
$p = self::factory()->post->create(
array(
'post_name' => '30',
)
);
$found = get_sample_permalink( $p );
$this->assertEquals( '30-2', $found[1] );
@@ -508,13 +570,17 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
public function test_get_sample_permalink_should_iterate_slug_suffix_when_a_date_conflict_is_found() {
$this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
self::factory()->post->create( array(
'post_name' => '30-2',
) );
self::factory()->post->create(
array(
'post_name' => '30-2',
)
);
$p = self::factory()->post->create( array(
'post_name' => '30',
) );
$p = self::factory()->post->create(
array(
'post_name' => '30',
)
);
$found = get_sample_permalink( $p );
$this->assertEquals( '30-3', $found[1] );
@@ -526,9 +592,11 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
public function test_get_sample_permalink_should_ignore_potential_day_conflicts_for_invalid_day() {
$this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
$p = self::factory()->post->create( array(
'post_name' => '32',
) );
$p = self::factory()->post->create(
array(
'post_name' => '32',
)
);
$found = get_sample_permalink( $p );
$this->assertEquals( '32', $found[1] );
@@ -540,9 +608,11 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
public function test_get_sample_permalink_should_allow_daylike_slugs_if_permastruct_does_not_cause_an_archive_conflict() {
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
$p = self::factory()->post->create( array(
'post_name' => '30',
) );
$p = self::factory()->post->create(
array(
'post_name' => '30',
)
);
$found = get_sample_permalink( $p );
$this->assertEquals( '30', $found[1] );
@@ -554,17 +624,21 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
public function test_get_sample_permalink_should_respect_hierarchy_of_draft_pages() {
$this->set_permalink_structure( '/%postname%/' );
$parent = self::factory()->post->create( array(
'post_type' => 'page',
'post_title' => 'Parent Page',
) );
$parent = self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'Parent Page',
)
);
$child = self::factory()->post->create( array(
'post_type' => 'page',
'post_title' => 'Child Page',
'post_parent' => $parent,
'post_status' => 'draft',
) );
$child = self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'Child Page',
'post_parent' => $parent,
'post_status' => 'draft',
)
);
$actual = get_sample_permalink( $child );
$this->assertSame( home_url() . '/parent-page/%pagename%/', $actual[0] );
@@ -572,28 +646,34 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
}
public function test_post_exists_should_match_title() {
$p = self::factory()->post->create( array(
'post_title' => 'Foo Bar',
) );
$p = self::factory()->post->create(
array(
'post_title' => 'Foo Bar',
)
);
$this->assertSame( $p, post_exists( 'Foo Bar' ) );
}
public function test_post_exists_should_not_match_nonexistent_title() {
$p = self::factory()->post->create( array(
'post_title' => 'Foo Bar',
) );
$p = self::factory()->post->create(
array(
'post_title' => 'Foo Bar',
)
);
$this->assertSame( 0, post_exists( 'Foo Bar Baz' ) );
}
public function test_post_exists_should_match_nonempty_content() {
$title = 'Foo Bar';
$title = 'Foo Bar';
$content = 'Foo Bar Baz';
$p = self::factory()->post->create( array(
'post_title' => $title,
'post_content' => $content,
) );
$p = self::factory()->post->create(
array(
'post_title' => $title,
'post_content' => $content,
)
);
$this->assertSame( $p, post_exists( $title, $content ) );
}
@@ -602,58 +682,68 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase {
* @ticket 35246
*/
public function test_post_exists_should_match_content_with_no_title() {
$title = '';
$title = '';
$content = 'Foo Bar Baz';
$p = self::factory()->post->create( array(
'post_title' => $title,
'post_content' => $content,
) );
$p = self::factory()->post->create(
array(
'post_title' => $title,
'post_content' => $content,
)
);
$this->assertSame( $p, post_exists( $title, $content ) );
}
public function test_post_exists_should_not_match_when_nonempty_content_doesnt_match() {
$title = 'Foo Bar';
$title = 'Foo Bar';
$content = 'Foo Bar Baz';
$p = self::factory()->post->create( array(
'post_title' => $title,
'post_content' => $content . ' Quz',
) );
$p = self::factory()->post->create(
array(
'post_title' => $title,
'post_content' => $content . ' Quz',
)
);
$this->assertSame( 0, post_exists( $title, $content ) );
}
public function test_post_exists_should_match_nonempty_date() {
$title = 'Foo Bar';
$date = '2014-05-08 12:00:00';
$p = self::factory()->post->create( array(
'post_title' => $title,
'post_date' => $date,
) );
$date = '2014-05-08 12:00:00';
$p = self::factory()->post->create(
array(
'post_title' => $title,
'post_date' => $date,
)
);
$this->assertSame( $p, post_exists( $title, '', $date ) );
}
public function test_post_exists_should_not_match_when_nonempty_date_doesnt_match() {
$title = 'Foo Bar';
$date = '2014-05-08 12:00:00';
$p = self::factory()->post->create( array(
'post_title' => $title,
'post_date' => '2015-10-10 00:00:00',
) );
$date = '2014-05-08 12:00:00';
$p = self::factory()->post->create(
array(
'post_title' => $title,
'post_date' => '2015-10-10 00:00:00',
)
);
$this->assertSame( 0, post_exists( $title, '', $date ) );
}
public function test_post_exists_should_match_nonempty_title_content_and_date() {
$title = 'Foo Bar';
$title = 'Foo Bar';
$content = 'Foo Bar Baz';
$date = '2014-05-08 12:00:00';
$p = self::factory()->post->create( array(
'post_title' => $title,
'post_content' => $content,
'post_date' => $date,
) );
$date = '2014-05-08 12:00:00';
$p = self::factory()->post->create(
array(
'post_title' => $title,
'post_content' => $content,
'post_date' => $date,
)
);
$this->assertSame( $p, post_exists( $title, $content, $date ) );
}
+157 -56
View File
@@ -6,31 +6,122 @@
*/
class Tests_Admin_includesScreen extends WP_UnitTestCase {
var $core_screens = array(
'index.php' => array( 'base' => 'dashboard', 'id' => 'dashboard' ),
'edit.php' => array( 'base' => 'edit', 'id' => 'edit-post', 'post_type' => 'post' ),
'post-new.php'=> array( 'action' => 'add', 'base' => 'post', 'id' => 'post', 'post_type' => 'post' ),
'edit-tags.php' => array( 'base' => 'edit-tags', 'id' => 'edit-post_tag', 'post_type' => 'post', 'taxonomy' => 'post_tag' ),
'edit-tags.php?taxonomy=post_tag' => array( 'base' => 'edit-tags', 'id' => 'edit-post_tag', 'post_type' => 'post', 'taxonomy' => 'post_tag' ),
'edit-tags.php?taxonomy=category' => array( 'base' => 'edit-tags', 'id' => 'edit-category', 'post_type' => 'post', 'taxonomy' => 'category' ),
'upload.php' => array( 'base' => 'upload', 'id' => 'upload', 'post_type' => 'attachment' ),
'media-new.php' => array( 'action' => 'add', 'base' => 'media', 'id' => 'media' ),
'edit.php?post_type=page' => array( 'base' => 'edit', 'id' => 'edit-page', 'post_type' => 'page' ),
'link-manager.php' => array( 'base' => 'link-manager', 'id' => 'link-manager' ),
'link-add.php' => array( 'action' => 'add', 'base' => 'link', 'id' => 'link' ),
'edit-tags.php?taxonomy=link_category' => array( 'base' => 'edit-tags', 'id' => 'edit-link_category', 'taxonomy' => 'link_category', 'post_type' => '' ),
'edit-comments.php' => array( 'base' => 'edit-comments', 'id' => 'edit-comments' ),
'themes.php' => array( 'base' => 'themes', 'id' => 'themes' ),
'widgets.php' => array( 'base' => 'widgets', 'id' => 'widgets' ),
'nav-menus.php' => array( 'base' => 'nav-menus', 'id' => 'nav-menus' ),
'plugins.php' => array( 'base' => 'plugins', 'id' => 'plugins' ),
'users.php' => array( 'base' => 'users', 'id' => 'users' ),
'user-new.php' => array( 'action' => 'add', 'base' => 'user', 'id' => 'user' ),
'profile.php' => array( 'base' => 'profile', 'id' => 'profile' ),
'tools.php' => array( 'base' => 'tools', 'id' => 'tools' ),
'import.php' => array( 'base' => 'import', 'id' => 'import' ),
'export.php' => array( 'base' => 'export', 'id' => 'export' ),
'options-general.php' => array( 'base' => 'options-general', 'id' => 'options-general' ),
'options-writing.php' => array( 'base' => 'options-writing', 'id' => 'options-writing' ),
'index.php' => array(
'base' => 'dashboard',
'id' => 'dashboard',
),
'edit.php' => array(
'base' => 'edit',
'id' => 'edit-post',
'post_type' => 'post',
),
'post-new.php' => array(
'action' => 'add',
'base' => 'post',
'id' => 'post',
'post_type' => 'post',
),
'edit-tags.php' => array(
'base' => 'edit-tags',
'id' => 'edit-post_tag',
'post_type' => 'post',
'taxonomy' => 'post_tag',
),
'edit-tags.php?taxonomy=post_tag' => array(
'base' => 'edit-tags',
'id' => 'edit-post_tag',
'post_type' => 'post',
'taxonomy' => 'post_tag',
),
'edit-tags.php?taxonomy=category' => array(
'base' => 'edit-tags',
'id' => 'edit-category',
'post_type' => 'post',
'taxonomy' => 'category',
),
'upload.php' => array(
'base' => 'upload',
'id' => 'upload',
'post_type' => 'attachment',
),
'media-new.php' => array(
'action' => 'add',
'base' => 'media',
'id' => 'media',
),
'edit.php?post_type=page' => array(
'base' => 'edit',
'id' => 'edit-page',
'post_type' => 'page',
),
'link-manager.php' => array(
'base' => 'link-manager',
'id' => 'link-manager',
),
'link-add.php' => array(
'action' => 'add',
'base' => 'link',
'id' => 'link',
),
'edit-tags.php?taxonomy=link_category' => array(
'base' => 'edit-tags',
'id' => 'edit-link_category',
'taxonomy' => 'link_category',
'post_type' => '',
),
'edit-comments.php' => array(
'base' => 'edit-comments',
'id' => 'edit-comments',
),
'themes.php' => array(
'base' => 'themes',
'id' => 'themes',
),
'widgets.php' => array(
'base' => 'widgets',
'id' => 'widgets',
),
'nav-menus.php' => array(
'base' => 'nav-menus',
'id' => 'nav-menus',
),
'plugins.php' => array(
'base' => 'plugins',
'id' => 'plugins',
),
'users.php' => array(
'base' => 'users',
'id' => 'users',
),
'user-new.php' => array(
'action' => 'add',
'base' => 'user',
'id' => 'user',
),
'profile.php' => array(
'base' => 'profile',
'id' => 'profile',
),
'tools.php' => array(
'base' => 'tools',
'id' => 'tools',
),
'import.php' => array(
'base' => 'import',
'id' => 'import',
),
'export.php' => array(
'base' => 'export',
'id' => 'export',
),
'options-general.php' => array(
'base' => 'options-general',
'id' => 'options-general',
),
'options-writing.php' => array(
'base' => 'options-writing',
'id' => 'options-writing',
),
);
function setUp() {
@@ -47,19 +138,21 @@ class Tests_Admin_includesScreen extends WP_UnitTestCase {
global $current_screen;
foreach ( $this->core_screens as $hook_name => $screen ) {
$_GET = $_POST = $_REQUEST = array();
$_GET = $_POST = $_REQUEST = array();
$GLOBALS['taxnow'] = $GLOBALS['typenow'] = '';
$screen = (object) $screen;
$hook = parse_url( $hook_name );
$screen = (object) $screen;
$hook = parse_url( $hook_name );
if ( ! empty( $hook['query'] ) ) {
$args = wp_parse_args( $hook['query'] );
if ( isset( $args['taxonomy'] ) )
if ( isset( $args['taxonomy'] ) ) {
$GLOBALS['taxnow'] = $_GET['taxonomy'] = $_POST['taxonomy'] = $_REQUEST['taxonomy'] = $args['taxonomy'];
if ( isset( $args['post_type'] ) )
}
if ( isset( $args['post_type'] ) ) {
$GLOBALS['typenow'] = $_GET['post_type'] = $_POST['post_type'] = $_REQUEST['post_type'] = $args['post_type'];
else if ( isset( $screen->post_type ) )
} elseif ( isset( $screen->post_type ) ) {
$GLOBALS['typenow'] = $_GET['post_type'] = $_POST['post_type'] = $_REQUEST['post_type'] = $screen->post_type;
}
}
$GLOBALS['hook_suffix'] = $hook['path'];
@@ -67,14 +160,17 @@ class Tests_Admin_includesScreen extends WP_UnitTestCase {
$this->assertEquals( $screen->id, $current_screen->id, $hook_name );
$this->assertEquals( $screen->base, $current_screen->base, $hook_name );
if ( isset( $screen->action ) )
if ( isset( $screen->action ) ) {
$this->assertEquals( $screen->action, $current_screen->action, $hook_name );
if ( isset( $screen->post_type ) )
}
if ( isset( $screen->post_type ) ) {
$this->assertEquals( $screen->post_type, $current_screen->post_type, $hook_name );
else
} else {
$this->assertEmpty( $current_screen->post_type, $hook_name );
if ( isset( $screen->taxonomy ) )
}
if ( isset( $screen->taxonomy ) ) {
$this->assertEquals( $screen->taxonomy, $current_screen->taxonomy, $hook_name );
}
$this->assertTrue( $current_screen->in_admin() );
$this->assertTrue( $current_screen->in_admin( 'site' ) );
@@ -87,8 +183,9 @@ class Tests_Admin_includesScreen extends WP_UnitTestCase {
// With convert_to_screen(), the hook_suffix should return the exact $current_screen.
// But, convert_to_screen() cannot figure out ?taxonomy and ?post_type.
if ( empty( $hook['query'] ) )
if ( empty( $hook['query'] ) ) {
$this->assertSame( $current_screen, convert_to_screen( $GLOBALS['hook_suffix'] ), $hook_name );
}
}
}
@@ -154,23 +251,25 @@ class Tests_Admin_includesScreen extends WP_UnitTestCase {
}
function test_help_tabs() {
$tab = __FUNCTION__;
$tab = __FUNCTION__;
$tab_args = array(
'id' => $tab,
'title' => 'Help!',
'content' => 'Some content',
'id' => $tab,
'title' => 'Help!',
'content' => 'Some content',
'callback' => false,
);
$screen = get_current_screen();
$screen->add_help_tab( $tab_args );
$this->assertEquals( $screen->get_help_tab( $tab ), array(
'id' => $tab,
'title' => 'Help!',
'content' => 'Some content',
'callback' => false,
'priority' => 10,
) );
$this->assertEquals(
$screen->get_help_tab( $tab ), array(
'id' => $tab,
'title' => 'Help!',
'content' => 'Some content',
'callback' => false,
'priority' => 10,
)
);
$tabs = $screen->get_help_tabs();
$this->assertArrayHasKey( $tab, $tabs );
@@ -235,7 +334,7 @@ class Tests_Admin_includesScreen extends WP_UnitTestCase {
$screen->add_help_tab( $tab_4_args );
// Priority is added with the default for future calls
$tab_4_args[ 'priority' ] = 10;
$tab_4_args['priority'] = 10;
$this->assertEquals( $screen->get_help_tab( $tab_4 ), $tab_4_args );
$tabs = $screen->get_help_tabs();
@@ -247,12 +346,14 @@ class Tests_Admin_includesScreen extends WP_UnitTestCase {
// Test priority order.
$this->assertSame( array(
$tab_2 => $tab_2_args,
$tab_1 => $tab_1_args,
$tab_4 => $tab_4_args,
$tab_3 => $tab_3_args,
), $tabs );
$this->assertSame(
array(
$tab_2 => $tab_2_args,
$tab_1 => $tab_1_args,
$tab_4 => $tab_4_args,
$tab_3 => $tab_3_args,
), $tabs
);
$screen->remove_help_tab( $tab_1 );
$this->assertNull( $screen->get_help_tab( $tab_1 ) );
@@ -278,11 +379,11 @@ class Tests_Admin_includesScreen extends WP_UnitTestCase {
* @ticket 25799
*/
function test_options() {
$option = __FUNCTION__;
$option = __FUNCTION__;
$option_args = array(
'label' => 'Option',
'default' => 10,
'option' => $option
'option' => $option,
);
$screen = get_current_screen();
+29 -29
View File
@@ -4,53 +4,53 @@
*/
class Tests_Admin_includesTemplate extends WP_UnitTestCase {
function test_equal() {
$this->assertEquals(' selected=\'selected\'', selected('foo','foo',false));
$this->assertEquals(' checked=\'checked\'', checked('foo','foo',false));
$this->assertEquals( ' selected=\'selected\'', selected( 'foo', 'foo', false ) );
$this->assertEquals( ' checked=\'checked\'', checked( 'foo', 'foo', false ) );
$this->assertEquals(' selected=\'selected\'', selected('1',1,false));
$this->assertEquals(' checked=\'checked\'', checked('1',1,false));
$this->assertEquals( ' selected=\'selected\'', selected( '1', 1, false ) );
$this->assertEquals( ' checked=\'checked\'', checked( '1', 1, false ) );
$this->assertEquals(' selected=\'selected\'', selected('1',true,false));
$this->assertEquals(' checked=\'checked\'', checked('1',true,false));
$this->assertEquals( ' selected=\'selected\'', selected( '1', true, false ) );
$this->assertEquals( ' checked=\'checked\'', checked( '1', true, false ) );
$this->assertEquals(' selected=\'selected\'', selected(1,1,false));
$this->assertEquals(' checked=\'checked\'', checked(1,1,false));
$this->assertEquals( ' selected=\'selected\'', selected( 1, 1, false ) );
$this->assertEquals( ' checked=\'checked\'', checked( 1, 1, false ) );
$this->assertEquals(' selected=\'selected\'', selected(1,true,false));
$this->assertEquals(' checked=\'checked\'', checked(1,true,false));
$this->assertEquals( ' selected=\'selected\'', selected( 1, true, false ) );
$this->assertEquals( ' checked=\'checked\'', checked( 1, true, false ) );
$this->assertEquals(' selected=\'selected\'', selected(true,true,false));
$this->assertEquals(' checked=\'checked\'', checked(true,true,false));
$this->assertEquals( ' selected=\'selected\'', selected( true, true, false ) );
$this->assertEquals( ' checked=\'checked\'', checked( true, true, false ) );
$this->assertEquals(' selected=\'selected\'', selected('0',0,false));
$this->assertEquals(' checked=\'checked\'', checked('0',0,false));
$this->assertEquals( ' selected=\'selected\'', selected( '0', 0, false ) );
$this->assertEquals( ' checked=\'checked\'', checked( '0', 0, false ) );
$this->assertEquals(' selected=\'selected\'', selected(0,0,false));
$this->assertEquals(' checked=\'checked\'', checked(0,0,false));
$this->assertEquals( ' selected=\'selected\'', selected( 0, 0, false ) );
$this->assertEquals( ' checked=\'checked\'', checked( 0, 0, false ) );
$this->assertEquals(' selected=\'selected\'', selected('',false,false));
$this->assertEquals(' checked=\'checked\'', checked('',false,false));
$this->assertEquals( ' selected=\'selected\'', selected( '', false, false ) );
$this->assertEquals( ' checked=\'checked\'', checked( '', false, false ) );
$this->assertEquals(' selected=\'selected\'', selected(false,false,false));
$this->assertEquals(' checked=\'checked\'', checked(false,false,false));
$this->assertEquals( ' selected=\'selected\'', selected( false, false, false ) );
$this->assertEquals( ' checked=\'checked\'', checked( false, false, false ) );
}
function test_notequal() {
$this->assertEquals('', selected('0','',false));
$this->assertEquals('', checked('0','',false));
$this->assertEquals( '', selected( '0', '', false ) );
$this->assertEquals( '', checked( '0', '', false ) );
$this->assertEquals('', selected(0,'',false));
$this->assertEquals('', checked(0,'',false));
$this->assertEquals( '', selected( 0, '', false ) );
$this->assertEquals( '', checked( 0, '', false ) );
$this->assertEquals('', selected(0,false,false));
$this->assertEquals('', checked(0,false,false));
$this->assertEquals( '', selected( 0, false, false ) );
$this->assertEquals( '', checked( 0, false, false ) );
}
public function test_add_meta_box() {
global $wp_meta_boxes;
add_meta_box( 'testbox1', 'Test Metabox', '__return_false', 'post' );
$this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['post']['advanced']['default'] );
}
@@ -79,7 +79,7 @@ class Tests_Admin_includesTemplate extends WP_UnitTestCase {
// Add a meta box to three different post types
add_meta_box( 'testbox1', 'Test Metabox', '__return_false', array( 'post', 'comment', 'attachment' ) );
$this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['post']['advanced']['default'] );
$this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['post']['advanced']['default'] );
$this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['comment']['advanced']['default'] );
$this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['attachment']['advanced']['default'] );
}
@@ -108,4 +108,4 @@ class Tests_Admin_includesTemplate extends WP_UnitTestCase {
$this->assertFalse( $wp_meta_boxes['attachment']['advanced']['default']['testbox1'] );
}
}
}
+95 -71
View File
@@ -8,12 +8,12 @@ class Tests_Admin_includesTheme extends WP_UnitTestCase {
parent::setUp();
$this->theme_root = DIR_TESTDATA . '/themedir1';
$this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
$this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
$GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
add_filter('theme_root', array($this, '_theme_root'));
add_filter( 'stylesheet_root', array($this, '_theme_root') );
add_filter( 'template_root', array($this, '_theme_root') );
add_filter( 'theme_root', array( $this, '_theme_root' ) );
add_filter( 'stylesheet_root', array( $this, '_theme_root' ) );
add_filter( 'template_root', array( $this, '_theme_root' ) );
// clear caches
wp_clean_themes_cache();
@@ -22,9 +22,9 @@ class Tests_Admin_includesTheme extends WP_UnitTestCase {
function tearDown() {
$GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
remove_filter('theme_root', array($this, '_theme_root'));
remove_filter( 'stylesheet_root', array($this, '_theme_root') );
remove_filter( 'template_root', array($this, '_theme_root') );
remove_filter( 'theme_root', array( $this, '_theme_root' ) );
remove_filter( 'stylesheet_root', array( $this, '_theme_root' ) );
remove_filter( 'template_root', array( $this, '_theme_root' ) );
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
@@ -32,7 +32,7 @@ class Tests_Admin_includesTheme extends WP_UnitTestCase {
}
// replace the normal theme root dir with our premade test dir
function _theme_root($dir) {
function _theme_root( $dir ) {
return $this->theme_root;
}
@@ -48,22 +48,26 @@ class Tests_Admin_includesTheme extends WP_UnitTestCase {
switch_theme( $theme['Template'], $theme['Stylesheet'] );
$this->assertEqualSetsWithIndex( array(
'Top Level' => 'template-top-level.php',
'Sub Dir' => 'subdir/template-sub-dir.php',
'This Template Header Is On One Line' => 'template-header.php',
), get_page_templates() );
$this->assertEqualSetsWithIndex(
array(
'Top Level' => 'template-top-level.php',
'Sub Dir' => 'subdir/template-sub-dir.php',
'This Template Header Is On One Line' => 'template-header.php',
), get_page_templates()
);
$theme = wp_get_theme( 'page-templates' );
$this->assertNotEmpty( $theme );
switch_theme( $theme['Template'], $theme['Stylesheet'] );
$this->assertEqualSetsWithIndex( array(
'Top Level' => 'template-top-level.php',
'Sub Dir' => 'subdir/template-sub-dir.php',
'This Template Header Is On One Line' => 'template-header.php',
), get_page_templates() );
$this->assertEqualSetsWithIndex(
array(
'Top Level' => 'template-top-level.php',
'Sub Dir' => 'subdir/template-sub-dir.php',
'This Template Header Is On One Line' => 'template-header.php',
), get_page_templates()
);
}
/**
@@ -75,14 +79,18 @@ class Tests_Admin_includesTheme extends WP_UnitTestCase {
switch_theme( $theme['Template'], $theme['Stylesheet'] );
$this->assertEqualSetsWithIndex( array(
'Top Level' => 'template-top-level-post-types.php',
'Sub Dir' => 'subdir/template-sub-dir-post-types.php',
), get_page_templates( null, 'foo' ) );
$this->assertEqualSetsWithIndex( array(
'Top Level' => 'template-top-level-post-types.php',
'Sub Dir' => 'subdir/template-sub-dir-post-types.php',
), get_page_templates( null, 'post' ) );
$this->assertEqualSetsWithIndex(
array(
'Top Level' => 'template-top-level-post-types.php',
'Sub Dir' => 'subdir/template-sub-dir-post-types.php',
), get_page_templates( null, 'foo' )
);
$this->assertEqualSetsWithIndex(
array(
'Top Level' => 'template-top-level-post-types.php',
'Sub Dir' => 'subdir/template-sub-dir-post-types.php',
), get_page_templates( null, 'post' )
);
$this->assertEquals( array(), get_page_templates( null, 'bar' ) );
}
@@ -95,21 +103,25 @@ class Tests_Admin_includesTheme extends WP_UnitTestCase {
switch_theme( $theme['Template'], $theme['Stylesheet'] );
$this->assertEqualSetsWithIndex( array(
'No Trailing Period' => '38766/no-trailing-period-post-types.php',
'Trailing Period.' => '38766/trailing-period-post-types.php',
'Trailing Comma,' => '38766/trailing-comma-post-types.php',
'Trailing Period, White Space.' => '38766/trailing-period-whitespace-post-types.php',
'Trailing White Space, Period.' => '38766/trailing-whitespace-period-post-types.php',
'Tilde in Post Type.' => '38766/tilde-post-types.php',
), get_page_templates( null, 'period' ) );
$this->assertEqualSetsWithIndex( array(
'No Trailing Period' => '38766/no-trailing-period-post-types.php',
'Trailing Period.' => '38766/trailing-period-post-types.php',
'Trailing Comma,' => '38766/trailing-comma-post-types.php',
'Trailing Period, White Space.' => '38766/trailing-period-whitespace-post-types.php',
'Trailing White Space, Period.' => '38766/trailing-whitespace-period-post-types.php',
), get_page_templates( null, 'full-stop' ) );
$this->assertEqualSetsWithIndex(
array(
'No Trailing Period' => '38766/no-trailing-period-post-types.php',
'Trailing Period.' => '38766/trailing-period-post-types.php',
'Trailing Comma,' => '38766/trailing-comma-post-types.php',
'Trailing Period, White Space.' => '38766/trailing-period-whitespace-post-types.php',
'Trailing White Space, Period.' => '38766/trailing-whitespace-period-post-types.php',
'Tilde in Post Type.' => '38766/tilde-post-types.php',
), get_page_templates( null, 'period' )
);
$this->assertEqualSetsWithIndex(
array(
'No Trailing Period' => '38766/no-trailing-period-post-types.php',
'Trailing Period.' => '38766/trailing-period-post-types.php',
'Trailing Comma,' => '38766/trailing-comma-post-types.php',
'Trailing Period, White Space.' => '38766/trailing-period-whitespace-post-types.php',
'Trailing White Space, Period.' => '38766/trailing-whitespace-period-post-types.php',
), get_page_templates( null, 'full-stop' )
);
}
/**
@@ -121,23 +133,29 @@ class Tests_Admin_includesTheme extends WP_UnitTestCase {
switch_theme( $theme['Template'], $theme['Stylesheet'] );
$this->assertEqualSetsWithIndex( array(
'Top Level' => 'template-top-level-post-types.php',
'Sub Dir' => 'subdir/template-sub-dir-post-types.php',
'Top Level In A Child Theme' => 'template-top-level-post-types-child.php',
'Sub Dir In A Child Theme' => 'subdir/template-sub-dir-post-types-child.php',
), get_page_templates( null, 'foo' ) );
$this->assertEqualSetsWithIndex(
array(
'Top Level' => 'template-top-level-post-types.php',
'Sub Dir' => 'subdir/template-sub-dir-post-types.php',
'Top Level In A Child Theme' => 'template-top-level-post-types-child.php',
'Sub Dir In A Child Theme' => 'subdir/template-sub-dir-post-types-child.php',
), get_page_templates( null, 'foo' )
);
$this->assertEqualSetsWithIndex( array(
'Top Level' => 'template-top-level-post-types.php',
'Sub Dir' => 'subdir/template-sub-dir-post-types.php',
), get_page_templates( null, 'post' ) );
$this->assertEqualSetsWithIndex(
array(
'Top Level' => 'template-top-level-post-types.php',
'Sub Dir' => 'subdir/template-sub-dir-post-types.php',
), get_page_templates( null, 'post' )
);
$this->assertEqualSetsWithIndex( array(
'Top Level' => 'template-top-level.php',
'Sub Dir' => 'subdir/template-sub-dir.php',
'This Template Header Is On One Line' => 'template-header.php',
), get_page_templates() );
$this->assertEqualSetsWithIndex(
array(
'Top Level' => 'template-top-level.php',
'Sub Dir' => 'subdir/template-sub-dir.php',
'This Template Header Is On One Line' => 'template-header.php',
), get_page_templates()
);
$this->assertEquals( array(), get_page_templates( null, 'bar' ) );
}
@@ -153,23 +171,29 @@ class Tests_Admin_includesTheme extends WP_UnitTestCase {
$post_templates = $theme->get_post_templates();
$this->assertEqualSetsWithIndex( array(
'template-top-level-post-types.php' => 'Top Level',
'subdir/template-sub-dir-post-types.php' => 'Sub Dir',
'template-top-level-post-types-child.php' => 'Top Level In A Child Theme',
'subdir/template-sub-dir-post-types-child.php' => 'Sub Dir In A Child Theme',
), $post_templates['foo'] );
$this->assertEqualSetsWithIndex(
array(
'template-top-level-post-types.php' => 'Top Level',
'subdir/template-sub-dir-post-types.php' => 'Sub Dir',
'template-top-level-post-types-child.php' => 'Top Level In A Child Theme',
'subdir/template-sub-dir-post-types-child.php' => 'Sub Dir In A Child Theme',
), $post_templates['foo']
);
$this->assertEqualSetsWithIndex( array(
'template-top-level-post-types.php' => 'Top Level',
'subdir/template-sub-dir-post-types.php' => 'Sub Dir',
), $post_templates['post'] );
$this->assertEqualSetsWithIndex(
array(
'template-top-level-post-types.php' => 'Top Level',
'subdir/template-sub-dir-post-types.php' => 'Sub Dir',
), $post_templates['post']
);
$this->assertEqualSetsWithIndex( array(
'template-top-level.php' => 'Top Level',
'subdir/template-sub-dir.php' => 'Sub Dir',
'template-header.php' => 'This Template Header Is On One Line',
), $post_templates['page'] );
$this->assertEqualSetsWithIndex(
array(
'template-top-level.php' => 'Top Level',
'subdir/template-sub-dir.php' => 'Sub Dir',
'template-header.php' => 'This Template Header Is On One Line',
), $post_templates['page']
);
}
/**
+76 -47
View File
@@ -53,21 +53,30 @@ class Tests_AdminBar extends WP_UnitTestCase {
$admin_bar = new WP_Admin_Bar;
$admin_bar->add_node( array(
'id' => 'test-node',
'meta' => array( 'class' => 'test-class' ),
) );
$admin_bar->add_node(
array(
'id' => 'test-node',
'meta' => array( 'class' => 'test-class' ),
)
);
$node1 = $admin_bar->get_node( 'test-node' );
$this->assertEquals( array( 'class' => 'test-class' ), $node1->meta );
$admin_bar->add_node( array(
'id' => 'test-node',
'meta' => array( 'some-meta' => 'value' ),
) );
$admin_bar->add_node(
array(
'id' => 'test-node',
'meta' => array( 'some-meta' => 'value' ),
)
);
$node2 = $admin_bar->get_node( 'test-node' );
$this->assertEquals( array( 'class' => 'test-class', 'some-meta' => 'value' ), $node2->meta );
$this->assertEquals(
array(
'class' => 'test-class',
'some-meta' => 'value',
), $node2->meta
);
}
/**
@@ -128,9 +137,11 @@ class Tests_AdminBar extends WP_UnitTestCase {
* @group ms-required
*/
public function test_admin_bar_contains_correct_links_for_users_with_no_role_on_blog() {
$blog_id = self::factory()->blog->create( array(
'user_id' => self::$admin_id,
) );
$blog_id = self::factory()->blog->create(
array(
'user_id' => self::$admin_id,
)
);
$this->assertTrue( user_can( self::$admin_id, 'read' ) );
$this->assertTrue( user_can( self::$editor_id, 'read' ) );
@@ -178,9 +189,11 @@ class Tests_AdminBar extends WP_UnitTestCase {
$this->assertTrue( user_can( self::$admin_id, 'read' ) );
$this->assertFalse( user_can( self::$no_role_id, 'read' ) );
$blog_id = self::factory()->blog->create( array(
'user_id' => self::$admin_id,
) );
$blog_id = self::factory()->blog->create(
array(
'user_id' => self::$admin_id,
)
);
$this->assertTrue( is_user_member_of_blog( self::$admin_id, $blog_id ) );
$this->assertFalse( is_user_member_of_blog( self::$no_role_id, $blog_id ) );
@@ -272,7 +285,7 @@ class Tests_AdminBar extends WP_UnitTestCase {
array(
// Empty string.
array(
'id' => 'test-node',
'id' => 'test-node',
'meta' => array( 'tabindex' => '' ),
),
'<div class="ab-item ab-empty-item">',
@@ -335,16 +348,19 @@ class Tests_AdminBar extends WP_UnitTestCase {
wp_set_current_user( self::$editor_id );
$post = array(
'post_author' => self::$editor_id,
'post_status' => 'publish',
'post_author' => self::$editor_id,
'post_status' => 'publish',
'post_content' => 'Post Content',
'post_title' => 'Post Title',
'post_title' => 'Post Title',
);
$id = wp_insert_post( $post );
$id = wp_insert_post( $post );
// Set queried object to the newly created post.
global $wp_the_query;
$wp_the_query->queried_object = (object) array( 'ID' => $id, 'post_type' => 'post' );
$wp_the_query->queried_object = (object) array(
'ID' => $id,
'post_type' => 'post',
);
$wp_admin_bar = $this->get_standard_admin_bar();
@@ -360,7 +376,10 @@ class Tests_AdminBar extends WP_UnitTestCase {
// Set queried object to a non-existing post.
global $wp_the_query;
$wp_the_query->queried_object = (object) array( 'ID' => 0, 'post_type' => 'post' );
$wp_the_query->queried_object = (object) array(
'ID' => 0,
'post_type' => 'post',
);
$wp_admin_bar = $this->get_standard_admin_bar();
@@ -471,11 +490,13 @@ class Tests_AdminBar extends WP_UnitTestCase {
* @ticket 34113
*/
public function test_admin_bar_has_no_archives_link_for_non_public_cpt() {
register_post_type( 'foo-non-public', array(
'public' => false,
'has_archive' => true,
'show_in_admin_bar' => true,
) );
register_post_type(
'foo-non-public', array(
'public' => false,
'has_archive' => true,
'show_in_admin_bar' => true,
)
);
set_current_screen( 'edit-foo-non-public' );
@@ -492,11 +513,13 @@ class Tests_AdminBar extends WP_UnitTestCase {
* @ticket 34113
*/
public function test_admin_bar_has_no_archives_link_for_cpt_without_archive() {
register_post_type( 'foo-non-public', array(
'public' => true,
'has_archive' => false,
'show_in_admin_bar' => true,
) );
register_post_type(
'foo-non-public', array(
'public' => true,
'has_archive' => false,
'show_in_admin_bar' => true,
)
);
set_current_screen( 'edit-foo-non-public' );
@@ -513,11 +536,13 @@ class Tests_AdminBar extends WP_UnitTestCase {
* @ticket 34113
*/
public function test_admin_bar_has_no_archives_link_for_cpt_not_shown_in_admin_bar() {
register_post_type( 'foo-non-public', array(
'public' => true,
'has_archive' => true,
'show_in_admin_bar' => false,
) );
register_post_type(
'foo-non-public', array(
'public' => true,
'has_archive' => true,
'show_in_admin_bar' => false,
)
);
set_current_screen( 'edit-foo-non-public' );
@@ -633,22 +658,26 @@ class Tests_AdminBar extends WP_UnitTestCase {
$this->go_to( home_url( "/?customize_changeset_uuid=$uuid" ) );
wp_set_current_user( self::$admin_id );
$this->factory()->post->create( array(
'post_type' => 'customize_changeset',
'post_status' => 'auto-draft',
'post_name' => $uuid,
) );
$wp_customize = new WP_Customize_Manager( array(
'changeset_uuid' => $uuid,
) );
$this->factory()->post->create(
array(
'post_type' => 'customize_changeset',
'post_status' => 'auto-draft',
'post_name' => $uuid,
)
);
$wp_customize = new WP_Customize_Manager(
array(
'changeset_uuid' => $uuid,
)
);
$wp_customize->start_previewing_theme();
set_current_screen( 'front' );
$wp_admin_bar = $this->get_standard_admin_bar();
$node = $wp_admin_bar->get_node( 'customize' );
$node = $wp_admin_bar->get_node( 'customize' );
$this->assertNotEmpty( $node );
$parsed_url = wp_parse_url( $node->href );
$parsed_url = wp_parse_url( $node->href );
$query_params = array();
wp_parse_str( $parsed_url['query'], $query_params );
$this->assertEquals( $uuid, $query_params['changeset_uuid'] );
+18 -14
View File
@@ -15,14 +15,16 @@ class Tests_Ajax_Attachments extends WP_Ajax_UnitTestCase {
*/
public function test_wp_ajax_send_attachment_to_editor_should_return_an_image() {
// Become an administrator
$post = $_POST;
$user_id = self::factory()->user->create( array(
'role' => 'administrator',
'user_login' => 'user_36578_administrator',
'user_email' => 'user_36578_administrator@example.com',
) );
$post = $_POST;
$user_id = self::factory()->user->create(
array(
'role' => 'administrator',
'user_login' => 'user_36578_administrator',
'user_email' => 'user_36578_administrator@example.com',
)
);
wp_set_current_user( $user_id );
$_POST = array_merge($_POST, $post);
$_POST = array_merge( $_POST, $post );
$filename = DIR_TESTDATA . '/images/canola.jpg';
$contents = file_get_contents( $filename );
@@ -64,14 +66,16 @@ class Tests_Ajax_Attachments extends WP_Ajax_UnitTestCase {
*/
public function test_wp_ajax_send_attachment_to_editor_should_return_a_link() {
// Become an administrator
$post = $_POST;
$user_id = self::factory()->user->create( array(
'role' => 'administrator',
'user_login' => 'user_36578_administrator',
'user_email' => 'user_36578_administrator@example.com',
) );
$post = $_POST;
$user_id = self::factory()->user->create(
array(
'role' => 'administrator',
'user_login' => 'user_36578_administrator',
'user_email' => 'user_36578_administrator@example.com',
)
);
wp_set_current_user( $user_id );
$_POST = array_merge($_POST, $post);
$_POST = array_merge( $_POST, $post );
$filename = DIR_TESTDATA . '/formatting/entities.txt';
$contents = file_get_contents( $filename );
+25 -21
View File
@@ -17,11 +17,12 @@ class Tests_Ajax_Autosave extends WP_Ajax_UnitTestCase {
/**
* Post
*
* @var mixed
*/
protected $_post = null;
protected static $admin_id = 0;
protected static $admin_id = 0;
protected static $editor_id = 0;
protected static $post;
protected static $post_id;
@@ -32,7 +33,7 @@ class Tests_Ajax_Autosave extends WP_Ajax_UnitTestCase {
self::$user_ids[] = self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
self::$post_id = $factory->post->create( array( 'post_status' => 'draft' ) );
self::$post = get_post( self::$post_id );
self::$post = get_post( self::$post_id );
}
/**
@@ -46,6 +47,7 @@ class Tests_Ajax_Autosave extends WP_Ajax_UnitTestCase {
/**
* Test autosaving a post
*
* @return void
*/
public function test_autosave_post() {
@@ -53,16 +55,16 @@ class Tests_Ajax_Autosave extends WP_Ajax_UnitTestCase {
wp_set_current_user( self::$admin_id );
// Set up the $_POST request
$md5 = md5( uniqid() );
$md5 = md5( uniqid() );
$_POST = array(
'action' => 'heartbeat',
'action' => 'heartbeat',
'_nonce' => wp_create_nonce( 'heartbeat-nonce' ),
'data' => array(
'data' => array(
'wp_autosave' => array(
'post_id' => self::$post_id,
'_wpnonce' => wp_create_nonce( 'update-post_' . self::$post->ID ),
'post_content' => self::$post->post_content . PHP_EOL . $md5,
'post_type' => 'post',
'post_id' => self::$post_id,
'_wpnonce' => wp_create_nonce( 'update-post_' . self::$post->ID ),
'post_content' => self::$post->post_content . PHP_EOL . $md5,
'post_type' => 'post',
),
),
);
@@ -88,6 +90,7 @@ class Tests_Ajax_Autosave extends WP_Ajax_UnitTestCase {
/**
* Test autosaving a locked post
*
* @return void
*/
public function test_autosave_locked_post() {
@@ -101,16 +104,16 @@ class Tests_Ajax_Autosave extends WP_Ajax_UnitTestCase {
$this->assertEquals( self::$editor_id, wp_check_post_lock( self::$post_id ) );
// Set up the $_POST request
$md5 = md5( uniqid() );
$md5 = md5( uniqid() );
$_POST = array(
'action' => 'heartbeat',
'action' => 'heartbeat',
'_nonce' => wp_create_nonce( 'heartbeat-nonce' ),
'data' => array(
'data' => array(
'wp_autosave' => array(
'post_id' => self::$post_id,
'_wpnonce' => wp_create_nonce( 'update-post_' . self::$post_id ),
'post_content' => self::$post->post_content . PHP_EOL . $md5,
'post_type' => 'post',
'post_id' => self::$post_id,
'_wpnonce' => wp_create_nonce( 'update-post_' . self::$post_id ),
'post_content' => self::$post->post_content . PHP_EOL . $md5,
'post_type' => 'post',
),
),
);
@@ -140,20 +143,21 @@ class Tests_Ajax_Autosave extends WP_Ajax_UnitTestCase {
/**
* Test with an invalid nonce
*
* @return void
*/
public function test_with_invalid_nonce( ) {
public function test_with_invalid_nonce() {
wp_set_current_user( self::$admin_id );
// Set up the $_POST request
$_POST = array(
'action' => 'heartbeat',
'action' => 'heartbeat',
'_nonce' => wp_create_nonce( 'heartbeat-nonce' ),
'data' => array(
'data' => array(
'wp_autosave' => array(
'post_id' => self::$post_id,
'_wpnonce' => substr( md5( uniqid() ), 0, 10 ),
'post_id' => self::$post_id,
'_wpnonce' => substr( md5( uniqid() ), 0, 10 ),
),
),
);
+6 -5
View File
@@ -56,7 +56,7 @@ class Tests_Ajax_CompressionTest extends WP_Ajax_UnitTestCase {
*/
public function test_gzdeflate() {
if ( !function_exists( 'gzdeflate' ) ) {
if ( ! function_exists( 'gzdeflate' ) ) {
$this->fail( 'gzdeflate function not available' );
}
@@ -64,7 +64,7 @@ class Tests_Ajax_CompressionTest extends WP_Ajax_UnitTestCase {
$this->_setRole( 'administrator' );
// Set up a default request
$_GET['test'] = 2;
$_GET['test'] = 2;
$_SERVER['HTTP_ACCEPT_ENCODING'] = 'deflate';
// Make the request
@@ -83,7 +83,7 @@ class Tests_Ajax_CompressionTest extends WP_Ajax_UnitTestCase {
*/
public function test_gzencode() {
if ( !function_exists('gzencode') ) {
if ( ! function_exists( 'gzencode' ) ) {
$this->fail( 'gzencode function not available' );
}
@@ -91,7 +91,7 @@ class Tests_Ajax_CompressionTest extends WP_Ajax_UnitTestCase {
$this->_setRole( 'administrator' );
// Set up a default request
$_GET['test'] = 2;
$_GET['test'] = 2;
$_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
// Make the request
@@ -114,7 +114,7 @@ class Tests_Ajax_CompressionTest extends WP_Ajax_UnitTestCase {
$this->_setRole( 'administrator' );
// Set up a default request
$_GET['test'] = 2;
$_GET['test'] = 2;
$_SERVER['HTTP_ACCEPT_ENCODING'] = 'unknown';
// Make the request
@@ -200,6 +200,7 @@ class Tests_Ajax_CompressionTest extends WP_Ajax_UnitTestCase {
/**
* Undo gzencode. This is ugly, but there's no stock gzdecode() function.
*
* @param string $encoded_data
* @return string
*/
+166 -126
View File
@@ -44,7 +44,7 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$subscriber_user_id = $factory->user->create( array( 'role' => 'subscriber' ) );
self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );
self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );
}
/**
@@ -70,7 +70,7 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
*/
protected function make_ajax_call( $action ) {
$this->_last_response_parsed = null;
$this->_last_response = '';
$this->_last_response = '';
try {
$this->_handleAjax( $action );
} catch ( WPAjaxDieContinueException $e ) {
@@ -119,9 +119,9 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
// Unauthorized.
wp_set_current_user( self::$subscriber_user_id );
$nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
$nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
$_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce;
$exception = null;
$exception = null;
try {
ob_start();
$wp_customize->setup_theme();
@@ -133,7 +133,7 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
// Not called setup_theme.
wp_set_current_user( self::$admin_user_id );
$nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
$nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
$_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce;
$this->make_ajax_call( 'customize_save' );
$this->assertFalse( $this->_last_response_parsed['success'] );
@@ -147,9 +147,9 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
$this->assertEquals( 'invalid_nonce', $this->_last_response_parsed['data'] );
// User cannot create.
$nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
$_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce;
$post_type_obj = get_post_type_object( 'customize_changeset' );
$nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
$_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce;
$post_type_obj = get_post_type_object( 'customize_changeset' );
$post_type_obj->cap->create_posts = 'create_customize_changesets';
$this->make_ajax_call( 'customize_save' );
$this->assertFalse( $this->_last_response_parsed['success'] );
@@ -165,13 +165,15 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
$this->make_ajax_call( 'customize_save' );
$this->assertFalse( $this->_last_response_parsed['success'] );
$this->assertEquals( 'changeset_already_published', $this->_last_response_parsed['data']['code'] );
wp_update_post( array(
'ID' => $wp_customize->changeset_post_id(),
'post_status' => 'auto-draft',
) );
wp_update_post(
array(
'ID' => $wp_customize->changeset_post_id(),
'post_status' => 'auto-draft',
)
);
// User cannot edit.
$post_type_obj = get_post_type_object( 'customize_changeset' );
$post_type_obj = get_post_type_object( 'customize_changeset' );
$post_type_obj->cap->edit_post = 'edit_customize_changesets';
$this->make_ajax_call( 'customize_save' );
$this->assertFalse( $this->_last_response_parsed['success'] );
@@ -188,15 +190,15 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
$this->assertEquals( 'invalid_customize_changeset_data', $this->_last_response_parsed['data'] );
// Bad customize_changeset_status.
$_POST['customize_changeset_data'] = '{}';
$_POST['customize_changeset_data'] = '{}';
$_POST['customize_changeset_status'] = 'unrecognized';
$this->make_ajax_call( 'customize_save' );
$this->assertFalse( $this->_last_response_parsed['success'] );
$this->assertEquals( 'bad_customize_changeset_status', $this->_last_response_parsed['data'] );
// Disallowed publish posts if not allowed.
$post_type_obj = get_post_type_object( 'customize_changeset' );
$post_type_obj->cap->publish_posts = 'publish_customize_changesets';
$post_type_obj = get_post_type_object( 'customize_changeset' );
$post_type_obj->cap->publish_posts = 'publish_customize_changesets';
$_POST['customize_changeset_status'] = 'publish';
$this->make_ajax_call( 'customize_save' );
$this->assertFalse( $this->_last_response_parsed['success'] );
@@ -209,7 +211,7 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
// Validate date.
$_POST['customize_changeset_status'] = 'draft';
$_POST['customize_changeset_date'] = 'BAD DATE';
$_POST['customize_changeset_date'] = 'BAD DATE';
$this->make_ajax_call( 'customize_save' );
$this->assertFalse( $this->_last_response_parsed['success'] );
$this->assertEquals( 'bad_customize_changeset_date', $this->_last_response_parsed['data'] );
@@ -221,14 +223,16 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
$this->make_ajax_call( 'customize_save' );
$this->assertTrue( $this->_last_response_parsed['success'] );
$_POST['customize_changeset_status'] = 'future';
$_POST['customize_changeset_date'] = '+10 minutes';
$_POST['customize_changeset_date'] = '+10 minutes';
$this->make_ajax_call( 'customize_save' );
$this->assertTrue( $this->_last_response_parsed['success'] );
$this->assertEquals( 'future', get_post_status( $wp_customize->changeset_post_id() ) );
wp_update_post( array(
'ID' => $wp_customize->changeset_post_id(),
'post_status' => 'auto-draft',
) );
wp_update_post(
array(
'ID' => $wp_customize->changeset_post_id(),
'post_status' => 'auto-draft',
)
);
}
/**
@@ -240,11 +244,13 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
protected function set_up_valid_state( $uuid = null ) {
global $wp_customize;
wp_set_current_user( self::$admin_user_id );
$wp_customize = new WP_Customize_Manager( array(
'changeset_uuid' => $uuid,
) );
$wp_customize = new WP_Customize_Manager(
array(
'changeset_uuid' => $uuid,
)
);
$wp_customize->register_controls();
$nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
$nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
$_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce;
$wp_customize->setup_theme();
return $wp_customize;
@@ -260,12 +266,14 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
$wp_customize = $this->set_up_valid_state();
$_POST['customize_changeset_status'] = 'publish';
$_POST['customize_changeset_title'] = 'Success Changeset';
$_POST['customize_changeset_data'] = wp_json_encode( array(
'blogname' => array(
'value' => 'Successful Site Title',
),
) );
$_POST['customize_changeset_title'] = 'Success Changeset';
$_POST['customize_changeset_data'] = wp_json_encode(
array(
'blogname' => array(
'value' => 'Successful Site Title',
),
)
);
$this->make_ajax_call( 'customize_save' );
$this->assertTrue( $this->_last_response_parsed['success'] );
$this->assertInternalType( 'array', $this->_last_response_parsed['data'] );
@@ -286,21 +294,25 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
function test_save_success_publish_edit() {
$uuid = wp_generate_uuid4();
$post_id = $this->factory()->post->create( array(
'post_name' => $uuid,
'post_title' => 'Original',
'post_type' => 'customize_changeset',
'post_status' => 'auto-draft',
'post_content' => wp_json_encode( array(
'blogname' => array(
'value' => 'New Site Title',
$post_id = $this->factory()->post->create(
array(
'post_name' => $uuid,
'post_title' => 'Original',
'post_type' => 'customize_changeset',
'post_status' => 'auto-draft',
'post_content' => wp_json_encode(
array(
'blogname' => array(
'value' => 'New Site Title',
),
)
),
) ),
) );
)
);
$wp_customize = $this->set_up_valid_state( $uuid );
$_POST['customize_changeset_status'] = 'publish';
$_POST['customize_changeset_title'] = 'Published';
$_POST['customize_changeset_title'] = 'Published';
$this->make_ajax_call( 'customize_save' );
$this->assertTrue( $this->_last_response_parsed['success'] );
$this->assertInternalType( 'array', $this->_last_response_parsed['data'] );
@@ -319,25 +331,29 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
* @covers WP_Customize_Manager::save()
*/
function test_success_save_post_date() {
$uuid = wp_generate_uuid4();
$post_id = $this->factory()->post->create( array(
'post_name' => $uuid,
'post_title' => 'Original',
'post_type' => 'customize_changeset',
'post_status' => 'auto-draft',
'post_content' => wp_json_encode( array(
'blogname' => array(
'value' => 'New Site Title',
$uuid = wp_generate_uuid4();
$post_id = $this->factory()->post->create(
array(
'post_name' => $uuid,
'post_title' => 'Original',
'post_type' => 'customize_changeset',
'post_status' => 'auto-draft',
'post_content' => wp_json_encode(
array(
'blogname' => array(
'value' => 'New Site Title',
),
)
),
) ),
) );
)
);
$wp_customize = $this->set_up_valid_state( $uuid );
// Success future schedule date.
$future_date = ( gmdate( 'Y' ) + 1 ) . '-01-01 00:00:00';
$future_date = ( gmdate( 'Y' ) + 1 ) . '-01-01 00:00:00';
$_POST['customize_changeset_status'] = 'future';
$_POST['customize_changeset_title'] = 'Future date';
$_POST['customize_changeset_date'] = $future_date;
$_POST['customize_changeset_title'] = 'Future date';
$_POST['customize_changeset_date'] = $future_date;
$this->make_ajax_call( 'customize_save' );
$this->assertTrue( $this->_last_response_parsed['success'] );
$this->assertArrayHasKey( 'changeset_date', $this->_last_response_parsed['data'] );
@@ -362,12 +378,14 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
$this->assertEquals( $future_date, $changeset_post_schedule->post_date );
// Success if draft with past date.
$now = current_time( 'mysql' );
wp_update_post( array(
'ID' => $post_id,
'post_status' => 'draft',
'post_date' => $now,
'post_date_gmt' => get_gmt_from_date( $now ),
) );
wp_update_post(
array(
'ID' => $post_id,
'post_status' => 'draft',
'post_date' => $now,
'post_date_gmt' => get_gmt_from_date( $now ),
)
);
// Fail if future request and existing date is past.
$_POST['customize_changeset_status'] = 'future';
@@ -377,12 +395,14 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
$this->assertEquals( 'not_future_date', $this->_last_response_parsed['data']['code'] );
// Success publish changeset reset date to current.
wp_update_post( array(
'ID' => $post_id,
'post_status' => 'future',
'post_date' => $future_date,
'post_date_gmt' => get_gmt_from_date( $future_date ),
) );
wp_update_post(
array(
'ID' => $post_id,
'post_status' => 'future',
'post_date' => $future_date,
'post_date_gmt' => get_gmt_from_date( $future_date ),
)
);
unset( $_POST['customize_changeset_date'] );
$_POST['customize_changeset_status'] = 'publish';
$this->make_ajax_call( 'customize_save' );
@@ -411,25 +431,31 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
public function test_save_autosave() {
$uuid = wp_generate_uuid4();
$post_id = $this->factory()->post->create( array(
'post_name' => $uuid,
'post_type' => 'customize_changeset',
'post_status' => 'draft',
'post_content' => wp_json_encode( array(
'blogname' => array(
'value' => 'New Site Title',
$post_id = $this->factory()->post->create(
array(
'post_name' => $uuid,
'post_type' => 'customize_changeset',
'post_status' => 'draft',
'post_content' => wp_json_encode(
array(
'blogname' => array(
'value' => 'New Site Title',
),
)
),
) ),
) );
)
);
$this->set_up_valid_state( $uuid );
$this->assertFalse( wp_get_post_autosave( $post_id ) );
$_POST['customize_changeset_data'] = wp_json_encode( array(
'blogname' => array(
'value' => 'Autosaved Site Title',
),
) );
$_POST['customize_changeset_data'] = wp_json_encode(
array(
'blogname' => array(
'value' => 'Autosaved Site Title',
),
)
);
$_POST['customize_changeset_autosave'] = 'on';
$this->make_ajax_call( 'customize_save' );
@@ -449,14 +475,14 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
* @covers WP_Customize_Manager::handle_changeset_trash_request()
*/
public function test_handle_changeset_trash_request() {
$uuid = wp_generate_uuid4();
$uuid = wp_generate_uuid4();
$wp_customize = $this->set_up_valid_state( $uuid );
$this->make_ajax_call( 'customize_trash' );
$this->assertFalse( $this->_last_response_parsed['success'] );
$this->assertEquals( 'invalid_nonce', $this->_last_response_parsed['data']['code'] );
$nonce = wp_create_nonce( 'trash_customize_changeset' );
$nonce = wp_create_nonce( 'trash_customize_changeset' );
$_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce;
$this->make_ajax_call( 'customize_trash' );
$this->assertFalse( $this->_last_response_parsed['success'] );
@@ -464,9 +490,11 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
$wp_customize->register_controls(); // And settings too.
$wp_customize->set_post_value( 'blogname', 'HELLO' );
$wp_customize->save_changeset_post( array(
'status' => 'save',
) );
$wp_customize->save_changeset_post(
array(
'status' => 'save',
)
);
add_filter( 'map_meta_cap', array( $this, 'return_do_not_allow' ) );
$this->make_ajax_call( 'customize_trash' );
@@ -474,18 +502,22 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
$this->assertEquals( 'changeset_trash_unauthorized', $this->_last_response_parsed['data']['code'] );
remove_filter( 'map_meta_cap', array( $this, 'return_do_not_allow' ) );
wp_update_post( array(
'ID' => $wp_customize->changeset_post_id(),
'post_status' => 'trash',
) );
wp_update_post(
array(
'ID' => $wp_customize->changeset_post_id(),
'post_status' => 'trash',
)
);
$this->make_ajax_call( 'customize_trash' );
$this->assertFalse( $this->_last_response_parsed['success'] );
$this->assertEquals( 'changeset_already_trashed', $this->_last_response_parsed['data']['code'] );
wp_update_post( array(
'ID' => $wp_customize->changeset_post_id(),
'post_status' => 'draft',
) );
wp_update_post(
array(
'ID' => $wp_customize->changeset_post_id(),
'post_status' => 'draft',
)
);
$wp_trash_post_count = did_action( 'wp_trash_post' );
add_filter( 'pre_trash_post', '__return_false' );
@@ -520,14 +552,14 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
* @covers WP_Customize_Manager::dismiss_user_auto_draft_changesets()
*/
public function test_handle_dismiss_autosave_or_lock_request() {
$uuid = wp_generate_uuid4();
$uuid = wp_generate_uuid4();
$wp_customize = $this->set_up_valid_state( $uuid );
$this->make_ajax_call( 'customize_dismiss_autosave_or_lock' );
$this->assertFalse( $this->_last_response_parsed['success'] );
$this->assertEquals( 'invalid_nonce', $this->_last_response_parsed['data'] );
$nonce = wp_create_nonce( 'customize_dismiss_autosave_or_lock' );
$nonce = wp_create_nonce( 'customize_dismiss_autosave_or_lock' );
$_POST['nonce'] = $_GET['nonce'] = $_REQUEST['nonce'] = $nonce;
$_POST['dismiss_lock'] = $_GET['dismiss_lock'] = $_REQUEST['dismiss_lock'] = true;
@@ -545,23 +577,27 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
// Create auto-drafts.
$user_auto_draft_ids = array();
for ( $i = 0; $i < 3; $i++ ) {
$user_auto_draft_ids[] = $this->factory()->post->create( array(
'post_name' => wp_generate_uuid4(),
'post_type' => 'customize_changeset',
'post_status' => 'auto-draft',
'post_author' => self::$admin_user_id,
'post_content' => wp_json_encode( array() ),
) );
$user_auto_draft_ids[] = $this->factory()->post->create(
array(
'post_name' => wp_generate_uuid4(),
'post_type' => 'customize_changeset',
'post_status' => 'auto-draft',
'post_author' => self::$admin_user_id,
'post_content' => wp_json_encode( array() ),
)
);
}
$other_user_auto_draft_ids = array();
for ( $i = 0; $i < 3; $i++ ) {
$other_user_auto_draft_ids[] = $this->factory()->post->create( array(
'post_name' => wp_generate_uuid4(),
'post_type' => 'customize_changeset',
'post_status' => 'auto-draft',
'post_author' => $other_user_id,
'post_content' => wp_json_encode( array() ),
) );
$other_user_auto_draft_ids[] = $this->factory()->post->create(
array(
'post_name' => wp_generate_uuid4(),
'post_type' => 'customize_changeset',
'post_status' => 'auto-draft',
'post_author' => $other_user_id,
'post_content' => wp_json_encode( array() ),
)
);
}
foreach ( array_merge( $user_auto_draft_ids, $other_user_auto_draft_ids ) as $post_id ) {
$this->assertFalse( (bool) get_post_meta( $post_id, '_customize_restore_dismissed', true ) );
@@ -584,14 +620,16 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
$this->assertEquals( 'no_auto_draft_to_delete', $this->_last_response_parsed['data'] );
// Save a changeset as a draft.
$r = $wp_customize->save_changeset_post( array(
'data' => array(
'blogname' => array(
'value' => 'Foo',
$r = $wp_customize->save_changeset_post(
array(
'data' => array(
'blogname' => array(
'value' => 'Foo',
),
),
),
'status' => 'draft',
) );
'status' => 'draft',
)
);
$_POST['dismiss_autosave'] = $_GET['dismiss_autosave'] = $_REQUEST['dismiss_autosave'] = false;
$this->make_ajax_call( 'customize_dismiss_autosave_or_lock' );
@@ -609,14 +647,16 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase {
$this->assertEquals( 'no_autosave_revision_to_delete', $this->_last_response_parsed['data'] );
// Add the autosave revision.
$r = $wp_customize->save_changeset_post( array(
'data' => array(
'blogname' => array(
'value' => 'Bar',
$r = $wp_customize->save_changeset_post(
array(
'data' => array(
'blogname' => array(
'value' => 'Bar',
),
),
),
'autosave' => true,
) );
'autosave' => true,
)
);
$this->assertNotInstanceOf( 'WP_Error', $r );
$autosave_revision = wp_get_post_autosave( $wp_customize->changeset_post_id() );
$this->assertInstanceOf( 'WP_Post', $autosave_revision );
+156 -117
View File
@@ -25,7 +25,7 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
global $wp_customize;
$this->wp_customize = new WP_Customize_Manager();
$wp_customize = $this->wp_customize;
$wp_customize = $this->wp_customize;
}
/**
@@ -86,8 +86,8 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
*
* @return array {
* @type array {
* @string string $role The role that will test caps for.
* @array array $expected_results The expected results from the ajax call.
* @string string $role The role that will test caps for.
* @array array $expected_results The expected results from the ajax call.
* }
* }
*/
@@ -129,10 +129,12 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
*/
function test_ajax_load_available_items_error_messages( $post_args, $expected_results ) {
$_POST = array_merge( array(
'action' => 'load-available-menu-items-customizer',
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
), $post_args );
$_POST = array_merge(
array(
'action' => 'load-available-menu-items-customizer',
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
), $post_args
);
// Make the request.
$this->make_ajax_call( 'load-available-menu-items-customizer' );
@@ -153,8 +155,8 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
*
* @return array {
* @type array {
* @array array $post_args The arguments that will merged with the $_POST array.
* @array array $expected_results The expected results from the ajax call.
* @array array $post_args The arguments that will merged with the $_POST array.
* @array array $expected_results The expected results from the ajax call.
* }
* }
*/
@@ -163,34 +165,34 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
// Testing empty obj_type and type.
array(
array(
'type' => '',
'object' => '',
'type' => '',
'object' => '',
),
array(
'success' => false,
'data' => 'nav_menus_missing_type_or_object_parameter',
'success' => false,
'data' => 'nav_menus_missing_type_or_object_parameter',
),
),
// Testing empty obj_type.
array(
array(
'type' => 'post_type',
'object' => '',
'type' => 'post_type',
'object' => '',
),
array(
'success' => false,
'data' => 'nav_menus_missing_type_or_object_parameter',
'success' => false,
'data' => 'nav_menus_missing_type_or_object_parameter',
),
),
// Testing empty type.
array(
array(
'type' => '',
'object' => 'post',
'type' => '',
'object' => 'post',
),
array(
'success' => false,
'data' => 'nav_menus_missing_type_or_object_parameter',
'success' => false,
'data' => 'nav_menus_missing_type_or_object_parameter',
),
),
// Testing empty type of a bulk request.
@@ -198,29 +200,29 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
array(
'item_types' => array(
array(
'type' => 'post_type',
'object' => 'post',
'type' => 'post_type',
'object' => 'post',
),
array(
'type' => 'post_type',
'object' => '',
'type' => 'post_type',
'object' => '',
),
),
),
array(
'success' => false,
'data' => 'nav_menus_missing_type_or_object_parameter',
'success' => false,
'data' => 'nav_menus_missing_type_or_object_parameter',
),
),
// Testing incorrect type option.
array(
array(
'type' => 'post_type',
'object' => 'invalid',
'type' => 'post_type',
'object' => 'invalid',
),
array(
'success' => false,
'data' => 'nav_menus_invalid_post_type',
'success' => false,
'data' => 'nav_menus_invalid_post_type',
),
),
);
@@ -236,10 +238,12 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
*/
function test_ajax_load_available_items_success_status( $post_args, $success_status ) {
$_POST = array_merge( array(
'action' => 'load-available-menu-items-customizer',
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
), $post_args );
$_POST = array_merge(
array(
'action' => 'load-available-menu-items-customizer',
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
), $post_args
);
// Make the request.
$this->make_ajax_call( 'load-available-menu-items-customizer' );
@@ -269,29 +273,29 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
return array(
array(
array(
'type' => 'post_type',
'object' => 'post',
'type' => 'post_type',
'object' => 'post',
),
true,
),
array(
array(
'type' => 'post_type',
'object' => 'page',
'type' => 'post_type',
'object' => 'page',
),
true,
),
array(
array(
'type' => 'post_type',
'object' => 'custom',
'type' => 'post_type',
'object' => 'custom',
),
false,
),
array(
array(
'type' => 'taxonomy',
'object' => 'post_tag',
'type' => 'taxonomy',
'object' => 'post_tag',
),
true,
),
@@ -300,12 +304,12 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
array(
'item_types' => array(
array(
'type' => 'post_type',
'object' => 'post',
'type' => 'post_type',
'object' => 'post',
),
array(
'type' => 'post_type',
'object' => 'page',
'type' => 'post_type',
'object' => 'page',
),
),
),
@@ -337,14 +341,21 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
// Create some terms and pages.
self::factory()->term->create_many( 5 );
self::factory()->post->create_many( 5, array( 'post_type' => 'page' ) );
$auto_draft_post = $this->wp_customize->nav_menus->insert_auto_draft_post( array( 'post_title' => 'Test Auto Draft', 'post_type' => 'post' ) );
$auto_draft_post = $this->wp_customize->nav_menus->insert_auto_draft_post(
array(
'post_title' => 'Test Auto Draft',
'post_type' => 'post',
)
);
$this->wp_customize->set_post_value( 'nav_menus_created_posts', array( $auto_draft_post->ID ) );
$this->wp_customize->get_setting( 'nav_menus_created_posts' )->preview();
$_POST = array_merge( array(
'action' => 'load-available-menu-items-customizer',
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
), $post_args );
$_POST = array_merge(
array(
'action' => 'load-available-menu-items-customizer',
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
), $post_args
);
// Make the request.
$this->make_ajax_call( 'load-available-menu-items-customizer' );
@@ -356,7 +367,7 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
// Get the second index to avoid the home page edge case.
$first_prop = current( $response['data']['items'] );
$test_item = $first_prop[1];
$test_item = $first_prop[1];
foreach ( $expected_keys as $key ) {
$this->assertArrayHasKey( $key, $test_item );
@@ -366,7 +377,7 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
// Special test for the home page.
if ( 'page' === $test_item['object'] ) {
$first_prop = current( $response['data']['items'] );
$home = $first_prop[0];
$home = $first_prop[0];
foreach ( $expected_keys as $key ) {
if ( 'object_id' !== $key ) {
$this->assertArrayHasKey( $key, $home );
@@ -398,20 +409,20 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
return array(
array(
array(
'type' => 'post_type',
'object' => 'post',
'type' => 'post_type',
'object' => 'post',
),
),
array(
array(
'type' => 'post_type',
'object' => 'page',
'type' => 'post_type',
'object' => 'page',
),
),
array(
array(
'type' => 'taxonomy',
'object' => 'post_tag',
'type' => 'taxonomy',
'object' => 'post_tag',
),
),
);
@@ -462,8 +473,8 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
*
* @return array {
* @type array {
* @string string $role The role that will test caps for.
* @array array $expected_results The expected results from the ajax call.
* @string string $role The role that will test caps for.
* @array array $expected_results The expected results from the ajax call.
* }
* }
*/
@@ -507,15 +518,27 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
do_action( 'customize_register', $this->wp_customize );
self::factory()->post->create_many( 5, array( 'post_title' => 'Test Post' ) );
$included_auto_draft_post = $this->wp_customize->nav_menus->insert_auto_draft_post( array( 'post_title' => 'Test Included Auto Draft', 'post_type' => 'post' ) );
$excluded_auto_draft_post = $this->wp_customize->nav_menus->insert_auto_draft_post( array( 'post_title' => 'Excluded Auto Draft', 'post_type' => 'post' ) );
$included_auto_draft_post = $this->wp_customize->nav_menus->insert_auto_draft_post(
array(
'post_title' => 'Test Included Auto Draft',
'post_type' => 'post',
)
);
$excluded_auto_draft_post = $this->wp_customize->nav_menus->insert_auto_draft_post(
array(
'post_title' => 'Excluded Auto Draft',
'post_type' => 'post',
)
);
$this->wp_customize->set_post_value( 'nav_menus_created_posts', array( $included_auto_draft_post->ID, $excluded_auto_draft_post->ID ) );
$this->wp_customize->get_setting( 'nav_menus_created_posts' )->preview();
$_POST = array_merge( array(
'action' => 'search-available-menu-items-customizer',
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
), $post_args );
$_POST = array_merge(
array(
'action' => 'search-available-menu-items-customizer',
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
), $post_args
);
$this->make_ajax_call( 'search-available-menu-items-customizer' );
@@ -541,8 +564,8 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
*
* @return array {
* @type array {
* @string string $post_args The args that will be passed to ajax.
* @array array $expected_results The expected results from the ajax call.
* @string string $post_args The args that will be passed to ajax.
* @array array $expected_results The expected results from the ajax call.
* }
* }
*/
@@ -557,7 +580,7 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
),
array(
array(
'search' => 'all_the_things',
'search' => 'all_the_things',
),
array(
'success' => false,
@@ -568,7 +591,7 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
),
array(
array(
'search' => 'test',
'search' => 'test',
),
array(
'success' => true,
@@ -584,13 +607,15 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
* @covers WP_Customize_Nav_Menus::ajax_insert_auto_draft_post()
*/
function test_ajax_insert_auto_draft_post_success() {
$_POST = wp_slash( array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
'params' => array(
'post_type' => 'post',
'post_title' => 'Hello World',
),
) );
$_POST = wp_slash(
array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
'params' => array(
'post_type' => 'post',
'post_title' => 'Hello World',
),
)
);
$this->_last_response = '';
$this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
$response = json_decode( $this->_last_response, true );
@@ -613,7 +638,7 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
*/
function test_ajax_insert_auto_draft_failures() {
// No nonce.
$_POST = array();
$_POST = array();
$this->_last_response = '';
$this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
$response = json_decode( $this->_last_response, true );
@@ -621,9 +646,11 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
$this->assertEquals( 'bad_nonce', $response['data'] );
// Bad nonce.
$_POST = wp_slash( array(
'customize-menus-nonce' => 'bad',
) );
$_POST = wp_slash(
array(
'customize-menus-nonce' => 'bad',
)
);
$this->_last_response = '';
$this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
$response = json_decode( $this->_last_response, true );
@@ -632,9 +659,11 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
// Bad nonce.
wp_set_current_user( $this->factory()->user->create( array( 'role' => 'subscriber' ) ) );
$_POST = wp_slash( array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
) );
$_POST = wp_slash(
array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
)
);
$this->_last_response = '';
$this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
$response = json_decode( $this->_last_response, true );
@@ -643,9 +672,11 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
// Missing params.
wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) );
$_POST = wp_slash( array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
) );
$_POST = wp_slash(
array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
)
);
$this->_last_response = '';
$this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
$response = json_decode( $this->_last_response, true );
@@ -654,12 +685,14 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
// insufficient_post_permissions.
register_post_type( 'privilege', array( 'capability_type' => 'privilege' ) );
$_POST = wp_slash( array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
'params' => array(
'post_type' => 'privilege',
),
) );
$_POST = wp_slash(
array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
'params' => array(
'post_type' => 'privilege',
),
)
);
$this->_last_response = '';
$this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
$response = json_decode( $this->_last_response, true );
@@ -667,12 +700,14 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
$this->assertEquals( 'insufficient_post_permissions', $response['data'] );
// insufficient_post_permissions.
$_POST = wp_slash( array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
'params' => array(
'post_type' => 'non-existent',
),
) );
$_POST = wp_slash(
array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
'params' => array(
'post_type' => 'non-existent',
),
)
);
$this->_last_response = '';
$this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
$response = json_decode( $this->_last_response, true );
@@ -680,13 +715,15 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
$this->assertEquals( 'missing_post_type_param', $response['data'] );
// missing_post_title.
$_POST = wp_slash( array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
'params' => array(
'post_type' => 'post',
'post_title' => ' ',
),
) );
$_POST = wp_slash(
array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
'params' => array(
'post_type' => 'post',
'post_title' => ' ',
),
)
);
$this->_last_response = '';
$this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
$response = json_decode( $this->_last_response, true );
@@ -694,15 +731,17 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
$this->assertEquals( 'missing_post_title', $response['data'] );
// illegal_params.
$_POST = wp_slash( array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
'params' => array(
'post_type' => 'post',
'post_title' => 'OK',
'post_name' => 'bad',
'post_content' => 'bad',
),
) );
$_POST = wp_slash(
array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
'params' => array(
'post_type' => 'post',
'post_title' => 'OK',
'post_name' => 'bad',
'post_content' => 'bad',
),
)
);
$this->_last_response = '';
$this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
$response = json_decode( $this->_last_response, true );
+27 -15
View File
@@ -17,12 +17,14 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
/**
* List of comments
*
* @var array
*/
protected static $comments = array();
/**
* ID of a post.
*
* @var int
*/
protected static $post_id;
@@ -30,7 +32,7 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
public static function wpSetUpBeforeClass( $factory ) {
self::$post_id = $factory->post->create();
$comment_ids = $factory->comment->create_post_comments( self::$post_id, 8 );
$comment_ids = $factory->comment->create_post_comments( self::$post_id, 8 );
self::$comments = array_map( 'get_comment', $comment_ids );
}
@@ -38,11 +40,11 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
* Clear the POST actions in between requests
*/
protected function _clear_post_action() {
unset($_POST['trash']);
unset($_POST['untrash']);
unset($_POST['spam']);
unset($_POST['unspam']);
unset($_POST['delete']);
unset( $_POST['trash'] );
unset( $_POST['untrash'] );
unset( $_POST['spam'] );
unset( $_POST['unspam'] );
unset( $_POST['delete'] );
$this->_last_response = '';
}
@@ -53,6 +55,7 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
/**
* Test as a privilged user (administrator)
* Expects test to pass
*
* @param mixed $comment Comment object
* @param string action trash, untrash, etc.
* @return void
@@ -68,7 +71,7 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
// Set up a default request
$_POST['id'] = $comment->comment_ID;
$_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_' . $comment->comment_ID );
$_POST[$action] = 1;
$_POST[ $action ] = 1;
$_POST['_total'] = count( self::$comments );
$_POST['_per_page'] = 100;
$_POST['_page'] = 1;
@@ -94,23 +97,24 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
if ( in_array( $action, array( 'trash', 'spam', 'delete' ) ) ) {
$total = $_POST['_total'] - 1;
// unspam, untrash should make the total go up
// unspam, untrash should make the total go up
} elseif ( in_array( $action, array( 'untrash', 'unspam' ) ) ) {
$total = $_POST['_total'] + 1;
}
// The total is calculated based on a page break -OR- a random number. Let's look for both possible outcomes
$comment_count = wp_count_comments( 0 );
$recalc_total = $comment_count->total_comments;
$recalc_total = $comment_count->total_comments;
// Check for either possible total
$message = sprintf( 'returned value: %1$d $total: %2$d $recalc_total: %3$d', (int) $xml->response[0]->comment[0]->supplemental[0]->total[0], $total, $recalc_total );
$this->assertTrue( in_array( (int) $xml->response[0]->comment[0]->supplemental[0]->total[0] , array( $total, $recalc_total ) ), $message );
$message = sprintf( 'returned value: %1$d $total: %2$d $recalc_total: %3$d', (int) $xml->response[0]->comment[0]->supplemental[0]->total[0], $total, $recalc_total );
$this->assertTrue( in_array( (int) $xml->response[0]->comment[0]->supplemental[0]->total[0], array( $total, $recalc_total ) ), $message );
}
/**
* Test as a non-privileged user (subscriber)
* Expects test to fail
*
* @param mixed $comment Comment object
* @param string action trash, untrash, etc.
* @return void
@@ -126,7 +130,7 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
// Set up the $_POST request
$_POST['id'] = $comment->comment_ID;
$_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_' . $comment->comment_ID );
$_POST[$action] = 1;
$_POST[ $action ] = 1;
$_POST['_total'] = count( self::$comments );
$_POST['_per_page'] = 100;
$_POST['_page'] = 1;
@@ -141,6 +145,7 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
/**
* Test with a bad nonce
* Expects test to fail
*
* @param mixed $comment Comment object
* @param string action trash, untrash, etc.
* @return void
@@ -156,7 +161,7 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
// Set up the $_POST request
$_POST['id'] = $comment->comment_ID;
$_POST['_ajax_nonce'] = wp_create_nonce( uniqid() );
$_POST[$action] = 1;
$_POST[ $action ] = 1;
$_POST['_total'] = count( self::$comments );
$_POST['_per_page'] = 100;
$_POST['_page'] = 1;
@@ -170,6 +175,7 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
/**
* Test with a bad id
* Expects test to fail
*
* @param mixed $comment Comment object
* @param string action trash, untrash, etc.
* @return void
@@ -185,7 +191,7 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
// Set up the $_POST request
$_POST['id'] = 12346789;
$_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_12346789' );
$_POST[$action] = 1;
$_POST[ $action ] = 1;
$_POST['_total'] = count( self::$comments );
$_POST['_per_page'] = 100;
$_POST['_page'] = 1;
@@ -206,6 +212,7 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
/**
* Test doubling the action (e.g. trash a trashed comment)
* Expects test to fail
*
* @param mixed $comment Comment object
* @param string action trash, untrash, etc.
* @return void
@@ -221,7 +228,7 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
// Set up the $_POST request
$_POST['id'] = $comment->comment_ID;
$_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_' . $comment->comment_ID );
$_POST[$action] = 1;
$_POST[ $action ] = 1;
$_POST['_total'] = count( self::$comments );
$_POST['_per_page'] = 100;
$_POST['_page'] = 1;
@@ -254,6 +261,7 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
/**
* Delete a comment as an administrator (expects success)
*
* @return void
*/
public function test_ajax_comment_trash_actions_as_administrator() {
@@ -271,6 +279,7 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
/**
* Delete a comment as a subscriber (expects permission denied)
*
* @return void
*/
public function test_ajax_comment_trash_actions_as_subscriber() {
@@ -288,6 +297,7 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
/**
* Delete a comment with no id
*
* @return void
*/
public function test_ajax_trash_comment_no_id() {
@@ -305,6 +315,7 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
/**
* Delete a comment with a bad nonce
*
* @return void
*/
public function test_ajax_trash_comment_bad_nonce() {
@@ -322,6 +333,7 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase {
/**
* Test trashing an already trashed comment, etc.
*
* @return void
*/
public function test_ajax_trash_double_action() {
+4 -4
View File
@@ -146,10 +146,10 @@ class Tests_Ajax_Delete_Plugin extends WP_Ajax_UnitTestCase {
$expected = array(
'success' => true,
'data' => array(
'delete' => 'plugin',
'slug' => 'foo',
'plugin' => 'foo.php',
'pluginName' => '',
'delete' => 'plugin',
'slug' => 'foo',
'plugin' => 'foo.php',
'pluginName' => '',
),
);
+16 -7
View File
@@ -17,6 +17,7 @@ class Tests_Ajax_DimComment extends WP_Ajax_UnitTestCase {
/**
* List of comments
*
* @var array
*/
protected $_comments = array();
@@ -26,7 +27,7 @@ class Tests_Ajax_DimComment extends WP_Ajax_UnitTestCase {
*/
public function setUp() {
parent::setUp();
$post_id = self::factory()->post->create();
$post_id = self::factory()->post->create();
$this->_comments = self::factory()->comment->create_post_comments( $post_id, 15 );
$this->_comments = array_map( 'get_comment', $this->_comments );
}
@@ -35,8 +36,8 @@ class Tests_Ajax_DimComment extends WP_Ajax_UnitTestCase {
* Clear the POST actions in between requests
*/
protected function _clear_post_action() {
unset($_POST['id']);
unset($_POST['new']);
unset( $_POST['id'] );
unset( $_POST['new'] );
$this->_last_response = '';
}
@@ -47,6 +48,7 @@ class Tests_Ajax_DimComment extends WP_Ajax_UnitTestCase {
/**
* Test as a privilged user (administrator)
* Expects test to pass
*
* @param mixed $comment Comment object
* @return void
*/
@@ -67,7 +69,7 @@ class Tests_Ajax_DimComment extends WP_Ajax_UnitTestCase {
$_POST['_url'] = admin_url( 'edit-comments.php' );
// Save the comment status
$prev_status = wp_get_comment_status( $comment->comment_ID );
$prev_status = wp_get_comment_status( $comment->comment_ID );
// Make the request
try {
@@ -87,7 +89,7 @@ class Tests_Ajax_DimComment extends WP_Ajax_UnitTestCase {
// Check the status
$current = wp_get_comment_status( $comment->comment_ID );
if (in_array( $prev_status, array( 'unapproved', 'spam') ) ) {
if ( in_array( $prev_status, array( 'unapproved', 'spam' ) ) ) {
$this->assertEquals( 'approved', $current );
} else {
$this->assertEquals( 'unapproved', $current );
@@ -95,18 +97,19 @@ class Tests_Ajax_DimComment extends WP_Ajax_UnitTestCase {
// The total is calculated based on a page break -OR- a random number. Let's look for both possible outcomes
$comment_count = wp_count_comments( 0 );
$recalc_total = $comment_count->total_comments;
$recalc_total = $comment_count->total_comments;
// Delta is not specified, it will always be 1 lower than the request
$total = $_POST['_total'] - 1;
// Check for either possible total
$this->assertTrue( in_array( (int) $xml->response[0]->comment[0]->supplemental[0]->total[0] , array( $total, $recalc_total ) ) );
$this->assertTrue( in_array( (int) $xml->response[0]->comment[0]->supplemental[0]->total[0], array( $total, $recalc_total ) ) );
}
/**
* Test as a non-privileged user (subscriber)
* Expects test to fail
*
* @param mixed $comment Comment object
* @return void
*/
@@ -134,6 +137,7 @@ class Tests_Ajax_DimComment extends WP_Ajax_UnitTestCase {
/**
* Test with a bad nonce
* Expects test to fail
*
* @param mixed $comment Comment object
* @return void
*/
@@ -161,6 +165,7 @@ class Tests_Ajax_DimComment extends WP_Ajax_UnitTestCase {
/**
* Test with a bad id
* Expects test to fail
*
* @return void
*/
public function test_with_bad_id() {
@@ -200,6 +205,7 @@ class Tests_Ajax_DimComment extends WP_Ajax_UnitTestCase {
/**
* Dim a comment as an administrator (expects success)
*
* @return void
*/
public function test_ajax_comment_dim_actions_as_administrator() {
@@ -210,6 +216,7 @@ class Tests_Ajax_DimComment extends WP_Ajax_UnitTestCase {
/**
* Dim a comment as a subscriber (expects permission denied)
*
* @return void
*/
public function test_ajax_comment_dim_actions_as_subscriber() {
@@ -219,6 +226,7 @@ class Tests_Ajax_DimComment extends WP_Ajax_UnitTestCase {
/**
* Dim a comment with no id
*
* @return void
*/
public function test_ajax_dim_comment_no_id() {
@@ -228,6 +236,7 @@ class Tests_Ajax_DimComment extends WP_Ajax_UnitTestCase {
/**
* Dim a comment with a bad nonce
*
* @return void
*/
public function test_ajax_dim_comment_bad_nonce() {
+29 -16
View File
@@ -17,6 +17,7 @@ class Tests_Ajax_EditComment extends WP_Ajax_UnitTestCase {
/**
* A post with at least one comment
*
* @var mixed
*/
protected $_comment_post = null;
@@ -34,6 +35,7 @@ class Tests_Ajax_EditComment extends WP_Ajax_UnitTestCase {
/**
* Get comments as a privilged user (administrator)
* Expects test to pass
*
* @return void
*/
public function test_as_admin() {
@@ -42,10 +44,12 @@ class Tests_Ajax_EditComment extends WP_Ajax_UnitTestCase {
$this->_setRole( 'administrator' );
// Get a comment
$comments = get_comments( array(
'post_id' => $this->_comment_post->ID
) );
$comment = array_pop( $comments );
$comments = get_comments(
array(
'post_id' => $this->_comment_post->ID,
)
);
$comment = array_pop( $comments );
// Set up a default request
$_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
@@ -84,10 +88,12 @@ class Tests_Ajax_EditComment extends WP_Ajax_UnitTestCase {
$this->_setRole( 'editor' );
// Get a comment
$comments = get_comments( array(
'post_id' => $this->_comment_post->ID
) );
$comment = array_pop( $comments );
$comments = get_comments(
array(
'post_id' => $this->_comment_post->ID,
)
);
$comment = array_pop( $comments );
// Manually update the comment_post_ID, because wp_update_comment() will prevent it.
$wpdb->query( "UPDATE {$wpdb->comments} SET comment_post_ID=0 WHERE comment_ID={$comment->comment_ID}" );
@@ -123,6 +129,7 @@ class Tests_Ajax_EditComment extends WP_Ajax_UnitTestCase {
/**
* Get comments as a non-privileged user (subscriber)
* Expects test to fail
*
* @return void
*/
public function test_as_subscriber() {
@@ -131,10 +138,12 @@ class Tests_Ajax_EditComment extends WP_Ajax_UnitTestCase {
$this->_setRole( 'subscriber' );
// Get a comment
$comments = get_comments( array(
'post_id' => $this->_comment_post->ID
) );
$comment = array_pop( $comments );
$comments = get_comments(
array(
'post_id' => $this->_comment_post->ID,
)
);
$comment = array_pop( $comments );
// Set up a default request
$_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
@@ -149,6 +158,7 @@ class Tests_Ajax_EditComment extends WP_Ajax_UnitTestCase {
/**
* Get comments with a bad nonce
* Expects test to fail
*
* @return void
*/
public function test_bad_nonce() {
@@ -157,10 +167,12 @@ class Tests_Ajax_EditComment extends WP_Ajax_UnitTestCase {
$this->_setRole( 'administrator' );
// Get a comment
$comments = get_comments( array(
'post_id' => $this->_comment_post->ID
) );
$comment = array_pop( $comments );
$comments = get_comments(
array(
'post_id' => $this->_comment_post->ID,
)
);
$comment = array_pop( $comments );
// Set up a default request
$_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( uniqid() );
@@ -175,6 +187,7 @@ class Tests_Ajax_EditComment extends WP_Ajax_UnitTestCase {
/**
* Get comments for an invalid post
* This should return valid XML
*
* @return void
*/
public function test_invalid_comment() {
+9 -2
View File
@@ -17,12 +17,14 @@ class Tests_Ajax_GetComments extends WP_Ajax_UnitTestCase {
/**
* A post with at least one comment
*
* @var mixed
*/
protected static $comment_post = null;
/**
* A post with no comments
*
* @var mixed
*/
protected static $no_comment_post = null;
@@ -30,14 +32,15 @@ class Tests_Ajax_GetComments extends WP_Ajax_UnitTestCase {
protected static $comment_ids = array();
public static function wpSetUpBeforeClass( $factory ) {
self::$comment_post = $factory->post->create_and_get();
self::$comment_ids = $factory->comment->create_post_comments( self::$comment_post->ID, 5 );
self::$comment_post = $factory->post->create_and_get();
self::$comment_ids = $factory->comment->create_post_comments( self::$comment_post->ID, 5 );
self::$no_comment_post = $factory->post->create_and_get();
}
/**
* Get comments as a privilged user (administrator)
* Expects test to pass
*
* @return void
*/
public function test_as_admin() {
@@ -75,6 +78,7 @@ class Tests_Ajax_GetComments extends WP_Ajax_UnitTestCase {
/**
* Get comments as a non-privileged user (subscriber)
* Expects test to fail
*
* @return void
*/
public function test_as_subscriber() {
@@ -95,6 +99,7 @@ class Tests_Ajax_GetComments extends WP_Ajax_UnitTestCase {
/**
* Get comments with a bad nonce
* Expects test to fail
*
* @return void
*/
public function test_bad_nonce() {
@@ -115,6 +120,7 @@ class Tests_Ajax_GetComments extends WP_Ajax_UnitTestCase {
/**
* Get comments for an invalid post
* Bad post IDs are set to 0, this should return valid XML
*
* @return void
*/
public function test_invalid_post() {
@@ -135,6 +141,7 @@ class Tests_Ajax_GetComments extends WP_Ajax_UnitTestCase {
/**
* Get comments for an invalid post
* Bad post IDs are set to 0, this should return valid XML
*
* @return void
*/
public function test_post_with_no_comments() {
+5 -5
View File
@@ -22,9 +22,9 @@ class Tests_Ajax_Manage_Themes extends WP_Ajax_UnitTestCase {
// /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
$GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
add_filter( 'theme_root', array( $this, 'filter_theme_root' ) );
add_filter( 'theme_root', array( $this, 'filter_theme_root' ) );
add_filter( 'stylesheet_root', array( $this, 'filter_theme_root' ) );
add_filter( 'template_root', array( $this, 'filter_theme_root' ) );
add_filter( 'template_root', array( $this, 'filter_theme_root' ) );
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
@@ -32,9 +32,9 @@ class Tests_Ajax_Manage_Themes extends WP_Ajax_UnitTestCase {
function tearDown() {
$GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
remove_filter( 'theme_root', array( $this, 'filter_theme_root' ) );
remove_filter( 'theme_root', array( $this, 'filter_theme_root' ) );
remove_filter( 'stylesheet_root', array( $this, 'filter_theme_root' ) );
remove_filter( 'template_root', array( $this, 'filter_theme_root' ) );
remove_filter( 'template_root', array( $this, 'filter_theme_root' ) );
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
@@ -122,7 +122,7 @@ class Tests_Ajax_Manage_Themes extends WP_Ajax_UnitTestCase {
// Get the response.
$response = json_decode( $this->_last_response, true );
$theme = wp_get_theme( 'twentyten' );
$theme = wp_get_theme( 'twentyten' );
$expected = array(
'success' => false,
'data' => array(
+21 -21
View File
@@ -30,26 +30,26 @@ class Tests_Ajax_MediaEdit extends WP_Ajax_UnitTestCase {
include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
$filename = DIR_TESTDATA . '/images/canola.jpg';
$contents = file_get_contents($filename);
$contents = file_get_contents( $filename );
$upload = wp_upload_bits(basename($filename), null, $contents);
$id = $this->_make_attachment($upload);
$upload = wp_upload_bits( basename( $filename ), null, $contents );
$id = $this->_make_attachment( $upload );
$_REQUEST['action'] = 'image-editor';
$_REQUEST['action'] = 'image-editor';
$_REQUEST['context'] = 'edit-attachment';
$_REQUEST['postid'] = $id;
$_REQUEST['target'] = 'thumbnail';
$_REQUEST['do'] = 'save';
$_REQUEST['postid'] = $id;
$_REQUEST['target'] = 'thumbnail';
$_REQUEST['do'] = 'save';
$_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]';
$media_meta = wp_get_attachment_metadata($id);
$this->assertArrayHasKey('sizes', $media_meta, 'attachment should have size data');
$this->assertArrayHasKey('medium', $media_meta['sizes'], 'attachment should have data for medium size');
$ret = wp_save_image($id);
$media_meta = wp_get_attachment_metadata( $id );
$this->assertArrayHasKey( 'sizes', $media_meta, 'attachment should have size data' );
$this->assertArrayHasKey( 'medium', $media_meta['sizes'], 'attachment should have data for medium size' );
$ret = wp_save_image( $id );
$media_meta = wp_get_attachment_metadata($id);
$this->assertArrayHasKey('sizes', $media_meta, 'cropped attachment should have size data');
$this->assertArrayHasKey('medium', $media_meta['sizes'], 'cropped attachment should have data for medium size');
$media_meta = wp_get_attachment_metadata( $id );
$this->assertArrayHasKey( 'sizes', $media_meta, 'cropped attachment should have size data' );
$this->assertArrayHasKey( 'medium', $media_meta['sizes'], 'cropped attachment should have data for medium size' );
}
/**
@@ -64,26 +64,26 @@ class Tests_Ajax_MediaEdit extends WP_Ajax_UnitTestCase {
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( basename( $filename ), null, $contents );
$id = $this->_make_attachment( $upload );
$id = $this->_make_attachment( $upload );
$_REQUEST['action'] = 'image-editor';
$_REQUEST['action'] = 'image-editor';
$_REQUEST['context'] = 'edit-attachment';
$_REQUEST['postid'] = $id;
$_REQUEST['target'] = 'all';
$_REQUEST['do'] = 'save';
$_REQUEST['postid'] = $id;
$_REQUEST['target'] = 'all';
$_REQUEST['do'] = 'save';
$_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]';
$ret = wp_save_image( $id );
$media_meta = wp_get_attachment_metadata( $id );
$sizes1 = $media_meta['sizes'];
$sizes1 = $media_meta['sizes'];
$_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":189,"h":322}}]';
$ret = wp_save_image( $id );
$media_meta = wp_get_attachment_metadata( $id );
$sizes2 = $media_meta['sizes'];
$sizes2 = $media_meta['sizes'];
$file_path = dirname( get_attached_file( $id ) );
+36 -26
View File
@@ -16,40 +16,50 @@ class Tests_Ajax_QuickEdit extends WP_Ajax_UnitTestCase {
* @ticket 26948
*/
public function test_dont_process_terms_if_taxonomy_does_not_allow_show_on_quick_edit() {
register_taxonomy( 'wptests_tax_1', 'post', array(
'show_in_quick_edit' => false,
'hierarchical' => true,
) );
register_taxonomy( 'wptests_tax_2', 'post', array(
'show_in_quick_edit' => true,
'hierarchical' => true,
) );
register_taxonomy(
'wptests_tax_1', 'post', array(
'show_in_quick_edit' => false,
'hierarchical' => true,
)
);
register_taxonomy(
'wptests_tax_2', 'post', array(
'show_in_quick_edit' => true,
'hierarchical' => true,
)
);
$t1 = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax_1',
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax_2',
) );
$t1 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax_1',
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax_2',
)
);
// Become an administrator.
$this->_setRole( 'administrator' );
$post = self::factory()->post->create_and_get( array(
'post_author' => get_current_user_id(),
) );
$post = self::factory()->post->create_and_get(
array(
'post_author' => get_current_user_id(),
)
);
// Set up a request.
$_POST['_inline_edit'] = wp_create_nonce( 'inlineeditnonce' );
$_POST['post_ID'] = $post->ID;
$_POST['post_type'] = $post->post_type;
$_POST['content'] = $post->post_content;
$_POST['excerpt'] = $post->post_excerpt;
$_POST['_status'] = $post->post_status;
$_POST['post_status'] = $post->post_status;
$_POST['screen'] = 'post';
$_POST['post_view'] = 'excerpt';
$_POST['tax_input'] = array(
$_POST['post_ID'] = $post->ID;
$_POST['post_type'] = $post->post_type;
$_POST['content'] = $post->post_content;
$_POST['excerpt'] = $post->post_excerpt;
$_POST['_status'] = $post->post_status;
$_POST['post_status'] = $post->post_status;
$_POST['screen'] = 'post';
$_POST['post_view'] = 'excerpt';
$_POST['tax_input'] = array(
'wptests_tax_1' => array( $t1 ),
'wptests_tax_2' => array( $t2 ),
);
+32 -16
View File
@@ -17,12 +17,14 @@ class Tests_Ajax_ReplytoComment extends WP_Ajax_UnitTestCase {
/**
* A post with at least one comment
*
* @var mixed
*/
protected static $comment_post = null;
/**
* Draft post
*
* @var mixed
*/
protected static $draft_post = null;
@@ -31,8 +33,8 @@ class Tests_Ajax_ReplytoComment extends WP_Ajax_UnitTestCase {
public static function wpSetUpBeforeClass( $factory ) {
self::$comment_post = $factory->post->create_and_get();
self::$comment_ids = $factory->comment->create_post_comments( self::$comment_post->ID, 5 );
self::$draft_post = $factory->post->create_and_get( array( 'post_status' => 'draft' ) );
self::$comment_ids = $factory->comment->create_post_comments( self::$comment_post->ID, 5 );
self::$draft_post = $factory->post->create_and_get( array( 'post_status' => 'draft' ) );
}
public function tearDown() {
@@ -43,6 +45,7 @@ class Tests_Ajax_ReplytoComment extends WP_Ajax_UnitTestCase {
/**
* Reply as a privilged user (administrator)
* Expects test to pass
*
* @return void
*/
public function test_as_admin() {
@@ -51,10 +54,12 @@ class Tests_Ajax_ReplytoComment extends WP_Ajax_UnitTestCase {
$this->_setRole( 'administrator' );
// Get a comment
$comments = get_comments( array(
'post_id' => self::$comment_post->ID
) );
$comment = array_pop( $comments );
$comments = get_comments(
array(
'post_id' => self::$comment_post->ID,
)
);
$comment = array_pop( $comments );
// Set up a default request
$_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
@@ -87,6 +92,7 @@ class Tests_Ajax_ReplytoComment extends WP_Ajax_UnitTestCase {
/**
* Reply as a non-privileged user (subscriber)
* Expects test to fail
*
* @return void
*/
public function test_as_subscriber() {
@@ -95,10 +101,12 @@ class Tests_Ajax_ReplytoComment extends WP_Ajax_UnitTestCase {
$this->_setRole( 'subscriber' );
// Get a comment
$comments = get_comments( array(
'post_id' => self::$comment_post->ID
) );
$comment = array_pop( $comments );
$comments = get_comments(
array(
'post_id' => self::$comment_post->ID,
)
);
$comment = array_pop( $comments );
// Set up a default request
$_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
@@ -114,6 +122,7 @@ class Tests_Ajax_ReplytoComment extends WP_Ajax_UnitTestCase {
/**
* Reply using a bad nonce
* Expects test to fail
*
* @return void
*/
public function test_bad_nonce() {
@@ -122,10 +131,12 @@ class Tests_Ajax_ReplytoComment extends WP_Ajax_UnitTestCase {
$this->_setRole( 'administrator' );
// Get a comment
$comments = get_comments( array(
'post_id' => self::$comment_post->ID
) );
$comment = array_pop( $comments );
$comments = get_comments(
array(
'post_id' => self::$comment_post->ID,
)
);
$comment = array_pop( $comments );
// Set up a default request
$_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( uniqid() );
@@ -141,6 +152,7 @@ class Tests_Ajax_ReplytoComment extends WP_Ajax_UnitTestCase {
/**
* Reply to an invalid post
* Expects test to fail
*
* @return void
*/
public function test_invalid_post() {
@@ -161,6 +173,7 @@ class Tests_Ajax_ReplytoComment extends WP_Ajax_UnitTestCase {
/**
* Reply to a draft post
* Expects test to fail
*
* @return void
*/
public function test_with_draft_post() {
@@ -181,6 +194,7 @@ class Tests_Ajax_ReplytoComment extends WP_Ajax_UnitTestCase {
/**
* Reply to a post with a simulated database failure
* Expects test to fail
*
* @global $wpdb
* @return void
*/
@@ -204,7 +218,7 @@ class Tests_Ajax_ReplytoComment extends WP_Ajax_UnitTestCase {
$this->_handleAjax( 'replyto-comment' );
$wpdb->suppress_errors( false );
$this->fail();
} catch ( WPAjaxDieStopException $e ) {
} catch ( WPAjaxDieStopException $e ) {
$wpdb->suppress_errors( false );
$this->assertContains( '1', $e->getMessage() );
}
@@ -212,12 +226,13 @@ class Tests_Ajax_ReplytoComment extends WP_Ajax_UnitTestCase {
/**
* Block comments from being saved
*
* @param string $sql
* @return string
*/
public function _block_comments( $sql ) {
global $wpdb;
if ( false !== strpos( $sql, $wpdb->comments ) && 0 === stripos( trim ( $sql ), 'INSERT INTO') ) {
if ( false !== strpos( $sql, $wpdb->comments ) && 0 === stripos( trim( $sql ), 'INSERT INTO' ) ) {
return '';
}
return $sql;
@@ -225,6 +240,7 @@ class Tests_Ajax_ReplytoComment extends WP_Ajax_UnitTestCase {
/**
* Raises WP_Error after Posted a new pre comment
*
* @ticket 39730
* @return void
*/
+6 -1
View File
@@ -11,6 +11,7 @@ class Tests_Ajax_Response extends WP_UnitTestCase {
/**
* Saved error reporting level
*
* @var int
*/
protected $_error_level = 0;
@@ -42,6 +43,7 @@ class Tests_Ajax_Response extends WP_UnitTestCase {
/**
* Return our callback handler
*
* @return callback
*/
public function getDieHandler() {
@@ -51,6 +53,7 @@ class Tests_Ajax_Response extends WP_UnitTestCase {
/**
* Handler for wp_die()
* Don't die, just continue on.
*
* @param string $message
*/
public function dieHandler( $message ) {
@@ -62,13 +65,14 @@ class Tests_Ajax_Response extends WP_UnitTestCase {
* xdebug_get_headers if it's available
* Needs a separate process to get around the headers/output from the
* bootstrapper
*
* @ticket 19448
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_response_charset_in_header() {
if ( !function_exists( 'xdebug_get_headers' ) ) {
if ( ! function_exists( 'xdebug_get_headers' ) ) {
$this->markTestSkipped( 'xdebug is required for this test' );
}
@@ -86,6 +90,7 @@ class Tests_Ajax_Response extends WP_UnitTestCase {
/**
* Test that charset in the xml tag matches blog_charset
*
* @ticket 19448
*/
public function test_response_charset_in_xml() {
+8 -2
View File
@@ -17,10 +17,16 @@ class Tests_Ajax_TagSearch extends WP_Ajax_UnitTestCase {
/**
* List of terms to insert on setup
*
* @var array
*/
private static $terms = array(
'chattels', 'depo', 'energumen', 'figuriste', 'habergeon', 'impropriation'
'chattels',
'depo',
'energumen',
'figuriste',
'habergeon',
'impropriation',
);
private static $term_ids = array();
@@ -64,7 +70,7 @@ class Tests_Ajax_TagSearch extends WP_Ajax_UnitTestCase {
// Set up a default request
$_GET['tax'] = 'post_tag';
$_GET['q'] = md5(uniqid());
$_GET['q'] = md5( uniqid() );
// Make the request
// No output, so we get a stop exception
+18 -15
View File
@@ -24,29 +24,32 @@ class Tests_Attachment_Slashes extends WP_UnitTestCase {
/**
* Tests the model function that expects slashed data
*
*/
function test_wp_insert_attachment() {
$id = wp_insert_attachment(array(
'post_status' => 'publish',
'post_title' => $this->slash_1,
'post_content_filtered' => $this->slash_3,
'post_excerpt' => $this->slash_5,
'post_type' => 'post'
));
$id = wp_insert_attachment(
array(
'post_status' => 'publish',
'post_title' => $this->slash_1,
'post_content_filtered' => $this->slash_3,
'post_excerpt' => $this->slash_5,
'post_type' => 'post',
)
);
$post = get_post( $id );
$this->assertEquals( wp_unslash( $this->slash_1 ), $post->post_title );
$this->assertEquals( wp_unslash( $this->slash_3 ), $post->post_content_filtered );
$this->assertEquals( wp_unslash( $this->slash_5 ), $post->post_excerpt );
$id = wp_insert_attachment(array(
'post_status' => 'publish',
'post_title' => $this->slash_2,
'post_content_filtered' => $this->slash_4,
'post_excerpt' => $this->slash_6,
'post_type' => 'post'
));
$id = wp_insert_attachment(
array(
'post_status' => 'publish',
'post_title' => $this->slash_2,
'post_content_filtered' => $this->slash_4,
'post_excerpt' => $this->slash_6,
'post_type' => 'post',
)
);
$post = get_post( $id );
$this->assertEquals( wp_unslash( $this->slash_2 ), $post->post_title );
+44 -34
View File
@@ -16,9 +16,11 @@ class Tests_Auth extends WP_UnitTestCase {
protected $nonce_failure_hook = 'wp_verify_nonce_failed';
public static function wpSetUpBeforeClass( $factory ) {
self::$_user = $factory->user->create_and_get( array(
'user_login' => 'password-tests'
) );
self::$_user = $factory->user->create_and_get(
array(
'user_login' => 'password-tests',
)
);
self::$user_id = self::$_user->ID;
@@ -48,9 +50,9 @@ class Tests_Auth extends WP_UnitTestCase {
$cookie = wp_generate_auth_cookie( self::$user_id, time() + 3600, 'auth' );
$this->assertEquals( false, wp_validate_auth_cookie( $cookie, 'logged_in' ), 'wrong auth scheme' );
$cookie = wp_generate_auth_cookie( self::$user_id, time() + 3600, 'auth' );
list($a, $b, $c) = explode('|', $cookie);
$cookie = $a . '|' . ($b + 1) . '|' . $c;
$cookie = wp_generate_auth_cookie( self::$user_id, time() + 3600, 'auth' );
list($a, $b, $c) = explode( '|', $cookie );
$cookie = $a . '|' . ( $b + 1 ) . '|' . $c;
$this->assertEquals( false, wp_validate_auth_cookie( self::$user_id, 'auth' ), 'altered cookie' );
}
@@ -75,7 +77,7 @@ class Tests_Auth extends WP_UnitTestCase {
' a password with trailing and leading spaces ',
);
foreach( $passwords_to_test as $password_to_test ) {
foreach ( $passwords_to_test as $password_to_test ) {
wp_set_password( $password_to_test, $this->user->ID );
$authed_user = wp_authenticate( $this->user->user_login, $password_to_test );
@@ -157,7 +159,7 @@ class Tests_Auth extends WP_UnitTestCase {
// A valid nonce needs to be set so the check doesn't die()
$_REQUEST['_wpnonce'] = wp_create_nonce( -1 );
$result = check_admin_referer();
$result = check_admin_referer();
$this->assertSame( 1, $result );
unset( $_REQUEST['_wpnonce'] );
@@ -171,7 +173,7 @@ class Tests_Auth extends WP_UnitTestCase {
// A valid nonce needs to be set so the check doesn't die()
$_REQUEST['_wpnonce'] = wp_create_nonce( -1 );
$result = check_ajax_referer();
$result = check_ajax_referer();
$this->assertSame( 1, $result );
unset( $_REQUEST['_wpnonce'] );
@@ -230,12 +232,14 @@ class Tests_Auth extends WP_UnitTestCase {
function test_user_activation_key_is_checked() {
global $wpdb;
$key = wp_generate_password( 20, false );
$wpdb->update( $wpdb->users, array(
'user_activation_key' => strtotime( '-1 hour' ) . ':' . self::$wp_hasher->HashPassword( $key ),
), array(
'ID' => $this->user->ID,
) );
$key = wp_generate_password( 20, false );
$wpdb->update(
$wpdb->users, array(
'user_activation_key' => strtotime( '-1 hour' ) . ':' . self::$wp_hasher->HashPassword( $key ),
), array(
'ID' => $this->user->ID,
)
);
// A valid key should be accepted
$check = check_password_reset_key( $key, $this->user->user_login );
@@ -253,7 +257,7 @@ class Tests_Auth extends WP_UnitTestCase {
// A truncated key should be rejected
$partial = substr( $key, 0, 10 );
$check = check_password_reset_key( $partial, $this->user->user_login );
$check = check_password_reset_key( $partial, $this->user->user_login );
$this->assertInstanceOf( 'WP_Error', $check );
}
@@ -263,12 +267,14 @@ class Tests_Auth extends WP_UnitTestCase {
function test_expired_user_activation_key_is_rejected() {
global $wpdb;
$key = wp_generate_password( 20, false );
$wpdb->update( $wpdb->users, array(
'user_activation_key' => strtotime( '-48 hours' ) . ':' . self::$wp_hasher->HashPassword( $key ),
), array(
'ID' => $this->user->ID,
) );
$key = wp_generate_password( 20, false );
$wpdb->update(
$wpdb->users, array(
'user_activation_key' => strtotime( '-48 hours' ) . ':' . self::$wp_hasher->HashPassword( $key ),
), array(
'ID' => $this->user->ID,
)
);
// An expired but otherwise valid key should be rejected
$check = check_password_reset_key( $key, $this->user->user_login );
@@ -296,12 +302,14 @@ class Tests_Auth extends WP_UnitTestCase {
// A legacy user_activation_key is one without the `time()` prefix introduced in WordPress 4.3.
$key = wp_generate_password( 20, false );
$wpdb->update( $wpdb->users, array(
'user_activation_key' => self::$wp_hasher->HashPassword( $key ),
), array(
'ID' => $this->user->ID,
) );
$key = wp_generate_password( 20, false );
$wpdb->update(
$wpdb->users, array(
'user_activation_key' => self::$wp_hasher->HashPassword( $key ),
), array(
'ID' => $this->user->ID,
)
);
// A legacy user_activation_key should not be accepted
$check = check_password_reset_key( $key, $this->user->user_login );
@@ -321,12 +329,14 @@ class Tests_Auth extends WP_UnitTestCase {
// A plaintext user_activation_key is one stored before hashing was introduced in WordPress 3.7.
$key = wp_generate_password( 20, false );
$wpdb->update( $wpdb->users, array(
'user_activation_key' => $key,
), array(
'ID' => $this->user->ID,
) );
$key = wp_generate_password( 20, false );
$wpdb->update(
$wpdb->users, array(
'user_activation_key' => $key,
), array(
'ID' => $this->user->ID,
)
);
// A plaintext user_activation_key should not allow an otherwise valid key to be accepted
$check = check_password_reset_key( $key, $this->user->user_login );
+27 -16
View File
@@ -22,7 +22,7 @@ class Tests_Avatar extends WP_UnitTestCase {
$this->assertEquals( preg_match( '|\?.*s=96|', $url ), 1 );
$args = array( 'size' => 100 );
$url = get_avatar_url( 1, $args );
$url = get_avatar_url( 1, $args );
$this->assertEquals( preg_match( '|\?.*s=100|', $url ), 1 );
}
@@ -34,12 +34,12 @@ class Tests_Avatar extends WP_UnitTestCase {
$this->assertEquals( preg_match( '|\?.*d=mm|', $url ), 1 );
$args = array( 'default' => 'wavatar' );
$url = get_avatar_url( 1, $args );
$url = get_avatar_url( 1, $args );
$this->assertEquals( preg_match( '|\?.*d=wavatar|', $url ), 1 );
$this->assertEquals( preg_match( '|\?.*f=y|', $url ), 0 );
$args = array( 'force_default' => true );
$url = get_avatar_url( 1, $args );
$url = get_avatar_url( 1, $args );
$this->assertEquals( preg_match( '|\?.*f=y|', $url ), 1 );
}
@@ -51,7 +51,7 @@ class Tests_Avatar extends WP_UnitTestCase {
$this->assertEquals( preg_match( '|\?.*r=g|', $url ), 1 );
$args = array( 'rating' => 'M' );
$url = get_avatar_url( 1, $args );
$url = get_avatar_url( 1, $args );
$this->assertEquals( preg_match( '|\?.*r=m|', $url ), 1 );
}
@@ -63,11 +63,11 @@ class Tests_Avatar extends WP_UnitTestCase {
$this->assertEquals( preg_match( '|^http://|', $url ), 1 );
$args = array( 'scheme' => 'https' );
$url = get_avatar_url( 1, $args );
$url = get_avatar_url( 1, $args );
$this->assertEquals( preg_match( '|^https://|', $url ), 1 );
$args = array( 'scheme' => 'lolcat' );
$url = get_avatar_url( 1, $args );
$url = get_avatar_url( 1, $args );
$this->assertEquals( preg_match( '|^lolcat://|', $url ), 0 );
}
@@ -88,13 +88,18 @@ class Tests_Avatar extends WP_UnitTestCase {
$this->assertEquals( $url, $url2 );
$post_id = self::factory()->post->create( array( 'post_author' => 1 ) );
$post = get_post( $post_id );
$url2 = get_avatar_url( $post );
$post = get_post( $post_id );
$url2 = get_avatar_url( $post );
$this->assertEquals( $url, $url2 );
$comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id, 'user_id' => 1 ) );
$comment = get_comment( $comment_id );
$url2 = get_avatar_url( $comment );
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'user_id' => 1,
)
);
$comment = get_comment( $comment_id );
$url2 = get_avatar_url( $comment );
$this->assertEquals( $url, $url2 );
}
@@ -138,9 +143,15 @@ class Tests_Avatar extends WP_UnitTestCase {
public function test_get_avatar_comment_types_filter() {
$url = get_avatar_url( 1 );
$post_id = self::factory()->post->create( array( 'post_author' => 1 ) );
$comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id, 'user_id' => 1, 'comment_type' => 'pingback' ) );
$comment = get_comment( $comment_id );
$post_id = self::factory()->post->create( array( 'post_author' => 1 ) );
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'user_id' => 1,
'comment_type' => 'pingback',
)
);
$comment = get_comment( $comment_id );
$url2 = get_avatar_url( $comment );
$this->assertFalse( $url2 );
@@ -163,7 +174,7 @@ class Tests_Avatar extends WP_UnitTestCase {
public function test_get_avatar_size() {
$size = '100';
$img = get_avatar( 1, $size );
$img = get_avatar( 1, $size );
$this->assertEquals( preg_match( "|^<img .*height='$size'.*width='$size'|", $img ), 1 );
}
@@ -175,7 +186,7 @@ class Tests_Avatar extends WP_UnitTestCase {
public function test_get_avatar_class() {
$class = 'first';
$img = get_avatar( 1, 96, '', '', array( 'class' => $class ) );
$img = get_avatar( 1, 96, '', '', array( 'class' => $class ) );
$this->assertEquals( preg_match( "|^<img .*class='[^']*{$class}[^']*'|", $img ), 1 );
}
+28 -23
View File
@@ -18,8 +18,8 @@ class Tests_Basic extends WP_UnitTestCase {
}
function test_package_json() {
$package_json = file_get_contents( dirname( ABSPATH ) . '/package.json' );
$package_json = json_decode( $package_json, true );
$package_json = file_get_contents( dirname( ABSPATH ) . '/package.json' );
$package_json = json_decode( $package_json, true );
list( $version ) = explode( '-', $GLOBALS['wp_version'] );
// package.json uses x.y.z, so fill cleaned $wp_version for .0 releases
if ( 1 == substr_count( $version, '.' ) ) {
@@ -37,35 +37,40 @@ class Tests_Basic extends WP_UnitTestCase {
$this->assertArrayHasKey( 'node', $package_json['engines'] );
$node = $package_json['engines']['node'];
$this->assertRegExp( '~^=?\d+\.\d+\.\d+$~', $node, "package.json's node version cannot be a range." );
}
}
// two tests for a lame bug in PHPUnit that broke the $GLOBALS reference
function test_globals() {
global $test_foo;
$test_foo = array('foo', 'bar', 'baz');
$test_foo = array( 'foo', 'bar', 'baz' );
function test_globals_foo() {
unset($GLOBALS['test_foo'][1]);
unset( $GLOBALS['test_foo'][1] );
}
test_globals_foo();
$this->assertEquals($test_foo, array(0=>'foo', 2=>'baz'));
$this->assertEquals($test_foo, $GLOBALS['test_foo']);
$this->assertEquals(
$test_foo, array(
0 => 'foo',
2 => 'baz',
)
);
$this->assertEquals( $test_foo, $GLOBALS['test_foo'] );
}
function test_globals_bar() {
global $test_bar;
$test_bar = array('a', 'b', 'c');
$this->assertEquals($test_bar, $GLOBALS['test_bar']);
$test_bar = array( 'a', 'b', 'c' );
$this->assertEquals( $test_bar, $GLOBALS['test_bar'] );
}
// test some helper utility functions
function test_strip_ws() {
$this->assertEquals('', strip_ws(''));
$this->assertEquals('foo', strip_ws('foo'));
$this->assertEquals('', strip_ws("\r\n\t \n\r\t"));
$this->assertEquals( '', strip_ws( '' ) );
$this->assertEquals( 'foo', strip_ws( 'foo' ) );
$this->assertEquals( '', strip_ws( "\r\n\t \n\r\t" ) );
$in = "asdf\n";
$in .= "asdf asdf\n";
@@ -83,9 +88,9 @@ class Tests_Basic extends WP_UnitTestCase {
$expected .= "asdf\n";
$expected .= "asdf\n";
$expected .= "foo bar\n";
$expected .= "foo";
$expected .= 'foo';
$this->assertEquals($expected, strip_ws($in));
$this->assertEquals( $expected, strip_ws( $in ) );
}
@@ -103,20 +108,20 @@ EOF;
<p>If a new user is created by WordPress, the password will be set, by default, to "changeme". Quite suggestive, eh? ;)</p>
<ol id="authors"><form action="?import=wordpress&amp;step=2&amp;id=" method="post"><input type="hidden" name="_wpnonce" value="***" /><input type="hidden" name="_wp_http_referer" value="wp-test.php" /><li>Current author: <strong>Alex Shiels</strong><br />Create user <input type="text" value="Alex Shiels" name="user[]" maxlength="30"> <br /> or map to existing<select name="userselect[0]">
EOF;
$this->assertEquals($expected, mask_input_value($in));
$this->assertEquals( $expected, mask_input_value( $in ) );
}
/**
* @ticket 17884
*/
function test_setting_nonexistent_arrays() {
$page = 1;
$page = 1;
$field = 'settings';
$empty_array[$page][$field] = 'foo';
$empty_array[ $page ][ $field ] = 'foo';
// Assertion not strictly needed; we mainly want to show that a notice is not thrown.
unset( $empty_array[$page]['bar']['baz'] );
unset( $empty_array[ $page ]['bar']['baz'] );
$this->assertFalse( isset( $empty_array[ $page ]['bar']['baz'] ) );
}
@@ -166,11 +171,11 @@ EOF;
function _switch_order_helper( $var ) {
$return = 'no match';
switch ( $var ) {
default:
break;
case 1:
$return = 'match';
break;
default:
break;
case 1:
$return = 'match';
break;
}
return $return;
+36 -22
View File
@@ -9,15 +9,19 @@ class Tests_Bookmark_GetBookmarks extends WP_UnitTestCase {
$bookmarks = self::factory()->bookmark->create_many( 2 );
$found1 = get_bookmarks( array(
'orderby' => 'link_id',
) );
$found1 = get_bookmarks(
array(
'orderby' => 'link_id',
)
);
$num_queries = $wpdb->num_queries;
$found2 = get_bookmarks( array(
'orderby' => 'link_id',
) );
$found2 = get_bookmarks(
array(
'orderby' => 'link_id',
)
);
$this->assertEqualSets( $found1, $found2 );
$this->assertSame( $num_queries, $wpdb->num_queries );
@@ -29,20 +33,26 @@ class Tests_Bookmark_GetBookmarks extends WP_UnitTestCase {
$bookmarks = self::factory()->bookmark->create_many( 2 );
// Prime cache.
$found1 = get_bookmarks( array(
'orderby' => 'link_id',
) );
$found1 = get_bookmarks(
array(
'orderby' => 'link_id',
)
);
$num_queries = $wpdb->num_queries;
$bookmarks[] = wp_insert_link( array(
'link_name' => 'foo',
'link_url' => 'http://example.com',
) );
$bookmarks[] = wp_insert_link(
array(
'link_name' => 'foo',
'link_url' => 'http://example.com',
)
);
$found2 = get_bookmarks( array(
'orderby' => 'link_id',
) );
$found2 = get_bookmarks(
array(
'orderby' => 'link_id',
)
);
$this->assertEqualSets( $bookmarks, wp_list_pluck( $found2, 'link_id' ) );
$this->assertTrue( $num_queries < $wpdb->num_queries );
@@ -56,15 +66,19 @@ class Tests_Bookmark_GetBookmarks extends WP_UnitTestCase {
$bookmarks = self::factory()->bookmark->create_many( 2 );
$found1 = get_bookmarks( array(
'orderby' => 'rand',
) );
$found1 = get_bookmarks(
array(
'orderby' => 'rand',
)
);
$num_queries = $wpdb->num_queries;
$found2 = get_bookmarks( array(
'orderby' => 'rand',
) );
$found2 = get_bookmarks(
array(
'orderby' => 'rand',
)
);
// equal sets != same order
$this->assertEqualSets( $found1, $found2 );
+44 -42
View File
@@ -21,21 +21,21 @@ class Tests_Cache extends WP_UnitTestCase {
function &init_cache() {
global $wp_object_cache;
$cache_class = get_class( $wp_object_cache );
$cache = new $cache_class();
$cache = new $cache_class();
$cache->add_global_groups( array( 'global-cache-test', 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );
return $cache;
}
function test_miss() {
$this->assertEquals( NULL, $this->cache->get( 'test_miss' ) );
$this->assertEquals( null, $this->cache->get( 'test_miss' ) );
}
function test_add_get() {
$key = __FUNCTION__;
$val = 'val';
$this->cache->add($key, $val);
$this->assertEquals($val, $this->cache->get($key));
$this->cache->add( $key, $val );
$this->assertEquals( $val, $this->cache->get( $key ) );
}
function test_add_get_0() {
@@ -43,94 +43,95 @@ class Tests_Cache extends WP_UnitTestCase {
$val = 0;
// you can store zero in the cache
$this->cache->add($key, $val);
$this->assertEquals($val, $this->cache->get($key));
$this->cache->add( $key, $val );
$this->assertEquals( $val, $this->cache->get( $key ) );
}
function test_add_get_null() {
$key = __FUNCTION__;
$val = null;
$this->assertTrue( $this->cache->add($key, $val) );
$this->assertTrue( $this->cache->add( $key, $val ) );
// null is converted to empty string
$this->assertEquals( '', $this->cache->get($key) );
$this->assertEquals( '', $this->cache->get( $key ) );
}
function test_add() {
$key = __FUNCTION__;
$key = __FUNCTION__;
$val1 = 'val1';
$val2 = 'val2';
// add $key to the cache
$this->assertTrue($this->cache->add($key, $val1));
$this->assertEquals($val1, $this->cache->get($key));
$this->assertTrue( $this->cache->add( $key, $val1 ) );
$this->assertEquals( $val1, $this->cache->get( $key ) );
// $key is in the cache, so reject new calls to add()
$this->assertFalse($this->cache->add($key, $val2));
$this->assertEquals($val1, $this->cache->get($key));
$this->assertFalse( $this->cache->add( $key, $val2 ) );
$this->assertEquals( $val1, $this->cache->get( $key ) );
}
function test_replace() {
$key = __FUNCTION__;
$val = 'val1';
$key = __FUNCTION__;
$val = 'val1';
$val2 = 'val2';
// memcached rejects replace() if the key does not exist
$this->assertFalse($this->cache->replace($key, $val));
$this->assertFalse($this->cache->get($key));
$this->assertTrue($this->cache->add($key, $val));
$this->assertEquals($val, $this->cache->get($key));
$this->assertTrue($this->cache->replace($key, $val2));
$this->assertEquals($val2, $this->cache->get($key));
$this->assertFalse( $this->cache->replace( $key, $val ) );
$this->assertFalse( $this->cache->get( $key ) );
$this->assertTrue( $this->cache->add( $key, $val ) );
$this->assertEquals( $val, $this->cache->get( $key ) );
$this->assertTrue( $this->cache->replace( $key, $val2 ) );
$this->assertEquals( $val2, $this->cache->get( $key ) );
}
function test_set() {
$key = __FUNCTION__;
$key = __FUNCTION__;
$val1 = 'val1';
$val2 = 'val2';
// memcached accepts set() if the key does not exist
$this->assertTrue($this->cache->set($key, $val1));
$this->assertEquals($val1, $this->cache->get($key));
$this->assertTrue( $this->cache->set( $key, $val1 ) );
$this->assertEquals( $val1, $this->cache->get( $key ) );
// Second set() with same key should be allowed
$this->assertTrue($this->cache->set($key, $val2));
$this->assertEquals($val2, $this->cache->get($key));
$this->assertTrue( $this->cache->set( $key, $val2 ) );
$this->assertEquals( $val2, $this->cache->get( $key ) );
}
function test_flush() {
global $_wp_using_ext_object_cache;
if ( $_wp_using_ext_object_cache )
if ( $_wp_using_ext_object_cache ) {
return;
}
$key = __FUNCTION__;
$val = 'val';
$this->cache->add($key, $val);
$this->cache->add( $key, $val );
// item is visible to both cache objects
$this->assertEquals($val, $this->cache->get($key));
$this->assertEquals( $val, $this->cache->get( $key ) );
$this->cache->flush();
// If there is no value get returns false.
$this->assertFalse($this->cache->get($key));
$this->assertFalse( $this->cache->get( $key ) );
}
// Make sure objects are cloned going to and from the cache
function test_object_refs() {
$key = __FUNCTION__ . '_1';
$object_a = new stdClass;
$key = __FUNCTION__ . '_1';
$object_a = new stdClass;
$object_a->foo = 'alpha';
$this->cache->set( $key, $object_a );
$object_a->foo = 'bravo';
$object_b = $this->cache->get( $key );
$object_b = $this->cache->get( $key );
$this->assertEquals( 'alpha', $object_b->foo );
$object_b->foo = 'charlie';
$this->assertEquals( 'bravo', $object_a->foo );
$key = __FUNCTION__ . '_2';
$object_a = new stdClass;
$key = __FUNCTION__ . '_2';
$object_a = new stdClass;
$object_a->foo = 'alpha';
$this->cache->add( $key, $object_a );
$object_a->foo = 'bravo';
$object_b = $this->cache->get( $key );
$object_b = $this->cache->get( $key );
$this->assertEquals( 'alpha', $object_b->foo );
$object_b->foo = 'charlie';
$this->assertEquals( 'bravo', $object_a->foo );
@@ -211,7 +212,7 @@ class Tests_Cache extends WP_UnitTestCase {
$this->assertTrue( $this->cache->delete( $key ) );
$this->assertFalse( $this->cache->get( $key ) );
$this->assertFalse( $this->cache->delete( $key, 'default') );
$this->assertFalse( $this->cache->delete( $key, 'default' ) );
}
function test_wp_cache_delete() {
@@ -230,15 +231,16 @@ class Tests_Cache extends WP_UnitTestCase {
// Delete returns (bool) true when key is not set and $force is true
// $this->assertTrue( wp_cache_delete( $key, 'default', true ) );
$this->assertFalse( wp_cache_delete( $key, 'default') );
$this->assertFalse( wp_cache_delete( $key, 'default' ) );
}
function test_switch_to_blog() {
if ( ! method_exists( $this->cache, 'switch_to_blog' ) )
if ( ! method_exists( $this->cache, 'switch_to_blog' ) ) {
return;
}
$key = __FUNCTION__;
$val = 'val1';
$key = __FUNCTION__;
$val = 'val1';
$val2 = 'val2';
if ( ! is_multisite() ) {
@@ -286,7 +288,7 @@ class Tests_Cache extends WP_UnitTestCase {
if ( wp_using_ext_object_cache() ) {
// External caches will contain property values that contain non-matching resource IDs
$this->assertInstanceOf( 'WP_Object_Cache', $wp_object_cache );
$this->assertInstanceOf( 'WP_Object_Cache', $wp_object_cache );
} else {
$this->assertEquals( $wp_object_cache, $new_blank_cache_object );
}
+113 -39
View File
@@ -21,10 +21,12 @@ class Tests_Canonical extends WP_Canonical_UnitTestCase {
function test_canonical( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
if ( false !== strpos( $test_url, '%d' ) ) {
if ( false !== strpos( $test_url, '/?author=%d' ) )
if ( false !== strpos( $test_url, '/?author=%d' ) ) {
$test_url = sprintf( $test_url, self::$author_id );
if ( false !== strpos( $test_url, '?cat=%d' ) )
}
if ( false !== strpos( $test_url, '?cat=%d' ) ) {
$test_url = sprintf( $test_url, self::$terms[ $expected['url'] ] );
}
}
$this->assertCanonical( $test_url, $expected, $ticket, $expected_doing_it_wrong );
@@ -48,44 +50,116 @@ class Tests_Canonical extends WP_Canonical_UnitTestCase {
array( '?cat=%d', array( 'url' => '/category/parent/' ), 15256 ),
array( '?cat=%d', array( 'url' => '/category/parent/child-1/' ), 15256 ),
array( '?cat=%d', array( 'url' => '/category/parent/child-1/child-2/' ) ), // no children
array( '/category/uncategorized/', array( 'url' => '/category/uncategorized/', 'qv' => array( 'category_name' => 'uncategorized' ) ) ),
array( '/category/uncategorized/page/2/', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ),
array( '/category/uncategorized/?paged=2', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ),
array( '/category/uncategorized/?paged=2&category_name=uncategorized', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ), 17174 ),
array(
'/category/uncategorized/',
array(
'url' => '/category/uncategorized/',
'qv' => array( 'category_name' => 'uncategorized' ),
),
),
array(
'/category/uncategorized/page/2/',
array(
'url' => '/category/uncategorized/page/2/',
'qv' => array(
'category_name' => 'uncategorized',
'paged' => 2,
),
),
),
array(
'/category/uncategorized/?paged=2',
array(
'url' => '/category/uncategorized/page/2/',
'qv' => array(
'category_name' => 'uncategorized',
'paged' => 2,
),
),
),
array(
'/category/uncategorized/?paged=2&category_name=uncategorized',
array(
'url' => '/category/uncategorized/page/2/',
'qv' => array(
'category_name' => 'uncategorized',
'paged' => 2,
),
),
17174,
),
// Categories & Intersections with other vars
array( '/category/uncategorized/?tag=post-formats', array( 'url' => '/category/uncategorized/?tag=post-formats', 'qv' => array('category_name' => 'uncategorized', 'tag' => 'post-formats') ) ),
array( '/?category_name=cat-a,cat-b', array( 'url' => '/?category_name=cat-a,cat-b', 'qv' => array( 'category_name' => 'cat-a,cat-b' ) ) ),
array(
'/category/uncategorized/?tag=post-formats',
array(
'url' => '/category/uncategorized/?tag=post-formats',
'qv' => array(
'category_name' => 'uncategorized',
'tag' => 'post-formats',
),
),
),
array(
'/?category_name=cat-a,cat-b',
array(
'url' => '/?category_name=cat-a,cat-b',
'qv' => array( 'category_name' => 'cat-a,cat-b' ),
),
),
// Taxonomies with extra Query Vars
array( '/category/cat-a/page/1/?test=one%20two', '/category/cat-a/?test=one%20two', 18086), // Extra query vars should stay encoded
array( '/category/cat-a/page/1/?test=one%20two', '/category/cat-a/?test=one%20two', 18086 ), // Extra query vars should stay encoded
// Categories with Dates
array( '/2008/04/?cat=1', array( 'url' => '/2008/04/?cat=1', 'qv' => array('cat' => '1', 'year' => '2008', 'monthnum' => '04' ) ), 17661 ),
// array( '/2008/?category_name=cat-a', array( 'url' => '/2008/?category_name=cat-a', 'qv' => array('category_name' => 'cat-a', 'year' => '2008' ) ) ),
array(
'/2008/04/?cat=1',
array(
'url' => '/2008/04/?cat=1',
'qv' => array(
'cat' => '1',
'year' => '2008',
'monthnum' => '04',
),
),
17661,
),
// array( '/2008/?category_name=cat-a', array( 'url' => '/2008/?category_name=cat-a', 'qv' => array('category_name' => 'cat-a', 'year' => '2008' ) ) ),
// Pages
array( '/child-page-1/', '/parent-page/child-page-1/'),
array( '/?page_id=144', '/parent-page/child-page-1/'),
// Pages
array( '/child-page-1/', '/parent-page/child-page-1/' ),
array( '/?page_id=144', '/parent-page/child-page-1/' ),
array( '/abo', '/about/' ),
array( '/parent/child1/grandchild/', '/parent/child1/grandchild/' ),
array( '/parent/child2/grandchild/', '/parent/child2/grandchild/' ),
// Posts
array( '?p=587', '/2008/06/02/post-format-test-audio/'),
array( '/?name=images-test', '/2008/09/03/images-test/'),
array( '?p=587', '/2008/06/02/post-format-test-audio/' ),
array( '/?name=images-test', '/2008/09/03/images-test/' ),
// Incomplete slug should resolve and remove the ?name= parameter
array( '/?name=images-te', '/2008/09/03/images-test/', 20374),
array( '/?name=images-te', '/2008/09/03/images-test/', 20374 ),
// Page slug should resolve to post slug and remove the ?pagename= parameter
array( '/?pagename=images-test', '/2008/09/03/images-test/', 20374),
array( '/?pagename=images-test', '/2008/09/03/images-test/', 20374 ),
array( '/2008/06/02/post-format-test-au/', '/2008/06/02/post-format-test-audio/'),
array( '/2008/06/post-format-test-au/', '/2008/06/02/post-format-test-audio/'),
array( '/2008/post-format-test-au/', '/2008/06/02/post-format-test-audio/'),
array( '/2010/post-format-test-au/', '/2008/06/02/post-format-test-audio/'), // A Year the post is not in
array( '/post-format-test-au/', '/2008/06/02/post-format-test-audio/'),
array( '/2008/06/02/post-format-test-au/', '/2008/06/02/post-format-test-audio/' ),
array( '/2008/06/post-format-test-au/', '/2008/06/02/post-format-test-audio/' ),
array( '/2008/post-format-test-au/', '/2008/06/02/post-format-test-audio/' ),
array( '/2010/post-format-test-au/', '/2008/06/02/post-format-test-audio/' ), // A Year the post is not in
array( '/post-format-test-au/', '/2008/06/02/post-format-test-audio/' ),
array( '/2008/09/03/images-test/3/', array( 'url' => '/2008/09/03/images-test/3/', 'qv' => array( 'name' => 'images-test', 'year' => '2008', 'monthnum' => '09', 'day' => '03', 'page' => '3' ) ) ),
array(
'/2008/09/03/images-test/3/',
array(
'url' => '/2008/09/03/images-test/3/',
'qv' => array(
'name' => 'images-test',
'year' => '2008',
'monthnum' => '09',
'day' => '03',
'page' => '3',
),
),
),
array( '/2008/09/03/images-test/?page=3', '/2008/09/03/images-test/3/' ),
array( '/2008/09/03/images-te?page=3', '/2008/09/03/images-test/3/' ),
@@ -98,32 +172,32 @@ class Tests_Canonical extends WP_Canonical_UnitTestCase {
// Dates
array( '/?m=2008', '/2008/' ),
array( '/?m=200809', '/2008/09/'),
array( '/?m=20080905', '/2008/09/05/'),
array( '/?m=200809', '/2008/09/' ),
array( '/?m=20080905', '/2008/09/05/' ),
array( '/2008/?day=05', '/2008/?day=05'), // no redirect
array( '/2008/09/?day=05', '/2008/09/05/'),
array( '/2008/?monthnum=9', '/2008/09/'),
array( '/2008/?day=05', '/2008/?day=05' ), // no redirect
array( '/2008/09/?day=05', '/2008/09/05/' ),
array( '/2008/?monthnum=9', '/2008/09/' ),
array( '/?year=2008', '/2008/'),
array( '/?year=2008', '/2008/' ),
array( '/2012/13/', '/2012/'),
array( '/2012/13/', '/2012/' ),
array( '/2012/11/51/', '/2012/11/', 0, array( 'WP_Date_Query' ) ),
// Authors
array( '/?author=%d', '/author/canonical-author/' ),
// array( '/?author=%d&year=2008', '/2008/?author=3'),
// array( '/author/canonical-author/?year=2008', '/2008/?author=3'), //Either or, see previous testcase.
// array( '/?author=%d&year=2008', '/2008/?author=3'),
// array( '/author/canonical-author/?year=2008', '/2008/?author=3'), //Either or, see previous testcase.
// Feeds
array( '/?feed=atom', '/feed/atom/' ),
// Feeds
array( '/?feed=atom', '/feed/atom/' ),
array( '/?feed=rss2', '/feed/' ),
array( '/?feed=comments-rss2', '/comments/feed/'),
array( '/?feed=comments-atom', '/comments/feed/atom/'),
array( '/?feed=comments-rss2', '/comments/feed/' ),
array( '/?feed=comments-atom', '/comments/feed/atom/' ),
// Feeds (per-post)
array( '/2008/03/03/comment-test/?feed=comments-atom', '/2008/03/03/comment-test/feed/atom/'),
array( '/?p=149&feed=comments-atom', '/2008/03/03/comment-test/feed/atom/'),
array( '/2008/03/03/comment-test/?feed=comments-atom', '/2008/03/03/comment-test/feed/atom/' ),
array( '/?p=149&feed=comments-atom', '/2008/03/03/comment-test/feed/atom/' ),
// Index
array( '/?paged=1', '/' ),
+59 -9
View File
@@ -7,15 +7,15 @@ class Tests_Canonical_Category extends WP_Canonical_UnitTestCase {
public $structure = '/%category%/%postname%/';
public static $posts = array();
public static $cats = array();
public static $cats = array();
public static function wpSetUpBeforeClass( $factory ) {
self::$posts[0] = $factory->post->create( array( 'post_name' => 'post0' ) );
self::$posts[1] = $factory->post->create( array( 'post_name' => 'post1' ) );
self::$cats[0] = $factory->category->create( array( 'slug' => 'cat0' ) );
self::$cats[1] = $factory->category->create( array( 'slug' => 'cat1' ) );
self::$cats[2] = $factory->category->create( array( 'slug' => 'cat2' ) );
self::$cats[0] = $factory->category->create( array( 'slug' => 'cat0' ) );
self::$cats[1] = $factory->category->create( array( 'slug' => 'cat1' ) );
self::$cats[2] = $factory->category->create( array( 'slug' => 'cat2' ) );
wp_set_post_categories( self::$posts[0], self::$cats[2] );
wp_set_post_categories( self::$posts[0], self::$cats[0] );
@@ -42,19 +42,69 @@ class Tests_Canonical_Category extends WP_Canonical_UnitTestCase {
return array(
// Valid category.
array( '/cat0/post0/', array( 'url' => '/cat0/post0/', 'qv' => array( 'category_name' => 'cat0', 'name' => 'post0', 'page' => '' ) ) ),
array(
'/cat0/post0/',
array(
'url' => '/cat0/post0/',
'qv' => array(
'category_name' => 'cat0',
'name' => 'post0',
'page' => '',
),
),
),
// Category other than the first one will redirect to first "canonical" category.
array( '/cat2/post0/', array( 'url' => '/cat0/post0/', 'qv' => array( 'category_name' => 'cat0', 'name' => 'post0', 'page' => '' ) ) ),
array(
'/cat2/post0/',
array(
'url' => '/cat0/post0/',
'qv' => array(
'category_name' => 'cat0',
'name' => 'post0',
'page' => '',
),
),
),
// Incorrect category will redirect to correct one.
array( '/cat1/post0/', array( 'url' => '/cat0/post0/', 'qv' => array( 'category_name' => 'cat0', 'name' => 'post0', 'page' => '' ) ) ),
array(
'/cat1/post0/',
array(
'url' => '/cat0/post0/',
'qv' => array(
'category_name' => 'cat0',
'name' => 'post0',
'page' => '',
),
),
),
// Nonexistent category will redirect to correct one.
array( '/foo/post0/', array( 'url' => '/cat0/post0/', 'qv' => array( 'category_name' => 'cat0', 'name' => 'post0', 'page' => '' ) ) ),
array(
'/foo/post0/',
array(
'url' => '/cat0/post0/',
'qv' => array(
'category_name' => 'cat0',
'name' => 'post0',
'page' => '',
),
),
),
// Embed URLs should not redirect to post permalinks.
array( '/cat0/post0/embed/', array( 'url' => '/cat0/post0/embed/', 'qv' => array( 'category_name' => 'cat0', 'name' => 'post0', 'embed' => 'true' ) ) ),
array(
'/cat0/post0/embed/',
array(
'url' => '/cat0/post0/embed/',
'qv' => array(
'category_name' => 'cat0',
'name' => 'post0',
'embed' => 'true',
),
),
),
);
}
}
+21 -3
View File
@@ -11,7 +11,7 @@ class Tests_Canonical_CustomRules extends WP_Canonical_UnitTestCase {
parent::setUp();
global $wp_rewrite;
// Add a custom Rewrite rule to test category redirections.
$wp_rewrite->add_rule('ccr/(.+?)/sort/(asc|desc)', 'index.php?category_name=$matches[1]&order=$matches[2]', 'top'); // ccr = Custom_Cat_Rule
$wp_rewrite->add_rule( 'ccr/(.+?)/sort/(asc|desc)', 'index.php?category_name=$matches[1]&order=$matches[2]', 'top' ); // ccr = Custom_Cat_Rule
$wp_rewrite->flush_rules();
}
@@ -33,8 +33,26 @@ class Tests_Canonical_CustomRules extends WP_Canonical_UnitTestCase {
*/
return array(
// Custom Rewrite rules leading to Categories
array( '/ccr/uncategorized/sort/asc/', array( 'url' => '/ccr/uncategorized/sort/asc/', 'qv' => array( 'category_name' => 'uncategorized', 'order' => 'asc' ) ) ),
array( '/ccr/uncategorized/sort/desc/', array( 'url' => '/ccr/uncategorized/sort/desc/', 'qv' => array( 'category_name' => 'uncategorized', 'order' => 'desc' ) ) ),
array(
'/ccr/uncategorized/sort/asc/',
array(
'url' => '/ccr/uncategorized/sort/asc/',
'qv' => array(
'category_name' => 'uncategorized',
'order' => 'asc',
),
),
),
array(
'/ccr/uncategorized/sort/desc/',
array(
'url' => '/ccr/uncategorized/sort/desc/',
'qv' => array(
'category_name' => 'uncategorized',
'order' => 'desc',
),
),
),
);
}
}
+214 -46
View File
@@ -47,54 +47,222 @@ class Tests_Canonical_NoRewrite extends WP_Canonical_UnitTestCase {
array( '/?post_type=page&page_id=1', '/?p=1' ),
// Trailing spaces and punctuation in query string args.
array( '/?p=358 ', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // space
array( '/?p=358%20', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // encoded space
array( '/?p=358!', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // exclamation mark
array( '/?p=358%21', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // encoded exclamation mark
array( '/?p=358"', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // double quote
array( '/?p=358%22', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // encoded double quote
array( '/?p=358\'', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // single quote
array( '/?p=358%27', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // encoded single quote
array( '/?p=358(', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // opening bracket
array( '/?p=358%28', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // encoded opening bracket
array( '/?p=358)', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // closing bracket
array( '/?p=358%29', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // encoded closing bracket
array( '/?p=358,', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // comma
array( '/?p=358%2C', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // encoded comma
array( '/?p=358.', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // period
array( '/?p=358%2E', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // encoded period
array( '/?p=358;', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // semicolon
array( '/?p=358%3B', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // encoded semicolon
array( '/?p=358{', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // opening curly bracket
array( '/?p=358%7B', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // encoded opening curly bracket
array( '/?p=358}', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // closing curly bracket
array( '/?p=358%7D', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // encoded closing curly bracket
array( '/?p=358%E2%80%9C', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // encoded opening curly quote
array( '/?p=358%E2%80%9D', array( 'url' => '/?p=358', 'qv' => array( 'p' => '358' ) ), 20383 ), // encoded closing curly quote
array(
'/?p=358 ',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // space
array(
'/?p=358%20',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // encoded space
array(
'/?p=358!',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // exclamation mark
array(
'/?p=358%21',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // encoded exclamation mark
array(
'/?p=358"',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // double quote
array(
'/?p=358%22',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // encoded double quote
array(
'/?p=358\'',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // single quote
array(
'/?p=358%27',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // encoded single quote
array(
'/?p=358(',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // opening bracket
array(
'/?p=358%28',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // encoded opening bracket
array(
'/?p=358)',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // closing bracket
array(
'/?p=358%29',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // encoded closing bracket
array(
'/?p=358,',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // comma
array(
'/?p=358%2C',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // encoded comma
array(
'/?p=358.',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // period
array(
'/?p=358%2E',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // encoded period
array(
'/?p=358;',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // semicolon
array(
'/?p=358%3B',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // encoded semicolon
array(
'/?p=358{',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // opening curly bracket
array(
'/?p=358%7B',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // encoded opening curly bracket
array(
'/?p=358}',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // closing curly bracket
array(
'/?p=358%7D',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // encoded closing curly bracket
array(
'/?p=358%E2%80%9C',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // encoded opening curly quote
array(
'/?p=358%E2%80%9D',
array(
'url' => '/?p=358',
'qv' => array( 'p' => '358' ),
),
20383,
), // encoded closing curly quote
// Trailing spaces and punctuation in permalinks.
array( '/page/2/ ', '/page/2/', 20383 ), // space
array( '/page/2/%20', '/page/2/', 20383 ), // encoded space
array( '/page/2/!', '/page/2/', 20383 ), // exclamation mark
array( '/page/2/%21', '/page/2/', 20383 ), // encoded exclamation mark
array( '/page/2/"', '/page/2/', 20383 ), // double quote
array( '/page/2/%22', '/page/2/', 20383 ), // encoded double quote
array( '/page/2/\'', '/page/2/', 20383 ), // single quote
array( '/page/2/%27', '/page/2/', 20383 ), // encoded single quote
array( '/page/2/(', '/page/2/', 20383 ), // opening bracket
array( '/page/2/%28', '/page/2/', 20383 ), // encoded opening bracket
array( '/page/2/)', '/page/2/', 20383 ), // closing bracket
array( '/page/2/%29', '/page/2/', 20383 ), // encoded closing bracket
array( '/page/2/,', '/page/2/', 20383 ), // comma
array( '/page/2/%2C', '/page/2/', 20383 ), // encoded comma
array( '/page/2/.', '/page/2/', 20383 ), // period
array( '/page/2/%2E', '/page/2/', 20383 ), // encoded period
array( '/page/2/;', '/page/2/', 20383 ), // semicolon
array( '/page/2/%3B', '/page/2/', 20383 ), // encoded semicolon
array( '/page/2/{', '/page/2/', 20383 ), // opening curly bracket
array( '/page/2/%7B', '/page/2/', 20383 ), // encoded opening curly bracket
array( '/page/2/}', '/page/2/', 20383 ), // closing curly bracket
array( '/page/2/%7D', '/page/2/', 20383 ), // encoded closing curly bracket
array( '/page/2/ ', '/page/2/', 20383 ), // space
array( '/page/2/%20', '/page/2/', 20383 ), // encoded space
array( '/page/2/!', '/page/2/', 20383 ), // exclamation mark
array( '/page/2/%21', '/page/2/', 20383 ), // encoded exclamation mark
array( '/page/2/"', '/page/2/', 20383 ), // double quote
array( '/page/2/%22', '/page/2/', 20383 ), // encoded double quote
array( '/page/2/\'', '/page/2/', 20383 ), // single quote
array( '/page/2/%27', '/page/2/', 20383 ), // encoded single quote
array( '/page/2/(', '/page/2/', 20383 ), // opening bracket
array( '/page/2/%28', '/page/2/', 20383 ), // encoded opening bracket
array( '/page/2/)', '/page/2/', 20383 ), // closing bracket
array( '/page/2/%29', '/page/2/', 20383 ), // encoded closing bracket
array( '/page/2/,', '/page/2/', 20383 ), // comma
array( '/page/2/%2C', '/page/2/', 20383 ), // encoded comma
array( '/page/2/.', '/page/2/', 20383 ), // period
array( '/page/2/%2E', '/page/2/', 20383 ), // encoded period
array( '/page/2/;', '/page/2/', 20383 ), // semicolon
array( '/page/2/%3B', '/page/2/', 20383 ), // encoded semicolon
array( '/page/2/{', '/page/2/', 20383 ), // opening curly bracket
array( '/page/2/%7B', '/page/2/', 20383 ), // encoded opening curly bracket
array( '/page/2/}', '/page/2/', 20383 ), // closing curly bracket
array( '/page/2/%7D', '/page/2/', 20383 ), // encoded closing curly bracket
array( '/page/2/%E2%80%9C', '/page/2/', 20383 ), // encoded opening curly quote
array( '/page/2/%E2%80%9D', '/page/2/', 20383 ), // encoded closing curly quote
+17 -2
View File
@@ -11,8 +11,23 @@ class Tests_Canonical_PageOnFront extends WP_Canonical_UnitTestCase {
parent::setUp();
update_option( 'show_on_front', 'page' );
update_option( 'page_for_posts', self::factory()->post->create( array( 'post_title' => 'blog-page', 'post_type' => 'page' ) ) );
update_option( 'page_on_front', self::factory()->post->create( array( 'post_title' => 'front-page', 'post_type' => 'page', 'post_content' => "Page 1\n<!--nextpage-->\nPage 2" ) ) );
update_option(
'page_for_posts', self::factory()->post->create(
array(
'post_title' => 'blog-page',
'post_type' => 'page',
)
)
);
update_option(
'page_on_front', self::factory()->post->create(
array(
'post_title' => 'front-page',
'post_type' => 'page',
'post_content' => "Page 1\n<!--nextpage-->\nPage 2",
)
)
);
}
/**
+8 -6
View File
@@ -12,14 +12,16 @@ class Tests_Canonical_Paged extends WP_Canonical_UnitTestCase {
This is a paragraph.';
$next = '<!--nextpage-->';
$post_id = self::factory()->post->create( array(
'post_status' => 'publish',
'post_content' => "{$para}{$next}{$para}{$next}{$para}"
) );
$post_id = self::factory()->post->create(
array(
'post_status' => 'publish',
'post_content' => "{$para}{$next}{$para}{$next}{$para}",
)
);
$link = parse_url( get_permalink( $post_id ), PHP_URL_PATH );
$link = parse_url( get_permalink( $post_id ), PHP_URL_PATH );
$paged = $link . '4/';
$this->assertCanonical( $paged, $link );
}
}
}
+38 -38
View File
@@ -25,11 +25,11 @@ class Tests_Category extends WP_UnitTestCase {
// create new taxonomy to ensure not included
register_taxonomy( 'test_tax_cat', 'post' );
wp_insert_term( "test1", 'test_tax_cat' );
wp_insert_term( 'test1', 'test_tax_cat' );
// Validate length is 1 + created due to uncategorized
$cat_ids = get_all_category_ids();
$this->assertEquals( 3, count($cat_ids));
$this->assertEquals( 3, count( $cat_ids ) );
}
/**
@@ -38,16 +38,16 @@ class Tests_Category extends WP_UnitTestCase {
function test_get_category_by_slug() {
// create Test Categories
$testcat = self::factory()->category->create_and_get(
$testcat = self::factory()->category->create_and_get(
array(
'slug' => 'testcat',
'name' => 'Test Category 1'
'name' => 'Test Category 1',
)
);
$testcat2 = self::factory()->category->create_and_get(
array(
'slug' => 'testcat2',
'name' => 'Test Category 2'
'name' => 'Test Category 2',
)
);
@@ -68,21 +68,21 @@ class Tests_Category extends WP_UnitTestCase {
function test__make_cat_compat() {
// create Test Categories and Array Representations
$testcat_array = array(
'slug' => 'testmcc',
'name' => 'Test MCC',
'description' => 'Category Test'
$testcat_array = array(
'slug' => 'testmcc',
'name' => 'Test MCC',
'description' => 'Category Test',
);
$testcat = self::factory()->category->create_and_get( $testcat_array );
$testcat = self::factory()->category->create_and_get( $testcat_array );
$testcat_array['term_id'] = $testcat->term_id;
$testcat2_array = array(
'slug' => 'testmcc',
'name' => 'Test MCC',
$testcat2_array = array(
'slug' => 'testmcc',
'name' => 'Test MCC',
'description' => 'Category Test',
'parent' => $testcat->term_id
'parent' => $testcat->term_id,
);
$testcat2 = self::factory()->category->create_and_get( $testcat2_array );
$testcat2 = self::factory()->category->create_and_get( $testcat2_array );
$testcat2_array['term_id'] = $testcat2->term_id;
// unset properties to enable validation of object
@@ -148,7 +148,7 @@ class Tests_Category extends WP_UnitTestCase {
$testcat = self::factory()->category->create_and_get(
array(
'slug' => 'testcat',
'name' => 'Test Category 1'
'name' => 'Test Category 1',
)
);
@@ -168,13 +168,13 @@ class Tests_Category extends WP_UnitTestCase {
$testcat = self::factory()->category->create_and_get(
array(
'slug' => 'testcat',
'name' => 'Test Category 1'
'name' => 'Test Category 1',
)
);
// Validate
$this->assertEquals( $testcat->term_id, get_cat_ID( $testcat->name ) );
$this->assertEquals( 0, get_cat_ID( "NO CAT" ) );
$this->assertEquals( 0, get_cat_ID( 'NO CAT' ) );
$this->assertEquals( 0, get_cat_ID( 12 ) );
}
@@ -185,45 +185,45 @@ class Tests_Category extends WP_UnitTestCase {
function test_get_category_by_path() {
// create Test Categories
$root_id = self::factory()->category->create(
$root_id = self::factory()->category->create(
array(
'slug' => 'root',
)
);
$root_cat_id = self::factory()->category->create(
$root_cat_id = self::factory()->category->create(
array(
'slug' => 'cat',
'parent' => $root_id
'slug' => 'cat',
'parent' => $root_id,
)
);
$root_cat_cat_id = self::factory()->category->create(
$root_cat_cat_id = self::factory()->category->create(
array(
'slug' => 'cat', //note this is modified on create
'parent' => $root_cat_id
'slug' => 'cat', //note this is modified on create
'parent' => $root_cat_id,
)
);
$root_path_id = self::factory()->category->create(
$root_path_id = self::factory()->category->create(
array(
'slug' => 'path',
'parent' => $root_id
'slug' => 'path',
'parent' => $root_id,
)
);
$root_path_cat_id = self::factory()->category->create(
$root_path_cat_id = self::factory()->category->create(
array(
'slug' => 'cat', //note this is modified on create
'parent' => $root_path_id
'slug' => 'cat', //note this is modified on create
'parent' => $root_path_id,
)
);
$root_level_id = self::factory()->category->create(
$root_level_id = self::factory()->category->create(
array(
'slug' => 'level-1',
'parent' => $root_id
'slug' => 'level-1',
'parent' => $root_id,
)
);
$root_level_cat_id = self::factory()->category->create(
array(
'slug' => 'cat', //note this is modified on create
'parent' => $root_level_id
'slug' => 'cat', //note this is modified on create
'parent' => $root_level_id,
)
);
@@ -231,7 +231,7 @@ class Tests_Category extends WP_UnitTestCase {
$ret_cat = get_category_by_path( '/root/level-1', true );
$this->assertEquals( $root_level_id, $ret_cat->term_id );
$this->assertNull( get_category_by_path( 'level-1', true ) );
$this->assertNull( get_category_by_path( 'nocat/nocat/', true) );
$this->assertNull( get_category_by_path( 'nocat/nocat/', true ) );
// Validate Partial Match
$ret_cat = get_category_by_path( 'level-1', false );
@@ -240,6 +240,6 @@ class Tests_Category extends WP_UnitTestCase {
$this->assertEquals( $root_level_id, $ret_cat->term_id );
$ret_cat = get_category_by_path( 'root$2Fcat%20%2Flevel-1', false );
$this->assertEquals( $root_level_id, $ret_cat->term_id );
$this->assertNull( get_category_by_path( 'nocat/nocat/', false) );
$this->assertNull( get_category_by_path( 'nocat/nocat/', false ) );
}
}
@@ -11,9 +11,11 @@ class Tests_Category_GetCategoryParents extends WP_UnitTestCase {
parent::setUp();
$this->c1 = self::factory()->category->create_and_get();
$this->c2 = self::factory()->category->create_and_get( array(
'parent' => $this->c1->term_id,
) );
$this->c2 = self::factory()->category->create_and_get(
array(
'parent' => $this->c1->term_id,
)
);
}
public function test_should_return_wp_error_for_invalid_category() {
@@ -21,32 +23,32 @@ class Tests_Category_GetCategoryParents extends WP_UnitTestCase {
}
public function test_with_default_parameters() {
$expected = $this->c1->name . '/'. $this->c2->name . '/';
$found = get_category_parents( $this->c2->term_id );
$expected = $this->c1->name . '/' . $this->c2->name . '/';
$found = get_category_parents( $this->c2->term_id );
$this->assertSame( $expected, $found );
}
public function test_link_true() {
$expected = '<a href="' . get_category_link( $this->c1->term_id ) . '">' . $this->c1->name . '</a>/<a href="' . get_category_link( $this->c2->term_id ) . '">'. $this->c2->name . '</a>/';
$found = get_category_parents( $this->c2->term_id, true );
$expected = '<a href="' . get_category_link( $this->c1->term_id ) . '">' . $this->c1->name . '</a>/<a href="' . get_category_link( $this->c2->term_id ) . '">' . $this->c2->name . '</a>/';
$found = get_category_parents( $this->c2->term_id, true );
$this->assertSame( $expected, $found );
}
public function test_separator() {
$expected = $this->c1->name . ' --- ' . $this->c2->name . ' --- ';
$found = get_category_parents( $this->c2->term_id, false, ' --- ', false );
$found = get_category_parents( $this->c2->term_id, false, ' --- ', false );
$this->assertSame( $expected, $found );
}
public function test_nicename_false() {
$expected = $this->c1->name . '/'. $this->c2->name . '/';
$found = get_category_parents( $this->c2->term_id, false, '/', false );
$expected = $this->c1->name . '/' . $this->c2->name . '/';
$found = get_category_parents( $this->c2->term_id, false, '/', false );
$this->assertSame( $expected, $found );
}
public function test_nicename_true() {
$expected = $this->c1->slug . '/'. $this->c2->slug . '/';
$found = get_category_parents( $this->c2->term_id, false, '/', true );
$expected = $this->c1->slug . '/' . $this->c2->slug . '/';
$found = get_category_parents( $this->c2->term_id, false, '/', true );
$this->assertSame( $expected, $found );
}
@@ -57,7 +59,7 @@ class Tests_Category_GetCategoryParents extends WP_UnitTestCase {
public function test_category_without_parents() {
$expected = $this->c1->name . '/';
$found = get_category_parents( $this->c1->term_id );
$found = get_category_parents( $this->c1->term_id );
$this->assertSame( $expected, $found );
}
}
@@ -9,16 +9,20 @@ class Tests_Category_WpDropdownCategories extends WP_UnitTestCase {
*/
public function test_wp_dropdown_categories_value_field_should_default_to_term_id() {
// Create a test category.
$cat_id = self::factory()->category->create( array(
'name' => 'Test Category',
'slug' => 'test_category',
) );
$cat_id = self::factory()->category->create(
array(
'name' => 'Test Category',
'slug' => 'test_category',
)
);
// Get the default functionality of wp_dropdown_categories().
$dropdown_default = wp_dropdown_categories( array(
'echo' => 0,
'hide_empty' => 0,
) );
$dropdown_default = wp_dropdown_categories(
array(
'echo' => 0,
'hide_empty' => 0,
)
);
// Test to see if it returns the default with the category ID.
$this->assertContains( 'value="' . $cat_id . '"', $dropdown_default );
@@ -29,17 +33,21 @@ class Tests_Category_WpDropdownCategories extends WP_UnitTestCase {
*/
public function test_wp_dropdown_categories_value_field_term_id() {
// Create a test category.
$cat_id = self::factory()->category->create( array(
'name' => 'Test Category',
'slug' => 'test_category',
) );
$cat_id = self::factory()->category->create(
array(
'name' => 'Test Category',
'slug' => 'test_category',
)
);
// Get the default functionality of wp_dropdown_categories().
$found = wp_dropdown_categories( array(
'echo' => 0,
'hide_empty' => 0,
'value_field' => 'term_id',
) );
$found = wp_dropdown_categories(
array(
'echo' => 0,
'hide_empty' => 0,
'value_field' => 'term_id',
)
);
// Test to see if it returns the default with the category ID.
$this->assertContains( 'value="' . $cat_id . '"', $found );
@@ -50,17 +58,21 @@ class Tests_Category_WpDropdownCategories extends WP_UnitTestCase {
*/
public function test_wp_dropdown_categories_value_field_slug() {
// Create a test category.
$cat_id = self::factory()->category->create( array(
'name' => 'Test Category',
'slug' => 'test_category',
) );
$cat_id = self::factory()->category->create(
array(
'name' => 'Test Category',
'slug' => 'test_category',
)
);
// Get the default functionality of wp_dropdown_categories().
$found = wp_dropdown_categories( array(
'echo' => 0,
'hide_empty' => 0,
'value_field' => 'slug',
) );
$found = wp_dropdown_categories(
array(
'echo' => 0,
'hide_empty' => 0,
'value_field' => 'slug',
)
);
// Test to see if it returns the default with the category slug.
$this->assertContains( 'value="test_category"', $found );
@@ -71,17 +83,21 @@ class Tests_Category_WpDropdownCategories extends WP_UnitTestCase {
*/
public function test_wp_dropdown_categories_value_field_should_fall_back_on_term_id_when_an_invalid_value_is_provided() {
// Create a test category.
$cat_id = self::factory()->category->create( array(
'name' => 'Test Category',
'slug' => 'test_category',
) );
$cat_id = self::factory()->category->create(
array(
'name' => 'Test Category',
'slug' => 'test_category',
)
);
// Get the default functionality of wp_dropdown_categories().
$found = wp_dropdown_categories( array(
'echo' => 0,
'hide_empty' => 0,
'value_field' => 'foo',
) );
$found = wp_dropdown_categories(
array(
'echo' => 0,
'hide_empty' => 0,
'value_field' => 'foo',
)
);
// Test to see if it returns the default with the category slug.
$this->assertContains( 'value="' . $cat_id . '"', $found );
@@ -91,24 +107,30 @@ class Tests_Category_WpDropdownCategories extends WP_UnitTestCase {
* @ticket 32330
*/
public function test_wp_dropdown_categories_selected_should_respect_custom_value_field() {
$c1 = self::factory()->category->create( array(
'name' => 'Test Category 1',
'slug' => 'test_category_1',
) );
$c1 = self::factory()->category->create(
array(
'name' => 'Test Category 1',
'slug' => 'test_category_1',
)
);
$c2 = self::factory()->category->create( array(
'name' => 'Test Category 2',
'slug' => 'test_category_2',
) );
$c2 = self::factory()->category->create(
array(
'name' => 'Test Category 2',
'slug' => 'test_category_2',
)
);
$found = wp_dropdown_categories( array(
'echo' => 0,
'hide_empty' => 0,
'value_field' => 'slug',
'selected' => 'test_category_2',
) );
$found = wp_dropdown_categories(
array(
'echo' => 0,
'hide_empty' => 0,
'value_field' => 'slug',
'selected' => 'test_category_2',
)
);
$this->assertContains( "value=\"test_category_2\" selected=\"selected\"", $found );
$this->assertContains( 'value="test_category_2" selected="selected"', $found );
}
/**
@@ -117,12 +139,14 @@ class Tests_Category_WpDropdownCategories extends WP_UnitTestCase {
public function test_wp_dropdown_categories_show_option_all_should_be_selected_if_no_selected_value_is_explicitly_passed_and_value_field_does_not_have_string_values() {
$cats = self::factory()->category->create_many( 3 );
$found = wp_dropdown_categories( array(
'echo' => 0,
'hide_empty' => 0,
'show_option_all' => 'Foo',
'value_field' => 'slug',
) );
$found = wp_dropdown_categories(
array(
'echo' => 0,
'hide_empty' => 0,
'show_option_all' => 'Foo',
'value_field' => 'slug',
)
);
$this->assertContains( "value='0' selected='selected'", $found );
@@ -138,13 +162,15 @@ class Tests_Category_WpDropdownCategories extends WP_UnitTestCase {
public function test_wp_dropdown_categories_show_option_all_should_be_selected_if_selected_value_of_0_string_is_explicitly_passed_and_value_field_does_not_have_string_values() {
$cats = self::factory()->category->create_many( 3 );
$found = wp_dropdown_categories( array(
'echo' => 0,
'hide_empty' => 0,
'show_option_all' => 'Foo',
'value_field' => 'slug',
'selected' => '0',
) );
$found = wp_dropdown_categories(
array(
'echo' => 0,
'hide_empty' => 0,
'show_option_all' => 'Foo',
'value_field' => 'slug',
'selected' => '0',
)
);
$this->assertContains( "value='0' selected='selected'", $found );
@@ -159,14 +185,16 @@ class Tests_Category_WpDropdownCategories extends WP_UnitTestCase {
*/
public function test_required_true_should_add_required_attribute() {
// Create a test category.
$cat_id = self::factory()->category->create( array(
'name' => 'Test Category',
'slug' => 'test_category',
) );
$cat_id = self::factory()->category->create(
array(
'name' => 'Test Category',
'slug' => 'test_category',
)
);
$args = array(
$args = array(
'show_option_none' => __( 'Select one', 'text-domain' ),
'option_none_value' => "",
'option_none_value' => '',
'required' => true,
'hide_empty' => 0,
'echo' => 0,
@@ -182,14 +210,16 @@ class Tests_Category_WpDropdownCategories extends WP_UnitTestCase {
*/
public function test_required_false_should_omit_required_attribute() {
// Create a test category.
$cat_id = self::factory()->category->create( array(
'name' => 'Test Category',
'slug' => 'test_category',
) );
$cat_id = self::factory()->category->create(
array(
'name' => 'Test Category',
'slug' => 'test_category',
)
);
$args = array(
$args = array(
'show_option_none' => __( 'Select one', 'text-domain' ),
'option_none_value' => "",
'option_none_value' => '',
'required' => false,
'hide_empty' => 0,
'echo' => 0,
@@ -205,14 +235,16 @@ class Tests_Category_WpDropdownCategories extends WP_UnitTestCase {
*/
public function test_required_should_default_to_false() {
// Create a test category.
$cat_id = self::factory()->category->create( array(
'name' => 'Test Category',
'slug' => 'test_category',
) );
$cat_id = self::factory()->category->create(
array(
'name' => 'Test Category',
'slug' => 'test_category',
)
);
$args = array(
$args = array(
'show_option_none' => __( 'Select one', 'text-domain' ),
'option_none_value' => "",
'option_none_value' => '',
'hide_empty' => 0,
'echo' => 0,
);
+374 -162
View File
@@ -7,10 +7,12 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
public function test_class() {
$c = self::factory()->category->create();
$found = wp_list_categories( array(
'hide_empty' => false,
'echo' => false,
) );
$found = wp_list_categories(
array(
'hide_empty' => false,
'echo' => false,
)
);
$this->assertContains( 'class="cat-item cat-item-' . $c . '"', $found );
}
@@ -19,11 +21,13 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
$c1 = self::factory()->category->create();
$c2 = self::factory()->category->create();
$found = wp_list_categories( array(
'hide_empty' => false,
'echo' => false,
'current_category' => $c2,
) );
$found = wp_list_categories(
array(
'hide_empty' => false,
'echo' => false,
'current_category' => $c2,
)
);
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $c1 . '[^"]*current-cat[^"]*"/', $found );
$this->assertRegExp( '/class="[^"]*cat-item-' . $c2 . '[^"]*current-cat[^"]*"/', $found );
@@ -31,15 +35,19 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
public function test_class_containing_current_cat_parent() {
$c1 = self::factory()->category->create();
$c2 = self::factory()->category->create( array(
'parent' => $c1,
) );
$c2 = self::factory()->category->create(
array(
'parent' => $c1,
)
);
$found = wp_list_categories( array(
'hide_empty' => false,
'echo' => false,
'current_category' => $c2,
) );
$found = wp_list_categories(
array(
'hide_empty' => false,
'echo' => false,
'current_category' => $c2,
)
);
$this->assertRegExp( '/class="[^"]*cat-item-' . $c1 . '[^"]*current-cat-parent[^"]*"/', $found );
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $c2 . '[^"]*current-cat-parent[^"]*"/', $found );
@@ -51,11 +59,13 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
public function test_current_category_should_accept_an_array_of_ids() {
$cats = self::factory()->category->create_many( 3 );
$found = wp_list_categories( array(
'echo' => false,
'hide_empty' => false,
'current_category' => array( $cats[0], $cats[2] ),
) );
$found = wp_list_categories(
array(
'echo' => false,
'hide_empty' => false,
'current_category' => array( $cats[0], $cats[2] ),
)
);
$this->assertRegExp( '/class="[^"]*cat-item-' . $cats[0] . '[^"]*current-cat[^"]*"/', $found );
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $cats[1] . '[^"]*current[^"]*"/', $found );
@@ -66,18 +76,24 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
* @ticket 16792
*/
public function test_should_not_create_element_when_cat_name_is_filtered_to_empty_string() {
$c1 = self::factory()->category->create( array(
'name' => 'Test Cat 1',
) );
$c2 = self::factory()->category->create( array(
'name' => 'Test Cat 2',
) );
$c1 = self::factory()->category->create(
array(
'name' => 'Test Cat 1',
)
);
$c2 = self::factory()->category->create(
array(
'name' => 'Test Cat 2',
)
);
add_filter( 'list_cats', array( $this, 'list_cats_callback' ) );
$found = wp_list_categories( array(
'hide_empty' => false,
'echo' => false,
) );
$found = wp_list_categories(
array(
'hide_empty' => false,
'echo' => false,
)
);
remove_filter( 'list_cats', array( $this, 'list_cats_callback' ) );
$this->assertContains( "cat-item-$c2", $found );
@@ -90,29 +106,33 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
public function test_show_option_all_link_should_go_to_home_page_when_show_on_front_is_false() {
$cats = self::factory()->category->create_many( 2 );
$found = wp_list_categories( array(
'echo' => false,
'show_option_all' => 'All',
'hide_empty' => false,
'taxonomy' => 'category',
) );
$found = wp_list_categories(
array(
'echo' => false,
'show_option_all' => 'All',
'hide_empty' => false,
'taxonomy' => 'category',
)
);
$this->assertContains( "<li class='cat-item-all'><a href='" . home_url( '/' ) . "'>All</a></li>", $found );
}
public function test_show_option_all_link_should_respect_page_for_posts() {
$cats = self::factory()->category->create_many( 2 );
$p = self::factory()->post->create( array( 'post_type' => 'page' ) );
$p = self::factory()->post->create( array( 'post_type' => 'page' ) );
update_option( 'show_on_front', 'page' );
update_option( 'page_for_posts', $p );
$found = wp_list_categories( array(
'echo' => false,
'show_option_all' => 'All',
'hide_empty' => false,
'taxonomy' => 'category',
) );
$found = wp_list_categories(
array(
'echo' => false,
'show_option_all' => 'All',
'hide_empty' => false,
'taxonomy' => 'category',
)
);
$this->assertContains( "<li class='cat-item-all'><a href='" . get_permalink( $p ) . "'>All</a></li>", $found );
}
@@ -125,16 +145,20 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
register_post_type( 'wptests_pt2', array( 'has_archive' => true ) );
register_taxonomy( 'wptests_tax', array( 'foo', 'wptests_pt', 'wptests_pt2' ) );
$terms = self::factory()->term->create_many( 2, array(
'taxonomy' => 'wptests_tax',
) );
$terms = self::factory()->term->create_many(
2, array(
'taxonomy' => 'wptests_tax',
)
);
$found = wp_list_categories( array(
'echo' => false,
'show_option_all' => 'All',
'hide_empty' => false,
'taxonomy' => 'wptests_tax',
) );
$found = wp_list_categories(
array(
'echo' => false,
'show_option_all' => 'All',
'hide_empty' => false,
'taxonomy' => 'wptests_tax',
)
);
$pt_archive = get_post_type_archive_link( 'wptests_pt' );
@@ -149,16 +173,20 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
register_post_type( 'wptests_pt2', array( 'has_archive' => true ) );
register_taxonomy( 'wptests_tax', array( 'foo', 'wptests_pt', 'wptests_pt2' ) );
$terms = self::factory()->term->create_many( 2, array(
'taxonomy' => 'wptests_tax',
) );
$terms = self::factory()->term->create_many(
2, array(
'taxonomy' => 'wptests_tax',
)
);
$found = wp_list_categories( array(
'echo' => false,
'show_option_all' => 'All',
'hide_empty' => false,
'taxonomy' => 'wptests_tax',
) );
$found = wp_list_categories(
array(
'echo' => false,
'show_option_all' => 'All',
'hide_empty' => false,
'taxonomy' => 'wptests_tax',
)
);
$pt_archive = get_post_type_archive_link( 'wptests_pt2' );
@@ -170,16 +198,20 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
register_post_type( 'wptests_pt2', array( 'has_archive' => true ) );
register_taxonomy( 'wptests_tax', array( 'foo', 'wptests_pt', 'post', 'wptests_pt2' ) );
$terms = self::factory()->term->create_many( 2, array(
'taxonomy' => 'wptests_tax',
) );
$terms = self::factory()->term->create_many(
2, array(
'taxonomy' => 'wptests_tax',
)
);
$found = wp_list_categories( array(
'echo' => false,
'show_option_all' => 'All',
'hide_empty' => false,
'taxonomy' => 'wptests_tax',
) );
$found = wp_list_categories(
array(
'echo' => false,
'show_option_all' => 'All',
'hide_empty' => false,
'taxonomy' => 'wptests_tax',
)
);
$url = home_url( '/' );
@@ -191,16 +223,20 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
register_post_type( 'wptests_pt2', array( 'has_archive' => false ) );
register_taxonomy( 'wptests_tax', array( 'foo', 'wptests_pt', 'wptests_pt2' ) );
$terms = self::factory()->term->create_many( 2, array(
'taxonomy' => 'wptests_tax',
) );
$terms = self::factory()->term->create_many(
2, array(
'taxonomy' => 'wptests_tax',
)
);
$found = wp_list_categories( array(
'echo' => false,
'show_option_all' => 'All',
'hide_empty' => false,
'taxonomy' => 'wptests_tax',
) );
$found = wp_list_categories(
array(
'echo' => false,
'show_option_all' => 'All',
'hide_empty' => false,
'taxonomy' => 'wptests_tax',
)
);
$url = home_url( '/' );
@@ -219,9 +255,11 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
* @ticket 33460
*/
public function test_title_li_should_be_shown_by_default_for_empty_lists() {
$found = wp_list_categories( array(
'echo' => false,
) );
$found = wp_list_categories(
array(
'echo' => false,
)
);
$this->assertContains( '<li class="categories">Categories', $found );
}
@@ -230,10 +268,12 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
* @ticket 33460
*/
public function test_hide_title_if_empty_should_be_respected_for_empty_lists_when_true() {
$found = wp_list_categories( array(
'echo' => false,
'hide_title_if_empty' => true,
) );
$found = wp_list_categories(
array(
'echo' => false,
'hide_title_if_empty' => true,
)
);
$this->assertNotContains( '<li class="categories">Categories', $found );
}
@@ -242,10 +282,12 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
* @ticket 33460
*/
public function test_hide_title_if_empty_should_be_respected_for_empty_lists_when_false() {
$found = wp_list_categories( array(
'echo' => false,
'hide_title_if_empty' => false,
) );
$found = wp_list_categories(
array(
'echo' => false,
'hide_title_if_empty' => false,
)
);
$this->assertContains( '<li class="categories">Categories', $found );
}
@@ -256,11 +298,13 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
public function test_hide_title_if_empty_should_be_ignored_when_category_list_is_not_empty() {
$cat = self::factory()->category->create();
$found = wp_list_categories( array(
'echo' => false,
'hide_empty' => false,
'hide_title_if_empty' => true,
) );
$found = wp_list_categories(
array(
'echo' => false,
'hide_empty' => false,
'hide_title_if_empty' => true,
)
);
$this->assertContains( '<li class="categories">Categories', $found );
}
@@ -271,12 +315,14 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
public function test_hide_title_if_empty_should_not_output_stray_closing_tags() {
$cat = self::factory()->category->create();
$found = wp_list_categories( array(
'echo' => false,
'show_option_none' => '',
'child_of' => 1,
'hide_title_if_empty' => true,
) );
$found = wp_list_categories(
array(
'echo' => false,
'show_option_none' => '',
'child_of' => 1,
'hide_title_if_empty' => true,
)
);
$this->assertNotContains( '</ul></li>', $found );
}
@@ -285,11 +331,26 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
* @ticket 12981
*/
public function test_exclude_tree_should_be_respected() {
$c = self::factory()->category->create();
$parent = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
$child = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
$c = self::factory()->category->create();
$parent = self::factory()->category->create(
array(
'name' => 'Parent',
'slug' => 'parent',
)
);
$child = self::factory()->category->create(
array(
'name' => 'Child',
'slug' => 'child',
'parent' => $parent,
)
);
$args = array( 'echo' => 0, 'hide_empty' => 0, 'exclude_tree' => $parent );
$args = array(
'echo' => 0,
'hide_empty' => 0,
'exclude_tree' => $parent,
);
$actual = wp_list_categories( $args );
@@ -302,20 +363,48 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
* @ticket 12981
*/
public function test_exclude_tree_should_be_merged_with_exclude() {
$c = self::factory()->category->create();
$parent = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
$child = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
$parent2 = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent2' ) );
$child2 = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child2', 'parent' => $parent2 ) );
$c = self::factory()->category->create();
$parent = self::factory()->category->create(
array(
'name' => 'Parent',
'slug' => 'parent',
)
);
$child = self::factory()->category->create(
array(
'name' => 'Child',
'slug' => 'child',
'parent' => $parent,
)
);
$parent2 = self::factory()->category->create(
array(
'name' => 'Parent',
'slug' => 'parent2',
)
);
$child2 = self::factory()->category->create(
array(
'name' => 'Child',
'slug' => 'child2',
'parent' => $parent2,
)
);
$args = array( 'echo' => 0, 'hide_empty' => 0, 'exclude_tree' => $parent );
$args = array(
'echo' => 0,
'hide_empty' => 0,
'exclude_tree' => $parent,
);
$actual = wp_list_categories( array(
'echo' => 0,
'hide_empty' => 0,
'exclude' => $parent,
'exclude_tree' => $parent2,
) );
$actual = wp_list_categories(
array(
'echo' => 0,
'hide_empty' => 0,
'exclude' => $parent,
'exclude_tree' => $parent2,
)
);
$this->assertNotContains( '<li class="cat-item cat-item-' . $parent . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $parent2 . '">', $actual );
@@ -328,25 +417,74 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
* @ticket 35156
*/
public function test_comma_separated_exclude_tree_should_be_merged_with_exclude() {
$c = self::factory()->category->create();
$parent = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
$child = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
$parent2 = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent2' ) );
$child2 = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child2', 'parent' => $parent2 ) );
$parent3 = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent3' ) );
$child3 = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child3', 'parent' => $parent3 ) );
$parent4 = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent4' ) );
$child4 = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child4', 'parent' => $parent4 ) );
$c = self::factory()->category->create();
$parent = self::factory()->category->create(
array(
'name' => 'Parent',
'slug' => 'parent',
)
);
$child = self::factory()->category->create(
array(
'name' => 'Child',
'slug' => 'child',
'parent' => $parent,
)
);
$parent2 = self::factory()->category->create(
array(
'name' => 'Parent',
'slug' => 'parent2',
)
);
$child2 = self::factory()->category->create(
array(
'name' => 'Child',
'slug' => 'child2',
'parent' => $parent2,
)
);
$parent3 = self::factory()->category->create(
array(
'name' => 'Parent',
'slug' => 'parent3',
)
);
$child3 = self::factory()->category->create(
array(
'name' => 'Child',
'slug' => 'child3',
'parent' => $parent3,
)
);
$parent4 = self::factory()->category->create(
array(
'name' => 'Parent',
'slug' => 'parent4',
)
);
$child4 = self::factory()->category->create(
array(
'name' => 'Child',
'slug' => 'child4',
'parent' => $parent4,
)
);
$args = array(
'echo' => 0,
'hide_empty' => 0,
'exclude_tree' => $parent,
);
$args = array( 'echo' => 0, 'hide_empty' => 0, 'exclude_tree' => $parent );
$actual = wp_list_categories( array(
'echo' => 0,
'hide_empty' => 0,
'exclude' => "$parent,$parent2",
'exclude_tree' => "$parent3,$parent4",
) );
$actual = wp_list_categories(
array(
'echo' => 0,
'hide_empty' => 0,
'exclude' => "$parent,$parent2",
'exclude_tree' => "$parent3,$parent4",
)
);
$this->assertContains( '<li class="cat-item cat-item-' . $c . '">', $actual );
@@ -365,25 +503,74 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
* @ticket 35156
*/
public function test_array_exclude_tree_should_be_merged_with_exclude() {
$c = self::factory()->category->create();
$parent = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
$child = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
$parent2 = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent2' ) );
$child2 = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child2', 'parent' => $parent2 ) );
$parent3 = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent3' ) );
$child3 = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child3', 'parent' => $parent3 ) );
$parent4 = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent4' ) );
$child4 = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child4', 'parent' => $parent4 ) );
$c = self::factory()->category->create();
$parent = self::factory()->category->create(
array(
'name' => 'Parent',
'slug' => 'parent',
)
);
$child = self::factory()->category->create(
array(
'name' => 'Child',
'slug' => 'child',
'parent' => $parent,
)
);
$parent2 = self::factory()->category->create(
array(
'name' => 'Parent',
'slug' => 'parent2',
)
);
$child2 = self::factory()->category->create(
array(
'name' => 'Child',
'slug' => 'child2',
'parent' => $parent2,
)
);
$parent3 = self::factory()->category->create(
array(
'name' => 'Parent',
'slug' => 'parent3',
)
);
$child3 = self::factory()->category->create(
array(
'name' => 'Child',
'slug' => 'child3',
'parent' => $parent3,
)
);
$parent4 = self::factory()->category->create(
array(
'name' => 'Parent',
'slug' => 'parent4',
)
);
$child4 = self::factory()->category->create(
array(
'name' => 'Child',
'slug' => 'child4',
'parent' => $parent4,
)
);
$args = array(
'echo' => 0,
'hide_empty' => 0,
'exclude_tree' => $parent,
);
$args = array( 'echo' => 0, 'hide_empty' => 0, 'exclude_tree' => $parent );
$actual = wp_list_categories( array(
'echo' => 0,
'hide_empty' => 0,
'exclude' => array( $parent, $parent2 ),
'exclude_tree' => array( $parent3, $parent4 ),
) );
$actual = wp_list_categories(
array(
'echo' => 0,
'hide_empty' => 0,
'exclude' => array( $parent, $parent2 ),
'exclude_tree' => array( $parent3, $parent4 ),
)
);
$this->assertContains( '<li class="cat-item cat-item-' . $c . '">', $actual );
@@ -402,16 +589,41 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
* @ticket 10676
*/
public function test_class_containing_current_cat_ancestor() {
$parent = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
$child = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
$child2 = self::factory()->category->create( array( 'name' => 'Child 2', 'slug' => 'child2', 'parent' => $parent ) );
$grandchild = self::factory()->category->create( array( 'name' => 'Grand Child', 'slug' => 'child', 'parent' => $child ) );
$parent = self::factory()->category->create(
array(
'name' => 'Parent',
'slug' => 'parent',
)
);
$child = self::factory()->category->create(
array(
'name' => 'Child',
'slug' => 'child',
'parent' => $parent,
)
);
$child2 = self::factory()->category->create(
array(
'name' => 'Child 2',
'slug' => 'child2',
'parent' => $parent,
)
);
$grandchild = self::factory()->category->create(
array(
'name' => 'Grand Child',
'slug' => 'child',
'parent' => $child,
)
);
$actual = wp_list_categories( array(
'echo' => 0,
'hide_empty' => false,
'current_category' => $grandchild,
) );
$actual = wp_list_categories(
array(
'echo' => 0,
'hide_empty' => false,
'current_category' => $grandchild,
)
);
$this->assertRegExp( '/class="[^"]*cat-item-' . $parent . '[^"]*current-cat-ancestor[^"]*"/', $actual );
$this->assertRegExp( '/class="[^"]*cat-item-' . $child . '[^"]*current-cat-ancestor[^"]*"/', $actual );
+215 -157
View File
@@ -17,7 +17,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$this->assertSame( 0, did_action( $error ) );
$data = array(
$data = array(
'comment_post_ID' => 0,
);
$comment = wp_handle_comment_submission( $data );
@@ -34,10 +34,12 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$this->assertSame( 0, did_action( $error ) );
$post = self::factory()->post->create_and_get( array(
'comment_status' => 'closed',
) );
$data = array(
$post = self::factory()->post->create_and_get(
array(
'comment_status' => 'closed',
)
);
$data = array(
'comment_post_ID' => $post->ID,
);
$comment = wp_handle_comment_submission( $data );
@@ -56,7 +58,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$post = self::factory()->post->create_and_get();
wp_trash_post( $post->ID );
$data = array(
$data = array(
'comment_post_ID' => $post->ID,
);
$comment = wp_handle_comment_submission( $data );
@@ -72,10 +74,12 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$this->assertSame( 0, did_action( $error ) );
$post = self::factory()->post->create_and_get( array(
'post_status' => 'draft',
) );
$data = array(
$post = self::factory()->post->create_and_get(
array(
'post_status' => 'draft',
)
);
$data = array(
'comment_post_ID' => $post->ID,
);
$comment = wp_handle_comment_submission( $data );
@@ -93,19 +97,23 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
public function test_submitting_comment_to_draft_post_returns_error_message_for_user_with_correct_caps() {
$error = 'comment_on_draft';
$user = self::factory()->user->create_and_get( array(
'role' => 'author',
) );
$user = self::factory()->user->create_and_get(
array(
'role' => 'author',
)
);
wp_set_current_user( $user->ID );
$this->assertSame( 0, did_action( $error ) );
$post = self::factory()->post->create_and_get( array(
'post_status' => 'draft',
'post_author' => $user->ID,
) );
$data = array(
$post = self::factory()->post->create_and_get(
array(
'post_status' => 'draft',
'post_author' => $user->ID,
)
);
$data = array(
'comment_post_ID' => $post->ID,
);
$comment = wp_handle_comment_submission( $data );
@@ -123,13 +131,15 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$this->assertSame( 0, did_action( $error ) );
$post = self::factory()->post->create_and_get( array(
'post_date' => date( 'Y-m-d H:i:s', strtotime( '+1 day' ) ),
) );
$post = self::factory()->post->create_and_get(
array(
'post_date' => date( 'Y-m-d H:i:s', strtotime( '+1 day' ) ),
)
);
$this->assertSame( 'future', $post->post_status );
$data = array(
$data = array(
'comment_post_ID' => $post->ID,
);
$comment = wp_handle_comment_submission( $data );
@@ -146,10 +156,12 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$this->assertSame( 0, did_action( $error ) );
$post = self::factory()->post->create_and_get( array(
'post_password' => 'password',
) );
$data = array(
$post = self::factory()->post->create_and_get(
array(
'post_password' => 'password',
)
);
$data = array(
'comment_post_ID' => $post->ID,
);
$comment = wp_handle_comment_submission( $data );
@@ -165,12 +177,14 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$password = 'password';
$hasher = new PasswordHash( 8, true );
$_COOKIE['wp-postpass_' . COOKIEHASH] = $hasher->HashPassword( $password );
$_COOKIE[ 'wp-postpass_' . COOKIEHASH ] = $hasher->HashPassword( $password );
$post = self::factory()->post->create_and_get( array(
'post_password' => $password,
) );
$data = array(
$post = self::factory()->post->create_and_get(
array(
'post_password' => $password,
)
);
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment',
'author' => 'Comment Author',
@@ -178,7 +192,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
);
$comment = wp_handle_comment_submission( $data );
unset( $_COOKIE['wp-postpass_' . COOKIEHASH] );
unset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
$this->assertNotWPError( $comment );
$this->assertInstanceOf( 'WP_Comment', $comment );
@@ -187,14 +201,16 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
public function test_submitting_valid_comment_as_logged_in_user_succeeds() {
$user = self::factory()->user->create_and_get( array(
'user_url' => 'http://user.example.org'
) );
$user = self::factory()->user->create_and_get(
array(
'user_url' => 'http://user.example.org',
)
);
wp_set_current_user( $user->ID );
$post = self::factory()->post->create_and_get();
$data = array(
$post = self::factory()->post->create_and_get();
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment',
);
@@ -203,7 +219,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$this->assertNotWPError( $comment );
$this->assertInstanceOf( 'WP_Comment', $comment );
$this->assertSame( 'Comment', $comment->comment_content);
$this->assertSame( 'Comment', $comment->comment_content );
$this->assertSame( $user->display_name, $comment->comment_author );
$this->assertSame( $user->user_email, $comment->comment_author_email );
$this->assertSame( $user->user_url, $comment->comment_author_url );
@@ -213,20 +229,20 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
public function test_submitting_valid_comment_anonymously_succeeds() {
$post = self::factory()->post->create_and_get();
$data = array(
$post = self::factory()->post->create_and_get();
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment',
'author' => 'Comment Author',
'email' => 'comment@example.org',
'url' => 'user.example.org'
'url' => 'user.example.org',
);
$comment = wp_handle_comment_submission( $data );
$this->assertNotWPError( $comment );
$this->assertInstanceOf( 'WP_Comment', $comment );
$this->assertSame( 'Comment', $comment->comment_content);
$this->assertSame( 'Comment', $comment->comment_content );
$this->assertSame( 'Comment Author', $comment->comment_author );
$this->assertSame( 'comment@example.org', $comment->comment_author_email );
$this->assertSame( 'http://user.example.org', $comment->comment_author_url );
@@ -241,8 +257,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
*/
public function test_submitting_comment_handles_slashes_correctly_handles_slashes() {
$post = self::factory()->post->create_and_get();
$data = array(
$post = self::factory()->post->create_and_get();
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment with 1 slash: \\',
'author' => 'Comment Author with 1 slash: \\',
@@ -253,7 +269,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$this->assertNotWPError( $comment );
$this->assertInstanceOf( 'WP_Comment', $comment );
$this->assertSame( 'Comment with 1 slash: \\', $comment->comment_content);
$this->assertSame( 'Comment with 1 slash: \\', $comment->comment_content );
$this->assertSame( 'Comment Author with 1 slash: \\', $comment->comment_author );
$this->assertSame( 'comment@example.org', $comment->comment_author_email );
@@ -263,10 +279,12 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$error = 'comment_id_not_found';
$post = self::factory()->post->create_and_get( array(
'post_status' => 'private',
) );
$data = array(
$post = self::factory()->post->create_and_get(
array(
'post_status' => 'private',
)
);
$data = array(
'comment_post_ID' => $post->ID,
);
$comment = wp_handle_comment_submission( $data );
@@ -279,22 +297,28 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
public function test_submitting_comment_as_logged_in_user_to_inaccessible_private_post_returns_error() {
$error = 'comment_id_not_found';
$error = 'comment_id_not_found';
$author = self::factory()->user->create_and_get( array(
'role' => 'author',
) );
$user = self::factory()->user->create_and_get( array(
'role' => 'author',
) );
$author = self::factory()->user->create_and_get(
array(
'role' => 'author',
)
);
$user = self::factory()->user->create_and_get(
array(
'role' => 'author',
)
);
wp_set_current_user( $user->ID );
$post = self::factory()->post->create_and_get( array(
'post_status' => 'private',
'post_author' => $author->ID,
) );
$data = array(
$post = self::factory()->post->create_and_get(
array(
'post_status' => 'private',
'post_author' => $author->ID,
)
);
$data = array(
'comment_post_ID' => $post->ID,
);
$comment = wp_handle_comment_submission( $data );
@@ -307,23 +331,29 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
public function test_submitting_comment_to_private_post_with_closed_comments_returns_correct_error() {
$error = 'comment_id_not_found';
$error = 'comment_id_not_found';
$author = self::factory()->user->create_and_get( array(
'role' => 'author',
) );
$user = self::factory()->user->create_and_get( array(
'role' => 'author',
) );
$author = self::factory()->user->create_and_get(
array(
'role' => 'author',
)
);
$user = self::factory()->user->create_and_get(
array(
'role' => 'author',
)
);
wp_set_current_user( $user->ID );
$post = self::factory()->post->create_and_get( array(
'post_status' => 'private',
'post_author' => $author->ID,
'comment_status' => 'closed',
) );
$data = array(
$post = self::factory()->post->create_and_get(
array(
'post_status' => 'private',
'post_author' => $author->ID,
'comment_status' => 'closed',
)
);
$data = array(
'comment_post_ID' => $post->ID,
);
$comment = wp_handle_comment_submission( $data );
@@ -340,11 +370,13 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
wp_set_current_user( $user->ID );
$post = self::factory()->post->create_and_get( array(
'post_status' => 'private',
'post_author' => $user->ID,
) );
$data = array(
$post = self::factory()->post->create_and_get(
array(
'post_status' => 'private',
'post_author' => $user->ID,
)
);
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment',
);
@@ -358,20 +390,26 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
public function test_submitting_comment_to_accessible_private_post_succeeds() {
$author = self::factory()->user->create_and_get( array(
'role' => 'author',
) );
$user = self::factory()->user->create_and_get( array(
'role' => 'editor',
) );
$author = self::factory()->user->create_and_get(
array(
'role' => 'author',
)
);
$user = self::factory()->user->create_and_get(
array(
'role' => 'editor',
)
);
wp_set_current_user( $user->ID );
$post = self::factory()->post->create_and_get( array(
'post_status' => 'private',
'post_author' => $author->ID,
) );
$data = array(
$post = self::factory()->post->create_and_get(
array(
'post_status' => 'private',
'post_author' => $author->ID,
)
);
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment',
);
@@ -385,8 +423,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
public function test_anonymous_user_cannot_comment_unfiltered_html() {
$post = self::factory()->post->create_and_get();
$data = array(
$post = self::factory()->post->create_and_get();
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment <script>alert(document.cookie);</script>',
'author' => 'Comment Author',
@@ -402,15 +440,17 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
public function test_unprivileged_user_cannot_comment_unfiltered_html() {
$user = self::factory()->user->create_and_get( array(
'role' => 'author',
) );
$user = self::factory()->user->create_and_get(
array(
'role' => 'author',
)
);
wp_set_current_user( $user->ID );
$this->assertFalse( current_user_can( 'unfiltered_html' ) );
$post = self::factory()->post->create_and_get();
$data = array(
$post = self::factory()->post->create_and_get();
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment <script>alert(document.cookie);</script>',
);
@@ -424,9 +464,11 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
public function test_unprivileged_user_cannot_comment_unfiltered_html_even_with_valid_nonce() {
$user = self::factory()->user->create_and_get( array(
'role' => 'author',
) );
$user = self::factory()->user->create_and_get(
array(
'role' => 'author',
)
);
wp_set_current_user( $user->ID );
$this->assertFalse( current_user_can( 'unfiltered_html' ) );
@@ -437,7 +479,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$this->assertNotEmpty( wp_verify_nonce( $nonce, $action ) );
$data = array(
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment <script>alert(document.cookie);</script>',
'_wp_unfiltered_html_comment' => $nonce,
@@ -454,9 +496,11 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$this->assertFalse( defined( 'DISALLOW_UNFILTERED_HTML' ) );
$user = self::factory()->user->create_and_get( array(
'role' => 'editor',
) );
$user = self::factory()->user->create_and_get(
array(
'role' => 'editor',
)
);
if ( is_multisite() ) {
// In multisite, only Super Admins can post unfiltered HTML
@@ -474,7 +518,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$this->assertNotEmpty( wp_verify_nonce( $nonce, $action ) );
$data = array(
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment <script>alert(document.cookie);</script>',
'_wp_unfiltered_html_comment' => $nonce,
@@ -489,9 +533,11 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
public function test_privileged_user_cannot_comment_unfiltered_html_without_valid_nonce() {
$user = self::factory()->user->create_and_get( array(
'role' => 'editor',
) );
$user = self::factory()->user->create_and_get(
array(
'role' => 'editor',
)
);
if ( is_multisite() ) {
// In multisite, only Super Admins can post unfiltered HTML
@@ -503,8 +549,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$this->assertTrue( current_user_can( 'unfiltered_html' ) );
$post = self::factory()->post->create_and_get();
$data = array(
$post = self::factory()->post->create_and_get();
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment <script>alert(document.cookie);</script>',
);
@@ -523,8 +569,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$_comment_registration = get_option( 'comment_registration' );
update_option( 'comment_registration', '1' );
$post = self::factory()->post->create_and_get();
$data = array(
$post = self::factory()->post->create_and_get();
$data = array(
'comment_post_ID' => $post->ID,
);
$comment = wp_handle_comment_submission( $data );
@@ -543,8 +589,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$_require_name_email = get_option( 'require_name_email' );
update_option( 'require_name_email', '1' );
$post = self::factory()->post->create_and_get();
$data = array(
$post = self::factory()->post->create_and_get();
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment',
'email' => 'comment@example.org',
@@ -565,8 +611,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$_require_name_email = get_option( 'require_name_email' );
update_option( 'require_name_email', '1' );
$post = self::factory()->post->create_and_get();
$data = array(
$post = self::factory()->post->create_and_get();
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment',
'author' => 'Comment Author',
@@ -587,8 +633,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$_require_name_email = get_option( 'require_name_email' );
update_option( 'require_name_email', '1' );
$post = self::factory()->post->create_and_get();
$data = array(
$post = self::factory()->post->create_and_get();
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment',
'author' => 'Comment Author',
@@ -607,8 +653,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$error = 'require_valid_comment';
$post = self::factory()->post->create_and_get();
$data = array(
$post = self::factory()->post->create_and_get();
$data = array(
'comment_post_ID' => $post->ID,
'comment' => '',
'author' => 'Comment Author',
@@ -629,7 +675,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$post = self::factory()->post->create_and_get();
$data = array(
$data = array(
'comment_post_ID' => $post->ID,
'comment' => rand_long_str( 65536 ),
'author' => 'Comment Author',
@@ -649,7 +695,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$post = self::factory()->post->create_and_get();
$data = array(
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment',
'author' => rand_long_str( 255 ),
@@ -669,7 +715,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$post = self::factory()->post->create_and_get();
$data = array(
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment',
'author' => 'Comment Author',
@@ -687,8 +733,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
public function test_submitting_comment_with_url_too_long_returns_error() {
$error = 'comment_author_url_column_length';
$post = self::factory()->post->create_and_get();
$data = array(
$post = self::factory()->post->create_and_get();
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Comment',
'author' => 'Comment Author',
@@ -706,9 +752,11 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
*/
public function test_comment_submission_sends_all_expected_parameters_to_preprocess_comment_filter() {
$user = self::factory()->user->create_and_get( array(
'role' => 'author',
) );
$user = self::factory()->user->create_and_get(
array(
'role' => 'author',
)
);
wp_set_current_user( $user->ID );
$post = self::factory()->post->create_and_get();
@@ -724,17 +772,19 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
remove_filter( 'preprocess_comment', array( $this, 'filter_preprocess_comment' ) );
$this->assertNotWPError( $comment );
$this->assertEquals( array(
'comment_post_ID' => $post->ID,
'comment_author' => $user->display_name,
'comment_author_email' => $user->user_email,
'comment_author_url' => $user->user_url,
'comment_content' => $data['comment'],
'comment_type' => '',
'comment_parent' => '0',
'user_ID' => $user->ID,
'user_id' => $user->ID,
), $this->preprocess_comment_data );
$this->assertEquals(
array(
'comment_post_ID' => $post->ID,
'comment_author' => $user->display_name,
'comment_author_email' => $user->user_email,
'comment_author_url' => $user->user_url,
'comment_content' => $data['comment'],
'comment_type' => '',
'comment_parent' => '0',
'user_ID' => $user->ID,
'user_id' => $user->ID,
), $this->preprocess_comment_data
);
}
@@ -747,16 +797,18 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
* @ticket 36901
*/
public function test_submitting_duplicate_comments() {
$post = self::factory()->post->create_and_get( array(
'post_status' => 'publish',
) );
$data = array(
$post = self::factory()->post->create_and_get(
array(
'post_status' => 'publish',
)
);
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Did I say that?',
'author' => 'Repeat myself',
'email' => 'mail@example.com',
);
$first_comment = wp_handle_comment_submission( $data );
$first_comment = wp_handle_comment_submission( $data );
$second_comment = wp_handle_comment_submission( $data );
$this->assertWPError( $second_comment );
$this->assertSame( 'comment_duplicate', $second_comment->get_error_code() );
@@ -766,10 +818,12 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
* @ticket 36901
*/
public function test_comments_flood() {
$post = self::factory()->post->create_and_get( array(
'post_status' => 'publish',
) );
$data = array(
$post = self::factory()->post->create_and_get(
array(
'post_status' => 'publish',
)
);
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Did I say that?',
'author' => 'Repeat myself',
@@ -778,7 +832,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$first_comment = wp_handle_comment_submission( $data );
$data['comment'] = 'Wow! I am quick!';
$second_comment = wp_handle_comment_submission( $data );
$second_comment = wp_handle_comment_submission( $data );
$this->assertWPError( $second_comment );
$this->assertSame( 'comment_flood', $second_comment->get_error_code() );
@@ -788,15 +842,19 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
* @ticket 36901
*/
public function test_comments_flood_user_is_admin() {
$user = self::factory()->user->create_and_get( array(
'role' => 'administrator',
) );
$user = self::factory()->user->create_and_get(
array(
'role' => 'administrator',
)
);
wp_set_current_user( $user->ID );
$post = self::factory()->post->create_and_get( array(
'post_status' => 'publish',
) );
$data = array(
$post = self::factory()->post->create_and_get(
array(
'post_status' => 'publish',
)
);
$data = array(
'comment_post_ID' => $post->ID,
'comment' => 'Did I say that?',
'author' => 'Repeat myself',
@@ -805,7 +863,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$first_comment = wp_handle_comment_submission( $data );
$data['comment'] = 'Wow! I am quick!';
$second_comment = wp_handle_comment_submission( $data );
$second_comment = wp_handle_comment_submission( $data );
$this->assertNotWPError( $second_comment );
$this->assertEquals( $post->ID, $second_comment->comment_post_ID );
+251 -137
View File
@@ -13,29 +13,58 @@ class Tests_Comment extends WP_UnitTestCase {
}
public static function wpSetUpBeforeClass( $factory ) {
self::$user_id = $factory->user->create( array(
'role' => 'author',
'user_login' => 'test_wp_user_get',
'user_pass' => 'password',
'user_email' => 'test@test.com',
) );
self::$user_id = $factory->user->create(
array(
'role' => 'author',
'user_login' => 'test_wp_user_get',
'user_pass' => 'password',
'user_email' => 'test@test.com',
)
);
self::$post_id = $factory->post->create( array(
'post_author' => self::$user_id
) );
self::$post_id = $factory->post->create(
array(
'post_author' => self::$user_id,
)
);
}
function test_wp_update_comment() {
$post = self::factory()->post->create_and_get( array( 'post_title' => 'some-post', 'post_type' => 'post' ) );
$post2 = self::factory()->post->create_and_get( array( 'post_title' => 'some-post-2', 'post_type' => 'post' ) );
$post = self::factory()->post->create_and_get(
array(
'post_title' => 'some-post',
'post_type' => 'post',
)
);
$post2 = self::factory()->post->create_and_get(
array(
'post_title' => 'some-post-2',
'post_type' => 'post',
)
);
$comments = self::factory()->comment->create_post_comments( $post->ID, 5 );
$result = wp_update_comment( array( 'comment_ID' => $comments[0], 'comment_parent' => $comments[1] ) );
$result = wp_update_comment(
array(
'comment_ID' => $comments[0],
'comment_parent' => $comments[1],
)
);
$this->assertEquals( 1, $result );
$comment = get_comment( $comments[0] );
$this->assertEquals( $comments[1], $comment->comment_parent );
$result = wp_update_comment( array( 'comment_ID' => $comments[0], 'comment_parent' => $comments[1] ) );
$result = wp_update_comment(
array(
'comment_ID' => $comments[0],
'comment_parent' => $comments[1],
)
);
$this->assertEquals( 0, $result );
$result = wp_update_comment( array( 'comment_ID' => $comments[0], 'comment_post_ID' => $post2->ID ) );
$result = wp_update_comment(
array(
'comment_ID' => $comments[0],
'comment_post_ID' => $post2->ID,
)
);
$comment = get_comment( $comments[0] );
$this->assertEquals( $post2->ID, $comment->comment_post_ID );
}
@@ -46,7 +75,12 @@ class Tests_Comment extends WP_UnitTestCase {
function test_wp_update_comment_updates_comment_type() {
$comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
wp_update_comment( array( 'comment_ID' => $comment_id, 'comment_type' => 'pingback' ) );
wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_type' => 'pingback',
)
);
$comment = get_comment( $comment_id );
$this->assertEquals( 'pingback', $comment->comment_type );
@@ -57,13 +91,15 @@ class Tests_Comment extends WP_UnitTestCase {
*/
function test_wp_update_comment_updates_comment_meta() {
$comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
wp_update_comment( array(
'comment_ID' => $comment_id,
'comment_meta' => array(
'food' => 'taco',
'sauce' => 'fire',
),
) );
wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_meta' => array(
'food' => 'taco',
'sauce' => 'fire',
),
)
);
$this->assertEquals( 'fire', get_comment_meta( $comment_id, 'sauce', true ) );
}
@@ -73,7 +109,12 @@ class Tests_Comment extends WP_UnitTestCase {
function test_wp_update_comment_updates_user_id() {
$comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
wp_update_comment( array( 'comment_ID' => $comment_id, 'user_id' => 1 ) );
wp_update_comment(
array(
'comment_ID' => $comment_id,
'user_id' => 1,
)
);
$comment = get_comment( $comment_id );
$this->assertEquals( 1, $comment->user_id );
@@ -87,7 +128,13 @@ class Tests_Comment extends WP_UnitTestCase {
$updated_comment_text = 'I should be able to update a comment with a Post ID of zero';
$update = wp_update_comment( array( 'comment_ID' => $comment_id, 'comment_content' => $updated_comment_text, 'comment_post_ID' => 0 ) );
$update = wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_content' => $updated_comment_text,
'comment_post_ID' => 0,
)
);
$this->assertSame( 1, $update );
$comment = get_comment( $comment_id );
@@ -95,27 +142,52 @@ class Tests_Comment extends WP_UnitTestCase {
}
public function test_get_approved_comments() {
$ca1 = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id, 'comment_approved' => '1'
) );
$ca2 = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id, 'comment_approved' => '1'
) );
$ca3 = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id, 'comment_approved' => '0'
) );
$c2 = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'pingback'
) );
$c3 = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'trackback'
) );
$c4 = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'mario'
) );
$c5 = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id, 'comment_approved' => '1', 'comment_type' => 'luigi'
) );
$ca1 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
)
);
$ca2 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
)
);
$ca3 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '0',
)
);
$c2 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_type' => 'pingback',
)
);
$c3 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_type' => 'trackback',
)
);
$c4 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_type' => 'mario',
)
);
$c5 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_type' => 'luigi',
)
);
$found = get_approved_comments( self::$post_id );
@@ -127,9 +199,12 @@ class Tests_Comment extends WP_UnitTestCase {
* @ticket 30412
*/
public function test_get_approved_comments_with_post_id_0_should_return_empty_array() {
$ca1 = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id, 'comment_approved' => '1'
) );
$ca1 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
)
);
$found = get_approved_comments( 0 );
@@ -141,14 +216,14 @@ class Tests_Comment extends WP_UnitTestCase {
*/
public function test_wp_new_comment_respects_dates() {
$data = array(
'comment_post_ID' => self::$post_id,
'comment_author' => 'Comment Author',
'comment_author_url' => '',
'comment_post_ID' => self::$post_id,
'comment_author' => 'Comment Author',
'comment_author_url' => '',
'comment_author_email' => '',
'comment_type' => '',
'comment_content' => 'Comment',
'comment_date' => '2011-01-01 10:00:00',
'comment_date_gmt' => '2011-01-01 10:00:00',
'comment_type' => '',
'comment_content' => 'Comment',
'comment_date' => '2011-01-01 10:00:00',
'comment_date_gmt' => '2011-01-01 10:00:00',
);
$id = wp_new_comment( $data );
@@ -270,14 +345,14 @@ class Tests_Comment extends WP_UnitTestCase {
public function test_comment_field_lengths() {
$data = array(
'comment_post_ID' => self::$post_id,
'comment_author' => 'Comment Author',
'comment_author_url' => '',
'comment_post_ID' => self::$post_id,
'comment_author' => 'Comment Author',
'comment_author_url' => '',
'comment_author_email' => '',
'comment_type' => '',
'comment_content' => str_repeat( 'A', 65536 ),
'comment_date' => '2011-01-01 10:00:00',
'comment_date_gmt' => '2011-01-01 10:00:00',
'comment_type' => '',
'comment_content' => str_repeat( 'A', 65536 ),
'comment_date' => '2011-01-01 10:00:00',
'comment_date_gmt' => '2011-01-01 10:00:00',
);
$id = wp_new_comment( $data );
@@ -291,31 +366,39 @@ class Tests_Comment extends WP_UnitTestCase {
* @ticket 32566
*/
public function test_wp_notify_moderator_should_not_throw_notice_when_post_author_is_0() {
$p = self::factory()->post->create( array(
'post_author' => 0,
) );
$p = self::factory()->post->create(
array(
'post_author' => 0,
)
);
$c = self::factory()->comment->create( array(
'comment_post_ID' => $p,
) );
$c = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
)
);
$this->assertTrue( wp_notify_moderator( $c ) );
}
public function test_wp_new_comment_notify_postauthor_should_send_email_when_comment_is_approved() {
$c = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id,
) );
$c = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
)
);
$sent = wp_new_comment_notify_postauthor( $c );
$this->assertTrue( $sent );
}
public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_is_unapproved() {
$c = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '0',
) );
$c = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '0',
)
);
$sent = wp_new_comment_notify_postauthor( $c );
$this->assertFalse( $sent );
@@ -325,10 +408,12 @@ class Tests_Comment extends WP_UnitTestCase {
* @ticket 33587
*/
public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_has_been_marked_as_spam() {
$c = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id,
'comment_approved' => 'spam',
) );
$c = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => 'spam',
)
);
$sent = wp_new_comment_notify_postauthor( $c );
$this->assertFalse( $sent );
@@ -338,10 +423,12 @@ class Tests_Comment extends WP_UnitTestCase {
* @ticket 35006
*/
public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_has_been_trashed() {
$c = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id,
'comment_approved' => 'trash',
) );
$c = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => 'trash',
)
);
$sent = wp_new_comment_notify_postauthor( $c );
$this->assertFalse( $sent );
@@ -351,13 +438,15 @@ class Tests_Comment extends WP_UnitTestCase {
* @ticket 12431
*/
public function test_wp_new_comment_with_meta() {
$c = self::factory()->comment->create( array(
'comment_approved' => '1',
'comment_meta' => array(
'food' => 'taco',
'sauce' => 'fire'
$c = self::factory()->comment->create(
array(
'comment_approved' => '1',
'comment_meta' => array(
'food' => 'taco',
'sauce' => 'fire',
),
)
) );
);
$this->assertEquals( 'fire', get_comment_meta( $c, 'sauce', true ) );
}
@@ -366,41 +455,53 @@ class Tests_Comment extends WP_UnitTestCase {
* @ticket 8071
*/
public function test_wp_comment_get_children_should_fill_children() {
$c1 = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
) );
$c1 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
)
);
$c2 = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_parent' => $c1,
) );
$c2 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_parent' => $c1,
)
);
$c3 = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_parent' => $c2,
) );
$c3 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_parent' => $c2,
)
);
$c4 = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_parent' => $c1,
) );
$c4 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_parent' => $c1,
)
);
$c5 = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
) );
$c5 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
)
);
$c6 = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_parent' => $c5,
) );
$c6 = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_parent' => $c5,
)
);
$comment = get_comment( $c1 );
$comment = get_comment( $c1 );
$children = $comment->get_children();
// Direct descendants of $c1.
@@ -416,7 +517,7 @@ class Tests_Comment extends WP_UnitTestCase {
public function test_post_properties_should_be_lazyloaded() {
$c = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
$post = get_post( self::$post_id );
$post = get_post( self::$post_id );
$comment = get_comment( $c );
$post_fields = array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_content_filtered', 'post_parent', 'guid', 'menu_order', 'post_type', 'post_mime_type', 'comment_count' );
@@ -434,7 +535,7 @@ class Tests_Comment extends WP_UnitTestCase {
* @since 4.4.0
* @access public
*/
public function setup_notify_comment(){
public function setup_notify_comment() {
/**
* Prevent flood alert from firing.
*/
@@ -443,13 +544,17 @@ class Tests_Comment extends WP_UnitTestCase {
/**
* Set up a comment for testing.
*/
$post = $this->factory->post->create( array(
'post_author' => self::$user_id,
) );
$post = $this->factory->post->create(
array(
'post_author' => self::$user_id,
)
);
$comment = $this->factory->comment->create( array(
'comment_post_ID' => $post,
) );
$comment = $this->factory->comment->create(
array(
'comment_post_ID' => $post,
)
);
return array(
'post' => $post,
@@ -632,7 +737,7 @@ class Tests_Comment extends WP_UnitTestCase {
// Close comments more than one day old.
update_option( 'close_comments_days_old', 1 );
$old_date = strtotime( '-25 hours' );
$old_date = strtotime( '-25 hours' );
$old_post_id = self::factory()->post->create( array( 'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $old_date ) ) );
$old_post_comment_status = _close_comments_for_old_post( true, $old_post_id );
@@ -643,7 +748,12 @@ class Tests_Comment extends WP_UnitTestCase {
}
public function test_close_comments_for_old_post_undated_draft() {
$draft_id = self::factory()->post->create( array( 'post_status' => 'draft', 'post_type' => 'post' ) );
$draft_id = self::factory()->post->create(
array(
'post_status' => 'draft',
'post_type' => 'post',
)
);
$draft_comment_status = _close_comments_for_old_post( true, $draft_id );
$this->assertTrue( $draft_comment_status );
@@ -673,11 +783,13 @@ class Tests_Comment extends WP_UnitTestCase {
$this->assertSame( $default_data['comment_agent'], $save->comment_agent );
// Update the comment.
wp_update_comment( array(
'comment_ID' => $comment_id,
'comment_author_IP' => '111.111.1.1',
'comment_agent' => 'SHIELD_AGENT',
) );
wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_author_IP' => '111.111.1.1',
'comment_agent' => 'SHIELD_AGENT',
)
);
// Retrieve and check the new values.
$updated = get_comment( $comment_id );
@@ -708,10 +820,12 @@ class Tests_Comment extends WP_UnitTestCase {
$comment = get_comment( $c );
$this->assertSame( 'Foo', $comment->comment_author );
wp_update_comment( array(
'comment_ID' => $c,
'comment_author' => 'Bar',
) );
wp_update_comment(
array(
'comment_ID' => $c,
'comment_author' => 'Bar',
)
);
$comment = get_comment( $c );
+32 -19
View File
@@ -34,8 +34,8 @@ class Tests_Comment_CheckComment extends WP_UnitTestCase {
}
public function test_should_return_true_when_comment_whitelist_is_enabled_and_author_has_approved_comment() {
$post_id = self::factory()->post->create();
$prev_args = array(
$post_id = self::factory()->post->create();
$prev_args = array(
'comment_post_ID' => $post_id,
'comment_content' => 'Can we build it?',
'comment_approved' => 0,
@@ -58,10 +58,12 @@ class Tests_Comment_CheckComment extends WP_UnitTestCase {
$this->assertFalse( $results );
// Approve the previous comment.
wp_update_comment( array(
'comment_ID' => $prev_comment_id,
'comment_approved' => 1,
) );
wp_update_comment(
array(
'comment_ID' => $prev_comment_id,
'comment_approved' => 1,
)
);
$results = check_comment( $author, $author_email, $author_url, $comment, $author_ip, $user_agent, $comment_type );
$this->assertTrue( $results );
}
@@ -77,7 +79,7 @@ class Tests_Comment_CheckComment extends WP_UnitTestCase {
$user_agent = '';
$comment_type = '';
update_option( 'moderation_keys',"foo\nbar\nscoop" );
update_option( 'moderation_keys', "foo\nbar\nscoop" );
$results = check_comment( $author, $author_email, $author_url, $comment, $author_ip, $user_agent, $comment_type );
$this->assertFalse( $results );
}
@@ -93,7 +95,7 @@ class Tests_Comment_CheckComment extends WP_UnitTestCase {
$user_agent = '';
$comment_type = '';
update_option( 'moderation_keys',"foo\nbar" );
update_option( 'moderation_keys', "foo\nbar" );
$results = check_comment( $author, $author_email, $author_url, $comment, $author_ip, $user_agent, $comment_type );
$this->assertTrue( $results );
}
@@ -134,15 +136,24 @@ class Tests_Comment_CheckComment extends WP_UnitTestCase {
* @ticket 28603
*/
public function test_should_return_true_when_comment_whitelist_is_enabled_and_user_has_previously_approved_comments_with_different_email() {
$subscriber_id = $this->factory()->user->create( array(
'role' => 'subscriber',
'email' => 'sub@example.com',
) );
$subscriber_id = $this->factory()->user->create(
array(
'role' => 'subscriber',
'email' => 'sub@example.com',
)
);
// Make sure comment author has an approved comment.
$this->factory->comment->create( array( 'user_id' => $subscriber_id, 'comment_approved' => '1', 'comment_author' => 'foo', 'comment_author_email' => 'sub@example.com' ) );
$this->factory->comment->create(
array(
'user_id' => $subscriber_id,
'comment_approved' => '1',
'comment_author' => 'foo',
'comment_author_email' => 'sub@example.com',
)
);
$subscriber_user = new WP_User( $subscriber_id );
$subscriber_user = new WP_User( $subscriber_id );
$subscriber_user->user_email = 'newsub@example.com';
wp_update_user( $subscriber_user );
@@ -157,12 +168,14 @@ class Tests_Comment_CheckComment extends WP_UnitTestCase {
* @ticket 28603
*/
public function test_should_return_false_when_comment_whitelist_is_enabled_and_user_does_not_have_a_previously_approved_comment_with_any_email() {
$subscriber_id = $this->factory()->user->create( array(
'role' => 'subscriber',
'email' => 'zig@example.com',
) );
$subscriber_id = $this->factory()->user->create(
array(
'role' => 'subscriber',
'email' => 'zig@example.com',
)
);
$subscriber_user = new WP_User( $subscriber_id );
$subscriber_user = new WP_User( $subscriber_id );
$subscriber_user->user_email = 'zag@example.com';
wp_update_user( $subscriber_user );
+12 -12
View File
@@ -8,8 +8,8 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase {
$p = self::factory()->post->create();
$args = array(
'name_submit' => 'foo-name',
'id_submit' => 'foo-id',
'name_submit' => 'foo-name',
'id_submit' => 'foo-id',
'class_submit' => 'foo-class',
'label_submit' => 'foo-label',
);
@@ -24,11 +24,11 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase {
$p = self::factory()->post->create();
$args = array(
'name_submit' => 'foo-name',
'id_submit' => 'foo-id',
'class_submit' => 'foo-class',
'label_submit' => 'foo-label',
'submit_button' => '<input name="custom-%1$s" type="submit" id="custom-%2$s" class="custom-%3$s" value="custom-%4$s" />'
'name_submit' => 'foo-name',
'id_submit' => 'foo-id',
'class_submit' => 'foo-class',
'label_submit' => 'foo-label',
'submit_button' => '<input name="custom-%1$s" type="submit" id="custom-%2$s" class="custom-%3$s" value="custom-%4$s" />',
);
$form = get_echo( 'comment_form', array( $args, $p ) );
@@ -40,11 +40,11 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase {
$p = self::factory()->post->create();
$args = array(
'name_submit' => 'foo-name',
'id_submit' => 'foo-id',
'name_submit' => 'foo-name',
'id_submit' => 'foo-id',
'class_submit' => 'foo-class',
'label_submit' => 'foo-label',
'submit_field' => '<p class="my-custom-submit-field">%1$s %2$s</p>'
'submit_field' => '<p class="my-custom-submit-field">%1$s %2$s</p>',
);
$form = get_echo( 'comment_form', array( $args, $p ) );
@@ -60,8 +60,8 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase {
$p = self::factory()->post->create();
$args = array(
'name_submit' => 'foo-name',
'id_submit' => 'foo-id',
'name_submit' => 'foo-name',
'id_submit' => 'foo-id',
'class_submit' => 'foo-class',
'label_submit' => 'foo-label',
);
File diff suppressed because it is too large Load Diff
+23 -17
View File
@@ -22,7 +22,7 @@ class Tests_Comment_DateQuery extends WP_UnitTestCase {
// Just some dummy posts to use as parents for comments
for ( $i = 1; $i <= 2; $i++ ) {
$this->posts[$i] = self::factory()->post->create();
$this->posts[ $i ] = self::factory()->post->create();
}
// Be careful modifying this. Tests are coded to expect this exact sample data.
@@ -39,31 +39,37 @@ class Tests_Comment_DateQuery extends WP_UnitTestCase {
);
foreach ( $comment_dates as $comment_date => $comment_parent ) {
$result = self::factory()->comment->create( array(
'comment_date' => $comment_date,
'comment_post_ID' => $this->posts[ $comment_parent ],
) );
$result = self::factory()->comment->create(
array(
'comment_date' => $comment_date,
'comment_post_ID' => $this->posts[ $comment_parent ],
)
);
}
}
public function _get_query_result( $args = array() ) {
$args = wp_parse_args( $args, array(
'post_id' => $this->posts[1],
'orderby' => 'comment_ID', // Same order they were created
'order' => 'ASC',
) );
$args = wp_parse_args(
$args, array(
'post_id' => $this->posts[1],
'orderby' => 'comment_ID', // Same order they were created
'order' => 'ASC',
)
);
return get_comments( $args );
}
public function test_year() {
$comments = $this->_get_query_result( array(
'date_query' => array(
array(
'year' => 2008,
$comments = $this->_get_query_result(
array(
'date_query' => array(
array(
'year' => 2008,
),
),
),
) );
)
);
$expected_dates = array(
'2008-03-29 09:04:25',
@@ -72,4 +78,4 @@ class Tests_Comment_DateQuery extends WP_UnitTestCase {
$this->assertEquals( $expected_dates, wp_list_pluck( $comments, 'comment_date' ) );
}
}
}
@@ -23,9 +23,11 @@ class Tests_Comment_GetCommentAuthorEmailLink extends WP_UnitTestCase {
}
public static function wpSetUpBeforeClass( $factory ) {
self::$comment = $factory->comment->create_and_get( array(
'comment_author_email' => 'foo@example.org'
) );
self::$comment = $factory->comment->create_and_get(
array(
'comment_author_email' => 'foo@example.org',
)
);
}
public function test_global_comment_with_default_parameters() {
@@ -43,9 +45,11 @@ class Tests_Comment_GetCommentAuthorEmailLink extends WP_UnitTestCase {
$linktext = 'linktext';
$before = 'before';
$after = 'after';
$comment = self::factory()->comment->create_and_get( array(
'comment_author_email' => $email = 'baz@example.org'
) );
$comment = self::factory()->comment->create_and_get(
array(
'comment_author_email' => $email = 'baz@example.org',
)
);
$expected = sprintf( '%1$s<a href="mailto:%2$s">%3$s</a>%4$s', $before, $email, $linktext, $after );
@@ -84,9 +88,11 @@ class Tests_Comment_GetCommentAuthorEmailLink extends WP_UnitTestCase {
* @ticket 36571
*/
public function test_comment_param_should_override_global() {
$comment = self::factory()->comment->create_and_get( array(
'comment_author_email' => $email = 'bar@example.org'
) );
$comment = self::factory()->comment->create_and_get(
array(
'comment_author_email' => $email = 'bar@example.org',
)
);
$expected = sprintf( '<a href="mailto:%1$s">%2$s</a>', $email, $email );
@@ -14,9 +14,11 @@ class Tests_Comment_GetCommentAuthorUrl extends WP_UnitTestCase {
* @ticket 41334
*/
public function test_comment_author_url_passes_correct_comment_id() {
$comment = self::factory()->comment->create_and_get( array(
'comment_post_ID' => 0,
) );
$comment = self::factory()->comment->create_and_get(
array(
'comment_post_ID' => 0,
)
);
add_filter( 'get_comment_author_url', array( $this, 'get_comment_author_url_filter' ), 99, 3 );
@@ -9,7 +9,7 @@ class Tests_Comment_GetCommentAuthorUrlLink extends WP_UnitTestCase {
public static function wpSetUpBeforeClass( $factory ) {
unset( $GLOBALS['comment'] );
$comment_ids = $factory->comment->create_post_comments( 0, 1 );
$comment_ids = $factory->comment->create_post_comments( 0, 1 );
self::$comments = array_map( 'get_comment', $comment_ids );
}
@@ -34,7 +34,7 @@ class Tests_Comment_GetCommentAuthorUrlLink extends WP_UnitTestCase {
$GLOBALS['comment'] = $comment = reset( self::$comments );
$url_link = get_comment_author_url_link();
$link = $this->parseCommentAuthorUrl( $comment );
$link = $this->parseCommentAuthorUrl( $comment );
$this->assertEquals( $link, $url_link );
}
@@ -42,7 +42,7 @@ class Tests_Comment_GetCommentAuthorUrlLink extends WP_UnitTestCase {
$comment = reset( self::$comments );
$url_link = get_comment_author_url_link( '', '', '', $comment );
$link = $this->parseCommentAuthorUrl( $comment );
$link = $this->parseCommentAuthorUrl( $comment );
$this->assertEquals( $link, $url_link );
}
@@ -50,7 +50,7 @@ class Tests_Comment_GetCommentAuthorUrlLink extends WP_UnitTestCase {
$comment = reset( self::$comments );
$url_link = get_comment_author_url_link( 'Burrito', '', '', $comment );
$link = $this->parseCommentAuthorUrl( $comment, 'Burrito' );
$link = $this->parseCommentAuthorUrl( $comment, 'Burrito' );
$this->assertEquals( $link, $url_link );
}
@@ -58,7 +58,7 @@ class Tests_Comment_GetCommentAuthorUrlLink extends WP_UnitTestCase {
$comment = reset( self::$comments );
$url_link = get_comment_author_url_link( 'Burrito', 'I would love a ', '', $comment );
$link = 'I would love a ' . $this->parseCommentAuthorUrl( $comment, 'Burrito' );
$link = 'I would love a ' . $this->parseCommentAuthorUrl( $comment, 'Burrito' );
$this->assertEquals( $link, $url_link );
}
@@ -66,7 +66,7 @@ class Tests_Comment_GetCommentAuthorUrlLink extends WP_UnitTestCase {
$comment = reset( self::$comments );
$url_link = get_comment_author_url_link( 'Burrito', '', ' is my favorite word.', $comment );
$link = $this->parseCommentAuthorUrl( $comment, 'Burrito' ) . ' is my favorite word.';
$link = $this->parseCommentAuthorUrl( $comment, 'Burrito' ) . ' is my favorite word.';
$this->assertEquals( $link, $url_link );
}
@@ -74,7 +74,7 @@ class Tests_Comment_GetCommentAuthorUrlLink extends WP_UnitTestCase {
$comment = reset( self::$comments );
$url_link = get_comment_author_url_link( 'Burrito', 'I would love a ', ' right now.', $comment );
$link = 'I would love a ' . $this->parseCommentAuthorUrl( $comment, 'Burrito' ) . ' right now.';
$link = 'I would love a ' . $this->parseCommentAuthorUrl( $comment, 'Burrito' ) . ' right now.';
$this->assertEquals( $link, $url_link );
}
}
+25 -15
View File
@@ -14,9 +14,11 @@ class Tests_Get_Comment_Count extends WP_UnitTestCase {
}
public function test_get_comment_count_approved() {
self::factory()->comment->create( array(
'comment_approved' => 1
) );
self::factory()->comment->create(
array(
'comment_approved' => 1,
)
);
$count = get_comment_count();
@@ -29,9 +31,11 @@ class Tests_Get_Comment_Count extends WP_UnitTestCase {
}
public function test_get_comment_count_awaiting() {
self::factory()->comment->create( array(
'comment_approved' => 0
) );
self::factory()->comment->create(
array(
'comment_approved' => 0,
)
);
$count = get_comment_count();
@@ -44,9 +48,11 @@ class Tests_Get_Comment_Count extends WP_UnitTestCase {
}
public function test_get_comment_count_spam() {
self::factory()->comment->create( array(
'comment_approved' => 'spam'
) );
self::factory()->comment->create(
array(
'comment_approved' => 'spam',
)
);
$count = get_comment_count();
@@ -59,9 +65,11 @@ class Tests_Get_Comment_Count extends WP_UnitTestCase {
}
public function test_get_comment_count_trash() {
self::factory()->comment->create( array(
'comment_approved' => 'trash'
) );
self::factory()->comment->create(
array(
'comment_approved' => 'trash',
)
);
$count = get_comment_count();
@@ -74,9 +82,11 @@ class Tests_Get_Comment_Count extends WP_UnitTestCase {
}
public function test_get_comment_count_post_trashed() {
self::factory()->comment->create( array(
'comment_approved' => 'post-trashed'
) );
self::factory()->comment->create(
array(
'comment_approved' => 'post-trashed',
)
);
$count = get_comment_count();
@@ -14,9 +14,11 @@ Shankle pork chop prosciutto ribeye ham hock pastrami. T-bone shank brisket baco
}
public function test_get_comment_excerpt() {
$comment_id = self::factory()->comment->create( array(
'comment_content' => self::$bacon_comment
) );
$comment_id = self::factory()->comment->create(
array(
'comment_content' => self::$bacon_comment,
)
);
$excerpt = get_comment_excerpt( $comment_id );
@@ -24,9 +26,11 @@ Shankle pork chop prosciutto ribeye ham hock pastrami. T-bone shank brisket baco
}
public function test_get_comment_excerpt_filtered() {
$comment_id = self::factory()->comment->create( array(
'comment_content' => self::$bacon_comment
) );
$comment_id = self::factory()->comment->create(
array(
'comment_content' => self::$bacon_comment,
)
);
add_filter( 'comment_excerpt_length', array( $this, '_filter_comment_excerpt_length' ) );
+43 -31
View File
@@ -8,39 +8,51 @@ class Tests_Comment_GetCommentLink extends WP_UnitTestCase {
protected static $comments = array();
public static function wpSetUpBeforeClass( $factory ) {
$now = time();
$now = time();
self::$p = self::factory()->post->create();
self::$comments[] = self::factory()->comment->create( array(
'comment_post_ID' => self::$p,
'comment_content' => '1',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
) );
self::$comments[] = self::factory()->comment->create( array(
'comment_post_ID' => self::$p,
'comment_content' => '2',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
) );
self::$comments[] = self::factory()->comment->create( array(
'comment_post_ID' => self::$p,
'comment_content' => '3',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
) );
self::$comments[] = self::factory()->comment->create( array(
'comment_post_ID' => self::$p,
'comment_content' => '4',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
) );
self::$comments[] = self::factory()->comment->create( array(
'comment_post_ID' => self::$p,
'comment_content' => '4',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 500 ),
) );
self::$comments[] = self::factory()->comment->create( array(
'comment_post_ID' => self::$p,
'comment_content' => '4',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 600 ),
) );
self::$comments[] = self::factory()->comment->create(
array(
'comment_post_ID' => self::$p,
'comment_content' => '1',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
)
);
self::$comments[] = self::factory()->comment->create(
array(
'comment_post_ID' => self::$p,
'comment_content' => '2',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
)
);
self::$comments[] = self::factory()->comment->create(
array(
'comment_post_ID' => self::$p,
'comment_content' => '3',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
)
);
self::$comments[] = self::factory()->comment->create(
array(
'comment_post_ID' => self::$p,
'comment_content' => '4',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
)
);
self::$comments[] = self::factory()->comment->create(
array(
'comment_post_ID' => self::$p,
'comment_content' => '4',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 500 ),
)
);
self::$comments[] = self::factory()->comment->create(
array(
'comment_post_ID' => self::$p,
'comment_content' => '4',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 600 ),
)
);
}
@@ -9,7 +9,7 @@ class Tests_Comment_GetCommentReplyLink extends WP_UnitTestCase {
*/
public function test_should_return_null_when_max_depth_is_less_than_depth() {
$args = array(
'depth' => 5,
'depth' => 5,
'max_depth' => 4,
);
@@ -1,148 +1,181 @@
<?php
/**
* Validate the logic of get_comments_pages_count
* @group comment
*/
class Tests_Comment_GetCommentsPagesCount extends WP_UnitTestCase {
protected $option_page_comments;
protected $option_comments_per_page;
protected $option_thread_comments;
protected $option_posts_per_rss;
/**
* setUp options
*/
function setUp() {
parent::setUp();
$this->option_page_comments = get_option( 'page_comments' );
$this->option_page_comments = get_option( 'comments_per_page' );
$this->option_page_comments = get_option( 'thread_comments' );
$this->option_posts_per_rss = get_option( 'posts_per_rss' );
update_option( 'page_comments', true );
}
/**
* tearDown options
*/
function tearDown() {
update_option( 'page_comments', $this->option_page_comments );
update_option( 'comments_per_page', $this->option_page_comments );
update_option( 'thread_comments', $this->option_page_comments );
update_option( 'posts_per_rss', $this->option_posts_per_rss );
parent::tearDown();
}
/**
* Validate get_comments_pages_count for empty comments
*/
function test_empty() {
//setup post and comments
$post_id = self::factory()->post->create( array( 'post_title' => 'comment--post', 'post_type' => 'post' ) );
$this->go_to( '/?p=' . $post_id );
global $wp_query;
unset( $wp_query->comments );
$comments = get_comments( array( 'post_id' => $post_id ) );
$this->assertEquals( 0, get_comment_pages_count( $comments, 10, false ) );
$this->assertEquals( 0, get_comment_pages_count( $comments, 1, false ) );
$this->assertEquals( 0, get_comment_pages_count( $comments, 0, false ) );
$this->assertEquals( 0, get_comment_pages_count( $comments, 10, true ) );
$this->assertEquals( 0, get_comment_pages_count( $comments, 5 ) );
$this->assertEquals( 0, get_comment_pages_count( $comments ) );
$this->assertequals( 0, get_comment_pages_count( null, 1 ) );
}
/**
* Validate get_comments_pages_count for treaded comments
*/
function test_threaded_comments( ) {
//setup post and comments
$post = self::factory()->post->create_and_get( array( 'post_title' => 'comment--post', 'post_type' => 'post' ) );
$comments = self::factory()->comment->create_post_comments( $post->ID, 15 );
self::factory()->comment->create_post_comments( $post->ID, 6, array( 'comment_parent' => $comments[0] ) );
$comments = get_comments( array( 'post_id' => $post->ID ) );
$this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) );
$this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) );
$this->assertEquals( 4, get_comment_pages_count( $comments, 4, true ) );
}
/**
* Validate get_comments_pages_count for option tread_comments
*/
function test_option_thread_comments() {
//setup post and comments
$post = self::factory()->post->create_and_get( array( 'post_title' => 'comment--post', 'post_type' => 'post' ) );
$comments = self::factory()->comment->create_post_comments( $post->ID, 15 );
self::factory()->comment->create_post_comments( $post->ID, 6, array('comment_parent' => $comments[0] ) );
$comments = get_comments( array( 'post_id' => $post->ID ) );
update_option( 'thread_comments', false );
$this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) );
$this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) );
$this->assertEquals( 3, get_comment_pages_count( $comments, 10, null ) );
$this->assertEquals( 3, get_comment_pages_count( $comments, 10 ) );
update_option( 'thread_comments', true );
$this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) );
$this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) );
$this->assertEquals( 2, get_comment_pages_count( $comments, 10, null ) );
$this->assertEquals( 2, get_comment_pages_count( $comments, 10 ) );
}
/**
* Validate $wp_query logic of get_comment_pages_count
*/
function test_wp_query_comments_per_page() {
global $wp_query;
update_option( 'posts_per_rss', 100 );
$post = self::factory()->post->create_and_get( array( 'post_title' => 'comment-post', 'post_type' => 'post' ) );
$comments = self::factory()->comment->create_post_comments( $post->ID, 25 );
$wp_query = new WP_Query( array( 'p' => $post->ID, 'comments_per_page' => 10, 'feed' =>'comments-' ) );
update_option( 'comments_per_page', 25 );
$this->assertEquals( 3, get_comment_pages_count() );
$this->assertEquals( 2, get_comment_pages_count( null, 20 ) );
$wp_query = new WP_Query( array( 'p' => $post->ID,'comments_per_page' => null, 'feed' =>'comments-' ) );
$this->assertEquals( 1, get_comment_pages_count() );
$this->assertEquals( 5, get_comment_pages_count( null, 5 ) );
$wp_query->query_vars['comments_per_page'] = null;
update_option( 'comments_per_page', 5 );
$this->assertEquals( 5, get_comment_pages_count() );
$this->assertEquals( 3, get_comment_pages_count( null, 11 ) );
$this->assertEquals( 5, get_comment_pages_count( null, 0 ) );
}
/**
* Validate max_num_comment_pages logic of get_comment_pages_count
*/
function test_max_num_comment_pages() {
global $wp_query;
$wp_query = new WP_Query();
$org_max_num_comment_pages = $wp_query->max_num_comment_pages;
$wp_query->max_num_comment_pages = 7;
$this->assertEquals( 7, get_comment_pages_count() );
$this->assertEquals( 7, get_comment_pages_count( null, null, null ) );
$this->assertEquals( 0, get_comment_pages_count( array(), null, null ) );
$wp_query->max_num_comment_pages = $org_max_num_comment_pages;
}
}
<?php
/**
* Validate the logic of get_comments_pages_count
*
* @group comment
*/
class Tests_Comment_GetCommentsPagesCount extends WP_UnitTestCase {
protected $option_page_comments;
protected $option_comments_per_page;
protected $option_thread_comments;
protected $option_posts_per_rss;
/**
* setUp options
*/
function setUp() {
parent::setUp();
$this->option_page_comments = get_option( 'page_comments' );
$this->option_page_comments = get_option( 'comments_per_page' );
$this->option_page_comments = get_option( 'thread_comments' );
$this->option_posts_per_rss = get_option( 'posts_per_rss' );
update_option( 'page_comments', true );
}
/**
* tearDown options
*/
function tearDown() {
update_option( 'page_comments', $this->option_page_comments );
update_option( 'comments_per_page', $this->option_page_comments );
update_option( 'thread_comments', $this->option_page_comments );
update_option( 'posts_per_rss', $this->option_posts_per_rss );
parent::tearDown();
}
/**
* Validate get_comments_pages_count for empty comments
*/
function test_empty() {
//setup post and comments
$post_id = self::factory()->post->create(
array(
'post_title' => 'comment--post',
'post_type' => 'post',
)
);
$this->go_to( '/?p=' . $post_id );
global $wp_query;
unset( $wp_query->comments );
$comments = get_comments( array( 'post_id' => $post_id ) );
$this->assertEquals( 0, get_comment_pages_count( $comments, 10, false ) );
$this->assertEquals( 0, get_comment_pages_count( $comments, 1, false ) );
$this->assertEquals( 0, get_comment_pages_count( $comments, 0, false ) );
$this->assertEquals( 0, get_comment_pages_count( $comments, 10, true ) );
$this->assertEquals( 0, get_comment_pages_count( $comments, 5 ) );
$this->assertEquals( 0, get_comment_pages_count( $comments ) );
$this->assertequals( 0, get_comment_pages_count( null, 1 ) );
}
/**
* Validate get_comments_pages_count for treaded comments
*/
function test_threaded_comments() {
//setup post and comments
$post = self::factory()->post->create_and_get(
array(
'post_title' => 'comment--post',
'post_type' => 'post',
)
);
$comments = self::factory()->comment->create_post_comments( $post->ID, 15 );
self::factory()->comment->create_post_comments( $post->ID, 6, array( 'comment_parent' => $comments[0] ) );
$comments = get_comments( array( 'post_id' => $post->ID ) );
$this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) );
$this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) );
$this->assertEquals( 4, get_comment_pages_count( $comments, 4, true ) );
}
/**
* Validate get_comments_pages_count for option tread_comments
*/
function test_option_thread_comments() {
//setup post and comments
$post = self::factory()->post->create_and_get(
array(
'post_title' => 'comment--post',
'post_type' => 'post',
)
);
$comments = self::factory()->comment->create_post_comments( $post->ID, 15 );
self::factory()->comment->create_post_comments( $post->ID, 6, array( 'comment_parent' => $comments[0] ) );
$comments = get_comments( array( 'post_id' => $post->ID ) );
update_option( 'thread_comments', false );
$this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) );
$this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) );
$this->assertEquals( 3, get_comment_pages_count( $comments, 10, null ) );
$this->assertEquals( 3, get_comment_pages_count( $comments, 10 ) );
update_option( 'thread_comments', true );
$this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) );
$this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) );
$this->assertEquals( 2, get_comment_pages_count( $comments, 10, null ) );
$this->assertEquals( 2, get_comment_pages_count( $comments, 10 ) );
}
/**
* Validate $wp_query logic of get_comment_pages_count
*/
function test_wp_query_comments_per_page() {
global $wp_query;
update_option( 'posts_per_rss', 100 );
$post = self::factory()->post->create_and_get(
array(
'post_title' => 'comment-post',
'post_type' => 'post',
)
);
$comments = self::factory()->comment->create_post_comments( $post->ID, 25 );
$wp_query = new WP_Query(
array(
'p' => $post->ID,
'comments_per_page' => 10,
'feed' => 'comments-',
)
);
update_option( 'comments_per_page', 25 );
$this->assertEquals( 3, get_comment_pages_count() );
$this->assertEquals( 2, get_comment_pages_count( null, 20 ) );
$wp_query = new WP_Query(
array(
'p' => $post->ID,
'comments_per_page' => null,
'feed' => 'comments-',
)
);
$this->assertEquals( 1, get_comment_pages_count() );
$this->assertEquals( 5, get_comment_pages_count( null, 5 ) );
$wp_query->query_vars['comments_per_page'] = null;
update_option( 'comments_per_page', 5 );
$this->assertEquals( 5, get_comment_pages_count() );
$this->assertEquals( 3, get_comment_pages_count( null, 11 ) );
$this->assertEquals( 5, get_comment_pages_count( null, 0 ) );
}
/**
* Validate max_num_comment_pages logic of get_comment_pages_count
*/
function test_max_num_comment_pages() {
global $wp_query;
$wp_query = new WP_Query();
$org_max_num_comment_pages = $wp_query->max_num_comment_pages;
$wp_query->max_num_comment_pages = 7;
$this->assertEquals( 7, get_comment_pages_count() );
$this->assertEquals( 7, get_comment_pages_count( null, null, null ) );
$this->assertEquals( 0, get_comment_pages_count( array(), null, null ) );
$wp_query->max_num_comment_pages = $org_max_num_comment_pages;
}
}
+196 -45
View File
@@ -28,32 +28,65 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
self::factory()->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-15 00:00:00' ) );
$comment_first = self::factory()->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-14 00:00:00' ) );
$this->assertEquals( 4, get_page_of_comment( $comment_last[0], array( 'per_page' => 3 ) ) );
$this->assertEquals( 2, get_page_of_comment( $comment_last[0], array( 'per_page' => 10 ) ) );
$this->assertEquals( 4, get_page_of_comment( $comment_last[0], array( 'per_page' => 3 ) ) );
$this->assertEquals( 2, get_page_of_comment( $comment_last[0], array( 'per_page' => 10 ) ) );
$this->assertEquals( 1, get_page_of_comment( $comment_first[0], array( 'per_page' => 3 ) ) );
$this->assertEquals( 1, get_page_of_comment( $comment_first[0], array( 'per_page' => 3 ) ) );
$this->assertEquals( 1, get_page_of_comment( $comment_first[0], array( 'per_page' => 10 ) ) );
}
public function test_type_pings() {
$p = self::factory()->post->create();
$p = self::factory()->post->create();
$now = time();
$trackbacks = array();
for ( $i = 0; $i <= 3; $i++ ) {
$trackbacks[ $i ] = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'trackback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
$now -= 10 * $i;
$trackbacks[ $i ] = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_type' => 'trackback',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ),
)
);
$now -= 10 * $i;
}
$pingbacks = array();
for ( $i = 0; $i <= 6; $i++ ) {
$pingbacks[ $i ] = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'pingback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
$now -= 10 * $i;
$pingbacks[ $i ] = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_type' => 'pingback',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ),
)
);
$now -= 10 * $i;
}
$this->assertEquals( 2, get_page_of_comment( $trackbacks[0], array( 'per_page' => 2, 'type' => 'trackback' ) ) );
$this->assertEquals( 3, get_page_of_comment( $pingbacks[0], array( 'per_page' => 2, 'type' => 'pingback' ) ) );
$this->assertEquals( 5, get_page_of_comment( $trackbacks[0], array( 'per_page' => 2, 'type' => 'pings' ) ) );
$this->assertEquals(
2, get_page_of_comment(
$trackbacks[0], array(
'per_page' => 2,
'type' => 'trackback',
)
)
);
$this->assertEquals(
3, get_page_of_comment(
$pingbacks[0], array(
'per_page' => 2,
'type' => 'pingback',
)
)
);
$this->assertEquals(
5, get_page_of_comment(
$trackbacks[0], array(
'per_page' => 2,
'type' => 'pings',
)
)
);
}
/**
@@ -69,7 +102,7 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
$page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
$num_queries = $wpdb->num_queries;
$page_2 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
$page_2 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
$this->assertSame( $page_1, $page_2 );
$this->assertSame( $num_queries, $wpdb->num_queries );
@@ -81,21 +114,42 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
public function test_cache_hits_should_be_sensitive_to_comment_type() {
global $wpdb;
$p = self::factory()->post->create();
$comment = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'comment' ) );
$p = self::factory()->post->create();
$comment = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_type' => 'comment',
)
);
$now = time();
$now = time();
$trackbacks = array();
for ( $i = 0; $i <= 5; $i++ ) {
$trackbacks[ $i ] = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'trackback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( 10 * $i ) ) ) );
$trackbacks[ $i ] = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_type' => 'trackback',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( 10 * $i ) ),
)
);
}
// Prime cache for trackbacks.
$page_trackbacks = get_page_of_comment( $trackbacks[1], array( 'per_page' => 3, 'type' => 'trackback' ) );
$page_trackbacks = get_page_of_comment(
$trackbacks[1], array(
'per_page' => 3,
'type' => 'trackback',
)
);
$this->assertEquals( 2, $page_trackbacks );
$num_queries = $wpdb->num_queries;
$page_comments = get_page_of_comment( $comment, array( 'per_page' => 3, 'type' => 'comment' ) );
$num_queries = $wpdb->num_queries;
$page_comments = get_page_of_comment(
$comment, array(
'per_page' => 3,
'type' => 'comment',
)
);
$this->assertEquals( 1, $page_comments );
$this->assertNotEquals( $num_queries, $wpdb->num_queries );
@@ -106,7 +160,12 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
*/
public function test_cache_should_be_invalidated_when_comment_is_approved() {
$p = self::factory()->post->create();
$c = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0 ) );
$c = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_approved' => 0,
)
);
// Prime cache.
$page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
@@ -155,10 +214,26 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
public function test_cache_should_be_invalidated_when_older_comment_is_published() {
$now = time();
$p = self::factory()->post->create();
$c1 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
$c2 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
$c3 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) );
$p = self::factory()->post->create();
$c1 = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ),
)
);
$c2 = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ),
)
);
$c3 = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_approved' => 0,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ),
)
);
$this->assertEquals( 1, get_page_of_comment( $c1, array( 'per_page' => 2 ) ) );
@@ -173,11 +248,21 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
public function test_query_should_be_limited_to_comments_on_the_proper_post() {
$posts = self::factory()->post->create_many( 2 );
$now = time();
$now = time();
$comments_0 = $comments_1 = array();
for ( $i = 0; $i < 5; $i++ ) {
$comments_0[] = self::factory()->comment->create( array( 'comment_post_ID' => $posts[0], 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ) ) );
$comments_1[] = self::factory()->comment->create( array( 'comment_post_ID' => $posts[1], 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ) ) );
$comments_0[] = self::factory()->comment->create(
array(
'comment_post_ID' => $posts[0],
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ),
)
);
$comments_1[] = self::factory()->comment->create(
array(
'comment_post_ID' => $posts[1],
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ),
)
);
}
$found_0 = get_page_of_comment( $comments_0[0], array( 'per_page' => 2 ) );
@@ -193,13 +278,24 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
public function test_only_top_level_comments_should_be_included_in_older_count() {
$post = self::factory()->post->create();
$now = time();
$now = time();
$comment_parents = $comment_children = array();
for ( $i = 0; $i < 5; $i++ ) {
$parent = self::factory()->comment->create( array( 'comment_post_ID' => $post, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ) ) );
$parent = self::factory()->comment->create(
array(
'comment_post_ID' => $post,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ),
)
);
$comment_parents[ $i ] = $parent;
$child = self::factory()->comment->create( array( 'comment_post_ID' => $post, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 59 ) ), 'comment_parent' => $parent ) );
$child = self::factory()->comment->create(
array(
'comment_post_ID' => $post,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 59 ) ),
'comment_parent' => $parent,
)
);
$comment_children[ $i ] = $child;
}
@@ -207,7 +303,7 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
$page_2_indicies = array( 0, 1 );
$args = array(
'per_page' => 3,
'per_page' => 3,
'max_depth' => 2,
);
@@ -228,10 +324,25 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
public function test_comments_per_page_option_should_be_fallback_when_query_var_is_not_available() {
$now = time();
$p = self::factory()->post->create();
$c1 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
$c2 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
$c3 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) );
$p = self::factory()->post->create();
$c1 = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ),
)
);
$c2 = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ),
)
);
$c3 = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ),
)
);
update_option( 'page_comments', 1 );
update_option( 'comments_per_page', 2 );
@@ -246,11 +357,31 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
public function test_should_ignore_comment_order() {
$now = time();
$p = self::factory()->post->create();
$c1 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
$c2 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
$c3 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) );
$c4 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 40 ) ) );
$p = self::factory()->post->create();
$c1 = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ),
)
);
$c2 = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ),
)
);
$c3 = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ),
)
);
$c4 = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 40 ),
)
);
update_option( 'comment_order', 'desc' );
update_option( 'page_comments', 1 );
@@ -266,11 +397,31 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
public function test_should_ignore_default_comment_page() {
$now = time();
$p = self::factory()->post->create();
$c1 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
$c2 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
$c3 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) );
$c4 = self::factory()->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 40 ) ) );
$p = self::factory()->post->create();
$c1 = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ),
)
);
$c2 = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ),
)
);
$c3 = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ),
)
);
$c4 = self::factory()->comment->create(
array(
'comment_post_ID' => $p,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 40 ),
)
);
update_option( 'default_comment_page', 'newest' );
update_option( 'page_comments', 1 );
@@ -10,82 +10,98 @@ class Tests_Comment_Last_Modified extends WP_UnitTestCase {
}
public function test_default_timezone() {
self::factory()->comment->create_and_get( array(
'comment_status' => 1,
'comment_date' => '2000-01-01 11:00:00',
'comment_date_gmt' => '2000-01-01 10:00:00',
) );
self::factory()->comment->create_and_get(
array(
'comment_status' => 1,
'comment_date' => '2000-01-01 11:00:00',
'comment_date_gmt' => '2000-01-01 10:00:00',
)
);
$this->assertSame( strtotime( '2000-01-01 10:00:00' ), strtotime( get_lastcommentmodified() ) );
}
public function test_server_timezone() {
self::factory()->comment->create_and_get( array(
'comment_status' => 1,
'comment_date' => '2000-01-01 11:00:00',
'comment_date_gmt' => '2000-01-01 10:00:00',
) );
self::factory()->comment->create_and_get(
array(
'comment_status' => 1,
'comment_date' => '2000-01-01 11:00:00',
'comment_date_gmt' => '2000-01-01 10:00:00',
)
);
$this->assertSame( strtotime( '2000-01-01 10:00:00' ), strtotime( get_lastcommentmodified() ) );
}
public function test_blog_timezone() {
self::factory()->comment->create_and_get( array(
'comment_status' => 1,
'comment_date' => '2000-01-01 11:00:00',
'comment_date_gmt' => '2000-01-01 10:00:00',
) );
self::factory()->comment->create_and_get(
array(
'comment_status' => 1,
'comment_date' => '2000-01-01 11:00:00',
'comment_date_gmt' => '2000-01-01 10:00:00',
)
);
$this->assertSame( '2000-01-01 11:00:00', get_lastcommentmodified( 'blog' ) );
}
public function test_gmt_timezone() {
self::factory()->comment->create_and_get( array(
'comment_status' => 1,
'comment_date' => '2000-01-01 11:00:00',
'comment_date_gmt' => '2000-01-01 10:00:00',
) );
self::factory()->comment->create_and_get(
array(
'comment_status' => 1,
'comment_date' => '2000-01-01 11:00:00',
'comment_date_gmt' => '2000-01-01 10:00:00',
)
);
$this->assertSame( strtotime( '2000-01-01 10:00:00' ), strtotime( get_lastcommentmodified( 'GMT' ) ) );
}
public function test_unknown_timezone() {
self::factory()->comment->create_and_get( array(
'comment_status' => 1,
'comment_date' => '2000-01-01 11:00:00',
'comment_date_gmt' => '2000-01-01 10:00:00',
) );
self::factory()->comment->create_and_get(
array(
'comment_status' => 1,
'comment_date' => '2000-01-01 11:00:00',
'comment_date_gmt' => '2000-01-01 10:00:00',
)
);
$this->assertFalse( get_lastcommentmodified( 'foo' ) );
}
public function test_data_is_cached() {
self::factory()->comment->create_and_get( array(
'comment_status' => 1,
'comment_date' => '2015-04-01 11:00:00',
'comment_date_gmt' => '2015-04-01 10:00:00',
) );
self::factory()->comment->create_and_get(
array(
'comment_status' => 1,
'comment_date' => '2015-04-01 11:00:00',
'comment_date_gmt' => '2015-04-01 10:00:00',
)
);
get_lastcommentmodified();
$this->assertSame( strtotime( '2015-04-01 10:00:00' ), strtotime( wp_cache_get( 'lastcommentmodified:server', 'timeinfo' ) ) );
}
public function test_cache_is_cleared() {
self::factory()->comment->create_and_get( array(
'comment_status' => 1,
'comment_date' => '2000-01-01 11:00:00',
'comment_date_gmt' => '2000-01-01 10:00:00',
) );
self::factory()->comment->create_and_get(
array(
'comment_status' => 1,
'comment_date' => '2000-01-01 11:00:00',
'comment_date_gmt' => '2000-01-01 10:00:00',
)
);
get_lastcommentmodified();
$this->assertSame( strtotime( '2000-01-01 10:00:00' ), strtotime( wp_cache_get( 'lastcommentmodified:server', 'timeinfo' ) ) );
self::factory()->comment->create_and_get( array(
'comment_status' => 1,
'comment_date' => '2000-01-02 11:00:00',
'comment_date_gmt' => '2000-01-02 10:00:00',
) );
self::factory()->comment->create_and_get(
array(
'comment_status' => 1,
'comment_date' => '2000-01-02 11:00:00',
'comment_date_gmt' => '2000-01-02 10:00:00',
)
);
$this->assertFalse( wp_cache_get( 'lastcommentmodified:server', 'timeinfo' ) );
$this->assertSame( strtotime( '2000-01-02 10:00:00' ), strtotime( get_lastcommentmodified() ) );
@@ -93,17 +109,21 @@ class Tests_Comment_Last_Modified extends WP_UnitTestCase {
}
public function test_cache_is_cleared_when_comment_is_trashed() {
$comment_1 = self::factory()->comment->create_and_get( array(
'comment_status' => 1,
'comment_date' => '1998-01-01 11:00:00',
'comment_date_gmt' => '1998-01-01 10:00:00',
) );
$comment_1 = self::factory()->comment->create_and_get(
array(
'comment_status' => 1,
'comment_date' => '1998-01-01 11:00:00',
'comment_date_gmt' => '1998-01-01 10:00:00',
)
);
$comment_2 = self::factory()->comment->create_and_get( array(
'comment_status' => 1,
'comment_date' => '2000-01-02 11:00:00',
'comment_date_gmt' => '2000-01-02 10:00:00',
) );
$comment_2 = self::factory()->comment->create_and_get(
array(
'comment_status' => 1,
'comment_date' => '2000-01-02 11:00:00',
'comment_date_gmt' => '2000-01-02 10:00:00',
)
);
get_lastcommentmodified();
+49 -35
View File
@@ -1,7 +1,7 @@
<?php
class Tests_Comment_Meta_Cache extends WP_UnitTestCase {
protected $i = 0;
protected $i = 0;
protected $queries = 0;
/**
@@ -10,7 +10,7 @@ class Tests_Comment_Meta_Cache extends WP_UnitTestCase {
public function test_update_comment_meta_cache_should_default_to_true() {
global $wpdb;
$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
foreach ( $comment_ids as $cid ) {
@@ -20,9 +20,11 @@ class Tests_Comment_Meta_Cache extends WP_UnitTestCase {
// Clear comment cache, just in case.
clean_comment_cache( $comment_ids );
$q = new WP_Comment_Query( array(
'post_ID' => $p,
) );
$q = new WP_Comment_Query(
array(
'post_ID' => $p,
)
);
$num_queries = $wpdb->num_queries;
foreach ( $comment_ids as $cid ) {
@@ -38,7 +40,7 @@ class Tests_Comment_Meta_Cache extends WP_UnitTestCase {
public function test_update_comment_meta_cache_true() {
global $wpdb;
$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
foreach ( $comment_ids as $cid ) {
@@ -48,10 +50,12 @@ class Tests_Comment_Meta_Cache extends WP_UnitTestCase {
// Clear comment cache, just in case.
clean_comment_cache( $comment_ids );
$q = new WP_Comment_Query( array(
'post_ID' => $p,
'update_comment_meta_cache' => true,
) );
$q = new WP_Comment_Query(
array(
'post_ID' => $p,
'update_comment_meta_cache' => true,
)
);
$num_queries = $wpdb->num_queries;
foreach ( $comment_ids as $cid ) {
@@ -67,17 +71,19 @@ class Tests_Comment_Meta_Cache extends WP_UnitTestCase {
public function test_update_comment_meta_cache_false() {
global $wpdb;
$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
foreach ( $comment_ids as $cid ) {
update_comment_meta( $cid, 'foo', 'bar' );
}
$q = new WP_Comment_Query( array(
'post_ID' => $p,
'update_comment_meta_cache' => false,
) );
$q = new WP_Comment_Query(
array(
'post_ID' => $p,
'update_comment_meta_cache' => false,
)
);
$num_queries = $wpdb->num_queries;
foreach ( $comment_ids as $cid ) {
@@ -93,7 +99,7 @@ class Tests_Comment_Meta_Cache extends WP_UnitTestCase {
public function test_comment_meta_should_be_lazy_loaded_for_all_comments_in_comments_template() {
global $wpdb;
$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
foreach ( $comment_ids as $cid ) {
@@ -130,13 +136,15 @@ class Tests_Comment_Meta_Cache extends WP_UnitTestCase {
$posts = self::factory()->post->create_many( 2, array( 'post_status' => 'publish' ) );
$now = time();
$now = time();
$comments = array();
for ( $i = 0; $i < 5; $i++ ) {
$comments[] = self::factory()->comment->create( array(
'comment_post_ID' => $posts[0],
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( 60 * $i ) ),
) );
$comments[] = self::factory()->comment->create(
array(
'comment_post_ID' => $posts[0],
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( 60 * $i ) ),
)
);
}
foreach ( $comments as $c ) {
@@ -145,10 +153,12 @@ class Tests_Comment_Meta_Cache extends WP_UnitTestCase {
update_option( 'posts_per_rss', 3 );
$q = new WP_Query( array(
'feed' => true,
'withcomments' => true,
) );
$q = new WP_Query(
array(
'feed' => true,
'withcomments' => true,
)
);
// First comment will cause the cache to be primed.
$num_queries = $wpdb->num_queries;
@@ -174,13 +184,15 @@ class Tests_Comment_Meta_Cache extends WP_UnitTestCase {
$posts = self::factory()->post->create_many( 2, array( 'post_status' => 'publish' ) );
$now = time();
$now = time();
$comments = array();
for ( $i = 0; $i < 5; $i++ ) {
$comments[] = self::factory()->comment->create( array(
'comment_post_ID' => $posts[0],
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( 60 * $i ) ),
) );
$comments[] = self::factory()->comment->create(
array(
'comment_post_ID' => $posts[0],
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( 60 * $i ) ),
)
);
}
foreach ( $comments as $c ) {
@@ -189,11 +201,13 @@ class Tests_Comment_Meta_Cache extends WP_UnitTestCase {
update_option( 'posts_per_rss', 3 );
$q = new WP_Query( array(
'feed' => true,
'withcomments' => true,
'p' => $posts[0],
) );
$q = new WP_Query(
array(
'feed' => true,
'withcomments' => true,
'p' => $posts[0],
)
);
// First comment will cause the cache to be primed.
$num_queries = $wpdb->num_queries;
File diff suppressed because it is too large Load Diff
+70 -62
View File
@@ -25,7 +25,6 @@ class Tests_Comment_Slashes extends WP_UnitTestCase {
/**
* Tests the extended model function that expects slashed data
*
*/
function test_wp_new_comment() {
$post_id = self::factory()->post->create();
@@ -33,31 +32,31 @@ class Tests_Comment_Slashes extends WP_UnitTestCase {
// not testing comment_author_email or comment_author_url
// as slashes are not permitted in that data
$data = array(
'comment_post_ID' => $post_id,
'comment_author' => $this->slash_1,
'comment_author_url' => '',
'comment_post_ID' => $post_id,
'comment_author' => $this->slash_1,
'comment_author_url' => '',
'comment_author_email' => '',
'comment_type' => '',
'comment_content' => $this->slash_7,
'comment_type' => '',
'comment_content' => $this->slash_7,
);
$id = wp_new_comment( $data );
$id = wp_new_comment( $data );
$comment = get_comment($id);
$comment = get_comment( $id );
$this->assertEquals( wp_unslash( $this->slash_1 ), $comment->comment_author );
$this->assertEquals( wp_unslash( $this->slash_7 ), $comment->comment_content );
$data = array(
'comment_post_ID' => $post_id,
'comment_author' => $this->slash_2,
'comment_author_url' => '',
'comment_post_ID' => $post_id,
'comment_author' => $this->slash_2,
'comment_author_url' => '',
'comment_author_email' => '',
'comment_type' => '',
'comment_content' => $this->slash_4,
'comment_type' => '',
'comment_content' => $this->slash_4,
);
$id = wp_new_comment( $data );
$id = wp_new_comment( $data );
$comment = get_comment($id);
$comment = get_comment( $id );
$this->assertEquals( wp_unslash( $this->slash_2 ), $comment->comment_author );
$this->assertEquals( wp_unslash( $this->slash_4 ), $comment->comment_content );
@@ -65,24 +64,25 @@ class Tests_Comment_Slashes extends WP_UnitTestCase {
/**
* Tests the controller function that expects slashed data
*
*/
function test_edit_comment() {
$post_id = self::factory()->post->create();
$comment_id = self::factory()->comment->create(array(
'comment_post_ID' => $post_id
));
$post_id = self::factory()->post->create();
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
)
);
// not testing comment_author_email or comment_author_url
// as slashes are not permitted in that data
$_POST = array();
$_POST['comment_ID'] = $comment_id;
$_POST['comment_status'] = '';
$_POST['newcomment_author'] = $this->slash_1;
$_POST['newcomment_author_url'] = '';
$_POST = array();
$_POST['comment_ID'] = $comment_id;
$_POST['comment_status'] = '';
$_POST['newcomment_author'] = $this->slash_1;
$_POST['newcomment_author_url'] = '';
$_POST['newcomment_author_email'] = '';
$_POST['content'] = $this->slash_7;
$_POST = add_magic_quotes( $_POST );
$_POST['content'] = $this->slash_7;
$_POST = add_magic_quotes( $_POST );
edit_comment();
$comment = get_comment( $comment_id );
@@ -90,14 +90,14 @@ class Tests_Comment_Slashes extends WP_UnitTestCase {
$this->assertEquals( $this->slash_1, $comment->comment_author );
$this->assertEquals( $this->slash_7, $comment->comment_content );
$_POST = array();
$_POST['comment_ID'] = $comment_id;
$_POST['comment_status'] = '';
$_POST['newcomment_author'] = $this->slash_2;
$_POST['newcomment_author_url'] = '';
$_POST = array();
$_POST['comment_ID'] = $comment_id;
$_POST['comment_status'] = '';
$_POST['newcomment_author'] = $this->slash_2;
$_POST['newcomment_author_url'] = '';
$_POST['newcomment_author_email'] = '';
$_POST['content'] = $this->slash_4;
$_POST = add_magic_quotes( $_POST );
$_POST['content'] = $this->slash_4;
$_POST = add_magic_quotes( $_POST );
edit_comment();
$comment = get_comment( $comment_id );
@@ -108,27 +108,30 @@ class Tests_Comment_Slashes extends WP_UnitTestCase {
/**
* Tests the model function that expects slashed data
*
*/
function test_wp_insert_comment() {
$post_id = self::factory()->post->create();
$comment_id = wp_insert_comment(array(
'comment_post_ID' => $post_id,
'comment_author' => $this->slash_1,
'comment_content' => $this->slash_7,
));
$comment = get_comment( $comment_id );
$comment_id = wp_insert_comment(
array(
'comment_post_ID' => $post_id,
'comment_author' => $this->slash_1,
'comment_content' => $this->slash_7,
)
);
$comment = get_comment( $comment_id );
$this->assertEquals( wp_unslash( $this->slash_1 ), $comment->comment_author );
$this->assertEquals( wp_unslash( $this->slash_7 ), $comment->comment_content );
$comment_id = wp_insert_comment(array(
'comment_post_ID' => $post_id,
'comment_author' => $this->slash_2,
'comment_content' => $this->slash_4,
));
$comment = get_comment( $comment_id );
$comment_id = wp_insert_comment(
array(
'comment_post_ID' => $post_id,
'comment_author' => $this->slash_2,
'comment_content' => $this->slash_4,
)
);
$comment = get_comment( $comment_id );
$this->assertEquals( wp_unslash( $this->slash_2 ), $comment->comment_author );
$this->assertEquals( wp_unslash( $this->slash_4 ), $comment->comment_content );
@@ -136,29 +139,34 @@ class Tests_Comment_Slashes extends WP_UnitTestCase {
/**
* Tests the model function that expects slashed data
*
*/
function test_wp_update_comment() {
$post_id = self::factory()->post->create();
$comment_id = self::factory()->comment->create(array(
'comment_post_ID' => $post_id
));
$post_id = self::factory()->post->create();
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
)
);
wp_update_comment(array(
'comment_ID' => $comment_id,
'comment_author' => $this->slash_1,
'comment_content' => $this->slash_7,
));
wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_author' => $this->slash_1,
'comment_content' => $this->slash_7,
)
);
$comment = get_comment( $comment_id );
$this->assertEquals( wp_unslash( $this->slash_1 ), $comment->comment_author );
$this->assertEquals( wp_unslash( $this->slash_7 ), $comment->comment_content );
wp_update_comment(array(
'comment_ID' => $comment_id,
'comment_author' => $this->slash_2,
'comment_content' => $this->slash_4,
));
wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_author' => $this->slash_2,
'comment_content' => $this->slash_4,
)
);
$comment = get_comment( $comment_id );
$this->assertEquals( wp_unslash( $this->slash_2 ), $comment->comment_author );
+4 -4
View File
@@ -18,7 +18,7 @@ class Tests_Comment_Template extends WP_UnitTestCase {
}
function test_get_comments_number_without_arg() {
$post_id = self::factory()->post->create();
$post_id = self::factory()->post->create();
$permalink = get_permalink( $post_id );
$this->go_to( $permalink );
@@ -34,7 +34,7 @@ class Tests_Comment_Template extends WP_UnitTestCase {
* @ticket 13651
*/
function test_get_comments_number_text_declension_with_default_args() {
$post_id = $this->factory->post->create();
$post_id = $this->factory->post->create();
$permalink = get_permalink( $post_id );
$this->go_to( $permalink );
@@ -57,7 +57,7 @@ class Tests_Comment_Template extends WP_UnitTestCase {
* @dataProvider data_get_comments_number_text_declension
*/
function test_get_comments_number_text_declension_with_custom_args( $number, $input, $output ) {
$post_id = $this->factory->post->create();
$post_id = $this->factory->post->create();
$permalink = get_permalink( $post_id );
$this->factory->comment->create_post_comments( $post_id, $number );
@@ -109,7 +109,7 @@ class Tests_Comment_Template extends WP_UnitTestCase {
array(
2,
'2 Comments<span class="screen-reader-text"> on Hello % world!</span>',
'2 Comments<span class="screen-reader-text"> on Hello 2 world!</span>' // See #WP37103
'2 Comments<span class="screen-reader-text"> on Hello 2 world!</span>', // See #WP37103
),
array(
2,
+24 -9
View File
@@ -16,15 +16,32 @@ class Tests_Comment_Walker extends WP_UnitTestCase {
*/
function test_has_children() {
$comment_parent = self::factory()->comment->create( array( 'comment_post_ID' => $this->post_id ) );
$comment_child = self::factory()->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_parent' => $comment_parent ) );
$comment_child = self::factory()->comment->create(
array(
'comment_post_ID' => $this->post_id,
'comment_parent' => $comment_parent,
)
);
$comment_parent = get_comment( $comment_parent );
$comment_child = get_comment( $comment_child );
$comment_child = get_comment( $comment_child );
$comment_walker = new Walker_Comment();
$comment_callback = new Comment_Callback_Test( $this, $comment_walker );
wp_list_comments( array( 'callback' => array( $comment_callback, 'comment' ), 'walker' => $comment_walker, 'echo' => false ), array( $comment_parent, $comment_child ) );
wp_list_comments( array( 'callback' => array( $comment_callback, 'comment' ), 'walker' => $comment_walker, 'echo' => false ), array( $comment_child, $comment_parent ) );
wp_list_comments(
array(
'callback' => array( $comment_callback, 'comment' ),
'walker' => $comment_walker,
'echo' => false,
), array( $comment_parent, $comment_child )
);
wp_list_comments(
array(
'callback' => array( $comment_callback, 'comment' ),
'walker' => $comment_walker,
'echo' => false,
), array( $comment_child, $comment_parent )
);
}
}
@@ -37,12 +54,10 @@ class Comment_Callback_Test {
public function comment( $comment, $args, $depth ) {
if ( 1 == $depth ) {
$this->test_walker->assertTrue( $this->walker->has_children );
$this->test_walker->assertTrue( $args['has_children'] ); // Back compat
}
else if ( 2 == $depth ) {
$this->test_walker->assertTrue( $args['has_children'] ); // Back compat
} elseif ( 2 == $depth ) {
$this->test_walker->assertFalse( $this->walker->has_children );
$this->test_walker->assertFalse( $args['has_children'] ); // Back compat
$this->test_walker->assertFalse( $args['has_children'] ); // Back compat
}
}
}
+31 -29
View File
@@ -10,15 +10,17 @@ class Tests_Comment_WpAllowComment extends WP_UnitTestCase {
function setUp() {
parent::setUp();
self::$post_id = self::factory()->post->create();
self::$comment_id = self::factory()->comment->create( array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_author' => 'Bob',
'comment_author_email' => 'bobthebuilder@example.com',
'comment_author_url' => 'http://example.com',
'comment_content' => 'Yes, we can!',
) );
self::$post_id = self::factory()->post->create();
self::$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_approved' => '1',
'comment_author' => 'Bob',
'comment_author_email' => 'bobthebuilder@example.com',
'comment_author_url' => 'http://example.com',
'comment_content' => 'Yes, we can!',
)
);
update_option( 'comment_whitelist', 0 );
}
@@ -31,18 +33,18 @@ class Tests_Comment_WpAllowComment extends WP_UnitTestCase {
}
public function test_allow_comment_if_comment_author_emails_differ() {
$now = time();
$now = time();
$comment_data = array(
'comment_post_ID' => self::$post_id,
'comment_author' => 'Bob',
'comment_post_ID' => self::$post_id,
'comment_author' => 'Bob',
'comment_author_email' => 'sideshowbob@example.com',
'comment_author_url' => 'http://example.com',
'comment_content' => 'Yes, we can!',
'comment_author_IP' => '192.168.0.1',
'comment_parent' => 0,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ),
'comment_agent' => 'Bobbot/2.1',
'comment_type' => '',
'comment_author_url' => 'http://example.com',
'comment_content' => 'Yes, we can!',
'comment_author_IP' => '192.168.0.1',
'comment_parent' => 0,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ),
'comment_agent' => 'Bobbot/2.1',
'comment_type' => '',
);
$result = wp_allow_comment( $comment_data );
@@ -54,18 +56,18 @@ class Tests_Comment_WpAllowComment extends WP_UnitTestCase {
* @expectedException WPDieException
*/
public function test_die_as_duplicate_if_comment_author_name_and_emails_match() {
$now = time();
$now = time();
$comment_data = array(
'comment_post_ID' => self::$post_id,
'comment_author' => 'Bob',
'comment_post_ID' => self::$post_id,
'comment_author' => 'Bob',
'comment_author_email' => 'bobthebuilder@example.com',
'comment_author_url' => 'http://example.com',
'comment_content' => 'Yes, we can!',
'comment_author_IP' => '192.168.0.1',
'comment_parent' => 0,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ),
'comment_agent' => 'Bobbot/2.1',
'comment_type' => '',
'comment_author_url' => 'http://example.com',
'comment_content' => 'Yes, we can!',
'comment_author_IP' => '192.168.0.1',
'comment_parent' => 0,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ),
'comment_agent' => 'Bobbot/2.1',
'comment_type' => '',
);
$result = wp_allow_comment( $comment_data );
@@ -13,7 +13,7 @@ class Tests_WP_Blacklist_Check extends WP_UnitTestCase {
$author_ip = '192.168.0.1';
$user_agent = '';
update_option( 'blacklist_keys',"well\nfoo" );
update_option( 'blacklist_keys', "well\nfoo" );
$result = wp_blacklist_check( $author, $author_email, $author_url, $comment, $author_ip, $user_agent );
@@ -31,7 +31,7 @@ class Tests_WP_Blacklist_Check extends WP_UnitTestCase {
$author_ip = '192.168.0.1';
$user_agent = '';
update_option( 'blacklist_keys',"halfway\nfoo" );
update_option( 'blacklist_keys', "halfway\nfoo" );
$result = wp_blacklist_check( $author, $author_email, $author_url, $comment, $author_ip, $user_agent );
@@ -46,7 +46,7 @@ class Tests_WP_Blacklist_Check extends WP_UnitTestCase {
$author_ip = '192.168.0.1';
$user_agent = '';
update_option( 'blacklist_keys',"sideshow\nfoo" );
update_option( 'blacklist_keys', "sideshow\nfoo" );
$result = wp_blacklist_check( $author, $author_email, $author_url, $comment, $author_ip, $user_agent );
@@ -61,7 +61,7 @@ class Tests_WP_Blacklist_Check extends WP_UnitTestCase {
$author_ip = '192.168.0.1';
$user_agent = '';
update_option( 'blacklist_keys',"example\nfoo" );
update_option( 'blacklist_keys', "example\nfoo" );
$result = wp_blacklist_check( $author, $author_email, $author_url, $comment, $author_ip, $user_agent );
@@ -79,7 +79,7 @@ class Tests_WP_Blacklist_Check extends WP_UnitTestCase {
$author_ip = '192.168.0.1';
$user_agent = '';
update_option( 'blacklist_keys',"/spam/" );
update_option( 'blacklist_keys', '/spam/' );
$result = wp_blacklist_check( $author, $author_email, $author_url, $comment, $author_ip, $user_agent );
@@ -94,7 +94,7 @@ class Tests_WP_Blacklist_Check extends WP_UnitTestCase {
$author_ip = '192.168.0.1';
$user_agent = '';
update_option( 'blacklist_keys',"sideshow\nfoobar" );
update_option( 'blacklist_keys', "sideshow\nfoobar" );
$result = wp_blacklist_check( $author, $author_email, $author_url, $comment, $author_ip, $user_agent );
+5 -3
View File
@@ -12,9 +12,11 @@ class Tests_Term_WpComment extends WP_UnitTestCase {
// Ensure that there is a comment with ID 1.
$comment_1 = WP_Comment::get_instance( 1 );
if ( ! $comment_1 ) {
$wpdb->insert( $wpdb->comments, array(
'comment_ID' => 1,
) );
$wpdb->insert(
$wpdb->comments, array(
'comment_ID' => 1,
)
);
clean_comment_cache( 1 );
}
+36 -22
View File
@@ -15,9 +15,11 @@ class Tests_WP_Count_Comments extends WP_UnitTestCase {
}
public function test_wp_count_comments_approved() {
self::factory()->comment->create( array(
'comment_approved' => 1
) );
self::factory()->comment->create(
array(
'comment_approved' => 1,
)
);
$count = wp_count_comments();
@@ -31,9 +33,11 @@ class Tests_WP_Count_Comments extends WP_UnitTestCase {
}
public function test_wp_count_comments_awaiting() {
self::factory()->comment->create( array(
'comment_approved' => 0
) );
self::factory()->comment->create(
array(
'comment_approved' => 0,
)
);
$count = wp_count_comments();
@@ -47,9 +51,11 @@ class Tests_WP_Count_Comments extends WP_UnitTestCase {
}
public function test_wp_count_comments_spam() {
self::factory()->comment->create( array(
'comment_approved' => 'spam'
) );
self::factory()->comment->create(
array(
'comment_approved' => 'spam',
)
);
$count = wp_count_comments();
@@ -63,9 +69,11 @@ class Tests_WP_Count_Comments extends WP_UnitTestCase {
}
public function test_wp_count_comments_trash() {
self::factory()->comment->create( array(
'comment_approved' => 'trash'
) );
self::factory()->comment->create(
array(
'comment_approved' => 'trash',
)
);
$count = wp_count_comments();
@@ -79,9 +87,11 @@ class Tests_WP_Count_Comments extends WP_UnitTestCase {
}
public function test_wp_count_comments_post_trashed() {
self::factory()->comment->create( array(
'comment_approved' => 'post-trashed'
) );
self::factory()->comment->create(
array(
'comment_approved' => 'post-trashed',
)
);
$count = wp_count_comments();
@@ -95,13 +105,17 @@ class Tests_WP_Count_Comments extends WP_UnitTestCase {
}
public function test_wp_count_comments_cache() {
$post_id = self::factory()->post->create( array(
'post_status' => 'publish'
) );
$comment_id = self::factory()->comment->create( array(
'comment_approved' => '1',
'comment_post_ID' => $post_id
) );
$post_id = self::factory()->post->create(
array(
'post_status' => 'publish',
)
);
$comment_id = self::factory()->comment->create(
array(
'comment_approved' => '1',
'comment_post_ID' => $post_id,
)
);
$count1 = wp_count_comments( $post_id );

Some files were not shown because too many files have changed in this diff Show More