Skip to:
Content

bbPress.org

Changeset 7785


Ignore:
Timestamp:
10/04/2026 08:08:18 AM (3 weeks ago)
Author:
johnjamesjacoby
Message:

Tests: Cover search template helpers.

Replace nine incomplete tests for search queries, loop advancement, titles, URLs, terms, and pagination output. Exercise populated and empty search results with stable fixtures.

In trunk, for 2.7.

See #3695.

File:
1 edited

Legend:

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

    r5754 r7785  
    1111        /**
    1212         * @covers ::bbp_has_search_results
    13          * @todo   Implement test_bbp_has_search_results().
    1413         */
    1514        public function test_bbp_has_search_results() {
    16                 // Remove the following lines when you implement this test.
    17                 $this->markTestIncomplete(
    18                         'This test has not been implemented yet.'
    19                 );
     15                $topic_id = $this->factory->topic->create( array( 'post_title' => 'UniqueSearchNeedle' ) );
     16
     17                $this->assertTrue( bbp_has_search_results( array( 's' => 'UniqueSearchNeedle', 'post_type' => bbp_get_topic_post_type() ) ) );
     18                $this->assertSame( $topic_id, bbpress()->search_query->posts[0]->ID );
     19                $this->assertFalse( bbp_has_search_results( array( 's' => 'NoMatchingSearchNeedle', 'post_type' => bbp_get_topic_post_type() ) ) );
    2020        }
    2121
    2222        /**
    2323         * @covers ::bbp_search_results
    24          * @todo   Implement test_bbp_search_results().
    2524         */
    2625        public function test_bbp_search_results() {
    27                 // Remove the following lines when you implement this test.
    28                 $this->markTestIncomplete(
    29                         'This test has not been implemented yet.'
    30                 );
     26                $this->factory->topic->create( array( 'post_title' => 'UniqueSearchNeedle' ) );
     27                bbp_has_search_results( array( 's' => 'UniqueSearchNeedle', 'post_type' => bbp_get_topic_post_type() ) );
     28
     29                $this->assertTrue( bbp_search_results() );
     30                bbp_the_search_result();
     31                $this->assertFalse( bbp_search_results() );
    3132        }
    3233
    3334        /**
    3435         * @covers ::bbp_the_search_result
    35          * @todo   Implement test_bbp_the_search_result().
    3636         */
    3737        public function test_bbp_the_search_result() {
    38                 // Remove the following lines when you implement this test.
    39                 $this->markTestIncomplete(
    40                         'This test has not been implemented yet.'
    41                 );
     38                $topic_id = $this->factory->topic->create( array( 'post_title' => 'UniqueSearchNeedle' ) );
     39                bbp_has_search_results( array( 's' => 'UniqueSearchNeedle', 'post_type' => bbp_get_topic_post_type() ) );
     40
     41                $this->assertTrue( bbp_search_results() );
     42                bbp_the_search_result();
     43                $this->assertSame( $topic_id, get_the_ID() );
     44                $this->assertSame( $topic_id, bbpress()->current_topic_id );
     45                $this->assertFalse( bbp_search_results() );
    4246        }
    4347
    … …  
    4549         * @covers ::bbp_search_title
    4650         * @covers ::bbp_get_search_title
    47          * @todo   Implement test_bbp_get_search_title().
    4851         */
    4952        public function test_bbp_get_search_title() {
    50                 // Remove the following lines when you implement this test.
    51                 $this->markTestIncomplete(
    52                         'This test has not been implemented yet.'
    53                 );
     53                $this->assertSame( 'Search', bbp_get_search_title() );
     54                $filter = static function() {
     55                        return 'sample query';
     56                };
     57                add_filter( 'bbp_get_search_terms', $filter );
     58                try {
     59                        $title = bbp_get_search_title();
     60                        $this->assertStringContainsString( 'sample query', $title );
     61                        $this->expectOutputString( $title );
     62                        bbp_search_title();
     63                } finally {
     64                        remove_filter( 'bbp_get_search_terms', $filter );
     65                }
    5466        }
    5567
    … …  
    5769         * @covers ::bbp_search_url
    5870         * @covers ::bbp_get_search_url
    59          * @todo   Implement test_bbp_get_search_url().
    6071         */
    6172        public function test_bbp_get_search_url() {
    62                 // Remove the following lines when you implement this test.
    63                 $this->markTestIncomplete(
    64                         'This test has not been implemented yet.'
    65                 );
     73                $url = bbp_get_search_url();
     74
     75                $this->assertStringContainsString( bbp_get_search_rewrite_id(), $url );
     76                $this->expectOutputString( esc_url( $url ) );
     77                bbp_search_url();
    6678        }
    6779
    … …  
    6981         * @covers ::bbp_search_results_url
    7082         * @covers ::bbp_get_search_results_url
    71          * @todo   Implement test_bbp_get_search_results_url().
    7283         */
    7384        public function test_bbp_get_search_results_url() {
    74                 // Remove the following lines when you implement this test.
    75                 $this->markTestIncomplete(
    76                         'This test has not been implemented yet.'
    77                 );
     85                $filter = static function() {
     86                        return 'sample query';
     87                };
     88                add_filter( 'bbp_get_search_terms', $filter );
     89                try {
     90                        $url = bbp_get_search_results_url();
     91                        $this->assertStringContainsString( 'sample', $url );
     92                        $this->expectOutputString( esc_url( $url ) );
     93                        bbp_search_results_url();
     94                } finally {
     95                        remove_filter( 'bbp_get_search_terms', $filter );
     96                }
    7897        }
    7998
    … …  
    81100         * @covers ::bbp_search_terms
    82101         * @covers ::bbp_get_search_terms
    83          * @todo   Implement test_bbp_get_search_terms().
    84102         */
    85103        public function test_bbp_get_search_terms() {
    86                 // Remove the following lines when you implement this test.
    87                 $this->markTestIncomplete(
    88                         'This test has not been implemented yet.'
    89                 );
     104                $this->assertFalse( bbp_get_search_terms() );
     105                $this->assertSame( 'sample-query', bbp_get_search_terms( 'Sample Query' ) );
     106                $this->expectOutputString( 'sample-query' );
     107                bbp_search_terms( 'Sample Query' );
    90108        }
    91109
    … …  
    93111         * @covers ::bbp_search_pagination_count
    94112         * @covers ::bbp_get_search_pagination_count
    95          * @todo   Implement test_bbp_get_search_pagination_count().
    96113         */
    97114        public function test_bbp_get_search_pagination_count() {
    98                 // Remove the following lines when you implement this test.
    99                 $this->markTestIncomplete(
    100                         'This test has not been implemented yet.'
    101                 );
     115                $this->factory->topic->create_many( 2, array( 'post_title' => 'SearchPageNeedle' ) );
     116                bbp_has_search_results( array(
     117                        's'              => 'SearchPageNeedle',
     118                        'post_type'      => bbp_get_topic_post_type(),
     119                        'posts_per_page' => 1,
     120                ) );
     121                $count = bbp_get_search_pagination_count();
     122
     123                $this->assertStringContainsString( 'Viewing', $count );
     124                $this->assertStringContainsString( '2 total', $count );
     125                $this->expectOutputString( $count );
     126                bbp_search_pagination_count();
    102127        }
    103128
    … …  
    105130         * @covers ::bbp_search_pagination_links
    106131         * @covers ::bbp_get_search_pagination_links
    107          * @todo   Implement test_bbp_get_search_pagination_links().
    108132         */
    109133        public function test_bbp_get_search_pagination_links() {
    110                 // Remove the following lines when you implement this test.
    111                 $this->markTestIncomplete(
    112                         'This test has not been implemented yet.'
    113                 );
     134                $this->factory->topic->create_many( 2, array( 'post_title' => 'SearchPageNeedle' ) );
     135                bbp_has_search_results( array(
     136                        's'              => 'SearchPageNeedle',
     137                        'post_type'      => bbp_get_topic_post_type(),
     138                        'posts_per_page' => 1,
     139                ) );
     140                $links = bbp_get_search_pagination_links();
     141
     142                $this->assertNotEmpty( $links );
     143                $this->assertStringContainsString( 'page-numbers', $links );
     144                $this->expectOutputString( $links );
     145                bbp_search_pagination_links();
    114146        }
    115147}
Note: See TracChangeset for help on using the changeset viewer.