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