| 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.org/' => array( 'domain' => 'www.wordpress.org', 'path' => '/', 'site_id' => self::$network_ids['wordpress.org/'] ), |
| 31 | 'www.wordpress.net/' => array( 'domain' => 'www.wordpress.net', 'path' => '/', 'site_id' => self::$network_ids['www.wordpress.net/'] ), |
| 32 | 'foo.wordpress.net/' => array( 'domain' => 'foo.wordpress.net', 'path' => '/', 'site_id' => self::$network_ids['www.wordpress.net/'] ), |
| 33 | 'www.wordpress.net/foo/' => array( 'domain' => 'www.wordpress.net', 'path' => '/foo/', 'site_id' => self::$network_ids['www.wordpress.net/'] ), |
| 34 | ); |
| 35 | |
| 36 | foreach ( self::$site_ids as &$id ) { |
| 37 | $id = $factory->blog->create( $id ); |
| 38 | } |
| 39 | unset( $id ); |
| 40 | } |
| 41 | |
| 42 | public static function wpTearDownAfterClass() { |
| 43 | global $wpdb; |
| 44 | |
| 45 | foreach( self::$site_ids as $id ) { |
| 46 | wpmu_delete_blog( $id, true ); |
| 47 | } |
| 48 | |
| 49 | foreach( self::$network_ids as $id ) { |
| 50 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) ); |
| 51 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) ); |
| 52 | } |
| 53 | |
| 54 | wp_update_network_site_counts(); |
| 55 | } |
| 56 | |
| 57 | public function test_get_site_by_id_with_valid_id() { |
| 58 | $result = get_site_by( 'id', self::$site_ids['wordpress.org/'] ); |
| 59 | |
| 60 | $this->assertEquals( self::$site_ids['wordpress.org/'], $result->id ); |
| 61 | } |
| 62 | |
| 63 | public function test_get_site_by_id_with_invalid_id() { |
| 64 | $result = get_site_by( 'id', 'wp.org' ); |
| 65 | |
| 66 | $this->assertNull( $result ); |
| 67 | } |
| 68 | |
| 69 | public function test_get_site_by_slug_subdomain() { |
| 70 | if ( ! is_subdomain_install() ) { |
| 71 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| 72 | } |
| 73 | |
| 74 | $result = get_site_by( 'slug', 'foo', self::$network_ids['wordpress.org/'] ); |
| 75 | |
| 76 | $this->assertEquals( self::$site_ids['foo.wordpress.org/'], $result->id ); |
| 77 | } |
| 78 | |
| 79 | public function test_get_site_by_slug_with_www_subdomain() { |
| 80 | if ( ! is_subdomain_install() ) { |
| 81 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| 82 | } |
| 83 | |
| 84 | $result = get_site_by( 'slug', 'foo', self::$network_ids['www.wordpress.net/'] ); |
| 85 | |
| 86 | $this->assertEquals( self::$site_ids['foo.wordpress.net/'], $result->id ); |
| 87 | } |
| 88 | |
| 89 | public function test_get_site_by_slug_subdirectory() { |
| 90 | if ( is_subdomain_install() ) { |
| 91 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| 92 | } |
| 93 | |
| 94 | $result = get_site_by( 'slug', 'foo', self::$network_ids['wordpress.org/'] ); |
| 95 | |
| 96 | $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $result->id ); |
| 97 | } |
| 98 | |
| 99 | public function test_get_site_by_slug_with_www_subdirectory() { |
| 100 | if ( is_subdomain_install() ) { |
| 101 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| 102 | } |
| 103 | |
| 104 | $result = get_site_by( 'slug', 'foo', self::$network_ids['www.wordpress.net/'] ); |
| 105 | |
| 106 | $this->assertEquals( self::$site_ids['www.wordpress.net/foo/'], $result->id ); |
| 107 | } |
| 108 | |
| 109 | public function test_get_site_by_slug_with_first_network() { |
| 110 | $result = get_site_by( 'slug', 'foo', self::$network_ids['wordpress.org/'] ); |
| 111 | |
| 112 | if ( is_subdomain_install() ) { |
| 113 | $this->assertEquals( self::$site_ids['foo.wordpress.org/'], $result->id ); |
| 114 | } else { |
| 115 | $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $result->id ); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public function test_get_site_by_slug_with_second_network() { |
| 120 | $result = get_site_by( 'slug', 'foo', self::$network_ids['www.wordpress.net/'] ); |
| 121 | |
| 122 | if ( is_subdomain_install() ) { |
| 123 | $this->assertEquals( self::$site_ids['foo.wordpress.net/'], $result->id ); |
| 124 | } else { |
| 125 | $this->assertEquals( self::$site_ids['www.wordpress.net/foo/'], $result->id ); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | public function test_get_site_by_slug_with_invalid_network() { |
| 130 | $result = get_site_by( 'slug', 'foo', 444 ); |
| 131 | |
| 132 | $this->assertNull( $result ); |
| 133 | } |
| 134 | |
| 135 | public function test_get_site_by_slug_with_empty_string_returns_null() { |
| 136 | $result = get_site_by( 'slug', '' ); |
| 137 | |
| 138 | $this->assertNull( $result ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @dataProvider data_get_site_by_url |
| 143 | */ |
| 144 | public function test_get_site_by_url( $url, $expected ) { |
| 145 | $result = get_site_by( 'url', $url ); |
| 146 | |
| 147 | $this->assertEquals( self::$site_ids[ $expected ], $result->id ); |
| 148 | } |
| 149 | |
| 150 | public function data_get_site_by_url() { |
| 151 | return array( |
| 152 | array( |
| 153 | 'wordpress.org/foo/', |
| 154 | 'wordpress.org/foo/', |
| 155 | ), |
| 156 | array( |
| 157 | 'wordpress.org/foo', |
| 158 | 'wordpress.org/foo/', |
| 159 | ), |
| 160 | array( |
| 161 | 'foo.wordpress.org/', |
| 162 | 'foo.wordpress.org/', |
| 163 | ), |
| 164 | array( |
| 165 | 'foo.wordpress.org', |
| 166 | 'foo.wordpress.org/', |
| 167 | ), |
| 168 | array( |
| 169 | 'www.wordpress.net/', |
| 170 | 'www.wordpress.net/', |
| 171 | ), |
| 172 | array( |
| 173 | 'www.wordpress.org/', |
| 174 | 'www.wordpress.org/', |
| 175 | ), |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | public function test_get_site_by_url_with_empty_string_returns_null() { |
| 180 | $result = get_site_by( 'url', '' ); |
| 181 | |
| 182 | $this->assertNull( $result ); |
| 183 | } |
| 184 | |
| 185 | public function test_get_site_by_url_with_invalid_url() { |
| 186 | $result = get_site_by( 'url', 'not a url' ); |
| 187 | |
| 188 | $this->assertNull( $result ); |
| 189 | } |
| 190 | |
| 191 | public function test_get_site_by_domain_subdomain() { |
| 192 | if ( ! is_subdomain_install() ) { |
| 193 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| 194 | } |
| 195 | |
| 196 | $result = get_site_by( 'domain', 'foo.wordpress.org' ); |
| 197 | |
| 198 | $this->assertEquals( self::$site_ids['foo.wordpress.org/'], $result->id ); |
| 199 | } |
| 200 | |
| 201 | public function test_get_site_by_domain_subdirectory() { |
| 202 | if ( is_subdomain_install() ) { |
| 203 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| 204 | } |
| 205 | |
| 206 | $result = get_site_by( 'domain', 'foo.wordpress.org' ); |
| 207 | |
| 208 | $this->assertNull( $result ); |
| 209 | } |
| 210 | |
| 211 | public function test_get_site_by_domain_with_empty_string_returns_null() { |
| 212 | if ( ! is_subdomain_install() ) { |
| 213 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| 214 | } |
| 215 | |
| 216 | $result = get_site_by( 'domain', '' ); |
| 217 | |
| 218 | $this->assertNull( $result ); |
| 219 | } |
| 220 | |
| 221 | public function test_get_site_by_path_subdomain() { |
| 222 | if ( ! is_subdomain_install() ) { |
| 223 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| 224 | } |
| 225 | |
| 226 | $result = get_site_by( 'path', '/foo/' ); |
| 227 | |
| 228 | $this->assertNull( $result ); |
| 229 | } |
| 230 | |
| 231 | public function test_get_site_by_path_subdirectory() { |
| 232 | if ( is_subdomain_install() ) { |
| 233 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| 234 | } |
| 235 | |
| 236 | $result = get_site_by( 'path', '/foo/' ); |
| 237 | |
| 238 | $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $result->id ); |
| 239 | } |
| 240 | |
| 241 | public function test_get_site_by_path_with_empty_string_returns_null() { |
| 242 | if ( is_subdomain_install() ) { |
| 243 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| 244 | } |
| 245 | |
| 246 | $result = get_site_by( 'path', '' ); |
| 247 | |
| 248 | $this->assertNull( $result ); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | endif; |