Make WordPress Core

source: trunk/tests/phpunit/tests/multisite/bootstrap.php

Last change on this file was 60148, checked in by SergeyBiryukov, 3 months ago

Tests: Use the ms-required group where appropriate.

This replaces the if ( is_multisite() ) conditional wrapping entire test classes with the ms-required group for more consistency across the test suite.

Follow-up to [40520].

See #63167.

  • Property svn:eol-style set to native
File size: 13.3 KB
Line 
1<?php
2
3/**
4 * Tests specific to the bootstrap process of Multisite.
5 *
6 * @group ms-bootstrap
7 * @group ms-required
8 * @group multisite
9 */
10class Tests_Multisite_Bootstrap extends WP_UnitTestCase {
11
12        protected static $network_ids;
13        protected static $site_ids;
14
15        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
16                self::$network_ids = array(
17                        'wordpress.org/'         => array(
18                                'domain' => 'wordpress.org',
19                                'path'   => '/',
20                        ),
21                        'make.wordpress.org/'    => array(
22                                'domain' => 'make.wordpress.org',
23                                'path'   => '/',
24                        ),
25                        'wordpress.org/one/'     => array(
26                                'domain' => 'wordpress.org',
27                                'path'   => '/one/',
28                        ),
29                        'wordpress.org/one/b/'   => array(
30                                'domain' => 'wordpress.org',
31                                'path'   => '/one/b/',
32                        ),
33                        'wordpress.net/'         => array(
34                                'domain' => 'wordpress.net',
35                                'path'   => '/',
36                        ),
37                        'www.wordpress.net/'     => array(
38                                'domain' => 'www.wordpress.net',
39                                'path'   => '/',
40                        ),
41                        'www.wordpress.net/two/' => array(
42                                'domain' => 'www.wordpress.net',
43                                'path'   => '/two/',
44                        ),
45                        'wordpress.net/three/'   => array(
46                                'domain' => 'wordpress.net',
47                                'path'   => '/three/',
48                        ),
49                );
50
51                foreach ( self::$network_ids as &$id ) {
52                        $id = $factory->network->create( $id );
53                }
54                unset( $id );
55
56                self::$site_ids = array(
57                        'wordpress.org/'          => array(
58                                'domain'     => 'wordpress.org',
59                                'path'       => '/',
60                                'network_id' => self::$network_ids['wordpress.org/'],
61                        ),
62                        'wordpress.org/foo/'      => array(
63                                'domain'     => 'wordpress.org',
64                                'path'       => '/foo/',
65                                'network_id' => self::$network_ids['wordpress.org/'],
66                        ),
67                        'wordpress.org/foo/bar/'  => array(
68                                'domain'     => 'wordpress.org',
69                                'path'       => '/foo/bar/',
70                                'network_id' => self::$network_ids['wordpress.org/'],
71                        ),
72                        'make.wordpress.org/'     => array(
73                                'domain'     => 'make.wordpress.org',
74                                'path'       => '/',
75                                'network_id' => self::$network_ids['make.wordpress.org/'],
76                        ),
77                        'make.wordpress.org/foo/' => array(
78                                'domain'     => 'make.wordpress.org',
79                                'path'       => '/foo/',
80                                'network_id' => self::$network_ids['make.wordpress.org/'],
81                        ),
82                        'www.w.org/'              => array(
83                                'domain' => 'www.w.org',
84                                'path'   => '/',
85                        ),
86                        'www.w.org/foo/'          => array(
87                                'domain' => 'www.w.org',
88                                'path'   => '/foo/',
89                        ),
90                        'www.w.org/foo/bar/'      => array(
91                                'domain' => 'www.w.org',
92                                'path'   => '/foo/bar/',
93                        ),
94                );
95
96                foreach ( self::$site_ids as &$id ) {
97                        $id = $factory->blog->create( $id );
98                }
99                unset( $id );
100        }
101
102        public static function wpTearDownAfterClass() {
103                global $wpdb;
104
105                foreach ( self::$site_ids as $id ) {
106                        wp_delete_site( $id );
107                }
108
109                foreach ( self::$network_ids as $id ) {
110                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
111                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
112                }
113
114                wp_update_network_site_counts();
115        }
116
117        /**
118         * @ticket 27003
119         * @dataProvider data_get_network_by_path
120         *
121         * @param string $expected_key The array key associated with expected data for the test.
122         * @param string $domain       The requested domain.
123         * @param string $path         The requested path.
124         * @param string $message      The message to pass for failed tests.
125         */
126        public function test_get_network_by_path( $expected_key, $domain, $path, $message ) {
127                $network = get_network_by_path( $domain, $path );
128                $this->assertSame( self::$network_ids[ $expected_key ], $network->id, $message );
129        }
130
131        public function data_get_network_by_path() {
132                return array(
133                        array( 'wordpress.org/', 'wordpress.org', '/', 'A standard domain and path request should work.' ),
134                        array( 'wordpress.net/', 'wordpress.net', '/notapath/', 'A missing path on a top level domain should find the correct network.' ),
135                        array( 'www.wordpress.net/', 'www.wordpress.net', '/notapath/', 'A missing path should find the correct network.' ),
136                        array( 'wordpress.org/one/', 'www.wordpress.org', '/one/', 'Should find the path despite the www.' ),
137                        array( 'wordpress.org/one/', 'wordpress.org', '/one/page/', 'A request with two path segments should find the correct network.' ),
138                        array( 'wordpress.org/one/b/', 'wordpress.org', '/one/b/', 'A request with two valid path segments should find the correct network.' ),
139                        array( 'wordpress.org/', 'site1.wordpress.org', '/one/', 'Should not find path because domains do not match.' ),
140                        array( 'wordpress.net/three/', 'wordpress.net', '/three/', 'A network can have a path.' ),
141                        array( 'www.wordpress.net/two/', 'www.wordpress.net', '/two/', 'A www network with a path can coexist with a non-www network.' ),
142                        array( 'wordpress.net/', 'site1.wordpress.net', '/notapath/', 'An invalid subdomain should find the top level network domain.' ),
143                        array( 'wordpress.net/', 'site1.wordpress.net', '/three/', 'An invalid subdomain and path should find the top level network domain.' ),
144                        array( 'wordpress.net/', 'x.y.wordpress.net', '/', 'An invalid two level subdomain should find the top level network domain.' ),
145                );
146        }
147
148        /**
149         * @ticket 37217
150         * @dataProvider data_get_network_by_path_with_zero_path_segments
151         *
152         * @param string $expected_key The array key associated with expected data for the test.
153         * @param string $domain       The requested domain.
154         * @param string $path         The requested path.
155         * @param string $message      The message to pass for failed tests.
156         */
157        public function test_get_network_by_path_with_zero_path_segments( $expected_key, $domain, $path, $message ) {
158                add_filter( 'network_by_path_segments_count', '__return_zero' );
159
160                $network = get_network_by_path( $domain, $path );
161
162                remove_filter( 'network_by_path_segments_count', '__return_zero' );
163
164                $this->assertSame( self::$network_ids[ $expected_key ], $network->id, $message );
165        }
166
167        public function data_get_network_by_path_with_zero_path_segments() {
168                return array(
169                        array( 'wordpress.org/', 'wordpress.org', '/', 'A standard domain and path request should work.' ),
170                        array( 'wordpress.net/', 'wordpress.net', '/notapath/', 'A network matching a top level domain should be found regardless of path.' ),
171                        array( 'www.wordpress.net/', 'www.wordpress.net', '/notapath/', 'A network matching a domain should be found regardless of path.' ),
172                        array( 'wordpress.org/', 'www.wordpress.org', '/one/', 'Should find the network despite the www and regardless of path.' ),
173                        array( 'wordpress.org/', 'site1.wordpress.org', '/one/', 'Should find the network with the corresponding top level domain regardless of path.' ),
174                        array( 'www.wordpress.net/', 'www.wordpress.net', '/two/', 'A www network can coexist with a non-www network.' ),
175                        array( 'make.wordpress.org/', 'make.wordpress.org', '/notapath/', 'A subdomain network should be found regardless of path.' ),
176                        array( 'wordpress.net/', 'x.y.wordpress.net', '/', 'An invalid two level subdomain should find the top level network domain.' ),
177                );
178        }
179
180        /**
181         * Even if a matching network is available, it should not match if the the filtered
182         * value for network path segments is fewer than the number of paths passed.
183         */
184        public function test_get_network_by_path_with_forced_single_path_segment_returns_single_path_network() {
185                add_filter( 'network_by_path_segments_count', array( $this, 'filter_network_path_segments' ) );
186                $network = get_network_by_path( 'wordpress.org', '/one/b/' );
187                remove_filter( 'network_by_path_segments_count', array( $this, 'filter_network_path_segments' ) );
188
189                $this->assertSame( self::$network_ids['wordpress.org/one/'], $network->id );
190        }
191
192        public function filter_network_path_segments() {
193                return 1;
194        }
195
196        /**
197         * @ticket 27003
198         * @ticket 27927
199         * @dataProvider data_get_site_by_path
200         *
201         * @param string $expected_key The array key associated with expected data for the test.
202         * @param string $domain       The requested domain.
203         * @param string $path         The requested path.
204         * @param int    $segments     Optional. Number of segments to use in `get_site_by_path()`.
205         */
206        public function test_get_site_by_path( $expected_key, $domain, $path, $segments = null ) {
207                $site = get_site_by_path( $domain, $path, $segments );
208
209                if ( $expected_key ) {
210                        $this->assertEquals( self::$site_ids[ $expected_key ], $site->blog_id );
211                } else {
212                        $this->assertFalse( $site );
213                }
214        }
215
216        public function data_get_site_by_path() {
217                return array(
218                        array( 'wordpress.org/', 'wordpress.org', '/notapath/' ),
219                        array( 'wordpress.org/', 'www.wordpress.org', '/notapath/' ),
220                        array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/' ),
221                        array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/' ),
222                        array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/', 3 ),
223                        array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/', 3 ),
224                        array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/', 2 ),
225                        array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/', 2 ),
226                        array( 'wordpress.org/foo/', 'wordpress.org', '/foo/bar/baz/', 1 ),
227                        array( 'wordpress.org/foo/', 'www.wordpress.org', '/foo/bar/baz/', 1 ),
228                        array( 'wordpress.org/', 'wordpress.org', '/', 0 ),
229                        array( 'wordpress.org/', 'www.wordpress.org', '/', 0 ),
230                        array( 'make.wordpress.org/foo/', 'make.wordpress.org', '/foo/bar/baz/quz/', 4 ),
231                        array( 'make.wordpress.org/foo/', 'www.make.wordpress.org', '/foo/bar/baz/quz/', 4 ),
232                        array( 'www.w.org/', 'www.w.org', '/', 0 ),
233                        array( 'www.w.org/', 'www.w.org', '/notapath' ),
234                        array( 'www.w.org/foo/bar/', 'www.w.org', '/foo/bar/baz/' ),
235                        array( 'www.w.org/foo/', 'www.w.org', '/foo/bar/baz/', 1 ),
236
237                        // A site installed with www will not be found by the root domain.
238                        array( false, 'w.org', '/' ),
239                        array( false, 'w.org', '/notapath/' ),
240                        array( false, 'w.org', '/foo/bar/baz/' ),
241                        array( false, 'w.org', '/foo/bar/baz/', 1 ),
242
243                        // A site will not be found by its root domain when an invalid subdomain is requested.
244                        array( false, 'invalid.wordpress.org', '/' ),
245                        array( false, 'invalid.wordpress.org', '/foo/bar/' ),
246                );
247        }
248
249        /**
250         * @ticket 27884
251         * @dataProvider data_multisite_bootstrap
252         *
253         * @param string $site_key    The array key associated with the expected site for the test.
254         * @param string $network_key The array key associated with the expected network for the test.
255         * @param string $domain      The requested domain.
256         * @param string $path        The requested path.
257         */
258        public function test_multisite_bootstrap( $site_key, $network_key, $domain, $path ) {
259                global $current_blog;
260
261                $expected = array(
262                        'network_id' => self::$network_ids[ $network_key ],
263                        'site_id'    => self::$site_ids[ $site_key ],
264                );
265
266                ms_load_current_site_and_network( $domain, $path );
267
268                $actual = array(
269                        'network_id' => $current_blog->site_id,
270                        'site_id'    => $current_blog->blog_id,
271                );
272
273                ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' );
274
275                $this->assertEqualSetsWithIndex( $expected, $actual );
276        }
277
278        public function data_multisite_bootstrap() {
279                return array(
280                        array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/' ),
281                        array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/2014/04/23/hello-world/' ),
282                        array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/sample-page/' ),
283                        array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/?p=1' ),
284                        array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/wp-admin/' ),
285                        array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/' ),
286                        array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/FOO/' ),
287                        array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/2014/04/23/hello-world/' ),
288                        array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/sample-page/' ),
289                        array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/?p=1' ),
290                        array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/wp-admin/' ),
291                        array( 'make.wordpress.org/', 'make.wordpress.org/', 'make.wordpress.org', '/' ),
292                        array( 'make.wordpress.org/foo/', 'make.wordpress.org/', 'make.wordpress.org', '/foo/' ),
293                );
294        }
295
296        /**
297         * @ticket 27884
298         */
299        public function test_multisite_bootstrap_additional_path_segments() {
300                global $current_blog;
301
302                $expected = array(
303                        'network_id' => self::$network_ids['wordpress.org/'],
304                        'site_id'    => self::$site_ids['wordpress.org/foo/bar/'],
305                );
306
307                add_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) );
308                ms_load_current_site_and_network( 'wordpress.org', '/foo/bar/' );
309
310                $actual = array(
311                        'network_id' => $current_blog->site_id,
312                        'site_id'    => $current_blog->blog_id,
313                );
314
315                remove_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) );
316                ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' );
317
318                $this->assertEqualSetsWithIndex( $expected, $actual );
319        }
320
321        /**
322         * @ticket 37053
323         */
324        public function test_get_site_by_path_returns_wp_site() {
325                add_filter( 'pre_get_site_by_path', array( $this, 'filter_pre_get_site_by_path' ), 10, 3 );
326
327                $site = get_site_by_path( 'example.com', '/foo/' );
328
329                remove_filter( 'pre_get_site_by_path', array( $this, 'filter_pre_get_site_by_path' ), 10 );
330
331                $this->assertInstanceOf( 'WP_Site', $site );
332        }
333
334        public function filter_path_segments_to_two() {
335                return 2;
336        }
337
338        public function filter_pre_get_site_by_path( $site, $domain, $path ) {
339                $site          = new stdClass();
340                $site->blog_id = 100;
341                $site->domain  = $domain;
342                $site->path    = $path;
343                $site->site_id = 1;
344
345                return $site;
346        }
347}
Note: See TracBrowser for help on using the repository browser.