Introduce get_main_network_id()

Expand on the logic previously available as part of `is_main_network()` and provide a way to obtain the ID of the main network. Most useful in multi-network configurations.

Props @johnjamesjacoby for the initial patch.
Fixes #30294.


git-svn-id: https://develop.svn.wordpress.org/trunk@32775 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jeremy Felt
2015-06-14 21:44:45 +00:00
parent 97e6967d63
commit 7819ac5d16
2 changed files with 101 additions and 17 deletions

View File

@@ -21,11 +21,70 @@ class Tests_Multisite_Network extends WP_UnitTestCase {
}
function tearDown() {
global $wpdb;
global $wpdb, $current_site;
$wpdb->suppress_errors( $this->suppress );
$current_site->id = 1;
parent::tearDown();
}
/**
* By default, only one network exists and has a network ID of 1.
*/
function test_get_main_network_id_default() {
$this->assertEquals( 1, get_main_network_id() );
}
/**
* If a second network is created, network ID 1 should still be returned
* as the main network ID.
*/
function test_get_main_network_id_two_networks() {
$this->factory->network->create();
$this->assertEquals( 1, get_main_network_id() );
}
/**
* When the `$current_site` global is populated with another network, the
* main network should still return as 1.
*/
function test_get_main_network_id_after_network_switch() {
global $current_site;
$id = $this->factory->network->create();
$current_site->id = (int) $id;
$this->assertEquals( 1, get_main_network_id() );
}
/**
* When the first network is removed, the next should return as the main
* network ID.
*
* @todo In the future, we'll have a smarter way of deleting a network. For now,
* fake the process with UPDATE queries.
*/
function test_get_main_network_id_after_network_delete() {
global $wpdb, $current_site;
$id = $this->factory->network->create();
$current_site->id = (int) $id;
$wpdb->query( "UPDATE {$wpdb->site} SET id=100 WHERE id=1" );
$this->assertEquals( $id, get_main_network_id() );
$wpdb->query( "UPDATE {$wpdb->site} SET id=1 WHERE id=100" );
}
function test_get_main_network_id_filtered() {
add_filter( 'get_main_network_id', array( $this, '_get_main_network_id' ) );
$this->assertEquals( 3, get_main_network_id() );
remove_filter( 'get_main_network_id', array( $this, '_get_main_network_id' ) );
}
function _get_main_network_id() {
return 3;
}
/**
* @ticket 22917
*/