| | 1 | <?php |
| | 2 | |
| | 3 | if ( is_multisite() ) : |
| | 4 | /** |
| | 5 | * Test get_site_by() in multisite. |
| | 6 | * |
| | 7 | * @ticket 40180 |
| | 8 | * @group ms-site |
| | 9 | * @group multisite |
| | 10 | */ |
| | 11 | class Tests_Multisite_Get_Site_By extends WP_UnitTestCase { |
| | 12 | protected static $network_ids; |
| | 13 | protected static $site_ids; |
| | 14 | |
| | 15 | public static function wpSetUpBeforeClass( $factory ) { |
| | 16 | self::$network_ids = array( |
| | 17 | 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), |
| | 18 | 'www.wordpress.net/' => array( 'domain' => 'www.wordpress.net', 'path' => '/' ), |
| | 19 | ); |
| | 20 | |
| | 21 | foreach ( self::$network_ids as &$id ) { |
| | 22 | $id = $factory->network->create( $id ); |
| | 23 | } |
| | 24 | unset( $id ); |
| | 25 | |
| | 26 | self::$site_ids = array( |
| | 27 | 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', 'site_id' => self::$network_ids['wordpress.org/'] ), |
| | 28 | 'foo.wordpress.org/' => array( 'domain' => 'foo.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 | 'www.wordpress.net/' => array( 'domain' => 'www.wordpress.net', 'path' => '/', 'site_id' => self::$network_ids['www.wordpress.net/'] ), |
| | 31 | 'foo.wordpress.net/' => array( 'domain' => 'foo.wordpress.net', 'path' => '/', 'site_id' => self::$network_ids['www.wordpress.net/'] ), |
| | 32 | 'www.wordpress.net/foo/' => array( 'domain' => 'www.wordpress.net', 'path' => '/foo/', 'site_id' => self::$network_ids['www.wordpress.net/'] ), |
| | 33 | ); |
| | 34 | |
| | 35 | foreach ( self::$site_ids as &$id ) { |
| | 36 | $id = $factory->blog->create( $id ); |
| | 37 | } |
| | 38 | unset( $id ); |
| | 39 | } |
| | 40 | |
| | 41 | public static function wpTearDownAfterClass() { |
| | 42 | global $wpdb; |
| | 43 | |
| | 44 | foreach( self::$site_ids as $id ) { |
| | 45 | wpmu_delete_blog( $id, true ); |
| | 46 | } |
| | 47 | |
| | 48 | foreach( self::$network_ids as $id ) { |
| | 49 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) ); |
| | 50 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) ); |
| | 51 | } |
| | 52 | |
| | 53 | wp_update_network_site_counts(); |
| | 54 | } |
| | 55 | |
| | 56 | public function test_get_site_by_id() { |
| | 57 | $result = get_site_by( 'id', self::$site_ids['wordpress.org/'] ); |
| | 58 | $this->assertEquals( self::$site_ids['wordpress.org/'], $result->id ); |
| | 59 | } |
| | 60 | |
| | 61 | public function test_get_site_by_id_in_array() { |
| | 62 | $result = get_site_by( array( 'id' => self::$site_ids['wordpress.org/'] ) ); |
| | 63 | $this->assertEquals( self::$site_ids['wordpress.org/'], $result->id ); |
| | 64 | } |
| | 65 | |
| | 66 | public function test_get_site_by_current_id() { |
| | 67 | $result = get_site_by( 'id', null ); |
| | 68 | $this->assertEquals( get_current_blog_id(), $result->id ); |
| | 69 | } |
| | 70 | |
| | 71 | public function test_get_site_by_name_subdomain() { |
| | 72 | global $current_site; |
| | 73 | |
| | 74 | if ( ! is_subdomain_install() ) { |
| | 75 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| | 76 | } |
| | 77 | |
| | 78 | $original_network = $current_site; |
| | 79 | $current_site = get_network( self::$network_ids['wordpress.org/'] ); |
| | 80 | |
| | 81 | $result = get_site_by( 'slug', 'foo' ); |
| | 82 | $current_site = $original_network; |
| | 83 | |
| | 84 | $this->assertEquals( self::$site_ids['foo.wordpress.org/'], $result->id ); |
| | 85 | } |
| | 86 | |
| | 87 | public function test_get_site_by_name_with_www_subdomain() { |
| | 88 | global $current_site; |
| | 89 | |
| | 90 | if ( ! is_subdomain_install() ) { |
| | 91 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| | 92 | } |
| | 93 | |
| | 94 | $original_network = $current_site; |
| | 95 | $current_site = get_network( self::$network_ids['www.wordpress.net/'] ); |
| | 96 | |
| | 97 | $result = get_site_by( 'slug', 'foo' ); |
| | 98 | $current_site = $original_network; |
| | 99 | |
| | 100 | $this->assertEquals( self::$site_ids['foo.wordpress.net/'], $result->id ); |
| | 101 | } |
| | 102 | |
| | 103 | public function test_get_site_by_name_and_network_subdomain() { |
| | 104 | if ( ! is_subdomain_install() ) { |
| | 105 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| | 106 | } |
| | 107 | |
| | 108 | $result = get_site_by( array( |
| | 109 | 'slug' => 'foo', |
| | 110 | 'network_id' => self::$network_ids['wordpress.org/'], |
| | 111 | ) ); |
| | 112 | |
| | 113 | $this->assertEquals( self::$site_ids['foo.wordpress.org/'], $result->id ); |
| | 114 | } |
| | 115 | |
| | 116 | public function test_get_site_by_name_and_network_with_www_subdomain() { |
| | 117 | if ( ! is_subdomain_install() ) { |
| | 118 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| | 119 | } |
| | 120 | |
| | 121 | $result = get_site_by( array( |
| | 122 | 'slug' => 'foo', |
| | 123 | 'network_id' => self::$network_ids['www.wordpress.net/'], |
| | 124 | ) ); |
| | 125 | |
| | 126 | $this->assertEquals( self::$site_ids['foo.wordpress.net/'], $result->id ); |
| | 127 | } |
| | 128 | |
| | 129 | public function test_get_site_by_name_subdirectory() { |
| | 130 | global $current_site; |
| | 131 | |
| | 132 | if ( is_subdomain_install() ) { |
| | 133 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| | 134 | } |
| | 135 | |
| | 136 | $original_network = $current_site; |
| | 137 | $current_site = get_network( self::$network_ids['wordpress.org/'] ); |
| | 138 | |
| | 139 | $result = get_site_by( 'slug', 'foo' ); |
| | 140 | $current_site = $original_network; |
| | 141 | |
| | 142 | $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $result->id ); |
| | 143 | } |
| | 144 | |
| | 145 | public function test_get_site_by_name_with_www_subdirectory() { |
| | 146 | global $current_site; |
| | 147 | |
| | 148 | if ( is_subdomain_install() ) { |
| | 149 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| | 150 | } |
| | 151 | |
| | 152 | $original_network = $current_site; |
| | 153 | $current_site = get_network( self::$network_ids['www.wordpress.net/'] ); |
| | 154 | |
| | 155 | $result = get_site_by( 'slug', 'foo' ); |
| | 156 | $current_site = $original_network; |
| | 157 | |
| | 158 | $this->assertEquals( self::$site_ids['www.wordpress.net/foo/'], $result->id ); |
| | 159 | } |
| | 160 | |
| | 161 | public function test_get_site_by_name_and_network_subdirectory() { |
| | 162 | if ( is_subdomain_install() ) { |
| | 163 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| | 164 | } |
| | 165 | |
| | 166 | $result = get_site_by( array( |
| | 167 | 'slug' => 'foo', |
| | 168 | 'network_id' => self::$network_ids['wordpress.org/'], |
| | 169 | ) ); |
| | 170 | |
| | 171 | $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $result->id ); |
| | 172 | } |
| | 173 | |
| | 174 | public function test_get_site_by_name_and_network_with_www_subdirectory() { |
| | 175 | if ( is_subdomain_install() ) { |
| | 176 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| | 177 | } |
| | 178 | |
| | 179 | $result = get_site_by( array( |
| | 180 | 'slug' => 'foo', |
| | 181 | 'network_id' => self::$network_ids['www.wordpress.net/'], |
| | 182 | ) ); |
| | 183 | |
| | 184 | $this->assertEquals( self::$site_ids['www.wordpress.net/foo/'], $result->id ); |
| | 185 | } |
| | 186 | |
| | 187 | public function test_get_site_by_domain_subdomain() { |
| | 188 | if ( ! is_subdomain_install() ) { |
| | 189 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| | 190 | } |
| | 191 | |
| | 192 | $result = get_site_by( 'domain', 'wordpress.org' ); |
| | 193 | $this->assertEquals( self::$site_ids['wordpress.org/'], $result->id ); |
| | 194 | } |
| | 195 | |
| | 196 | public function test_get_site_by_domain_subdirectory() { |
| | 197 | if ( is_subdomain_install() ) { |
| | 198 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| | 199 | } |
| | 200 | |
| | 201 | $result = get_site_by( 'domain', 'wordpress.org' ); |
| | 202 | $this->assertNull( $result ); |
| | 203 | } |
| | 204 | |
| | 205 | public function test_get_site_by_path() { |
| | 206 | $result = get_site_by( 'path', '/foo/' ); |
| | 207 | $this->assertNull( $result ); |
| | 208 | } |
| | 209 | } |
| | 210 | |
| | 211 | endif; |