Skip to:
Content

bbPress.org

Changeset 7775


Ignore:
Timestamp:
10/04/2026 02:49:39 AM (5 weeks ago)
Author:
johnjamesjacoby
Message:

Tests: Cover forum template helpers and counts.

Replace eight incomplete forum template tests with coverage for lookup, archive titles, hierarchy, listings, links, and classes. Add hidden-reply-count coverage and exercise category listings and subscription states.

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/counts.php

    r7449 r7775  
    256256                $this->assertSame( $int_value, $count );
    257257        }
     258
     259        /**
     260         * @covers ::bbp_forum_reply_count_hidden
     261         * @covers ::bbp_get_forum_reply_count_hidden
     262         */
     263        public function test_bbp_get_forum_reply_count_hidden() {
     264                $forum_id = $this->factory->forum->create();
     265                update_post_meta( $forum_id, '_bbp_reply_count_hidden', 2 );
     266                update_post_meta( $forum_id, '_bbp_total_reply_count_hidden', 5 );
     267
     268                $this->assertSame( 2, bbp_get_forum_reply_count_hidden( $forum_id, false, true ) );
     269                $this->assertSame( 5, bbp_get_forum_reply_count_hidden( $forum_id, true, true ) );
     270
     271                ob_start();
     272                bbp_forum_reply_count_hidden( $forum_id, false, true );
     273                $this->assertSame( '2', ob_get_clean() );
     274
     275                $filter = function( $count, $filtered_forum_id ) use ( $forum_id ) {
     276                        $this->assertSame( $forum_id, $filtered_forum_id );
     277                        return $count + 1;
     278                };
     279                add_filter( 'bbp_get_forum_reply_count_hidden_int', $filter, 10, 2 );
     280                try {
     281                        $this->assertSame( 3, bbp_get_forum_reply_count_hidden( $forum_id, false, true ) );
     282                } finally {
     283                        remove_filter( 'bbp_get_forum_reply_count_hidden_int', $filter );
     284                }
     285        }
    258286}
  • trunk/tests/phpunit/testcases/forums/template/forum.php

    r7669 r7775  
    2323        /**
    2424         * @covers ::bbp_get_forum
    25          * @todo   Implement test_bbp_get_forum().
    2625         */
    2726        public function test_bbp_get_forum() {
    28                 // Remove the following lines when you implement this test.
    29                 $this->markTestIncomplete(
    30                         'This test has not been implemented yet.'
    31                 );
     27                $forum_id = $this->factory->forum->create();
     28                $topic_id = $this->factory->topic->create( array( 'post_parent' => $forum_id ) );
     29                $forum    = bbp_get_forum( $forum_id );
     30
     31                $this->assertInstanceOf( 'WP_Post', $forum );
     32                $this->assertSame( $forum_id, $forum->ID );
     33                $this->assertSame( $forum_id, bbp_get_forum( $forum )->ID );
     34                $this->assertSame( $forum_id, bbp_get_forum( $forum_id, ARRAY_A )['ID'] );
     35                $this->assertSame( array_values( get_object_vars( $forum ) ), bbp_get_forum( $forum_id, ARRAY_N ) );
     36                $this->assertNull( bbp_get_forum( $topic_id ) );
     37                $this->assertNull( bbp_get_forum( 999999 ) );
    3238        }
    3339
    … …  
    117123         * @covers ::bbp_forum_archive_title
    118124         * @covers ::bbp_get_forum_archive_title
    119          * @todo   Implement test_bbp_forum_archive_title().
    120          * @todo   Implement test_bbp_get_forum_archive_title().
    121125         */
    122126        public function test_bbp_get_forum_archive_title() {
    123                 // Remove the following lines when you implement this test.
    124                 $this->markTestIncomplete(
    125                         'This test has not been implemented yet.'
    126                 );
     127                $title = get_post_type_object( bbp_get_forum_post_type() )->labels->name;
     128
     129                $this->assertSame( $title, bbp_get_forum_archive_title() );
     130                $this->assertSame( 'Custom archive', bbp_get_forum_archive_title( 'Custom archive' ) );
     131                $this->expectOutputString( 'Custom archive' );
     132                bbp_forum_archive_title( 'Custom archive' );
    127133        }
    128134
    … …  
    527533        /**
    528534         * @covers ::bbp_get_forum_ancestors
    529          * @todo   Implement test_bbp_get_forum_ancestors().
    530535         */
    531536        public function test_bbp_get_forum_ancestors() {
    532                 // Remove the following lines when you implement this test.
    533                 $this->markTestIncomplete(
    534                         'This test has not been implemented yet.'
    535                 );
     537                $root_id   = $this->factory->forum->create();
     538                $parent_id = $this->factory->forum->create( array( 'post_parent' => $root_id ) );
     539                $child_id  = $this->factory->forum->create( array( 'post_parent' => $parent_id ) );
     540
     541                $this->assertSame( array(), bbp_get_forum_ancestors( $root_id ) );
     542                $this->assertSame( array( $parent_id, $root_id ), bbp_get_forum_ancestors( $child_id ) );
     543                $this->assertSame( array(), bbp_get_forum_ancestors( 999999 ) );
    536544        }
    537545
    538546        /**
    539547         * @covers ::bbp_forum_get_subforums
    540          * @todo   Implement test_bbp_forum_get_subforums().
    541548         */
    542549        public function test_bbp_forum_get_subforums() {
    543                 // Remove the following lines when you implement this test.
    544                 $this->markTestIncomplete(
    545                         'This test has not been implemented yet.'
    546                 );
     550                $parent_id = $this->factory->forum->create();
     551                $first_id  = $this->factory->forum->create( array( 'post_parent' => $parent_id, 'post_title' => 'Alpha' ) );
     552                $second_id = $this->factory->forum->create( array( 'post_parent' => $parent_id, 'post_title' => 'Beta' ) );
     553                $this->factory->forum->create();
     554                bbp_update_forum_subforum_count( $parent_id );
     555
     556                $this->assertSame( array(), bbp_forum_get_subforums( 999999 ) );
     557                $this->assertSame( array( $first_id, $second_id ), wp_list_pluck( bbp_forum_get_subforums( $parent_id ), 'ID' ) );
     558                $this->assertSame( array( $second_id ), wp_list_pluck( bbp_forum_get_subforums( array( 'post_parent' => $parent_id, 'posts_per_page' => 1, 'order' => 'DESC' ) ), 'ID' ) );
    547559        }
    548560
    549561        /**
    550562         * @covers ::bbp_list_forums
    551          * @todo   Implement test_bbp_list_forums().
    552563         */
    553564        public function test_bbp_list_forums() {
    554                 // Remove the following lines when you implement this test.
    555                 $this->markTestIncomplete(
    556                         'This test has not been implemented yet.'
    557                 );
     565                $parent_id = $this->factory->forum->create();
     566                $child_id  = $this->factory->forum->create( array( 'post_parent' => $parent_id, 'post_title' => 'Listed child' ) );
     567                $category_id = $this->factory->forum->create( array( 'post_parent' => $parent_id, 'post_title' => 'Listed category' ) );
     568                bbp_categorize_forum( $category_id );
     569                bbp_update_forum_subforum_count( $parent_id );
     570
     571                $this->assertSame( '', bbp_list_forums( array( 'forum_id' => $child_id, 'echo' => false ) ) );
     572                $list = bbp_list_forums( array( 'forum_id' => $parent_id, 'echo' => false, 'show_topic_count' => false, 'show_reply_count' => false ) );
     573                $this->assertStringContainsString( '<ul class="bbp-forums-list">', $list );
     574                $this->assertStringContainsString( 'Listed child', $list );
     575                $this->assertStringContainsString( 'Listed category', $list );
     576                $this->assertStringContainsString( esc_url( bbp_get_forum_permalink( $child_id ) ), $list );
     577                $this->assertStringNotContainsString( ' (0, 0)', $list );
     578                $counted_list = bbp_list_forums( array( 'forum_id' => $parent_id, 'echo' => false ) );
     579                $this->assertStringContainsString( 'Listed child (0, 0)', $counted_list );
     580                $this->assertStringContainsString( 'Listed category</a>', $counted_list );
     581
     582                ob_start();
     583                bbp_list_forums( array( 'forum_id' => $parent_id, 'show_topic_count' => false, 'show_reply_count' => false ) );
     584                $this->assertSame( $list, ob_get_clean() );
    558585        }
    559586
    … …  
    561588         * @covers ::bbp_forum_subscription_link
    562589         * @covers ::bbp_get_forum_subscription_link
    563          * @todo   Implement test_bbp_get_forum_subscription_link().
    564590         */
    565591        public function test_bbp_get_forum_subscription_link() {
    566                 // Remove the following lines when you implement this test.
    567                 $this->markTestIncomplete(
    568                         'This test has not been implemented yet.'
    569                 );
     592                $forum_id = $this->factory->forum->create();
     593                $user_id  = $this->factory->user->create( array( 'role' => bbp_get_participant_role() ) );
     594                $this->set_current_user( $user_id );
     595
     596                $args = array( 'user_id' => $user_id, 'object_id' => $forum_id );
     597                $this->assertTrue( bbp_is_subscriptions_active() );
     598                $this->assertFalse( bbp_is_forum_category() );
     599                $this->assertTrue( current_user_can( 'read_forum', $forum_id ) );
     600                $this->assertTrue( current_user_can( 'edit_user', $user_id ) );
     601                $link = bbp_get_forum_subscription_link( $args );
     602                $this->assertStringContainsString( 'Subscribe', $link );
     603                $this->assertStringContainsString( 'data-bbp-object-id="' . $forum_id . '"', $link );
     604                $this->assertStringContainsString( 'data-bbp-object-type="post"', $link );
     605                $this->assertTrue( bbp_add_user_subscription( $user_id, $forum_id ) );
     606                $subscribed_link = bbp_get_forum_subscription_link( $args );
     607                $this->assertStringContainsString( 'Unsubscribe', $subscribed_link );
     608                $this->assertStringContainsString( 'is-subscribed', $subscribed_link );
     609
     610                ob_start();
     611                bbp_forum_subscription_link( $args );
     612                $this->assertSame( $subscribed_link, ob_get_clean() );
     613
     614                $this->set_current_user( 0 );
     615                $this->assertFalse( bbp_get_forum_subscription_link( array( 'object_id' => $forum_id ) ) );
    570616        }
    571617
    … …  
    573619         * @covers ::bbp_forum_topics_link
    574620         * @covers ::bbp_get_forum_topics_link
    575          * @todo   Implement test_bbp_get_forum_topics_link().
    576621         */
    577622        public function test_bbp_get_forum_topics_link() {
    578                 // Remove the following lines when you implement this test.
    579                 $this->markTestIncomplete(
    580                         'This test has not been implemented yet.'
    581                 );
     623                $forum_id = $this->factory->forum->create();
     624                $this->assertSame( '0 topics', bbp_get_forum_topics_link( $forum_id ) );
     625
     626                $this->factory->topic->create( array( 'post_parent' => $forum_id, 'topic_meta' => array( 'forum_id' => $forum_id ) ) );
     627                $this->assertSame( '1 topic', bbp_get_forum_topics_link( $forum_id ) );
     628
     629                ob_start();
     630                bbp_forum_topics_link( $forum_id );
     631                $this->assertSame( '1 topic', ob_get_clean() );
    582632        }
    583633
    … …  
    585635         * @covers ::bbp_forum_class
    586636         * @covers ::bbp_get_forum_class
    587          * @todo   Implement test_bbp_get_forum_class().
    588637         */
    589638        public function test_bbp_get_forum_class() {
    590                 // Remove the following lines when you implement this test.
    591                 $this->markTestIncomplete(
    592                         'This test has not been implemented yet.'
    593                 );
     639                $parent_id = $this->factory->forum->create();
     640                $child_id  = $this->factory->forum->create( array( 'post_parent' => $parent_id ) );
     641                bbp_categorize_forum( $child_id );
     642                bbp_close_forum( $child_id );
     643
     644                $class = bbp_get_forum_class( $child_id, array( 'custom-forum-class' ) );
     645                $this->assertStringContainsString( 'custom-forum-class', $class );
     646                $this->assertStringContainsString( 'bbp-forum-status-closed', $class );
     647                $this->assertStringContainsString( 'bbp-forum-visibility-publish', $class );
     648                $this->assertStringContainsString( 'status-category', $class );
     649                $this->assertStringContainsString( 'bbp-parent-forum-' . $parent_id, $class );
     650
     651                ob_start();
     652                bbp_forum_class( $child_id, array( 'custom-forum-class' ) );
     653                $this->assertSame( $class, ob_get_clean() );
    594654        }
    595655
Note: See TracChangeset for help on using the changeset viewer.