2013-08-07 08:38:38 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Ajax test case class
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage UnitTests
|
|
|
|
* @since 3.4.0
|
|
|
|
*/
|
|
|
|
abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
|
|
|
|
|
|
|
|
/**
|
2020-01-05 16:31:10 +01:00
|
|
|
* Last Ajax response. This is set via echo -or- wp_die.
|
2017-12-01 00:09:33 +01:00
|
|
|
*
|
2016-07-27 19:42:01 +02:00
|
|
|
* @var string
|
2013-08-07 08:38:38 +02:00
|
|
|
*/
|
|
|
|
protected $_last_response = '';
|
|
|
|
|
|
|
|
/**
|
2020-01-05 16:31:10 +01:00
|
|
|
* List of Ajax actions called via GET.
|
2017-12-01 00:09:33 +01:00
|
|
|
*
|
2016-07-27 19:42:01 +02:00
|
|
|
* @var array
|
2013-08-07 08:38:38 +02:00
|
|
|
*/
|
2015-10-21 05:17:36 +02:00
|
|
|
protected static $_core_actions_get = array(
|
2017-12-01 00:09:33 +01:00
|
|
|
'fetch-list',
|
|
|
|
'ajax-tag-search',
|
|
|
|
'wp-compression-test',
|
|
|
|
'imgedit-preview',
|
|
|
|
'oembed-cache',
|
|
|
|
'autocomplete-user',
|
|
|
|
'dashboard-widgets',
|
|
|
|
'logged-in',
|
2015-10-21 05:17:36 +02:00
|
|
|
);
|
2013-08-07 08:38:38 +02:00
|
|
|
|
|
|
|
/**
|
2020-01-05 16:31:10 +01:00
|
|
|
* Saved error reporting level.
|
2017-12-01 00:09:33 +01:00
|
|
|
*
|
2013-08-07 08:38:38 +02:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $_error_level = 0;
|
|
|
|
|
|
|
|
/**
|
2020-01-05 16:31:10 +01:00
|
|
|
* List of Ajax actions called via POST.
|
2017-12-01 00:09:33 +01:00
|
|
|
*
|
2016-07-27 19:42:01 +02:00
|
|
|
* @var array
|
2013-08-07 08:38:38 +02:00
|
|
|
*/
|
2015-10-21 05:17:36 +02:00
|
|
|
protected static $_core_actions_post = array(
|
2017-12-01 00:09:33 +01:00
|
|
|
'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',
|
2019-03-15 19:07:09 +01:00
|
|
|
'wp-privacy-export-personal-data',
|
|
|
|
'wp-privacy-erase-personal-data',
|
2013-08-07 08:38:38 +02:00
|
|
|
);
|
|
|
|
|
2015-10-21 05:17:36 +02:00
|
|
|
public static function setUpBeforeClass() {
|
|
|
|
remove_action( 'admin_init', '_maybe_update_core' );
|
|
|
|
remove_action( 'admin_init', '_maybe_update_plugins' );
|
|
|
|
remove_action( 'admin_init', '_maybe_update_themes' );
|
|
|
|
|
2020-01-29 01:43:23 +01:00
|
|
|
// Register the core actions.
|
2015-10-21 05:17:36 +02:00
|
|
|
foreach ( array_merge( self::$_core_actions_get, self::$_core_actions_post ) as $action ) {
|
|
|
|
if ( function_exists( 'wp_ajax_' . str_replace( '-', '_', $action ) ) ) {
|
|
|
|
add_action( 'wp_ajax_' . $action, 'wp_ajax_' . str_replace( '-', '_', $action ), 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::setUpBeforeClass();
|
|
|
|
}
|
|
|
|
|
2013-08-07 08:38:38 +02:00
|
|
|
/**
|
2020-01-05 16:31:10 +01:00
|
|
|
* Sets up the test fixture.
|
|
|
|
*
|
|
|
|
* Overrides wp_die(), pretends to be Ajax, and suppresses E_WARNINGs.
|
2013-08-07 08:38:38 +02:00
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-10-23 07:10:25 +02:00
|
|
|
add_filter( 'wp_doing_ajax', '__return_true' );
|
2013-08-07 08:38:38 +02:00
|
|
|
add_filter( 'wp_die_ajax_handler', array( $this, 'getDieHandler' ), 1, 1 );
|
2015-10-21 05:17:36 +02:00
|
|
|
|
2013-08-07 08:38:38 +02:00
|
|
|
set_current_screen( 'ajax' );
|
|
|
|
|
2020-01-29 01:43:23 +01:00
|
|
|
// Clear logout cookies.
|
2013-08-07 08:38:38 +02:00
|
|
|
add_action( 'clear_auth_cookie', array( $this, 'logout' ) );
|
|
|
|
|
2020-01-29 01:43:23 +01:00
|
|
|
// Suppress warnings from "Cannot modify header information - headers already sent by".
|
2013-08-07 08:38:38 +02:00
|
|
|
$this->_error_level = error_reporting();
|
|
|
|
error_reporting( $this->_error_level & ~E_WARNING );
|
|
|
|
|
2020-01-29 01:43:23 +01:00
|
|
|
// Make some posts.
|
2015-10-17 20:02:16 +02:00
|
|
|
self::factory()->post->create_many( 5 );
|
2013-08-07 08:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-05 16:31:10 +01:00
|
|
|
* Tears down the test fixture.
|
|
|
|
*
|
|
|
|
* Resets $_POST, removes the wp_die() override, restores error reporting.
|
2013-08-07 08:38:38 +02:00
|
|
|
*/
|
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
|
|
|
$_POST = array();
|
2017-12-01 00:09:33 +01:00
|
|
|
$_GET = array();
|
2013-08-07 08:38:38 +02:00
|
|
|
unset( $GLOBALS['post'] );
|
|
|
|
unset( $GLOBALS['comment'] );
|
|
|
|
remove_filter( 'wp_die_ajax_handler', array( $this, 'getDieHandler' ), 1, 1 );
|
|
|
|
remove_action( 'clear_auth_cookie', array( $this, 'logout' ) );
|
|
|
|
error_reporting( $this->_error_level );
|
|
|
|
set_current_screen( 'front' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-05 16:31:10 +01:00
|
|
|
* Clears login cookies, unsets the current user.
|
2013-08-07 08:38:38 +02:00
|
|
|
*/
|
|
|
|
public function logout() {
|
|
|
|
unset( $GLOBALS['current_user'] );
|
2017-12-01 00:09:33 +01:00
|
|
|
$cookies = array( AUTH_COOKIE, SECURE_AUTH_COOKIE, LOGGED_IN_COOKIE, USER_COOKIE, PASS_COOKIE );
|
|
|
|
foreach ( $cookies as $c ) {
|
|
|
|
unset( $_COOKIE[ $c ] );
|
|
|
|
}
|
2013-08-07 08:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-05 16:31:10 +01:00
|
|
|
* Returns our callback handler
|
2017-12-01 00:09:33 +01:00
|
|
|
*
|
2013-08-07 08:38:38 +02:00
|
|
|
* @return callback
|
|
|
|
*/
|
|
|
|
public function getDieHandler() {
|
|
|
|
return array( $this, 'dieHandler' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-05 16:31:10 +01:00
|
|
|
* Handler for wp_die().
|
|
|
|
*
|
2013-08-07 08:38:38 +02:00
|
|
|
* Save the output for analysis, stop execution by throwing an exception.
|
2020-01-05 16:31:10 +01:00
|
|
|
*
|
2020-09-18 15:53:31 +02:00
|
|
|
* Error conditions (no output, just die) will throw <code>WPAjaxDieStopException( $message )</code>.
|
2013-08-07 08:38:38 +02:00
|
|
|
* You can test for this with:
|
|
|
|
* <code>
|
2020-09-18 15:53:31 +02:00
|
|
|
* $this->expectException( 'WPAjaxDieStopException' );
|
|
|
|
* $this->expectExceptionMessage( 'something contained in $message' );
|
2013-08-07 08:38:38 +02:00
|
|
|
* </code>
|
2020-09-18 15:53:31 +02:00
|
|
|
*
|
|
|
|
* Normal program termination (wp_die called at the end of output) will throw <code>WPAjaxDieContinueException( $message )</code>.
|
2013-08-07 08:38:38 +02:00
|
|
|
* You can test for this with:
|
|
|
|
* <code>
|
2020-09-18 15:53:31 +02:00
|
|
|
* $this->expectException( 'WPAjaxDieContinueException' );
|
|
|
|
* $this->expectExceptionMessage( 'something contained in $message' );
|
2013-08-07 08:38:38 +02:00
|
|
|
* </code>
|
2017-12-01 00:09:33 +01:00
|
|
|
*
|
2020-01-05 16:31:10 +01:00
|
|
|
* @param string $message The message to set.
|
|
|
|
*
|
|
|
|
* @throws WPAjaxDieStopException Thrown to stop further execution.
|
|
|
|
* @throws WPAjaxDieContinueException Thrown to stop execution of the Ajax function,
|
|
|
|
* but continue the unit test.
|
2013-08-07 08:38:38 +02:00
|
|
|
*/
|
|
|
|
public function dieHandler( $message ) {
|
|
|
|
$this->_last_response .= ob_get_clean();
|
2013-09-13 23:32:54 +02:00
|
|
|
|
2013-08-07 08:38:38 +02:00
|
|
|
if ( '' === $this->_last_response ) {
|
2013-09-14 20:35:43 +02:00
|
|
|
if ( is_scalar( $message ) ) {
|
2013-08-07 08:38:38 +02:00
|
|
|
throw new WPAjaxDieStopException( (string) $message );
|
|
|
|
} else {
|
|
|
|
throw new WPAjaxDieStopException( '0' );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new WPAjaxDieContinueException( $message );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-05 16:31:10 +01:00
|
|
|
* Switches between user roles.
|
|
|
|
*
|
|
|
|
* E.g. administrator, editor, author, contributor, subscriber.
|
2017-12-01 00:09:33 +01:00
|
|
|
*
|
2020-01-05 16:31:10 +01:00
|
|
|
* @param string $role The role to set.
|
2013-08-07 08:38:38 +02:00
|
|
|
*/
|
|
|
|
protected function _setRole( $role ) {
|
2017-12-01 00:09:33 +01:00
|
|
|
$post = $_POST;
|
2015-10-17 20:02:16 +02:00
|
|
|
$user_id = self::factory()->user->create( array( 'role' => $role ) );
|
2013-08-07 08:38:38 +02:00
|
|
|
wp_set_current_user( $user_id );
|
2017-12-01 00:09:33 +01:00
|
|
|
$_POST = array_merge( $_POST, $post );
|
2013-08-07 08:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-05 16:31:10 +01:00
|
|
|
* Mimics the Ajax handling of admin-ajax.php.
|
|
|
|
*
|
|
|
|
* Captures the output via output buffering, and if there is any,
|
|
|
|
* stores it in $this->_last_response.
|
2017-12-01 00:09:33 +01:00
|
|
|
*
|
2020-01-05 16:31:10 +01:00
|
|
|
* @param string $action The action to handle.
|
2013-08-07 08:38:38 +02:00
|
|
|
*/
|
2016-04-14 04:28:03 +02:00
|
|
|
protected function _handleAjax( $action ) {
|
2013-08-07 08:38:38 +02:00
|
|
|
|
2020-01-29 01:43:23 +01:00
|
|
|
// Start output buffering.
|
2013-08-07 08:38:38 +02:00
|
|
|
ini_set( 'implicit_flush', false );
|
|
|
|
ob_start();
|
|
|
|
|
2020-01-29 01:43:23 +01:00
|
|
|
// Build the request.
|
2013-08-07 08:38:38 +02:00
|
|
|
$_POST['action'] = $action;
|
|
|
|
$_GET['action'] = $action;
|
|
|
|
$_REQUEST = array_merge( $_POST, $_GET );
|
|
|
|
|
2020-01-29 01:43:23 +01:00
|
|
|
// Call the hooks.
|
2013-08-07 08:38:38 +02:00
|
|
|
do_action( 'admin_init' );
|
|
|
|
do_action( 'wp_ajax_' . $_REQUEST['action'], null );
|
|
|
|
|
2020-01-29 01:43:23 +01:00
|
|
|
// Save the output.
|
2013-08-07 08:38:38 +02:00
|
|
|
$buffer = ob_get_clean();
|
2017-12-01 00:09:33 +01:00
|
|
|
if ( ! empty( $buffer ) ) {
|
2013-08-07 08:38:38 +02:00
|
|
|
$this->_last_response = $buffer;
|
2017-12-01 00:09:33 +01:00
|
|
|
}
|
2013-08-07 08:38:38 +02:00
|
|
|
}
|
|
|
|
}
|