| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Tests the WP_SimplePie_File class, whether it conforms with the SimplePie_File. |
| 5 | * |
| 6 | * The SimplePie_File class joins multiple headers of the same name into one with |
| 7 | * values separated by a comma, except for the content-type header, where only |
| 8 | * the last one is used. |
| 9 | * |
| 10 | * @group feed |
| 11 | */ |
| 12 | class Tests_WP_SimplePie_File extends WP_UnitTestCase { |
| 13 | |
| 14 | public static function setUpBeforeClass() { |
| 15 | require_once ABSPATH . '/wp-includes/class-simplepie.php'; |
| 16 | require_once ABSPATH . '/wp-includes/class-wp-simplepie-file.php'; |
| 17 | } |
| 18 | |
| 19 | public function setUp() { |
| 20 | add_filter( 'pre_http_request', array( $this, 'mocked_http_request' ) ); |
| 21 | } |
| 22 | |
| 23 | public function tearDown() { |
| 24 | remove_filter( 'pre_http_request', array( $this, 'mocked_http_request' ) ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Multiple headers of the same name should be joint into one, separated by a comma. |
| 29 | * |
| 30 | * @ticket 51056 |
| 31 | */ |
| 32 | public function test_link_headers_parsing() { |
| 33 | $file = new WP_SimplePie_File( 'https://wordpress.org/news/feed/' ); |
| 34 | |
| 35 | $this->assertSame( '<https://wordpress.org/news/wp-json/>; rel="https://api.w.org/", <https://wordpress.org/news/wp/v2/categories/3>; rel="alternate"; type="application/json"', $file->headers['link'] ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Only the last content type header should be used. |
| 40 | * |
| 41 | * @ticket 51056 |
| 42 | */ |
| 43 | public function test_content_type_header_parsing() { |
| 44 | $file = new WP_SimplePie_File( 'https://wordpress.org/news/feed/' ); |
| 45 | $this->assertSame( 'application/rss+xml; charset=UTF-8', $file->headers['content-type'] ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Mock the http request to a feed. |
| 50 | */ |
| 51 | public function mocked_http_request() { |
| 52 | $headers = new Requests_Utility_CaseInsensitiveDictionary( array( |
| 53 | 'content-type' => 'application/rss+xml; charset=ISO-8859-2', |
| 54 | 'link' => array( |
| 55 | '<https://wordpress.org/news/wp-json/>; rel="https://api.w.org/"', |
| 56 | '<https://wordpress.org/news/wp/v2/categories/3>; rel="alternate"; type="application/json"', |
| 57 | ), |
| 58 | 'content-type' => 'application/rss+xml; charset=UTF-8', |
| 59 | ) ); |
| 60 | $body = <<<EOL |
| 61 | <?xml version="1.0" encoding="UTF-8"?><rss version="2.0" |
| 62 | xmlns:content="http://purl.org/rss/1.0/modules/content/" |
| 63 | xmlns:wfw="http://wellformedweb.org/CommentAPI/" |
| 64 | xmlns:dc="http://purl.org/dc/elements/1.1/" |
| 65 | xmlns:atom="http://www.w3.org/2005/Atom" |
| 66 | xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" |
| 67 | xmlns:slash="http://purl.org/rss/1.0/modules/slash/" |
| 68 | xmlns:georss="http://www.georss.org/georss" |
| 69 | xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" |
| 70 | > |
| 71 | <channel> |
| 72 | <title>News – – WordPress.org</title> |
| 73 | <atom:link href="https://wordpress.org/news/feed/" rel="self" type="application/rss+xml" /> |
| 74 | <link>https://wordpress.org/news</link> |
| 75 | <description>WordPress News</description> |
| 76 | <lastBuildDate>Tue, 01 Sep 2020 21:00:15 +0000</lastBuildDate> |
| 77 | <language>en-US</language> |
| 78 | <sy:updatePeriod> |
| 79 | hourly </sy:updatePeriod> |
| 80 | <sy:updateFrequency> |
| 81 | 1 </sy:updateFrequency> |
| 82 | <generator>https://wordpress.org/?v=5.6-alpha-49040</generator> |
| 83 | <site xmlns="com-wordpress:feed-additions:1">14607090</site> <item> |
| 84 | <title>WordPress 5.5.1 Maintenance Release</title> |
| 85 | <link>https://wordpress.org/news/2020/09/wordpress-5-5-1-maintenance-release/</link> |
| 86 | <dc:creator><![CDATA[Jb Audras]]></dc:creator> |
| 87 | <pubDate>Tue, 01 Sep 2020 19:13:53 +0000</pubDate> |
| 88 | <category><![CDATA[Releases]]></category> |
| 89 | <guid isPermaLink="false">https://wordpress.org/news/?p=8979</guid> |
| 90 | |
| 91 | <description><![CDATA[WordPress 5.5.1 is now available! This maintenance release features 34 bug fixes, 5 enhancements, and 5 bug fixes for the block editor. These bugs affect WordPress version 5.5, so you’ll want to upgrade. You can download WordPress 5.5.1 directly, or visit the Dashboard → Updates screen and click Update Now. If your sites support automatic background updates, they’ve already started the update process. […]]]></description> |
| 92 | <content:encoded><![CDATA[ |
| 93 | <p>WordPress 5.5.1 is now available!</p> |
| 94 | ]]></content:encoded> |
| 95 | <post-id xmlns="com-wordpress:feed-additions:1">8979</post-id> </item> |
| 96 | EOL; |
| 97 | return array( |
| 98 | 'headers' => $headers, |
| 99 | 'body' => $body, |
| 100 | 'response' => array( |
| 101 | 'code' => 200, |
| 102 | 'message' => 'OK' |
| 103 | ), |
| 104 | 'cookies' => array(), |
| 105 | 'filename' => null, |
| 106 | ); |
| 107 | } |
| 108 | } |