Skip to:
Content

bbPress.org

Changeset 7786


Ignore:
Timestamp:
10/04/2026 08:15:48 AM (6 weeks ago)
Author:
johnjamesjacoby
Message:

Tests: Cover topic and reply form templates.

Replace 17 incomplete stubs with checks for form defaults, submitted and edit values, dropdown output, and nonce validation.

In trunk, for 2.7.

See #3695.

Location:
trunk/tests/phpunit/testcases
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/replies/template/forms.php

    r7461 r7786  
    99 */
    1010class BBP_Tests_Replies_Template_Forms extends BBP_UnitTestCase {
     11
     12        private function with_reply_edit( $reply_id, $callback ) {
     13                $old_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
     14                $old_reply_id = bbpress()->current_reply_id;
     15                $GLOBALS['post'] = get_post( $reply_id );
     16                bbpress()->current_reply_id = $reply_id;
     17                setup_postdata( $GLOBALS['post'] );
     18                add_filter( 'bbp_is_reply_edit', '__return_true' );
     19
     20                try {
     21                        $callback();
     22                } finally {
     23                        remove_filter( 'bbp_is_reply_edit', '__return_true' );
     24                        bbpress()->current_reply_id = $old_reply_id;
     25                        $GLOBALS['post'] = $old_post;
     26                        wp_reset_postdata();
     27                }
     28        }
     29
     30        private function with_reply_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        }
    1146
    1247        /**
    … …  
    71106         * @covers ::bbp_form_reply_content
    72107         * @covers ::bbp_get_form_reply_content
    73          * @todo   Implement test_bbp_get_form_reply_content().
    74108         */
    75109        public function test_bbp_get_form_reply_content() {
    76                 // Remove the following lines when you implement this test.
    77                 $this->markTestIncomplete(
    78                         'This test has not been implemented yet.'
    79                 );
     110                $this->assertSame( '', bbp_get_form_reply_content() );
     111                $topic_id = $this->factory->topic->create();
     112                $reply_id = $this->factory->reply->create( array( 'post_parent' => $topic_id, 'post_content' => 'Saved reply' ) );
     113                $this->with_reply_edit( $reply_id, function() {
     114                        $this->assertSame( 'Saved reply', bbp_get_form_reply_content() );
     115                } );
     116                $this->with_reply_post( 'bbp-new-reply', array( 'bbp_reply_content' => wp_slash( 'Posted "reply"' ) ), function() {
     117                        $this->assertSame( 'Posted "reply"', bbp_get_form_reply_content() );
     118                        $this->expectOutputString( 'Posted "reply"' );
     119                        bbp_form_reply_content();
     120                } );
    80121        }
    81122
    … …  
    83124         * @covers ::bbp_form_reply_to
    84125         * @covers ::bbp_get_form_reply_to
    85          * @todo   Implement test_bbp_get_form_reply_to().
    86126         */
    87127        public function test_bbp_get_form_reply_to() {
    88                 // Remove the following lines when you implement this test.
    89                 $this->markTestIncomplete(
    90                         'This test has not been implemented yet.'
    91                 );
     128                $this->assertSame( 0, bbp_get_form_reply_to() );
     129                $forum_id = $this->factory->forum->create();
     130                $topic_id = $this->factory->topic->create( array( 'post_parent' => $forum_id ) );
     131                $reply_id = $this->factory->reply->create( array( 'post_parent' => $topic_id ) );
     132                $old_request = $_REQUEST;
     133                try {
     134                        $_REQUEST['bbp_reply_to'] = (string) $reply_id;
     135                        $this->assertSame( $reply_id, bbp_get_form_reply_to() );
     136                        $this->expectOutputString( (string) $reply_id );
     137                        bbp_form_reply_to();
     138                } finally {
     139                        $_REQUEST = $old_request;
     140                }
    92141        }
    93142
    … …  
    95144         * @covers ::bbp_reply_to_dropdown
    96145         * @covers ::bbp_get_reply_to_dropdown
    97          * @todo   Implement test_bbp_get_reply_to_dropdown().
    98146         */
    99147        public function test_bbp_get_reply_to_dropdown() {
    100                 // Remove the following lines when you implement this test.
    101                 $this->markTestIncomplete(
    102                         'This test has not been implemented yet.'
    103                 );
     148                $forum_id = $this->factory->forum->create();
     149                $topic_id = $this->factory->topic->create( array( 'post_parent' => $forum_id ) );
     150                $reply_id = $this->factory->reply->create( array( 'post_parent' => $topic_id ) );
     151                $child_id = $this->factory->reply->create( array( 'post_parent' => $topic_id ) );
     152                bbp_update_reply_to( $child_id, $reply_id );
     153                $html = bbp_get_reply_to_dropdown( $child_id );
     154                $this->assertStringContainsString( 'name="bbp_reply_to"', $html );
     155                $this->assertStringContainsString( 'value="' . $reply_id . '"', $html );
     156                $this->assertMatchesRegularExpression( '/value="' . $child_id . '"[^>]*disabled=/', $html );
     157                $this->expectOutputString( $html );
     158                bbp_reply_to_dropdown( $child_id );
    104159        }
    105160
    … …  
    107162         * @covers ::bbp_form_reply_log_edit
    108163         * @covers ::bbp_get_form_reply_log_edit
    109          * @todo   Implement test_bbp_get_form_reply_log_edit().
    110164         */
    111165        public function test_bbp_get_form_reply_log_edit() {
    112                 // Remove the following lines when you implement this test.
    113                 $this->markTestIncomplete(
    114                         'This test has not been implemented yet.'
    115                 );
     166                $this->assertSame( ' checked=\'checked\'', bbp_get_form_reply_log_edit() );
     167                $this->with_reply_post( 'bbp-new-reply', array( 'bbp_log_reply_edit' => '0' ), function() {
     168                        $this->assertSame( '', bbp_get_form_reply_log_edit() );
     169                        $this->expectOutputString( '' );
     170                        bbp_form_reply_log_edit();
     171                } );
    116172        }
    117173
    … …  
    119175         * @covers ::bbp_form_reply_edit_reason
    120176         * @covers ::bbp_get_form_reply_edit_reason
    121          * @todo   Implement test_bbp_get_form_reply_edit_reason().
    122177         */
    123178        public function test_bbp_get_form_reply_edit_reason() {
    124                 // Remove the following lines when you implement this test.
    125                 $this->markTestIncomplete(
    126                         'This test has not been implemented yet.'
    127                 );
     179                $this->assertSame( '', bbp_get_form_reply_edit_reason() );
     180                $this->with_reply_post( 'bbp-new-reply', array( 'bbp_reply_edit_reason' => wp_slash( 'Fixed "details"' ) ), function() {
     181                        $this->assertSame( 'Fixed "details"', bbp_get_form_reply_edit_reason() );
     182                        $this->expectOutputString( 'Fixed "details"' );
     183                        bbp_form_reply_edit_reason();
     184                } );
    128185        }
    129186
    … …  
    131188         * @covers ::bbp_form_reply_status_dropdown
    132189         * @covers ::bbp_get_form_reply_status_dropdown
    133          * @todo   Implement test_bbp_get_form_reply_status_dropdown().
    134190         */
    135191        public function test_bbp_get_form_reply_status_dropdown() {
    136                 // Remove the following lines when you implement this test.
    137                 $this->markTestIncomplete(
    138                         'This test has not been implemented yet.'
    139                 );
     192                $args = array( 'select_id' => 'custom_reply_status', 'selected' => bbp_get_pending_status_id() );
     193                $html = bbp_get_form_reply_status_dropdown( $args );
     194                $this->assertStringContainsString( 'name="custom_reply_status"', $html );
     195                $this->assertMatchesRegularExpression( '/<option value="pending" selected=/', $html );
     196                $this->assertStringContainsString( '<option value="publish"', $html );
     197                $this->expectOutputString( $html );
     198                bbp_form_reply_status_dropdown( $args );
    140199        }
    141200
    142201        /**
    143202         * @covers ::bbp_is_reply_form_post_request
    144          * @todo   Implement test_bbp_is_reply_form_post_request().
    145203         */
    146204        public function test_bbp_is_reply_form_post_request() {
    147                 // Remove the following lines when you implement this test.
    148                 $this->markTestIncomplete(
    149                         'This test has not been implemented yet.'
    150                 );
     205                $this->assertFalse( bbp_is_reply_form_post_request() );
     206                $this->with_reply_post( 'bbp-new-reply', array(), function() {
     207                        $this->assertTrue( bbp_is_reply_form_post_request() );
     208                        $nonce = $_REQUEST['_wpnonce'];
     209                        $_REQUEST['_wpnonce'] = 'invalid';
     210                        $this->assertFalse( bbp_is_reply_form_post_request() );
     211                        $_REQUEST['_wpnonce'] = $nonce;
     212                        $_SERVER['REQUEST_METHOD'] = 'GET';
     213                        $this->assertFalse( bbp_is_reply_form_post_request() );
     214                } );
     215                $this->with_reply_post( 'bbp-edit-reply', array(), function() {
     216                        $this->assertTrue( bbp_is_reply_form_post_request() );
     217                } );
    151218        }
    152219}
  • trunk/tests/phpunit/testcases/topics/template/forms.php

    r7463 r7786  
    1616         */
    1717        protected $topic_tag_name = '';
     18
     19        private function with_topic_edit( $topic_id, $callback ) {
     20                $old_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
     21                $old_topic_id = bbpress()->current_topic_id;
     22                $GLOBALS['post'] = get_post( $topic_id );
     23                bbpress()->current_topic_id = $topic_id;
     24                setup_postdata( $GLOBALS['post'] );
     25                add_filter( 'bbp_is_topic_edit', '__return_true' );
     26
     27                try {
     28                        $callback();
     29                } finally {
     30                        remove_filter( 'bbp_is_topic_edit', '__return_true' );
     31                        bbpress()->current_topic_id = $old_topic_id;
     32                        $GLOBALS['post'] = $old_post;
     33                        wp_reset_postdata();
     34                }
     35        }
     36
     37        private function with_topic_post( $action, $values, $callback ) {
     38                $old_method = $_SERVER['REQUEST_METHOD'];
     39                $old_post = $_POST;
     40                $old_request = $_REQUEST;
     41                $_SERVER['REQUEST_METHOD'] = 'POST';
     42                $_POST = $values;
     43                $_REQUEST = array_merge( $old_request, $values, array( '_wpnonce' => wp_create_nonce( $action ) ) );
     44
     45                try {
     46                        $callback();
     47                } finally {
     48                        $_SERVER['REQUEST_METHOD'] = $old_method;
     49                        $_POST = $old_post;
     50                        $_REQUEST = $old_request;
     51                }
     52        }
    1853
    1954        /**
    … …  
    105140         * @covers ::bbp_form_topic_type_dropdown
    106141         * @covers ::bbp_get_form_topic_type_dropdown
    107          * @todo   Implement test_bbp_get_form_topic_type_dropdown().
    108142         */
    109143        public function test_bbp_get_form_topic_type_dropdown() {
    110                 // Remove the following lines when you implement this test.
    111                 $this->markTestIncomplete(
    112                         'This test has not been implemented yet.'
    113                 );
     144                $args = array( 'select_id' => 'custom_topic_type', 'selected' => 'stick' );
     145                $html = bbp_get_form_topic_type_dropdown( $args );
     146                $this->assertStringContainsString( 'name="custom_topic_type"', $html );
     147                $this->assertMatchesRegularExpression( '/<option value="stick" selected=/', $html );
     148                $this->assertStringContainsString( '<option value="unstick"', $html );
     149                $this->expectOutputString( $html );
     150                bbp_form_topic_type_dropdown( $args );
    114151        }
    115152
    … …  
    117154         * @covers ::bbp_form_topic_status_dropdown
    118155         * @covers ::bbp_get_form_topic_status_dropdown
    119          * @todo   Implement test_bbp_get_form_topic_status_dropdown().
    120156         */
    121157        public function test_bbp_get_form_topic_status_dropdown() {
    122                 // Remove the following lines when you implement this test.
    123                 $this->markTestIncomplete(
    124                         'This test has not been implemented yet.'
    125                 );
     158                $args = array( 'select_id' => 'custom_topic_status', 'selected' => bbp_get_closed_status_id() );
     159                $html = bbp_get_form_topic_status_dropdown( $args );
     160                $this->assertStringContainsString( 'name="custom_topic_status"', $html );
     161                $this->assertMatchesRegularExpression( '/<option value="closed" selected=/', $html );
     162                $this->assertStringContainsString( '<option value="publish"', $html );
     163                $this->expectOutputString( $html );
     164                bbp_form_topic_status_dropdown( $args );
    126165        }
    127166
    … …  
    129168         * @covers ::bbp_form_topic_title
    130169         * @covers ::bbp_get_form_topic_title
    131          * @todo   Implement test_bbp_get_form_topic_title().
    132170         */
    133171        public function test_bbp_get_form_topic_title() {
    134                 // Remove the following lines when you implement this test.
    135                 $this->markTestIncomplete(
    136                         'This test has not been implemented yet.'
    137                 );
     172                $this->assertSame( '', bbp_get_form_topic_title() );
     173                $topic_id = $this->factory->topic->create( array( 'post_title' => 'Saved title' ) );
     174                $this->with_topic_edit( $topic_id, function() {
     175                        $this->assertSame( 'Saved title', bbp_get_form_topic_title() );
     176                } );
     177                $this->with_topic_post( 'bbp-new-topic', array( 'bbp_topic_title' => wp_slash( 'Posted "title"' ) ), function() {
     178                        $this->assertSame( 'Posted &quot;title&quot;', bbp_get_form_topic_title() );
     179                        $this->expectOutputString( 'Posted &quot;title&quot;' );
     180                        bbp_form_topic_title();
     181                } );
    138182        }
    139183
    … …  
    141185         * @covers ::bbp_form_topic_content
    142186         * @covers ::bbp_get_form_topic_content
    143          * @todo   Implement test_bbp_get_form_topic_content().
    144187         */
    145188        public function test_bbp_get_form_topic_content() {
    146                 // Remove the following lines when you implement this test.
    147                 $this->markTestIncomplete(
    148                         'This test has not been implemented yet.'
    149                 );
     189                $this->assertSame( '', bbp_get_form_topic_content() );
     190                $topic_id = $this->factory->topic->create( array( 'post_content' => 'Saved content' ) );
     191                $this->with_topic_edit( $topic_id, function() {
     192                        $this->assertSame( 'Saved content', bbp_get_form_topic_content() );
     193                } );
     194                $this->with_topic_post( 'bbp-new-topic', array( 'bbp_topic_content' => wp_slash( 'Posted "content"' ) ), function() {
     195                        $this->assertSame( 'Posted &quot;content&quot;', bbp_get_form_topic_content() );
     196                        $this->expectOutputString( 'Posted &quot;content&quot;' );
     197                        bbp_form_topic_content();
     198                } );
    150199        }
    151200
    … …  
    153202         * @covers ::bbp_form_topic_tags
    154203         * @covers ::bbp_get_form_topic_tags
    155          * @todo   Implement test_bbp_get_form_topic_tags().
    156204         */
    157205        public function test_bbp_get_form_topic_tags() {
    158                 // Remove the following lines when you implement this test.
    159                 $this->markTestIncomplete(
    160                         'This test has not been implemented yet.'
    161                 );
     206                $this->assertSame( '', bbp_get_form_topic_tags() );
     207                $this->with_topic_post( 'bbp-new-topic', array( 'bbp_topic_tags' => wp_slash( 'alpha, beta' ) ), function() {
     208                        $this->assertSame( 'alpha, beta', bbp_get_form_topic_tags() );
     209                        $this->expectOutputString( 'alpha, beta' );
     210                        bbp_form_topic_tags();
     211                } );
    162212        }
    163213
    … …  
    165215         * @covers ::bbp_form_topic_forum
    166216         * @covers ::bbp_get_form_topic_forum
    167          * @todo   Implement test_bbp_get_form_topic_forum().
    168217         */
    169218        public function test_bbp_get_form_topic_forum() {
    170                 // Remove the following lines when you implement this test.
    171                 $this->markTestIncomplete(
    172                         'This test has not been implemented yet.'
    173                 );
     219                $this->assertSame( 0, bbp_get_form_topic_forum() );
     220                $forum_id = $this->factory->forum->create();
     221                $this->with_topic_post( 'bbp-new-topic', array( 'bbp_forum_id' => (string) $forum_id ), function() use ( $forum_id ) {
     222                        $this->assertSame( $forum_id, bbp_get_form_topic_forum() );
     223                        $this->expectOutputString( (string) $forum_id );
     224                        bbp_form_topic_forum();
     225                } );
    174226        }
    175227
    … …  
    177229         * @covers ::bbp_form_topic_subscribed
    178230         * @covers ::bbp_get_form_topic_subscribed
    179          * @todo   Implement test_bbp_get_form_topic_subscribed().
    180231         */
    181232        public function test_bbp_get_form_topic_subscribed() {
    182                 // Remove the following lines when you implement this test.
    183                 $this->markTestIncomplete(
    184                         'This test has not been implemented yet.'
    185                 );
     233                $this->assertSame( '', bbp_get_form_topic_subscribed() );
     234                $this->with_topic_post( 'bbp-new-topic', array( 'bbp_topic_subscription' => '1' ), function() {
     235                        $this->assertSame( ' checked=\'checked\'', bbp_get_form_topic_subscribed() );
     236                        $this->expectOutputString( bbp_get_form_topic_subscribed() );
     237                        bbp_form_topic_subscribed();
     238                } );
    186239        }
    187240
    … …  
    189242         * @covers ::bbp_form_topic_log_edit
    190243         * @covers ::bbp_get_form_topic_log_edit
    191          * @todo   Implement test_bbp_get_form_topic_log_edit().
    192244         */
    193245        public function test_bbp_get_form_topic_log_edit() {
    194                 // Remove the following lines when you implement this test.
    195                 $this->markTestIncomplete(
    196                         'This test has not been implemented yet.'
    197                 );
     246                $this->assertSame( ' checked=\'checked\'', bbp_get_form_topic_log_edit() );
     247                $this->with_topic_post( 'bbp-new-topic', array( 'bbp_log_topic_edit' => '0' ), function() {
     248                        $this->assertSame( '', bbp_get_form_topic_log_edit() );
     249                        $this->expectOutputString( '' );
     250                        bbp_form_topic_log_edit();
     251                } );
    198252        }
    199253
    … …  
    201255         * @covers ::bbp_form_topic_edit_reason
    202256         * @covers ::bbp_get_form_topic_edit_reason
    203          * @todo   Implement test_bbp_get_form_topic_edit_reason().
    204257         */
    205258        public function test_bbp_get_form_topic_edit_reason() {
    206                 // Remove the following lines when you implement this test.
    207                 $this->markTestIncomplete(
    208                         'This test has not been implemented yet.'
    209                 );
     259                $this->assertSame( '', bbp_get_form_topic_edit_reason() );
     260                $this->with_topic_post( 'bbp-new-topic', array( 'bbp_topic_edit_reason' => wp_slash( 'Fixed "details"' ) ), function() {
     261                        $this->assertSame( 'Fixed &quot;details&quot;', bbp_get_form_topic_edit_reason() );
     262                        $this->expectOutputString( 'Fixed &quot;details&quot;' );
     263                        bbp_form_topic_edit_reason();
     264                } );
    210265        }
    211266
    212267        /**
    213268         * @covers ::bbp_is_topic_form_post_request
    214          * @todo   Implement test_bbp_is_topic_form_post_request().
    215269         */
    216270        public function test_bbp_is_topic_form_post_request() {
    217                 // Remove the following lines when you implement this test.
    218                 $this->markTestIncomplete(
    219                         'This test has not been implemented yet.'
    220                 );
     271                $this->assertFalse( bbp_is_topic_form_post_request() );
     272                $this->with_topic_post( 'bbp-new-topic', array(), function() {
     273                        $this->assertTrue( bbp_is_topic_form_post_request() );
     274                        $nonce = $_REQUEST['_wpnonce'];
     275                        $_REQUEST['_wpnonce'] = 'invalid';
     276                        $this->assertFalse( bbp_is_topic_form_post_request() );
     277                        $_REQUEST['_wpnonce'] = $nonce;
     278                        $_SERVER['REQUEST_METHOD'] = 'GET';
     279                        $this->assertFalse( bbp_is_topic_form_post_request() );
     280                } );
     281                $topic_id = $this->factory->topic->create();
     282                $this->with_topic_edit( $topic_id, function() use ( $topic_id ) {
     283                        $this->with_topic_post( 'bbp-edit-topic_' . $topic_id, array(), function() {
     284                                $this->assertTrue( bbp_is_topic_form_post_request() );
     285                        } );
     286                } );
    221287        }
    222288}
Note: See TracChangeset for help on using the changeset viewer.