Skip to:
Content

bbPress.org

Changeset 7772


Ignore:
Timestamp:
10/03/2026 02:10:51 AM (6 weeks ago)
Author:
johnjamesjacoby
Message:

Forums: Correct form moderator and dropdown values.

Restore assigned moderator nicenames in forum edit forms, explicitly select the forum type for new forums, and add a visibility dropdown filter. Preserve the existing type dropdown filter call for compatibility. Complete the forum-form template tests and add regressions for these defects.

In trunk, for 2.7.

See #3697.
See #3695.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/CHANGELOG.md

    r7768 r7772  
    4545### Fixed
    4646
     47- Restored assigned moderator names in forum edit forms and corrected forum type and visibility dropdown behavior.
    4748- Kept closed category ancestors from being ignored by forum status checks.
    4849- Preserved topic subscriptions when topics and replies are edited in administration.
  • trunk/src/includes/forums/template.php

    r7768 r7772  
    24082408                                // Comma separate user nicenames
    24092409                                if ( ! empty( $user_nicenames ) ) {
    2410                                         $forum_mods = implode( ', ', wp_list_pluck( $user_nicenames, 'user_nicename' ) );
     2410                                        $forum_mods = implode( ', ', $user_nicenames );
    24112411                                }
    24122412                        }
    … …  
    26292629                        $r['selected'] = bbp_get_forum_type( $r['forum_id'] );
    26302630
    2631                 // New topic
     2631                // New forum
    26322632                } else {
    2633                         $r['selected'] = bbp_get_public_status_id();
     2633                        $r['selected'] = 'forum';
    26342634                }
    26352635        }
    … …  
    28242824        <?php
    28252825
    2826         // Filter & return
    2827         return apply_filters( 'bbp_get_form_forum_type_dropdown', ob_get_clean(), $r, $args );
     2826        // Preserve the existing type filter for visibility dropdowns.
     2827        $html = apply_filters( 'bbp_get_form_forum_type_dropdown', ob_get_clean(), $r, $args );
     2828
     2829        // Filter & return
     2830        return apply_filters( 'bbp_get_form_forum_visibility_dropdown', $html, $r, $args );
    28282831}
    28292832
  • trunk/tests/phpunit/testcases/forums/template/forms.php

    r7461 r7772  
    1010class BBP_Tests_Forums_Template_Forms extends BBP_UnitTestCase {
    1111
     12        private function with_forum_edit( $forum_id, $callback ) {
     13                $old_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
     14                $old_forum_id = bbpress()->current_forum_id;
     15                $GLOBALS['post'] = get_post( $forum_id );
     16                bbpress()->current_forum_id = $forum_id;
     17                setup_postdata( $GLOBALS['post'] );
     18                add_filter( 'bbp_is_forum_edit', '__return_true' );
     19
     20                try {
     21                        $callback();
     22                } finally {
     23                        remove_filter( 'bbp_is_forum_edit', '__return_true' );
     24                        bbpress()->current_forum_id = $old_forum_id;
     25                        $GLOBALS['post'] = $old_post;
     26                        wp_reset_postdata();
     27                }
     28        }
     29
     30        private function with_forum_post( $action, $values, $callback ) {
     31                $old_method  = $_SERVER['REQUEST_METHOD'];
     32                $old_post    = $_POST;
     33                $old_request = $_REQUEST;
     34                $_SERVER['REQUEST_METHOD'] = 'POST';
     35                $_POST = $values;
     36                $_REQUEST = array_merge( $old_request, $values, array( '_wpnonce' => wp_create_nonce( $action ) ) );
     37
     38                try {
     39                        $callback();
     40                } finally {
     41                        $_SERVER['REQUEST_METHOD'] = $old_method;
     42                        $_POST    = $old_post;
     43                        $_REQUEST = $old_request;
     44                }
     45        }
     46
    1247        /**
    1348         * @covers ::bbp_form_forum_title
    … …  
    3570
    3671        /**
     72         * @covers ::bbp_form_forum_title
     73         * @covers ::bbp_get_form_forum_title
     74         */
     75        public function test_bbp_get_form_forum_title_from_post() {
     76                $this->assertSame( '', bbp_get_form_forum_title() );
     77
     78                $this->with_forum_post( 'bbp-new-forum', array( 'bbp_forum_title' => 'Posted forum' ), function() {
     79                        $this->assertSame( 'Posted forum', bbp_get_form_forum_title() );
     80                        $this->expectOutputString( 'Posted forum' );
     81                        bbp_form_forum_title();
     82                } );
     83        }
     84
     85        /**
    3786         * @covers ::bbp_form_forum_content
    3887         * @covers ::bbp_get_form_forum_content
    39          * @todo   Implement test_bbp_form_forum_content().
    40          * @todo   Implement test_bbp_get_form_forum_content().
    4188         */
    4289        public function test_bbp_get_form_forum_content() {
    43                 // Remove the following lines when you implement this test.
    44                 $this->markTestIncomplete(
    45                         'This test has not been implemented yet.'
    46                 );
     90                $this->assertSame( '', bbp_get_form_forum_content() );
     91
     92                $forum_id = $this->factory->forum->create( array( 'post_content' => 'Saved forum content' ) );
     93                $this->with_forum_edit( $forum_id, function() {
     94                        $this->assertSame( 'Saved forum content', bbp_get_form_forum_content() );
     95                } );
     96
     97                $this->with_forum_post( 'bbp-new-forum', array( 'bbp_forum_content' => 'Submitted \\"content\\"' ), function() {
     98                        $this->assertSame( 'Submitted &quot;content&quot;', bbp_get_form_forum_content() );
     99                        $this->expectOutputString( 'Submitted &quot;content&quot;' );
     100                        bbp_form_forum_content();
     101                } );
     102        }
     103
     104        /**
     105         * @covers ::bbp_form_forum_moderators
     106         * @covers ::bbp_get_form_forum_moderators
     107         */
     108        public function test_bbp_get_form_forum_moderators() {
     109                $this->assertSame( '', bbp_get_form_forum_moderators() );
     110
     111                $user_id  = $this->factory->user->create();
     112                $forum_id = $this->factory->forum->create();
     113                bbp_add_moderator( $forum_id, $user_id );
     114                $this->with_forum_edit( $forum_id, function() use ( $user_id, $forum_id ) {
     115                        $this->assertSame( $forum_id, get_the_ID() );
     116                        $this->assertContains( $user_id, bbp_get_moderator_ids( $forum_id ) );
     117                        $this->assertSame( get_userdata( $user_id )->user_nicename, bbp_get_form_forum_moderators() );
     118                } );
     119
     120                $this->with_forum_post( 'bbp-new-forum', array( 'bbp_moderators' => 'posted-moderator' ), function() {
     121                        $this->assertSame( 'posted-moderator', bbp_get_form_forum_moderators() );
     122                        $this->expectOutputString( 'posted-moderator' );
     123                        bbp_form_forum_moderators();
     124                } );
    47125        }
    48126
    … …  
    50128         * @covers ::bbp_form_forum_parent
    51129         * @covers ::bbp_get_form_forum_parent
    52          * @todo   Implement test_bbp_form_forum_parent().
    53          * @todo   Implement test_bbp_get_form_forum_parent().
    54130         */
    55131        public function test_bbp_get_form_forum_parent() {
    56                 // Remove the following lines when you implement this test.
    57                 $this->markTestIncomplete(
    58                         'This test has not been implemented yet.'
    59                 );
     132                $this->assertSame( 0, bbp_get_form_forum_parent() );
     133
     134                $parent_id = $this->factory->forum->create();
     135                $forum_id = $this->factory->forum->create( array( 'post_parent' => $parent_id ) );
     136                $this->with_forum_edit( $forum_id, function() use ( $parent_id ) {
     137                        $this->assertSame( $parent_id, bbp_get_form_forum_parent() );
     138                } );
     139
     140                $this->with_forum_post( 'bbp-new-forum', array( 'bbp_forum_id' => (string) $parent_id ), function() use ( $parent_id ) {
     141                        $this->assertSame( $parent_id, bbp_get_form_forum_parent() );
     142                        $this->expectOutputString( (string) $parent_id );
     143                        bbp_form_forum_parent();
     144                } );
    60145        }
    61146
    … …  
    63148         * @covers ::bbp_form_forum_type
    64149         * @covers ::bbp_get_form_forum_type
    65          * @todo   Implement test_bbp_form_forum_type().
    66          * @todo   Implement test_bbp_get_form_forum_type().
    67150         */
    68151        public function test_bbp_get_form_forum_type() {
    69                 // Remove the following lines when you implement this test.
    70                 $this->markTestIncomplete(
    71                         'This test has not been implemented yet.'
    72                 );
     152                $this->assertSame( 'forum', bbp_get_form_forum_type() );
     153
     154                $forum_id = $this->factory->forum->create();
     155                bbp_categorize_forum( $forum_id );
     156                $this->with_forum_edit( $forum_id, function() {
     157                        $this->assertSame( 'category', bbp_get_form_forum_type() );
     158                } );
     159
     160                $this->with_forum_post( 'bbp-new-forum', array( 'bbp_forum_type' => 'CATEGORY' ), function() {
     161                        $this->assertSame( 'category', bbp_get_form_forum_type() );
     162                        $this->expectOutputString( 'category' );
     163                        bbp_form_forum_type();
     164                } );
    73165        }
    74166
    … …  
    76168         * @covers ::bbp_form_forum_visibility
    77169         * @covers ::bbp_get_form_forum_visibility
    78          * @todo   Implement test_bbp_form_forum_visibility().
    79          * @todo   Implement test_bbp_get_form_forum_visibility().
    80170         */
    81171        public function test_bbp_get_form_forum_visibility() {
    82                 // Remove the following lines when you implement this test.
    83                 $this->markTestIncomplete(
    84                         'This test has not been implemented yet.'
    85                 );
     172                $this->assertSame( bbp_get_public_status_id(), bbp_get_form_forum_visibility() );
     173
     174                $forum_id = $this->factory->forum->create();
     175                bbp_hide_forum( $forum_id );
     176                $this->with_forum_edit( $forum_id, function() {
     177                        $this->assertSame( bbp_get_hidden_status_id(), bbp_get_form_forum_visibility() );
     178                } );
     179
     180                $this->with_forum_post( 'bbp-new-forum', array( 'bbp_forum_visibility' => 'PRIVATE' ), function() {
     181                        $this->assertSame( bbp_get_private_status_id(), bbp_get_form_forum_visibility() );
     182                        $this->expectOutputString( bbp_get_private_status_id() );
     183                        bbp_form_forum_visibility();
     184                } );
    86185        }
    87186
    … …  
    89188         * @covers ::bbp_form_forum_subscribed
    90189         * @covers ::bbp_get_form_forum_subscribed
    91          * @todo   Implement test_bbp_form_forum_subscribed().
    92          * @todo   Implement test_bbp_get_form_forum_subscribed().
    93190         */
    94191        public function test_bbp_get_form_forum_subscribed() {
    95                 // Remove the following lines when you implement this test.
    96                 $this->markTestIncomplete(
    97                         'This test has not been implemented yet.'
    98                 );
     192                $this->assertSame( '', bbp_get_form_forum_subscribed() );
     193
     194                $user_id  = $this->factory->user->create();
     195                $forum_id = $this->factory->forum->create( array( 'post_author' => $user_id ) );
     196                bbp_add_user_forum_subscription( $user_id, $forum_id );
     197                $this->with_forum_edit( $forum_id, function() {
     198                        $this->assertSame( checked( true, true, false ), bbp_get_form_forum_subscribed() );
     199                } );
     200
     201                $old_user = get_current_user_id();
     202                $old_forum_id = bbpress()->current_forum_id;
     203                $this->set_current_user( $user_id );
     204                bbpress()->current_forum_id = $forum_id;
     205                add_filter( 'bbp_is_single_forum', '__return_true' );
     206                try {
     207                        $this->assertSame( checked( true, true, false ), bbp_get_form_forum_subscribed() );
     208                } finally {
     209                        remove_filter( 'bbp_is_single_forum', '__return_true' );
     210                        bbpress()->current_forum_id = $old_forum_id;
     211                        $this->set_current_user( $old_user );
     212                }
     213
     214                $this->with_forum_post( 'bbp-new-forum', array( 'bbp_forum_subscription' => '1' ), function() {
     215                        $checked = checked( true, true, false );
     216                        $this->assertSame( $checked, bbp_get_form_forum_subscribed() );
     217                        $this->expectOutputString( $checked );
     218                        bbp_form_forum_subscribed();
     219                } );
    99220        }
    100221
    … …  
    102223         * @covers ::bbp_form_forum_type_dropdown
    103224         * @covers ::bbp_get_form_forum_type_dropdown
    104          * @todo   Implement test_bbp_form_forum_type_dropdown().
    105          * @todo   Implement test_bbp_get_form_forum_type_dropdown().
    106225         */
    107226        public function test_bbp_get_form_forum_type_dropdown() {
    108                 // Remove the following lines when you implement this test.
    109                 $this->markTestIncomplete(
    110                         'This test has not been implemented yet.'
     227                $args = array( 'select_id' => 'custom_forum_type', 'selected' => 'category' );
     228                $html = bbp_get_form_forum_type_dropdown( $args );
     229                $this->assertStringContainsString( 'name="custom_forum_type"', $html );
     230                $this->assertStringContainsString( 'id="custom_forum_type_select"', $html );
     231                $this->assertMatchesRegularExpression( '/<option value="category" selected=/', $html );
     232                $this->assertStringContainsString( '<option value="forum"', $html );
     233                $this->expectOutputString( $html );
     234                bbp_form_forum_type_dropdown( $args );
     235        }
     236
     237        /**
     238         * @covers ::bbp_get_form_forum_type_dropdown
     239         */
     240        public function test_bbp_get_form_forum_type_dropdown_selects_forum_by_default() {
     241                $this->assertMatchesRegularExpression(
     242                        '/<option value="forum" selected=/',
     243                        bbp_get_form_forum_type_dropdown()
    111244                );
    112245        }
    … …  
    115248         * @covers ::bbp_form_forum_status_dropdown
    116249         * @covers ::bbp_get_form_forum_status_dropdown
    117          * @todo   Implement test_bbp_form_forum_status_dropdown().
    118          * @todo   Implement test_bbp_get_form_forum_status_dropdown().
    119250         */
    120251        public function test_bbp_get_form_forum_status_dropdown() {
    121                 // Remove the following lines when you implement this test.
    122                 $this->markTestIncomplete(
    123                         'This test has not been implemented yet.'
    124                 );
     252                $args = array( 'select_id' => 'custom_forum_status', 'selected' => 'closed' );
     253                $html = bbp_get_form_forum_status_dropdown( $args );
     254                $this->assertStringContainsString( 'name="custom_forum_status"', $html );
     255                $this->assertMatchesRegularExpression( '/<option value="closed" selected=/', $html );
     256                $this->assertStringContainsString( '<option value="open"', $html );
     257                $this->expectOutputString( $html );
     258                bbp_form_forum_status_dropdown( $args );
    125259        }
    126260
    … …  
    128262         * @covers ::bbp_form_forum_visibility_dropdown
    129263         * @covers ::bbp_get_form_forum_visibility_dropdown
    130          * @todo   Implement test_bbp_form_forum_visibility_dropdown().
    131          * @todo   Implement test_bbp_get_form_forum_visibility_dropdown().
    132264         */
    133265        public function test_bbp_get_form_forum_visibility_dropdown() {
    134                 // Remove the following lines when you implement this test.
    135                 $this->markTestIncomplete(
    136                         'This test has not been implemented yet.'
    137                 );
     266                $args = array( 'select_id' => 'custom_forum_visibility', 'selected' => bbp_get_private_status_id() );
     267                $html = bbp_get_form_forum_visibility_dropdown( $args );
     268                $this->assertStringContainsString( 'name="custom_forum_visibility"', $html );
     269                $this->assertMatchesRegularExpression( '/<option value="private" selected=/', $html );
     270                $this->assertStringContainsString( '<option value="publish"', $html );
     271                $this->assertStringContainsString( '<option value="hidden"', $html );
     272                $this->expectOutputString( $html );
     273                bbp_form_forum_visibility_dropdown( $args );
     274        }
     275
     276        /**
     277         * @covers ::bbp_get_form_forum_visibility_dropdown
     278         */
     279        public function test_bbp_get_form_forum_visibility_dropdown_has_distinct_filter() {
     280                $filter = function( $html ) {
     281                        return 'filtered visibility';
     282                };
     283                add_filter( 'bbp_get_form_forum_visibility_dropdown', $filter );
     284                try {
     285                        $this->assertSame( 'filtered visibility', bbp_get_form_forum_visibility_dropdown() );
     286                } finally {
     287                        remove_filter( 'bbp_get_form_forum_visibility_dropdown', $filter );
     288                }
     289        }
     290
     291        /**
     292         * @covers ::bbp_get_form_forum_visibility_dropdown
     293         */
     294        public function test_bbp_get_form_forum_visibility_dropdown_preserves_type_filter() {
     295                $filter = function( $html, $parsed_args, $args ) {
     296                        $this->assertSame( 'bbp_forum_visibility', $parsed_args['select_id'] );
     297                        $this->assertSame( array(), $args );
     298                        return 'filtered by type hook';
     299                };
     300                add_filter( 'bbp_get_form_forum_type_dropdown', $filter, 10, 3 );
     301                try {
     302                        $this->assertSame( 'filtered by type hook', bbp_get_form_forum_visibility_dropdown() );
     303                } finally {
     304                        remove_filter( 'bbp_get_form_forum_type_dropdown', $filter );
     305                }
    138306        }
    139307
    140308        /**
    141309         * @covers ::bbp_is_forum_form_post_request
    142          * @todo   Implement test_bbp_is_forum_form_post_request().
    143310         */
    144311        public function test_bbp_is_forum_form_post_request() {
    145                 // Remove the following lines when you implement this test.
    146                 $this->markTestIncomplete(
    147                         'This test has not been implemented yet.'
    148                 );
     312                $this->assertFalse( bbp_is_forum_form_post_request() );
     313
     314                $this->with_forum_post( 'bbp-new-forum', array(), function() {
     315                        $this->assertTrue( bbp_is_forum_form_post_request() );
     316
     317                        $nonce = $_REQUEST['_wpnonce'];
     318                        $_REQUEST['_wpnonce'] = 'invalid';
     319                        $this->assertFalse( bbp_is_forum_form_post_request() );
     320                        $_REQUEST['_wpnonce'] = $nonce;
     321
     322                        $_SERVER['REQUEST_METHOD'] = 'GET';
     323                        $this->assertFalse( bbp_is_forum_form_post_request() );
     324                } );
     325
     326                $forum_id = $this->factory->forum->create();
     327                $this->with_forum_edit( $forum_id, function() use ( $forum_id ) {
     328                        $this->with_forum_post( 'bbp-edit-forum_' . $forum_id, array(), function() {
     329                                $this->assertTrue( bbp_is_forum_form_post_request() );
     330                        } );
     331                } );
    149332        }
    150333}
Note: See TracChangeset for help on using the changeset viewer.