Skip to:
Content

bbPress.org

Changeset 7794


Ignore:
Timestamp:
10/04/2026 08:58:32 AM (5 weeks ago)
Author:
johnjamesjacoby
Message:

Tests: Cover user request and anonymous helpers.

Replace six user-function stubs with assertions for login redirect selection, anonymous posting eligibility, commenter cookie reads and output escaping, IP sanitization, and user-agent length limits.

In trunk, for 2.7.

See #3695.

File:
1 edited

Legend:

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

    r7698 r7794  
    1818        /**
    1919         * @covers ::bbp_redirect_login
    20          * @todo   Implement test_bbp_redirect_login().
    2120         */
    2221        public function test_bbp_redirect_login() {
    23                 // Remove the following lines when you implement this test.
    24                 $this->markTestIncomplete(
    25                         'This test has not been implemented yet.'
    26                 );
     22                $this->assertSame( home_url(), bbp_redirect_login() );
     23                $this->assertSame( home_url(), bbp_redirect_login( admin_url() ) );
     24                $this->assertSame( 'https://example.org/target', bbp_redirect_login( home_url(), 'https://example.org/target' ) );
     25                $this->assertSame( 'https://example.org/manual', bbp_redirect_login( 'https://example.org/manual' ) );
    2726        }
    2827
    2928        /**
    3029         * @covers ::bbp_is_anonymous
    31          * @todo   Implement test_bbp_is_anonymous().
    3230         */
    3331        public function test_bbp_is_anonymous() {
    34                 // Remove the following lines when you implement this test.
    35                 $this->markTestIncomplete(
    36                         'This test has not been implemented yet.'
    37                 );
     32                $old_user = get_current_user_id();
     33                add_filter( 'bbp_allow_anonymous', '__return_true' );
     34
     35                try {
     36                        $this->set_current_user( 0 );
     37                        $this->assertTrue( bbp_is_anonymous() );
     38                        $this->set_current_user( $this->factory->user->create() );
     39                        $this->assertFalse( bbp_is_anonymous() );
     40                        $this->set_current_user( 0 );
     41                        remove_filter( 'bbp_allow_anonymous', '__return_true' );
     42                        $this->assertFalse( bbp_is_anonymous() );
     43                } finally {
     44                        remove_filter( 'bbp_allow_anonymous', '__return_true' );
     45                        $this->set_current_user( $old_user );
     46                }
    3847        }
    3948
    4049        /**
    4150         * @covers ::bbp_current_anonymous_user_data
    42          * @todo   Implement test_bbp_current_anonymous_user_data().
    4351         */
    4452        public function test_bbp_current_anonymous_user_data() {
    45                 // Remove the following lines when you implement this test.
    46                 $this->markTestIncomplete(
    47                         'This test has not been implemented yet.'
    48                 );
     53                $name = 'comment_author_' . COOKIEHASH;
     54                $old  = isset( $_COOKIE[ $name ] ) ? $_COOKIE[ $name ] : null;
     55
     56                try {
     57                        $_COOKIE[ $name ] = 'A & B';
     58                        $this->expectOutputString( 'A & B' );
     59                        bbp_current_anonymous_user_data( 'name' );
     60                } finally {
     61                        if ( null === $old ) {
     62                                unset( $_COOKIE[ $name ] );
     63                        } else {
     64                                $_COOKIE[ $name ] = $old;
     65                        }
     66                }
    4967        }
    5068
    5169        /**
    5270         * @covers ::bbp_get_current_anonymous_user_data
    53          * @todo   Implement test_bbp_get_current_anonymous_user_data().
    5471         */
    5572        public function test_bbp_get_current_anonymous_user_data() {
    56                 // Remove the following lines when you implement this test.
    57                 $this->markTestIncomplete(
    58                         'This test has not been implemented yet.'
    59                 );
     73                $name = 'comment_author_' . COOKIEHASH;
     74                $old  = isset( $_COOKIE[ $name ] ) ? $_COOKIE[ $name ] : null;
     75
     76                try {
     77                        $_COOKIE[ $name ] = 'Guest';
     78                        $this->assertSame( 'Guest', bbp_get_current_anonymous_user_data( 'name' ) );
     79                        $this->assertSame( 'Guest', bbp_get_current_anonymous_user_data( 'comment_author' ) );
     80                        $this->assertSame( 'Guest', bbp_get_current_anonymous_user_data()['comment_author'] );
     81                        $this->assertIsArray( bbp_get_current_anonymous_user_data( 'unknown' ) );
     82                } finally {
     83                        if ( null === $old ) {
     84                                unset( $_COOKIE[ $name ] );
     85                        } else {
     86                                $_COOKIE[ $name ] = $old;
     87                        }
     88                }
    6089        }
    6190
    … …  
    73102        /**
    74103         * @covers ::bbp_current_author_ip
    75          * @todo   Implement test_bbp_current_author_ip().
    76104         */
    77105        public function test_bbp_current_author_ip() {
    78                 // Remove the following lines when you implement this test.
    79                 $this->markTestIncomplete(
    80                         'This test has not been implemented yet.'
    81                 );
     106                $old_address = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null;
     107
     108                try {
     109                        unset( $_SERVER['REMOTE_ADDR'] );
     110                        $this->assertSame( '127.0.0.1', bbp_current_author_ip() );
     111                        $_SERVER['REMOTE_ADDR'] = '2001:db8::1<script>';
     112                        $this->assertSame( '2001:db8::1c', bbp_current_author_ip() );
     113                } finally {
     114                        if ( null === $old_address ) {
     115                                unset( $_SERVER['REMOTE_ADDR'] );
     116                        } else {
     117                                $_SERVER['REMOTE_ADDR'] = $old_address;
     118                        }
     119                }
    82120        }
    83121
    84122        /**
    85123         * @covers ::bbp_current_author_ua
    86          * @todo   Implement test_bbp_current_author_ua().
    87124         */
    88125        public function test_bbp_current_author_ua() {
    89                 // Remove the following lines when you implement this test.
    90                 $this->markTestIncomplete(
    91                         'This test has not been implemented yet.'
    92                 );
     126                $old_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : null;
     127
     128                try {
     129                        unset( $_SERVER['HTTP_USER_AGENT'] );
     130                        $this->assertSame( '', bbp_current_author_ua() );
     131                        $_SERVER['HTTP_USER_AGENT'] = str_repeat( 'a', 255 );
     132                        $this->assertSame( str_repeat( 'a', 254 ), bbp_current_author_ua() );
     133                } finally {
     134                        if ( null === $old_agent ) {
     135                                unset( $_SERVER['HTTP_USER_AGENT'] );
     136                        } else {
     137                                $_SERVER['HTTP_USER_AGENT'] = $old_agent;
     138                        }
     139                }
    93140        }
    94141
Note: See TracChangeset for help on using the changeset viewer.