Skip to:
Content

bbPress.org

Changeset 7770


Ignore:
Timestamp:
10/02/2026 04:48:39 PM (3 weeks ago)
Author:
johnjamesjacoby
Message:

Tests: Cover forum last-reply and post-type helpers.

Replace five incomplete tests with coverage of reply permalink and URL behavior, topic-ID and missing-metadata fallbacks, forum post-type defaults and filter hooks. Correct the forum post-type output assertion.

In trunk, for 2.7.
See #3695.

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

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/forums/template/get-last-thing.php

    r6423 r7770  
    615615         * @covers ::bbp_forum_last_reply_permalink
    616616         * @covers ::bbp_get_forum_last_reply_permalink
    617          * @todo   Implement test_bbp_get_forum_last_reply_permalink().
    618617         */
    619618        public function test_bbp_get_forum_last_reply_permalink() {
    620                 // Remove the following lines when you implement this test.
    621                 $this->markTestIncomplete(
    622                         'This test has not been implemented yet.'
    623                 );
     619                $category_id = $this->factory->forum->create();
     620                bbp_categorize_forum( $category_id );
     621                $forum_id = $this->factory->forum->create( array( 'post_parent' => $category_id ) );
     622                $topic_id = $this->factory->topic->create( array(
     623                        'post_parent' => $forum_id,
     624                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     625                ) );
     626                $reply_id = $this->factory->reply->create( array(
     627                        'post_parent' => $topic_id,
     628                        'reply_meta'  => array( 'forum_id' => $forum_id, 'topic_id' => $topic_id ),
     629                ) );
     630                $permalink = bbp_get_reply_permalink( $reply_id );
     631
     632                $this->assertSame( $reply_id, bbp_get_forum_last_reply_id( $forum_id ) );
     633                $this->assertSame( $permalink, bbp_get_forum_last_reply_permalink( $forum_id ) );
     634                $this->assertSame( $permalink, bbp_get_forum_last_reply_permalink( $category_id ) );
     635                $this->expectOutputString( esc_url( $permalink ) );
     636                bbp_forum_last_reply_permalink( $forum_id );
     637        }
     638
     639        /**
     640         * @covers ::bbp_get_forum_last_reply_permalink
     641         */
     642        public function test_bbp_get_forum_last_reply_permalink_filter() {
     643                $forum_id = $this->factory->forum->create();
     644                $topic_id = $this->factory->topic->create( array(
     645                        'post_parent' => $forum_id,
     646                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     647                ) );
     648                $reply_id = $this->factory->reply->create( array(
     649                        'post_parent' => $topic_id,
     650                        'reply_meta'  => array( 'forum_id' => $forum_id, 'topic_id' => $topic_id ),
     651                ) );
     652                $filter = function( $permalink, $filtered_forum_id, $filtered_reply_id ) use ( $forum_id, $reply_id ) {
     653                        $this->assertSame( bbp_get_reply_permalink( $reply_id ), $permalink );
     654                        $this->assertSame( $forum_id, $filtered_forum_id );
     655                        $this->assertSame( $reply_id, $filtered_reply_id );
     656                        return 'https://example.org/last-reply/';
     657                };
     658                add_filter( 'bbp_get_forum_last_reply_permalink', $filter, 10, 3 );
     659                try {
     660                        $this->assertSame( 'https://example.org/last-reply/', bbp_get_forum_last_reply_permalink( $forum_id ) );
     661                } finally {
     662                        remove_filter( 'bbp_get_forum_last_reply_permalink', $filter );
     663                }
    624664        }
    625665
    … …  
    627667         * @covers ::bbp_forum_last_reply_url
    628668         * @covers ::bbp_get_forum_last_reply_url
    629          * @todo   Implement test_bbp_get_forum_last_reply_url().
    630669         */
    631670        public function test_bbp_get_forum_last_reply_url() {
    632                 // Remove the following lines when you implement this test.
    633                 $this->markTestIncomplete(
    634                         'This test has not been implemented yet.'
    635                 );
     671                $forum_id = $this->factory->forum->create();
     672                $this->assertSame( '', bbp_get_forum_last_reply_url( $forum_id ) );
     673
     674                $topic_id = $this->factory->topic->create( array(
     675                        'post_parent' => $forum_id,
     676                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     677                ) );
     678                $this->assertSame( $topic_id, bbp_get_forum_last_reply_id( $forum_id ) );
     679                $this->assertSame( bbp_get_reply_url( $topic_id ), bbp_get_forum_last_reply_url( $forum_id ) );
     680
     681                delete_post_meta( $forum_id, '_bbp_last_reply_id' );
     682                $this->assertSame( 0, bbp_get_forum_last_reply_id( $forum_id ) );
     683                $this->assertSame( bbp_get_topic_permalink( $topic_id ), bbp_get_forum_last_reply_url( $forum_id ) );
     684
     685                $reply_id = $this->factory->reply->create( array(
     686                        'post_parent' => $topic_id,
     687                        'reply_meta'  => array( 'forum_id' => $forum_id, 'topic_id' => $topic_id ),
     688                ) );
     689                $url = bbp_get_reply_url( $reply_id );
     690                $this->assertSame( $reply_id, bbp_get_forum_last_reply_id( $forum_id ) );
     691                $this->assertSame( $url, bbp_get_forum_last_reply_url( $forum_id ) );
     692                $this->expectOutputString( esc_url( $url ) );
     693                bbp_forum_last_reply_url( $forum_id );
     694        }
     695
     696        /**
     697         * @covers ::bbp_get_forum_last_reply_url
     698         */
     699        public function test_bbp_get_forum_last_reply_url_filter() {
     700                $forum_id = $this->factory->forum->create();
     701                $filter = function( $url, $filtered_forum_id, $reply_id ) use ( $forum_id ) {
     702                        $this->assertSame( '', $url );
     703                        $this->assertSame( $forum_id, $filtered_forum_id );
     704                        $this->assertSame( 0, $reply_id );
     705                        return 'https://example.org/forum/last/';
     706                };
     707                add_filter( 'bbp_get_forum_last_reply_url', $filter, 10, 3 );
     708                try {
     709                        $this->assertSame( 'https://example.org/forum/last/', bbp_get_forum_last_reply_url( $forum_id ) );
     710                } finally {
     711                        remove_filter( 'bbp_get_forum_last_reply_url', $filter );
     712                }
    636713        }
    637714
  • trunk/tests/phpunit/testcases/forums/template/post_type.php

    r6071 r7770  
    1515         */
    1616        public function test_bbp_get_forum_post_type() {
    17                 $f = $this->factory->forum->create();
    18 
    1917                $fobj = get_post_type_object( 'forum' );
    2018
    … …  
    3129                $this->assertTrue( is_post_type_hierarchical( 'forum' ) );
    3230
    33                 $forum_type = bbp_forum_post_type( $f );
    34                 $this->expectOutputString( 'forum', $forum_type );
     31                $this->assertSame( 'forum', bbp_get_forum_post_type() );
     32                $this->expectOutputString( 'forum' );
     33                bbp_forum_post_type();
     34        }
    3535
    36                 $forum_type = bbp_get_forum_post_type( $f );
    37                 $this->assertSame( 'forum', $forum_type );
     36        /**
     37         * @covers ::bbp_get_forum_post_type
     38         */
     39        public function test_bbp_get_forum_post_type_filter() {
     40                $filter = function( $post_type ) {
     41                        $this->assertSame( 'forum', $post_type );
     42                        return 'custom-forum';
     43                };
     44                add_filter( 'bbp_get_forum_post_type', $filter );
     45                try {
     46                        $this->assertSame( 'custom-forum', bbp_get_forum_post_type() );
     47                } finally {
     48                        remove_filter( 'bbp_get_forum_post_type', $filter );
     49                }
    3850        }
    3951
    4052        /**
    4153         * @covers ::bbp_get_forum_post_type_labels
    42          * @todo   Implement test_bbp_get_forum_post_type_labels().
    4354         */
    4455        public function test_bbp_get_forum_post_type_labels() {
    45                 // Remove the following lines when you implement this test.
    46                 $this->markTestIncomplete(
    47                         'This test has not been implemented yet.'
     56                $labels = bbp_get_forum_post_type_labels();
     57
     58                $this->assertSame(
     59                        array(
     60                                'name', 'menu_name', 'singular_name', 'all_items', 'add_new',
     61                                'add_new_item', 'edit', 'edit_item', 'new_item', 'view',
     62                                'view_item', 'view_items', 'search_items', 'not_found',
     63                                'not_found_in_trash', 'filter_items_list', 'items_list',
     64                                'items_list_navigation', 'parent_item_colon', 'archives',
     65                                'attributes', 'insert_into_item', 'uploaded_to_this_item',
     66                                'featured_image', 'set_featured_image', 'remove_featured_image',
     67                                'use_featured_image', 'item_published', 'item_published_privately',
     68                                'item_reverted_to_draft', 'item_scheduled', 'item_updated'
     69                        ),
     70                        array_keys( $labels )
    4871                );
     72                $this->assertSame( 'Forums', $labels['name'] );
     73                $this->assertSame( 'Forum', $labels['singular_name'] );
     74                $this->assertSame( 'No forums found', $labels['not_found'] );
     75                $this->assertSame( 'Forum updated.', $labels['item_updated'] );
     76
     77                $filter = function( $default ) use ( $labels ) {
     78                        $this->assertSame( $labels, $default );
     79                        return array( 'name' => 'Discussion areas' );
     80                };
     81                add_filter( 'bbp_get_forum_post_type_labels', $filter );
     82                try {
     83                        $this->assertSame( array( 'name' => 'Discussion areas' ), bbp_get_forum_post_type_labels() );
     84                } finally {
     85                        remove_filter( 'bbp_get_forum_post_type_labels', $filter );
     86                }
    4987        }
    5088
    5189        /**
    5290         * @covers ::bbp_get_forum_post_type_rewrite
    53          * @todo   Implement test_bbp_get_forum_post_type_rewrite().
    5491         */
    5592        public function test_bbp_get_forum_post_type_rewrite() {
    56                 // Remove the following lines when you implement this test.
    57                 $this->markTestIncomplete(
    58                         'This test has not been implemented yet.'
     93                $this->assertSame(
     94                        array( 'slug' => bbp_get_forum_slug(), 'with_front' => false ),
     95                        bbp_get_forum_post_type_rewrite()
    5996                );
     97
     98                $filter = function( $rewrite ) {
     99                        $this->assertSame( bbp_get_forum_slug(), $rewrite['slug'] );
     100                        $this->assertFalse( $rewrite['with_front'] );
     101                        $rewrite['slug'] = 'discussion-area';
     102                        return $rewrite;
     103                };
     104                add_filter( 'bbp_get_forum_post_type_rewrite', $filter );
     105                try {
     106                        $this->assertSame(
     107                                array( 'slug' => 'discussion-area', 'with_front' => false ),
     108                                bbp_get_forum_post_type_rewrite()
     109                        );
     110                } finally {
     111                        remove_filter( 'bbp_get_forum_post_type_rewrite', $filter );
     112                }
    60113        }
    61114
    62115        /**
    63116         * @covers ::bbp_get_forum_post_type_supports
    64          * @todo   Implement test_bbp_get_forum_post_type_supports().
    65117         */
    66118        public function test_bbp_get_forum_post_type_supports() {
    67                 // Remove the following lines when you implement this test.
    68                 $this->markTestIncomplete(
    69                         'This test has not been implemented yet.'
    70                 );
     119                $this->assertSame( array( 'title', 'editor', 'revisions' ), bbp_get_forum_post_type_supports() );
     120
     121                $filter = function( $supports ) {
     122                        $this->assertSame( array( 'title', 'editor', 'revisions' ), $supports );
     123                        return array( 'title', 'comments' );
     124                };
     125                add_filter( 'bbp_get_forum_post_type_supports', $filter );
     126                try {
     127                        $this->assertSame( array( 'title', 'comments' ), bbp_get_forum_post_type_supports() );
     128                } finally {
     129                        remove_filter( 'bbp_get_forum_post_type_supports', $filter );
     130                }
    71131        }
    72132}
Note: See TracChangeset for help on using the changeset viewer.