Changeset 60148 for trunk/tests/phpunit/tests/multisite/getMainSiteId.php
- Timestamp:
- 04/09/2025 01:29:39 PM (8 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/multisite/getMainSiteId.php
r54256 r60148 1 1 <?php 2 2 3 if ( is_multisite() ) : 3 /** 4 * Tests for the get_main_site_id() function. 5 * 6 * @group ms-required 7 * @group ms-site 8 * @group multisite 9 */ 10 class Tests_Multisite_GetMainSiteId extends WP_UnitTestCase { 11 12 protected static $network_ids; 13 protected static $site_ids; 14 15 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 16 self::$network_ids = array( 17 'wordpress.org/' => array( 18 'domain' => 'wordpress.org', 19 'path' => '/', 20 ), 21 'wp.org/' => array( 22 'domain' => 'wp.org', 23 'path' => '/', 24 ), // A network with no sites. 25 ); 26 27 foreach ( self::$network_ids as &$id ) { 28 $id = $factory->network->create( $id ); 29 } 30 unset( $id ); 31 32 self::$site_ids = array( 33 'www.w.org/' => array( 34 'domain' => 'www.w.org', 35 'path' => '/', 36 ), 37 'wordpress.org/' => array( 38 'domain' => 'wordpress.org', 39 'path' => '/', 40 'network_id' => self::$network_ids['wordpress.org/'], 41 ), 42 'wordpress.org/foo/' => array( 43 'domain' => 'wordpress.org', 44 'path' => '/foo/', 45 'network_id' => self::$network_ids['wordpress.org/'], 46 ), 47 ); 48 49 foreach ( self::$site_ids as &$id ) { 50 $id = $factory->blog->create( $id ); 51 } 52 unset( $id ); 53 } 54 55 public static function wpTearDownAfterClass() { 56 foreach ( self::$site_ids as $id ) { 57 wp_delete_site( $id ); 58 } 59 60 global $wpdb; 61 62 foreach ( self::$network_ids as $id ) { 63 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) ); 64 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) ); 65 } 66 67 wp_update_network_site_counts(); 68 } 4 69 5 70 /** 6 * Tests for the get_main_site_id() function. 7 * 8 * @group ms-site 9 * @group multisite 71 * @ticket 29684 10 72 */ 11 class Tests_Multisite_GetMainSiteId extends WP_UnitTestCase{12 protected static $network_ids;13 protected static $site_ids;73 public function test_get_main_site_id_on_main_site_returns_self() { 74 $this->assertSame( get_current_blog_id(), get_main_site_id() ); 75 } 14 76 15 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 16 self::$network_ids = array( 17 'wordpress.org/' => array( 18 'domain' => 'wordpress.org', 19 'path' => '/', 20 ), 21 'wp.org/' => array( 22 'domain' => 'wp.org', 23 'path' => '/', 24 ), // A network with no sites. 25 ); 77 /** 78 * @ticket 29684 79 */ 80 public function test_get_main_site_id_returns_main_site_in_switched_context() { 81 $main_site_id = get_current_blog_id(); 82 $other_site_id = self::$site_ids['www.w.org/']; 26 83 27 foreach ( self::$network_ids as &$id ) { 28 $id = $factory->network->create( $id ); 29 } 30 unset( $id ); 84 switch_to_blog( $other_site_id ); 85 $result = get_main_site_id(); 86 restore_current_blog(); 31 87 32 self::$site_ids = array( 33 'www.w.org/' => array( 34 'domain' => 'www.w.org', 35 'path' => '/', 36 ), 37 'wordpress.org/' => array( 38 'domain' => 'wordpress.org', 39 'path' => '/', 40 'network_id' => self::$network_ids['wordpress.org/'], 41 ), 42 'wordpress.org/foo/' => array( 43 'domain' => 'wordpress.org', 44 'path' => '/foo/', 45 'network_id' => self::$network_ids['wordpress.org/'], 46 ), 47 ); 88 $this->assertSame( $main_site_id, $result ); 89 } 48 90 49 foreach ( self::$site_ids as &$id ) { 50 $id = $factory->blog->create( $id ); 51 } 52 unset( $id ); 53 } 91 /** 92 * @ticket 55802 93 */ 94 public function test_get_main_site_id_with_different_network_cache_id() { 95 $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ), 'Main blog id needs to match blog id of wordpress.org/' ); 96 $this->assertSame( self::$site_ids['wordpress.org/'], (int) get_network_option( self::$network_ids['wordpress.org/'], 'main_site' ), 'Network option needs to match blog id of wordpress.org/' ); 54 97 55 public static function wpTearDownAfterClass() { 56 foreach ( self::$site_ids as $id ) { 57 wp_delete_site( $id ); 58 } 98 $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ), 'Main blog id should not be found' ); 99 $this->assertSame( 0, (int) get_network_option( self::$network_ids['wp.org/'], 'main_site' ), 'Network option should not be found' ); 100 } 59 101 60 global $wpdb; 102 /** 103 * @ticket 29684 104 */ 105 public function test_get_main_site_id_with_different_network_returns_correct_id() { 106 $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) ); 107 } 61 108 62 foreach ( self::$network_ids as $id ) { 63 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) ); 64 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) ); 65 } 109 /** 110 * @ticket 29684 111 */ 112 public function test_get_main_site_id_on_network_without_site_returns_0() { 113 $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) ); 114 } 66 115 67 wp_update_network_site_counts(); 68 } 116 /** 117 * @ticket 29684 118 */ 119 public function test_get_main_site_id_on_invalid_network_returns_0() { 120 $this->assertSame( 0, get_main_site_id( 333 ) ); 121 } 69 122 70 /**71 * @ticket 2968472 */73 public function test_get_main_site_id_on_main_site_returns_self() {74 $this->assertSame( get_current_blog_id(), get_main_site_id() );75 }123 /** 124 * @ticket 29684 125 */ 126 public function test_get_main_site_id_filtered() { 127 add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); 128 $result = get_main_site_id(); 76 129 77 /** 78 * @ticket 29684 79 */ 80 public function test_get_main_site_id_returns_main_site_in_switched_context() { 81 $main_site_id = get_current_blog_id(); 82 $other_site_id = self::$site_ids['www.w.org/']; 130 $this->assertSame( 333, $result ); 131 } 83 132 84 switch_to_blog( $other_site_id );85 $result = get_main_site_id();86 restore_current_blog();133 public function filter_get_main_site_id() { 134 return 333; 135 } 87 136 88 $this->assertSame( $main_site_id, $result ); 89 } 137 /** 138 * @ticket 29684 139 */ 140 public function test_get_main_site_id_filtered_depending_on_network() { 141 add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id_depending_on_network' ), 10, 2 ); 142 $result = get_main_site_id( self::$network_ids['wordpress.org/'] ); 90 143 91 /** 92 * @ticket 55802 93 */ 94 public function test_get_main_site_id_with_different_network_cache_id() { 95 $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ), 'Main blog id needs to match blog id of wordpress.org/' ); 96 $this->assertSame( self::$site_ids['wordpress.org/'], (int) get_network_option( self::$network_ids['wordpress.org/'], 'main_site' ), 'Network option needs to match blog id of wordpress.org/' ); 144 $this->assertSame( 333, $result ); 145 } 97 146 98 $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ), 'Main blog id should not be found' ); 99 $this->assertSame( 0, (int) get_network_option( self::$network_ids['wp.org/'], 'main_site' ), 'Network option should not be found' ); 100 } 101 102 /** 103 * @ticket 29684 104 */ 105 public function test_get_main_site_id_with_different_network_returns_correct_id() { 106 $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) ); 107 } 108 109 /** 110 * @ticket 29684 111 */ 112 public function test_get_main_site_id_on_network_without_site_returns_0() { 113 $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) ); 114 } 115 116 /** 117 * @ticket 29684 118 */ 119 public function test_get_main_site_id_on_invalid_network_returns_0() { 120 $this->assertSame( 0, get_main_site_id( 333 ) ); 121 } 122 123 /** 124 * @ticket 29684 125 */ 126 public function test_get_main_site_id_filtered() { 127 add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); 128 $result = get_main_site_id(); 129 130 $this->assertSame( 333, $result ); 131 } 132 133 public function filter_get_main_site_id() { 147 public function filter_get_main_site_id_depending_on_network( $main_site_id, $network ) { 148 // Override main site ID for a specific network for the test. 149 if ( $network->id === (int) self::$network_ids['wordpress.org/'] ) { 134 150 return 333; 135 151 } 136 152 137 /** 138 * @ticket 29684 139 */ 140 public function test_get_main_site_id_filtered_depending_on_network() { 141 add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id_depending_on_network' ), 10, 2 ); 142 $result = get_main_site_id( self::$network_ids['wordpress.org/'] ); 143 144 $this->assertSame( 333, $result ); 145 } 146 147 public function filter_get_main_site_id_depending_on_network( $main_site_id, $network ) { 148 // Override main site ID for a specific network for the test. 149 if ( $network->id === (int) self::$network_ids['wordpress.org/'] ) { 150 return 333; 151 } 152 153 return $main_site_id; 154 } 155 156 /** 157 * @ticket 41936 158 */ 159 public function test_get_main_site_id_with_property_value() { 160 global $current_site; 161 162 $original_main_site_id = $current_site->blog_id; 163 $current_site->blog_id = '123'; 164 165 $result = get_main_site_id(); 166 167 $current_site->blog_id = $original_main_site_id; 168 169 $this->assertSame( 123, $result ); 170 } 171 172 /** 173 * @ticket 41936 174 */ 175 public function test_get_main_site_id_filtered_with_property_value() { 176 global $current_site; 177 178 $original_main_site_id = $current_site->blog_id; 179 $current_site->blog_id = '123'; 180 181 add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); 182 $result = get_main_site_id(); 183 184 $current_site->blog_id = $original_main_site_id; 185 186 $this->assertSame( 333, $result ); 187 } 153 return $main_site_id; 188 154 } 189 155 190 endif; 156 /** 157 * @ticket 41936 158 */ 159 public function test_get_main_site_id_with_property_value() { 160 global $current_site; 161 162 $original_main_site_id = $current_site->blog_id; 163 $current_site->blog_id = '123'; 164 165 $result = get_main_site_id(); 166 167 $current_site->blog_id = $original_main_site_id; 168 169 $this->assertSame( 123, $result ); 170 } 171 172 /** 173 * @ticket 41936 174 */ 175 public function test_get_main_site_id_filtered_with_property_value() { 176 global $current_site; 177 178 $original_main_site_id = $current_site->blog_id; 179 $current_site->blog_id = '123'; 180 181 add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); 182 $result = get_main_site_id(); 183 184 $current_site->blog_id = $original_main_site_id; 185 186 $this->assertSame( 333, $result ); 187 } 188 }
Note: See TracChangeset
for help on using the changeset viewer.