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