Skip to:
Content

BuddyPress.org

Changeset 7058


Ignore:
Timestamp:
05/13/2013 02:25:04 PM (12 years ago)
Author:
boonebgorges
Message:

Improvements to BP_UnitTestCase::go_to() for multisite

When using go_to() to ape a pageload on a secondary site in multisite, a
number of global variables (such as $current_blog and $current_site, which
are normall set in ms-settings.php) must be reset for that secondary blog.
Having the globals set correctly is important for certain kinds of MS-specific
unit tests, such as test_avatars_on_non_root_blog().

As a convenience, this changeset introduces BP_UnitTestCase::go_to_root(),
which allows for the easy resetting of the relevant globals at the end of a
testcase where go_to() has been used on a secondary blog.

See #4948

Props r-a-y

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/includes/testcase.php

    r7043 r7058  
    4646
    4747    function go_to( $url ) {
    48         // Set this for bp_core_set_uri_globals()
    49         $GLOBALS['_SERVER']['REQUEST_URI'] = $url = str_replace( untrailingslashit( network_home_url() ), '', $url );
    50 
    5148        // note: the WP and WP_Query classes like to silently fetch parameters
    5249        // from all over the place (globals, GET, etc), which makes it tricky
     
    5855        $parts = parse_url($url);
    5956        if (isset($parts['scheme'])) {
     57            // set the HTTP_HOST
     58            $GLOBALS['_SERVER']['HTTP_HOST'] = $parts['host'];
     59
    6060            $req = $parts['path'];
    6161            if (isset($parts['query'])) {
     
    7878        }
    7979
    80         $_SERVER['REQUEST_URI'] = $req;
     80        // Set this for bp_core_set_uri_globals()
     81        $GLOBALS['_SERVER']['REQUEST_URI'] = $req;
    8182        unset($_SERVER['PATH_INFO']);
    8283
    83         $this->flush_cache();
     84        // setup $current_site and $current_blog globals for multisite based on
     85        // REQUEST_URI; mostly copied from /wp-includes/ms-settings.php
     86        if ( is_multisite() ) {
     87            $domain = addslashes( $_SERVER['HTTP_HOST'] );
     88            if ( false !== strpos( $domain, ':' ) ) {
     89                if ( substr( $domain, -3 ) == ':80' ) {
     90                    $domain = substr( $domain, 0, -3 );
     91                    $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
     92                } elseif ( substr( $domain, -4 ) == ':443' ) {
     93                    $domain = substr( $domain, 0, -4 );
     94                    $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
     95                }
     96            }
     97
     98            $domain = rtrim( $domain, '.' );
     99            $cookie_domain = $domain;
     100            if ( substr( $cookie_domain, 0, 4 ) == 'www.' )
     101                $cookie_domain = substr( $cookie_domain, 4 );
     102
     103            $path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $GLOBALS['_SERVER']['REQUEST_URI'] );
     104            $path = str_replace ( '/wp-admin/', '/', $path );
     105            $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
     106
     107            $GLOBALS['current_site'] = wpmu_current_site();
     108            if ( ! isset( $GLOBALS['current_site']->blog_id ) )
     109                $GLOBALS['current_site']->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $GLOBALS['current_site']->domain, $GLOBALS['current_site']->path ) );
     110
     111            // unit tests only support subdirectory install at the moment
     112            // removed object cache references
     113            if ( ! is_subdomain_install() ) {
     114                $blogname = htmlspecialchars( substr( $GLOBALS['_SERVER']['REQUEST_URI'], strlen( $path ) ) );
     115                if ( false !== strpos( $blogname, '/' ) )
     116                    $blogname = substr( $blogname, 0, strpos( $blogname, '/' ) );
     117                if ( false !== strpos( $blogname, '?' ) )
     118                    $blogname = substr( $blogname, 0, strpos( $blogname, '?' ) );
     119                $reserved_blognames = array( 'page', 'comments', 'blog', 'wp-admin', 'wp-includes', 'wp-content', 'files', 'feed' );
     120                if ( $blogname != '' && ! in_array( $blogname, $reserved_blognames ) && ! is_file( $blogname ) )
     121                    $path .= $blogname . '/';
     122
     123                $GLOBALS['current_blog'] = get_blog_details( array( 'domain' => $domain, 'path' => $path ), false );
     124
     125                unset($reserved_blognames);
     126            }
     127
     128            $GLOBALS['blog_id'] = $GLOBALS['current_blog']->blog_id;
     129        }
     130
    84131        unset($GLOBALS['wp_query'], $GLOBALS['wp_the_query']);
    85132        $GLOBALS['wp_the_query'] =& new WP_Query();
     
    197244    }
    198245
    199 
     246    /**
     247     * Go to the root blog. This helps reset globals after moving between
     248     * blogs.
     249     */
     250    public function go_to_root() {
     251        $blog_1_url = get_blog_option( 1, 'home' );
     252        $this->go_to( str_replace( $blog_1_url, '', trailingslashit( bp_get_root_domain() ) ) );
     253    }
    200254}
Note: See TracChangeset for help on using the changeset viewer.