123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
-
- protected $_last_response = '';
-
- protected $_core_actions_get = array( 'fetch-list', 'ajax-tag-search', 'wp-compression-test', 'imgedit-preview', 'oembed_cache' );
-
- protected $_error_level = 0;
-
- protected $_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', 'autosave', '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', 'nopriv_autosave'
- );
-
- public function setUp() {
- parent::setUp();
-
- foreach ( array_merge( $this->_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' );
-
- add_action( 'clear_auth_cookie', array( $this, 'logout' ) );
-
- $this->_error_level = error_reporting();
- error_reporting( $this->_error_level & ~E_WARNING );
-
- $this->factory->post->create_many( 5 );
- }
-
- 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' );
- }
-
- 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] );
- }
-
- public function getDieHandler() {
- return array( $this, 'dieHandler' );
- }
-
- 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 );
- }
- }
-
- 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);
- }
-
- protected function _handleAjax($action) {
-
- ini_set( 'implicit_flush', false );
- ob_start();
-
- $_POST['action'] = $action;
- $_GET['action'] = $action;
- $_REQUEST = array_merge( $_POST, $_GET );
-
- do_action( 'admin_init' );
- do_action( 'wp_ajax_' . $_REQUEST['action'], null );
-
- $buffer = ob_get_clean();
- if ( !empty( $buffer ) )
- $this->_last_response = $buffer;
- }
- }
|