_error_level = error_reporting();
error_reporting( $this->_error_level & ~E_WARNING );
// Make some posts.
self::factory()->post->create_many( 5 );
}
/**
* Tears down the test fixture.
*
* Resets $_POST, removes the wp_die() override, restores error reporting.
*/
public function tearDown() {
parent::tearDown();
$_POST = array();
$_GET = array();
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' );
}
/**
* Clears login cookies, unsets the current user.
*/
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 ] );
}
}
/**
* Returns our callback handler
*
* @return callback
*/
public function getDieHandler() {
return array( $this, 'dieHandler' );
}
/**
* Handler for wp_die().
*
* Save the output for analysis, stop execution by throwing an exception.
*
* Error conditions (no output, just die) will throw WPAjaxDieStopException( $message )
* You can test for this with:
*
* $this->setExpectedException( 'WPAjaxDieStopException', 'something contained in $message' );
*
* Normal program termination (wp_die called at then end of output) will throw WPAjaxDieContinueException( $message )
* You can test for this with:
*
* $this->setExpectedException( 'WPAjaxDieContinueException', 'something contained in $message' );
*
*
* @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.
*/
public function dieHandler( $message ) {
$this->_last_response .= ob_get_clean();
if ( '' === $this->_last_response ) {
if ( is_scalar( $message ) ) {
throw new WPAjaxDieStopException( (string) $message );
} else {
throw new WPAjaxDieStopException( '0' );
}
} else {
throw new WPAjaxDieContinueException( $message );
}
}
/**
* Switches between user roles.
*
* E.g. administrator, editor, author, contributor, subscriber.
*
* @param string $role The role to set.
*/
protected function _setRole( $role ) {
$post = $_POST;
$user_id = self::factory()->user->create( array( 'role' => $role ) );
wp_set_current_user( $user_id );
$_POST = array_merge( $_POST, $post );
}
/**
* 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.
*
* @param string $action The action to handle.
*/
protected function _handleAjax( $action ) {
// Start output buffering.
ini_set( 'implicit_flush', false );
ob_start();
// Build the request.
$_POST['action'] = $action;
$_GET['action'] = $action;
$_REQUEST = array_merge( $_POST, $_GET );
// Call the hooks.
do_action( 'admin_init' );
do_action( 'wp_ajax_' . $_REQUEST['action'], null );
// Save the output.
$buffer = ob_get_clean();
if ( ! empty( $buffer ) ) {
$this->_last_response = $buffer;
}
}
}