|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for wp_is_xml_request(). |
| 5 | + * |
| 6 | + * @group load |
| 7 | + * |
| 8 | + * @covers ::wp_is_xml_request |
| 9 | + */ |
| 10 | +class Tests_Load_WpIsXmlRequest extends WP_UnitTestCase { |
| 11 | + |
| 12 | + /** |
| 13 | + * Original HTTP_ACCEPT value. |
| 14 | + * |
| 15 | + * @var string|null |
| 16 | + */ |
| 17 | + private $original_http_accept; |
| 18 | + |
| 19 | + /** |
| 20 | + * Original CONTENT_TYPE value. |
| 21 | + * |
| 22 | + * @var string|null |
| 23 | + */ |
| 24 | + private $original_content_type; |
| 25 | + |
| 26 | + public function set_up() { |
| 27 | + parent::set_up(); |
| 28 | + |
| 29 | + $this->original_http_accept = isset( $_SERVER['HTTP_ACCEPT'] ) ? $_SERVER['HTTP_ACCEPT'] : null; |
| 30 | + $this->original_content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? $_SERVER['CONTENT_TYPE'] : null; |
| 31 | + } |
| 32 | + |
| 33 | + public function tear_down() { |
| 34 | + if ( null === $this->original_http_accept ) { |
| 35 | + unset( $_SERVER['HTTP_ACCEPT'] ); |
| 36 | + } else { |
| 37 | + $_SERVER['HTTP_ACCEPT'] = $this->original_http_accept; |
| 38 | + } |
| 39 | + |
| 40 | + if ( null === $this->original_content_type ) { |
| 41 | + unset( $_SERVER['CONTENT_TYPE'] ); |
| 42 | + } else { |
| 43 | + $_SERVER['CONTENT_TYPE'] = $this->original_content_type; |
| 44 | + } |
| 45 | + |
| 46 | + parent::tear_down(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Tests that non-XML Accept and Content-Type headers are not XML requests. |
| 51 | + * |
| 52 | + * @ticket 65819 |
| 53 | + */ |
| 54 | + public function test_returns_false_for_non_xml_headers() { |
| 55 | + unset( $_SERVER['HTTP_ACCEPT'], $_SERVER['CONTENT_TYPE'] ); |
| 56 | + |
| 57 | + $this->assertFalse( wp_is_xml_request() ); |
| 58 | + |
| 59 | + $_SERVER['HTTP_ACCEPT'] = 'text/html'; |
| 60 | + $this->assertFalse( wp_is_xml_request() ); |
| 61 | + |
| 62 | + unset( $_SERVER['HTTP_ACCEPT'] ); |
| 63 | + $_SERVER['CONTENT_TYPE'] = 'application/json'; |
| 64 | + $this->assertFalse( wp_is_xml_request() ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Tests XML detection via the Accept header. |
| 69 | + * |
| 70 | + * @ticket 65819 |
| 71 | + * |
| 72 | + * @dataProvider data_xml_accept_headers |
| 73 | + * |
| 74 | + * @param string $accept Accept header value. |
| 75 | + */ |
| 76 | + public function test_returns_true_for_xml_accept_header( $accept ) { |
| 77 | + unset( $_SERVER['CONTENT_TYPE'] ); |
| 78 | + $_SERVER['HTTP_ACCEPT'] = $accept; |
| 79 | + |
| 80 | + $this->assertTrue( wp_is_xml_request() ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Data provider for XML Accept headers. |
| 85 | + * |
| 86 | + * @return array[] |
| 87 | + */ |
| 88 | + public static function data_xml_accept_headers() { |
| 89 | + return array( |
| 90 | + 'text/xml' => array( 'text/xml' ), |
| 91 | + 'application/rss+xml' => array( 'application/rss+xml' ), |
| 92 | + 'application/atom+xml' => array( 'application/atom+xml' ), |
| 93 | + 'application/rdf+xml' => array( 'application/rdf+xml' ), |
| 94 | + 'text/xml+oembed' => array( 'text/xml+oembed' ), |
| 95 | + 'application/xml+oembed' => array( 'application/xml+oembed' ), |
| 96 | + 'accept among other types' => array( 'text/html, application/rss+xml, */*' ), |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Tests XML detection via the Content-Type header. |
| 102 | + * |
| 103 | + * @ticket 65819 |
| 104 | + * |
| 105 | + * @dataProvider data_xml_content_types |
| 106 | + * |
| 107 | + * @param string $content_type Content-Type header value. |
| 108 | + */ |
| 109 | + public function test_returns_true_for_xml_content_type( $content_type ) { |
| 110 | + unset( $_SERVER['HTTP_ACCEPT'] ); |
| 111 | + $_SERVER['CONTENT_TYPE'] = $content_type; |
| 112 | + |
| 113 | + $this->assertTrue( wp_is_xml_request() ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Data provider for XML Content-Type headers. |
| 118 | + * |
| 119 | + * @return array[] |
| 120 | + */ |
| 121 | + public static function data_xml_content_types() { |
| 122 | + return array( |
| 123 | + 'text/xml' => array( 'text/xml' ), |
| 124 | + 'application/rss+xml' => array( 'application/rss+xml' ), |
| 125 | + 'application/atom+xml' => array( 'application/atom+xml' ), |
| 126 | + 'application/rdf+xml' => array( 'application/rdf+xml' ), |
| 127 | + 'text/xml+oembed' => array( 'text/xml+oembed' ), |
| 128 | + 'application/xml+oembed' => array( 'application/xml+oembed' ), |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
0 commit comments