1 | <?php |
---|
2 | |
---|
3 | if ( is_multisite() ) : |
---|
4 | |
---|
5 | /** |
---|
6 | * Tests specific to the bootstrap process of Multisite. |
---|
7 | * |
---|
8 | * @group ms-bootstrap |
---|
9 | * @group multisite |
---|
10 | */ |
---|
11 | class Tests_Multisite_Bootstrap 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 | 'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/' ), |
---|
19 | 'wordpress.org/one/' => array( 'domain' => 'wordpress.org', 'path' => '/one/' ), |
---|
20 | 'wordpress.net/' => array( 'domain' => 'wordpress.net', 'path' => '/' ), |
---|
21 | 'www.wordpress.net/' => array( 'domain' => 'www.wordpress.net', 'path' => '/' ), |
---|
22 | 'www.wordpress.net/two/' => array( 'domain' => 'www.wordpress.net', 'path' => '/two/' ), |
---|
23 | 'wordpress.net/three/' => array( 'domain' => 'wordpress.net', 'path' => '/three/' ), |
---|
24 | ); |
---|
25 | |
---|
26 | foreach ( self::$network_ids as &$id ) { |
---|
27 | $id = $factory->network->create( $id ); |
---|
28 | } |
---|
29 | unset( $id ); |
---|
30 | |
---|
31 | self::$site_ids = array( |
---|
32 | 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', 'site_id' => self::$network_ids['wordpress.org/'] ), |
---|
33 | 'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['wordpress.org/'] ), |
---|
34 | 'wordpress.org/foo/bar/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/bar/', 'site_id' => self::$network_ids['wordpress.org/'] ), |
---|
35 | 'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/', 'site_id' => self::$network_ids['make.wordpress.org/'] ), |
---|
36 | 'make.wordpress.org/foo/' => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['make.wordpress.org/'] ), |
---|
37 | 'www.w.org/' => array( 'domain' => 'www.w.org', 'path' => '/' ), |
---|
38 | 'www.w.org/foo/' => array( 'domain' => 'www.w.org', 'path' => '/foo/' ), |
---|
39 | 'www.w.org/foo/bar/' => array( 'domain' => 'www.w.org', 'path' => '/foo/bar/' ), |
---|
40 | ); |
---|
41 | |
---|
42 | foreach ( self::$site_ids as &$id ) { |
---|
43 | $id = $factory->blog->create( $id ); |
---|
44 | } |
---|
45 | unset( $id ); |
---|
46 | } |
---|
47 | |
---|
48 | public static function wpTearDownAfterClass() { |
---|
49 | global $wpdb; |
---|
50 | |
---|
51 | foreach( self::$site_ids as $id ) { |
---|
52 | wpmu_delete_blog( $id, true ); |
---|
53 | } |
---|
54 | |
---|
55 | foreach( self::$network_ids as $id ) { |
---|
56 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) ); |
---|
57 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) ); |
---|
58 | } |
---|
59 | |
---|
60 | wp_update_network_site_counts(); |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * @ticket 27003 |
---|
65 | * @dataProvider data_get_network_by_path |
---|
66 | * |
---|
67 | * @param string $expected_key The array key associated with expected data for the test. |
---|
68 | * @param string $domain The requested domain. |
---|
69 | * @param string $path The requested path. |
---|
70 | * @param string $message The message to pass for failed tests. |
---|
71 | */ |
---|
72 | function test_get_network_by_path( $expected_key, $domain, $path, $message ) { |
---|
73 | $network = get_network_by_path( $domain, $path ); |
---|
74 | $this->assertEquals( self::$network_ids[ $expected_key ], $network->id, $message ); |
---|
75 | } |
---|
76 | |
---|
77 | public function data_get_network_by_path() { |
---|
78 | return array( |
---|
79 | array( 'wordpress.org/', 'wordpress.org', '/', 'A standard domain and path request should work.' ), |
---|
80 | array( 'wordpress.net/', 'wordpress.net', '/notapath/', 'A missing path on a top level domain should find the correct network.' ), |
---|
81 | array( 'www.wordpress.net/', 'www.wordpress.net', '/notapath/', 'A missing path should find the correct network.' ), |
---|
82 | array( 'wordpress.org/one/', 'www.wordpress.org', '/one/', 'Should find the path despite the www.' ), |
---|
83 | array( 'wordpress.org/', 'site1.wordpress.org', '/one/', 'Should not find path because domains do not match.' ), |
---|
84 | array( 'wordpress.net/three/', 'wordpress.net', '/three/', 'A network can have a path.' ), |
---|
85 | array( 'www.wordpress.net/two/', 'www.wordpress.net', '/two/', 'A www network with a path can coexist with a non-www network.' ), |
---|
86 | array( 'wordpress.net/', 'site1.wordpress.net', '/notapath/', 'An invalid subdomain should find the top level network domain.' ), |
---|
87 | array( 'wordpress.net/', 'site1.wordpress.net', '/three/', 'An invalid subdomain and path should find the top level network domain.' ), |
---|
88 | ); |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * @ticket 27003 |
---|
93 | * @ticket 27927 |
---|
94 | * @dataProvider data_get_site_by_path |
---|
95 | * |
---|
96 | * @param string $expected_key The array key associated with expected data for the test. |
---|
97 | * @param string $domain The requested domain. |
---|
98 | * @param string $path The requested path. |
---|
99 | * @param int $segments Optional. Number of segments to use in `get_site_by_path()`. |
---|
100 | */ |
---|
101 | public function test_get_site_by_path( $expected_key, $domain, $path, $segments = null ) { |
---|
102 | $site = get_site_by_path( $domain, $path, $segments ); |
---|
103 | |
---|
104 | if ( $expected_key ) { |
---|
105 | $this->assertEquals( self::$site_ids[ $expected_key ], $site->blog_id ); |
---|
106 | } else { |
---|
107 | $this->assertFalse( $site ); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | public function data_get_site_by_path() { |
---|
112 | return array( |
---|
113 | array( 'wordpress.org/', 'wordpress.org', '/notapath/' ), |
---|
114 | array( 'wordpress.org/', 'www.wordpress.org', '/notapath/' ), |
---|
115 | array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/' ), |
---|
116 | array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/' ), |
---|
117 | array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/', 3 ), |
---|
118 | array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/', 3 ), |
---|
119 | array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/', 2 ), |
---|
120 | array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/', 2 ), |
---|
121 | array( 'wordpress.org/foo/', 'wordpress.org', '/foo/bar/baz/', 1 ), |
---|
122 | array( 'wordpress.org/foo/', 'www.wordpress.org', '/foo/bar/baz/', 1 ), |
---|
123 | array( 'wordpress.org/', 'wordpress.org', '/', 0 ), |
---|
124 | array( 'wordpress.org/', 'www.wordpress.org', '/', 0 ), |
---|
125 | array( 'make.wordpress.org/foo/', 'make.wordpress.org', '/foo/bar/baz/quz/', 4 ), |
---|
126 | array( 'make.wordpress.org/foo/', 'www.make.wordpress.org', '/foo/bar/baz/quz/', 4 ), |
---|
127 | array( 'www.w.org/', 'www.w.org', '/', 0 ), |
---|
128 | array( 'www.w.org/', 'www.w.org', '/notapath' ), |
---|
129 | array( 'www.w.org/foo/bar/', 'www.w.org', '/foo/bar/baz/' ), |
---|
130 | array( 'www.w.org/foo/', 'www.w.org', '/foo/bar/baz/', 1 ), |
---|
131 | |
---|
132 | // A site installed with www will not be found by the root domain. |
---|
133 | array( false, 'w.org', '/' ), |
---|
134 | array( false, 'w.org', '/notapath/' ), |
---|
135 | array( false, 'w.org', '/foo/bar/baz/' ), |
---|
136 | array( false, 'w.org', '/foo/bar/baz/', 1 ), |
---|
137 | |
---|
138 | // A site will not be found by its root domain when an invalid subdomain is requested. |
---|
139 | array( false, 'invalid.wordpress.org', '/' ), |
---|
140 | array( false, 'invalid.wordpress.org', '/foo/bar/' ), |
---|
141 | ); |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * @ticket 27884 |
---|
146 | * @dataProvider data_multisite_bootstrap |
---|
147 | * |
---|
148 | * @param string $site_key The array key associated with the expected site for the test. |
---|
149 | * @param string $network_key The array key associated with the expected network for the test. |
---|
150 | * @param string $domain The requested domain. |
---|
151 | * @param string $path The requested path. |
---|
152 | */ |
---|
153 | function test_multisite_bootstrap( $site_key, $network_key, $domain, $path ) { |
---|
154 | global $current_blog; |
---|
155 | |
---|
156 | $expected = array( |
---|
157 | 'network_id' => self::$network_ids[ $network_key ], |
---|
158 | 'site_id' => self::$site_ids[ $site_key ], |
---|
159 | ); |
---|
160 | |
---|
161 | ms_load_current_site_and_network( $domain, $path ); |
---|
162 | $actual = array( |
---|
163 | 'network_id' => $current_blog->site_id, |
---|
164 | 'site_id' => $current_blog->blog_id, |
---|
165 | ); |
---|
166 | ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' ); |
---|
167 | |
---|
168 | $this->assertEqualSetsWithIndex( $expected, $actual ); |
---|
169 | } |
---|
170 | |
---|
171 | public function data_multisite_bootstrap() { |
---|
172 | return array( |
---|
173 | array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/' ), |
---|
174 | array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/2014/04/23/hello-world/' ), |
---|
175 | array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/sample-page/' ), |
---|
176 | array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/?p=1' ), |
---|
177 | array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/wp-admin/' ), |
---|
178 | array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/' ), |
---|
179 | array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/FOO/' ), |
---|
180 | array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/2014/04/23/hello-world/' ), |
---|
181 | array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/sample-page/' ), |
---|
182 | array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/?p=1' ), |
---|
183 | array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/wp-admin/' ), |
---|
184 | array( 'make.wordpress.org/', 'make.wordpress.org/', 'make.wordpress.org', '/' ), |
---|
185 | array( 'make.wordpress.org/foo/', 'make.wordpress.org/', 'make.wordpress.org', '/foo/' ), |
---|
186 | ); |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * @ticket 27884 |
---|
191 | */ |
---|
192 | public function test_multisite_bootstrap_additional_path_segments() { |
---|
193 | global $current_blog; |
---|
194 | |
---|
195 | $expected = array( |
---|
196 | 'network_id' => self::$network_ids['wordpress.org/'], |
---|
197 | 'site_id' => self::$site_ids['wordpress.org/foo/bar/'], |
---|
198 | ); |
---|
199 | add_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) ); |
---|
200 | ms_load_current_site_and_network( 'wordpress.org', '/foo/bar/' ); |
---|
201 | $actual = array( |
---|
202 | 'network_id' => $current_blog->site_id, |
---|
203 | 'site_id' => $current_blog->blog_id, |
---|
204 | ); |
---|
205 | remove_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) ); |
---|
206 | ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' ); |
---|
207 | |
---|
208 | $this->assertEqualSetsWithIndex( $expected, $actual ); |
---|
209 | } |
---|
210 | |
---|
211 | public function filter_path_segments_to_two() { |
---|
212 | return 2; |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | endif; |
---|