wordpress-develop/tests/phpunit/tests/multisite/bootstrap.php
Jeremy Felt 757c0767f7 Multisite: Wrap the main bootstrap process in a function
Introduce `ms_load_current_site_and_network`. This is used by core during the multisite bootstrap process to populate the `$current_site` and `$current_blog` globals based on a requested domain and path.

Return values from this function inform `ms-settings.php` as to whether a page view should continue, `ms_not_installed()` should fire, or a redirect to a new location should occur.

This was previously a procedural block in `ms-settings.php`. Wrapping this code and providing specific return values allows us to write tests that do not rely on the manual and repeated inclusion of `ms-settings.php`.

This should not be used by plugins or themes. Please.

See #34941.


git-svn-id: https://develop.svn.wordpress.org/trunk@37475 602fd350-edb4-49c9-b593-d223f7449a82
2016-05-20 20:56:54 +00:00

217 lines
10 KiB
PHP

<?php
if ( is_multisite() ) :
/**
* Tests specific to the bootstrap process of Multisite.
*
* @group ms-bootstrap
* @group multisite
*/
class Tests_Multisite_Bootstrap extends WP_UnitTestCase {
protected static $network_ids;
protected static $site_ids;
public static function wpSetUpBeforeClass( $factory ) {
self::$network_ids = array(
'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ),
'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/' ),
'wordpress.org/one/' => array( 'domain' => 'wordpress.org', 'path' => '/one/' ),
'wordpress.net/' => array( 'domain' => 'wordpress.net', 'path' => '/' ),
'www.wordpress.net/' => array( 'domain' => 'www.wordpress.net', 'path' => '/' ),
'www.wordpress.net/two/' => array( 'domain' => 'www.wordpress.net', 'path' => '/two/' ),
'wordpress.net/three/' => array( 'domain' => 'wordpress.net', 'path' => '/three/' ),
);
foreach ( self::$network_ids as &$id ) {
$id = $factory->network->create( $id );
}
unset( $id );
self::$site_ids = array(
'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', 'site_id' => self::$network_ids['wordpress.org/'] ),
'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['wordpress.org/'] ),
'wordpress.org/foo/bar/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/bar/', 'site_id' => self::$network_ids['wordpress.org/'] ),
'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/', 'site_id' => self::$network_ids['make.wordpress.org/'] ),
'make.wordpress.org/foo/' => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['make.wordpress.org/'] ),
'www.w.org/' => array( 'domain' => 'www.w.org', 'path' => '/' ),
'www.w.org/foo/' => array( 'domain' => 'www.w.org', 'path' => '/foo/' ),
'www.w.org/foo/bar/' => array( 'domain' => 'www.w.org', 'path' => '/foo/bar/' ),
);
foreach ( self::$site_ids as &$id ) {
$id = $factory->blog->create( $id );
}
unset( $id );
}
public static function wpTearDownAfterClass() {
global $wpdb;
foreach( self::$site_ids as $id ) {
wpmu_delete_blog( $id, true );
}
foreach( self::$network_ids as $id ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
}
wp_update_network_site_counts();
}
/**
* @ticket 27003
* @dataProvider data_get_network_by_path
*
* @param string $expected_key The array key associated with expected data for the test.
* @param string $domain The requested domain.
* @param string $path The requested path.
* @param string $message The message to pass for failed tests.
*/
function test_get_network_by_path( $expected_key, $domain, $path, $message ) {
$network = get_network_by_path( $domain, $path );
$this->assertEquals( self::$network_ids[ $expected_key ], $network->id, $message );
}
public function data_get_network_by_path() {
return array(
array( 'wordpress.org/', 'wordpress.org', '/', 'A standard domain and path request should work.' ),
array( 'wordpress.net/', 'wordpress.net', '/notapath/', 'A missing path on a top level domain should find the correct network.' ),
array( 'www.wordpress.net/', 'www.wordpress.net', '/notapath/', 'A missing path should find the correct network.' ),
array( 'wordpress.org/one/', 'www.wordpress.org', '/one/', 'Should find the path despite the www.' ),
array( 'wordpress.org/', 'site1.wordpress.org', '/one/', 'Should not find path because domains do not match.' ),
array( 'wordpress.net/three/', 'wordpress.net', '/three/', 'A network can have a path.' ),
array( 'www.wordpress.net/two/', 'www.wordpress.net', '/two/', 'A www network with a path can coexist with a non-www network.' ),
array( 'wordpress.net/', 'site1.wordpress.net', '/notapath/', 'An invalid subdomain should find the top level network domain.' ),
array( 'wordpress.net/', 'site1.wordpress.net', '/three/', 'An invalid subdomain and path should find the top level network domain.' ),
);
}
/**
* @ticket 27003
* @ticket 27927
* @dataProvider data_get_site_by_path
*
* @param string $expected_key The array key associated with expected data for the test.
* @param string $domain The requested domain.
* @param string $path The requested path.
* @param int $segments Optional. Number of segments to use in `get_site_by_path()`.
*/
public function test_get_site_by_path( $expected_key, $domain, $path, $segments = null ) {
$site = get_site_by_path( $domain, $path, $segments );
if ( $expected_key ) {
$this->assertEquals( self::$site_ids[ $expected_key ], $site->blog_id );
} else {
$this->assertFalse( $site );
}
}
public function data_get_site_by_path() {
return array(
array( 'wordpress.org/', 'wordpress.org', '/notapath/' ),
array( 'wordpress.org/', 'www.wordpress.org', '/notapath/' ),
array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/' ),
array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/' ),
array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/', 3 ),
array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/', 3 ),
array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/', 2 ),
array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/', 2 ),
array( 'wordpress.org/foo/', 'wordpress.org', '/foo/bar/baz/', 1 ),
array( 'wordpress.org/foo/', 'www.wordpress.org', '/foo/bar/baz/', 1 ),
array( 'wordpress.org/', 'wordpress.org', '/', 0 ),
array( 'wordpress.org/', 'www.wordpress.org', '/', 0 ),
array( 'make.wordpress.org/foo/', 'make.wordpress.org', '/foo/bar/baz/quz/', 4 ),
array( 'make.wordpress.org/foo/', 'www.make.wordpress.org', '/foo/bar/baz/quz/', 4 ),
array( 'www.w.org/', 'www.w.org', '/', 0 ),
array( 'www.w.org/', 'www.w.org', '/notapath' ),
array( 'www.w.org/foo/bar/', 'www.w.org', '/foo/bar/baz/' ),
array( 'www.w.org/foo/', 'www.w.org', '/foo/bar/baz/', 1 ),
// A site installed with www will not be found by the root domain.
array( false, 'w.org', '/' ),
array( false, 'w.org', '/notapath/' ),
array( false, 'w.org', '/foo/bar/baz/' ),
array( false, 'w.org', '/foo/bar/baz/', 1 ),
// A site will not be found by its root domain when an invalid subdomain is requested.
array( false, 'invalid.wordpress.org', '/' ),
array( false, 'invalid.wordpress.org', '/foo/bar/' ),
);
}
/**
* @ticket 27884
* @dataProvider data_multisite_bootstrap
*
* @param string $site_key The array key associated with the expected site for the test.
* @param string $network_key The array key associated with the expected network for the test.
* @param string $domain The requested domain.
* @param string $path The requested path.
*/
function test_multisite_bootstrap( $site_key, $network_key, $domain, $path ) {
global $current_blog;
$expected = array(
'network_id' => self::$network_ids[ $network_key ],
'site_id' => self::$site_ids[ $site_key ],
);
ms_load_current_site_and_network( $domain, $path );
$actual = array(
'network_id' => $current_blog->site_id,
'site_id' => $current_blog->blog_id,
);
ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' );
$this->assertEqualSetsWithIndex( $expected, $actual );
}
public function data_multisite_bootstrap() {
return array(
array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/' ),
array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/2014/04/23/hello-world/' ),
array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/sample-page/' ),
array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/?p=1' ),
array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/wp-admin/' ),
array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/' ),
array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/FOO/' ),
array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/2014/04/23/hello-world/' ),
array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/sample-page/' ),
array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/?p=1' ),
array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/wp-admin/' ),
array( 'make.wordpress.org/', 'make.wordpress.org/', 'make.wordpress.org', '/' ),
array( 'make.wordpress.org/foo/', 'make.wordpress.org/', 'make.wordpress.org', '/foo/' ),
);
}
/**
* @ticket 27884
*/
public function test_multisite_bootstrap_additional_path_segments() {
global $current_blog;
$expected = array(
'network_id' => self::$network_ids['wordpress.org/'],
'site_id' => self::$site_ids['wordpress.org/foo/bar/'],
);
add_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) );
ms_load_current_site_and_network( 'wordpress.org', '/foo/bar/' );
$actual = array(
'network_id' => $current_blog->site_id,
'site_id' => $current_blog->blog_id,
);
remove_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) );
ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' );
$this->assertEqualSetsWithIndex( $expected, $actual );
}
public function filter_path_segments_to_two() {
return 2;
}
}
endif;