From 8045afd81b7c80f6ef5b327c115a5bbb43e4b65c Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Thu, 29 Aug 2013 18:39:34 +0000 Subject: Move PHPUnit tests into a tests/phpunit directory. wp-tests-config.php can/should reside in the root of a develop checkout. `phpunit` should be run from the root. see #25088. git-svn-id: https://develop.svn.wordpress.org/trunk@25165 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/testcase-ajax.php | 182 +++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 tests/phpunit/includes/testcase-ajax.php (limited to 'tests/phpunit/includes/testcase-ajax.php') diff --git a/tests/phpunit/includes/testcase-ajax.php b/tests/phpunit/includes/testcase-ajax.php new file mode 100644 index 0000000000..fcceacc42b --- /dev/null +++ b/tests/phpunit/includes/testcase-ajax.php @@ -0,0 +1,182 @@ +_core_actions_get, $this->_core_actions_post ) as $action ) + if ( function_exists( 'wp_ajax_' . str_replace( '-', '_', $action ) ) ) + add_action( 'wp_ajax_' . $action, 'wp_ajax_' . str_replace( '-', '_', $action ), 1 ); + + add_filter( 'wp_die_ajax_handler', array( $this, 'getDieHandler' ), 1, 1 ); + if ( !defined( 'DOING_AJAX' ) ) + define( 'DOING_AJAX', true ); + set_current_screen( 'ajax' ); + + // Clear logout cookies + add_action( 'clear_auth_cookie', array( $this, 'logout' ) ); + + // Suppress warnings from "Cannot modify header information - headers already sent by" + $this->_error_level = error_reporting(); + error_reporting( $this->_error_level & ~E_WARNING ); + + // Make some posts + $this->factory->post->create_many( 5 ); + } + + /** + * Tear down the test fixture. + * Reset $_POST, remove the wp_die() override, restore 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' ); + } + + /** + * Clear login cookies, unset 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] ); + } + + /** + * Return 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 + */ + public function dieHandler( $message ) { + $this->_last_response .= ob_get_clean(); + ob_end_clean(); + if ( '' === $this->_last_response ) { + if ( is_scalar( $message) ) { + throw new WPAjaxDieStopException( (string) $message ); + } else { + throw new WPAjaxDieStopException( '0' ); + } + } else { + throw new WPAjaxDieContinueException( $message ); + } + } + + /** + * Switch between user roles + * E.g. administrator, editor, author, contributor, subscriber + * @param string $role + */ + protected function _setRole( $role ) { + $post = $_POST; + $user_id = $this->factory->user->create( array( 'role' => $role ) ); + wp_set_current_user( $user_id ); + $_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_message. + * @param string $action + */ + 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; + } +} -- cgit v1.2.3