Skip to:
Content

bbPress.org

Changeset 7764


Ignore:
Timestamp:
10/01/2026 10:40:54 PM (3 weeks ago)
Author:
johnjamesjacoby
Message:

Tests: Cover user option behavior.

Replace seven incomplete user-option tests with assertions for defaults, option updates and deletion, filter setup and overrides, last-posted values, and multisite isolation. Restore hook and global state after each test.

In trunk, for 2.7.
See #3695.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/users/functions/options.php

    r5887 r7764  
    22
    33/**
    4  * Tests for the user component count functions.
     4 * Tests for the user component option functions.
    55 *
    66 * @group users
    … …  
    1212        /**
    1313         * @covers ::bbp_get_default_user_options
    14          * @todo   Implement test_bbp_get_default_user_options().
    1514         */
    1615        public function test_bbp_get_default_user_options() {
    17                 // Remove the following lines when you implement this test.
    18                 $this->markTestIncomplete(
    19                         'This test has not been implemented yet.'
     16                $defaults = array(
     17                        '_bbp_last_posted' => '0',
     18                        '_bbp_topic_count' => '0',
     19                        '_bbp_reply_count' => '0',
    2020                );
     21                $this->assertSame( $defaults, bbp_get_default_user_options() );
     22
     23                $filter = function( $options ) {
     24                        $options['_bbp_custom_option'] = 'custom';
     25                        return $options;
     26                };
     27                add_filter( 'bbp_get_default_user_options', $filter );
     28
     29                try {
     30                        $defaults['_bbp_custom_option'] = 'custom';
     31                        $this->assertSame( $defaults, bbp_get_default_user_options() );
     32                } finally {
     33                        remove_filter( 'bbp_get_default_user_options', $filter );
     34                }
     35
     36                $scalar_filter = function() {
     37                        return 'scalar';
     38                };
     39                add_filter( 'bbp_get_default_user_options', $scalar_filter );
     40                try {
     41                        $this->assertSame( array( 'scalar' ), bbp_get_default_user_options() );
     42                } finally {
     43                        remove_filter( 'bbp_get_default_user_options', $scalar_filter );
     44                }
    2145        }
    2246
    2347        /**
    2448         * @covers ::bbp_add_user_options
    25          * @todo   Implement test_bbp_add_user_options().
    2649         */
    2750        public function test_bbp_add_user_options() {
    28                 // Remove the following lines when you implement this test.
    29                 $this->markTestIncomplete(
    30                         'This test has not been implemented yet.'
    31                 );
     51                $user_id = $this->factory->user->create();
     52                $called  = null;
     53                $action  = function( $id ) use ( &$called ) {
     54                        $called = $id;
     55                };
     56                $filter  = function( $options ) {
     57                        $options['_bbp_custom_option'] = 'custom';
     58                        return $options;
     59                };
     60
     61                update_user_option( $user_id, '_bbp_topic_count', '9' );
     62                update_user_option( $user_id, 'unrelated_option', 'keep' );
     63                add_action( 'bbp_add_user_options', $action );
     64                add_filter( 'bbp_get_default_user_options', $filter );
     65
     66                try {
     67                        $invalid_user = function() {
     68                                return 0;
     69                        };
     70                        add_filter( 'bbp_get_user_id', $invalid_user );
     71                        try {
     72                                bbp_add_user_options( 0 );
     73                        } finally {
     74                                remove_filter( 'bbp_get_user_id', $invalid_user );
     75                        }
     76                        $this->assertNull( $called );
     77                        bbp_add_user_options( $user_id );
     78                } finally {
     79                        remove_action( 'bbp_add_user_options', $action );
     80                        remove_filter( 'bbp_get_default_user_options', $filter );
     81                }
     82
     83                $this->assertSame( $user_id, $called );
     84                $this->assertSame( '0', get_user_option( '_bbp_last_posted', $user_id ) );
     85                $this->assertSame( '0', get_user_option( '_bbp_topic_count', $user_id ) );
     86                $this->assertSame( '0', get_user_option( '_bbp_reply_count', $user_id ) );
     87                $this->assertSame( 'custom', get_user_option( '_bbp_custom_option', $user_id ) );
     88                $this->assertSame( 'keep', get_user_option( 'unrelated_option', $user_id ) );
    3289        }
    3390
    3491        /**
    3592         * @covers ::bbp_delete_user_options
    36          * @todo   Implement test_bbp_delete_user_options().
    3793         */
    3894        public function test_bbp_delete_user_options() {
    39                 // Remove the following lines when you implement this test.
    40                 $this->markTestIncomplete(
    41                         'This test has not been implemented yet.'
    42                 );
     95                $user_id = $this->factory->user->create();
     96                $called  = null;
     97                $action  = function( $id ) use ( &$called ) {
     98                        $called = $id;
     99                };
     100                $filter  = function( $options ) {
     101                        $options['_bbp_custom_option'] = 'custom';
     102                        return $options;
     103                };
     104
     105                add_filter( 'bbp_get_default_user_options', $filter );
     106                add_action( 'bbp_delete_user_options', $action );
     107
     108                try {
     109                        bbp_add_user_options( $user_id );
     110                        update_user_option( $user_id, 'unrelated_option', 'keep' );
     111                        $invalid_user = function() {
     112                                return 0;
     113                        };
     114                        add_filter( 'bbp_get_user_id', $invalid_user );
     115                        try {
     116                                bbp_delete_user_options( 0 );
     117                        } finally {
     118                                remove_filter( 'bbp_get_user_id', $invalid_user );
     119                        }
     120                        $this->assertNull( $called );
     121                        $this->assertSame( '0', get_user_option( '_bbp_topic_count', $user_id ) );
     122                        bbp_delete_user_options( $user_id );
     123                } finally {
     124                        remove_action( 'bbp_delete_user_options', $action );
     125                        remove_filter( 'bbp_get_default_user_options', $filter );
     126                }
     127
     128                $this->assertSame( $user_id, $called );
     129                $this->assertFalse( get_user_option( '_bbp_last_posted', $user_id ) );
     130                $this->assertFalse( get_user_option( '_bbp_topic_count', $user_id ) );
     131                $this->assertFalse( get_user_option( '_bbp_reply_count', $user_id ) );
     132                $this->assertFalse( get_user_option( '_bbp_custom_option', $user_id ) );
     133                $this->assertSame( 'keep', get_user_option( 'unrelated_option', $user_id ) );
    43134        }
    44135
    45136        /**
    46137         * @covers ::bbp_setup_user_option_filters
    47          * @todo   Implement test_bbp_setup_user_option_filters().
    48138         */
    49139        public function test_bbp_setup_user_option_filters() {
    50                 // Remove the following lines when you implement this test.
    51                 $this->markTestIncomplete(
    52                         'This test has not been implemented yet.'
    53                 );
     140                $keys        = array_keys( bbp_get_default_user_options() );
     141                $priorities  = array();
     142                $old_options = bbpress()->user_options;
     143                $called      = 0;
     144                $action      = function() use ( &$called ) {
     145                        ++$called;
     146                };
     147
     148                foreach ( $keys as $key ) {
     149                        $priorities[ $key ] = has_filter( 'get_user_option_' . $key, 'bbp_filter_get_user_option' );
     150                }
     151
     152                add_action( 'bbp_setup_user_option_filters', $action );
     153                try {
     154                        foreach ( $priorities as $key => $priority ) {
     155                                if ( false !== $priority ) {
     156                                        remove_filter( 'get_user_option_' . $key, 'bbp_filter_get_user_option', $priority );
     157                                }
     158                                $this->assertFalse( has_filter( 'get_user_option_' . $key, 'bbp_filter_get_user_option' ) );
     159                        }
     160
     161                        bbp_setup_user_option_filters();
     162                        $this->assertSame( 1, $called );
     163                        foreach ( $keys as $key ) {
     164                                $this->assertSame( 10, has_filter( 'get_user_option_' . $key, 'bbp_filter_get_user_option' ) );
     165                        }
     166
     167                        $user_id = $this->factory->user->create();
     168                        update_user_option( $user_id, '_bbp_topic_count', '2' );
     169                        bbpress()->user_options[ $user_id ] = array( '_bbp_topic_count' => '7' );
     170                        $this->assertSame( '7', get_user_option( '_bbp_topic_count', $user_id ) );
     171                } finally {
     172                        remove_action( 'bbp_setup_user_option_filters', $action );
     173                        foreach ( $priorities as $key => $priority ) {
     174                                remove_filter( 'get_user_option_' . $key, 'bbp_filter_get_user_option', 10 );
     175                                if ( false !== $priority ) {
     176                                        add_filter( 'get_user_option_' . $key, 'bbp_filter_get_user_option', $priority, 3 );
     177                                }
     178                        }
     179                        bbpress()->user_options = $old_options;
     180                }
     181        }
     182
     183        /**
     184         * @covers ::bbp_setup_user_option_filters
     185         */
     186        public function test_bbp_setup_user_option_filters_accepts_custom_option() {
     187                $filter = function( $options ) {
     188                        $options['_bbp_custom_option'] = 'custom';
     189                        return $options;
     190                };
     191                add_filter( 'bbp_get_default_user_options', $filter );
     192
     193                try {
     194                        bbp_setup_user_option_filters();
     195                        $this->assertSame( 10, has_filter( 'get_user_option__bbp_custom_option', 'bbp_filter_get_user_option' ) );
     196                } finally {
     197                        remove_filter( 'bbp_get_default_user_options', $filter );
     198                        remove_filter( 'get_user_option__bbp_custom_option', 'bbp_filter_get_user_option', 10 );
     199                }
    54200        }
    55201
    56202        /**
    57203         * @covers ::bbp_filter_get_user_option
    58          * @todo   Implement test_bbp_filter_get_user_option().
    59204         */
    60205        public function test_bbp_filter_get_user_option() {
    61                 // Remove the following lines when you implement this test.
    62                 $this->markTestIncomplete(
    63                         'This test has not been implemented yet.'
    64                 );
     206                $user_id      = $this->factory->user->create();
     207                $other_id     = $this->factory->user->create();
     208                $user         = get_userdata( $user_id );
     209                $other_user   = get_userdata( $other_id );
     210                $old_options  = bbpress()->user_options;
     211
     212                update_user_option( $user_id, '_bbp_topic_count', '2' );
     213
     214                try {
     215                        bbpress()->user_options[ $user_id ] = array(
     216                                '_bbp_topic_count' => '7',
     217                                '_bbp_reply_count' => '0',
     218                        );
     219
     220                        $this->assertSame( '7', bbp_filter_get_user_option( '2', '_bbp_topic_count', $user ) );
     221                        $this->assertSame( '7', get_user_option( '_bbp_topic_count', $user_id ) );
     222                        $this->assertSame( '2', bbp_filter_get_user_option( '2', '_bbp_reply_count', $user ) );
     223                        $this->assertSame( '2', bbp_filter_get_user_option( '2', '_bbp_last_posted', $user ) );
     224                        $this->assertSame( '2', bbp_filter_get_user_option( '2', '_bbp_topic_count', $other_user ) );
     225                        $this->assertSame( '2', bbp_filter_get_user_option( '2', '_bbp_topic_count', $user_id ) );
     226                        $this->assertFalse( bbp_filter_get_user_option( false, '_bbp_topic_count' ) );
     227                } finally {
     228                        bbpress()->user_options = $old_options;
     229                }
    65230        }
    66231
    67232        /**
    68233         * @covers ::bbp_update_user_last_posted
    69          * @todo   Implement test_bbp_update_user_last_posted().
    70234         */
    71235        public function test_bbp_update_user_last_posted() {
    72                 // Remove the following lines when you implement this test.
    73                 $this->markTestIncomplete(
    74                         'This test has not been implemented yet.'
    75                 );
     236                $user_id = $this->factory->user->create();
     237
     238                $invalid_user = function() {
     239                        return 0;
     240                };
     241                add_filter( 'bbp_get_user_id', $invalid_user );
     242                try {
     243                        $this->assertFalse( bbp_update_user_last_posted( 0, 123 ) );
     244                } finally {
     245                        remove_filter( 'bbp_get_user_id', $invalid_user );
     246                }
     247
     248                $this->assertNotFalse( bbp_update_user_last_posted( $user_id, 123 ) );
     249                $this->assertSame( 123, (int) get_user_option( '_bbp_last_posted', $user_id ) );
     250
     251                $before = time();
     252                $this->assertNotFalse( bbp_update_user_last_posted( $user_id ) );
     253                $after  = time();
     254                $stored = (int) get_user_option( '_bbp_last_posted', $user_id );
     255
     256                $this->assertGreaterThanOrEqual( $before, $stored );
     257                $this->assertLessThanOrEqual( $after, $stored );
    76258        }
    77259
    … …  
    79261         * @covers ::bbp_user_last_posted
    80262         * @covers ::bbp_get_user_last_posted
    81          * @todo   Implement test_bbp_get_user_last_posted().
    82263         */
    83264        public function test_bbp_get_user_last_posted() {
    84                 // Remove the following lines when you implement this test.
    85                 $this->markTestIncomplete(
    86                         'This test has not been implemented yet.'
    87                 );
     265                $user_id = $this->factory->user->create();
     266                $called  = 0;
     267                $filter  = function( $value ) use ( &$called ) {
     268                        ++$called;
     269                        return $value;
     270                };
     271                $invalid_user = function() {
     272                        return 0;
     273                };
     274                add_filter( 'bbp_get_user_last_posted', $filter );
     275                add_filter( 'bbp_get_user_id', $invalid_user );
     276                try {
     277                        $this->assertFalse( bbp_get_user_last_posted( 0 ) );
     278                        $this->assertSame( 0, $called );
     279                } finally {
     280                        remove_filter( 'bbp_get_user_id', $invalid_user );
     281                        remove_filter( 'bbp_get_user_last_posted', $filter );
     282                }
     283
     284                delete_user_option( $user_id, '_bbp_last_posted' );
     285                $this->assertFalse( bbp_get_user_last_posted( $user_id ) );
     286
     287                update_user_option( $user_id, '_bbp_last_posted', 123 );
     288                $filter = function( $value, $id ) use ( $user_id ) {
     289                        $this->assertSame( $user_id, $id );
     290                        return (int) $value + 1;
     291                };
     292                add_filter( 'bbp_get_user_last_posted', $filter, 10, 2 );
     293
     294                try {
     295                        $this->assertSame( 124, bbp_get_user_last_posted( $user_id ) );
     296                        $this->expectOutputString( '124' );
     297                        bbp_user_last_posted( $user_id );
     298                } finally {
     299                        remove_filter( 'bbp_get_user_last_posted', $filter, 10 );
     300                }
     301        }
     302
     303        /**
     304         * @covers ::bbp_add_user_options
     305         * @covers ::bbp_delete_user_options
     306         */
     307        public function test_bbp_user_options_are_site_specific() {
     308                if ( ! is_multisite() ) {
     309                        $this->markTestSkipped( 'Requires multisite.' );
     310                }
     311
     312                $user_id = $this->factory->user->create();
     313                $site_id = $this->factory->blog->create();
     314
     315                bbp_add_user_options( $user_id );
     316                bbp_update_user_last_posted( $user_id, 123 );
     317                switch_to_blog( $site_id );
     318
     319                try {
     320                        $this->assertFalse( get_user_option( '_bbp_topic_count', $user_id ) );
     321                        bbp_add_user_options( $user_id );
     322                        update_user_option( $user_id, '_bbp_topic_count', '5' );
     323                        $this->assertSame( '5', get_user_option( '_bbp_topic_count', $user_id ) );
     324                        bbp_delete_user_options( $user_id );
     325                        $this->assertFalse( get_user_option( '_bbp_topic_count', $user_id ) );
     326                } finally {
     327                        restore_current_blog();
     328                }
     329
     330                $this->assertSame( '0', get_user_option( '_bbp_topic_count', $user_id ) );
     331                $this->assertSame( 123, (int) get_user_option( '_bbp_last_posted', $user_id ) );
    88332        }
    89333}
Note: See TracChangeset for help on using the changeset viewer.