Skip to:
Content

bbPress.org

Changeset 7776


Ignore:
Timestamp:
10/04/2026 03:44:23 AM (5 weeks ago)
Author:
johnjamesjacoby
Message:

Tests: Cover forum lifecycle helpers.

Replace nine incomplete tests with coverage for forum topic trash and restore, subscription cleanup, and lifecycle actions. Verify that restoring a forum leaves previously trashed topics alone.

In trunk, for 2.7.

See #3695.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/forums/functions/forum.php

    r7455 r7776  
    99 */
    1010class BBP_Tests_Forums_Functions_Forum extends BBP_UnitTestCase {
     11
     12        /**
     13         * Assert a forum lifecycle wrapper validates its target and fires its action.
     14         *
     15         * @param string $function Wrapper function.
     16         * @param string $action   Action name.
     17         */
     18        protected function assert_forum_action_dispatches( $function, $action ) {
     19                $forum_id = $this->factory->forum->create();
     20                $observed = array();
     21                $callback = function( $id ) use ( &$observed ) {
     22                        $observed[] = $id;
     23                };
     24
     25                add_action( $action, $callback );
     26                try {
     27                        $this->assertFalse( call_user_func( $function, 0 ) );
     28                        call_user_func( $function, $forum_id );
     29                } finally {
     30                        remove_action( $action, $callback );
     31                }
     32
     33                $this->assertSame( array( $forum_id ), $observed );
     34        }
    1135
    1236        /**
    … …  
    156180        /**
    157181         * @covers ::bbp_remove_forum_from_all_subscriptions
    158          * @todo   Implement test_bbp_remove_forum_from_all_subscriptions().
    159182         */
    160183        public function test_bbp_remove_forum_from_all_subscriptions() {
    161                 // Remove the following lines when you implement this test.
    162                 $this->markTestIncomplete(
    163                         'This test has not been implemented yet.'
    164                 );
     184                $forum_id = $this->factory->forum->create();
     185                $user_ids = $this->factory->user->create_many( 2 );
     186
     187                foreach ( $user_ids as $user_id ) {
     188                        $this->assertTrue( bbp_add_user_subscription( $user_id, $forum_id ) );
     189                        $this->assertTrue( bbp_is_user_subscribed( $user_id, $forum_id ) );
     190                }
     191
     192                bbp_remove_forum_from_all_subscriptions( $forum_id );
     193                foreach ( $user_ids as $user_id ) {
     194                        $this->assertFalse( bbp_is_user_subscribed( $user_id, $forum_id ) );
     195                }
    165196        }
    166197
    … …  
    256287        /**
    257288         * @covers ::bbp_trash_forum_topics
    258          * @todo   Implement test_bbp_trash_forum_topics().
    259289         */
    260290        public function test_bbp_trash_forum_topics() {
    261                 // Remove the following lines when you implement this test.
    262                 $this->markTestIncomplete(
    263                         'This test has not been implemented yet.'
    264                 );
     291                $forum_id = $this->factory->forum->create();
     292                $topic_id = $this->factory->topic->create( array(
     293                        'post_parent' => $forum_id,
     294                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     295                ) );
     296                $spam_id = $this->factory->topic->create( array(
     297                        'post_parent' => $forum_id,
     298                        'post_status' => bbp_get_spam_status_id(),
     299                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     300                ) );
     301
     302                bbp_trash_forum_topics( $forum_id );
     303
     304                $this->assertSame( 'trash', get_post_status( $topic_id ) );
     305                $this->assertSame( bbp_get_spam_status_id(), get_post_status( $spam_id ) );
     306                $this->assertSame( array( $topic_id ), get_post_meta( $forum_id, '_bbp_pre_trashed_topics', true ) );
    265307        }
    266308
    267309        /**
    268310         * @covers ::bbp_untrash_forum_topics
    269          * @todo   Implement test_bbp_untrash_forum_topics().
    270311         */
    271312        public function test_bbp_untrash_forum_topics() {
    272                 // Remove the following lines when you implement this test.
    273                 $this->markTestIncomplete(
    274                         'This test has not been implemented yet.'
    275                 );
     313                $forum_id = $this->factory->forum->create();
     314                $topic_id = $this->factory->topic->create( array(
     315                        'post_parent' => $forum_id,
     316                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     317                ) );
     318                $already_trashed_id = $this->factory->topic->create( array(
     319                        'post_parent' => $forum_id,
     320                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     321                ) );
     322                wp_trash_post( $already_trashed_id );
     323                bbp_trash_forum_topics( $forum_id );
     324
     325                $this->assertSame( 'trash', get_post_status( $topic_id ) );
     326                bbp_untrash_forum_topics( $forum_id );
     327                $this->assertSame( bbp_get_public_status_id(), get_post_status( $topic_id ) );
     328                $this->assertSame( 'trash', get_post_status( $already_trashed_id ) );
     329        }
     330
     331        /**
     332         * @covers ::bbp_trash_forum
     333         * @covers ::bbp_untrash_forum
     334         * @covers ::bbp_trash_forum_topics
     335         * @covers ::bbp_untrash_forum_topics
     336         * @covers ::bbp_remove_forum_from_all_subscriptions
     337         */
     338        public function test_forum_trash_restores_only_its_topics_and_removes_subscriptions() {
     339                $forum_id = $this->factory->forum->create();
     340                $topic_id = $this->factory->topic->create( array(
     341                        'post_parent' => $forum_id,
     342                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     343                ) );
     344                $already_trashed_id = $this->factory->topic->create( array(
     345                        'post_parent' => $forum_id,
     346                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     347                ) );
     348                $user_id = $this->factory->user->create();
     349                $this->assertTrue( bbp_add_user_subscription( $user_id, $forum_id ) );
     350                wp_trash_post( $already_trashed_id );
     351
     352                wp_trash_post( $forum_id );
     353                $this->assertSame( 'trash', get_post_status( $forum_id ) );
     354                $this->assertSame( 'trash', get_post_status( $topic_id ) );
     355                $this->assertFalse( bbp_is_user_subscribed( $user_id, $forum_id ) );
     356
     357                wp_untrash_post( $forum_id );
     358                $this->assertSame( bbp_get_public_status_id(), get_post_status( $forum_id ) );
     359                $this->assertSame( bbp_get_public_status_id(), get_post_status( $topic_id ) );
     360                $this->assertSame( 'trash', get_post_status( $already_trashed_id ) );
    276361        }
    277362
    278363        /**
    279364         * @covers ::bbp_delete_forum
    280          * @todo   Implement test_bbp_delete_forum().
    281365         */
    282366        public function test_bbp_delete_forum() {
    283                 // Remove the following lines when you implement this test.
    284                 $this->markTestIncomplete(
    285                         'This test has not been implemented yet.'
    286                 );
     367                $this->assert_forum_action_dispatches( 'bbp_delete_forum', 'bbp_delete_forum' );
    287368        }
    288369
    289370        /**
    290371         * @covers ::bbp_trash_forum
    291          * @todo   Implement test_bbp_trash_forum().
    292372         */
    293373        public function test_bbp_trash_forum() {
    294                 // Remove the following lines when you implement this test.
    295                 $this->markTestIncomplete(
    296                         'This test has not been implemented yet.'
    297                 );
     374                $this->assert_forum_action_dispatches( 'bbp_trash_forum', 'bbp_trash_forum' );
    298375        }
    299376
    300377        /**
    301378         * @covers ::bbp_untrash_forum
    302          * @todo   Implement test_bbp_untrash_forum().
    303379         */
    304380        public function test_bbp_untrash_forum() {
    305                 // Remove the following lines when you implement this test.
    306                 $this->markTestIncomplete(
    307                         'This test has not been implemented yet.'
    308                 );
     381                $this->assert_forum_action_dispatches( 'bbp_untrash_forum', 'bbp_untrash_forum' );
    309382        }
    310383
    311384        /**
    312385         * @covers ::bbp_deleted_forum
    313          * @todo   Implement test_bbp_deleted_forum().
    314386         */
    315387        public function test_bbp_deleted_forum() {
    316                 // Remove the following lines when you implement this test.
    317                 $this->markTestIncomplete(
    318                         'This test has not been implemented yet.'
    319                 );
     388                $forum_id = $this->factory->forum->create();
     389                $forum    = get_post( $forum_id );
     390                $observed = array();
     391                $callback = function( $id, $post ) use ( &$observed ) {
     392                        $observed[] = array( $id, $post );
     393                };
     394
     395                add_action( 'bbp_deleted_forum', $callback, 10, 2 );
     396                try {
     397                        $this->assertFalse( bbp_deleted_forum( 0 ) );
     398                        bbp_deleted_forum( $forum_id, $forum );
     399                } finally {
     400                        remove_action( 'bbp_deleted_forum', $callback );
     401                }
     402
     403                $this->assertSame( array( array( $forum_id, $forum ) ), $observed );
    320404        }
    321405
    322406        /**
    323407         * @covers ::bbp_trashed_forum
    324          * @todo   Implement test_bbp_trashed_forum().
    325408         */
    326409        public function test_bbp_trashed_forum() {
    327                 // Remove the following lines when you implement this test.
    328                 $this->markTestIncomplete(
    329                         'This test has not been implemented yet.'
    330                 );
     410                $this->assert_forum_action_dispatches( 'bbp_trashed_forum', 'bbp_trashed_forum' );
    331411        }
    332412
    333413        /**
    334414         * @covers ::bbp_untrashed_forum
    335          * @todo   Implement test_bbp_untrashed_forum().
    336415         */
    337416        public function test_bbp_untrashed_forum() {
    338                 // Remove the following lines when you implement this test.
    339                 $this->markTestIncomplete(
    340                         'This test has not been implemented yet.'
    341                 );
     417                $this->assert_forum_action_dispatches( 'bbp_untrashed_forum', 'bbp_untrashed_forum' );
    342418        }
    343419}
Note: See TracChangeset for help on using the changeset viewer.