PluginProbe
Groups – Memberships and Access Control / 1.1.4
Groups – Memberships and Access Control v1.1.4
4.7.1 4.7.0 4.6.0 4.5.0 4.4.0 4.3.0 trunk 1.0.0-beta-1 1.0.0-beta-2 1.0.0-beta-3 1.0.0-beta-3b 1.0.0-beta-3c 1.0.0-beta-3d 1.1.4 1.1.5 1.10.0 1.10.1 1.10.2 1.10.3 1.11.0 1.11.1 1.11.2 1.11.3 1.12.0 1.13.0 All 131 releases
groups / lib / core / groups-tests.php

groups-tests.php in Groups – Memberships and Access Control 1.1.4, at lib/core/groups-tests.php

420 lines 16.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * groups-tests.php
4 *
5 * Copyright (c) "kento" Karim Rahimpur www.itthinx.com
6 *
7 * This code is released under the GNU General Public License.
8 * See COPYRIGHT.txt and LICENSE.txt.
9 *
10 * This code is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * This header and all notices must be kept intact.
16 *
17 * @author Karim Rahimpur
18 * @package groups
19 * @since groups 1.0.0
20 */
21 // bootstrap WordPress
22 if ( !defined( 'ABSPATH' ) ) {
23 $wp_load = 'wp-load.php';
24 $max_depth = 100; // prevent death by depth
25 while ( !file_exists( $wp_load ) && ( $max_depth > 0 ) ) {
26 $wp_load = '../' . $wp_load;
27 $max_depth--;
28 }
29 if ( file_exists( $wp_load ) ) {
30 require_once $wp_load;
31 }
32 }
33 if ( defined( 'ABSPATH' ) ) {
34
35 function groups_tests() {
36
37 assert_options( ASSERT_ACTIVE, true );
38 assert_options( ASSERT_WARNING, true );
39 assert_options( ASSERT_BAIL, false );
40
41 //
42 // PART 1 : create test data
43 //
44
45 // *** groups ***
46
47 // create valid test groups
48 //
49 // Fruits [dance] {foo}
50 // / \
51 // Sweet {dol} Sour [sing]
52 // | |
53 // Banana {baz} Lemon {bar}
54 //
55 // All groups can dance.
56 // Only Sour and Lemon can sing.
57 $fruits_group_id = Groups_Group::create( array( 'name' => 'Fruits' ) );
58 assert( '$fruits_group_id !== false' );
59 $sweet_group_id = Groups_Group::create( array( 'name' => 'Sweet', 'parent_id' => $fruits_group_id ) );
60 assert( '$sweet_group_id !== false' );
61 $sour_group_id = Groups_Group::create( array( 'name' => 'Sour', 'parent_id' => $fruits_group_id ) );
62 assert( '$sour_group_id !== false' );
63 $lemon_group_id = Groups_Group::create( array( 'name' => 'Lemon', 'parent_id' => $sour_group_id ) );
64 assert( '$lemon_group_id !== false' );
65 $banana_group_id = Groups_Group::create( array( 'name' => 'Banana', 'parent_id' => $sweet_group_id ) );
66 assert( '$banana_group_id !== false' );
67
68 // fail to create group with missing name
69 $bogus_group_id = Groups_Group::create( array( ) );
70 assert( '$bogus_group_id === false; /* empty name */' );
71
72 // fail to create group with wrong parent_id
73 $bogus_group_id = Groups_Group::create( array( 'name' => 'bogus', 'parent_id' => -1 ) );
74 assert( '$bogus_group_id === false; /* wrong parent_id */' );
75
76 // *** capabilities ***
77 $sing_capability_id = Groups_Capability::create( array( 'capability' => 'sing' ) );
78 assert( '$sing_capability_id !== false' );
79
80 $dance_capability_id = Groups_Capability::create( array( 'capability' => 'dance' ) );
81 assert( '$dance_capability_id !== false' );
82
83 $clap_capability_id = Groups_Capability::create( array( 'capability' => 'clap' ) );
84 assert( '$clap_capability_id !== false' );
85
86 // read capability by id
87 assert( 'Groups_Capability::read( $sing_capability_id )' );
88
89 // read capability by unique label
90 assert( 'Groups_Capability::read_by_capability( "dance" )' );
91
92
93 // *** users ***
94
95 // create test users
96 $fooname = 'foo' . rand(0, 100);
97 $foo_user_id = wp_create_user( $fooname, 'foo', $fooname . '@example.com' );
98 assert( '$foo_user_id instanceof WP_Error === false');
99
100 $barname = 'bar' . rand(0, 100);
101 $bar_user_id = wp_create_user( $barname, 'bar', $barname . '@example.com' );
102 assert( '$bar_user_id instanceof WP_Error === false');
103
104 // this user is used to test the automatic resolution of its relationship
105 // with the banana group when the group is deleted
106 // it's also used to test automatic resolution of its "clap" capability
107 // after that capability has been deleted
108 $bazname = 'baz' . rand(0, 100);
109 $baz_user_id = wp_create_user( $bazname, 'baz', $bazname . '@example.com' );
110 assert( '$baz_user_id instanceof WP_Error === false');
111
112 // this user is deleted, the group relation must be deleted automatically
113 $dolname = 'dol' . rand(0, 100);
114 $dol_user_id = wp_create_user( $dolname, 'dol', $dolname . ' @example.com' );
115 assert( '$dol_user_id instanceof WP_Error === false');
116
117 // this user is a simple editor, used to test WordPress capabilities
118 $editorname = 'rotide' . rand(0, 100);
119 $editor_user_id = wp_create_user( $editorname, 'rotide', $editorname . '@example.com' );
120 assert( '$editor_user_id instanceof WP_Error === false');
121 if ( !( $editor_user_id instanceof WP_Error ) ) {
122 $editor_user = new WP_User( $editor_user_id );
123 $editor_user->set_role( 'editor' );
124 } else {
125 $editor_user_id = false;
126 }
127
128 // *** users & groups ***
129
130 // add valid users to groups
131 // echo "foo user id: $foo_user_id group: $fruits_group_id<br/>";
132 assert( 'Groups_User_Group::create(array( "user_id" => $foo_user_id, "group_id" => $fruits_group_id ) )' );
133 assert( 'Groups_User_Group::create(array( "user_id" => $bar_user_id, "group_id" => $lemon_group_id ) )' );
134 assert( 'Groups_User_Group::create(array( "user_id" => $baz_user_id, "group_id" => $banana_group_id ) )' );
135 assert( 'Groups_User_Group::create(array( "user_id" => $dol_user_id, "group_id" => $sweet_group_id ) )' );
136
137 // add invalid user to group
138 assert( 'Groups_User_Group::create(array( "user_id" => -1, "group_id" => $sweet_group_id ) ) === false' );
139
140 // add valid user to invalid group
141 assert( 'Groups_User_Group::create(array( "user_id" => $dol_user_id, "group_id" => -1 ) ) === false' );
142
143 // define capabilities for groups
144 assert( 'Groups_Group_Capability::create( array( "group_id" => $fruits_group_id, "capability_id" => $dance_capability_id ) )' );
145 assert( 'Groups_Group_Capability::create( array( "group_id" => $sour_group_id, "capability_id" => $sing_capability_id ) )' );
146
147 // define capabilities for users
148 assert( 'Groups_User_Capability::create( array( "user_id" => $foo_user_id, "capability_id" => $clap_capability_id ) )' );
149 assert( 'Groups_User_Capability::create( array( "user_id" => $baz_user_id, "capability_id" => $clap_capability_id ) )' );
150
151 // check groups that can dance (all)
152 // just reading will not check the hierarchy of course ...
153 assert( 'Groups_Group_Capability::read( $fruits_group_id, $dance_capability_id )' );
154 assert( 'Groups_Group_Capability::read( $sweet_group_id, $dance_capability_id ) === false' );
155 assert( 'Groups_Group_Capability::read( $banana_group_id, $dance_capability_id ) === false' );
156 assert( 'Groups_Group_Capability::read( $sour_group_id, $dance_capability_id ) === false' );
157 assert( 'Groups_Group_Capability::read( $lemon_group_id, $dance_capability_id ) === false' );
158 // same for check on groups that can sing
159 assert( 'Groups_Group_Capability::read( $fruits_group_id, $sing_capability_id ) === false' );
160 assert( 'Groups_Group_Capability::read( $sweet_group_id, $sing_capability_id ) === false' );
161 assert( 'Groups_Group_Capability::read( $banana_group_id, $sing_capability_id ) === false' );
162 assert( 'Groups_Group_Capability::read( $sour_group_id, $sing_capability_id )' );
163 assert( 'Groups_Group_Capability::read( $lemon_group_id, $sing_capability_id ) === false' );
164
165 // hierarchical groups
166 $fruits_group = new Groups_Group( $fruits_group_id );
167 $sweet_group = new Groups_Group( $sweet_group_id );
168 $banana_group = new Groups_Group( $banana_group_id );
169 $sour_group = new Groups_Group( $sour_group_id );
170 $lemon_group = new Groups_Group( $lemon_group_id );
171
172 // all should be able to "dance" : check by capability label ...
173 assert( '$fruits_group->can( "dance" )' );
174 assert( '$sweet_group->can( "dance" )' );
175 assert( '$banana_group->can( "dance" )' );
176 assert( '$sour_group->can( "dance" )' );
177 assert( '$lemon_group->can( "dance" )' );
178 // ... or id
179 assert( '$fruits_group->can( $dance_capability_id )' );
180 assert( '$sweet_group->can( $dance_capability_id )' );
181 assert( '$banana_group->can( $dance_capability_id )' );
182 assert( '$sour_group->can( $dance_capability_id )' );
183 assert( '$lemon_group->can( $dance_capability_id )' );
184
185 // only sour and lemon can sing:
186 assert( '!$fruits_group->can( "sing" )' );
187 assert( '!$sweet_group->can( "sing" )' );
188 assert( '!$banana_group->can( "sing" )' );
189 assert( '$sour_group->can( "sing" )' );
190 assert( '$lemon_group->can( "sing" )' );
191 // ... or id
192 assert( '!$fruits_group->can( $sing_capability_id )' );
193 assert( '!$sweet_group->can( $sing_capability_id )' );
194 assert( '!$banana_group->can( $sing_capability_id )' );
195 assert( '$sour_group->can( $sing_capability_id )' );
196 assert( '$lemon_group->can( $sing_capability_id )' );
197
198 // no group can clap
199 assert( '!$fruits_group->can( $clap_capability_id )' );
200 assert( '!$sweet_group->can( $clap_capability_id )' );
201 assert( '!$banana_group->can( $clap_capability_id )' );
202 assert( '!$sour_group->can( $clap_capability_id )' );
203 assert( '!$lemon_group->can( $clap_capability_id )' );
204
205 // user capabilities
206 $foo = new Groups_User( $foo_user_id );
207 $dol = new Groups_User( $dol_user_id );
208 $baz = new Groups_User( $baz_user_id );
209 $bar = new Groups_User( $bar_user_id );
210
211 assert( '$foo->can( "dance" )' );
212 assert( '$dol->can( "dance" )' );
213 assert( '$baz->can( "dance" )' );
214 assert( '$bar->can( "dance" )' );
215
216 assert( '$foo->can( $dance_capability_id )' );
217 assert( '$dol->can( $dance_capability_id )' );
218 assert( '$baz->can( $dance_capability_id )' );
219 assert( '$bar->can( $dance_capability_id )' );
220
221 assert( '!$foo->can( "sing" )' );
222 assert( '!$dol->can( "sing" )' );
223 assert( '!$baz->can( "sing" )' );
224 assert( '$bar->can( "sing" )' );
225
226 assert( '!$foo->can( $sing_capability_id )' );
227 assert( '!$dol->can( $sing_capability_id )' );
228 assert( '!$baz->can( $sing_capability_id )' );
229 assert( '$bar->can( $sing_capability_id )' );
230
231 // only foo & baz can clap
232 assert( '$foo->can( "clap" )' );
233 assert( '!$dol->can( "clap" )' );
234 assert( '$baz->can( "clap" )' );
235 assert( '!$bar->can( "clap" )' );
236
237 assert( '$foo->can( $clap_capability_id )' );
238 assert( '!$dol->can( $clap_capability_id )' );
239 assert( '$baz->can( $clap_capability_id )' );
240 assert( '!$bar->can( $clap_capability_id )' );
241
242 // user can not what is not defined
243 assert( '!$foo->can( null )' );
244 assert( '!$dol->can( null )' );
245 assert( '!$baz->can( null )' );
246 assert( '!$bar->can( null )' );
247
248 assert( '!$foo->can( "bogus" )' );
249 assert( '!$dol->can( "bogus" )' );
250 assert( '!$baz->can( "bogus" )' );
251 assert( '!$bar->can( "bogus" )' );
252
253 // groups can not what is not defined
254 assert( '!$fruits_group->can( null )' );
255 assert( '!$sweet_group->can( null )' );
256 assert( '!$banana_group->can( null )' );
257 assert( '!$sour_group->can( null )' );
258 assert( '!$lemon_group->can( null )' );
259
260 assert( '!$fruits_group->can( "bogus" )' );
261 assert( '!$sweet_group->can( "bogus" )' );
262 assert( '!$banana_group->can( "bogus" )' );
263 assert( '!$sour_group->can( "bogus" )' );
264 assert( '!$lemon_group->can( "bogus" )' );
265
266 // test WordPress capabilities
267 $administrator = new Groups_User( 1 );
268 assert( '$administrator->can( "activate_plugins" )' );
269
270 if ( $editor_user_id ) {
271 $editor = new Groups_User( $editor_user_id );
272 assert( '$editor->can( "edit_posts" )' );
273 assert( '!$editor->can( "activate_plugins" )' );
274 }
275
276 if ( is_multisite() ) {
277 // $randext = rand( 0, 100 );
278 // $wpmu_test_user_id = wp_create_user( 'wpmu_test_user' . $randext, 'wpmu_test_user' );
279 // assert( '$wpmu_test_user_id instanceof WP_Error === false');
280 // @todo create a blog => must create new tables
281 // wpmu_create_blog( "groups_wpmu_" . $randext, "groups_wpmu_" . $randext, "Groups WPMU Test", $wpmu_test_user_id );
282 // @todo add user to new blog
283 // @todo switch to new blog
284 // @todo check that new user is in "Registered" group
285 // @todo switch to current blog
286 }
287
288 //
289 // PART 2 : delete test data
290 //
291
292 if ( is_multisite() ) {
293 // @todo delete new blog
294 }
295
296 // remove capabilities from groups
297 assert( 'Groups_Group_Capability::delete( $fruits_group_id, $dance_capability_id )' );
298
299 // remove users from groups
300 assert( 'Groups_User_Group::delete($foo_user_id, $fruits_group_id)' );
301 assert( 'Groups_User_Group::delete($bar_user_id, $lemon_group_id)' );
302 // baz must be deleted from user_group when banana group is deleted
303
304 // invalid remove user from group
305 assert( 'Groups_User_Group::delete($foo_user_id, $banana_group_id) === false' );
306
307 // delete test users
308 include_once( ABSPATH . '/wp-admin/includes/user.php' );
309 if ( $foo_user_id && !( $foo_user_id instanceof WP_Error ) ) {
310 assert( 'wp_delete_user( $foo_user_id ) === true' );
311 }
312 if ( $bar_user_id && !( $bar_user_id instanceof WP_Error ) ) {
313 assert( 'wp_delete_user( $bar_user_id ) === true' );
314 }
315 if ( $dol_user_id && !( $dol_user_id instanceof WP_Error ) ) {
316 assert( 'wp_delete_user( $dol_user_id ) === true' );
317 if ( $sweet_group_id ) {
318 // see above, this user must have been removed from the group upon its deletion
319 assert( 'Groups_User_Group::read( $dol_user_id, $sweet_group_id ) === false' );
320 }
321 }
322 if ( $editor_user_id && !( $editor_user_id instanceof WP_Error ) ) {
323 assert( 'wp_delete_user( $editor_user_id ) === true' );
324 }
325 // fail to delete inexisting capabilities
326 assert( 'Groups_Capability::delete( -1 ) === false' );
327
328 // delete valid test capabilities
329 if ( $sing_capability_id ) {
330 assert( 'Groups_Capability::delete( $sing_capability_id ) !== false' );
331 }
332 if ( $dance_capability_id ) {
333 assert( 'Groups_Capability::delete( $dance_capability_id ) !== false' );
334 }
335 if ( $clap_capability_id ) {
336 assert( 'Groups_Capability::delete( $clap_capability_id ) !== false' );
337 }
338 // baz shouldn't be able to clap if there's no clapping capability anymore
339 if ( $baz_user_id && !( $baz_user_id instanceof WP_Error ) ) {
340 assert( '!$baz->can( "clap" )' );
341 }
342
343 // fail to delete inexisting group
344 assert( 'Groups_Group::delete( -1 ) === false' );
345
346 // delete invalid test group if creation was successful
347 if ( $bogus_group_id ) {
348 assert( 'Groups_Group::delete( $bogus_group_id )' );
349 }
350
351 // delete valid test groups
352 if ( $fruits_group_id ) {
353 assert( 'Groups_Group::delete( $fruits_group_id )' );
354 }
355 if ( $sweet_group_id ) {
356 assert( 'Groups_Group::delete( $sweet_group_id )' );
357 }
358 if ( $sour_group_id ) {
359 assert( 'Groups_Group::delete( $sour_group_id )' );
360 }
361 if ( $lemon_group_id ) {
362 assert( 'Groups_Group::delete( $lemon_group_id )');
363 }
364 if ( $banana_group_id ) {
365 assert( 'Groups_Group::delete( $banana_group_id )' );
366 assert( 'Groups_User_Group::delete($baz_user_id, $banana_group_id ) === false' );
367 }
368 // this user must not be deleted before as it is used to test that its
369 // relationship with the banana group is resolved when the group is deleted
370 if ( $baz_user_id && !( $baz_user_id instanceof WP_Error ) ) {
371 assert( 'wp_delete_user( $baz_user_id ) === true' );
372 }
373 }
374
375 $active_plugins = get_option( 'active_plugins', array() );
376 $active_sitewide_plugins = array();
377 if ( is_multisite() ) {
378 $active_sitewide_plugins = get_site_option( 'active_sitewide_plugins', array() );
379 }
380 if ( in_array( 'groups/groups.php', $active_plugins ) || key_exists( 'groups/groups.php', $active_sitewide_plugins ) ) {
381 if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
382 wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) );
383 } else {
384 $run = isset( $_POST['run'] ) ? $_POST['run'] : null;
385 switch( $run ) {
386 case 'run' :
387 if ( !isset( $_POST['groups-test-nonce'] ) || !wp_verify_nonce( $_POST['groups-test-nonce'], 'run-tests' ) ) {
388 wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) );
389 }
390 echo '<h1>Running tests for <i>Groups</i> plugin ...</h1>';
391 groups_tests();
392 echo '<h2>Finished.</h2>';
393 break;
394 default :
395 $url = get_bloginfo( 'url' );
396 echo '<p style="color:#f00; font-weight:bold;">';
397 echo 'DO NOT CONTINUE UNLESS YOU KNOW WHAT YOU ARE DOING.';
398 echo '</p>';
399 echo '<ul>';
400 echo '<li>This will run tests for the Groups plugin on this site.</li>';
401 echo '<li>Unless you are a developer who knows what she or he is doing, you do not need to do this and you do not want to proceed.</li>';
402 echo '<li>It may <strong>completely destroy your site</strong>.</li>';
403 echo '<li>Run only at your own risk, do not blame anyone if something goes wrong.</li>';
404 echo '<li>You <strong>agree to be solely responsible for any damage</strong> this may cause to the site.';
405 echo '<li>Make a full backup of your site and database before you continue.</li>';
406 echo '<li>If in doubt, <strong><a href="' . $url . '">do not continue</a></strong>.</li>';
407 echo '</ul>';
408 echo '<form action="" method="post">';
409 echo '<input name="run" value="run" type="hidden" />';
410 echo '<input type="submit" value="Go" />';
411 wp_nonce_field( 'run-tests', 'groups-test-nonce', true, true );
412 echo '</form>';
413 }
414
415 }
416 } else {
417 echo 'The <i>Groups</i> plugin is not active, not running tests.';
418 }
419
420 } // ABSPATH defined