Make WordPress Core

source: trunk/tests/phpunit/includes/testcase-ajax.php @ 37475

Last change on this file since 37475 was 37288, checked in by ocean90, 9 years ago

Media: Remove an extra quote when sending a link of a media file to the editor.

Introduced in [37035].

Props joemcgill, swissspidy, boonebgorges.
Fixes #36578.

File size: 6.0 KB
Line 
1<?php
2/**
3 * Ajax test cases
4 *
5 * @package    WordPress
6 * @subpackage UnitTests
7 * @since      3.4.0
8 */
9
10/**
11 * Ajax test case class
12 *
13 * @package    WordPress
14 * @subpackage UnitTests
15 * @since      3.4.0
16 */
17abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
18
19        /**
20         * Last AJAX response.  This is set via echo -or- wp_die.
21         * @var type
22         */
23        protected $_last_response = '';
24
25        /**
26         * List of ajax actions called via POST
27         * @var type
28         */
29        protected static $_core_actions_get = array(
30                'fetch-list', 'ajax-tag-search', 'wp-compression-test', 'imgedit-preview', 'oembed-cache',
31                'autocomplete-user', 'dashboard-widgets', 'logged-in',
32        );
33
34        /**
35         * Saved error reporting level
36         * @var int
37         */
38        protected $_error_level = 0;
39
40        /**
41         * List of ajax actions called via GET
42         * @var type
43         */
44        protected static $_core_actions_post = array(
45                'oembed_cache', 'image-editor', 'delete-comment', 'delete-tag', 'delete-link',
46                'delete-meta', 'delete-post', 'trash-post', 'untrash-post', 'delete-page', 'dim-comment',
47                'add-link-category', 'add-tag', 'get-tagcloud', 'get-comments', 'replyto-comment',
48                'edit-comment', 'add-menu-item', 'add-meta', 'add-user', 'closed-postboxes',
49                'hidden-columns', 'update-welcome-panel', 'menu-get-metabox', 'wp-link-ajax',
50                'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink',
51                'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
52                'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post',
53                'wp-remove-post-lock', 'dismiss-wp-pointer', 'send-attachment-to-editor', 'heartbeat', 'nopriv_heartbeat', 'get-revision-diffs',
54                'save-user-color-scheme', 'update-widget', 'query-themes', 'parse-embed', 'set-attachment-thumbnail',
55                'parse-media-shortcode', 'destroy-sessions', 'install-plugin', 'update-plugin', 'press-this-save-post',
56                'press-this-add-category', 'crop-image', 'generate-password',
57        );
58
59        public static function setUpBeforeClass() {
60                if ( ! defined( 'DOING_AJAX' ) ) {
61                        define( 'DOING_AJAX', true );
62                }
63
64                remove_action( 'admin_init', '_maybe_update_core' );
65                remove_action( 'admin_init', '_maybe_update_plugins' );
66                remove_action( 'admin_init', '_maybe_update_themes' );
67
68                // Register the core actions
69                foreach ( array_merge( self::$_core_actions_get, self::$_core_actions_post ) as $action ) {
70                        if ( function_exists( 'wp_ajax_' . str_replace( '-', '_', $action ) ) ) {
71                                add_action( 'wp_ajax_' . $action, 'wp_ajax_' . str_replace( '-', '_', $action ), 1 );
72                        }
73                }
74
75                parent::setUpBeforeClass();
76        }
77
78        /**
79         * Set up the test fixture.
80         * Override wp_die(), pretend to be ajax, and suppres E_WARNINGs
81         */
82        public function setUp() {
83                parent::setUp();
84
85                add_filter( 'wp_die_ajax_handler', array( $this, 'getDieHandler' ), 1, 1 );
86
87                set_current_screen( 'ajax' );
88
89                // Clear logout cookies
90                add_action( 'clear_auth_cookie', array( $this, 'logout' ) );
91
92                // Suppress warnings from "Cannot modify header information - headers already sent by"
93                $this->_error_level = error_reporting();
94                error_reporting( $this->_error_level & ~E_WARNING );
95
96                // Make some posts
97                self::factory()->post->create_many( 5 );
98        }
99
100        /**
101         * Tear down the test fixture.
102         * Reset $_POST, remove the wp_die() override, restore error reporting
103         */
104        public function tearDown() {
105                parent::tearDown();
106                $_POST = array();
107                $_GET = array();
108                unset( $GLOBALS['post'] );
109                unset( $GLOBALS['comment'] );
110                remove_filter( 'wp_die_ajax_handler', array( $this, 'getDieHandler' ), 1, 1 );
111                remove_action( 'clear_auth_cookie', array( $this, 'logout' ) );
112                error_reporting( $this->_error_level );
113                set_current_screen( 'front' );
114        }
115
116        /**
117         * Clear login cookies, unset the current user
118         */
119        public function logout() {
120                unset( $GLOBALS['current_user'] );
121                $cookies = array(AUTH_COOKIE, SECURE_AUTH_COOKIE, LOGGED_IN_COOKIE, USER_COOKIE, PASS_COOKIE);
122                foreach ( $cookies as $c )
123                        unset( $_COOKIE[$c] );
124        }
125
126        /**
127         * Return our callback handler
128         * @return callback
129         */
130        public function getDieHandler() {
131                return array( $this, 'dieHandler' );
132        }
133
134        /**
135         * Handler for wp_die()
136         * Save the output for analysis, stop execution by throwing an exception.
137         * Error conditions (no output, just die) will throw <code>WPAjaxDieStopException( $message )</code>
138         * You can test for this with:
139         * <code>
140         * $this->setExpectedException( 'WPAjaxDieStopException', 'something contained in $message' );
141         * </code>
142         * Normal program termination (wp_die called at then end of output) will throw <code>WPAjaxDieContinueException( $message )</code>
143         * You can test for this with:
144         * <code>
145         * $this->setExpectedException( 'WPAjaxDieContinueException', 'something contained in $message' );
146         * </code>
147         * @param string $message
148         */
149        public function dieHandler( $message ) {
150                $this->_last_response .= ob_get_clean();
151
152                if ( '' === $this->_last_response ) {
153                        if ( is_scalar( $message ) ) {
154                                throw new WPAjaxDieStopException( (string) $message );
155                        } else {
156                                throw new WPAjaxDieStopException( '0' );
157                        }
158                } else {
159                        throw new WPAjaxDieContinueException( $message );
160                }
161        }
162
163        /**
164         * Switch between user roles
165         * E.g. administrator, editor, author, contributor, subscriber
166         * @param string $role
167         */
168        protected function _setRole( $role ) {
169                $post = $_POST;
170                $user_id = self::factory()->user->create( array( 'role' => $role ) );
171                wp_set_current_user( $user_id );
172                $_POST = array_merge($_POST, $post);
173        }
174
175        /**
176         * Mimic the ajax handling of admin-ajax.php
177         * Capture the output via output buffering, and if there is any, store
178         * it in $this->_last_response.
179         * @param string $action
180         */
181        protected function _handleAjax( $action ) {
182
183                // Start output buffering
184                ini_set( 'implicit_flush', false );
185                ob_start();
186
187                // Build the request
188                $_POST['action'] = $action;
189                $_GET['action']  = $action;
190                $_REQUEST        = array_merge( $_POST, $_GET );
191
192                // Call the hooks
193                do_action( 'admin_init' );
194                do_action( 'wp_ajax_' . $_REQUEST['action'], null );
195
196                // Save the output
197                $buffer = ob_get_clean();
198                if ( !empty( $buffer ) )
199                        $this->_last_response = $buffer;
200        }
201}
Note: See TracBrowser for help on using the repository browser.