| | 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_same_network() { |
| | 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_returns_main_site_from_different_network_without_network_id_while_switched() { |
| | 70 | switch_to_blog( self::$site_ids['wordpress.org/foo/'] ); |
| | 71 | $result = get_main_site_id(); |
| | 72 | restore_current_blog(); |
| | 73 | |
| | 74 | $this->assertSame( self::$site_ids['wordpress.org/'], $result ); |
| | 75 | } |
| | 76 | |
| | 77 | /** |
| | 78 | * @ticket 29684 |
| | 79 | */ |
| | 80 | public function test_get_main_site_id_returns_main_site_from_different_network_with_network_id_while_switched() { |
| | 81 | switch_to_blog( self::$site_ids['wordpress.org/foo/'] ); |
| | 82 | $result = get_main_site_id( self::$network_ids['wordpress.org/'] ); |
| | 83 | restore_current_blog(); |
| | 84 | |
| | 85 | $this->assertSame( self::$site_ids['wordpress.org/'], $result ); |
| | 86 | } |
| | 87 | |
| | 88 | /** |
| | 89 | * @ticket 29684 |
| | 90 | */ |
| | 91 | public function test_get_main_site_id_with_different_network_returns_correct_id() { |
| | 92 | $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) ); |
| | 93 | } |
| | 94 | |
| | 95 | /** |
| | 96 | * @ticket 29684 |
| | 97 | */ |
| | 98 | public function test_get_main_site_id_on_network_without_site_returns_0() { |
| | 99 | $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) ); |
| | 100 | } |
| | 101 | |
| | 102 | /** |
| | 103 | * @ticket 29684 |
| | 104 | */ |
| | 105 | public function test_get_main_site_id_on_invalid_network_returns_0() { |
| | 106 | $this->assertSame( 0, get_main_site_id( 333 ) ); |
| | 107 | } |
| | 108 | |
| | 109 | /** |
| | 110 | * @ticket 29684 |
| | 111 | */ |
| | 112 | public function test_get_main_site_id_filtered() { |
| | 113 | add_filter( 'get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); |
| | 114 | $result = get_main_site_id(); |
| | 115 | remove_filter( 'get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); |
| | 116 | |
| | 117 | $this->assertSame( 333, $result ); |
| | 118 | } |
| | 119 | |
| | 120 | public function filter_get_main_site_id( $main_site_id ) { |
| | 121 | return 333; |
| | 122 | } |
| | 123 | } |
| | 124 | <?php |
| | 125 | |
| | 126 | class Tests_Multisite_Get_Main_Site_ID extends WP_UnitTestCase { |
| | 127 | protected static $network_ids; |
| | 128 | protected static $site_ids; |
| | 129 | |
| | 130 | public static function wpSetUpBeforeClass( $factory ) { |
| | 131 | self::$network_ids = array( |
| | 132 | 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), |
| | 133 | 'wp.org/' => array( 'domain' => 'wp.org', 'path' => '/' ), // A network with no sites. |
| | 134 | ); |
| | 135 | |
| | 136 | foreach ( self::$network_ids as &$id ) { |
| | 137 | $id = $factory->network->create( $id ); |
| | 138 | } |
| | 139 | unset( $id ); |
| | 140 | |
| | 141 | self::$site_ids = array( |
| | 142 | 'www.w.org/' => array( 'domain' => 'www.w.org', 'path' => '/' ), |
| | 143 | 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', 'site_id' => self::$network_ids['wordpress.org/'] ), |
| | 144 | 'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['wordpress.org/'] ), |
| | 145 | ); |
| | 146 | |
| | 147 | foreach ( self::$site_ids as &$id ) { |
| | 148 | $id = $factory->blog->create( $id ); |
| | 149 | } |
| | 150 | unset( $id ); |
| | 151 | } |
| | 152 | |
| | 153 | public static function wpTearDownAfterClass() { |
| | 154 | foreach( self::$site_ids as $id ) { |
| | 155 | wpmu_delete_blog( $id, true ); |
| | 156 | } |
| | 157 | |
| | 158 | global $wpdb; |
| | 159 | |
| | 160 | foreach( self::$network_ids as $id ) { |
| | 161 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) ); |
| | 162 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) ); |
| | 163 | } |
| | 164 | |
| | 165 | wp_update_network_site_counts(); |
| | 166 | } |
| | 167 | |
| | 168 | /** |
| | 169 | * @ticket 29684 |
| | 170 | */ |
| | 171 | public function test_get_main_site_id_on_main_site_returns_self() { |
| | 172 | $this->assertSame( get_current_blog_id(), get_main_site_id() ); |
| | 173 | } |
| | 174 | |
| | 175 | /** |
| | 176 | * @ticket 29684 |
| | 177 | */ |
| | 178 | public function test_get_main_site_id_returns_main_site_in_switched_context_same_network() { |
| | 179 | $main_site_id = get_current_blog_id(); |
| | 180 | $other_site_id = self::$site_ids['www.w.org/']; |
| | 181 | |
| | 182 | switch_to_blog( $other_site_id ); |
| | 183 | $result = get_main_site_id(); |
| | 184 | restore_current_blog(); |
| | 185 | |
| | 186 | $this->assertSame( $main_site_id, $result ); |
| | 187 | } |
| | 188 | |
| | 189 | /** |
| | 190 | * @ticket 29684 |
| | 191 | */ |
| | 192 | public function test_get_main_site_id_returns_main_site_from_different_network_without_network_id_while_switched() { |
| | 193 | switch_to_blog( self::$site_ids['wordpress.org/foo/'] ); |
| | 194 | $result = get_main_site_id(); |
| | 195 | restore_current_blog(); |
| | 196 | |
| | 197 | $this->assertSame( self::$site_ids['wordpress.org/'], $result ); |
| | 198 | } |
| | 199 | |
| | 200 | /** |
| | 201 | * @ticket 29684 |
| | 202 | */ |
| | 203 | public function test_get_main_site_id_returns_main_site_from_different_network_with_network_id_while_switched() { |
| | 204 | switch_to_blog( self::$site_ids['wordpress.org/foo/'] ); |
| | 205 | $result = get_main_site_id( self::$network_ids['wordpress.org/'] ); |
| | 206 | restore_current_blog(); |
| | 207 | |
| | 208 | $this->assertSame( self::$site_ids['wordpress.org/'], $result ); |
| | 209 | } |
| | 210 | |
| | 211 | /** |
| | 212 | * @ticket 29684 |
| | 213 | */ |
| | 214 | public function test_get_main_site_id_with_different_network_returns_correct_id() { |
| | 215 | $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) ); |
| | 216 | } |
| | 217 | |
| | 218 | /** |
| | 219 | * @ticket 29684 |
| | 220 | */ |
| | 221 | public function test_get_main_site_id_on_network_without_site_returns_0() { |
| | 222 | $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) ); |
| | 223 | } |
| | 224 | |
| | 225 | /** |
| | 226 | * @ticket 29684 |
| | 227 | */ |
| | 228 | public function test_get_main_site_id_on_invalid_network_returns_0() { |
| | 229 | $this->assertSame( 0, get_main_site_id( 333 ) ); |
| | 230 | } |
| | 231 | |
| | 232 | /** |
| | 233 | * @ticket 29684 |
| | 234 | */ |
| | 235 | public function test_get_main_site_id_filtered() { |
| | 236 | add_filter( 'get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); |
| | 237 | $result = get_main_site_id(); |
| | 238 | remove_filter( 'get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); |
| | 239 | |
| | 240 | $this->assertSame( 333, $result ); |
| | 241 | } |
| | 242 | |
| | 243 | public function filter_get_main_site_id( $main_site_id ) { |
| | 244 | return 333; |
| | 245 | } |
| | 246 | } |