testcase-ajax.php 6.6 KB

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