diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index ee3f58fa36..4f097b4611 100644 --- a/tests/phpunit/tests/multisite/site.php +++ b/tests/phpunit/tests/multisite/site.php @@ -671,66 +671,94 @@ class Tests_Multisite_Site extends WP_UnitTestCase { } /** + * Test the default arguments for wp_get_sites, which should return only + * public sites from the current network. + * * @ticket 14511 */ - function test_wp_get_sites() { - $this->factory->blog->create_many( 2, array( 'site_id' => 2, 'meta' => array( 'public' => 1 ) ) ); - $this->factory->blog->create_many( 3, array( 'site_id' => 3, 'meta' => array( 'public' => 0 ) ) ); + function test_wp_get_sites_with_default_arguments() { + $this->factory->blog->create( array( 'site_id' => 2 ) ); - // Expect no sites when passed an invalid network_id - $this->assertCount( 0, wp_get_sites( array( 'network_id' => 0 ) ) ); - $this->assertCount( 0, wp_get_sites( array( 'network_id' => 4 ) ) ); - - // Expect 1 site when no network_id is specified - defaults to current network. $this->assertCount( 1, wp_get_sites() ); - // Expect 6 sites when network_id = null. - $this->assertCount( 6, wp_get_sites( array( 'network_id' => null ) ) ); - - // Expect 1 site with a network_id of 1, 2 for network_id 2, 3 for 3 - $this->assertCount( 1, wp_get_sites( array( 'network_id' => 1 ) ) ); - $this->assertCount( 2, wp_get_sites( array( 'network_id' => 2 ) ) ); - $this->assertCount( 3, wp_get_sites( array( 'network_id' => 3 ) ) ); - - // Expect 6 sites when public is null (across all networks) - $this->assertCount( 6, wp_get_sites( array( 'public' => null, 'network_id' => null ) ) ); - - // Expect 3 sites when public is 1 - $this->assertCount( 3, wp_get_sites( array( 'public' => 1, 'network_id' => null ) ) ); - - // Expect 2 sites when public is 1 and network_id is 2 - $this->assertCount( 2, wp_get_sites( array( 'network_id' => 2, 'public' => 1 ) ) ); - - // Expect no sites when public is set to 0 and network_id is not 3 - $this->assertCount( 0, wp_get_sites( array( 'network_id' => 1, 'public' => 0 ) ) ); - - // Test public + network_id = 3 - $this->assertCount( 0, wp_get_sites( array( 'network_id' => 3, 'public' => 1 ) ) ); - $this->assertCount( 3, wp_get_sites( array( 'network_id' => 3, 'public' => 0 ) ) ); } /** - * @ticket 14511 + * No sites should match a query that specifies an invalid network ID. + */ + function test_wp_get_sites_with_invalid_network_id() { + $this->assertcount( 0, wp_get_sites( array( 'network_id' => 999 ) ) ); + } + + /** + * A network ID of null should query for all public sites on all networks. + */ + function test_wp_get_sites_with_network_id_null() { + $this->factory->blog->create( array( 'site_id' => 2 ) ); + + $this->assertCount( 2, wp_get_sites( array( 'network_id' => null ) ) ); + } + + /** + * Expect only sites on the specified network ID to be returned. + */ + function test_wp_get_sites_with_specific_network_id() { + $this->factory->blog->create( array( 'site_id' => 2 ) ); + + $this->assertCount( 1, wp_get_sites( array( 'network_id' => 2 ) ) ); + } + + /** + * Expect sites from both networks if both network IDs are specified. + */ + function test_wp_get_sites_with_multiple_network_ids() { + $this->factory->blog->create( array( 'site_id' => 2 ) ); + + $this->assertCount( 2, wp_get_sites( array( 'network_id' => array( 1, 2 ) ) ) ); + } + + /** + * Queries for public or non public sites should work across all networks if network ID is null. + */ + function test_wp_get_sites_with_public_meta_on_all_networks() { + $this->factory->blog->create( array( 'site_id' => 2, 'meta' => array( 'public' => 0 ) ) ); + + $this->assertCount( 1, wp_get_sites( array( 'public' => 1, 'network_id' => null ) ) ); + $this->assertcount( 1, wp_get_sites( array( 'public' => 0, 'network_id' => null ) ) ); + } + + /** + * If a network ID is specified, queries for public sites should be restricted to that network. + */ + function test_wp_get_sites_with_public_meta_restrict_to_one_network() { + $this->factory->blog->create( array( 'site_id' => 1, 'meta' => array( 'public' => 0 ) ) ); + + $this->assertCount( 1, wp_get_sites( array( 'public' => 1, 'network_id' => 1 ) ) ); + $this->assertCount( 0, wp_get_sites( array( 'public' => 1, 'network_id' => 2 ) ) ); + } + + /** + * Test the limit and offset arguments for wp_get_sites when multiple sites are available. */ function test_wp_get_sites_limit_offset() { - // Create 4 more sites (in addition to the default one) - $this->factory->blog->create_many( 4, array( 'meta' => array( 'public' => 1 ) ) ); - - // Expect all 5 sites when no limit/offset is specified - $this->assertCount( 5, wp_get_sites() ); + // Create 2 more sites (in addition to the default one) + $this->factory->blog->create_many( 2 ); // Expect first 2 sites when using limit $this->assertCount( 2, wp_get_sites( array( 'limit' => 2 ) ) ); - // Expect only the last 3 sites when using offset of 2 (limit will default to 100) - $this->assertCount( 3, wp_get_sites( array( 'offset' => 2 ) ) ); + // Expect only the last 2 sites when using offset of 1 (limit will default to 100) + $this->assertCount( 2, wp_get_sites( array( 'offset' => 1 ) ) ); - // Expect only the last 1 site when using offset of 4 and limit of 2 - $this->assertCount( 1, wp_get_sites( array( 'limit' => 2, 'offset' => 4 ) ) ); - - // Expect 0 sites when using an offset larger than the number of sites - $this->assertCount( 0, wp_get_sites( array( 'offset' => 20 ) ) ); + // Expect only the last 1 site when using offset of 2 and limit of 2 + $this->assertCount( 1, wp_get_sites( array( 'limit' => 2, 'offset' => 2 ) ) ); } + /** + * Expect 0 sites when using an offset larger than the total number of sites. + */ + function test_wp_get_sites_offset_greater_than_available_sites() { + $this->assertCount( 0, wp_get_sites( array( 'offset' => 20 ) ) ); + } /** * @ticket 27952