Make WordPress Core

Ticket #38202: 38202.2.diff

File 38202.2.diff, 9.6 KB (added by dshanske, 9 years ago)

Adds some unit tests

  • src/wp-includes/comment.php

    diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php
    index 861ad6b..44a72a9 100644
    a b function do_all_pings() { 
    23082308 * Perform trackbacks.
    23092309 *
    23102310 * @since 1.5.0
     2311 * @since 4.7.0
    23112312 *
    23122313 * @global wpdb $wpdb WordPress database abstraction object.
    23132314 *
    2314  * @param int $post_id Post ID to do trackbacks on.
     2315 * @param int|WP_Post $post Post Object or ID to do trackbacks on.
    23152316 */
    2316 function do_trackbacks($post_id) {
     2317function do_trackbacks( $post ) {
    23172318        global $wpdb;
     2319        $post = get_post( $post );
     2320        if ( ! $post ) {
     2321                return false;
     2322        }
    23182323
    2319         $post = get_post( $post_id );
    2320         $to_ping = get_to_ping($post_id);
    2321         $pinged  = get_pung($post_id);
     2324        $to_ping = get_to_ping( $post );
     2325        $pinged  = get_pung( $post );
    23222326        if ( empty($to_ping) ) {
    2323                 $wpdb->update($wpdb->posts, array('to_ping' => ''), array('ID' => $post_id) );
     2327                $wpdb->update($wpdb->posts, array('to_ping' => ''), array('ID' => $post->ID) );
    23242328                return;
    23252329        }
    23262330
    function do_trackbacks($post_id) { 
    23432347                foreach ( (array) $to_ping as $tb_ping ) {
    23442348                        $tb_ping = trim($tb_ping);
    23452349                        if ( !in_array($tb_ping, $pinged) ) {
    2346                                 trackback($tb_ping, $post_title, $excerpt, $post_id);
     2350                                trackback($tb_ping, $post_title, $excerpt, $post->ID);
    23472351                                $pinged[] = $tb_ping;
    23482352                        } else {
    2349                                 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $tb_ping, $post_id) );
     2353                                $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s,
     2354                                        '')) WHERE ID = %d", $tb_ping, $post->ID) );
    23502355                        }
    23512356                }
    23522357        }
    function generic_ping( $post_id = 0 ) { 
    23782383 *
    23792384 * @since 0.71
    23802385 *
    2381  * @param string $content Post content to check for links.
    2382  * @param int $post_ID Post ID.
     2386 * @param string $content Post content to check for links. If empty will retrieve from post.
     2387 * @param int|WP_Post $post Post Object or ID.
    23832388 */
    2384 function pingback($content, $post_ID) {
     2389function pingback($content, $post) {
    23852390        include_once( ABSPATH . WPINC . '/class-IXR.php' );
    23862391        include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );
    23872392
    23882393        // original code by Mort (http://mort.mine.nu:8080)
    23892394        $post_links = array();
    23902395
    2391         $pung = get_pung($post_ID);
     2396        $post = get_post( $post );
     2397        if ( ! $post ) {
     2398                return false;
     2399        }
     2400
     2401        $pung = get_pung( $post );
     2402
     2403        if ( empty( $content ) ) {
     2404                $content = $post->post_content;
     2405        }
    23922406
    23932407        // Step 1
    23942408        // Parsing the post, external links (if any) are stored in the $post_links array
    function pingback($content, $post_ID) { 
    24042418        // We don't wanna ping first and second types, even if they have a valid <link/>
    24052419
    24062420        foreach ( (array) $post_links_temp as $link_test ) :
    2407                 if ( !in_array($link_test, $pung) && (url_to_postid($link_test) != $post_ID) // If we haven't pung it already and it isn't a link to itself
     2421                if ( !in_array($link_test, $pung) && (url_to_postid($link_test) != $post->ID) // If we haven't pung it already and it isn't a link to itself
    24082422                                && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
    24092423                        if ( $test = @parse_url($link_test) ) {
    24102424                                if ( isset($test['query']) )
    function pingback($content, $post_ID) { 
    24252439         * @param array &$pung       Whether a link has already been pinged, passed by reference.
    24262440         * @param int   $post_ID     The post ID.
    24272441         */
    2428         do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post_ID ) );
     2442        do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post->ID ) );
    24292443
    24302444        foreach ( (array) $post_links as $pagelinkedto ) {
    24312445                $pingback_server_url = discover_pingback_server_uri( $pagelinkedto );
    function pingback($content, $post_ID) { 
    24332447                if ( $pingback_server_url ) {
    24342448                        @ set_time_limit( 60 );
    24352449                        // Now, the RPC call
    2436                         $pagelinkedfrom = get_permalink($post_ID);
     2450                        $pagelinkedfrom = get_permalink( $post );
    24372451
    24382452                        // using a timeout of 3 seconds should be enough to cover slow servers
    24392453                        $client = new WP_HTTP_IXR_Client($pingback_server_url);
    function pingback($content, $post_ID) { 
    24552469                        $client->debug = false;
    24562470
    24572471                        if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
    2458                                 add_ping( $post_ID, $pagelinkedto );
     2472                                add_ping( $post, $pagelinkedto );
    24592473                }
    24602474        }
    24612475}
  • src/wp-includes/post.php

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index c02108e..a19af05 100644
    a b function wp_transition_post_status( $new_status, $old_status, $post ) { 
    39093909 *
    39103910 * @since 1.5.0
    39113911 *
     3912 * @since 4.7.0 Support Post Object and Allow for Array
     3913 *
    39123914 * @global wpdb $wpdb WordPress database abstraction object.
    39133915 *
    3914  * @param int    $post_id Post ID.
    3915  * @param string $uri     Ping URI.
     3916 * @param int|WP_Post    $post Post Object or ID.
     3917 * @param string|array $uri     Ping URI or array of URIs.
    39163918 * @return int|false How many rows were updated.
    39173919 */
    3918 function add_ping( $post_id, $uri ) {
     3920function add_ping( $post, $uri ) {
    39193921        global $wpdb;
    3920         $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id ));
    3921         $pung = trim($pung);
     3922       
     3923        $post = get_post( $post );
     3924        if ( ! $post ) {
     3925                return false;
     3926        }
     3927
     3928        $pung = trim($post->pinged);
    39223929        $pung = preg_split('/\s/', $pung);
    3923         $pung[] = $uri;
     3930        if ( is_array( $uri ) ) {
     3931                $pung = array_merge( $pung, $uri );
     3932        }
     3933        else {
     3934                $pung[] = $uri;
     3935        }
    39243936        $new = implode("\n", $pung);
    39253937
    39263938        /**
    function add_ping( $post_id, $uri ) { 
    39323944         */
    39333945        $new = apply_filters( 'add_ping', $new );
    39343946
    3935         // expected_slashed ($new).
    3936         $new = wp_unslash($new);
    3937         return $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post_id ) );
     3947        $return = $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post->ID ) );
     3948        clean_post_cache( $post->ID );
     3949        return $return;
    39383950}
    39393951
    39403952/**
    function get_enclosed( $post_id ) { 
    39763988 *
    39773989 * @since 1.5.0
    39783990 *
    3979  * @global wpdb $wpdb WordPress database abstraction object.
     3991 * @since 4.7.0 Accept Post Object
    39803992 *
    3981  * @param int $post_id Post ID.
     3993 * @param int|WP_Post $post Post ID or object.
    39823994 * @return array
    39833995 */
    3984 function get_pung( $post_id ) {
    3985         global $wpdb;
    3986         $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id ));
    3987         $pung = trim($pung);
    3988         $pung = preg_split('/\s/', $pung);
     3996function get_pung( $post ) {
     3997        $post = get_post( $post );
     3998        if ( ! $post ) {
     3999                return false;
     4000        }
     4001        $pung = trim( $post->pinged );
     4002        $pung = preg_split( '/\s/', $pung );
    39894003
    39904004        /**
    39914005         * Filters the list of already-pinged URLs for the given post.
    function get_pung( $post_id ) { 
    40024016 *
    40034017 * @since 1.5.0
    40044018 *
    4005  * @global wpdb $wpdb WordPress database abstraction object.
     4019 * @since 4.7.0 Support Post Object.
    40064020 *
    4007  * @param int $post_id Post ID
     4021 * @param int|WP_Post $post Post Object or ID
    40084022 * @return array
    40094023 */
    4010 function get_to_ping( $post_id ) {
    4011         global $wpdb;
    4012         $to_ping = $wpdb->get_var( $wpdb->prepare( "SELECT to_ping FROM $wpdb->posts WHERE ID = %d", $post_id ));
    4013         $to_ping = sanitize_trackback_urls( $to_ping );
     4024function get_to_ping( $post ) {
     4025        $post = get_post( $post );
     4026        if ( ! $post ) {
     4027                return false;
     4028        }
     4029
     4030        $to_ping = sanitize_trackback_urls( $post->to_ping );
    40144031        $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY);
    40154032
    40164033        /**
  • tests/phpunit/tests/post/pings.php

    diff --git a/tests/phpunit/tests/post/pings.php b/tests/phpunit/tests/post/pings.php
    index 3935d15..aa2bd2e 100644
    a b  
    55 * @group pings
    66 */
    77class Tests_Ping_and_Trackback_Sending extends WP_UnitTestCase {
    8         public function test_returns_pinged_sites() {
    9                 $post_id = self::factory()->post->create( array( 'pinged' => 'foo\nbar\nbaz' ) );
     8        public function test_returns_to_ping_sites_from_post_id() {
     9                $post_id = self::factory()->post->create( array( 'to_ping' => 'http://www.example.com
     10                                        http://www.otherexample.com' ) );
     11                $this->assertEqualSets( array( 'http://www.example.com', 'http://www.otherexample.com' ), get_to_ping( $post_id ) );
     12        }
     13        public function test_returns_to_ping_sites_from_post_object() {
     14                $post_id = self::factory()->post->create( array( 'to_ping' => 'http://www.example.com
     15                                        http://www.otherexample.com' ) );
     16                $post = get_post( $post_id );
     17                $this->assertEqualSets( array( 'http://www.example.com', 'http://www.otherexample.com' ), get_to_ping( $post ) );
     18        }
     19
     20        public function test_returns_pinged_sites_from_post_id() {
     21                $post_id = self::factory()->post->create( array( 'pinged' => 'foo bar baz' ) );
     22                $this->assertEqualSets( array( 'foo', 'bar', 'baz' ), get_pung( $post_id ) );
     23        }
     24        public function test_returns_pinged_sites_from_post_object() {
     25                $post_id = self::factory()->post->create( array( 'pinged' => 'foo bar baz' ) );
     26                $post = get_post( $post_id );
     27                $this->assertEqualSets( array( 'foo', 'bar', 'baz' ), get_pung( $post ) );
     28        }
     29        public function test_add_ping_with_post_id() {
     30                $post_id = self::factory()->post->create();
     31                add_ping( $post_id, 'foo' );
     32                add_ping( $post_id, 'bar' );
     33                add_ping( $post_id, 'baz' );
     34                $this->assertEqualSets( array( 'foo', 'bar', 'baz' ), get_pung( $post_id ) );
     35        }
     36
     37        public function test_add_ping_array_with_post_id() {
     38                $post_id = self::factory()->post->create();
     39                add_ping( $post_id, array( 'foo', 'bar', 'baz' ) );
    1040                $this->assertEqualSets( array( 'foo', 'bar', 'baz' ), get_pung( $post_id ) );
    1141        }
     42
     43  public function test_add_ping_with_post_object() {
     44                $post_id = self::factory()->post->create();
     45                $post = get_post( $post_id );
     46                add_ping( $post, 'foo' );
     47                $this->assertEqualSets( array( 'foo' ), get_pung( $post_id ) );
     48        }
     49
     50
     51
    1252}
    1353