| | 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_current_id() { |
| | 62 | $result = get_site_by( 'id', null ); |
| | 63 | $this->assertEquals( get_current_blog_id(), $result->id ); |
| | 64 | } |
| | 65 | |
| | 66 | public function test_get_site_by_slug_subdomain() { |
| | 67 | if ( ! is_subdomain_install() ) { |
| | 68 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| | 69 | } |
| | 70 | |
| | 71 | $result = get_site_by( 'slug', 'foo', self::$network_ids['wordpress.org/'] ); |
| | 72 | $this->assertEquals( self::$site_ids['foo.wordpress.org/'], $result->id ); |
| | 73 | } |
| | 74 | |
| | 75 | public function test_get_site_by_slug_with_www_subdomain() { |
| | 76 | if ( ! is_subdomain_install() ) { |
| | 77 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| | 78 | } |
| | 79 | |
| | 80 | $result = get_site_by( 'slug', 'foo', self::$network_ids['www.wordpress.net/'] ); |
| | 81 | $this->assertEquals( self::$site_ids['foo.wordpress.net/'], $result->id ); |
| | 82 | } |
| | 83 | |
| | 84 | public function test_get_site_by_slug_subdirectory() { |
| | 85 | if ( is_subdomain_install() ) { |
| | 86 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| | 87 | } |
| | 88 | |
| | 89 | $result = get_site_by( 'slug', 'foo', self::$network_ids['wordpress.org/'] ); |
| | 90 | $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $result->id ); |
| | 91 | } |
| | 92 | |
| | 93 | public function test_get_site_by_slug_with_www_subdirectory() { |
| | 94 | if ( is_subdomain_install() ) { |
| | 95 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| | 96 | } |
| | 97 | |
| | 98 | $result = get_site_by( 'slug', 'foo', self::$network_ids['www.wordpress.net/'] ); |
| | 99 | $this->assertEquals( self::$site_ids['www.wordpress.net/foo/'], $result->id ); |
| | 100 | } |
| | 101 | |
| | 102 | public function get_site_by_slug_with_invalid_network() { |
| | 103 | $result = get_site_by( 'slug', 'foo', 444 ); |
| | 104 | $this->assertNull( $result ); |
| | 105 | } |
| | 106 | |
| | 107 | public function get_site_by_url( $url, $expected ) { |
| | 108 | $result = get_site_by( 'url', $url ); |
| | 109 | $this->assertEquals( $expected, $result->id ); |
| | 110 | } |
| | 111 | |
| | 112 | public function data_get_site_by_url() { |
| | 113 | return array( |
| | 114 | array( |
| | 115 | 'wordpress.org/foo/', |
| | 116 | self::$site_ids['wordpress.org/foo/'], |
| | 117 | ), |
| | 118 | array( |
| | 119 | 'wordpress.org/foo', |
| | 120 | self::$site_ids['wordpress.org/foo/'], |
| | 121 | ), |
| | 122 | array( |
| | 123 | 'foo.wordpress.org/', |
| | 124 | self::$site_ids['foo.wordpress.org/'], |
| | 125 | ), |
| | 126 | array( |
| | 127 | 'foo.wordpress.org', |
| | 128 | self::$site_ids['foo.wordpress.org/'], |
| | 129 | ), |
| | 130 | array( |
| | 131 | 'www.wordpress.net/', |
| | 132 | self::$site_ids['www.wordpress.net/'], |
| | 133 | ), |
| | 134 | ); |
| | 135 | } |
| | 136 | |
| | 137 | public function get_site_by_url_with_invalid_url() { |
| | 138 | $result = get_site_by( 'url', 'not a url' ); |
| | 139 | $this->assertNull( $result ); |
| | 140 | } |
| | 141 | |
| | 142 | public function test_get_site_by_domain_subdomain() { |
| | 143 | if ( ! is_subdomain_install() ) { |
| | 144 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| | 145 | } |
| | 146 | |
| | 147 | $result = get_site_by( 'domain', 'foo.wordpress.org' ); |
| | 148 | $this->assertEquals( self::$site_ids['foo.wordpress.org/'], $result->id ); |
| | 149 | } |
| | 150 | |
| | 151 | public function test_get_site_by_domain_subdirectory() { |
| | 152 | if ( is_subdomain_install() ) { |
| | 153 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| | 154 | } |
| | 155 | |
| | 156 | $result = get_site_by( 'domain', 'foo.wordpress.org' ); |
| | 157 | $this->assertNull( $result ); |
| | 158 | } |
| | 159 | |
| | 160 | public function test_get_site_by_path_subdomain() { |
| | 161 | if ( ! is_subdomain_install() ) { |
| | 162 | $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' ); |
| | 163 | } |
| | 164 | |
| | 165 | $result = get_site_by( 'path', '/foo/' ); |
| | 166 | $this->assertNull( $result ); |
| | 167 | } |
| | 168 | |
| | 169 | public function test_get_site_by_path_subdirectory() { |
| | 170 | if ( is_subdomain_install() ) { |
| | 171 | $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' ); |
| | 172 | } |
| | 173 | |
| | 174 | $result = get_site_by( 'path', '/foo/' ); |
| | 175 | $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $result->id ); |
| | 176 | } |
| | 177 | } |
| | 178 | |
| | 179 | endif; |