| | 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() { |
| | 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_current_id() { |
| | 64 | $result = get_site_by( 'id', null ); |
| | 65 | |
| | 66 | $this->assertEquals( get_current_blog_id(), $result->id ); |
| | 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 | /** |
| | 136 | * @dataProvider data_get_site_by_url |
| | 137 | */ |
| | 138 | public function test_get_site_by_url( $url, $expected ) { |
| | 139 | $result = get_site_by( 'url', $url ); |
| | 140 | |
| | 141 | $this->assertEquals( self::$site_ids[ $expected ], $result->id ); |
| | 142 | } |
| | 143 | |
| | 144 | public function data_get_site_by_url() { |
| | 145 | return array( |
| | 146 | array( |
| | 147 | 'wordpress.org/foo/', |
| | 148 | 'wordpress.org/foo/', |
| | 149 | ), |
| | 150 | array( |
| | 151 | 'wordpress.org/foo', |
| | 152 | 'wordpress.org/foo/', |
| | 153 | ), |
| | 154 | array( |
| | 155 | 'foo.wordpress.org/', |
| | 156 | 'foo.wordpress.org/', |
| | 157 | ), |
| | 158 | array( |
| | 159 | 'foo.wordpress.org', |
| | 160 | 'foo.wordpress.org/', |
| | 161 | ), |
| | 162 | array( |
| | 163 | 'www.wordpress.net/', |
| | 164 | 'www.wordpress.net/', |
| | 165 | ), |
| | 166 | array( |
| | 167 | 'www.wordpress.org/', |
| | 168 | 'www.wordpress.org/', |
| | 169 | ), |
| | 170 | ); |
| | 171 | } |
| | 172 | |
| | 173 | public function test_get_site_by_url_with_invalid_url() { |
| | 174 | $result = get_site_by( 'url', 'not a url' ); |
| | 175 | |
| | 176 | $this->assertNull( $result ); |
| | 177 | } |
| | 178 | |
| | 179 | public function test_get_site_by_domain_subdomain() { |
| | 180 | if ( ! is_subdomain_install() ) { |
| | 181 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| | 182 | } |
| | 183 | |
| | 184 | $result = get_site_by( 'domain', 'foo.wordpress.org' ); |
| | 185 | |
| | 186 | $this->assertEquals( self::$site_ids['foo.wordpress.org/'], $result->id ); |
| | 187 | } |
| | 188 | |
| | 189 | public function test_get_site_by_domain_subdirectory() { |
| | 190 | if ( is_subdomain_install() ) { |
| | 191 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| | 192 | } |
| | 193 | |
| | 194 | $result = get_site_by( 'domain', 'foo.wordpress.org' ); |
| | 195 | |
| | 196 | $this->assertNull( $result ); |
| | 197 | } |
| | 198 | |
| | 199 | public function test_get_site_by_path_subdomain() { |
| | 200 | if ( ! is_subdomain_install() ) { |
| | 201 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| | 202 | } |
| | 203 | |
| | 204 | $result = get_site_by( 'path', '/foo/' ); |
| | 205 | |
| | 206 | $this->assertNull( $result ); |
| | 207 | } |
| | 208 | |
| | 209 | public function test_get_site_by_path_subdirectory() { |
| | 210 | if ( is_subdomain_install() ) { |
| | 211 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| | 212 | } |
| | 213 | |
| | 214 | $result = get_site_by( 'path', '/foo/' ); |
| | 215 | |
| | 216 | $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $result->id ); |
| | 217 | } |
| | 218 | } |
| | 219 | |
| | 220 | endif; |