Make WordPress Core

Ticket #32243: 32243_3.diff

File 32243_3.diff, 7.7 KB (added by nikonratm, 10 years ago)

Added unit testing

  • src/wp-includes/post.php

     
    53415341 *
    53425342 * @see get_private_posts_cap_sql()
    53435343 *
    5344  * @param string $post_type   Post type.
     5344 * @param array  $post_types  Post type(s).
    53455345 * @param bool   $full        Optional. Returns a full WHERE statement instead of just
    53465346 *                            an 'andalso' term. Default true.
    53475347 * @param int    $post_author Optional. Query posts having a single author ID. Default null.
     
    53495349 *                            $current_user.  Default false.
    53505350 * @return string SQL WHERE code that can be added to a query.
    53515351 */
    5352 function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) {
     5352function get_posts_by_author_sql( $post_types, $full = true, $post_author = null, $public_only = false ) {
    53535353        global $wpdb;
    53545354
    5355         // Private posts.
    5356         $post_type_obj = get_post_type_object( $post_type );
    5357         if ( ! $post_type_obj )
    5358                 return $full ? 'WHERE 1 = 0' : ' 1 = 0 ';
     5355        if ( is_string( $post_types ) ) $post_types = explode(',', $post_types);
    53595356
    5360         /**
    5361          * Filter the capability to read private posts for a custom post type
    5362          * when generating SQL for getting posts by author.
    5363          *
    5364          * @since 2.2.0
    5365          * @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless".
    5366          *
    5367          * @param string $cap Capability.
    5368          */
    5369         if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) ) {
    5370                 $cap = $post_type_obj->cap->read_private_posts;
     5357        $post_types_w_caps = array();
     5358        foreach ($post_types as $post_type) {
     5359                $post_type_obj = get_post_type_object( $post_type );
     5360                if ( ! $post_type_obj )
     5361                        return $full ? 'WHERE 1 = 0' : ' 1 = 0 ';
     5362       
     5363                /**
     5364                 * Filter the capability to read private posts for a custom post type
     5365                 * when generating SQL for getting posts by author.
     5366                 *
     5367                 * @since 2.2.0
     5368                 * @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless".
     5369                 *
     5370                 * @param string $cap Capability.
     5371                 */
     5372                if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) ) $cap = current_user_can( $post_type_obj->cap->read_private_posts );
     5373
     5374                // Only need to check the cap if $public_only is false.
     5375                $post_status_sql = "post_status = 'publish'";
     5376                if ( false === $public_only ) {
     5377                        if ( $cap ) {
     5378                                // Does the user have the capability to view private posts? Guess so.
     5379                                $post_status_sql .= " OR post_status = 'private'";
     5380                        } elseif ( is_user_logged_in() ) {
     5381                                // Users can view their own private posts.
     5382                                $id = get_current_user_id();
     5383                                if ( null === $post_author || ! $full ) {
     5384                                        $post_status_sql .= " OR post_status = 'private' AND post_author = $id";
     5385                                } elseif ( $id == (int) $post_author ) {
     5386                                        $post_status_sql .= " OR post_status = 'private'";
     5387                                } // else none
     5388                        } // else none
     5389                }
     5390               
     5391                $post_types_w_caps[] = "( post_type = '" . $post_type . "' AND ( $post_status_sql ) )";
    53715392        }
    53725393
    5373         $sql = $wpdb->prepare( 'post_type = %s', $post_type );
    5374 
     5394//      $post_type_sql = array_fill(0, count($post_types), '%s');
     5395        $sql = '( '.implode(' OR ', $post_types_w_caps).' )';
     5396//      $sql = $wpdb->prepare($where, $post_types);
     5397       
    53755398        if ( null !== $post_author ) {
    53765399                $sql .= $wpdb->prepare( ' AND post_author = %d', $post_author );
    53775400        }
    53785401
    5379         // Only need to check the cap if $public_only is false.
    5380         $post_status_sql = "post_status = 'publish'";
    5381         if ( false === $public_only ) {
    5382                 if ( current_user_can( $cap ) ) {
    5383                         // Does the user have the capability to view private posts? Guess so.
    5384                         $post_status_sql .= " OR post_status = 'private'";
    5385                 } elseif ( is_user_logged_in() ) {
    5386                         // Users can view their own private posts.
    5387                         $id = get_current_user_id();
    5388                         if ( null === $post_author || ! $full ) {
    5389                                 $post_status_sql .= " OR post_status = 'private' AND post_author = $id";
    5390                         } elseif ( $id == (int) $post_author ) {
    5391                                 $post_status_sql .= " OR post_status = 'private'";
    5392                         } // else none
    5393                 } // else none
    5394         }
     5402//      $sql .= " AND ($post_status_sql)";
    53955403
    5396         $sql .= " AND ($post_status_sql)";
    5397 
    53985404        if ( $full ) {
    53995405                $sql = 'WHERE ' . $sql;
    54005406        }
  • src/wp-includes/user.php

     
    254254 *
    255255 * @global wpdb $wpdb WordPress database object for queries.
    256256 *
    257  * @param int    $userid    User ID.
    258  * @param string $post_type Optional. Post type to count the number of posts for. Default 'post'.
     257 * @param int           $userid User ID.
     258 * @param array/string  $post_types Optional. Post type(s) to count the number of posts for. Default 'post'.
    259259 * @return int Number of posts the user has written in this post type.
    260260 */
    261 function count_user_posts( $userid, $post_type = 'post' ) {
     261function count_user_posts( $userid, $post_types = 'post' ) {
    262262        global $wpdb;
     263       
     264        if ( is_string( $post_types ) ) $post_types = explode(',', $post_types);
    263265
    264         $where = get_posts_by_author_sql( $post_type, true, $userid );
     266        $where = get_posts_by_author_sql( $post_types, true, $userid );
    265267
    266268        $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
    267269
     
    273275         *
    274276         * @param int    $count     The user's post count.
    275277         * @param int    $userid    User ID.
    276          * @param string $post_type Post type to count the number of posts for.
     278         * @param array  $post_types Post types to count the number of posts for.
    277279         */
    278         return apply_filters( 'get_usernumposts', $count, $userid, $post_type );
     280        return apply_filters( 'get_usernumposts', $count, $userid, $post_types );
    279281}
    280282
    281283/**
  • tests/phpunit/tests/post/getPostsByAuthorSql.php

     
    2020                $this->assertContains( '1 = 0', $maybe_string );
    2121        }
    2222
     23        public function test_multiple_post_types(){
     24                register_post_type( 'foo' );
     25                register_post_type( 'bar' );
     26
     27                $maybe_string = get_posts_by_author_sql( 'foo,bar' );
     28                $this->assertContains( "post_type = 'foo'", $maybe_string );
     29                $this->assertContains( "post_type = 'bar'", $maybe_string );
     30
     31                _unregister_post_type( 'foo' );
     32                _unregister_post_type( 'bar' );
     33        }
     34
    2335        public function test_full_true(){
    2436                $maybe_string = get_posts_by_author_sql( 'post', true );
    2537                $this->assertRegExp( '/^WHERE /', $maybe_string );
     
    112124
    113125                wp_set_current_user( $current_user );
    114126        }
     127
     128        public function test_user_has_access_only_to_private_posts_for_certain_post_types(){
     129                register_post_type( 'foo', array( 'capabilities' => array( 'read_private_posts' => 'read_private_foo' ) )  );
     130                register_post_type( 'bar', array( 'capabilities' => array( 'read_private_posts' => 'read_private_bar' ) ) );
     131                register_post_type( 'baz', array( 'capabilities' => array( 'read_private_posts' => 'read_private_baz' ) ) );
     132                $current_user = get_current_user_id();
     133                $u = $this->factory->user->create( array( 'role' => 'editor' ) );
     134                $editor_role = get_role('editor');
     135                $editor_role->add_cap( 'read_private_baz' );
     136                wp_set_current_user( $u );
     137
     138                $maybe_string = get_posts_by_author_sql( 'foo,bar,baz' );
     139                $this->assertNotContains( "post_type = 'foo' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );
     140                $this->assertNotContains( "post_type = 'bar' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );
     141                $this->assertContains( "post_type = 'baz' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );
     142
     143                _unregister_post_type( 'foo' );
     144                _unregister_post_type( 'bar' );
     145                _unregister_post_type( 'baz' );
     146                wp_set_current_user( $current_user );
     147        }
    115148}