1 | <?php |
---|
2 | |
---|
3 | if ( is_multisite() ) : |
---|
4 | /** |
---|
5 | * Test wp_count_sites() in multisite. |
---|
6 | * |
---|
7 | * @group ms-site |
---|
8 | * @group multisite |
---|
9 | * @ticket 38071 |
---|
10 | */ |
---|
11 | class Tests_Multisite_Count_Sites extends WP_UnitTestCase { |
---|
12 | protected static $site_ids; |
---|
13 | protected static $expected; |
---|
14 | |
---|
15 | protected static $different_network_id; |
---|
16 | protected static $different_site_ids; |
---|
17 | protected static $different_expected; |
---|
18 | |
---|
19 | public static function wpSetUpBeforeClass( $factory ) { |
---|
20 | self::$site_ids = array( |
---|
21 | 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' , |
---|
22 | 'meta' => array( 'public' => 1, 'archived' => 0, 'mature' => 0, 'spam' => 0, 'deleted' => 0 ) ), |
---|
23 | 'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/' , |
---|
24 | 'meta' => array( 'public' => 0, 'archived' => 0, 'mature' => 0, 'spam' => 0, 'deleted' => 0 ) ), |
---|
25 | 'wordpress.org/foo/bar/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/bar/', |
---|
26 | 'meta' => array( 'public' => 1, 'archived' => 1, 'mature' => 1, 'spam' => 0, 'deleted' => 0 ) ), |
---|
27 | 'wordpress.org/bif/' => array( 'domain' => 'wordpress.org', 'path' => '/bif/' , |
---|
28 | 'meta' => array( 'public' => 1, 'archived' => 0, 'mature' => 1, 'spam' => 1, 'deleted' => 0 ) ), |
---|
29 | 'wordpress.org/baz/' => array( 'domain' => 'wordpress.org', 'path' => '/baz/' , |
---|
30 | 'meta' => array( 'public' => 1, 'archived' => 0, 'mature' => 0, 'spam' => 1, 'deleted' => 1 ) ), |
---|
31 | 'wordpress.org/boo/' => array( 'domain' => 'wordpress.org', 'path' => '/boo/' , |
---|
32 | 'meta' => array( 'public' => 1, 'archived' => 1, 'mature' => 1, 'spam' => 1, 'deleted' => 1 ) ), |
---|
33 | ); |
---|
34 | |
---|
35 | foreach ( self::$site_ids as &$id ) { |
---|
36 | $id = $factory->blog->create( $id ); |
---|
37 | } |
---|
38 | unset( $id ); |
---|
39 | |
---|
40 | self::$expected = (object) array ( |
---|
41 | 'public' => 6, // +1 because of example.org/ site |
---|
42 | 'archived' => 2, |
---|
43 | 'mature' => 3, |
---|
44 | 'spam' => 3, |
---|
45 | 'deleted' => 2, |
---|
46 | ); |
---|
47 | |
---|
48 | self::$different_network_id = $factory->network->create( array( 'domain' => 'diff_wordpress.org', 'path' => '/' ) ); |
---|
49 | |
---|
50 | self::$different_site_ids = array( |
---|
51 | 'diff.wordpress.org/' => array( 'domain' => 'diff.wordpress.org', 'path' => '/' , 'site_id' => self::$different_network_id, |
---|
52 | 'meta' => array( 'public' => 1, 'archived' => 0, 'mature' => 0, 'spam' => 0, 'deleted' => 0 ) ), |
---|
53 | 'diff.wordpress.org/foo/' => array( 'domain' => 'diff.wordpress.org', 'path' => '/foo/' , 'site_id' => self::$different_network_id, |
---|
54 | 'meta' => array( 'public' => 1, 'archived' => 1, 'mature' => 0, 'spam' => 0, 'deleted' => 0 ) ), |
---|
55 | 'diff.wordpress.org/foo/bar/' => array( 'domain' => 'diff.wordpress.org', 'path' => '/foo/bar/', 'site_id' => self::$different_network_id, |
---|
56 | 'meta' => array( 'public' => 0, 'archived' => 0, 'mature' => 1, 'spam' => 0, 'deleted' => 0 ) ), |
---|
57 | 'diff.wordpress.org/bif/' => array( 'domain' => 'diff.wordpress.org', 'path' => '/bif/' , 'site_id' => self::$different_network_id, |
---|
58 | 'meta' => array( 'public' => 1, 'archived' => 0, 'mature' => 1, 'spam' => 0, 'deleted' => 1 ) ), |
---|
59 | 'diff.wordpress.org/baz/' => array( 'domain' => 'diff.wordpress.org', 'path' => '/baz/' , 'site_id' => self::$different_network_id, |
---|
60 | 'meta' => array( 'public' => 1, 'archived' => 0, 'mature' => 0, 'spam' => 1, 'deleted' => 1 ) ), |
---|
61 | 'diff.wordpress.org/boo/' => array( 'domain' => 'diff.wordpress.org', 'path' => '/boo/' , 'site_id' => self::$different_network_id, |
---|
62 | 'meta' => array( 'public' => 1, 'archived' => 1, 'mature' => 0, 'spam' => 0, 'deleted' => 0 ) ), |
---|
63 | ); |
---|
64 | |
---|
65 | foreach ( self::$different_site_ids as &$id ) { |
---|
66 | $id = $factory->blog->create( $id ); |
---|
67 | } |
---|
68 | unset( $id ); |
---|
69 | |
---|
70 | self::$different_expected = (object) array ( |
---|
71 | 'public' => 5, |
---|
72 | 'archived' => 2, |
---|
73 | 'mature' => 2, |
---|
74 | 'spam' => 1, |
---|
75 | 'deleted' => 2, |
---|
76 | ); |
---|
77 | } |
---|
78 | |
---|
79 | public static function wpTearDownAfterClass() { |
---|
80 | // just in case |
---|
81 | self::switch_network( 1 ); |
---|
82 | |
---|
83 | foreach( self::$site_ids as $id ) { |
---|
84 | wpmu_delete_blog( $id, true ); |
---|
85 | } |
---|
86 | |
---|
87 | foreach( self::$different_site_ids as $id ) { |
---|
88 | wpmu_delete_blog( $id, true ); |
---|
89 | } |
---|
90 | |
---|
91 | self::delete_network ( self::$different_network_id ); |
---|
92 | |
---|
93 | wp_update_network_site_counts(); |
---|
94 | } |
---|
95 | |
---|
96 | public function test_count_sites() { |
---|
97 | $counts = wp_count_sites(); |
---|
98 | |
---|
99 | $this->assertEquals( self::$expected, $counts ); |
---|
100 | } |
---|
101 | |
---|
102 | public function test_filtered_count_sites() { |
---|
103 | add_filter( 'wp_count_sites', array( __CLASS__, 'modify_counts' ), 10, 2 ); |
---|
104 | |
---|
105 | $counts = wp_count_sites(); |
---|
106 | |
---|
107 | $this->assertEquals( self::modify_counts( self::$expected, get_current_network_id() ), $counts ); |
---|
108 | } |
---|
109 | |
---|
110 | public function test_different_network_count_sites() { |
---|
111 | $counts = wp_count_sites( self::$different_network_id ); |
---|
112 | |
---|
113 | $this->assertEquals( self::$different_expected, $counts ); |
---|
114 | } |
---|
115 | |
---|
116 | public function test_filtered_different_network_count_sites() { |
---|
117 | add_filter( 'wp_count_sites', array( __CLASS__, 'modify_counts' ), 10, 2 ); |
---|
118 | |
---|
119 | $counts = wp_count_sites( self::$different_network_id ); |
---|
120 | |
---|
121 | $this->assertEquals( self::modify_counts( self::$different_expected, self::$different_network_id ), $counts ); |
---|
122 | } |
---|
123 | |
---|
124 | public function test_switched_network_count_sites() { |
---|
125 | self::switch_network( self::$different_network_id ); |
---|
126 | |
---|
127 | $counts = wp_count_sites(); |
---|
128 | |
---|
129 | $this->assertEquals( self::$different_expected, $counts ); |
---|
130 | |
---|
131 | self::switch_network( 1 ); |
---|
132 | } |
---|
133 | |
---|
134 | public function test_filtered_switched_network_count_sites() { |
---|
135 | self::switch_network( self::$different_network_id ); |
---|
136 | |
---|
137 | add_filter( 'wp_count_sites', array( __CLASS__, 'modify_counts' ), 10, 2 ); |
---|
138 | |
---|
139 | $counts = wp_count_sites(); |
---|
140 | |
---|
141 | $this->assertEquals( self::modify_counts( self::$different_expected, self::$different_network_id ), $counts ); |
---|
142 | |
---|
143 | self::switch_network( 1 ); |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Filter the site counts |
---|
148 | * |
---|
149 | * if counts are for the default network then subtract 1 from all counts |
---|
150 | * if counts are for a different network then add 1 to all counts |
---|
151 | * |
---|
152 | * @param array $counts |
---|
153 | * @param int $network_id |
---|
154 | * @return array |
---|
155 | * |
---|
156 | * @filter wp_count_sites |
---|
157 | */ |
---|
158 | public static function modify_counts( $counts, $network_id ) { |
---|
159 | $diff = ( 1 === $network_id ? -1 : +1 ); |
---|
160 | |
---|
161 | $mod_counts = clone $counts ; |
---|
162 | foreach ( get_object_vars( $counts ) as $status => $count ) { |
---|
163 | $mod_counts->{$status} = max ( $count + $diff, 0 ); |
---|
164 | } |
---|
165 | |
---|
166 | return $mod_counts; |
---|
167 | } |
---|
168 | |
---|
169 | /* |
---|
170 | * I've never worked with multi-network setups before and was shocked |
---|
171 | * to find that there is no real API support in core for multi-networks |
---|
172 | * (@link https://core.trac.wordpress.org/ticket/29411). |
---|
173 | * |
---|
174 | * The functions below appear to be the BARE MINIMUM multi-network support |
---|
175 | * necessary for the tests in this file to run correctly and leave the |
---|
176 | * environment "clean" after they run. |
---|
177 | */ |
---|
178 | |
---|
179 | /** |
---|
180 | * minimalist network switching |
---|
181 | * |
---|
182 | * @param int $network_id |
---|
183 | */ |
---|
184 | public static function switch_network( $network_id ) { |
---|
185 | global $current_site, $wpdb; |
---|
186 | |
---|
187 | // so that get_current_network_id() will do the right thing |
---|
188 | $current_site->id = $network_id; |
---|
189 | |
---|
190 | // so that wp_update_network_site_counts() will do the right thing |
---|
191 | $wpdb->siteid = $network_id; |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | * minimalist network deletion |
---|
196 | * |
---|
197 | * @param int $network_id |
---|
198 | */ |
---|
199 | public static function delete_network( $network_id ) { |
---|
200 | global $wpdb; |
---|
201 | |
---|
202 | $sql = "DELETE FROM {$wpdb->site} WHERE id = %d"; |
---|
203 | $sql = $wpdb->prepare( $sql, $network_id ); |
---|
204 | $wpdb->query( $sql ); |
---|
205 | |
---|
206 | $sql = "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d"; |
---|
207 | $sql = $wpdb->prepare( $sql, $network_id ); |
---|
208 | $wpdb->query( $sql ); |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | endif; |
---|