Skip to content

Commit f0b35fe

Browse files
committed
Split and organize multisite unit tests
* Move `ms.php` to `multisite.php` * Create `multisite.php` under directories `option/` and `user/` to better match existing structure. * Create a `multisite/` directory containing `bootstrap.php`, `site.php`, and `network.php` for very multisite specific testing. * Add unit test groups ms-site, ms-user, ms-option, ms-network, and ms-bootstrap. Fixes #29896 git-svn-id: https://develop.svn.wordpress.org/trunk@29916 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0d58d32 commit f0b35fe

7 files changed

Lines changed: 1080 additions & 958 deletions

File tree

tests/phpunit/tests/multisite.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
if ( is_multisite() ) :
4+
5+
/**
6+
* A set of unit tests for WordPress Multisite
7+
*
8+
* @group multisite
9+
*/
10+
class Tests_Multisite extends WP_UnitTestCase {
11+
protected $suppress = false;
12+
13+
function setUp() {
14+
global $wpdb;
15+
parent::setUp();
16+
$this->suppress = $wpdb->suppress_errors();
17+
18+
$_SERVER['REMOTE_ADDR'] = '';
19+
}
20+
21+
function tearDown() {
22+
global $wpdb;
23+
parent::tearDown();
24+
$wpdb->suppress_errors( $this->suppress );
25+
}
26+
27+
function test_wpmu_log_new_registrations() {
28+
global $wpdb;
29+
30+
$user = new WP_User( 1 );
31+
$ip = preg_replace( '/[^0-9., ]/', '',$_SERVER['REMOTE_ADDR'] );
32+
33+
wpmu_log_new_registrations(1,1);
34+
35+
// currently there is no wrapper function for the registration_log
36+
$reg_blog = $wpdb->get_col( "SELECT email FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.blog_id = 1 AND IP LIKE '" . $ip . "'" );
37+
$this->assertEquals( $user->user_email, $reg_blog[ count( $reg_blog )-1 ] );
38+
}
39+
40+
/**
41+
* @ticket 21570
42+
*/
43+
function test_aggressiveness_of_is_email_address_unsafe() {
44+
update_site_option( 'banned_email_domains', array( 'bar.com', 'foo.co' ) );
45+
46+
foreach ( array( 'test@bar.com', 'test@foo.bar.com', 'test@foo.co', 'test@subdomain.foo.co' ) as $email_address ) {
47+
$this->assertTrue( is_email_address_unsafe( $email_address ), "$email_address should be UNSAFE" );
48+
}
49+
50+
foreach ( array( 'test@foobar.com', 'test@foo-bar.com', 'test@foo.com', 'test@subdomain.foo.com' ) as $email_address ) {
51+
$this->assertFalse( is_email_address_unsafe( $email_address ), "$email_address should be SAFE" );
52+
}
53+
}
54+
55+
/**
56+
* @ticket 25046
57+
*/
58+
function test_case_sensitivity_of_is_email_address_unsafe() {
59+
update_site_option( 'banned_email_domains', array( 'baR.com', 'Foo.co', 'barfoo.COM', 'BAZ.com' ) );
60+
61+
foreach ( array( 'test@Bar.com', 'tEst@bar.com', 'test@barFoo.com', 'tEst@foo.bar.com', 'test@baz.Com' ) as $email_address ) {
62+
$this->assertTrue( is_email_address_unsafe( $email_address ), "$email_address should be UNSAFE" );
63+
}
64+
65+
foreach ( array( 'test@Foobar.com', 'test@Foo-bar.com', 'tEst@foobar.com', 'test@Subdomain.Foo.com', 'test@fooBAz.com' ) as $email_address ) {
66+
$this->assertFalse( is_email_address_unsafe( $email_address ), "$email_address should be SAFE" );
67+
}
68+
69+
}
70+
}
71+
72+
endif;
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
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 $suppress = false;
13+
14+
function setUp() {
15+
global $wpdb;
16+
parent::setUp();
17+
$this->suppress = $wpdb->suppress_errors();
18+
19+
$_SERVER[ 'REMOTE_ADDR' ] = '';
20+
}
21+
22+
function tearDown() {
23+
global $wpdb;
24+
parent::tearDown();
25+
$wpdb->suppress_errors( $this->suppress );
26+
}
27+
28+
29+
/**
30+
* @ticket 27003
31+
*/
32+
function test_get_network_by_path() {
33+
global $wpdb;
34+
35+
$ids = array(
36+
'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ),
37+
'wordpress.org/one/' => array( 'domain' => 'wordpress.org', 'path' => '/one/' ),
38+
'wordpress.net/' => array( 'domain' => 'wordpress.net', 'path' => '/' ),
39+
'www.wordpress.net/' => array( 'domain' => 'www.wordpress.net', 'path' => '/' ),
40+
'www.wordpress.net/two/' => array( 'domain' => 'www.wordpress.net', 'path' => '/two/' ),
41+
'wordpress.net/three/' => array( 'domain' => 'wordpress.net', 'path' => '/three/' ),
42+
);
43+
44+
foreach ( $ids as &$id ) {
45+
$id = $this->factory->network->create( $id );
46+
}
47+
unset( $id );
48+
49+
$this->assertEquals( $ids['www.wordpress.net/'],
50+
get_network_by_path( 'www.wordpress.net', '/notapath/' )->id );
51+
52+
$this->assertEquals( $ids['www.wordpress.net/two/'],
53+
get_network_by_path( 'www.wordpress.net', '/two/' )->id );
54+
55+
// This should find /one/ despite the www.
56+
$this->assertEquals( $ids['wordpress.org/one/'],
57+
get_network_by_path( 'www.wordpress.org', '/one/' )->id );
58+
59+
// This should not find /one/ because the domains don't match.
60+
$this->assertEquals( $ids['wordpress.org/'],
61+
get_network_by_path( 'site1.wordpress.org', '/one/' )->id );
62+
63+
$this->assertEquals( $ids['wordpress.net/three/'],
64+
get_network_by_path( 'wordpress.net', '/three/' )->id );
65+
66+
$this->assertEquals( $ids['wordpress.net/'],
67+
get_network_by_path( 'wordpress.net', '/notapath/' )->id );
68+
69+
$this->assertEquals( $ids['wordpress.net/'],
70+
get_network_by_path( 'site1.wordpress.net', '/notapath/' )->id );
71+
72+
$this->assertEquals( $ids['wordpress.net/'],
73+
get_network_by_path( 'site1.wordpress.net', '/three/' )->id );
74+
}
75+
76+
/**
77+
* @ticket 27003
78+
* @ticket 27927
79+
*/
80+
function test_get_site_by_path() {
81+
$ids = array(
82+
'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ),
83+
'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/' ),
84+
'wordpress.org/foo/bar/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/bar/' ),
85+
'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/' ),
86+
'make.wordpress.org/foo/' => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/' ),
87+
'www.w.org/' => array( 'domain' => 'www.w.org', 'path' => '/' ),
88+
'www.w.org/foo/' => array( 'domain' => 'www.w.org', 'path' => '/foo/' ),
89+
'www.w.org/foo/bar/' => array( 'domain' => 'www.w.org', 'path' => '/foo/bar/' ),
90+
);
91+
92+
foreach ( $ids as &$id ) {
93+
$id = $this->factory->blog->create( $id );
94+
}
95+
unset( $id );
96+
97+
$this->assertEquals( $ids['wordpress.org/'],
98+
get_site_by_path( 'wordpress.org', '/notapath/' )->blog_id );
99+
100+
$this->assertEquals( $ids['wordpress.org/'],
101+
get_site_by_path( 'www.wordpress.org', '/notapath/' )->blog_id );
102+
103+
$this->assertEquals( $ids['wordpress.org/foo/bar/'],
104+
get_site_by_path( 'wordpress.org', '/foo/bar/baz/' )->blog_id );
105+
106+
$this->assertEquals( $ids['wordpress.org/foo/bar/'],
107+
get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/' )->blog_id );
108+
109+
$this->assertEquals( $ids['wordpress.org/foo/bar/'],
110+
get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 3 )->blog_id );
111+
112+
$this->assertEquals( $ids['wordpress.org/foo/bar/'],
113+
get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/', 3 )->blog_id );
114+
115+
$this->assertEquals( $ids['wordpress.org/foo/bar/'],
116+
get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 2 )->blog_id );
117+
118+
$this->assertEquals( $ids['wordpress.org/foo/bar/'],
119+
get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/', 2 )->blog_id );
120+
121+
$this->assertEquals( $ids['wordpress.org/foo/'],
122+
get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 1 )->blog_id );
123+
124+
$this->assertEquals( $ids['wordpress.org/foo/'],
125+
get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/', 1 )->blog_id );
126+
127+
$this->assertEquals( $ids['wordpress.org/'],
128+
get_site_by_path( 'wordpress.org', '/', 0 )->blog_id );
129+
130+
$this->assertEquals( $ids['wordpress.org/'],
131+
get_site_by_path( 'www.wordpress.org', '/', 0 )->blog_id );
132+
133+
$this->assertEquals( $ids['make.wordpress.org/foo/'],
134+
get_site_by_path( 'make.wordpress.org', '/foo/bar/baz/qux/', 4 )->blog_id );
135+
136+
$this->assertEquals( $ids['make.wordpress.org/foo/'],
137+
get_site_by_path( 'www.make.wordpress.org', '/foo/bar/baz/qux/', 4 )->blog_id );
138+
139+
$this->assertEquals( $ids['www.w.org/'],
140+
get_site_by_path( 'www.w.org', '/', 0 )->blog_id );
141+
142+
$this->assertEquals( $ids['www.w.org/'],
143+
get_site_by_path( 'www.w.org', '/notapath/' )->blog_id );
144+
145+
$this->assertEquals( $ids['www.w.org/foo/bar/'],
146+
get_site_by_path( 'www.w.org', '/foo/bar/baz/' )->blog_id );
147+
148+
$this->assertEquals( $ids['www.w.org/foo/'],
149+
get_site_by_path( 'www.w.org', '/foo/bar/baz/', 1 )->blog_id );
150+
151+
// A site installed with www will not be found by the root domain.
152+
$this->assertFalse( get_site_by_path( 'w.org', '/' ) );
153+
$this->assertFalse( get_site_by_path( 'w.org', '/notapath/' ) );
154+
$this->assertFalse( get_site_by_path( 'w.org', '/foo/bar/baz/' ) );
155+
$this->assertFalse( get_site_by_path( 'w.org', '/foo/bar/baz/', 1 ) );
156+
157+
// A site will not be found by its root domain when an invalid subdomain is requested.
158+
$this->assertFalse( get_site_by_path( 'invalid.wordpress.org', '/' ) );
159+
$this->assertFalse( get_site_by_path( 'invalid.wordpress.org', '/foo/bar/' ) );
160+
}
161+
162+
/**
163+
* @ticket 27884
164+
*/
165+
function test_multisite_bootstrap() {
166+
global $current_blog;
167+
168+
$network_ids = array(
169+
'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ),
170+
'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/' ),
171+
);
172+
173+
foreach ( $network_ids as &$id ) {
174+
$id = $this->factory->network->create( $id );
175+
}
176+
unset( $id );
177+
178+
$ids = array(
179+
'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', 'site_id' => $network_ids['wordpress.org/'] ),
180+
'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => $network_ids['wordpress.org/'] ),
181+
'wordpress.org/foo/bar/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/bar/', 'site_id' => $network_ids['wordpress.org/'] ),
182+
'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/', 'site_id' => $network_ids['make.wordpress.org/'] ),
183+
'make.wordpress.org/foo/' => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/', 'site_id' => $network_ids['make.wordpress.org/'] ),
184+
);
185+
186+
foreach ( $ids as &$id ) {
187+
$id = $this->factory->blog->create( $id );
188+
}
189+
unset( $id );
190+
191+
$this->_setup_host_request( 'wordpress.org', '/' );
192+
$this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
193+
$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
194+
195+
$this->_setup_host_request( 'wordpress.org', '/2014/04/23/hello-world/' );
196+
$this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
197+
$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
198+
199+
$this->_setup_host_request( 'wordpress.org', '/sample-page/' );
200+
$this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
201+
$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
202+
203+
$this->_setup_host_request( 'wordpress.org', '/?p=1' );
204+
$this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
205+
$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
206+
207+
$this->_setup_host_request( 'wordpress.org', '/wp-admin/' );
208+
$this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
209+
$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
210+
211+
$this->_setup_host_request( 'wordpress.org', '/foo/' );
212+
$this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
213+
$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
214+
215+
$this->_setup_host_request( 'wordpress.org', '/FOO/' );
216+
$this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
217+
$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
218+
219+
$this->_setup_host_request( 'wordpress.org', '/foo/2014/04/23/hello-world/' );
220+
$this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
221+
$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
222+
223+
$this->_setup_host_request( 'wordpress.org', '/foo/sample-page/' );
224+
$this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
225+
$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
226+
227+
$this->_setup_host_request( 'wordpress.org', '/foo/?p=1' );
228+
$this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
229+
$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
230+
231+
$this->_setup_host_request( 'wordpress.org', '/foo/wp-admin/' );
232+
$this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
233+
$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
234+
235+
// @todo not currently passing.
236+
//$this->_setup_host_request( 'wordpress.org', '/foo/bar/' );
237+
//$this->assertEquals( $ids['wordpress.org/foo/bar/'], $current_blog->blog_id );
238+
//$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
239+
240+
$this->_setup_host_request( 'make.wordpress.org', '/' );
241+
$this->assertEquals( $ids['make.wordpress.org/'], $current_blog->blog_id );
242+
$this->assertEquals( $network_ids['make.wordpress.org/'], $current_blog->site_id );
243+
244+
$this->_setup_host_request( 'make.wordpress.org', '/foo/' );
245+
$this->assertEquals( $ids['make.wordpress.org/foo/'], $current_blog->blog_id );
246+
$this->assertEquals( $network_ids['make.wordpress.org/'], $current_blog->site_id );
247+
248+
// Request the original tests domain and path to unpollute the stack.
249+
$this->_setup_host_request( WP_TESTS_DOMAIN, '/' );
250+
}
251+
252+
/**
253+
* Reset various globals required for a 'clean' multisite boot.
254+
*
255+
* The $wpdb and $table_prefix globals are required for ms-settings.php to
256+
* load properly.
257+
*
258+
* @param string $domain HTTP_HOST of the bootstrap request.
259+
* @param string $path REQUEST_URI of the boot strap request.
260+
*/
261+
function _setup_host_request( $domain, $path ) {
262+
global $current_site, $current_blog, $table_prefix, $wpdb;
263+
264+
$table_prefix = WP_TESTS_TABLE_PREFIX;
265+
$current_site = $current_blog = null;
266+
$_SERVER['HTTP_HOST'] = $domain;
267+
$_SERVER['REQUEST_URI'] = $path;
268+
269+
include ABSPATH . '/wp-includes/ms-settings.php';
270+
}
271+
}
272+
273+
endif;

0 commit comments

Comments
 (0)