Response.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Testing ajax response class
  4. *
  5. * @package WordPress
  6. * @subpackage UnitTests
  7. * @since 3.5.0
  8. * @group ajax
  9. */
  10. class Tests_Ajax_Response extends WP_UnitTestCase {
  11. /**
  12. * Saved error reporting level
  13. * @var int
  14. */
  15. protected $_error_level = 0;
  16. /**
  17. * Set up the test fixture.
  18. * Override wp_die(), pretend to be ajax, and suppres E_WARNINGs
  19. */
  20. public function setUp() {
  21. parent::setUp();
  22. add_filter( 'wp_die_ajax_handler', array( $this, 'getDieHandler' ), 1, 1 );
  23. if ( !defined( 'DOING_AJAX' ) )
  24. define( 'DOING_AJAX', true );
  25. // Suppress warnings from "Cannot modify header information - headers already sent by"
  26. $this->_error_level = error_reporting();
  27. error_reporting( $this->_error_level & ~E_WARNING );
  28. }
  29. /**
  30. * Tear down the test fixture.
  31. * Remove the wp_die() override, restore error reporting
  32. */
  33. public function tearDown() {
  34. parent::tearDown();
  35. remove_filter( 'wp_die_ajax_handler', array( $this, 'getDieHandler' ), 1, 1 );
  36. error_reporting( $this->_error_level );
  37. }
  38. /**
  39. * Return our callback handler
  40. * @return callback
  41. */
  42. public function getDieHandler() {
  43. return array( $this, 'dieHandler' );
  44. }
  45. /**
  46. * Handler for wp_die()
  47. * Don't die, just continue on.
  48. * @param string $message
  49. */
  50. public function dieHandler( $message ) {
  51. }
  52. /**
  53. * Test that charset in header matches blog_charset
  54. * Note: headers_list doesn't work properly in CLI mode, fall back on
  55. * xdebug_get_headers if it's available
  56. * Needs a separate process to get around the headers/output from the
  57. * bootstrapper
  58. * @ticket 19448
  59. * @runInSeparateProcess
  60. */
  61. public function test_response_charset_in_header() {
  62. if ( !function_exists( 'xdebug_get_headers' ) ) {
  63. $this->markTestSkipped( 'xdebug is required for this test' );
  64. }
  65. // Generate an ajax response
  66. ob_start();
  67. $ajax_response = new WP_Ajax_Response();
  68. $ajax_response->send();
  69. // Check the header
  70. $headers = xdebug_get_headers();
  71. ob_end_clean();
  72. $this->assertTrue( in_array( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), $headers ) );
  73. }
  74. /**
  75. * Test that charset in the xml tag matches blog_charset
  76. * @ticket 19448
  77. */
  78. public function test_response_charset_in_xml() {
  79. // Generate an ajax response
  80. ob_start();
  81. $ajax_response = new WP_Ajax_Response();
  82. $ajax_response->send();
  83. // Check the XML tag
  84. $contents = ob_get_clean();
  85. $this->assertRegExp( '/<\?xml\s+version=\'1.0\'\s+encoding=\'' . preg_quote( get_option( 'blog_charset' ) ) . '\'\s+standalone=\'yes\'\?>/', $contents );
  86. }
  87. }