Skip to:
Content

bbPress.org

Changeset 7818


Ignore:
Timestamp:
10/04/2026 05:37:32 PM (5 weeks ago)
Author:
johnjamesjacoby
Message:

Tests: Cover user profile rejection paths.

Verify anonymous cookie input guards, user edit nonce and email validation, and email confirmation rejection. Keep successful cookie headers marked for an HTTP test harness.

In trunk, for 2.7.

See #3695.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/users/functions/functions.php

    r7806 r7818  
    9191        /**
    9292         * @covers ::bbp_set_current_anonymous_user_data
    93          * @todo   Implement test_bbp_set_current_anonymous_user_data().
     93         */
     94        public function test_bbp_set_current_anonymous_user_data_rejects_invalid_input() {
     95                $called   = 0;
     96                $lifetime = function ( $value ) use ( &$called ) {
     97                        ++$called;
     98                        return 60;
     99                };
     100
     101                add_filter( 'comment_cookie_lifetime', $lifetime );
     102
     103                try {
     104                        $this->assertNull( bbp_set_current_anonymous_user_data() );
     105                        $this->assertNull( bbp_set_current_anonymous_user_data( 'not an array' ) );
     106                        $this->assertSame( 0, $called );
     107                } finally {
     108                        remove_filter( 'comment_cookie_lifetime', $lifetime );
     109                }
     110        }
     111
     112        /**
     113         * @covers ::bbp_set_current_anonymous_user_data
     114         * @todo   Test the Set-Cookie headers from an HTTP request.
    94115         */
    95116        public function test_bbp_set_current_anonymous_user_data() {
    96                 // Remove the following lines when you implement this test.
    97                 $this->markTestIncomplete(
    98                         'This test has not been implemented yet.'
    99                 );
     117                $this->markTestIncomplete( 'The successful cookie path requires an HTTP test harness.' );
    100118        }
    101119
    … …  
    199217        }
    200218
    201         /**
     219        /**
    202220         * @covers ::bbp_edit_user_handler
    203          * @todo   Implement test_bbp_edit_user_handler().
    204221         */
    205222        public function test_bbp_edit_user_handler() {
    206                 // Remove the following lines when you implement this test.
    207                 $this->markTestIncomplete(
    208                         'This test has not been implemented yet.'
    209                 );
     223                $user_id       = $this->factory->user->create( array( 'role' => 'administrator' ) );
     224                $other_id      = $this->factory->user->create();
     225                $old_user      = get_current_user_id();
     226                $old_post      = $_POST;
     227                $old_request   = $_REQUEST;
     228                $old_method    = $_SERVER['REQUEST_METHOD'];
     229                $old_errors    = bbpress()->errors;
     230                $old_displayed = bbpress()->displayed_user;
     231                $displayed     = function () use ( $user_id ) {
     232                        return $user_id;
     233                };
     234
     235                $this->set_current_user( $user_id );
     236                bbpress()->displayed_user = get_userdata( $user_id );
     237                bbpress()->errors         = new WP_Error();
     238                add_filter( 'bbp_get_displayed_user_id', $displayed );
     239
     240                try {
     241                        $this->assertNull( bbp_edit_user_handler( 'other' ) );
     242                        $this->assertFalse( bbp_has_errors() );
     243
     244                        $_SERVER['REQUEST_METHOD'] = 'POST';
     245                        $_REQUEST['_wpnonce']      = 'invalid';
     246                        bbp_edit_user_handler( 'bbp-update-user' );
     247                        $this->assertContains( 'bbp_update_user_nonce', bbpress()->errors->get_error_codes() );
     248
     249                        bbpress()->errors = new WP_Error();
     250                        $_REQUEST['_wpnonce'] = wp_create_nonce( 'update-user_' . $user_id );
     251                        unset( $_POST['email'] );
     252                        bbp_edit_user_handler( 'bbp-update-user' );
     253                        $this->assertContains( 'bbp_user_email_empty', bbpress()->errors->get_error_codes() );
     254
     255                        bbpress()->errors = new WP_Error();
     256                        $_POST['email']   = 'not-an-email';
     257                        bbp_edit_user_handler( 'bbp-update-user' );
     258                        $this->assertContains( 'bbp_user_email_invalid', bbpress()->errors->get_error_codes() );
     259
     260                        bbpress()->errors = new WP_Error();
     261                        $_POST['email']   = get_userdata( $other_id )->user_email;
     262                        bbp_edit_user_handler( 'bbp-update-user' );
     263                        $this->assertContains( 'bbp_user_email_taken', bbpress()->errors->get_error_codes() );
     264                } finally {
     265                        remove_filter( 'bbp_get_displayed_user_id', $displayed );
     266                        $this->set_current_user( $old_user );
     267                        bbpress()->displayed_user = $old_displayed;
     268                        bbpress()->errors         = $old_errors;
     269                        $_POST                     = $old_post;
     270                        $_REQUEST                  = $old_request;
     271                        $_SERVER['REQUEST_METHOD']  = $old_method;
     272                }
    210273        }
    211274
    212275        /**
    213276         * @covers ::bbp_user_email_change_handler
    214          * @todo   Implement test_bbp_user_email_change_handler().
    215277         */
    216278        public function test_bbp_user_email_change_handler() {
    217                 // Remove the following lines when you implement this test.
    218                 $this->markTestIncomplete(
    219                         'This test has not been implemented yet.'
    220                 );
     279                $user_id     = $this->factory->user->create();
     280                $other_id    = $this->factory->user->create();
     281                $old_get     = $_GET;
     282                $old_request = $_REQUEST;
     283                $old_errors  = bbpress()->errors;
     284                $displayed   = function () use ( $user_id ) {
     285                        return $user_id;
     286                };
     287                $home_edit = '__return_true';
     288                $key       = '_new_email';
     289                $pending   = array( 'hash' => 'valid-hash', 'newemail' => get_userdata( $other_id )->user_email );
     290
     291                add_filter( 'bbp_get_displayed_user_id', $displayed );
     292                add_filter( 'bbp_is_user_home_edit', $home_edit );
     293                bbpress()->errors = new WP_Error();
     294
     295                try {
     296                        update_user_meta( $user_id, $key, $pending );
     297                        $_GET['newuseremail'] = 'wrong-hash';
     298                        $this->assertNull( bbp_user_email_change_handler( 'other' ) );
     299                        $this->assertNull( bbp_user_email_change_handler( 'bbp-update-user-email' ) );
     300                        $this->assertSame( $pending, get_user_meta( $user_id, $key, true ) );
     301
     302                        $_GET['newuseremail'] = 'valid-hash';
     303                        bbp_user_email_change_handler( 'bbp-update-user-email' );
     304                        $this->assertContains( 'bbp_user_email_taken', bbpress()->errors->get_error_codes() );
     305                        $this->assertSame( '', get_user_meta( $user_id, $key, true ) );
     306
     307                        bbpress()->errors = new WP_Error();
     308                        $_GET = array( 'dismiss' => $user_id . $key );
     309                        unset( $_REQUEST['_wpnonce'] );
     310                        bbp_user_email_change_handler( 'bbp-update-user-email' );
     311                        $this->assertContains( 'bbp_dismiss_new_email_nonce', bbpress()->errors->get_error_codes() );
     312                } finally {
     313                        remove_filter( 'bbp_get_displayed_user_id', $displayed );
     314                        remove_filter( 'bbp_is_user_home_edit', $home_edit );
     315                        bbpress()->errors = $old_errors;
     316                        $_GET             = $old_get;
     317                        $_REQUEST         = $old_request;
     318                }
    221319        }
    222320
Note: See TracChangeset for help on using the changeset viewer.