Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (8 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/getPages.php

    r41849 r42343  
    2323        $this->assertEquals( 3, count( $pages ) );
    2424        $this->assertNotEmpty( $time1 = wp_cache_get( 'last_changed', 'posts' ) );
    25         $num_queries = $wpdb->num_queries;
    26         foreach ( $pages as $page )
     25        $num_queries                  = $wpdb->num_queries;
     26        foreach ( $pages as $page ) {
    2727            $this->assertInstanceOf( 'WP_Post', $page );
     28        }
    2829
    2930        // Again. num_queries and last_changed should remain the same.
     
    3233        $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
    3334        $this->assertEquals( $num_queries, $wpdb->num_queries );
    34         foreach ( $pages as $page )
     35        foreach ( $pages as $page ) {
    3536            $this->assertInstanceOf( 'WP_Post', $page );
     37        }
    3638
    3739        // Again with different args. last_changed should not increment because of
     
    4143        $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
    4244        $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
    43         foreach ( $pages as $page )
     45        foreach ( $pages as $page ) {
    4446            $this->assertInstanceOf( 'WP_Post', $page );
     47        }
    4548
    4649        $num_queries = $wpdb->num_queries;
     
    5154        $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
    5255        $this->assertEquals( $num_queries, $wpdb->num_queries );
    53         foreach ( $pages as $page )
     56        foreach ( $pages as $page ) {
    5457            $this->assertInstanceOf( 'WP_Post', $page );
     58        }
    5559
    5660        // Do the first query again. The interim queries should not affect it.
     
    5963        $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
    6064        $this->assertEquals( $num_queries, $wpdb->num_queries );
    61         foreach ( $pages as $page )
     65        foreach ( $pages as $page ) {
    6266            $this->assertInstanceOf( 'WP_Post', $page );
     67        }
    6368
    6469        // Force last_changed to increment.
     
    7378        $this->assertEquals( $time2, wp_cache_get( 'last_changed', 'posts' ) );
    7479        $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
    75         foreach ( $pages as $page )
     80        foreach ( $pages as $page ) {
    7681            $this->assertInstanceOf( 'WP_Post', $page );
     82        }
    7783
    7884        $last_changed = wp_cache_get( 'last_changed', 'posts' );
     
    8490        $this->assertGreaterThan( $old_changed_float, $new_changed_float );
    8591
    86         $num_queries = $wpdb->num_queries;
     92        $num_queries  = $wpdb->num_queries;
    8793        $last_changed = wp_cache_get( 'last_changed', 'posts' );
    8894
     
    9298        $this->assertEquals( $last_changed, wp_cache_get( 'last_changed', 'posts' ) );
    9399        $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
    94         foreach ( $pages as $page )
     100        foreach ( $pages as $page ) {
    95101            $this->assertInstanceOf( 'WP_Post', $page );
     102        }
    96103    }
    97104
     
    100107     */
    101108    public function test_cache_should_be_invalidated_by_add_post_meta() {
    102         $posts = self::factory()->post->create_many( 2, array(
    103             'post_type' => 'page',
    104         ) );
     109        $posts = self::factory()->post->create_many(
     110            2, array(
     111                'post_type' => 'page',
     112            )
     113        );
    105114
    106115        add_post_meta( $posts[0], 'foo', 'bar' );
    107116
    108         $cached = get_pages( array(
    109             'meta_key' => 'foo',
    110             'meta_value' => 'bar',
    111         ) );
     117        $cached = get_pages(
     118            array(
     119                'meta_key'   => 'foo',
     120                'meta_value' => 'bar',
     121            )
     122        );
    112123
    113124        $cached_ids = wp_list_pluck( $cached, 'ID' );
     
    116127        add_post_meta( $posts[1], 'foo', 'bar' );
    117128
    118         $found = get_pages( array(
    119             'meta_key' => 'foo',
    120             'meta_value' => 'bar',
    121         ) );
     129        $found = get_pages(
     130            array(
     131                'meta_key'   => 'foo',
     132                'meta_value' => 'bar',
     133            )
     134        );
    122135
    123136        $found_ids = wp_list_pluck( $found, 'ID' );
     
    129142     */
    130143    public function test_cache_should_be_invalidated_by_update_post_meta() {
    131         $posts = self::factory()->post->create_many( 2, array(
    132             'post_type' => 'page',
    133         ) );
     144        $posts = self::factory()->post->create_many(
     145            2, array(
     146                'post_type' => 'page',
     147            )
     148        );
    134149
    135150        add_post_meta( $posts[0], 'foo', 'bar' );
    136151        add_post_meta( $posts[1], 'foo', 'bar' );
    137152
    138         $cached = get_pages( array(
    139             'meta_key' => 'foo',
    140             'meta_value' => 'bar',
    141         ) );
     153        $cached = get_pages(
     154            array(
     155                'meta_key'   => 'foo',
     156                'meta_value' => 'bar',
     157            )
     158        );
    142159
    143160        $cached_ids = wp_list_pluck( $cached, 'ID' );
     
    146163        update_post_meta( $posts[1], 'foo', 'baz' );
    147164
    148         $found = get_pages( array(
    149             'meta_key' => 'foo',
    150             'meta_value' => 'bar',
    151         ) );
     165        $found = get_pages(
     166            array(
     167                'meta_key'   => 'foo',
     168                'meta_value' => 'bar',
     169            )
     170        );
    152171
    153172        $found_ids = wp_list_pluck( $found, 'ID' );
     
    159178     */
    160179    public function test_cache_should_be_invalidated_by_delete_post_meta() {
    161         $posts = self::factory()->post->create_many( 2, array(
    162             'post_type' => 'page',
    163         ) );
     180        $posts = self::factory()->post->create_many(
     181            2, array(
     182                'post_type' => 'page',
     183            )
     184        );
    164185
    165186        add_post_meta( $posts[0], 'foo', 'bar' );
    166187        add_post_meta( $posts[1], 'foo', 'bar' );
    167188
    168         $cached = get_pages( array(
    169             'meta_key' => 'foo',
    170             'meta_value' => 'bar',
    171         ) );
     189        $cached = get_pages(
     190            array(
     191                'meta_key'   => 'foo',
     192                'meta_value' => 'bar',
     193            )
     194        );
    172195
    173196        $cached_ids = wp_list_pluck( $cached, 'ID' );
     
    176199        delete_post_meta( $posts[1], 'foo' );
    177200
    178         $found = get_pages( array(
    179             'meta_key' => 'foo',
    180             'meta_value' => 'bar',
    181         ) );
     201        $found = get_pages(
     202            array(
     203                'meta_key'   => 'foo',
     204                'meta_value' => 'bar',
     205            )
     206        );
    182207
    183208        $found_ids = wp_list_pluck( $found, 'ID' );
     
    189214     */
    190215    public function test_cache_should_be_invalidated_by_delete_post_meta_by_key() {
    191         $posts = self::factory()->post->create_many( 2, array(
    192             'post_type' => 'page',
    193         ) );
     216        $posts = self::factory()->post->create_many(
     217            2, array(
     218                'post_type' => 'page',
     219            )
     220        );
    194221
    195222        add_post_meta( $posts[0], 'foo', 'bar' );
    196223        add_post_meta( $posts[1], 'foo', 'bar' );
    197224
    198         $cached = get_pages( array(
    199             'meta_key' => 'foo',
    200             'meta_value' => 'bar',
    201         ) );
     225        $cached = get_pages(
     226            array(
     227                'meta_key'   => 'foo',
     228                'meta_value' => 'bar',
     229            )
     230        );
    202231
    203232        $cached_ids = wp_list_pluck( $cached, 'ID' );
     
    206235        delete_post_meta_by_key( 'foo' );
    207236
    208         $found = get_pages( array(
    209             'meta_key' => 'foo',
    210             'meta_value' => 'bar',
    211         ) );
     237        $found = get_pages(
     238            array(
     239                'meta_key'   => 'foo',
     240                'meta_value' => 'bar',
     241            )
     242        );
    212243
    213244        $found_ids = wp_list_pluck( $found, 'ID' );
     
    224255        add_post_meta( $posts[2], 'some-meta-key', '1' );
    225256
    226         $this->assertEquals( 1, count( get_pages( array( 'meta_key' => 'some-meta-key', 'meta_value' => '0' ) ) ) );
    227         $this->assertEquals( 1, count( get_pages( array( 'meta_key' => 'some-meta-key', 'meta_value' => '1' ) ) ) );
     257        $this->assertEquals(
     258            1, count(
     259                get_pages(
     260                    array(
     261                        'meta_key'   => 'some-meta-key',
     262                        'meta_value' => '0',
     263                    )
     264                )
     265            )
     266        );
     267        $this->assertEquals(
     268            1, count(
     269                get_pages(
     270                    array(
     271                        'meta_key'   => 'some-meta-key',
     272                        'meta_value' => '1',
     273                    )
     274                )
     275            )
     276        );
    228277        $this->assertEquals( 3, count( get_pages( array( 'meta_key' => 'some-meta-key' ) ) ) );
    229278    }
     
    235284        $page_ids = array();
    236285
    237         foreach ( range( 1, 20 ) as $i )
     286        foreach ( range( 1, 20 ) as $i ) {
    238287            $page_ids[] = self::factory()->post->create( array( 'post_type' => 'page' ) );
     288        }
    239289
    240290        $inc = array_slice( $page_ids, 0, 10 );
     
    243293        sort( $exc );
    244294
    245         $include = get_pages( array( 'include' => $inc ) );
     295        $include    = get_pages( array( 'include' => $inc ) );
    246296        $inc_result = wp_list_pluck( $include, 'ID' );
    247297        sort( $inc_result );
    248298        $this->assertEquals( $inc, $inc_result );
    249299
    250         $exclude = get_pages( array( 'exclude' => $exc ) );
     300        $exclude    = get_pages( array( 'exclude' => $exc ) );
    251301        $exc_result = wp_list_pluck( $exclude, 'ID' );
    252302        sort( $exc_result );
     
    259309    function test_get_pages_parent() {
    260310        $page_id1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
    261         $page_id2 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) );
    262         $page_id3 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id2 ) );
    263         $page_id4 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) );
    264 
    265         $pages = get_pages( array( 'parent' => 0, 'hierarchical' => false ) );
     311        $page_id2 = self::factory()->post->create(
     312            array(
     313                'post_type'   => 'page',
     314                'post_parent' => $page_id1,
     315            )
     316        );
     317        $page_id3 = self::factory()->post->create(
     318            array(
     319                'post_type'   => 'page',
     320                'post_parent' => $page_id2,
     321            )
     322        );
     323        $page_id4 = self::factory()->post->create(
     324            array(
     325                'post_type'   => 'page',
     326                'post_parent' => $page_id1,
     327            )
     328        );
     329
     330        $pages = get_pages(
     331            array(
     332                'parent'       => 0,
     333                'hierarchical' => false,
     334            )
     335        );
    266336        $this->assertEqualSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
    267337
    268         $pages = get_pages( array( 'parent' => $page_id1, 'hierarchical' => false ) );
     338        $pages = get_pages(
     339            array(
     340                'parent'       => $page_id1,
     341                'hierarchical' => false,
     342            )
     343        );
    269344        $this->assertEqualSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
    270345
    271         $pages = get_pages( array( 'parent' => array( $page_id1, $page_id2 ), 'hierarchical' => false ) );
     346        $pages = get_pages(
     347            array(
     348                'parent'       => array( $page_id1, $page_id2 ),
     349                'hierarchical' => false,
     350            )
     351        );
    272352        $this->assertEqualSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
    273353
     
    297377     */
    298378    function test_get_chidren_fields_ids() {
    299         $post_id = self::factory()->post->create();
     379        $post_id   = self::factory()->post->create();
    300380        $child_ids = self::factory()->post->create_many( 5, array( 'post_parent' => $post_id ) );
    301381
    302         $post_ids = get_children( array( 'fields' => 'ids', 'post_parent' => $post_id ) );
     382        $post_ids = get_children(
     383            array(
     384                'fields'      => 'ids',
     385                'post_parent' => $post_id,
     386            )
     387        );
    303388        $this->assertEqualSets( $child_ids, $post_ids );
    304389    }
     
    310395        global $wpdb;
    311396        $page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
    312         $page_2 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    313         $page_3 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    314         $page_4 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_2 ) );
    315 
    316         $pages = get_pages(); // Defaults: hierarchical = true, parent = -1
    317         $pages_default_args = get_pages( array( 'hierarchical' => true, 'parent' => -1 ) );
     397        $page_2 = self::factory()->post->create(
     398            array(
     399                'post_type'   => 'page',
     400                'post_parent' => $page_1,
     401            )
     402        );
     403        $page_3 = self::factory()->post->create(
     404            array(
     405                'post_type'   => 'page',
     406                'post_parent' => $page_1,
     407            )
     408        );
     409        $page_4 = self::factory()->post->create(
     410            array(
     411                'post_type'   => 'page',
     412                'post_parent' => $page_2,
     413            )
     414        );
     415
     416        $pages              = get_pages(); // Defaults: hierarchical = true, parent = -1
     417        $pages_default_args = get_pages(
     418            array(
     419                'hierarchical' => true,
     420                'parent'       => -1,
     421            )
     422        );
    318423        // Confirm the defaults.
    319424        $this->assertEquals( $pages, $pages_default_args );
     
    340445        $page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
    341446        $page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
    342         $page_3 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    343         $page_4 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    344 
    345         $pages = get_pages(); // Defaults: hierarchical = true, child_of = '', parent = -1
    346         $default_args = get_pages( array(
    347             'hierarchical' => true,
    348             'child_of'     => ''
    349         ) );
     447        $page_3 = self::factory()->post->create(
     448            array(
     449                'post_type'   => 'page',
     450                'post_parent' => $page_1,
     451            )
     452        );
     453        $page_4 = self::factory()->post->create(
     454            array(
     455                'post_type'   => 'page',
     456                'post_parent' => $page_1,
     457            )
     458        );
     459
     460        $pages        = get_pages(); // Defaults: hierarchical = true, child_of = '', parent = -1
     461        $default_args = get_pages(
     462            array(
     463                'hierarchical' => true,
     464                'child_of'     => '',
     465            )
     466        );
    350467
    351468        $this->assertEquals( $pages, $default_args );
     
    374491        $page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
    375492        $page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
    376         $page_3 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    377         $page_4 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     493        $page_3 = self::factory()->post->create(
     494            array(
     495                'post_type'   => 'page',
     496                'post_parent' => $page_1,
     497            )
     498        );
     499        $page_4 = self::factory()->post->create(
     500            array(
     501                'post_type'   => 'page',
     502                'post_parent' => $page_1,
     503            )
     504        );
    378505
    379506        $pages = get_pages( array( 'hierarchical' => false ) ); // child_of = '', parent = -1
     
    400527        $page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
    401528        $page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
    402         $page_3 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    403         $page_4 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_3 ) );
    404         $page_5 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     529        $page_3 = self::factory()->post->create(
     530            array(
     531                'post_type'   => 'page',
     532                'post_parent' => $page_1,
     533            )
     534        );
     535        $page_4 = self::factory()->post->create(
     536            array(
     537                'post_type'   => 'page',
     538                'post_parent' => $page_3,
     539            )
     540        );
     541        $page_5 = self::factory()->post->create(
     542            array(
     543                'post_type'   => 'page',
     544                'post_parent' => $page_1,
     545            )
     546        );
    405547
    406548        $pages = get_pages( array( 'child_of' => $page_1 ) ); // Defaults: hierarchical = true, parent = -1.
     
    428570        $page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
    429571        $page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
    430         $page_3 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    431         $page_4 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_3 ) );
    432         $page_5 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    433 
    434         $pages = get_pages( array( 'hierarchical' => false, 'child_of' => $page_1 ) );
     572        $page_3 = self::factory()->post->create(
     573            array(
     574                'post_type'   => 'page',
     575                'post_parent' => $page_1,
     576            )
     577        );
     578        $page_4 = self::factory()->post->create(
     579            array(
     580                'post_type'   => 'page',
     581                'post_parent' => $page_3,
     582            )
     583        );
     584        $page_5 = self::factory()->post->create(
     585            array(
     586                'post_type'   => 'page',
     587                'post_parent' => $page_1,
     588            )
     589        );
     590
     591        $pages = get_pages(
     592            array(
     593                'hierarchical' => false,
     594                'child_of'     => $page_1,
     595            )
     596        );
    435597
    436598        /*
     
    456618    function test_wp_list_pages_classes() {
    457619        $type = 'taco';
    458         register_post_type( $type, array( 'hierarchical' => true, 'public' => true ) );
    459 
    460         $posts = self::factory()->post->create_many( 2, array( 'post_type' => $type ) );
     620        register_post_type(
     621            $type, array(
     622                'hierarchical' => true,
     623                'public'       => true,
     624            )
     625        );
     626
     627        $posts   = self::factory()->post->create_many( 2, array( 'post_type' => $type ) );
    461628        $post_id = reset( $posts );
    462629
     
    465632        $this->assertEquals( $post_id, get_queried_object_id() );
    466633
    467         $output = wp_list_pages( array(
    468             'echo' => false,
    469             'title_li' => '',
    470             'post_type' => $type
    471         ) );
     634        $output = wp_list_pages(
     635            array(
     636                'echo'      => false,
     637                'title_li'  => '',
     638                'post_type' => $type,
     639            )
     640        );
    472641
    473642        $this->assertNotEmpty( $output );
     
    481650    function test_exclude_tree() {
    482651        $post_id1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
    483         $post_id2 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id1 ) );
     652        $post_id2 = self::factory()->post->create(
     653            array(
     654                'post_type'   => 'page',
     655                'post_parent' => $post_id1,
     656            )
     657        );
    484658        $post_id3 = self::factory()->post->create( array( 'post_type' => 'page' ) );
    485         $post_id4 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id3 ) );
     659        $post_id4 = self::factory()->post->create(
     660            array(
     661                'post_type'   => 'page',
     662                'post_parent' => $post_id3,
     663            )
     664        );
    486665
    487666        $all = get_pages();
     
    505684
    506685        $post_id5 = self::factory()->post->create( array( 'post_type' => 'page' ) );
    507         $post_id6 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id5 ) );
     686        $post_id6 = self::factory()->post->create(
     687            array(
     688                'post_type'   => 'page',
     689                'post_parent' => $post_id5,
     690            )
     691        );
    508692
    509693        $exclude6 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) );
Note: See TracChangeset for help on using the changeset viewer.