Make WordPress Core

Ticket #37096: 37096.3.patch

File 37096.3.patch, 11.0 KB (added by pbearne, 4 years ago)

changed xmlrpc_getpostcategory to use the default_category set in options

  • src/wp-includes/functions.php

     
    742742function xmlrpc_getposttitle( $content ) {
    743743        global $post_default_title;
    744744        if ( preg_match( '/<title>(.+?)<\/title>/is', $content, $matchtitle ) ) {
    745                 $post_title = $matchtitle[1];
     745                $post_title = trim( $matchtitle[1] );
     746                if( empty( $post_title ) ) {
     747                        $post_title = $post_default_title;
     748                }
    746749        } else {
    747750                $post_title = $post_default_title;
    748751        }
     
    753756 * Retrieve the post category or categories from XMLRPC XML.
    754757 *
    755758 * If the category element is not found, then the default post category will be
    756  * used. The return type then would be what $post_default_category. If the
    757  * category is found, then it will always be an array.
     759 * used. The return type then would be what $post_default_category.
     760 * If more than one category is found, then it will return an array.
    758761 *
    759762 * @since 0.71
    760763 *
     
    764767 * @return string|array List of categories or category name.
    765768 */
    766769function xmlrpc_getpostcategory( $content ) {
    767         global $post_default_category;
     770        $default_term = get_term_by( 'id', get_option('default_category'), 'category' );
     771        $default_term_name = ( isset( $default_term->name ) ) ? $default_term->name : sanitize_title( _x( 'Uncategorized', 'Default category slug' ) );
     772
    768773        if ( preg_match( '/<category>(.+?)<\/category>/is', $content, $matchcat ) ) {
    769774                $post_category = trim( $matchcat[1], ',' );
    770                 $post_category = explode( ',', $post_category );
     775                $post_category = array_unique( array_map('trim', explode( ',', $post_category ) ) );
     776                if ( count($post_category) === 1 ){
     777                        $post_category = $post_category[0];
     778                }
     779                if( empty( $post_category ) ) {
     780                        $post_category = $default_term_name;
     781                }
    771782        } else {
    772                 $post_category = $post_default_category;
     783                $post_category = $default_term_name;
    773784        }
    774785        return $post_category;
    775786}
     
    53915402
    53925403        if ( ! is_null( $force ) ) {
    53935404                $old_forced = $forced;
    5394                 $forced     = $force;
     5405                $forced     = ( $force === true || trim( $force ) === 'true' ) ? true : false;
    53955406                return $old_forced;
    53965407        }
    53975408
  • tests/phpunit/tests/functions/xmlrpc-getpostcatergory.php

     
     1<?php
     2
     3/**
     4 * @group functions.php
     5 * @group xmlrpc2
     6 *
     7 * Class Tests_Xmlrpc_Getpostcategory
     8 * test the xmlrpc_getpostcategory function
     9 */
     10class Tests_Xmlrpc_GetPostCategory extends WP_UnitTestCase
     11{
     12        static $term_id;
     13        static $term_name;
     14
     15        public static function wpSetUpBeforeClass( $factory ) {
     16
     17                $id = $factory->category->create();
     18                update_option('default_category', $id );
     19                self::$term_name = get_term_by( 'id', $id,'category')->name;
     20        }
     21
     22//      /**
     23//       * setup
     24//       * declare the default category needed in function
     25//       */
     26//      function setup(){
     27//              self::$term_id = $this->factory->category->create();
     28//              update_option('default_category', self::$term_id );
     29//              self::$term_name = get_term_by( 'id', self::$term_id,'category')->name;
     30//      }
     31//
     32//      /**
     33//       * remove global
     34//       */
     35//      function tearDown(){
     36//              wp_delete_term( self::$term_id, 'category' );
     37//              delete_option('default_category' );
     38//      }
     39
     40        /**
     41         * @dataProvider xmlrpc_getpostcategory_data
     42         *
     43         * @covers ::xmlrpc_getpostcategory
     44         */
     45        function test_xmlrpc_getpostcategory( $expected, $value,  $text){
     46                if ( $expected === null ){
     47                        $expected = self::$term_name;
     48                }
     49
     50                $this->assertEquals($expected, xmlrpc_getpostcategory($value), $text);
     51        }
     52
     53
     54        function xmlrpc_getpostcategory_data(){
     55
     56                return array(
     57                        array('good_category', '<category>good_category</category>', 'single category'),
     58                        array(array('good_category1', 'good_category2', 'good_category3'), '<category>good_category1, good_category2, good_category3</category>', 'multiple categories'),
     59                        array(array(0 => 'good_category1', 2 => 'good_category3'), '<category>good_category1, good_category1, good_category3,</category>', 'duplicate categories'),
     60                        array('good_category', '<post><category>good_category</category><cat>fsdfsfsf</cat></post>', 'category in post tag'),
     61                        array('<category>category', '<category> <category>category</category> </category>', 'nested categories terms'),
     62                        array('<span>category</span>', '<category> <span>category</span> </category>', 'category nested span tags'),
     63                        array('category1', '<category>category1</category><category>category2</category>', 'find just the first category'),
     64                        array('<![CDATA["category"]]>', '<category><![CDATA["category"]]></category>', 'CDATA'),
     65                        array('<![CDATA["<category>category', '<category><![CDATA["<category>category</category>"]]></category>', 'cdata with tag category'),
     66
     67                        array(null, null, 'null'),
     68                        array(null, '<category>good_category<category>ssssss', 'missing close tag'),
     69                        array(null, '<category></category>', 'no category in tag'),
     70                        array(null, '<category> </category>', 'space in tag'),
     71                        array(null, '<category <span>category</span> </category>', 'bad opening tag'),
     72                        array(null, '<category>category</category dddddd>', 'bad closing tag'),
     73                        array(null, '<category id="id">category</category>', 'dosen\'t find category if attribute set'),
     74
     75
     76                        array( 'good_category', $this->_xml_rpc_request_fixture('<title>good_title</title>', '<category>good_category</category>' ), 'single title in XML'),
     77                );
     78        }
     79        private function _xml_rpc_request_fixture( $title = '', $category = '' )
     80        {
     81                $XML_RPC = "
     82                                <?xml version=\"1.0\"?>
     83                                        <methodResponse>
     84                                          <params>
     85                                                <param>
     86                                                  $title
     87                                                  $category
     88                                                </param>
     89                                          </params>
     90                                        </methodResponse>
     91                ";
     92
     93                return $XML_RPC;
     94        }
     95}
  • tests/phpunit/tests/functions/xmlrpc-getposttitle.php

     
     1<?php
     2
     3/**
     4 * @group functions.php
     5 * @group xmlrpc
     6 *
     7 * Class Tests_Xmlrpc_Getposttitle
     8 * test the xmlrpc_getposttitle function
     9 */
     10class Tests_Xmlrpc_Getposttitle extends WP_UnitTestCase
     11{
     12
     13        /**
     14         * setup
     15         * declare the default title needed in function
     16         */
     17        function setup()
     18        {
     19                global $post_default_title;
     20                $post_default_title = 'post_default_title';
     21        }
     22
     23        /**
     24         * remove global
     25         */
     26        function tearDown()
     27        {
     28                global $post_default_title;
     29                $post_default_title = null;
     30        }
     31
     32        /**
     33         * @dataProvider xmlrpc_getposttitle_data
     34         *
     35         * @covers ::xmlrpc_getposttitle
     36         */
     37        function test_xmlrpc_getposttitle( $expected, $value, $text)
     38        {
     39                $this->assertEquals($expected, xmlrpc_getposttitle( $value), $text);
     40        }
     41
     42
     43        function xmlrpc_getposttitle_data(){
     44                $post_default_title = 'post_default_title';
     45                return array(
     46                        array( 'good_title', '<title>good_title</title>', 'single title'),
     47                        array( 'good_title', '<post><title>good_title</title><cat>fsdfsfsf</cat></post>', 'good title in lot of content'),
     48                        array( '<title>title', '<title> <title>title</title> </title>', 'nested title'),
     49                        array( '<span>title</span>', '<title> <span>title</span> </title>', 'title with spans'),
     50                        array( '<![CDATA["title"]]>', '<title><![CDATA["title"]]></title>', 'pass cdata'),
     51                        array( '<![CDATA["<title>title', '<title><![CDATA["<title>title</title>"]]></title>', 'cdata with tag title'),
     52
     53                        array($post_default_title, null, 'null'),
     54                        array($post_default_title, '<title>good_title<title>ssssss', 'missing close'),
     55                        array($post_default_title, '<title></title>', 'title has no content'),
     56                        array($post_default_title, '<title> </title>', 'title is just space'),
     57                        array($post_default_title, '<title <span>title</span> </title>', 'bad opening tag'),
     58                        array($post_default_title, '<title>title</title dddddd>', ' bad closing tag'),
     59
     60                        array( 'good_title', $this->_xml_rpc_request_fixture('<title>good_title</title>', '<category>good_category</category>' ), 'single title in XML'),
     61                );
     62        }
     63
     64        private function _xml_rpc_request_fixture( $title = '', $category = '' )
     65        {
     66                $XML_RPC = "
     67                                <?xml version=\"1.0\"?>
     68                                        <methodResponse>
     69                                          <params>
     70                                                <param>
     71                                                  $title
     72                                                  $category
     73                                                </param>
     74                                          </params>
     75                                        </methodResponse>
     76                ";
     77
     78                return $XML_RPC;
     79        }
     80}
  • tests/phpunit/tests/functions/xmlrpc-removepostdata.php

     
     1<?php
     2/**
     3 * @group functions.php
     4 * @group xmlrpc
     5 *
     6 * Class Tests_Xmlrpc_Removepostdata
     7 * test the xmlrpc_removepostdata function
     8 */
     9
     10
     11class Tests_Xmlrpc_Removepostdata extends WP_UnitTestCase{
     12
     13        /**
     14         * @dataProvider xmlrpc_getposttitle_data
     15         *
     16         * @covers ::xmlrpc_getposttitle
     17         */
     18        function test_xmlrpc_getposttitle($expected, $value, $text)
     19        {
     20                foreach ($expected as $regx ){
     21
     22                        $this->assertEquals(0, preg_match( $regx, xmlrpc_removepostdata($value), $matchcat), $text .'shouldn\'t exists after the xmlrpc_removepostdata call');
     23                }
     24
     25        }
     26
     27
     28        function xmlrpc_getposttitle_data()
     29        {
     30                $post_default_title = 'post_default_title';
     31                return array(
     32                        array( array('/<title>(.+?)<\/title>/is'), $this->_xml_rpc_request_fixture('<title>good_title</title>' ), 'single title'),
     33//                      array('good_title', '<post><title>good_title</title><cat>fsdfsfsf</cat></post>', 'good title in lot of content'),
     34//                      array('<title>title', '<title> <title>title</title> </title>', 'nested title'),
     35//                      array('<span>title</span>', '<title> <span>title</span> </title>', 'title with spans'),
     36//                      array('<![CDATA["title"]]>', '<title><![CDATA["title"]]></title>', 'pass cdata'),
     37//                      array('<![CDATA["<title>title', '<title><![CDATA["<title>title</title>"]]></title>', 'cdata with tag title'),
     38
     39                        array( array('/<category>(.+?)<\/category>/is'),  $this->_xml_rpc_request_fixture('', '<category>good_category</category>'), 'single category' ),
     40                        array( array('/<category>(.+?)<\/category>/is'),  $this->_xml_rpc_request_fixture('', '<category>good_category1, good_category2, good_category3</category>' ), 'multiple categories'),
     41
     42                        array( array('/<category>(.+?)<\/category>/is','/<title>(.+?)<\/title>/is'),  $this->_xml_rpc_request_fixture('<title>good_title</title>','<category>good_category</category>'), 'single category' ),
     43                        array( array('/<category>(.+?)<\/category>/is', '/<title>(.+?)<\/title>/is'),  $this->_xml_rpc_request_fixture('<title>good_title</title>','<category>good_category1, good_category2, good_category3</category>' ), 'multiple categories'),
     44
     45                );
     46        }
     47
     48        private function _xml_rpc_request_fixture( $title = '', $category = '' )
     49        {
     50                $XML_RPC = "
     51                                <?xml version=\"1.0\"?>
     52                                        <methodResponse>
     53                                          <params>
     54                                                <param>
     55                                                  $title
     56                                                  $category
     57                                                </param>
     58                                          </params>
     59                                        </methodResponse>
     60                ";
     61
     62                return $XML_RPC;
     63        }
     64}