Make WordPress Core

Ticket #51056: 51056.diff

File 51056.diff, 5.4 KB (added by david.binda, 3 years ago)
  • src/wp-includes/class-wp-simplepie-file.php

     
    6262                                $this->success = false;
    6363                        } else {
    6464                                $this->headers     = wp_remote_retrieve_headers( $res );
     65                                // Merge multiple headers of the same name to a single one, separated by comma.
     66                                foreach ( $this->headers as $name => $value ) {
     67                                        if ( is_array( $value ) && $name === 'content-type' ) { // We should only use the last Content-Type header.
     68                                                $this->headers[ $name ] = array_pop( $value );
     69                                        } else if( is_array( $value ) ) {
     70                                                $this->headers[ $name ] = join( ', ', $value );
     71                                        }
     72                                }
    6573                                $this->body        = wp_remote_retrieve_body( $res );
    6674                                $this->status_code = wp_remote_retrieve_response_code( $res );
    6775                        }
  • tests/phpunit/tests/feed/wp-simplepie-file.php

     
     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 */
     12class 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 &#8211;  &#8211; 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&#160;34 bug fixes, 5 enhancements, and&#160;5 bug fixes&#160;for the&#160;block&#160;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&#160;Dashboard → Updates screen&#160;and click&#160;Update Now. If your sites support automatic background updates, they’ve already started the update process. [&#8230;]]]></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>
     96EOL;
     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}