Skip to:
Content

bbPress.org

Changeset 7792


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

Tests: Cover topic template links.

Replace fourteen incomplete stubs with checks for engagement, reply, moderation, edit, and pagination links across their relevant topic states.

In trunk, for 2.7.

See #3695.

File:
1 edited

Legend:

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

    r6802 r7792  
    1010class BBP_Tests_Topics_Template_Links extends BBP_UnitTestCase {
    1111
     12        private function with_keymaster_topic( $callback ) {
     13                $old_user = get_current_user_id();
     14                $user_id  = $this->factory->user->create( array( 'role' => 'administrator' ) );
     15                $forum_id = $this->factory->forum->create();
     16                $topic_id = $this->factory->topic->create( array(
     17                        'post_parent' => $forum_id,
     18                        'post_author' => $user_id,
     19                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     20                ) );
     21                bbp_set_user_role( $user_id, bbp_get_keymaster_role() );
     22                $this->set_current_user( $user_id );
     23
     24                try {
     25                        $callback( $topic_id, $forum_id, $user_id );
     26                } finally {
     27                        $this->set_current_user( $old_user );
     28                }
     29        }
     30
    1231        /**
    1332         * @covers ::bbp_topic_subscription_link
    1433         * @covers ::bbp_get_topic_subscription_link
    15          * @todo   Implement test_bbp_get_topic_subscription_link().
    1634         */
    1735        public function test_bbp_get_topic_subscription_link() {
    18                 // Remove the following lines when you implement this test.
    19                 $this->markTestIncomplete(
    20                         'This test has not been implemented yet.'
    21                 );
     36                $old_user = get_current_user_id();
     37                $user_id  = $this->factory->user->create( array( 'role' => 'administrator' ) );
     38                $forum_id = $this->factory->forum->create();
     39                $topic_id = $this->factory->topic->create( array(
     40                        'post_parent' => $forum_id,
     41                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     42                ) );
     43                $args = array( 'user_id' => $user_id, 'object_id' => $topic_id, 'before' => '' );
     44                bbp_set_user_role( $user_id, bbp_get_keymaster_role() );
     45                $this->set_current_user( $user_id );
     46
     47                try {
     48                        $this->assertStringContainsString( 'Subscribe', bbp_get_topic_subscription_link( $args ) );
     49                        bbp_add_user_topic_subscription( $user_id, $topic_id );
     50                        $html = bbp_get_topic_subscription_link( $args );
     51                        $this->assertStringContainsString( 'Unsubscribe', $html );
     52                        $this->expectOutputString( $html );
     53                        bbp_topic_subscription_link( $args );
     54                } finally {
     55                        $this->set_current_user( $old_user );
     56                }
    2257        }
    2358
    … …  
    2560         * @covers ::bbp_topic_favorite_link
    2661         * @covers ::bbp_get_topic_favorite_link
    27          * @todo   Implement test_bbp_get_topic_favorite_link().
    2862         */
    2963        public function test_bbp_get_topic_favorite_link() {
    30                 // Remove the following lines when you implement this test.
    31                 $this->markTestIncomplete(
    32                         'This test has not been implemented yet.'
    33                 );
     64                $old_user = get_current_user_id();
     65                $user_id  = $this->factory->user->create( array( 'role' => 'administrator' ) );
     66                $forum_id = $this->factory->forum->create();
     67                $topic_id = $this->factory->topic->create( array(
     68                        'post_parent' => $forum_id,
     69                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     70                ) );
     71                $args = array( 'user_id' => $user_id, 'object_id' => $topic_id );
     72                bbp_set_user_role( $user_id, bbp_get_keymaster_role() );
     73                $this->set_current_user( $user_id );
     74
     75                try {
     76                        $this->assertStringContainsString( 'Favorite', bbp_get_topic_favorite_link( $args ) );
     77                        bbp_add_user_favorite( $user_id, $topic_id );
     78                        $html = bbp_get_topic_favorite_link( $args );
     79                        $this->assertStringContainsString( 'Unfavorite', $html );
     80                        $this->expectOutputString( $html );
     81                        bbp_topic_favorite_link( $args );
     82                } finally {
     83                        $this->set_current_user( $old_user );
     84                }
    3485        }
    3586
    … …  
    213264         * @covers ::bbp_topic_replies_link
    214265         * @covers ::bbp_get_topic_replies_link
    215          * @todo   Implement test_bbp_get_topic_replies_link().
    216266         */
    217267        public function test_bbp_get_topic_replies_link() {
    218                 // Remove the following lines when you implement this test.
    219                 $this->markTestIncomplete(
    220                         'This test has not been implemented yet.'
    221                 );
     268                $forum_id = $this->factory->forum->create();
     269                $topic_id = $this->factory->topic->create( array( 'post_parent' => $forum_id ) );
     270                $this->assertSame( '0 replies', bbp_get_topic_replies_link( $topic_id ) );
     271
     272                $this->factory->reply->create( array( 'post_parent' => $topic_id ) );
     273                $html = bbp_get_topic_replies_link( $topic_id );
     274                $this->assertSame( '1 reply', $html );
     275                $this->expectOutputString( $html );
     276                bbp_topic_replies_link( $topic_id );
    222277        }
    223278
    … …  
    225280         * @covers ::bbp_topic_admin_links
    226281         * @covers ::bbp_get_topic_admin_links
    227          * @todo   Implement test_bbp_get_topic_admin_links().
    228282         */
    229283        public function test_bbp_get_topic_admin_links() {
    230                 // Remove the following lines when you implement this test.
    231                 $this->markTestIncomplete(
    232                         'This test has not been implemented yet.'
    233                 );
     284                $this->with_keymaster_topic( function( $topic_id ) {
     285                        $args = array( 'id' => $topic_id, 'before' => '[', 'after' => ']', 'sep' => ', ', 'links' => array( 'edit' => 'Edit', 'close' => 'Close' ) );
     286                        $this->assertSame( '[Edit, Close]', bbp_get_topic_admin_links( $args ) );
     287                        $this->assertStringContainsString( 'bbp-topic-edit-link', bbp_get_topic_admin_links( array( 'id' => $topic_id ) ) );
     288                        $this->expectOutputString( '[Edit, Close]' );
     289                        bbp_topic_admin_links( $args );
     290                } );
    234291        }
    235292
    … …  
    237294         * @covers ::bbp_topic_edit_link
    238295         * @covers ::bbp_get_topic_edit_link
    239          * @todo   Implement test_bbp_get_topic_edit_link().
    240296         */
    241297        public function test_bbp_get_topic_edit_link() {
    242                 // Remove the following lines when you implement this test.
    243                 $this->markTestIncomplete(
    244                         'This test has not been implemented yet.'
    245                 );
     298                $this->with_keymaster_topic( function( $topic_id ) {
     299                        $args = array( 'id' => $topic_id, 'edit_text' => 'Edit topic' );
     300                        $html = bbp_get_topic_edit_link( $args );
     301                        $this->assertStringContainsString( 'class="bbp-topic-edit-link"', $html );
     302                        $this->assertStringContainsString( '>Edit topic</a>', $html );
     303                        $this->assertStringContainsString( esc_url( bbp_get_topic_edit_url( $topic_id ) ), $html );
     304                        $this->expectOutputString( $html );
     305                        bbp_topic_edit_link( $args );
     306                } );
    246307        }
    247308
    … …  
    249310         * @covers ::bbp_topic_edit_url
    250311         * @covers ::bbp_get_topic_edit_url
    251          * @todo   Implement test_bbp_get_topic_edit_url().
    252312         */
    253313        public function test_bbp_get_topic_edit_url() {
    254                 // Remove the following lines when you implement this test.
    255                 $this->markTestIncomplete(
    256                         'This test has not been implemented yet.'
    257                 );
     314                $this->assertNull( bbp_get_topic_edit_url( PHP_INT_MAX ) );
     315                $this->with_keymaster_topic( function( $topic_id ) {
     316                        $url = bbp_get_topic_edit_url( $topic_id );
     317                        if ( false === strpos( bbp_get_topic_permalink( $topic_id ), '?' ) ) {
     318                                $this->assertStringContainsString( bbp_get_edit_slug(), $url );
     319                        } else {
     320                                $this->assertStringContainsString( bbp_get_edit_rewrite_id() . '=1', $url );
     321                        }
     322                        $this->expectOutputString( esc_url( $url ) );
     323                        bbp_topic_edit_url( $topic_id );
     324                } );
    258325        }
    259326
    … …  
    261328         * @covers ::bbp_topic_trash_link
    262329         * @covers ::bbp_get_topic_trash_link
    263          * @todo   Implement test_bbp_get_topic_trash_link().
    264330         */
    265331        public function test_bbp_get_topic_trash_link() {
    266                 // Remove the following lines when you implement this test.
    267                 $this->markTestIncomplete(
    268                         'This test has not been implemented yet.'
    269                 );
     332                $this->with_keymaster_topic( function( $topic_id ) {
     333                        $args = array( 'id' => $topic_id );
     334                        $html = bbp_get_topic_trash_link( $args );
     335                        $this->assertStringContainsString( 'class="bbp-topic-trash-link">Trash</a>', $html );
     336                        $this->expectOutputString( $html );
     337                        bbp_topic_trash_link( $args );
     338
     339                        wp_trash_post( $topic_id );
     340                        $trashed_html = bbp_get_topic_trash_link( $args );
     341                        $this->assertStringContainsString( 'class="bbp-topic-restore-link">Restore</a>', $trashed_html );
     342                        $this->assertStringContainsString( 'class="bbp-topic-delete-link">Delete</a>', $trashed_html );
     343                } );
    270344        }
    271345
    … …  
    273347         * @covers ::bbp_topic_close_link
    274348         * @covers ::bbp_get_topic_close_link
    275          * @todo   Implement test_bbp_get_topic_close_link().
    276349         */
    277350        public function test_bbp_get_topic_close_link() {
    278                 // Remove the following lines when you implement this test.
    279                 $this->markTestIncomplete(
    280                         'This test has not been implemented yet.'
    281                 );
     351                $this->with_keymaster_topic( function( $topic_id ) {
     352                        $args = array( 'id' => $topic_id );
     353                        $html = bbp_get_topic_close_link( $args );
     354                        $this->assertStringContainsString( 'class="bbp-topic-close-link">Close</a>', $html );
     355                        $this->expectOutputString( $html );
     356                        bbp_topic_close_link( $args );
     357                        bbp_close_topic( $topic_id );
     358                        $this->assertStringContainsString( 'class="bbp-topic-close-link">Open</a>', bbp_get_topic_close_link( $args ) );
     359                } );
    282360        }
    283361
    … …  
    285363         * @covers ::bbp_topic_approve_link
    286364         * @covers ::bbp_get_topic_approve_link
    287          * @todo   Implement test_bbp_get_topic_approve_link().
    288365         */
    289366        public function test_bbp_get_topic_approve_link() {
    290                 // Remove the following lines when you implement this test.
    291                 $this->markTestIncomplete(
    292                         'This test has not been implemented yet.'
    293                 );
     367                $this->with_keymaster_topic( function( $topic_id ) {
     368                        $args = array( 'id' => $topic_id );
     369                        $html = bbp_get_topic_approve_link( $args );
     370                        $this->assertStringContainsString( 'class="bbp-topic-approve-link">Unapprove</a>', $html );
     371                        $this->expectOutputString( $html );
     372                        bbp_topic_approve_link( $args );
     373                        bbp_unapprove_topic( $topic_id );
     374                        $this->assertStringContainsString( 'class="bbp-topic-approve-link">Approve</a>', bbp_get_topic_approve_link( $args ) );
     375                } );
    294376        }
    295377
    … …  
    297379         * @covers ::bbp_topic_stick_link
    298380         * @covers ::bbp_get_topic_stick_link
    299          * @todo   Implement test_bbp_get_topic_stick_link().
    300381         */
    301382        public function test_bbp_get_topic_stick_link() {
    302                 // Remove the following lines when you implement this test.
    303                 $this->markTestIncomplete(
    304                         'This test has not been implemented yet.'
    305                 );
     383                $this->with_keymaster_topic( function( $topic_id ) {
     384                        $args = array( 'id' => $topic_id );
     385                        $html = bbp_get_topic_stick_link( $args );
     386                        $this->assertStringContainsString( 'class="bbp-topic-sticky-link">Stick</a>', $html );
     387                        $this->assertStringContainsString( 'class="bbp-topic-super-sticky-link">(to front)</a>', $html );
     388                        $this->expectOutputString( $html );
     389                        bbp_topic_stick_link( $args );
     390
     391                        bbp_stick_topic( $topic_id );
     392                        $this->assertStringContainsString( 'class="bbp-topic-sticky-link">Unstick</a>', bbp_get_topic_stick_link( $args ) );
     393                } );
    306394        }
    307395
    … …  
    309397         * @covers ::bbp_topic_merge_link
    310398         * @covers ::bbp_get_topic_merge_link
    311          * @todo   Implement test_bbp_get_topic_merge_link().
    312399         */
    313400        public function test_bbp_get_topic_merge_link() {
    314                 // Remove the following lines when you implement this test.
    315                 $this->markTestIncomplete(
    316                         'This test has not been implemented yet.'
    317                 );
     401                $this->with_keymaster_topic( function( $topic_id ) {
     402                        $args = array( 'id' => $topic_id );
     403                        $html = bbp_get_topic_merge_link( $args );
     404                        $this->assertStringContainsString( 'class="bbp-topic-merge-link">Merge</a>', $html );
     405                        $this->assertStringContainsString( 'action=merge', $html );
     406                        $this->expectOutputString( $html );
     407                        bbp_topic_merge_link( $args );
     408                } );
    318409        }
    319410
    … …  
    321412         * @covers ::bbp_topic_spam_link
    322413         * @covers ::bbp_get_topic_spam_link
    323          * @todo   Implement test_bbp_get_topic_spam_link().
    324414         */
    325415        public function test_bbp_get_topic_spam_link() {
    326                 // Remove the following lines when you implement this test.
    327                 $this->markTestIncomplete(
    328                         'This test has not been implemented yet.'
    329                 );
     416                $this->with_keymaster_topic( function( $topic_id ) {
     417                        $args = array( 'id' => $topic_id );
     418                        $html = bbp_get_topic_spam_link( $args );
     419                        $this->assertStringContainsString( 'class="bbp-topic-spam-link">Spam</a>', $html );
     420                        $this->expectOutputString( $html );
     421                        bbp_topic_spam_link( $args );
     422                        bbp_spam_topic( $topic_id );
     423                        $this->assertStringContainsString( 'class="bbp-topic-spam-link">Unspam</a>', bbp_get_topic_spam_link( $args ) );
     424                } );
    330425        }
    331426
    … …  
    333428         * @covers ::bbp_topic_reply_link
    334429         * @covers ::bbp_get_topic_reply_link
    335          * @todo   Implement test_bbp_get_topic_reply_link().
    336430         */
    337431        public function test_bbp_get_topic_reply_link() {
    338                 // Remove the following lines when you implement this test.
    339                 $this->markTestIncomplete(
    340                         'This test has not been implemented yet.'
    341                 );
     432                $this->with_keymaster_topic( function( $topic_id ) {
     433                        $args = array( 'id' => $topic_id, 'reply_text' => 'Respond' );
     434                        $html = bbp_get_topic_reply_link( $args );
     435                        $this->assertStringContainsString( 'class="bbp-topic-reply-link"', $html );
     436                        $this->assertStringContainsString( '#new-post', $html );
     437                        $this->assertStringContainsString( '>Respond</a>', $html );
     438                        $this->expectOutputString( $html );
     439                        bbp_topic_reply_link( $args );
     440                } );
    342441        }
    343442
    … …  
    345444         * @covers ::bbp_forum_pagination_links
    346445         * @covers ::bbp_get_forum_pagination_links
    347          * @todo   Implement test_bbp_get_forum_pagination_links().
    348446         */
    349447        public function test_bbp_get_forum_pagination_links() {
    350                 // Remove the following lines when you implement this test.
    351                 $this->markTestIncomplete(
    352                         'This test has not been implemented yet.'
    353                 );
     448                $old_query = bbpress()->topic_query;
     449                try {
     450                        bbpress()->topic_query = null;
     451                        $this->assertFalse( bbp_get_forum_pagination_links() );
     452
     453                        bbpress()->topic_query = (object) array( 'pagination_links' => '<a href="?page=2">2</a>' );
     454                        $this->assertSame( '<a href="?page=2">2</a>', bbp_get_forum_pagination_links() );
     455                        $this->expectOutputString( '<a href="?page=2">2</a>' );
     456                        bbp_forum_pagination_links();
     457                } finally {
     458                        bbpress()->topic_query = $old_query;
     459                }
    354460        }
    355461}
Note: See TracChangeset for help on using the changeset viewer.