Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (9 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/query.php

    r40278 r42343  
    1212                $q = new WP_Query();
    1313
    14                 $term_id = self::factory()->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
    15                 $term_id2 = self::factory()->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) );
    16                 $post_id = self::factory()->post->create();
     14                $term_id  = self::factory()->category->create(
     15                        array(
     16                                'slug' => 'woo',
     17                                'name' => 'WOO!',
     18                        )
     19                );
     20                $term_id2 = self::factory()->category->create(
     21                        array(
     22                                'slug' => 'hoo',
     23                                'name' => 'HOO!',
     24                        )
     25                );
     26                $post_id  = self::factory()->post->create();
    1727
    1828                wp_set_post_categories( $post_id, $term_id );
     
    4252         */
    4353        function test_empty_category__in() {
    44                 $cat_id = self::factory()->category->create();
     54                $cat_id  = self::factory()->category->create();
    4555                $post_id = self::factory()->post->create();
    4656                wp_set_post_categories( $post_id, $cat_id );
     
    5161                $this->assertNotEmpty( $q2 );
    5262
    53                 $tag = wp_insert_term( 'woo', 'post_tag' );
     63                $tag    = wp_insert_term( 'woo', 'post_tag' );
    5464                $tag_id = $tag['term_id'];
    55                 $slug = get_tag( $tag_id )->slug;
     65                $slug   = get_tag( $tag_id )->slug;
    5666                wp_set_post_tags( $post_id, $slug );
    5767
     
    7383                // Create posts and clear their caches.
    7484                $post_ids = self::factory()->post->create_many( 4 );
    75                 foreach ( $post_ids as $post_id )
     85                foreach ( $post_ids as $post_id ) {
    7686                        clean_post_cache( $post_id );
     87                }
    7788
    7889                add_filter( 'the_posts', array( $this, 'the_posts_filter' ) );
    7990
    80                 $query = new WP_Query( array(
    81                         'post_type' => 'post',
    82                         'posts_per_page' => 3,
    83                 ) );
     91                $query = new WP_Query(
     92                        array(
     93                                'post_type'      => 'post',
     94                                'posts_per_page' => 3,
     95                        )
     96                );
    8497
    8598                // Fourth post added in filter
     
    109122
    110123                // Add some custom data to each post.
    111                 foreach ( $posts as $key => $post )
     124                foreach ( $posts as $key => $post ) {
    112125                        $posts[ $key ]->custom_data = array( $post->ID, 'custom data' );
     126                }
    113127
    114128                return $posts;
     
    116130
    117131        function test_post__in_ordering() {
    118                 $post_id1 = self::factory()->post->create( array( 'post_type' => 'page', 'menu_order' => rand( 1, 100 ) ) );
    119                 $post_id2 = self::factory()->post->create( array( 'post_type' => 'page', 'menu_order' => rand( 1, 100 ) ) );
    120                 $post_id3 = self::factory()->post->create( array(
    121                         'post_type' => 'page',
    122                         'post_parent' => $post_id2,
    123                         'menu_order' => rand( 1, 100 )
    124                 ) );
    125                 $post_id4 = self::factory()->post->create( array(
    126                         'post_type' => 'page',
    127                         'post_parent' => $post_id2,
    128                         'menu_order' => rand( 1, 100 )
    129                 ) );
    130                 $post_id5 = self::factory()->post->create( array( 'post_type' => 'page', 'menu_order' => rand( 1, 100 ) ) );
     132                $post_id1 = self::factory()->post->create(
     133                        array(
     134                                'post_type'  => 'page',
     135                                'menu_order' => rand( 1, 100 ),
     136                        )
     137                );
     138                $post_id2 = self::factory()->post->create(
     139                        array(
     140                                'post_type'  => 'page',
     141                                'menu_order' => rand( 1, 100 ),
     142                        )
     143                );
     144                $post_id3 = self::factory()->post->create(
     145                        array(
     146                                'post_type'   => 'page',
     147                                'post_parent' => $post_id2,
     148                                'menu_order'  => rand( 1, 100 ),
     149                        )
     150                );
     151                $post_id4 = self::factory()->post->create(
     152                        array(
     153                                'post_type'   => 'page',
     154                                'post_parent' => $post_id2,
     155                                'menu_order'  => rand( 1, 100 ),
     156                        )
     157                );
     158                $post_id5 = self::factory()->post->create(
     159                        array(
     160                                'post_type'  => 'page',
     161                                'menu_order' => rand( 1, 100 ),
     162                        )
     163                );
    131164
    132165                $ordered = array( $post_id2, $post_id4, $post_id3, $post_id1, $post_id5 );
    133166
    134                 $q = new WP_Query( array(
    135                         'post_type' => 'any',
    136                         'post__in' => $ordered,
    137                         'orderby' => 'post__in'
    138                 ) );
     167                $q = new WP_Query(
     168                        array(
     169                                'post_type' => 'any',
     170                                'post__in'  => $ordered,
     171                                'orderby'   => 'post__in',
     172                        )
     173                );
    139174                $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) );
    140175        }
    141176
    142177        function test_post__in_attachment_ordering() {
    143                 $post_id = self::factory()->post->create();
    144                 $att_ids = array();
    145                 $file = DIR_TESTDATA . '/images/canola.jpg';
    146                 $att_ids[1] = self::factory()->attachment->create_object( $file, $post_id, array(
    147                         'post_mime_type' => 'image/jpeg',
    148                         'menu_order' => rand( 1, 100 )
    149                 ) );
    150                 $att_ids[2] = self::factory()->attachment->create_object( $file, $post_id, array(
    151                         'post_mime_type' => 'image/jpeg',
    152                         'menu_order' => rand( 1, 100 )
    153                 ) );
    154                 $att_ids[3] = self::factory()->attachment->create_object( $file, $post_id, array(
    155                         'post_mime_type' => 'image/jpeg',
    156                         'menu_order' => rand( 1, 100 )
    157                 ) );
    158                 $att_ids[4] = self::factory()->attachment->create_object( $file, $post_id, array(
    159                         'post_mime_type' => 'image/jpeg',
    160                         'menu_order' => rand( 1, 100 )
    161                 ) );
    162                 $att_ids[5] = self::factory()->attachment->create_object( $file, $post_id, array(
    163                         'post_mime_type' => 'image/jpeg',
    164                         'menu_order' => rand( 1, 100 )
    165                 ) );
     178                $post_id    = self::factory()->post->create();
     179                $att_ids    = array();
     180                $file       = DIR_TESTDATA . '/images/canola.jpg';
     181                $att_ids[1] = self::factory()->attachment->create_object(
     182                        $file, $post_id, array(
     183                                'post_mime_type' => 'image/jpeg',
     184                                'menu_order'     => rand( 1, 100 ),
     185                        )
     186                );
     187                $att_ids[2] = self::factory()->attachment->create_object(
     188                        $file, $post_id, array(
     189                                'post_mime_type' => 'image/jpeg',
     190                                'menu_order'     => rand( 1, 100 ),
     191                        )
     192                );
     193                $att_ids[3] = self::factory()->attachment->create_object(
     194                        $file, $post_id, array(
     195                                'post_mime_type' => 'image/jpeg',
     196                                'menu_order'     => rand( 1, 100 ),
     197                        )
     198                );
     199                $att_ids[4] = self::factory()->attachment->create_object(
     200                        $file, $post_id, array(
     201                                'post_mime_type' => 'image/jpeg',
     202                                'menu_order'     => rand( 1, 100 ),
     203                        )
     204                );
     205                $att_ids[5] = self::factory()->attachment->create_object(
     206                        $file, $post_id, array(
     207                                'post_mime_type' => 'image/jpeg',
     208                                'menu_order'     => rand( 1, 100 ),
     209                        )
     210                );
    166211
    167212                $ordered = array( $att_ids[5], $att_ids[1], $att_ids[4], $att_ids[3], $att_ids[2] );
    168213
    169                 $attached = new WP_Query( array(
    170                         'post__in' => $ordered,
    171                         'post_type' => 'attachment',
    172                         'post_parent' => $post_id,
    173                         'post_mime_type' => 'image',
    174                         'post_status' => 'inherit',
    175                         'posts_per_page' => '-1',
    176                         'orderby' => 'post__in'
    177                 ) );
     214                $attached = new WP_Query(
     215                        array(
     216                                'post__in'       => $ordered,
     217                                'post_type'      => 'attachment',
     218                                'post_parent'    => $post_id,
     219                                'post_mime_type' => 'image',
     220                                'post_status'    => 'inherit',
     221                                'posts_per_page' => '-1',
     222                                'orderby'        => 'post__in',
     223                        )
     224                );
    178225                $this->assertSame( $ordered, wp_list_pluck( $attached->posts, 'ID' ) );
    179226        }
     
    183230         */
    184231        public function test_post_name__in_ordering() {
    185                 $post_id1 = self::factory()->post->create( array( 'post_name' => 'id-1', 'post_type' => 'page' ) );
    186                 $post_id2 = self::factory()->post->create( array( 'post_name' => 'id-2', 'post_type' => 'page' ) );
    187                 $post_id3 = self::factory()->post->create( array(
    188                         'post_name' => 'id-3',
    189                         'post_type' => 'page',
    190                         'post_parent' => $post_id2
    191                 ) );
     232                $post_id1 = self::factory()->post->create(
     233                        array(
     234                                'post_name' => 'id-1',
     235                                'post_type' => 'page',
     236                        )
     237                );
     238                $post_id2 = self::factory()->post->create(
     239                        array(
     240                                'post_name' => 'id-2',
     241                                'post_type' => 'page',
     242                        )
     243                );
     244                $post_id3 = self::factory()->post->create(
     245                        array(
     246                                'post_name'   => 'id-3',
     247                                'post_type'   => 'page',
     248                                'post_parent' => $post_id2,
     249                        )
     250                );
    192251
    193252                $ordered = array( 'id-2', 'id-3', 'id-1' );
    194253
    195                 $q = new WP_Query( array(
    196                         'post_type' => 'any',
    197                         'post_name__in' => $ordered,
    198                         'orderby' => 'post_name__in'
    199                 ) );
     254                $q = new WP_Query(
     255                        array(
     256                                'post_type'     => 'any',
     257                                'post_name__in' => $ordered,
     258                                'orderby'       => 'post_name__in',
     259                        )
     260                );
    200261
    201262                $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'post_name' ) );
     
    223284
    224285        /**
    225          *
    226286         * @ticket 17065
    227287         */
     
    229289                global $wpdb;
    230290
    231                 $q1 = new WP_Query( array(
    232                         'orderby' => array(
    233                                 'type' => 'DESC',
    234                                 'name' => 'ASC'
    235                         )
    236                 ) );
     291                $q1 = new WP_Query(
     292                        array(
     293                                'orderby' => array(
     294                                        'type' => 'DESC',
     295                                        'name' => 'ASC',
     296                                ),
     297                        )
     298                );
    237299                $this->assertContains(
    238300                        "ORDER BY $wpdb->posts.post_type DESC, $wpdb->posts.post_name ASC",
     
    258320
    259321        /**
    260          *
    261322         * @ticket 17065
    262323         */
     
    264325                global $wpdb;
    265326
    266                 $q1 = new WP_Query( array(
    267                         'orderby' => array(
    268                                 'post_type' => 'foo'
    269                         )
    270                 ) );
     327                $q1 = new WP_Query(
     328                        array(
     329                                'orderby' => array(
     330                                        'post_type' => 'foo',
     331                                ),
     332                        )
     333                );
    271334                $this->assertContains(
    272335                        "ORDER BY $wpdb->posts.post_type DESC",
     
    274337                );
    275338
    276                 $q2 = new WP_Query( array(
    277                         'orderby' => 'title',
    278                         'order'   => 'foo'
    279                 ) );
     339                $q2 = new WP_Query(
     340                        array(
     341                                'orderby' => 'title',
     342                                'order'   => 'foo',
     343                        )
     344                );
    280345                $this->assertContains(
    281346                        "ORDER BY $wpdb->posts.post_title DESC",
     
    283348                );
    284349
    285                 $q3 = new WP_Query( array(
    286                         'order' => 'asc'
    287                 ) );
     350                $q3 = new WP_Query(
     351                        array(
     352                                'order' => 'asc',
     353                        )
     354                );
    288355                $this->assertContains(
    289356                        "ORDER BY $wpdb->posts.post_date ASC",
     
    331398         */
    332399        public function test_orderby_rand_with_seed() {
    333                 $q = new WP_Query( array(
    334                         'orderby' => 'RAND(5)',
    335                 ) );
     400                $q = new WP_Query(
     401                        array(
     402                                'orderby' => 'RAND(5)',
     403                        )
     404                );
    336405
    337406                $this->assertContains( 'ORDER BY RAND(5)', $q->request );
     
    342411         */
    343412        public function test_orderby_rand_should_ignore_invalid_seed() {
    344                 $q = new WP_Query( array(
    345                         'orderby' => 'RAND(foo)',
    346                 ) );
     413                $q = new WP_Query(
     414                        array(
     415                                'orderby' => 'RAND(foo)',
     416                        )
     417                );
    347418
    348419                $this->assertNotContains( 'ORDER BY RAND', $q->request );
     
    353424         */
    354425        public function test_orderby_rand_with_seed_should_be_case_insensitive() {
    355                 $q = new WP_Query( array(
    356                         'orderby' => 'rand(5)',
    357                 ) );
     426                $q = new WP_Query(
     427                        array(
     428                                'orderby' => 'rand(5)',
     429                        )
     430                );
    358431
    359432                $this->assertContains( 'ORDER BY RAND(5)', $q->request );
     
    368441                $q = new WP_Query();
    369442
    370                 $post_ids[0] = self::factory()->post->create( array( 'post_title' => 'woo', 'post_date' => '2015-07-23 00:00:00' ) );
    371                 $post_ids[1] = self::factory()->post->create( array( 'post_title' => 'hoo', 'post_date' => '2015-07-23 00:00:00' ) );
    372                 $post_ids[2] = self::factory()->post->create( array( 'post_title' => 'test', 'post_date' => '2015-07-23 00:00:00' ) );
    373                 $post_ids[3] = self::factory()->post->create( array( 'post_title' => 'me', 'post_date' => '2015-07-23 00:00:00' ) );
     443                $post_ids[0] = self::factory()->post->create(
     444                        array(
     445                                'post_title' => 'woo',
     446                                'post_date'  => '2015-07-23 00:00:00',
     447                        )
     448                );
     449                $post_ids[1] = self::factory()->post->create(
     450                        array(
     451                                'post_title' => 'hoo',
     452                                'post_date'  => '2015-07-23 00:00:00',
     453                        )
     454                );
     455                $post_ids[2] = self::factory()->post->create(
     456                        array(
     457                                'post_title' => 'test',
     458                                'post_date'  => '2015-07-23 00:00:00',
     459                        )
     460                );
     461                $post_ids[3] = self::factory()->post->create(
     462                        array(
     463                                'post_title' => 'me',
     464                                'post_date'  => '2015-07-23 00:00:00',
     465                        )
     466                );
    374467
    375468                $requested = array( $post_ids[0], $post_ids[3] );
    376                 $q->query( array(
    377                         'post_name__in' => array( 'woo', 'me' ),
    378                         'fields' => 'ids',
    379                 ) );
     469                $q->query(
     470                        array(
     471                                'post_name__in' => array( 'woo', 'me' ),
     472                                'fields'        => 'ids',
     473                        )
     474                );
    380475                $actual_posts = $q->get_posts();
    381476                $this->assertEqualSets( $requested, $actual_posts );
    382477
    383478                $requested = array( $post_ids[1], $post_ids[2] );
    384                 $q->query( array(
    385                         'post_name__in' => array( 'hoo', 'test' ),
    386                         'fields' => 'ids',
    387                 ) );
     479                $q->query(
     480                        array(
     481                                'post_name__in' => array( 'hoo', 'test' ),
     482                                'fields'        => 'ids',
     483                        )
     484                );
    388485                $actual_posts = $q->get_posts();
    389486                $this->assertEqualSets( $requested, $actual_posts );
     
    399496
    400497                $num_queries = $wpdb->num_queries;
    401                 $q = new WP_Query( array(
    402                         'fields' => 'ids',
    403                         'no_found_rows' => true,
    404                 ) );
     498                $q           = new WP_Query(
     499                        array(
     500                                'fields'        => 'ids',
     501                                'no_found_rows' => true,
     502                        )
     503                );
    405504
    406505                remove_filter( 'posts_pre_query', array( __CLASS__, 'filter_posts_pre_query' ) );
     
    461560                }
    462561
    463                 $q = new WP_Query( array(
    464                         'post_type' => 'wptests_pt',
    465                         'posts_per_page' => 1,
    466                         'fields' => 'ids',
    467                 ) );
     562                $q = new WP_Query(
     563                        array(
     564                                'post_type'      => 'wptests_pt',
     565                                'posts_per_page' => 1,
     566                                'fields'         => 'ids',
     567                        )
     568                );
    468569
    469570                $this->assertEquals( 2, $q->found_posts );
     
    482583                }
    483584
    484                 $q = new WP_Query( array(
    485                         'post_type' => 'wptests_pt',
    486                         'posts_per_page' => 1,
    487                         'fields' => 'id=>parent',
    488                 ) );
     585                $q = new WP_Query(
     586                        array(
     587                                'post_type'      => 'wptests_pt',
     588                                'posts_per_page' => 1,
     589                                'fields'         => 'id=>parent',
     590                        )
     591                );
    489592
    490593                $this->assertEquals( 2, $q->found_posts );
     
    504607
    505608                add_filter( 'split_the_query', '__return_true' );
    506                 $q = new WP_Query( array(
    507                         'post_type' => 'wptests_pt',
    508                         'posts_per_page' => 1,
    509                 ) );
     609                $q = new WP_Query(
     610                        array(
     611                                'post_type'      => 'wptests_pt',
     612                                'posts_per_page' => 1,
     613                        )
     614                );
    510615                remove_filter( 'split_the_query', '__return_true' );
    511616
     
    527632                // ! $split_the_query
    528633                add_filter( 'split_the_query', '__return_false' );
    529                 $q = new WP_Query( array(
    530                         'post_type' => 'wptests_pt',
    531                         'posts_per_page' => 1,
    532                 ) );
     634                $q = new WP_Query(
     635                        array(
     636                                'post_type'      => 'wptests_pt',
     637                                'posts_per_page' => 1,
     638                        )
     639                );
    533640                remove_filter( 'split_the_query', '__return_false' );
    534641
Note: See TracChangeset for help on using the changeset viewer.