Skip to:
Content

bbPress.org

Changeset 7777


Ignore:
Timestamp:
10/04/2026 03:56:50 AM (3 weeks ago)
Author:
johnjamesjacoby
Message:

Tests: Cover forum submission and visibility.

Replace seven incomplete forum-function tests with cases for create/edit submissions, attribute saves, edit permissions, exclusion formats, and direct-URL visibility. Include invalid-nonce regressions for both submission handlers.

In trunk, for 2.7.

See #3695.

Location:
trunk/tests/phpunit/testcases/forums/functions
Files:
3 edited

Legend:

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

    r7776 r7777  
    3232
    3333                $this->assertSame( array( $forum_id ), $observed );
     34        }
     35
     36        /**
     37         * Submit a front-end forum form without allowing its redirect to exit PHPUnit.
     38         *
     39         * @param string $handler      Submission handler.
     40         * @param string $action       Form action.
     41         * @param string $nonce_action Nonce action.
     42         * @param string $saved_action After-save action.
     43         * @param array  $post         Form fields.
     44         * @return array Saved forum IDs, redirect, and errors.
     45         */
     46        protected function submit_forum_form( $handler, $action, $nonce_action, $saved_action, $post ) {
     47                $old_post    = $_POST;
     48                $old_request = $_REQUEST;
     49                $old_server  = $_SERVER;
     50                $old_errors  = bbpress()->errors;
     51                $home_url    = wp_parse_url( home_url( '/' ) );
     52                $saved_ids   = array();
     53                $redirect    = '';
     54                $on_save     = function( $forum_id ) use ( &$saved_ids ) {
     55                        $saved_ids[] = $forum_id;
     56                };
     57                $prevent_redirect = function( $location ) use ( &$redirect ) {
     58                        $redirect = $location;
     59                        throw new RuntimeException( 'Forum form redirect.' );
     60                };
     61
     62                $_SERVER['HTTP_HOST']   = $home_url['host'] . ( isset( $home_url['port'] ) ? ':' . $home_url['port'] : '' );
     63                $_SERVER['REQUEST_URI'] = $home_url['path'];
     64                $_POST                  = $post;
     65                $_REQUEST               = array( '_wpnonce' => wp_create_nonce( $nonce_action ) );
     66                bbpress()->errors       = new WP_Error();
     67                add_action( $saved_action, $on_save );
     68                add_filter( 'wp_redirect', $prevent_redirect );
     69
     70                try {
     71                        call_user_func( $handler, $action );
     72                } catch ( RuntimeException $exception ) {
     73                        if ( 'Forum form redirect.' !== $exception->getMessage() ) {
     74                                throw $exception;
     75                        }
     76                } finally {
     77                        $errors            = bbpress()->errors->get_error_codes();
     78                        $_POST             = $old_post;
     79                        $_REQUEST          = $old_request;
     80                        $_SERVER           = $old_server;
     81                        bbpress()->errors = $old_errors;
     82                        remove_action( $saved_action, $on_save );
     83                        remove_filter( 'wp_redirect', $prevent_redirect );
     84                }
     85
     86                return array( $saved_ids, $redirect, $errors );
    3487        }
    3588
    … …  
    147200        /**
    148201         * @covers ::bbp_new_forum_handler
    149          * @todo   Implement test_bbp_new_forum_handler().
    150202         */
    151203        public function test_bbp_new_forum_handler() {
    152                 // Remove the following lines when you implement this test.
    153                 $this->markTestIncomplete(
    154                         'This test has not been implemented yet.'
     204                $parent_id = $this->factory->forum->create();
     205                $user_id   = $this->factory->user->create();
     206                bbp_set_user_role( $user_id, bbp_get_keymaster_role() );
     207                $this->set_current_user( $user_id );
     208
     209                list( $saved_ids, $redirect, $errors ) = $this->submit_forum_form(
     210                        'bbp_new_forum_handler',
     211                        'bbp-new-forum',
     212                        'bbp-new-forum',
     213                        'bbp_new_forum_post_extras',
     214                        array(
     215                                'bbp_forum_parent_id' => $parent_id,
     216                                'bbp_forum_title'     => 'Front-end new forum',
     217                                'bbp_forum_content'   => 'New forum description',
     218                        )
    155219                );
     220
     221                $this->assertSame( array(), $errors );
     222                $this->assertCount( 1, $saved_ids );
     223                $forum_id = $saved_ids[0];
     224                $this->assertSame( bbp_get_forum_post_type(), get_post_type( $forum_id ) );
     225                $this->assertSame( $parent_id, wp_get_post_parent_id( $forum_id ) );
     226                $this->assertSame( 'Front-end new forum', get_the_title( $forum_id ) );
     227                $this->assertSame( 'New forum description', get_post_field( 'post_content', $forum_id ) );
     228                $this->assertSame( bbp_get_forum_permalink( $forum_id ), $redirect );
     229        }
     230
     231        /**
     232         * @covers ::bbp_new_forum_handler
     233         */
     234        public function test_bbp_new_forum_handler_rejects_invalid_nonce() {
     235                $user_id = $this->factory->user->create();
     236                bbp_set_user_role( $user_id, bbp_get_keymaster_role() );
     237                $this->set_current_user( $user_id );
     238
     239                list( $saved_ids, $redirect, $errors ) = $this->submit_forum_form(
     240                        'bbp_new_forum_handler',
     241                        'bbp-new-forum',
     242                        'invalid-new-forum-action',
     243                        'bbp_new_forum_post_extras',
     244                        array( 'bbp_forum_title' => 'Rejected forum' )
     245                );
     246
     247                $this->assertSame( array(), $saved_ids );
     248                $this->assertSame( '', $redirect );
     249                $this->assertContains( 'bbp_new_forum_nonce', $errors );
    156250        }
    157251
    158252        /**
    159253         * @covers ::bbp_edit_forum_handler
    160          * @todo   Implement test_bbp_edit_forum_handler().
    161254         */
    162255        public function test_bbp_edit_forum_handler() {
    163                 // Remove the following lines when you implement this test.
    164                 $this->markTestIncomplete(
    165                         'This test has not been implemented yet.'
     256                $forum_id = $this->factory->forum->create();
     257                $user_id  = $this->factory->user->create();
     258                bbp_set_user_role( $user_id, bbp_get_keymaster_role() );
     259                $this->set_current_user( $user_id );
     260
     261                list( $saved_ids, $redirect, $errors ) = $this->submit_forum_form(
     262                        'bbp_edit_forum_handler',
     263                        'bbp-edit-forum',
     264                        'bbp-edit-forum_' . $forum_id,
     265                        'bbp_edit_forum_post_extras',
     266                        array(
     267                                'bbp_forum_id'      => $forum_id,
     268                                'bbp_forum_title'   => 'Front-end edited forum',
     269                                'bbp_forum_content' => 'Edited forum description',
     270                        )
    166271                );
     272
     273                $this->assertSame( array(), $errors );
     274                $this->assertSame( array( $forum_id ), $saved_ids );
     275                $this->assertSame( 'Front-end edited forum', get_the_title( $forum_id ) );
     276                $this->assertSame( 'Edited forum description', get_post_field( 'post_content', $forum_id ) );
     277                $this->assertSame( $user_id, (int) get_post_meta( $forum_id, '_edit_last', true ) );
     278                $this->assertSame( bbp_get_forum_permalink( $forum_id ), $redirect );
     279        }
     280
     281        /**
     282         * @covers ::bbp_edit_forum_handler
     283         */
     284        public function test_bbp_edit_forum_handler_rejects_invalid_nonce() {
     285                $forum_id = $this->factory->forum->create( array( 'post_title' => 'Unchanged forum' ) );
     286                $user_id  = $this->factory->user->create();
     287                bbp_set_user_role( $user_id, bbp_get_keymaster_role() );
     288                $this->set_current_user( $user_id );
     289
     290                list( $saved_ids, $redirect, $errors ) = $this->submit_forum_form(
     291                        'bbp_edit_forum_handler',
     292                        'bbp-edit-forum',
     293                        'invalid-edit-forum-action',
     294                        'bbp_edit_forum_post_extras',
     295                        array(
     296                                'bbp_forum_id'    => $forum_id,
     297                                'bbp_forum_title' => 'Rejected edit',
     298                        )
     299                );
     300
     301                $this->assertSame( array(), $saved_ids );
     302                $this->assertSame( '', $redirect );
     303                $this->assertContains( 'bbp_edit_forum_nonce', $errors );
     304                $this->assertSame( 'Unchanged forum', get_the_title( $forum_id ) );
    167305        }
    168306
    169307        /**
    170308         * @covers ::bbp_save_forum_extras
    171          * @todo   Implement test_bbp_save_forum_extras().
    172309         */
    173310        public function test_bbp_save_forum_extras() {
    174                 // Remove the following lines when you implement this test.
    175                 $this->markTestIncomplete(
    176                         'This test has not been implemented yet.'
    177                 );
     311                $forum_id = $this->factory->forum->create();
     312                $user_id  = $this->factory->user->create();
     313                $old_post = $_POST;
     314                bbp_set_user_role( $user_id, bbp_get_keymaster_role() );
     315                $this->set_current_user( $user_id );
     316
     317                try {
     318                        $_POST = array(
     319                                'bbp_forum_status'     => 'closed',
     320                                'bbp_forum_type'       => 'category',
     321                                'bbp_forum_visibility' => bbp_get_hidden_status_id(),
     322                        );
     323                        bbp_save_forum_extras( $forum_id );
     324                        $this->assertTrue( bbp_is_forum_closed( $forum_id ) );
     325                        $this->assertTrue( bbp_is_forum_category( $forum_id ) );
     326                        $this->assertTrue( bbp_is_forum_hidden( $forum_id, false ) );
     327
     328                        $_POST = array(
     329                                'bbp_forum_status'     => 'open',
     330                                'bbp_forum_type'       => 'forum',
     331                                'bbp_forum_visibility' => bbp_get_public_status_id(),
     332                        );
     333                        bbp_save_forum_extras( $forum_id );
     334                        $this->assertTrue( bbp_is_forum_open( $forum_id ) );
     335                        $this->assertFalse( bbp_is_forum_category( $forum_id ) );
     336                        $this->assertTrue( bbp_is_forum_public( $forum_id, false ) );
     337                } finally {
     338                        $_POST = $old_post;
     339                }
    178340        }
    179341
    … …  
    248410        /**
    249411         * @covers ::bbp_check_forum_edit
    250          * @todo   Implement test_bbp_check_forum_edit().
    251412         */
    252413        public function test_bbp_check_forum_edit() {
    253                 // Remove the following lines when you implement this test.
    254                 $this->markTestIncomplete(
    255                         'This test has not been implemented yet.'
    256                 );
     414                $forum_id = $this->factory->forum->create();
     415                $redirect = '';
     416                $this->set_current_user( 0 );
     417
     418                $editing = '__return_true';
     419                $forum_filter = function( $resolved_id, $requested_id ) use ( $forum_id ) {
     420                        return empty( $requested_id ) ? $forum_id : $resolved_id;
     421                };
     422                $prevent_redirect = function( $location ) use ( &$redirect ) {
     423                        $redirect = $location;
     424                        throw new RuntimeException( 'Forum edit redirect.' );
     425                };
     426                add_filter( 'bbp_is_forum_edit', $editing );
     427                add_filter( 'bbp_get_forum_id', $forum_filter, 10, 2 );
     428                add_filter( 'wp_redirect', $prevent_redirect );
     429
     430                try {
     431                        bbp_check_forum_edit();
     432                        $this->fail( 'An unauthorized forum edit should redirect.' );
     433                } catch ( RuntimeException $exception ) {
     434                        $this->assertSame( 'Forum edit redirect.', $exception->getMessage() );
     435                } finally {
     436                        remove_filter( 'bbp_is_forum_edit', $editing );
     437                        remove_filter( 'bbp_get_forum_id', $forum_filter );
     438                        remove_filter( 'wp_redirect', $prevent_redirect );
     439                }
     440
     441                $this->assertSame( bbp_get_forum_permalink( $forum_id ), $redirect );
    257442        }
    258443        /**
  • trunk/tests/phpunit/testcases/forums/functions/query.php

    r7467 r7777  
    1212        /**
    1313         * @covers ::bbp_exclude_forum_ids
    14          * @todo   Implement test_bbp_exclude_forum_ids().
    1514         */
    1615        public function test_bbp_exclude_forum_ids() {
    17                 // Remove the following lines when you implement this test.
    18                 $this->markTestIncomplete(
    19                         'This test has not been implemented yet.'
    20                 );
     16                $hidden_id  = $this->factory->forum->create();
     17                $private_id = $this->factory->forum->create();
     18                bbp_hide_forum( $hidden_id );
     19                bbp_privatize_forum( $private_id );
     20                $this->set_current_user( 0 );
     21
     22                $excluded = bbp_exclude_forum_ids( 'array' );
     23                $this->assertEqualSets( array( $hidden_id, $private_id ), $excluded );
     24                $this->assertEqualSets( $excluded, wp_parse_id_list( bbp_exclude_forum_ids() ) );
     25                $this->assertSame( array(
     26                        'key'     => '_bbp_forum_id',
     27                        'value'   => $excluded,
     28                        'type'    => 'NUMERIC',
     29                        'compare' => 'NOT IN',
     30                ), bbp_exclude_forum_ids( 'meta_query' ) );
     31
     32                $keymaster_id = $this->factory->user->create();
     33                bbp_set_user_role( $keymaster_id, bbp_get_keymaster_role() );
     34                $this->set_current_user( $keymaster_id );
     35                $this->assertSame( array(), bbp_exclude_forum_ids( 'array' ) );
     36                $this->assertSame( '', bbp_exclude_forum_ids() );
     37                $this->assertSame( array(), bbp_exclude_forum_ids( 'meta_query' ) );
    2138        }
    2239
  • trunk/tests/phpunit/testcases/forums/functions/visibility.php

    r7719 r7777  
    99 */
    1010class BBP_Tests_Forums_Functions_Visibility extends BBP_UnitTestCase {
     11
     12        /**
     13         * Exercise a direct-URL visibility guard for a forum and its content.
     14         *
     15         * @param string $listener   Visibility guard callback.
     16         * @param string $visibility Restricted forum visibility.
     17         */
     18        protected function assert_forum_visibility_guard( $listener, $visibility ) {
     19                global $wp_query;
     20
     21                $posts    = $this->create_visibility_test_posts();
     22                $previous = $wp_query;
     23                $this->set_current_user( 0 );
     24
     25                try {
     26                        foreach ( array( 'forum', 'topic', 'reply' ) as $type ) {
     27                                $post_id = $posts[ "{$visibility}_{$type}" ];
     28                                $query   = new WP_Query();
     29                                $query->post        = get_post( $post_id );
     30                                $query->is_singular = true;
     31                                $query->set( 'post_type', get_post_type( $post_id ) );
     32                                $wp_query = $query;
     33
     34                                call_user_func( $listener );
     35                                $this->assertTrue( $query->is_404(), "{$type} in {$visibility} forum was not denied" );
     36                        }
     37
     38                        $query = new WP_Query();
     39                        $query->post        = get_post( $posts['public_forum'] );
     40                        $query->is_singular = true;
     41                        $query->set( 'post_type', bbp_get_forum_post_type() );
     42                        $wp_query = $query;
     43
     44                        call_user_func( $listener );
     45                        $this->assertFalse( $query->is_404() );
     46                } finally {
     47                        $wp_query = $previous;
     48                }
     49        }
    1150
    1251        /**
    … …  
    10531092        /**
    10541093         * @covers ::bbp_forum_enforce_hidden
    1055          * @todo   Implement test_bbp_forum_enforce_hidden().
    10561094         */
    10571095        public function test_bbp_forum_enforce_hidden() {
    1058                 // Remove the following lines when you implement this test.
    1059                 $this->markTestIncomplete(
    1060                         'This test has not been implemented yet.'
    1061                 );
     1096                $this->assert_forum_visibility_guard( 'bbp_forum_enforce_hidden', 'hidden' );
    10621097        }
    10631098
    10641099        /**
    10651100         * @covers ::bbp_forum_enforce_private
    1066          * @todo   Implement test_bbp_forum_enforce_private().
    10671101         */
    10681102        public function test_bbp_forum_enforce_private() {
    1069                 // Remove the following lines when you implement this test.
    1070                 $this->markTestIncomplete(
    1071                         'This test has not been implemented yet.'
    1072                 );
     1103                $this->assert_forum_visibility_guard( 'bbp_forum_enforce_private', 'private' );
    10731104        }
    10741105}
Note: See TracChangeset for help on using the changeset viewer.