Skip to:
Content

bbPress.org

Changeset 7811


Ignore:
Timestamp:
10/04/2026 03:18:35 PM (5 weeks ago)
Author:
johnjamesjacoby
Message:

Tests: Cover topic submission and edit handlers.

Replace incomplete stubs with checks for topic creation, editing, permission denials, moderation toggles, and edit redirects. Remove merge and split placeholders already covered by dedicated permission tests.

In trunk, for 2.7.

See #3695.

File:
1 edited

Legend:

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

    r7802 r7811  
    8686        /**
    8787         * @covers ::bbp_new_topic_handler
    88          * @todo   Implement test_bbp_new_topic_handler().
    8988         */
    9089        public function test_bbp_new_topic_handler() {
    91                 // Remove the following lines when you implement this test.
    92                 $this->markTestIncomplete(
    93                         'This test has not been implemented yet.'
    94                 );
     90                $forum_id         = $this->factory->forum->create();
     91                $old_post         = $_POST;
     92                $old_request      = $_REQUEST;
     93                $old_server       = $_SERVER;
     94                $old_user         = get_current_user_id();
     95                $old_errors       = bbpress()->errors;
     96                $created_topic_id = 0;
     97                $redirect         = null;
     98                $record_topic     = function ( $topic_id ) use ( &$created_topic_id ) {
     99                        $created_topic_id = $topic_id;
     100                };
     101                $prevent_redirect = function ( $location ) use ( &$redirect ) {
     102                        $redirect = $location;
     103                        throw new RuntimeException( 'New topic redirect.' );
     104                };
     105                $home_url = wp_parse_url( home_url( '/' ) );
     106
     107                $_POST                   = array();
     108                $_REQUEST                = array();
     109                $_SERVER['HTTP_HOST']    = $home_url['host'] . ( isset( $home_url['port'] ) ? ':' . $home_url['port'] : '' );
     110                $_SERVER['REQUEST_URI']  = $home_url['path'];
     111                bbpress()->errors       = new WP_Error();
     112                add_action( 'bbp_new_topic', $record_topic );
     113                add_filter( 'wp_redirect', $prevent_redirect );
     114
     115                try {
     116                        $this->assertNull( bbp_new_topic_handler( 'invalid-action' ) );
     117                        $this->assertNull( bbp_new_topic_handler( 'bbp-new-topic' ) );
     118                        $this->assertContains( 'bbp_new_topic_nonce', bbpress()->errors->get_error_codes() );
     119
     120                        $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
     121                        bbp_set_user_role( $admin_id, bbp_get_keymaster_role() );
     122                        $this->set_current_user( $admin_id );
     123                        bbpress()->errors = new WP_Error();
     124                        $_POST = array(
     125                                'bbp_forum_id'      => $forum_id,
     126                                'bbp_topic_title'   => 'New topic',
     127                                'bbp_topic_content' => 'Topic content',
     128                        );
     129                        $_REQUEST['_wpnonce'] = wp_create_nonce( 'bbp-new-topic' );
     130
     131                        try {
     132                                bbp_new_topic_handler( 'bbp-new-topic' );
     133                                $this->fail( 'A new topic should redirect.' );
     134                        } catch ( RuntimeException $exception ) {
     135                                if ( 'New topic redirect.' !== $exception->getMessage() ) {
     136                                        throw $exception;
     137                                }
     138                        }
     139
     140                        $this->assertSame( array(), bbpress()->errors->get_error_codes() );
     141                        $this->assertGreaterThan( 0, $created_topic_id );
     142                        $this->assertSame( $forum_id, wp_get_post_parent_id( $created_topic_id ) );
     143                        $this->assertSame( $admin_id, (int) get_post_field( 'post_author', $created_topic_id ) );
     144                        $this->assertSame( 'New topic', get_post_field( 'post_title', $created_topic_id ) );
     145                        $this->assertSame( 'Topic content', get_post_field( 'post_content', $created_topic_id ) );
     146                        $this->assertSame( bbp_get_topic_permalink( $created_topic_id ), $redirect );
     147                } finally {
     148                        remove_filter( 'wp_redirect', $prevent_redirect );
     149                        remove_action( 'bbp_new_topic', $record_topic );
     150                        $_POST            = $old_post;
     151                        $_REQUEST         = $old_request;
     152                        $_SERVER          = $old_server;
     153                        bbpress()->errors = $old_errors;
     154                        $this->set_current_user( $old_user );
     155                }
    95156        }
    96157
    97158        /**
    98159         * @covers ::bbp_edit_topic_handler
    99          * @todo   Implement test_bbp_edit_topic_handler().
    100160         */
    101161        public function test_bbp_edit_topic_handler() {
    102                 // Remove the following lines when you implement this test.
    103                 $this->markTestIncomplete(
    104                         'This test has not been implemented yet.'
    105                 );
     162                $forum_id         = $this->factory->forum->create();
     163                $admin_id         = $this->factory->user->create( array( 'role' => 'administrator' ) );
     164                $topic_id         = $this->factory->topic->create( array(
     165                        'post_author' => $admin_id,
     166                        'post_parent' => $forum_id,
     167                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     168                ) );
     169                $old_post         = $_POST;
     170                $old_request      = $_REQUEST;
     171                $old_server       = $_SERVER;
     172                $old_user         = get_current_user_id();
     173                $old_errors       = bbpress()->errors;
     174                $redirect         = null;
     175                $edited_topic_id  = 0;
     176                $record_topic     = function ( $edited_id ) use ( &$edited_topic_id ) {
     177                        $edited_topic_id = $edited_id;
     178                };
     179                $prevent_redirect = function ( $location ) use ( &$redirect ) {
     180                        $redirect = $location;
     181                        throw new RuntimeException( 'Edit topic redirect.' );
     182                };
     183                $home_url = wp_parse_url( home_url( '/' ) );
     184
     185                $_POST                   = array();
     186                $_REQUEST                = array();
     187                $_SERVER['HTTP_HOST']    = $home_url['host'] . ( isset( $home_url['port'] ) ? ':' . $home_url['port'] : '' );
     188                $_SERVER['REQUEST_URI']  = $home_url['path'];
     189                bbpress()->errors       = new WP_Error();
     190                add_action( 'bbp_edit_topic', $record_topic );
     191                add_filter( 'wp_redirect', $prevent_redirect );
     192
     193                try {
     194                        $this->assertNull( bbp_edit_topic_handler( 'invalid-action' ) );
     195                        $this->assertNull( bbp_edit_topic_handler( 'bbp-edit-topic' ) );
     196                        $this->assertContains( 'bbp_edit_topic_id', bbpress()->errors->get_error_codes() );
     197
     198                        $_POST['bbp_topic_id'] = $topic_id;
     199                        $this->set_current_user( 0 );
     200                        bbpress()->errors = new WP_Error();
     201                        $this->assertNull( bbp_edit_topic_handler( 'bbp-edit-topic' ) );
     202                        $this->assertContains( 'bbp_edit_topic_permission', bbpress()->errors->get_error_codes() );
     203
     204                        bbp_set_user_role( $admin_id, bbp_get_keymaster_role() );
     205                        $this->set_current_user( $admin_id );
     206                        bbpress()->errors = new WP_Error();
     207                        $_POST = array(
     208                                'bbp_topic_id'      => $topic_id,
     209                                'bbp_forum_id'      => $forum_id,
     210                                'bbp_topic_title'   => 'Edited topic',
     211                                'bbp_topic_content' => 'Edited content',
     212                        );
     213                        $_REQUEST['_wpnonce'] = wp_create_nonce( 'bbp-edit-topic_' . $topic_id );
     214
     215                        try {
     216                                bbp_edit_topic_handler( 'bbp-edit-topic' );
     217                                $this->fail( 'Editing a topic should redirect.' );
     218                        } catch ( RuntimeException $exception ) {
     219                                if ( 'Edit topic redirect.' !== $exception->getMessage() ) {
     220                                        throw $exception;
     221                                }
     222                        }
     223
     224                        $this->assertSame( array(), bbpress()->errors->get_error_codes() );
     225                        $this->assertSame( $topic_id, $edited_topic_id );
     226                        $this->assertSame( $forum_id, wp_get_post_parent_id( $topic_id ) );
     227                        $this->assertSame( 'Edited topic', get_post_field( 'post_title', $topic_id ) );
     228                        $this->assertSame( 'Edited content', get_post_field( 'post_content', $topic_id ) );
     229                        $this->assertSame( bbp_get_topic_permalink( $topic_id ), $redirect );
     230                } finally {
     231                        remove_filter( 'wp_redirect', $prevent_redirect );
     232                        remove_action( 'bbp_edit_topic', $record_topic );
     233                        $_POST            = $old_post;
     234                        $_REQUEST         = $old_request;
     235                        $_SERVER          = $old_server;
     236                        bbpress()->errors = $old_errors;
     237                        $this->set_current_user( $old_user );
     238                }
    106239        }
    107240
    … …  
    297430
    298431        /**
    299          * @covers ::bbp_merge_topic_handler
    300          * @todo   Implement test_bbp_merge_topic_handler().
    301          */
    302         public function test_bbp_merge_topic_handler() {
    303                 // Remove the following lines when you implement this test.
    304                 $this->markTestIncomplete(
    305                         'This test has not been implemented yet.'
    306                 );
    307         }
    308 
    309         /**
    310432         * @covers ::bbp_merge_topic_count
    311433         */
    … …  
    366488
    367489        /**
    368          * @covers ::bbp_split_topic_handler
    369          * @todo   Implement test_bbp_split_topic_handler().
    370          */
    371         public function test_bbp_split_topic_handler() {
    372                 // Remove the following lines when you implement this test.
    373                 $this->markTestIncomplete(
    374                         'This test has not been implemented yet.'
    375                 );
    376         }
    377 
    378         /**
    379490         * @covers ::bbp_split_topic_count
    380491         */
    … …  
    498609        /**
    499610         * @covers ::bbp_toggle_topic_handler
    500          * @todo   Implement test_bbp_toggle_topic_handler().
    501611         */
    502612        public function test_bbp_toggle_topic_handler() {
    503                 // Remove the following lines when you implement this test.
    504                 $this->markTestIncomplete(
    505                         'This test has not been implemented yet.'
    506                 );
     613                $forum_id         = $this->factory->forum->create();
     614                $topic_id         = $this->factory->topic->create( array( 'post_parent' => $forum_id ) );
     615                $old_get          = $_GET;
     616                $old_request      = $_REQUEST;
     617                $old_user         = get_current_user_id();
     618                $old_errors       = bbpress()->errors;
     619                $redirect         = null;
     620                $seen             = array();
     621                $prevent_redirect = function ( $location ) use ( &$redirect ) {
     622                        $redirect = $location;
     623                        throw new RuntimeException( 'Topic toggle redirect.' );
     624                };
     625                $record_toggle = function ( $status, $data, $action ) use ( &$seen ) {
     626                        $seen[] = array( $status, $data, $action );
     627                };
     628
     629                $_GET               = array();
     630                $_REQUEST           = array();
     631                bbpress()->errors   = new WP_Error();
     632                add_filter( 'wp_redirect', $prevent_redirect );
     633                add_action( 'bbp_toggle_topic_handler', $record_toggle, 10, 3 );
     634
     635                try {
     636                        $this->assertNull( bbp_toggle_topic_handler( 'bbp_toggle_topic_close' ) );
     637                        $_GET['topic_id'] = $topic_id;
     638                        $this->assertNull( bbp_toggle_topic_handler( 'invalid-action' ) );
     639                        $this->assertTrue( bbp_is_topic_open( $topic_id ) );
     640
     641                        $_GET['topic_id'] = 999999;
     642                        bbp_toggle_topic_handler( 'bbp_toggle_topic_close' );
     643                        $this->assertContains( 'bbp_toggle_topic_missing', bbpress()->errors->get_error_codes() );
     644
     645                        $_GET['topic_id'] = $topic_id;
     646                        $this->set_current_user( 0 );
     647                        bbp_toggle_topic_handler( 'bbp_toggle_topic_close' );
     648                        $this->assertContains( 'bbp_toggle_topic_permission', bbpress()->errors->get_error_codes() );
     649                        $this->assertTrue( bbp_is_topic_open( $topic_id ) );
     650
     651                        $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
     652                        bbp_set_user_role( $admin_id, bbp_get_keymaster_role() );
     653                        $this->set_current_user( $admin_id );
     654                        $_REQUEST['_wpnonce'] = wp_create_nonce( 'close-' . bbp_get_topic_post_type() . '_' . $topic_id );
     655                        try {
     656                                bbp_toggle_topic_handler( 'bbp_toggle_topic_close' );
     657                                $this->fail( 'Closing the topic should redirect.' );
     658                        } catch ( RuntimeException $exception ) {
     659                                if ( 'Topic toggle redirect.' !== $exception->getMessage() ) {
     660                                        throw $exception;
     661                                }
     662                        }
     663                        $this->assertTrue( bbp_is_topic_closed( $topic_id ) );
     664                        $this->assertSame( bbp_get_topic_permalink( $topic_id ), $redirect );
     665                        $this->assertCount( 1, $seen );
     666                        $this->assertSame( $topic_id, $seen[0][1]['ID'] );
     667                        $this->assertSame( 'bbp_toggle_topic_close', $seen[0][2] );
     668                } finally {
     669                        remove_action( 'bbp_toggle_topic_handler', $record_toggle, 10 );
     670                        remove_filter( 'wp_redirect', $prevent_redirect );
     671                        $_GET             = $old_get;
     672                        $_REQUEST         = $old_request;
     673                        bbpress()->errors = $old_errors;
     674                        $this->set_current_user( $old_user );
     675                }
    507676        }
    508677
    … …  
    772941        /**
    773942         * @covers ::bbp_check_topic_edit
    774          * @todo   Implement test_bbp_check_topic_edit().
    775943         */
    776944        public function test_bbp_check_topic_edit() {
    777                 // Remove the following lines when you implement this test.
    778                 $this->markTestIncomplete(
    779                         'This test has not been implemented yet.'
    780                 );
     945                $topic_id         = $this->factory->topic->create();
     946                $old_user         = get_current_user_id();
     947                $old_topic_id     = bbpress()->current_topic_id;
     948                $redirect         = null;
     949                $prevent_redirect = function ( $location ) use ( &$redirect ) {
     950                        $redirect = $location;
     951                        throw new RuntimeException( 'Topic edit redirect.' );
     952                };
     953
     954                bbpress()->current_topic_id = $topic_id;
     955                add_filter( 'wp_redirect', $prevent_redirect );
     956
     957                try {
     958                        $this->set_current_user( 0 );
     959                        $this->assertNull( bbp_check_topic_edit() );
     960                        $this->assertNull( $redirect );
     961
     962                        add_filter( 'bbp_is_topic_edit', '__return_true' );
     963                        try {
     964                                bbp_check_topic_edit();
     965                                $this->fail( 'Anonymous topic editing should redirect.' );
     966                        } catch ( RuntimeException $exception ) {
     967                                if ( 'Topic edit redirect.' !== $exception->getMessage() ) {
     968                                        throw $exception;
     969                                }
     970                        }
     971                        $this->assertSame( bbp_get_topic_permalink( $topic_id ), $redirect );
     972
     973                        $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
     974                        bbp_set_user_role( $admin_id, bbp_get_keymaster_role() );
     975                        $this->set_current_user( $admin_id );
     976                        $redirect = null;
     977                        $this->assertNull( bbp_check_topic_edit() );
     978                        $this->assertNull( $redirect );
     979                } finally {
     980                        remove_filter( 'bbp_is_topic_edit', '__return_true' );
     981                        remove_filter( 'wp_redirect', $prevent_redirect );
     982                        bbpress()->current_topic_id = $old_topic_id;
     983                        $this->set_current_user( $old_user );
     984                }
    781985        }
    782986}
Note: See TracChangeset for help on using the changeset viewer.