2013-08-07 08:38:38 +02:00
|
|
|
<?php
|
2019-08-15 19:16:21 +02:00
|
|
|
require_once __DIR__ . '/class-basic-object.php';
|
2013-08-07 08:38:38 +02:00
|
|
|
|
2019-01-28 15:10:24 +01:00
|
|
|
/**
|
|
|
|
* Retrieves PHPUnit runner version.
|
2019-04-11 19:29:20 +02:00
|
|
|
*
|
|
|
|
* @return double The version number.
|
2019-01-28 15:10:24 +01:00
|
|
|
*/
|
|
|
|
function tests_get_phpunit_version() {
|
|
|
|
if ( class_exists( 'PHPUnit_Runner_Version' ) ) {
|
|
|
|
$version = PHPUnit_Runner_Version::id();
|
|
|
|
} elseif ( class_exists( 'PHPUnit\Runner\Version' ) ) {
|
|
|
|
// Must be parsable by PHP 5.2.x.
|
|
|
|
$version = call_user_func( 'PHPUnit\Runner\Version::id' );
|
|
|
|
} else {
|
|
|
|
$version = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $version;
|
|
|
|
}
|
|
|
|
|
2016-02-25 22:59:56 +01:00
|
|
|
/**
|
|
|
|
* Resets various `$_SERVER` variables that can get altered during tests.
|
|
|
|
*/
|
2019-07-01 10:00:12 +02:00
|
|
|
function tests_reset__SERVER() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
|
2016-02-25 22:59:56 +01:00
|
|
|
$_SERVER['HTTP_HOST'] = WP_TESTS_DOMAIN;
|
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
2016-02-26 03:08:47 +01:00
|
|
|
$_SERVER['REQUEST_URI'] = '';
|
2016-02-25 22:59:56 +01:00
|
|
|
$_SERVER['SERVER_NAME'] = WP_TESTS_DOMAIN;
|
|
|
|
$_SERVER['SERVER_PORT'] = '80';
|
|
|
|
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
|
|
|
|
|
|
|
|
unset( $_SERVER['HTTP_REFERER'] );
|
|
|
|
unset( $_SERVER['HTTPS'] );
|
|
|
|
}
|
|
|
|
|
2018-08-03 00:35:10 +02:00
|
|
|
/**
|
|
|
|
* Adds hooks before loading WP.
|
|
|
|
*
|
2019-11-29 22:40:58 +01:00
|
|
|
* @see add_filter()
|
|
|
|
*
|
|
|
|
* @param string $tag The name of the filter to hook the $function_to_add callback to.
|
|
|
|
* @param callable $function_to_add The callback to be run when the filter is applied.
|
|
|
|
* @param int $priority Optional. Used to specify the order in which the functions
|
|
|
|
* associated with a particular action are executed.
|
|
|
|
* Lower numbers correspond with earlier execution,
|
|
|
|
* and functions with the same priority are executed
|
|
|
|
* in the order in which they were added to the action. Default 10.
|
|
|
|
* @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
|
|
|
|
* @return true
|
2018-08-03 00:35:10 +02:00
|
|
|
*/
|
2017-12-01 00:09:33 +01:00
|
|
|
function tests_add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
git-svn-id: https://develop.svn.wordpress.org/trunk@38571 602fd350-edb4-49c9-b593-d223f7449a82
2016-09-08 05:54:13 +02:00
|
|
|
global $wp_filter;
|
2013-08-07 08:38:38 +02:00
|
|
|
|
2016-09-09 02:33:52 +02:00
|
|
|
if ( function_exists( 'add_filter' ) ) {
|
|
|
|
add_filter( $tag, $function_to_add, $priority, $accepted_args );
|
|
|
|
} else {
|
2019-11-29 22:40:58 +01:00
|
|
|
$idx = _test_filter_build_unique_id( $tag, $function_to_add, $priority );
|
|
|
|
|
2017-12-01 00:09:33 +01:00
|
|
|
$wp_filter[ $tag ][ $priority ][ $idx ] = array(
|
|
|
|
'function' => $function_to_add,
|
|
|
|
'accepted_args' => $accepted_args,
|
|
|
|
);
|
2016-09-09 02:33:52 +02:00
|
|
|
}
|
2013-08-07 08:38:38 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-03 00:35:10 +02:00
|
|
|
/**
|
|
|
|
* Generates a unique function ID based on the given arguments.
|
|
|
|
*
|
2019-11-29 22:40:58 +01:00
|
|
|
* @see _wp_filter_build_unique_id()
|
|
|
|
*
|
|
|
|
* @param string $tag Unused. The name of the filter to build ID for.
|
|
|
|
* @param callable $function The function to generate ID for.
|
|
|
|
* @param int $priority Unused. The order in which the functions
|
|
|
|
* associated with a particular action are executed.
|
|
|
|
* @return string Unique function ID for usage as array key.
|
2018-08-03 00:35:10 +02:00
|
|
|
*/
|
2017-12-01 00:09:33 +01:00
|
|
|
function _test_filter_build_unique_id( $tag, $function, $priority ) {
|
|
|
|
if ( is_string( $function ) ) {
|
2013-08-07 08:38:38 +02:00
|
|
|
return $function;
|
2017-12-01 00:09:33 +01:00
|
|
|
}
|
2013-08-07 08:38:38 +02:00
|
|
|
|
2017-12-01 00:09:33 +01:00
|
|
|
if ( is_object( $function ) ) {
|
2018-08-03 00:35:10 +02:00
|
|
|
// Closures are currently implemented as objects.
|
2013-08-07 08:38:38 +02:00
|
|
|
$function = array( $function, '' );
|
|
|
|
} else {
|
|
|
|
$function = (array) $function;
|
|
|
|
}
|
|
|
|
|
2017-12-01 00:09:33 +01:00
|
|
|
if ( is_object( $function[0] ) ) {
|
2019-11-29 22:40:58 +01:00
|
|
|
// Object class calling.
|
2017-12-01 00:09:33 +01:00
|
|
|
return spl_object_hash( $function[0] ) . $function[1];
|
|
|
|
} elseif ( is_string( $function[0] ) ) {
|
2019-11-29 22:40:58 +01:00
|
|
|
// Static calling.
|
2019-11-29 23:15:31 +01:00
|
|
|
return $function[0] . '::' . $function[1];
|
2013-08-07 08:38:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 00:35:10 +02:00
|
|
|
/**
|
|
|
|
* Deletes all data from the database.
|
|
|
|
*/
|
2016-08-27 10:35:16 +02:00
|
|
|
function _delete_all_data() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
foreach ( array(
|
|
|
|
$wpdb->posts,
|
|
|
|
$wpdb->postmeta,
|
|
|
|
$wpdb->comments,
|
|
|
|
$wpdb->commentmeta,
|
|
|
|
$wpdb->term_relationships,
|
2017-12-01 00:09:33 +01:00
|
|
|
$wpdb->termmeta,
|
2016-08-27 10:35:16 +02:00
|
|
|
) as $table ) {
|
2019-07-08 02:55:20 +02:00
|
|
|
//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
|
2016-08-27 10:35:16 +02:00
|
|
|
$wpdb->query( "DELETE FROM {$table}" );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( array(
|
|
|
|
$wpdb->terms,
|
2017-12-01 00:09:33 +01:00
|
|
|
$wpdb->term_taxonomy,
|
2016-08-27 10:35:16 +02:00
|
|
|
) as $table ) {
|
2019-07-08 02:55:20 +02:00
|
|
|
//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
|
2016-08-27 10:35:16 +02:00
|
|
|
$wpdb->query( "DELETE FROM {$table} WHERE term_id != 1" );
|
|
|
|
}
|
|
|
|
|
|
|
|
$wpdb->query( "UPDATE {$wpdb->term_taxonomy} SET count = 0" );
|
|
|
|
|
|
|
|
$wpdb->query( "DELETE FROM {$wpdb->users} WHERE ID != 1" );
|
|
|
|
$wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE user_id != 1" );
|
|
|
|
}
|
|
|
|
|
2018-08-03 00:35:10 +02:00
|
|
|
/**
|
|
|
|
* Deletes all posts from the database.
|
|
|
|
*/
|
2013-08-07 08:38:38 +02:00
|
|
|
function _delete_all_posts() {
|
|
|
|
global $wpdb;
|
|
|
|
|
2016-08-27 10:35:16 +02:00
|
|
|
$all_posts = $wpdb->get_results( "SELECT ID, post_type from {$wpdb->posts}", ARRAY_A );
|
|
|
|
if ( ! $all_posts ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $all_posts as $data ) {
|
|
|
|
if ( 'attachment' === $data['post_type'] ) {
|
|
|
|
wp_delete_attachment( $data['ID'], true );
|
|
|
|
} else {
|
|
|
|
wp_delete_post( $data['ID'], true );
|
|
|
|
}
|
2013-08-07 08:38:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 19:29:20 +02:00
|
|
|
/**
|
|
|
|
* Handles the WP die handler by outputting the given values as text.
|
|
|
|
*
|
|
|
|
* @param string $message The message.
|
|
|
|
* @param string $title The title.
|
|
|
|
* @param array $args Array with arguments.
|
|
|
|
*/
|
2014-06-21 21:59:28 +02:00
|
|
|
function _wp_die_handler( $message, $title = '', $args = array() ) {
|
2017-12-01 00:09:33 +01:00
|
|
|
if ( ! $GLOBALS['_wp_die_disabled'] ) {
|
|
|
|
_wp_die_handler_txt( $message, $title, $args );
|
2014-06-21 21:59:28 +02:00
|
|
|
} else {
|
2020-01-29 01:43:23 +01:00
|
|
|
// Ignore at our peril.
|
2014-06-21 21:59:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 19:29:20 +02:00
|
|
|
/**
|
|
|
|
* Disables the WP die handler.
|
|
|
|
*/
|
2014-06-21 21:59:28 +02:00
|
|
|
function _disable_wp_die() {
|
|
|
|
$GLOBALS['_wp_die_disabled'] = true;
|
|
|
|
}
|
|
|
|
|
2019-04-11 19:29:20 +02:00
|
|
|
/**
|
|
|
|
* Enables the WP die handler.
|
|
|
|
*/
|
2014-06-21 21:59:28 +02:00
|
|
|
function _enable_wp_die() {
|
|
|
|
$GLOBALS['_wp_die_disabled'] = false;
|
|
|
|
}
|
|
|
|
|
2019-04-11 19:29:20 +02:00
|
|
|
/**
|
|
|
|
* Returns the die handler.
|
|
|
|
*
|
|
|
|
* @return string The die handler.
|
|
|
|
*/
|
2014-06-21 21:59:28 +02:00
|
|
|
function _wp_die_handler_filter() {
|
|
|
|
return '_wp_die_handler';
|
|
|
|
}
|
|
|
|
|
2019-04-11 19:29:20 +02:00
|
|
|
/**
|
|
|
|
* Returns the die handler.
|
|
|
|
*
|
|
|
|
* @return string The die handler.
|
|
|
|
*/
|
2017-10-21 16:13:37 +02:00
|
|
|
function _wp_die_handler_filter_exit() {
|
|
|
|
return '_wp_die_handler_exit';
|
|
|
|
}
|
|
|
|
|
2019-04-11 19:29:20 +02:00
|
|
|
/**
|
|
|
|
* Dies without an exit.
|
|
|
|
*
|
|
|
|
* @param string $message The message.
|
|
|
|
* @param string $title The title.
|
|
|
|
* @param array $args Array with arguments.
|
|
|
|
*/
|
2014-06-21 21:59:28 +02:00
|
|
|
function _wp_die_handler_txt( $message, $title, $args ) {
|
|
|
|
echo "\nwp_die called\n";
|
|
|
|
echo "Message : $message\n";
|
|
|
|
echo "Title : $title\n";
|
|
|
|
if ( ! empty( $args ) ) {
|
|
|
|
echo "Args: \n";
|
2017-12-01 00:09:33 +01:00
|
|
|
foreach ( $args as $k => $v ) {
|
2014-06-21 21:59:28 +02:00
|
|
|
echo "\t $k : $v\n";
|
|
|
|
}
|
|
|
|
}
|
2015-04-17 01:59:01 +02:00
|
|
|
}
|
|
|
|
|
2019-04-11 19:29:20 +02:00
|
|
|
/**
|
|
|
|
* Dies with an exit.
|
|
|
|
*
|
|
|
|
* @param string $message The message.
|
|
|
|
* @param string $title The title.
|
|
|
|
* @param array $args Array with arguments.
|
|
|
|
*/
|
2017-10-21 16:13:37 +02:00
|
|
|
function _wp_die_handler_exit( $message, $title, $args ) {
|
|
|
|
echo "\nwp_die called\n";
|
|
|
|
echo "Message : $message\n";
|
|
|
|
echo "Title : $title\n";
|
|
|
|
if ( ! empty( $args ) ) {
|
|
|
|
echo "Args: \n";
|
2017-12-01 00:09:33 +01:00
|
|
|
foreach ( $args as $k => $v ) {
|
2017-10-21 16:13:37 +02:00
|
|
|
echo "\t $k : $v\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
|
2015-04-17 01:59:01 +02:00
|
|
|
/**
|
|
|
|
* Set a permalink structure.
|
|
|
|
*
|
|
|
|
* Hooked as a callback to the 'populate_options' action, we use this function to set a permalink structure during
|
|
|
|
* `wp_install()`, so that WP doesn't attempt to do a time-consuming remote request.
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*/
|
|
|
|
function _set_default_permalink_structure_for_tests() {
|
|
|
|
update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
|
|
|
|
}
|
2016-02-17 23:51:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper used with the `upload_dir` filter to remove the /year/month sub directories from the uploads path and URL.
|
2019-04-11 19:29:20 +02:00
|
|
|
*
|
|
|
|
* @return array The altered array.
|
2016-02-17 23:51:01 +01:00
|
|
|
*/
|
|
|
|
function _upload_dir_no_subdir( $uploads ) {
|
|
|
|
$subdir = $uploads['subdir'];
|
|
|
|
|
|
|
|
$uploads['subdir'] = '';
|
2017-12-01 00:09:33 +01:00
|
|
|
$uploads['path'] = str_replace( $subdir, '', $uploads['path'] );
|
|
|
|
$uploads['url'] = str_replace( $subdir, '', $uploads['url'] );
|
2016-02-17 23:51:01 +01:00
|
|
|
|
|
|
|
return $uploads;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper used with the `upload_dir` filter to set https upload URL.
|
2019-04-11 19:29:20 +02:00
|
|
|
*
|
|
|
|
* @return array The altered array.
|
2016-08-19 17:44:37 +02:00
|
|
|
*/
|
2016-02-17 23:51:01 +01:00
|
|
|
function _upload_dir_https( $uploads ) {
|
2017-12-01 00:09:33 +01:00
|
|
|
$uploads['url'] = str_replace( 'http://', 'https://', $uploads['url'] );
|
2016-02-17 23:51:01 +01:00
|
|
|
$uploads['baseurl'] = str_replace( 'http://', 'https://', $uploads['baseurl'] );
|
|
|
|
|
|
|
|
return $uploads;
|
|
|
|
}
|
2016-11-14 08:12:31 +01:00
|
|
|
|
2018-02-21 17:24:30 +01:00
|
|
|
/**
|
|
|
|
* Use the Spy_REST_Server class for the REST server.
|
2019-04-11 19:29:20 +02:00
|
|
|
*
|
|
|
|
* @return string The server class name.
|
2018-02-21 17:24:30 +01:00
|
|
|
*/
|
|
|
|
function _wp_rest_server_class_filter() {
|
|
|
|
return 'Spy_REST_Server';
|
|
|
|
}
|
|
|
|
|
2016-11-14 08:12:31 +01:00
|
|
|
// Skip `setcookie` calls in auth_cookie functions due to warning:
|
2020-01-29 01:43:23 +01:00
|
|
|
// Cannot modify header information - headers already sent by...
|
2017-03-10 15:32:31 +01:00
|
|
|
tests_add_filter( 'send_auth_cookies', '__return_false' );
|
2018-12-13 19:11:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* After the init action has been run once, trying to re-register block types can cause
|
|
|
|
* _doing_it_wrong warnings. To avoid this, unhook the block registration functions.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*/
|
|
|
|
function _unhook_block_registration() {
|
|
|
|
remove_action( 'init', 'register_block_core_archives' );
|
2019-09-19 17:17:39 +02:00
|
|
|
remove_action( 'init', 'register_block_core_block' );
|
2019-03-07 12:53:15 +01:00
|
|
|
remove_action( 'init', 'register_block_core_calendar' );
|
2018-12-13 19:11:10 +01:00
|
|
|
remove_action( 'init', 'register_block_core_categories' );
|
2019-09-19 17:17:39 +02:00
|
|
|
remove_action( 'init', 'register_block_core_latest_comments' );
|
2018-12-13 19:11:10 +01:00
|
|
|
remove_action( 'init', 'register_block_core_latest_posts' );
|
2019-03-07 12:53:15 +01:00
|
|
|
remove_action( 'init', 'register_block_core_rss' );
|
|
|
|
remove_action( 'init', 'register_block_core_search' );
|
2018-12-13 19:11:10 +01:00
|
|
|
remove_action( 'init', 'register_block_core_shortcode' );
|
2019-09-19 17:46:02 +02:00
|
|
|
remove_action( 'init', 'register_block_core_social_link' );
|
2019-03-07 12:53:15 +01:00
|
|
|
remove_action( 'init', 'register_block_core_tag_cloud' );
|
2018-12-13 19:11:10 +01:00
|
|
|
}
|
|
|
|
tests_add_filter( 'init', '_unhook_block_registration', 1000 );
|