Skip to:
Content

bbPress.org

Changeset 7803


Ignore:
Timestamp:
10/04/2026 09:31:30 AM (5 weeks ago)
Author:
johnjamesjacoby
Message:

Tests: Cover reply metadata and lifecycle helpers.

Replace thirteen reply-function stubs with assertions for update metadata, revision logs, lifecycle hooks, pagination options, autoembed setup, lead-topic SQL, and raw positions.

In trunk, for 2.7.

See #3695.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/replies/functions/reply.php

    r7727 r7803  
    1010class BBP_Tests_Replies_Functions_Reply extends BBP_UnitTestCase {
    1111
     12        private function assert_reply_action( $function, $hook ) {
     13                $topic_id = $this->factory->topic->create();
     14                $reply_id = $this->factory->reply->create( array( 'post_parent' => $topic_id ) );
     15                $seen     = array();
     16                $callback = function ( $id ) use ( &$seen ) {
     17                        $seen[] = $id;
     18                };
     19                add_action( $hook, $callback );
     20
     21                try {
     22                        $this->assertFalse( $function( PHP_INT_MAX ) );
     23                        $function( $reply_id );
     24                        $this->assertSame( array( $reply_id ), $seen );
     25                } finally {
     26                        remove_action( $hook, $callback );
     27                }
     28        }
     29
    1230        /**
    1331         * @group canonical
    … …  
    7694        /**
    7795         * @covers ::bbp_update_reply
    78          * @todo   Implement test_bbp_update_reply().
    7996         */
    8097        public function test_bbp_update_reply() {
    81                 // Remove the following lines when you implement this test.
    82                 $this->markTestIncomplete(
    83                         'This test has not been implemented yet.'
    84                 );
     98                $old_user = get_current_user_id();
     99                $user_id  = $this->factory->user->create();
     100                $forum_id = $this->factory->forum->create();
     101                $topic_id = $this->factory->topic->create( array( 'post_parent' => $forum_id ) );
     102                $reply_id = $this->factory->reply->create( array( 'post_parent' => $topic_id, 'post_author' => $user_id ) );
     103                update_post_meta( $reply_id, '_edit_lock', 'stale' );
     104                delete_post_meta( $reply_id, '_bbp_forum_id' );
     105                delete_post_meta( $reply_id, '_bbp_topic_id' );
     106                $this->set_current_user( $user_id );
     107
     108                try {
     109                        bbp_update_reply( $reply_id, $topic_id, $forum_id, array(), $user_id, true );
     110                        $this->assertSame( (string) $user_id, get_post_meta( $reply_id, '_edit_last', true ) );
     111                        $this->assertFalse( metadata_exists( 'post', $reply_id, '_edit_lock' ) );
     112                        $this->assertSame( (string) $forum_id, get_post_meta( $reply_id, '_bbp_forum_id', true ) );
     113                        $this->assertSame( (string) $topic_id, get_post_meta( $reply_id, '_bbp_topic_id', true ) );
     114                        delete_post_meta( $reply_id, '_bbp_author_ip' );
     115                        bbp_update_reply( $reply_id, $topic_id, $forum_id, array(), $user_id, false );
     116                        $this->assertNotEmpty( get_post_meta( $reply_id, '_bbp_author_ip', true ) );
     117                } finally {
     118                        $this->set_current_user( $old_user );
     119                }
    85120        }
    86121
    … …  
    233268        /**
    234269         * @covers ::bbp_update_reply_revision_log
    235          * @todo   Implement test_bbp_update_reply_revision_log().
    236270         */
    237271        public function test_bbp_update_reply_revision_log() {
    238                 // Remove the following lines when you implement this test.
    239                 $this->markTestIncomplete(
    240                         'This test has not been implemented yet.'
    241                 );
     272                $topic_id = $this->factory->topic->create();
     273                $reply_id = $this->factory->reply->create( array( 'post_parent' => $topic_id ) );
     274                $user_id  = $this->factory->user->create();
     275                $log      = bbp_update_reply_revision_log( array( 'reply_id' => $reply_id, 'author_id' => $user_id, 'revision_id' => 17, 'reason' => 'Clarify wording' ) );
     276                $this->assertSame( $user_id, $log[17]['author'] );
     277                $this->assertSame( 'Clarify wording', $log[17]['reason'] );
     278                $this->assertSame( $log, bbp_get_reply_raw_revision_log( $reply_id ) );
    242279        }
    243280
    … …  
    391428        /**
    392429         * @covers ::bbp_delete_reply
    393          * @todo   Implement test_bbp_delete_reply().
    394430         */
    395431        public function test_bbp_delete_reply() {
    396                 // Remove the following lines when you implement this test.
    397                 $this->markTestIncomplete(
    398                         'This test has not been implemented yet.'
    399                 );
     432                $this->assert_reply_action( 'bbp_delete_reply', 'bbp_delete_reply' );
    400433        }
    401434
    402435        /**
    403436         * @covers ::bbp_trash_reply
    404          * @todo   Implement test_bbp_trash_reply().
    405437         */
    406438        public function test_bbp_trash_reply() {
    407                 // Remove the following lines when you implement this test.
    408                 $this->markTestIncomplete(
    409                         'This test has not been implemented yet.'
    410                 );
     439                $this->assert_reply_action( 'bbp_trash_reply', 'bbp_trash_reply' );
    411440        }
    412441
    413442        /**
    414443         * @covers ::bbp_untrash_reply
    415          * @todo   Implement test_bbp_untrash_reply().
    416444         */
    417445        public function test_bbp_untrash_reply() {
    418                 // Remove the following lines when you implement this test.
    419                 $this->markTestIncomplete(
    420                         'This test has not been implemented yet.'
    421                 );
     446                $this->assert_reply_action( 'bbp_untrash_reply', 'bbp_untrash_reply' );
    422447        }
    423448
    424449        /**
    425450         * @covers ::bbp_deleted_reply
    426          * @todo   Implement test_bbp_deleted_reply().
    427451         */
    428452        public function test_bbp_deleted_reply() {
    429                 // Remove the following lines when you implement this test.
    430                 $this->markTestIncomplete(
    431                         'This test has not been implemented yet.'
    432                 );
     453                $this->assert_reply_action( 'bbp_deleted_reply', 'bbp_deleted_reply' );
    433454        }
    434455
    435456        /**
    436457         * @covers ::bbp_trashed_reply
    437          * @todo   Implement test_bbp_trashed_reply().
    438458         */
    439459        public function test_bbp_trashed_reply() {
    440                 // Remove the following lines when you implement this test.
    441                 $this->markTestIncomplete(
    442                         'This test has not been implemented yet.'
    443                 );
     460                $this->assert_reply_action( 'bbp_trashed_reply', 'bbp_trashed_reply' );
    444461        }
    445462
    446463        /**
    447464         * @covers ::bbp_untrashed_reply
    448          * @todo   Implement test_bbp_untrashed_reply().
    449465         */
    450466        public function test_bbp_untrashed_reply() {
    451                 // Remove the following lines when you implement this test.
    452                 $this->markTestIncomplete(
    453                         'This test has not been implemented yet.'
    454                 );
     467                $this->assert_reply_action( 'bbp_untrashed_reply', 'bbp_untrashed_reply' );
    455468        }
    456469
    457470        /**
    458471         * @covers ::bbp_get_replies_per_page
    459          * @todo   Implement test_bbp_get_replies_per_page().
    460472         */
    461473        public function test_bbp_get_replies_per_page() {
    462                 // Remove the following lines when you implement this test.
    463                 $this->markTestIncomplete(
    464                         'This test has not been implemented yet.'
    465                 );
     474                update_option( '_bbp_replies_per_page', 0 );
     475                $this->assertSame( 12, bbp_get_replies_per_page( 12 ) );
     476                update_option( '_bbp_replies_per_page', '22' );
     477                $this->assertSame( 22, bbp_get_replies_per_page() );
    466478        }
    467479
    468480        /**
    469481         * @covers ::bbp_get_replies_per_rss_page
    470          * @todo   Implement test_bbp_get_replies_per_rss_page().
    471482         */
    472483        public function test_bbp_get_replies_per_rss_page() {
    473                 // Remove the following lines when you implement this test.
    474                 $this->markTestIncomplete(
    475                         'This test has not been implemented yet.'
    476                 );
     484                update_option( '_bbp_replies_per_rss_page', 0 );
     485                $this->assertSame( 18, bbp_get_replies_per_rss_page( 18 ) );
     486                update_option( '_bbp_replies_per_rss_page', '30' );
     487                $this->assertSame( 30, bbp_get_replies_per_rss_page() );
    477488        }
    478489
    479490        /**
    480491         * @covers ::bbp_reply_content_autoembed
    481          * @todo   Implement test_bbp_reply_content_autoembed().
    482492         */
    483493        public function test_bbp_reply_content_autoembed() {
    484                 // Remove the following lines when you implement this test.
    485                 $this->markTestIncomplete(
    486                         'This test has not been implemented yet.'
    487                 );
     494                global $wp_embed;
     495
     496                $callback = array( $wp_embed, 'autoembed' );
     497                $priority = has_filter( 'bbp_get_reply_content', $callback );
     498                remove_filter( 'bbp_get_reply_content', $callback, $priority );
     499                add_filter( 'bbp_use_autoembed', '__return_false' );
     500
     501                try {
     502                        bbp_reply_content_autoembed();
     503                        $this->assertFalse( has_filter( 'bbp_get_reply_content', $callback ) );
     504                        remove_filter( 'bbp_use_autoembed', '__return_false' );
     505                        add_filter( 'bbp_use_autoembed', '__return_true' );
     506                        bbp_reply_content_autoembed();
     507                        $this->assertSame( 2, has_filter( 'bbp_get_reply_content', $callback ) );
     508                } finally {
     509                        remove_filter( 'bbp_use_autoembed', '__return_false' );
     510                        remove_filter( 'bbp_use_autoembed', '__return_true' );
     511                        remove_filter( 'bbp_get_reply_content', $callback, 2 );
     512                        if ( false !== $priority ) {
     513                                add_filter( 'bbp_get_reply_content', $callback, $priority );
     514                        }
     515                }
    488516        }
    489517
    490518        /**
    491519         * @covers ::_bbp_has_replies_where
    492          * @todo   Implement test_bbp_has_replies_where().
    493520         */
    494521        public function test_bbp_has_replies_where() {
    495                 // Remove the following lines when you implement this test.
    496                 $this->markTestIncomplete(
    497                         'This test has not been implemented yet.'
    498                 );
     522                $topic_id = $this->factory->topic->create();
     523                $posts    = bbp_db()->prefix . 'posts';
     524                $where    = "WHERE 1=1  AND {$posts}.post_parent = {$topic_id}";
     525                $query    = new WP_Query();
     526                $query->set( 'post_parent', $topic_id );
     527                $query->set( 'post_type', array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ) );
     528
     529                $this->assertSame( $where, _bbp_has_replies_where( $where, false ) );
     530                $this->assertSame( "WHERE 1=1 AND ({$posts}.ID = {$topic_id} OR {$posts}.post_parent = {$topic_id})", _bbp_has_replies_where( $where, $query ) );
     531                $query->set( 'post__in', array( $topic_id ) );
     532                $this->assertSame( $where, _bbp_has_replies_where( $where, $query ) );
    499533        }
    500534
    … …  
    534568        /**
    535569         * @covers ::bbp_get_reply_position_raw
    536          * @todo   Implement test_bbp_get_reply_position_raw().
    537570         */
    538571        public function test_bbp_get_reply_position_raw() {
    539                 // Remove the following lines when you implement this test.
    540                 $this->markTestIncomplete(
    541                         'This test has not been implemented yet.'
    542                 );
     572                $topic_id = $this->factory->topic->create();
     573                $first_id = $this->factory->reply->create( array( 'post_parent' => $topic_id ) );
     574                $next_id  = $this->factory->reply->create( array( 'post_parent' => $topic_id ) );
     575                $this->assertSame( 0, bbp_get_reply_position_raw( $topic_id, $topic_id ) );
     576                $this->assertSame( 1, bbp_get_reply_position_raw( $first_id, $topic_id ) );
     577                $this->assertSame( 2, bbp_get_reply_position_raw( $next_id, $topic_id ) );
    543578        }
    544579
Note: See TracChangeset for help on using the changeset viewer.