Skip to:
Content

bbPress.org

Changeset 7806


Ignore:
Timestamp:
10/04/2026 09:45:37 AM (6 weeks ago)
Author:
johnjamesjacoby
Message:

Tests: Cover user profile edit permissions.

Replace the user-edit authorization stub with assertions for non-edit pages, denied users, self-editing, administrators, and the edit-any override. Inspect redirect decisions without exiting the PHPUnit process.

In trunk, for 2.7.

See #3695.

File:
1 edited

Legend:

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

    r7805 r7806  
    292292        /**
    293293         * @covers ::bbp_check_user_edit
    294          * @todo   Implement test_bbp_check_user_edit().
    295294         */
    296295        public function test_bbp_check_user_edit() {
    297                 // Remove the following lines when you implement this test.
    298                 $this->markTestIncomplete(
    299                         'This test has not been implemented yet.'
    300                 );
     296                $user_id   = $this->factory->user->create();
     297                $other_id  = $this->factory->user->create();
     298                $admin_id  = $this->factory->user->create( array( 'role' => 'administrator' ) );
     299                $old_user  = get_current_user_id();
     300                $decisions = array();
     301                $displayed = function () use ( $user_id ) {
     302                        return $user_id;
     303                };
     304                $check = function ( $redirect, $checked_user_id ) use ( &$decisions ) {
     305                        $decisions[] = array( $redirect, $checked_user_id );
     306
     307                        // Inspect the decision without an HTTP redirect.
     308                        return false;
     309                };
     310                add_filter( 'bbp_get_displayed_user_id', $displayed );
     311                add_filter( 'bbp_check_user_edit', $check, 10, 2 );
     312                if ( is_multisite() ) {
     313                        grant_super_admin( $admin_id );
     314                }
     315
     316                try {
     317                        $this->set_current_user( $other_id );
     318                        bbp_check_user_edit();
     319                        $this->assertSame( array(), $decisions );
     320
     321                        add_filter( 'bbp_is_single_user_edit', '__return_true' );
     322                        bbp_check_user_edit();
     323                        $this->assertSame( array( array( true, $user_id ) ), $decisions );
     324
     325                        $this->set_current_user( $user_id );
     326                        add_filter( 'bbp_is_user_home_edit', '__return_true' );
     327                        bbp_check_user_edit();
     328                        $this->assertSame( array( false, $user_id ), $decisions[1] );
     329                        remove_filter( 'bbp_is_user_home_edit', '__return_true' );
     330
     331                        $this->set_current_user( $admin_id );
     332                        bbp_check_user_edit();
     333                        $this->assertSame( array( false, $user_id ), $decisions[2] );
     334
     335                        $this->set_current_user( $other_id );
     336                        add_filter( 'enable_edit_any_user_configuration', '__return_true' );
     337                        bbp_check_user_edit();
     338                        $this->assertSame( array( false, $user_id ), $decisions[3] );
     339                } finally {
     340                        remove_filter( 'enable_edit_any_user_configuration', '__return_true' );
     341                        remove_filter( 'bbp_is_single_user_edit', '__return_true' );
     342                        remove_filter( 'bbp_is_user_home_edit', '__return_true' );
     343                        remove_filter( 'bbp_get_displayed_user_id', $displayed );
     344                        remove_filter( 'bbp_check_user_edit', $check, 10 );
     345                        $this->set_current_user( $old_user );
     346                        if ( is_multisite() ) {
     347                                revoke_super_admin( $admin_id );
     348                        }
     349                }
    301350        }
    302351
Note: See TracChangeset for help on using the changeset viewer.