Make WordPress Core


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

    r41644 r42343  
    1818        $this->assertSame( 0, did_action( $error ) );
    1919
    20         $data = array(
     20        $data    = array(
    2121            'comment_post_ID' => 0,
    2222        );
     
    3535        $this->assertSame( 0, did_action( $error ) );
    3636
    37         $post = self::factory()->post->create_and_get( array(
    38             'comment_status' => 'closed',
    39         ) );
    40         $data = array(
     37        $post    = self::factory()->post->create_and_get(
     38            array(
     39                'comment_status' => 'closed',
     40            )
     41        );
     42        $data    = array(
    4143            'comment_post_ID' => $post->ID,
    4244        );
     
    5759        $post = self::factory()->post->create_and_get();
    5860        wp_trash_post( $post->ID );
    59         $data = array(
     61        $data    = array(
    6062            'comment_post_ID' => $post->ID,
    6163        );
     
    7375        $this->assertSame( 0, did_action( $error ) );
    7476
    75         $post = self::factory()->post->create_and_get( array(
    76             'post_status' => 'draft',
    77         ) );
    78         $data = array(
     77        $post    = self::factory()->post->create_and_get(
     78            array(
     79                'post_status' => 'draft',
     80            )
     81        );
     82        $data    = array(
    7983            'comment_post_ID' => $post->ID,
    8084        );
     
    9498        $error = 'comment_on_draft';
    9599
    96         $user = self::factory()->user->create_and_get( array(
    97             'role' => 'author',
    98         ) );
     100        $user = self::factory()->user->create_and_get(
     101            array(
     102                'role' => 'author',
     103            )
     104        );
    99105
    100106        wp_set_current_user( $user->ID );
     
    102108        $this->assertSame( 0, did_action( $error ) );
    103109
    104         $post = self::factory()->post->create_and_get( array(
    105             'post_status' => 'draft',
    106             'post_author' => $user->ID,
    107         ) );
    108         $data = array(
     110        $post    = self::factory()->post->create_and_get(
     111            array(
     112                'post_status' => 'draft',
     113                'post_author' => $user->ID,
     114            )
     115        );
     116        $data    = array(
    109117            'comment_post_ID' => $post->ID,
    110118        );
     
    124132        $this->assertSame( 0, did_action( $error ) );
    125133
    126         $post = self::factory()->post->create_and_get( array(
    127             'post_date' => date( 'Y-m-d H:i:s', strtotime( '+1 day' ) ),
    128         ) );
     134        $post = self::factory()->post->create_and_get(
     135            array(
     136                'post_date' => date( 'Y-m-d H:i:s', strtotime( '+1 day' ) ),
     137            )
     138        );
    129139
    130140        $this->assertSame( 'future', $post->post_status );
    131141
    132         $data = array(
     142        $data    = array(
    133143            'comment_post_ID' => $post->ID,
    134144        );
     
    147157        $this->assertSame( 0, did_action( $error ) );
    148158
    149         $post = self::factory()->post->create_and_get( array(
    150             'post_password' => 'password',
    151         ) );
    152         $data = array(
     159        $post    = self::factory()->post->create_and_get(
     160            array(
     161                'post_password' => 'password',
     162            )
     163        );
     164        $data    = array(
    153165            'comment_post_ID' => $post->ID,
    154166        );
     
    166178        $hasher   = new PasswordHash( 8, true );
    167179
    168         $_COOKIE['wp-postpass_' . COOKIEHASH] = $hasher->HashPassword( $password );
    169 
    170         $post = self::factory()->post->create_and_get( array(
    171             'post_password' => $password,
    172         ) );
    173         $data = array(
     180        $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] = $hasher->HashPassword( $password );
     181
     182        $post    = self::factory()->post->create_and_get(
     183            array(
     184                'post_password' => $password,
     185            )
     186        );
     187        $data    = array(
    174188            'comment_post_ID' => $post->ID,
    175189            'comment'         => 'Comment',
     
    179193        $comment = wp_handle_comment_submission( $data );
    180194
    181         unset( $_COOKIE['wp-postpass_' . COOKIEHASH] );
     195        unset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
    182196
    183197        $this->assertNotWPError( $comment );
     
    188202    public function test_submitting_valid_comment_as_logged_in_user_succeeds() {
    189203
    190         $user = self::factory()->user->create_and_get( array(
    191             'user_url' => 'http://user.example.org'
    192         ) );
    193 
    194         wp_set_current_user( $user->ID );
    195 
    196         $post = self::factory()->post->create_and_get();
    197         $data = array(
    198             'comment_post_ID' => $post->ID,
    199             'comment'         => 'Comment',
    200         );
    201         $comment = wp_handle_comment_submission( $data );
    202 
    203         $this->assertNotWPError( $comment );
    204         $this->assertInstanceOf( 'WP_Comment', $comment );
    205 
    206         $this->assertSame( 'Comment', $comment->comment_content);
     204        $user = self::factory()->user->create_and_get(
     205            array(
     206                'user_url' => 'http://user.example.org',
     207            )
     208        );
     209
     210        wp_set_current_user( $user->ID );
     211
     212        $post    = self::factory()->post->create_and_get();
     213        $data    = array(
     214            'comment_post_ID' => $post->ID,
     215            'comment'         => 'Comment',
     216        );
     217        $comment = wp_handle_comment_submission( $data );
     218
     219        $this->assertNotWPError( $comment );
     220        $this->assertInstanceOf( 'WP_Comment', $comment );
     221
     222        $this->assertSame( 'Comment', $comment->comment_content );
    207223        $this->assertSame( $user->display_name, $comment->comment_author );
    208224        $this->assertSame( $user->user_email, $comment->comment_author_email );
     
    214230    public function test_submitting_valid_comment_anonymously_succeeds() {
    215231
    216         $post = self::factory()->post->create_and_get();
    217         $data = array(
     232        $post    = self::factory()->post->create_and_get();
     233        $data    = array(
    218234            'comment_post_ID' => $post->ID,
    219235            'comment'         => 'Comment',
    220236            'author'          => 'Comment Author',
    221237            'email'           => 'comment@example.org',
    222             'url'             => 'user.example.org'
    223         );
    224         $comment = wp_handle_comment_submission( $data );
    225 
    226         $this->assertNotWPError( $comment );
    227         $this->assertInstanceOf( 'WP_Comment', $comment );
    228 
    229         $this->assertSame( 'Comment', $comment->comment_content);
     238            'url'             => 'user.example.org',
     239        );
     240        $comment = wp_handle_comment_submission( $data );
     241
     242        $this->assertNotWPError( $comment );
     243        $this->assertInstanceOf( 'WP_Comment', $comment );
     244
     245        $this->assertSame( 'Comment', $comment->comment_content );
    230246        $this->assertSame( 'Comment Author', $comment->comment_author );
    231247        $this->assertSame( 'comment@example.org', $comment->comment_author_email );
     
    242258    public function test_submitting_comment_handles_slashes_correctly_handles_slashes() {
    243259
    244         $post = self::factory()->post->create_and_get();
    245         $data = array(
     260        $post    = self::factory()->post->create_and_get();
     261        $data    = array(
    246262            'comment_post_ID' => $post->ID,
    247263            'comment'         => 'Comment with 1 slash: \\',
     
    254270        $this->assertInstanceOf( 'WP_Comment', $comment );
    255271
    256         $this->assertSame( 'Comment with 1 slash: \\', $comment->comment_content);
     272        $this->assertSame( 'Comment with 1 slash: \\', $comment->comment_content );
    257273        $this->assertSame( 'Comment Author with 1 slash: \\', $comment->comment_author );
    258274        $this->assertSame( 'comment@example.org', $comment->comment_author_email );
     
    264280        $error = 'comment_id_not_found';
    265281
    266         $post = self::factory()->post->create_and_get( array(
    267             'post_status' => 'private',
    268         ) );
    269         $data = array(
     282        $post    = self::factory()->post->create_and_get(
     283            array(
     284                'post_status' => 'private',
     285            )
     286        );
     287        $data    = array(
    270288            'comment_post_ID' => $post->ID,
    271289        );
     
    280298    public function test_submitting_comment_as_logged_in_user_to_inaccessible_private_post_returns_error() {
    281299
    282         $error  = 'comment_id_not_found';
    283 
    284         $author = self::factory()->user->create_and_get( array(
    285             'role' => 'author',
    286         ) );
    287         $user   = self::factory()->user->create_and_get( array(
    288             'role' => 'author',
    289         ) );
    290 
    291         wp_set_current_user( $user->ID );
    292 
    293         $post = self::factory()->post->create_and_get( array(
    294             'post_status' => 'private',
    295             'post_author' => $author->ID,
    296         ) );
    297         $data = array(
     300        $error = 'comment_id_not_found';
     301
     302        $author = self::factory()->user->create_and_get(
     303            array(
     304                'role' => 'author',
     305            )
     306        );
     307        $user   = self::factory()->user->create_and_get(
     308            array(
     309                'role' => 'author',
     310            )
     311        );
     312
     313        wp_set_current_user( $user->ID );
     314
     315        $post    = self::factory()->post->create_and_get(
     316            array(
     317                'post_status' => 'private',
     318                'post_author' => $author->ID,
     319            )
     320        );
     321        $data    = array(
    298322            'comment_post_ID' => $post->ID,
    299323        );
     
    308332    public function test_submitting_comment_to_private_post_with_closed_comments_returns_correct_error() {
    309333
    310         $error  = 'comment_id_not_found';
    311 
    312         $author = self::factory()->user->create_and_get( array(
    313             'role' => 'author',
    314         ) );
    315         $user   = self::factory()->user->create_and_get( array(
    316             'role' => 'author',
    317         ) );
    318 
    319         wp_set_current_user( $user->ID );
    320 
    321         $post = self::factory()->post->create_and_get( array(
    322             'post_status'    => 'private',
    323             'post_author'    => $author->ID,
    324             'comment_status' => 'closed',
    325         ) );
    326         $data = array(
     334        $error = 'comment_id_not_found';
     335
     336        $author = self::factory()->user->create_and_get(
     337            array(
     338                'role' => 'author',
     339            )
     340        );
     341        $user   = self::factory()->user->create_and_get(
     342            array(
     343                'role' => 'author',
     344            )
     345        );
     346
     347        wp_set_current_user( $user->ID );
     348
     349        $post    = self::factory()->post->create_and_get(
     350            array(
     351                'post_status'    => 'private',
     352                'post_author'    => $author->ID,
     353                'comment_status' => 'closed',
     354            )
     355        );
     356        $data    = array(
    327357            'comment_post_ID' => $post->ID,
    328358        );
     
    341371        wp_set_current_user( $user->ID );
    342372
    343         $post = self::factory()->post->create_and_get( array(
    344             'post_status' => 'private',
    345             'post_author' => $user->ID,
    346         ) );
    347         $data = array(
     373        $post    = self::factory()->post->create_and_get(
     374            array(
     375                'post_status' => 'private',
     376                'post_author' => $user->ID,
     377            )
     378        );
     379        $data    = array(
    348380            'comment_post_ID' => $post->ID,
    349381            'comment'         => 'Comment',
     
    359391    public function test_submitting_comment_to_accessible_private_post_succeeds() {
    360392
    361         $author = self::factory()->user->create_and_get( array(
    362             'role' => 'author',
    363         ) );
    364         $user = self::factory()->user->create_and_get( array(
    365             'role' => 'editor',
    366         ) );
    367 
    368         wp_set_current_user( $user->ID );
    369 
    370         $post = self::factory()->post->create_and_get( array(
    371             'post_status' => 'private',
    372             'post_author' => $author->ID,
    373         ) );
    374         $data = array(
     393        $author = self::factory()->user->create_and_get(
     394            array(
     395                'role' => 'author',
     396            )
     397        );
     398        $user   = self::factory()->user->create_and_get(
     399            array(
     400                'role' => 'editor',
     401            )
     402        );
     403
     404        wp_set_current_user( $user->ID );
     405
     406        $post    = self::factory()->post->create_and_get(
     407            array(
     408                'post_status' => 'private',
     409                'post_author' => $author->ID,
     410            )
     411        );
     412        $data    = array(
    375413            'comment_post_ID' => $post->ID,
    376414            'comment'         => 'Comment',
     
    386424    public function test_anonymous_user_cannot_comment_unfiltered_html() {
    387425
    388         $post = self::factory()->post->create_and_get();
    389         $data = array(
     426        $post    = self::factory()->post->create_and_get();
     427        $data    = array(
    390428            'comment_post_ID' => $post->ID,
    391429            'comment'         => 'Comment <script>alert(document.cookie);</script>',
     
    403441    public function test_unprivileged_user_cannot_comment_unfiltered_html() {
    404442
    405         $user = self::factory()->user->create_and_get( array(
    406             'role' => 'author',
    407         ) );
     443        $user = self::factory()->user->create_and_get(
     444            array(
     445                'role' => 'author',
     446            )
     447        );
    408448        wp_set_current_user( $user->ID );
    409449
    410450        $this->assertFalse( current_user_can( 'unfiltered_html' ) );
    411451
    412         $post = self::factory()->post->create_and_get();
    413         $data = array(
     452        $post    = self::factory()->post->create_and_get();
     453        $data    = array(
    414454            'comment_post_ID' => $post->ID,
    415455            'comment'         => 'Comment <script>alert(document.cookie);</script>',
     
    425465    public function test_unprivileged_user_cannot_comment_unfiltered_html_even_with_valid_nonce() {
    426466
    427         $user = self::factory()->user->create_and_get( array(
    428             'role' => 'author',
    429         ) );
     467        $user = self::factory()->user->create_and_get(
     468            array(
     469                'role' => 'author',
     470            )
     471        );
    430472        wp_set_current_user( $user->ID );
    431473
     
    438480        $this->assertNotEmpty( wp_verify_nonce( $nonce, $action ) );
    439481
    440         $data = array(
     482        $data    = array(
    441483            'comment_post_ID'             => $post->ID,
    442484            'comment'                     => 'Comment <script>alert(document.cookie);</script>',
     
    455497        $this->assertFalse( defined( 'DISALLOW_UNFILTERED_HTML' ) );
    456498
    457         $user = self::factory()->user->create_and_get( array(
    458             'role' => 'editor',
    459         ) );
     499        $user = self::factory()->user->create_and_get(
     500            array(
     501                'role' => 'editor',
     502            )
     503        );
    460504
    461505        if ( is_multisite() ) {
     
    475519        $this->assertNotEmpty( wp_verify_nonce( $nonce, $action ) );
    476520
    477         $data = array(
     521        $data    = array(
    478522            'comment_post_ID'             => $post->ID,
    479523            'comment'                     => 'Comment <script>alert(document.cookie);</script>',
     
    490534    public function test_privileged_user_cannot_comment_unfiltered_html_without_valid_nonce() {
    491535
    492         $user = self::factory()->user->create_and_get( array(
    493             'role' => 'editor',
    494         ) );
     536        $user = self::factory()->user->create_and_get(
     537            array(
     538                'role' => 'editor',
     539            )
     540        );
    495541
    496542        if ( is_multisite() ) {
     
    504550        $this->assertTrue( current_user_can( 'unfiltered_html' ) );
    505551
    506         $post   = self::factory()->post->create_and_get();
    507         $data = array(
     552        $post    = self::factory()->post->create_and_get();
     553        $data    = array(
    508554            'comment_post_ID' => $post->ID,
    509555            'comment'         => 'Comment <script>alert(document.cookie);</script>',
     
    524570        update_option( 'comment_registration', '1' );
    525571
    526         $post = self::factory()->post->create_and_get();
    527         $data = array(
     572        $post    = self::factory()->post->create_and_get();
     573        $data    = array(
    528574            'comment_post_ID' => $post->ID,
    529575        );
     
    544590        update_option( 'require_name_email', '1' );
    545591
    546         $post = self::factory()->post->create_and_get();
    547         $data = array(
     592        $post    = self::factory()->post->create_and_get();
     593        $data    = array(
    548594            'comment_post_ID' => $post->ID,
    549595            'comment'         => 'Comment',
     
    566612        update_option( 'require_name_email', '1' );
    567613
    568         $post = self::factory()->post->create_and_get();
    569         $data = array(
     614        $post    = self::factory()->post->create_and_get();
     615        $data    = array(
    570616            'comment_post_ID' => $post->ID,
    571617            'comment'         => 'Comment',
     
    588634        update_option( 'require_name_email', '1' );
    589635
    590         $post = self::factory()->post->create_and_get();
    591         $data = array(
     636        $post    = self::factory()->post->create_and_get();
     637        $data    = array(
    592638            'comment_post_ID' => $post->ID,
    593639            'comment'         => 'Comment',
     
    608654        $error = 'require_valid_comment';
    609655
    610         $post = self::factory()->post->create_and_get();
    611         $data = array(
     656        $post    = self::factory()->post->create_and_get();
     657        $data    = array(
    612658            'comment_post_ID' => $post->ID,
    613659            'comment'         => '',
     
    630676        $post = self::factory()->post->create_and_get();
    631677
    632         $data = array(
     678        $data    = array(
    633679            'comment_post_ID' => $post->ID,
    634680            'comment'         => rand_long_str( 65536 ),
     
    650696        $post = self::factory()->post->create_and_get();
    651697
    652         $data = array(
     698        $data    = array(
    653699            'comment_post_ID' => $post->ID,
    654700            'comment'         => 'Comment',
     
    670716        $post = self::factory()->post->create_and_get();
    671717
    672         $data = array(
     718        $data    = array(
    673719            'comment_post_ID' => $post->ID,
    674720            'comment'         => 'Comment',
     
    688734        $error = 'comment_author_url_column_length';
    689735
    690         $post = self::factory()->post->create_and_get();
    691         $data = array(
     736        $post    = self::factory()->post->create_and_get();
     737        $data    = array(
    692738            'comment_post_ID' => $post->ID,
    693739            'comment'         => 'Comment',
     
    707753    public function test_comment_submission_sends_all_expected_parameters_to_preprocess_comment_filter() {
    708754
    709         $user = self::factory()->user->create_and_get( array(
    710             'role' => 'author',
    711         ) );
     755        $user = self::factory()->user->create_and_get(
     756            array(
     757                'role' => 'author',
     758            )
     759        );
    712760        wp_set_current_user( $user->ID );
    713761
     
    725773
    726774        $this->assertNotWPError( $comment );
    727         $this->assertEquals( array(
    728             'comment_post_ID'      => $post->ID,
    729             'comment_author'       => $user->display_name,
    730             'comment_author_email' => $user->user_email,
    731             'comment_author_url'   => $user->user_url,
    732             'comment_content'      => $data['comment'],
    733             'comment_type'         => '',
    734             'comment_parent'       => '0',
    735             'user_ID'              => $user->ID,
    736             'user_id'              => $user->ID,
    737         ), $this->preprocess_comment_data );
     775        $this->assertEquals(
     776            array(
     777                'comment_post_ID'      => $post->ID,
     778                'comment_author'       => $user->display_name,
     779                'comment_author_email' => $user->user_email,
     780                'comment_author_url'   => $user->user_url,
     781                'comment_content'      => $data['comment'],
     782                'comment_type'         => '',
     783                'comment_parent'       => '0',
     784                'user_ID'              => $user->ID,
     785                'user_id'              => $user->ID,
     786            ), $this->preprocess_comment_data
     787        );
    738788
    739789    }
     
    748798     */
    749799    public function test_submitting_duplicate_comments() {
    750         $post = self::factory()->post->create_and_get( array(
    751             'post_status' => 'publish',
    752         ) );
    753         $data = array(
     800        $post           = self::factory()->post->create_and_get(
     801            array(
     802                'post_status' => 'publish',
     803            )
     804        );
     805        $data           = array(
    754806            'comment_post_ID' => $post->ID,
    755807            'comment'         => 'Did I say that?',
     
    757809            'email'           => 'mail@example.com',
    758810        );
    759         $first_comment = wp_handle_comment_submission( $data );
     811        $first_comment  = wp_handle_comment_submission( $data );
    760812        $second_comment = wp_handle_comment_submission( $data );
    761813        $this->assertWPError( $second_comment );
     
    767819     */
    768820    public function test_comments_flood() {
    769         $post = self::factory()->post->create_and_get( array(
    770             'post_status' => 'publish',
    771         ) );
    772         $data = array(
     821        $post          = self::factory()->post->create_and_get(
     822            array(
     823                'post_status' => 'publish',
     824            )
     825        );
     826        $data          = array(
    773827            'comment_post_ID' => $post->ID,
    774828            'comment'         => 'Did I say that?',
     
    779833
    780834        $data['comment'] = 'Wow! I am quick!';
    781         $second_comment = wp_handle_comment_submission( $data );
     835        $second_comment  = wp_handle_comment_submission( $data );
    782836
    783837        $this->assertWPError( $second_comment );
     
    789843     */
    790844    public function test_comments_flood_user_is_admin() {
    791         $user = self::factory()->user->create_and_get( array(
    792             'role' => 'administrator',
    793         ) );
    794         wp_set_current_user( $user->ID );
    795 
    796         $post = self::factory()->post->create_and_get( array(
    797             'post_status' => 'publish',
    798         ) );
    799         $data = array(
     845        $user = self::factory()->user->create_and_get(
     846            array(
     847                'role' => 'administrator',
     848            )
     849        );
     850        wp_set_current_user( $user->ID );
     851
     852        $post          = self::factory()->post->create_and_get(
     853            array(
     854                'post_status' => 'publish',
     855            )
     856        );
     857        $data          = array(
    800858            'comment_post_ID' => $post->ID,
    801859            'comment'         => 'Did I say that?',
     
    806864
    807865        $data['comment'] = 'Wow! I am quick!';
    808         $second_comment = wp_handle_comment_submission( $data );
     866        $second_comment  = wp_handle_comment_submission( $data );
    809867
    810868        $this->assertNotWPError( $second_comment );
Note: See TracChangeset for help on using the changeset viewer.