testcase-ajax.php 5.3 KB

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