Skip to:
Content

bbPress.org

Changeset 7820


Ignore:
Timestamp:
10/04/2026 05:56:10 PM (3 weeks ago)
Author:
johnjamesjacoby
Message:

Tests: Cover anonymous author cookies.

Use a one-request loopback PHP server to inspect the Set-Cookie headers emitted by the anonymous author helper. Verify name, email, and website values without changing runtime behavior.

In trunk, for 2.7.

See #3695.

Location:
trunk/tests/phpunit
Files:
1 added
1 edited

Legend:

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

    r7818 r7820  
    112112        /**
    113113         * @covers ::bbp_set_current_anonymous_user_data
    114          * @todo   Test the Set-Cookie headers from an HTTP request.
    115114         */
    116115        public function test_bbp_set_current_anonymous_user_data() {
    117                 $this->markTestIncomplete( 'The successful cookie path requires an HTTP test harness.' );
     116                $listener = stream_socket_server( 'tcp://127.0.0.1:0', $error_number, $error_string );
     117                $this->assertIsResource( $listener, $error_string );
     118                $address = stream_socket_get_name( $listener, false );
     119                $port    = (int) substr( strrchr( $address, ':' ), 1 );
     120                fclose( $listener );
     121
     122                $runner  = dirname( __DIR__, 3 ) . '/includes/anonymous-cookie-runner.php';
     123                $command = array( PHP_BINARY, '-d', 'output_buffering=1048576', '-S', '127.0.0.1:' . $port, $runner );
     124                $pipes   = array();
     125                $env     = array_merge( getenv(), array( 'BBP_TEST_MULTISITE' => is_multisite() ? '1' : '0' ) );
     126                $server  = proc_open( $command, array(
     127                        0 => array( 'pipe', 'r' ),
     128                        1 => array( 'pipe', 'w' ),
     129                        2 => array( 'pipe', 'w' ),
     130                ), $pipes, dirname( __DIR__, 4 ), $env );
     131                $this->assertIsResource( $server );
     132                fclose( $pipes[0] );
     133
     134                try {
     135                        $ready = false;
     136                        for ( $attempt = 0; $attempt < 100; ++$attempt ) {
     137                                $socket = @fsockopen( '127.0.0.1', $port, $error_number, $error_string, 0.1 );
     138                                if ( is_resource( $socket ) ) {
     139                                        fclose( $socket );
     140                                        $ready = true;
     141                                        break;
     142                                }
     143                                usleep( 50000 );
     144                        }
     145                        $this->assertTrue( $ready, 'The cookie test server did not start.' );
     146
     147                        $response = wp_remote_get( 'http://127.0.0.1:' . $port . '/', array( 'timeout' => 15 ) );
     148                        $this->assertFalse( is_wp_error( $response ), is_wp_error( $response ) ? $response->get_error_message() : '' );
     149                        $this->assertSame( 200, wp_remote_retrieve_response_code( $response ) );
     150
     151                        $cookies = array();
     152                        foreach ( wp_remote_retrieve_cookies( $response ) as $cookie ) {
     153                                $cookies[ $cookie->name ] = $cookie->value;
     154                        }
     155                        $this->assertSame( 'Guest User', $cookies[ 'comment_author_' . COOKIEHASH ] );
     156                        $this->assertSame( 'guest@example.org', $cookies[ 'comment_author_email_' . COOKIEHASH ] );
     157                        $this->assertSame( 'https://example.org/', $cookies[ 'comment_author_url_' . COOKIEHASH ] );
     158                } finally {
     159                        proc_terminate( $server );
     160                        fclose( $pipes[1] );
     161                        fclose( $pipes[2] );
     162                        proc_close( $server );
     163                }
    118164        }
    119165
Note: See TracChangeset for help on using the changeset viewer.