| 1 | <?php |
| 2 | |
| 3 | class Tests_Multisite_Get_Main_Site_ID extends WP_UnitTestCase { |
| 4 | protected static $network_ids; |
| 5 | protected static $site_ids; |
| 6 | |
| 7 | public static function wpSetUpBeforeClass( $factory ) { |
| 8 | self::$network_ids = array( |
| 9 | 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), |
| 10 | 'wp.org/' => array( 'domain' => 'wp.org', 'path' => '/' ), // A network with no sites. |
| 11 | ); |
| 12 | |
| 13 | foreach ( self::$network_ids as &$id ) { |
| 14 | $id = $factory->network->create( $id ); |
| 15 | } |
| 16 | unset( $id ); |
| 17 | |
| 18 | self::$site_ids = array( |
| 19 | 'www.w.org/' => array( 'domain' => 'www.w.org', 'path' => '/' ), |
| 20 | 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', 'site_id' => self::$network_ids['wordpress.org/'] ), |
| 21 | 'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['wordpress.org/'] ), |
| 22 | ); |
| 23 | |
| 24 | foreach ( self::$site_ids as &$id ) { |
| 25 | $id = $factory->blog->create( $id ); |
| 26 | } |
| 27 | unset( $id ); |
| 28 | } |
| 29 | |
| 30 | public static function wpTearDownAfterClass() { |
| 31 | foreach( self::$site_ids as $id ) { |
| 32 | wpmu_delete_blog( $id, true ); |
| 33 | } |
| 34 | |
| 35 | global $wpdb; |
| 36 | |
| 37 | foreach( self::$network_ids as $id ) { |
| 38 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) ); |
| 39 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) ); |
| 40 | } |
| 41 | |
| 42 | wp_update_network_site_counts(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @ticket 29684 |
| 47 | */ |
| 48 | public function test_get_main_site_id_on_main_site_returns_self() { |
| 49 | $this->assertSame( get_current_blog_id(), get_main_site_id() ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @ticket 29684 |
| 54 | */ |
| 55 | public function test_get_main_site_id_returns_main_site_in_switched_context() { |
| 56 | $main_site_id = get_current_blog_id(); |
| 57 | $other_site_id = self::$site_ids['www.w.org/']; |
| 58 | |
| 59 | switch_to_blog( $other_site_id ); |
| 60 | $result = get_main_site_id(); |
| 61 | restore_current_blog(); |
| 62 | |
| 63 | $this->assertSame( $main_site_id, $result ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @ticket 29684 |
| 68 | */ |
| 69 | public function test_get_main_site_id_with_different_network_returns_correct_id() { |
| 70 | $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @ticket 29684 |
| 75 | */ |
| 76 | public function test_get_main_site_id_on_network_without_site_returns_0() { |
| 77 | $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @ticket 29684 |
| 82 | */ |
| 83 | public function test_get_main_site_id_on_invalid_network_returns_0() { |
| 84 | $this->assertSame( 0, get_main_site_id( 333 ) ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @ticket 29684 |
| 89 | */ |
| 90 | public function test_get_main_site_id_filtered() { |
| 91 | add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); |
| 92 | $result = get_main_site_id(); |
| 93 | |
| 94 | $this->assertSame( 333, $result ); |
| 95 | } |
| 96 | |
| 97 | public function filter_get_main_site_id() { |
| 98 | return 333; |
| 99 | } |
| 100 | } |
| 101 | <?php |
| 102 | |
| 103 | class Tests_Multisite_Get_Main_Site_ID extends WP_UnitTestCase { |
| 104 | protected static $network_ids; |
| 105 | protected static $site_ids; |
| 106 | |
| 107 | public static function wpSetUpBeforeClass( $factory ) { |
| 108 | self::$network_ids = array( |
| 109 | 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), |
| 110 | 'wp.org/' => array( 'domain' => 'wp.org', 'path' => '/' ), // A network with no sites. |
| 111 | ); |
| 112 | |
| 113 | foreach ( self::$network_ids as &$id ) { |
| 114 | $id = $factory->network->create( $id ); |
| 115 | } |
| 116 | unset( $id ); |
| 117 | |
| 118 | self::$site_ids = array( |
| 119 | 'www.w.org/' => array( 'domain' => 'www.w.org', 'path' => '/' ), |
| 120 | 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', 'site_id' => self::$network_ids['wordpress.org/'] ), |
| 121 | 'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['wordpress.org/'] ), |
| 122 | ); |
| 123 | |
| 124 | foreach ( self::$site_ids as &$id ) { |
| 125 | $id = $factory->blog->create( $id ); |
| 126 | } |
| 127 | unset( $id ); |
| 128 | } |
| 129 | |
| 130 | public static function wpTearDownAfterClass() { |
| 131 | foreach( self::$site_ids as $id ) { |
| 132 | wpmu_delete_blog( $id, true ); |
| 133 | } |
| 134 | |
| 135 | global $wpdb; |
| 136 | |
| 137 | foreach( self::$network_ids as $id ) { |
| 138 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) ); |
| 139 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) ); |
| 140 | } |
| 141 | |
| 142 | wp_update_network_site_counts(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @ticket 29684 |
| 147 | */ |
| 148 | public function test_get_main_site_id_on_main_site_returns_self() { |
| 149 | $this->assertSame( get_current_blog_id(), get_main_site_id() ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @ticket 29684 |
| 154 | */ |
| 155 | public function test_get_main_site_id_returns_main_site_in_switched_context() { |
| 156 | $main_site_id = get_current_blog_id(); |
| 157 | $other_site_id = self::$site_ids['www.w.org/']; |
| 158 | |
| 159 | switch_to_blog( $other_site_id ); |
| 160 | $result = get_main_site_id(); |
| 161 | restore_current_blog(); |
| 162 | |
| 163 | $this->assertSame( $main_site_id, $result ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @ticket 29684 |
| 168 | */ |
| 169 | public function test_get_main_site_id_with_different_network_returns_correct_id() { |
| 170 | $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) ); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @ticket 29684 |
| 175 | */ |
| 176 | public function test_get_main_site_id_on_network_without_site_returns_0() { |
| 177 | $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * @ticket 29684 |
| 182 | */ |
| 183 | public function test_get_main_site_id_on_invalid_network_returns_0() { |
| 184 | $this->assertSame( 0, get_main_site_id( 333 ) ); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @ticket 29684 |
| 189 | */ |
| 190 | public function test_get_main_site_id_filtered() { |
| 191 | add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); |
| 192 | $result = get_main_site_id(); |
| 193 | |
| 194 | $this->assertSame( 333, $result ); |
| 195 | } |
| 196 | |
| 197 | public function filter_get_main_site_id() { |
| 198 | return 333; |
| 199 | } |
| 200 | } |
| 201 | <?php |
| 202 | |
| 203 | class Tests_Multisite_Get_Main_Site_ID extends WP_UnitTestCase { |
| 204 | protected static $network_ids; |
| 205 | protected static $site_ids; |
| 206 | |
| 207 | public static function wpSetUpBeforeClass( $factory ) { |
| 208 | self::$network_ids = array( |
| 209 | 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), |
| 210 | 'wp.org/' => array( 'domain' => 'wp.org', 'path' => '/' ), // A network with no sites. |
| 211 | ); |
| 212 | |
| 213 | foreach ( self::$network_ids as &$id ) { |
| 214 | $id = $factory->network->create( $id ); |
| 215 | } |
| 216 | unset( $id ); |
| 217 | |
| 218 | self::$site_ids = array( |
| 219 | 'www.w.org/' => array( 'domain' => 'www.w.org', 'path' => '/' ), |
| 220 | 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', 'site_id' => self::$network_ids['wordpress.org/'] ), |
| 221 | 'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['wordpress.org/'] ), |
| 222 | ); |
| 223 | |
| 224 | foreach ( self::$site_ids as &$id ) { |
| 225 | $id = $factory->blog->create( $id ); |
| 226 | } |
| 227 | unset( $id ); |
| 228 | } |
| 229 | |
| 230 | public static function wpTearDownAfterClass() { |
| 231 | foreach( self::$site_ids as $id ) { |
| 232 | wpmu_delete_blog( $id, true ); |
| 233 | } |
| 234 | |
| 235 | global $wpdb; |
| 236 | |
| 237 | foreach( self::$network_ids as $id ) { |
| 238 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) ); |
| 239 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) ); |
| 240 | } |
| 241 | |
| 242 | wp_update_network_site_counts(); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * @ticket 29684 |
| 247 | */ |
| 248 | public function test_get_main_site_id_on_main_site_returns_self() { |
| 249 | $this->assertSame( get_current_blog_id(), get_main_site_id() ); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * @ticket 29684 |
| 254 | */ |
| 255 | public function test_get_main_site_id_returns_main_site_in_switched_context() { |
| 256 | $main_site_id = get_current_blog_id(); |
| 257 | $other_site_id = self::$site_ids['www.w.org/']; |
| 258 | |
| 259 | switch_to_blog( $other_site_id ); |
| 260 | $result = get_main_site_id(); |
| 261 | restore_current_blog(); |
| 262 | |
| 263 | $this->assertSame( $main_site_id, $result ); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * @ticket 29684 |
| 268 | */ |
| 269 | public function test_get_main_site_id_with_different_network_returns_correct_id() { |
| 270 | $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) ); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * @ticket 29684 |
| 275 | */ |
| 276 | public function test_get_main_site_id_on_network_without_site_returns_0() { |
| 277 | $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) ); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * @ticket 29684 |
| 282 | */ |
| 283 | public function test_get_main_site_id_on_invalid_network_returns_0() { |
| 284 | $this->assertSame( 0, get_main_site_id( 333 ) ); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * @ticket 29684 |
| 289 | */ |
| 290 | public function test_get_main_site_id_filtered() { |
| 291 | add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); |
| 292 | $result = get_main_site_id(); |
| 293 | |
| 294 | $this->assertSame( 333, $result ); |
| 295 | } |
| 296 | |
| 297 | public function filter_get_main_site_id() { |
| 298 | return 333; |
| 299 | } |
| 300 | } |
| 301 | <?php |
| 302 | |
| 303 | class Tests_Multisite_Get_Main_Site_ID extends WP_UnitTestCase { |
| 304 | protected static $network_ids; |
| 305 | protected static $site_ids; |
| 306 | |
| 307 | public static function wpSetUpBeforeClass( $factory ) { |
| 308 | self::$network_ids = array( |
| 309 | 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), |
| 310 | 'wp.org/' => array( 'domain' => 'wp.org', 'path' => '/' ), // A network with no sites. |
| 311 | ); |
| 312 | |
| 313 | foreach ( self::$network_ids as &$id ) { |
| 314 | $id = $factory->network->create( $id ); |
| 315 | } |
| 316 | unset( $id ); |
| 317 | |
| 318 | self::$site_ids = array( |
| 319 | 'www.w.org/' => array( 'domain' => 'www.w.org', 'path' => '/' ), |
| 320 | 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', 'site_id' => self::$network_ids['wordpress.org/'] ), |
| 321 | 'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['wordpress.org/'] ), |
| 322 | ); |
| 323 | |
| 324 | foreach ( self::$site_ids as &$id ) { |
| 325 | $id = $factory->blog->create( $id ); |
| 326 | } |
| 327 | unset( $id ); |
| 328 | } |
| 329 | |
| 330 | public static function wpTearDownAfterClass() { |
| 331 | foreach( self::$site_ids as $id ) { |
| 332 | wpmu_delete_blog( $id, true ); |
| 333 | } |
| 334 | |
| 335 | global $wpdb; |
| 336 | |
| 337 | foreach( self::$network_ids as $id ) { |
| 338 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) ); |
| 339 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) ); |
| 340 | } |
| 341 | |
| 342 | wp_update_network_site_counts(); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * @ticket 29684 |
| 347 | */ |
| 348 | public function test_get_main_site_id_on_main_site_returns_self() { |
| 349 | $this->assertSame( get_current_blog_id(), get_main_site_id() ); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * @ticket 29684 |
| 354 | */ |
| 355 | public function test_get_main_site_id_returns_main_site_in_switched_context() { |
| 356 | $main_site_id = get_current_blog_id(); |
| 357 | $other_site_id = self::$site_ids['www.w.org/']; |
| 358 | |
| 359 | switch_to_blog( $other_site_id ); |
| 360 | $result = get_main_site_id(); |
| 361 | restore_current_blog(); |
| 362 | |
| 363 | $this->assertSame( $main_site_id, $result ); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * @ticket 29684 |
| 368 | */ |
| 369 | public function test_get_main_site_id_with_different_network_returns_correct_id() { |
| 370 | $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) ); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * @ticket 29684 |
| 375 | */ |
| 376 | public function test_get_main_site_id_on_network_without_site_returns_0() { |
| 377 | $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) ); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * @ticket 29684 |
| 382 | */ |
| 383 | public function test_get_main_site_id_on_invalid_network_returns_0() { |
| 384 | $this->assertSame( 0, get_main_site_id( 333 ) ); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * @ticket 29684 |
| 389 | */ |
| 390 | public function test_get_main_site_id_filtered() { |
| 391 | add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); |
| 392 | $result = get_main_site_id(); |
| 393 | |
| 394 | $this->assertSame( 333, $result ); |
| 395 | } |
| 396 | |
| 397 | public function filter_get_main_site_id() { |
| 398 | return 333; |
| 399 | } |
| 400 | } |