Make WordPress Core

Ticket #37096: xmlrpc_getpost.patch

File xmlrpc_getpost.patch, 9.1 KB (added by pbearne, 9 years ago)

functions.php and unit tests

  • src/wp-includes/functions.php

  • tests/phpunit/tests/functions/xmlrpc-getpostcatergory.php

    @@ -458,7 +473,7 @@
     function xmlrpc_getposttitle( $content ) {
     	global $post_default_title;
     	if ( preg_match( '/<title>(.+?)<\/title>/is', $content, $matchtitle ) ) {
    -		$post_title = $matchtitle[1];
    +		$post_title = trim( $matchtitle[1] );
     	} else {
     		$post_title = $post_default_title;
     	}
    @@ -483,7 +498,7 @@
     	global $post_default_category;
     	if ( preg_match( '/<category>(.+?)<\/category>/is', $content, $matchcat ) ) {
     		$post_category = trim( $matchcat[1], ',' );
    -		$post_category = explode( ',', $post_category );
    +		$post_category = array_unique( array_map('trim', explode( ',', $post_category ) ) );
     	} else {
     		$post_category = $post_default_category;
     	}
     
     1<?php
     2
     3/**
     4 * Class Tests_Xmlrpc_Getpostcategory
     5 * test the xmlrpc_getpostcategory function
     6 */
     7class Tests_Xmlrpc_GetPostCategory extends WP_UnitTestCase {
     8
     9        /**
     10         * setup
     11         * declare the default category needed in function
     12         */
     13        function setup() {
     14                global $post_default_category;
     15                $post_default_category = 'post_default_category';
     16        }
     17
     18        /**
     19         * remove global
     20         */
     21        function tearDown() {
     22                global $post_default_category;
     23                $post_default_category = null;
     24        }
     25
     26
     27        /**
     28         * pass null
     29         */
     30        function test_xmlrpc_getpostcategory_null() {
     31                $this->assertEquals( 'post_default_category' , xmlrpc_getpostcategory( null ) );
     32        }
     33
     34        /**
     35         * pass good category in test
     36         */
     37        function test_xmlrpc_getpostcategory_good() {
     38
     39                $this->assertEquals( array( 'good_category' ) , xmlrpc_getpostcategory( '<category>good_category</category>' ) );
     40        }
     41
     42
     43        /**
     44         * pass good category in test
     45         */
     46        function test_xmlrpc_getpostcategorys_good() {
     47
     48                $this->assertEquals( array( 'good_category1', 'good_category2', 'good_category3' ), xmlrpc_getpostcategory( '<category>good_category1, good_category2, good_category3</category>' ) );
     49        }
     50
     51        /**
     52         * pass good category in test
     53         */
     54        function test_xmlrpc_getpostcategorys_dupicate_cat() {
     55
     56                $this->assertEquals( array( 0 => 'good_category1', 2 => 'good_category3' ), xmlrpc_getpostcategory( '<category>good_category1, good_category1, good_category3,</category>' ) );
     57        }
     58
     59        /**
     60         * pass good category in lot of content
     61         */
     62        function test_xmlrpc_getpostcategory_good_in_post_tag() {
     63
     64                $this->assertEquals( array( 'good_category' ) , xmlrpc_getpostcategory( '<post><category>good_category</category><cat>fsdfsfsf</cat></post>' ) );
     65        }
     66        /**
     67         * missing close
     68         */
     69        function test_xmlrpc_getpostcategory_bad() {
     70
     71                $this->assertEquals( 'post_default_category', xmlrpc_getpostcategory( '<category>good_category<category>ssssss' ) );
     72        }
     73
     74        /**
     75         * category has to be something or default is returned
     76         */
     77        function test_xmlrpc_getpostcategory_missing() {
     78
     79                $this->assertEquals( 'post_default_category' , xmlrpc_getpostcategory( '<category></category>' ) );
     80        }
     81
     82        /**
     83         * return space
     84         */
     85        function test_xmlrpc_getpostcategory_space() {
     86
     87                $this->assertEquals( array( '' ) , xmlrpc_getpostcategory( '<category> </category>' ) );
     88        }
     89
     90        /**
     91         * nest categorys tern bad string
     92         */
     93        function test_xmlrpc_getpostcategory_category() {
     94
     95                $this->assertEquals( array( '<category>category' ), xmlrpc_getpostcategory( '<category> <category>category</category> </category>' ) );
     96
     97        }
     98
     99        /**
     100         * inluded any tag in the category
     101         */
     102        function test_xmlrpc_getpostcategory_span() {
     103
     104                $this->assertEquals( array( '<span>category</span>' ), xmlrpc_getpostcategory( '<category> <span>category</span> </category>' ) );
     105        }
     106
     107
     108        /**
     109         * bad opening tag
     110         */
     111        function test_xmlrpc_getpostcategory_bad_open_tag() {
     112
     113                $this->assertEquals( 'post_default_category' , xmlrpc_getpostcategory( '<category <span>category</span> </category>' ) );
     114        }
     115
     116        /**
     117         * bad closing tag
     118         */
     119        function test_xmlrpc_getpostcategory_bad_close_tag() {
     120
     121                $this->assertEquals( 'post_default_category' , xmlrpc_getpostcategory( '<category>category</category dddddd>' ) );
     122        }
     123
     124        /**
     125         * dosen't find category if attribute set
     126         */
     127        function test_xmlrpc_getpostcategory_attribute_in_tag() {
     128
     129                $this->assertEquals( 'post_default_category' , xmlrpc_getpostcategory( '<category id="id">category</category>' ) );
     130        }
     131
     132        /**
     133         * find just the frist category
     134         */
     135        function test_xmlrpc_getpostcategory_2_categorys() {
     136
     137                $this->assertEquals( array( 'category1' ) , xmlrpc_getpostcategory( '<category>category1</category><category>category2</category>' ) );
     138        }
     139
     140        /**
     141         * pass cdata
     142         */
     143        function test_xmlrpc_getpostcategory_cdata() {
     144
     145                $this->assertEquals( array( '<![CDATA["category"]]>' ), xmlrpc_getpostcategory( '<category><![CDATA["category"]]></category>' ) );
     146        }
     147
     148        /**
     149         * pass cdata with tag category
     150         */
     151        function test_xmlrpc_getpostcategory_bad_cdate() {
     152
     153                $this->assertEquals( array( '<![CDATA["<category>category' ) , xmlrpc_getpostcategory( '<category><![CDATA["<category>category</category>"]]></category>' ) );
     154        }
     155}
  • tests/phpunit/tests/functions/xmlrpc-getposttitle.php

     
     1<?php
     2
     3/**
     4 * Class Tests_Xmlrpc_Getposttitle
     5 * test the xmlrpc_getposttitle function
     6 */
     7class Tests_Xmlrpc_Getposttitle extends WP_UnitTestCase {
     8
     9        /**
     10         * setup
     11         * declare the default title needed in function
     12         */
     13        function setup() {
     14                global $post_default_title;
     15                $post_default_title = 'post_default_title';
     16        }
     17
     18        /**
     19         * remove global
     20         */
     21        function tearDown() {
     22                global $post_default_title;
     23                $post_default_title = null;
     24        }
     25
     26
     27        /**
     28         * pass null
     29         */
     30        function test_xmlrpc_getposttitle_null() {
     31                $this->assertEquals( 'post_default_title' , xmlrpc_getposttitle( null ) );
     32        }
     33
     34        /**
     35         * pass good title in test
     36         */
     37        function test_xmlrpc_getposttitle_good() {
     38
     39                $this->assertEquals( 'good_title' , xmlrpc_getposttitle( '<title>good_title</title>' ) );
     40        }
     41        /**
     42         * pass good title in lot of content
     43         */
     44        function test_xmlrpc_getposttitle_good_in_post_tag() {
     45
     46                $this->assertEquals( 'good_title' , xmlrpc_getposttitle( '<post><title>good_title</title><cat>fsdfsfsf</cat></post>' ) );
     47        }
     48        /**
     49         * missing close
     50         */
     51        function test_xmlrpc_getposttitle_bad() {
     52
     53                $this->assertEquals( 'post_default_title', xmlrpc_getposttitle( '<title>good_title<title>ssssss' ) );
     54        }
     55
     56        /**
     57         * title has to be something or default is returned
     58         */
     59        function test_xmlrpc_getposttitle_missing() {
     60
     61                $this->assertEquals( 'post_default_title' , xmlrpc_getposttitle( '<title></title>' ) );
     62        }
     63
     64        /**
     65         * return space
     66         */
     67        function test_xmlrpc_getposttitle_space() {
     68
     69                $this->assertEquals( '' , xmlrpc_getposttitle( '<title> </title>' ) );
     70        }
     71
     72        /**
     73         * nest titles tern bad string
     74         */
     75        function test_xmlrpc_getposttitle_title() {
     76
     77                $this->assertEquals( ' <title>title' , xmlrpc_getposttitle( '<title> <title>title</title> </title>' ) );
     78
     79        }
     80
     81        /**
     82         * inluded any tag in the title
     83         */
     84        function test_xmlrpc_getposttitle_span() {
     85
     86                $this->assertEquals( ' <span>title</span> ' , xmlrpc_getposttitle( '<title> <span>title</span> </title>' ) );
     87        }
     88
     89
     90        /**
     91         * bad opening tag
     92         */
     93        function test_xmlrpc_getposttitle_bad_open_tag() {
     94
     95                $this->assertEquals( 'post_default_title' , xmlrpc_getposttitle( '<title <span>title</span> </title>' ) );
     96        }
     97
     98        /**
     99         * bad closing tag
     100         */
     101        function test_xmlrpc_getposttitle_bad_close_tag() {
     102
     103                $this->assertEquals( 'post_default_title' , xmlrpc_getposttitle( '<title>title</title dddddd>' ) );
     104        }
     105
     106
     107        /**
     108         * dosen't find title if attribute set
     109         */
     110        function test_xmlrpc_getposttitle_attribute_in_tag() {
     111
     112                $this->assertEquals( 'post_default_title' , xmlrpc_getposttitle( '<title id="id">title</title>' ) );
     113        }
     114
     115        /**
     116         * find just the frist title
     117         */
     118        function test_xmlrpc_getposttitle_2_titles() {
     119
     120                $this->assertEquals( 'title1' , xmlrpc_getposttitle( '<title>title1</title><title>title2</title>' ) );
     121        }
     122
     123        /**
     124         * pass cdata
     125         */
     126        function test_xmlrpc_getposttitle_cdata() {
     127
     128                $this->assertEquals( '<![CDATA["title"]]>' , xmlrpc_getposttitle( '<title><![CDATA["title"]]></title>' ) );
     129        }
     130
     131        /**
     132         * pass cdata with tag title
     133         */
     134        function test_xmlrpc_getposttitle_bad_cdate() {
     135
     136                $this->assertEquals( '<![CDATA["<title>title' , xmlrpc_getposttitle( '<title><![CDATA["<title>title</title>"]]></title>' ) );
     137        }
     138}