Skip to:
Content

bbPress.org

Changeset 7771


Ignore:
Timestamp:
10/03/2026 12:57:59 AM (6 weeks ago)
Author:
johnjamesjacoby
Message:

Tests: Cover forum loop behavior.

Replace three incomplete forum-loop tests with coverage for root and child queries, empty and searched results, filter arguments, and iteration cleanup. Restore query and request state after each test.

In trunk, for 2.7.
See #3695.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/forums/template/loop.php

    r5700 r7771  
    1212        /**
    1313         * @covers ::bbp_has_forums
    14          * @todo   Implement test_bbp_has_forums().
    1514         */
    1615        public function test_bbp_has_forums() {
    17                 // Remove the following lines when you implement this test.
    18                 $this->markTestIncomplete(
    19                         'This test has not been implemented yet.'
    20                 );
     16                $root_id  = $this->factory->forum->create( array( 'post_title' => 'Root forum' ) );
     17                $child_id = $this->factory->forum->create( array(
     18                        'post_title'  => 'Child forum',
     19                        'post_parent' => $root_id,
     20                ) );
     21                $bbp = bbpress();
     22                $previous_query = $bbp->forum_query;
     23
     24                try {
     25                        $this->assertTrue( bbp_has_forums() );
     26                        $this->assertInstanceOf( 'WP_Query', $bbp->forum_query );
     27                        $this->assertSame( bbp_get_forum_post_type(), $bbp->forum_query->get( 'post_type' ) );
     28                        $this->assertSame( 0, $bbp->forum_query->get( 'post_parent' ) );
     29                        $this->assertContains( bbp_get_public_status_id(), (array) $bbp->forum_query->get( 'post_status' ) );
     30                        $this->assertSame( array( $root_id ), wp_list_pluck( $bbp->forum_query->posts, 'ID' ) );
     31
     32                        $this->assertTrue( bbp_has_forums( array( 'post_parent' => $root_id ) ) );
     33                        $this->assertSame( array( $child_id ), wp_list_pluck( $bbp->forum_query->posts, 'ID' ) );
     34
     35                        $this->assertFalse( bbp_has_forums( array( 'post_parent' => -1 ) ) );
     36                        $this->assertSame( array(), $bbp->forum_query->posts );
     37                } finally {
     38                        $bbp->forum_query = $previous_query;
     39                }
     40        }
     41
     42        /**
     43         * @covers ::bbp_has_forums
     44         */
     45        public function test_bbp_has_forums_uses_forum_search_request() {
     46                $matching_id = $this->factory->forum->create( array( 'post_title' => 'Needle forum' ) );
     47                $this->factory->forum->create( array( 'post_title' => 'Other forum' ) );
     48                $bbp = bbpress();
     49                $previous_query = $bbp->forum_query;
     50                $had_search = array_key_exists( 'fs', $_REQUEST );
     51                $previous_search = $had_search ? $_REQUEST['fs'] : null;
     52                $_REQUEST['fs'] = 'Needle';
     53
     54                try {
     55                        $this->assertTrue( bbp_has_forums() );
     56                        $this->assertSame( 'Needle', $bbp->forum_query->get( 's' ) );
     57                        $this->assertSame( array( $matching_id ), wp_list_pluck( $bbp->forum_query->posts, 'ID' ) );
     58                } finally {
     59                        if ( $had_search ) {
     60                                $_REQUEST['fs'] = $previous_search;
     61                        } else {
     62                                unset( $_REQUEST['fs'] );
     63                        }
     64                        $bbp->forum_query = $previous_query;
     65                }
     66        }
     67
     68        /**
     69         * @covers ::bbp_has_forums
     70         */
     71        public function test_bbp_has_forums_filters_result_and_query() {
     72                $forum_id = $this->factory->forum->create();
     73                $bbp = bbpress();
     74                $previous_query = $bbp->forum_query;
     75                $filter = function( $has_forums, $query ) use ( $forum_id ) {
     76                        $this->assertTrue( $has_forums );
     77                        $this->assertInstanceOf( 'WP_Query', $query );
     78                        $this->assertSame( array( $forum_id ), wp_list_pluck( $query->posts, 'ID' ) );
     79                        return false;
     80                };
     81                add_filter( 'bbp_has_forums', $filter, 10, 2 );
     82                try {
     83                        $this->assertFalse( bbp_has_forums() );
     84                        $this->assertSame( array( $forum_id ), wp_list_pluck( $bbp->forum_query->posts, 'ID' ) );
     85                } finally {
     86                        remove_filter( 'bbp_has_forums', $filter );
     87                        $bbp->forum_query = $previous_query;
     88                }
    2189        }
    2290
    2391        /**
    2492         * @covers ::bbp_forums
    25          * @todo   Implement test_bbp_forums().
    2693         */
    2794        public function test_bbp_forums() {
    28                 // Remove the following lines when you implement this test.
    29                 $this->markTestIncomplete(
    30                         'This test has not been implemented yet.'
    31                 );
     95                $forum_id = $this->factory->forum->create();
     96                $bbp = bbpress();
     97                $previous_query = $bbp->forum_query;
     98
     99                try {
     100                        $this->assertTrue( bbp_has_forums() );
     101                        $this->assertTrue( bbp_forums() );
     102                        bbp_the_forum();
     103                        $this->assertSame( $forum_id, bbp_get_forum_id() );
     104                        $this->assertFalse( bbp_forums() );
     105                        $this->assertFalse( $bbp->forum_query->in_the_loop );
     106                } finally {
     107                        wp_reset_postdata();
     108                        $bbp->forum_query = $previous_query;
     109                }
    32110        }
    33111
    34112        /**
    35113         * @covers ::bbp_the_forum
    36          * @todo   Implement test_bbp_the_forum().
    37114         */
    38115        public function test_bbp_the_forum() {
    39                 // Remove the following lines when you implement this test.
    40                 $this->markTestIncomplete(
    41                         'This test has not been implemented yet.'
    42                 );
     116                $first_id  = $this->factory->forum->create( array( 'post_title' => 'Alpha forum' ) );
     117                $second_id = $this->factory->forum->create( array( 'post_title' => 'Beta forum' ) );
     118                $bbp = bbpress();
     119                $previous_query = $bbp->forum_query;
     120
     121                try {
     122                        $this->assertTrue( bbp_has_forums() );
     123                        $this->assertTrue( bbp_forums() );
     124                        bbp_the_forum();
     125                        $this->assertSame( $first_id, bbp_get_forum_id() );
     126                        $this->assertSame( 0, $bbp->forum_query->current_post );
     127
     128                        $this->assertTrue( bbp_forums() );
     129                        bbp_the_forum();
     130                        $this->assertSame( $second_id, bbp_get_forum_id() );
     131                        $this->assertSame( 1, $bbp->forum_query->current_post );
     132                        $this->assertFalse( bbp_forums() );
     133                } finally {
     134                        wp_reset_postdata();
     135                        $bbp->forum_query = $previous_query;
     136                }
    43137        }
    44138}
Note: See TracChangeset for help on using the changeset viewer.