/[drupal]/contributions/modules/autoassignrole/tests/autoassignrole.test
ViewVC logotype

Diff of /contributions/modules/autoassignrole/tests/autoassignrole.test

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1 by cyberswat, Mon May 11 21:25:08 2009 UTC revision 1.2 by cyberswat, Fri Sep 25 16:08:43 2009 UTC
# Line 0  Line 1 
1    <?php
2    // $Id: autoassignrole.test,v 1.1.2.8 2009/08/19 18:28:34 cyberswat Exp $
3    
4    /**
5     * @file
6     * Autoassignrole functionality tests.
7     */
8    class AAR extends DrupalWebTestCase {
9      function setUp() {
10        parent::setUp('autoassignrole');
11       // create the test roles
12        $this->roles = $this->createRoles(3);
13        $this->admin_user = $this->drupalCreateUser(array('access administration pages', 'administer autoassignrole'));
14      }
15    
16      /**
17       * Helper function to create the roles used for testing
18       */
19      function createRoles($count) {
20        $roles = array();
21        $x = 0;
22        while ($x < $count) {
23         $role = db_fetch_object(db_query("SELECT * FROM {role} WHERE rid = '%d'", $this->_drupalCreateRole()));
24         $roles[$role->rid] = $role->name;
25         $x++;
26        }
27        return $roles;
28      }
29    
30      /**
31       * Helper function that compares the edit array to AAR settings
32       */
33      function validate($edit, $title) {
34        foreach ($edit as $key => $value) {
35         $this->assertEqual($value, _autoassignrole_get_settings($key), $title. ': '. $key .' was set properly to ' .$value);
36        }
37      }
38    
39      /**
40       * Helper function that compares the edit array to AAR settings
41       */
42      function getEditValues($key) {
43       $edit = array();
44       switch ($key) {
45        case 'automatic_role_assignment':
46         $edit = array(
47           'auto_active' => 1,
48         );
49         foreach ($this->roles as $rid => $role) {
50          $edit["auto_roles[$rid]"] = TRUE;
51         }
52         break;
53        case 'assign_from_path':
54         // path and title are required for enabling Assign From Path
55         foreach ($this->roles as $rid => $role) {
56          $edit["path_active_$rid"] = 1;
57          $edit["path_weight_$rid"] = -10;
58          $edit["path_$rid"] = $this->randomName();
59          $edit["path_title_$rid"] = $this->randomName();
60          $edit["path_description_$rid"] = $this->randomName(100);
61          $edit["path_display_$rid"] = 1;
62         }
63        break;
64        case 'user_choice':
65         $edit['user_active'] = 1;
66         $edit['user_multiple'] = 1;
67         $edit['user_selection'] = 1;
68         $edit['user_required'] = 0;
69         $edit['user_sort'] = 'SORT_ASC';
70         $edit['user_fieldset_title'] = $this->randomName();
71         $edit['user_title'] = $this->randomName();
72         $edit['user_description'] = $this->randomName(100);
73         foreach ($this->roles as $rid => $role) {
74          $edit["user_roles[$rid]"] = TRUE;
75         }
76         break;
77       }
78       return $edit;
79      }
80    
81      /**
82       * Helper function for new users
83       */
84      function getNewUser() {
85       $new_user = array();
86       $new_user['name']   = $this->randomName();
87       $new_user['mail']   = $new_user['name'] . '@example.com';
88    
89       return $new_user;
90      }
91    
92      /**
93       * Helper function to determine sort order
94       * @param array $data An array of values to check
95       * @return string ASC, DESC or MIXED
96       */
97      function getSortOrder($data) {
98        $asc = array();
99        $desc = array();
100        foreach ($data as $key => $option) {
101          if ($option) {
102            if (!$previous) {
103              $previous = $option;
104            }
105            else {
106              if ($previous < $option) {
107                if (count($asc) == 0) {
108                  $asc[] = $previous;
109                }
110                $previous = $option;
111                $asc[] = $option;
112              }
113              else {
114                if (count($desc) == 0) {
115                  $desc[] = $previous;
116                }
117                $previous = $option;
118                $desc[] = $option;
119              }
120            }
121          }
122          else {
123            unset($data[$key]);
124          }
125        }
126        if (count($data) == count($asc)) {
127          return "ASC";
128        }
129        if (count($data) == count($desc)) {
130          return "DESC";
131        }
132        return "MIXED";
133      }
134    
135    }
136    
137    
138    class AutoassignroleAdminAutoRoleTestCase extends AAR {
139      public static function getInfo() {
140        return array(
141          'name' => t('Automatic Role Settings'),
142          'description' => t('Verify Automatic Role Assignment admin settings are stored as expected.'),
143          'group' => t('Autoassignrole'),
144        );
145      }
146    
147      function testAdminSettings() {
148        // Create a new user who can access the administration settings
149        $this->drupalLogin($this->admin_user);
150    
151        // Check that the user can see the admin settings page.
152        $this->drupalGet('admin/user/autoassignrole');
153        $this->assertRaw('Automatic Role Assignment', 'The Automatic Role Assignment section exists on the AAR admin page');
154    
155        // Enable "Automatic role assignment" and enable all test roles
156        $edit = $this->getEditValues('automatic_role_assignment');
157        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
158    
159        // Verify Automatic Role Assignment has been enabled
160        $this->assertEqual(_autoassignrole_get_settings('auto_active'), 1, 'Automatic Role Assignment has been enabled');
161    
162        $this->validate($edit, 'Automatic Role Assignment');
163    
164        // Disable "Automatic role assignment" and disable all test roles
165        $edit['auto_active'] = 0;
166        foreach ($this->roles as $rid => $role) {
167         $edit["auto_roles[$rid]"] = FALSE;
168        }
169    
170        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
171    
172        $this->validate($edit, 'Automatic Role Assignment');
173      }
174    }
175    
176    class AutoassignroleAdminAssignFromPathTestCase extends AAR {
177      public static function getInfo() {
178        return array(
179          'name' => t('Assign From Path Settings'),
180          'description' => t('Verify Assign From Path admin settings are stored as expected.'),
181          'group' => t('Autoassignrole'),
182        );
183      }
184    
185      function testAdminSettings() {
186        // Create a new user who can access the administration settings
187        $this->drupalLogin($this->admin_user);
188    
189        // Check that the user can see the admin settings page.
190        $this->drupalGet('admin/user/autoassignrole');
191        $this->assertRaw('Automatic Role Assignment', 'The Automatic Role Assignment section exists on the AAR admin page');
192    
193        // Check that each role has the correct fields in existence
194        foreach ($this->roles as $rid => $role) {
195         $this->assertField("path_active_$rid", "Assign from Path: $role has an Enable/Disable option", $group);
196         $this->assertField("path_display_$rid", "Assign from Path: $role has a Display Method option", $group);
197         $this->assertField("path_weight_$rid", "Assign from Path: $role has a Weight option", $group);
198         $this->assertField("path_$rid", "Assign from Path: $role has a Path option", $group);
199         $this->assertField("path_title_$rid", "Assign from Path: $role has a Path Title option", $group);
200         $this->assertField("path_description_$rid", "Assign from Path: $role has a Path Description option", $group);
201        }
202    
203        $edit = $this->getEditValues('assign_from_path');
204        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
205    
206        // verify that our changes were saved
207        foreach ($edit as $key => $value) {
208         $this->assertEqual($value, _autoassignrole_get_settings($key), 'Assign From Path: '. $key .' was set properly');
209        }
210    
211        // verify that a title is only required when a menu based choice is selected
212        foreach ($this->roles as $rid => $role) {
213         $edit["path_title_$rid"] = '';
214         $edit["path_display_$rid"] = 0;
215        }
216        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
217    
218        // verify that our changes were not saved
219        foreach ($edit as $key => $value) {
220         if (preg_match('/path_title_/', $key)) {
221           $this->assertNotEqual($value, _autoassignrole_get_settings($key), 'Assign From Path: '. $key .' failed validation correctly when Menu Items is selected');
222         }
223        }
224    
225        foreach ($this->roles as $rid => $role) {
226         // display 1 is Tabs on Registration Page
227         $edit["path_display_$rid"] = 1;
228        }
229        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
230        foreach ($edit as $key => $value) {
231         if (preg_match('/path_title_/', $key)) {
232           $this->assertNotEqual($value, _autoassignrole_get_settings($key), 'Assign From Path: '. $key .' failed validation correctly when Tabs on registration page is selected');
233         }
234        }
235    
236        foreach ($this->roles as $rid => $role) {
237         // display 1 is Pages with no navigation
238         $edit["path_display_$rid"] = 2;
239        }
240        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
241        foreach ($edit as $key => $value) {
242         if (preg_match('/path_title_/', $key)) {
243           $this->assertEqual($value, _autoassignrole_get_settings($key), 'Assign From Path: '. $key .' was set with an empty title when Pages with no navigation is selected');
244         }
245        }
246    
247      }
248    }
249    
250    class AutoassignroleAdminAllowUserChoiceTestCase extends AAR {
251      public static function getInfo() {
252        return array(
253          'name' => t('Allow User to Choose Settings'),
254          'description' => t('Verify Allow User to Choose admin settings are stored as expected.'),
255          'group' => t('Autoassignrole'),
256        );
257      }
258    
259      function testAdminSettings() {
260        // Create a new user who can access the administration settings
261        $this->drupalLogin($this->admin_user);
262    
263        // Check that the user can see the admin settings page.
264        $this->drupalGet('admin/user/autoassignrole');
265        $this->assertRaw('Automatic Role Assignment', 'The Automatic Role Assignment section exists on the AAR admin page');
266    
267        $edit = $this->getEditValues('user_choice');
268    
269        // if User role Selection is set to Multiple Roles then Selection Method can
270        // not be Radio Buttons
271        $edit['user_multiple'] = 1;
272        $edit['user_selection'] = 0;
273        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
274        $this->assertNotEqual($edit['user_active'], _autoassignrole_get_settings('user_active'), 'Allow User to Choose: user_active failed validation correctly if role Selection is set to Multiple Roles then Selection Method can not be Radio Buttons');
275    
276        // Get the form to pass validation
277        $edit['user_multiple'] = 0;
278        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
279    
280        // verify that our changes were saved
281        foreach ($edit as $key => $value) {
282         $this->assertEqual($value, _autoassignrole_get_settings($key), 'Allow User to Choose: '. $key .' was set properly');
283        }
284    
285      }
286    }
287    
288    class AutoassignroleUserAutoRoleTestCase extends AAR {
289      public static function getInfo() {
290        return array(
291          'name' => t('Automatic Role Assignment'),
292          'description' => t('Verify Automatic Role Assignment is functioning on new user accounts as expected.'),
293          'group' => t('Autoassignrole'),
294        );
295      }
296    
297      function testAutomaticRoleAssignment() {
298        // Create a new user who can access the administration settings
299        $admin_user = $this->drupalCreateUser(array('access administration pages', 'administer autoassignrole'));
300        $this->drupalLogin($admin_user);
301    
302        $edit = $this->getEditValues('automatic_role_assignment');
303        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
304    
305        $new_user = $this->drupalCreateUser();
306        $this->drupalLogin($new_user);
307        // check the user after login to make sure they have been assigned the appropriate
308        // roles
309        foreach ($this->roles as $rid => $role) {
310         $this->assertTrue(array_key_exists($rid, $this->loggedInUser->roles), "New User has been assigned the role $role");
311        }
312    
313      }
314    }
315    
316    class AutoassignroleUserPathTestCase extends AAR {
317      public static function getInfo() {
318        return array(
319          'name' => t('Assign From Path Assignment'),
320          'description' => t('Verify Assign From Path is functioning on new user accounts as expected.'),
321          'group' => t('Autoassignrole'),
322        );
323      }
324    
325      function testPathTabAssignment() {
326        // Create a new user who can access the administration settings
327        $this->drupalLogin($this->admin_user);
328    
329        $edit = $this->getEditValues('assign_from_path');
330        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
331    
332        //Create a new user normally and verify that no additional roles are added
333        $new_user = $this->drupalCreateUser();
334        $this->drupalLogin($new_user);
335    
336        // check the user after login to make sure they have not been assigned our
337        // roles
338        foreach ($this->roles as $rid => $role) {
339         $this->assertFalse(array_key_exists($rid, $this->loggedInUser->roles), "New User has not been assigned the role $role");
340        }
341    
342        $this->drupalLogout();
343        $user_page = $this->drupalGet('user');
344    
345        foreach ($this->roles as $rid => $role) {
346         $this->drupalLogout();
347         $path = $edit["path_$rid"];
348         $title = $edit["path_title_$rid"];
349         $description = $edit["path_description_$rid"];
350    
351         // Make sure all tabs are on the user page with the correct titles
352         $regex = '/\<ul class="tabs primary"\>.+(\/user\/'. $path .'"\>'. $title .').+\<\/ul>/si';
353         $this->assertTrue(preg_match($regex, $user_page), 'A tab named '. $title .' is pointing to user/'. $path);
354    
355         $role_page = $this->drupalGet('user/'. $path);
356    
357         // Verify our description is on the page
358         $regex = '/\<p\>('. $description .')\<\/p>/si';
359         $this->assertTrue(preg_match($regex, $role_page), 'The description '. $description .' is on the page at user/'. $path);
360    
361         // Create a new user at this page and load the user object
362        $new_user = $this->getNewUser();
363        $this->drupalPost('user/'. $path, $new_user, t('Create new account'));
364        $user = user_load(array('name' => $new_user['name'], 'mail' => $new_user['mail']));
365    
366        // verify the user has been assigned only this path based and authenticated
367        // user roles
368         $this->assertEqual(count($user->roles), 2, 'The roles array for user '. $new_user['name'] .' is the correct size');
369         $this->assertTrue(array_key_exists($rid, $user->roles), 'The user '. $new_user['name'] .' has been assigned the correct path based role');
370        }
371      }
372    
373      function testPathMenuAssignment () {
374        // Create a new user who can access the administration settings
375        $this->drupalLogin($this->admin_user);
376    
377        $edit = $this->getEditValues('assign_from_path');
378        // for each role we are going to create a path with Display Method of Tab
379        // Items
380        foreach ($this->roles as $rid => $role) {
381         $edit["path_display_$rid"] = 0;
382        }
383        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
384    
385        $this->drupalLogout();
386        $user_page = $this->drupalGet('user');
387    
388        foreach ($this->roles as $rid => $role) {
389         $this->drupalLogout();
390         $path = $edit["path_$rid"];
391         $title = $edit["path_title_$rid"];
392         $description = $edit["path_description_$rid"];
393    
394         // verify that our menu items are not showing up as tabs on the user page
395         $regex = '/\<ul class="tabs primary"\>.+(\/user\/'. $path .'"\>'. $title .').+\<\/ul>/si';
396         $this->assertFalse(preg_match($regex, $user_page), 'A tab named '. $title .' pointing to user/'. $path .' does not exist on the user page');
397    
398         // verify that our menu items are showing up as menu items on the user page
399         $regex = '/\<ul class="menu"\>.+(\/'. $path .'"\>'. $title .').+\<\/ul>/si';
400         $this->assertTrue(preg_match($regex, $user_page), 'A menu item named '. $title .' pointing to /'. $path .' exists on the user page');
401    
402         // Create a new user at this page and load the user object
403        $new_user = $this->getNewUser();
404        $this->drupalPost($path, $new_user, t('Create new account'));
405        $user = user_load(array('name' => $new_user['name'], 'mail' => $new_user['mail']));
406    
407        // verify the user has been assigned only this path based and authenticated
408        // user roles
409         $this->assertEqual(count($user->roles), 2, 'The roles array for user '. $new_user['name'] .' is the correct size');
410         $this->assertTrue(array_key_exists($rid, $user->roles), 'The user '. $new_user['name'] .' has been assigned the correct path based role');
411        }
412      }
413    
414    }
415    
416    class AutoassignroleUserChoiceTestCase extends AAR {
417      public static function getInfo() {
418        return array(
419          'name' => t('Allow User to Choose Assignment'),
420          'description' => t('Verify Allow User to Choose is functioning on new user accounts as expected.'),
421          'group' => t('Autoassignrole'),
422        );
423      }
424    
425      function testUserChoiceAssignment() {
426        // Create a new user who can access the administration settings
427        $this->drupalLogin($this->admin_user);
428    
429        $edit = $this->getEditValues('user_choice');
430        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
431    
432        //Create a new user normally and verify that no additional roles are added
433        $new_user = $this->drupalCreateUser();
434        $this->drupalLogin($new_user);
435    
436        // check the user after login to make sure they have not been assigned our
437        // roles
438        foreach ($this->roles as $rid => $role) {
439         $this->assertFalse(array_key_exists($rid, $this->loggedInUser->roles), "New User has not been assigned the role $role");
440        }
441        $this->drupalLogout();
442    
443        $user_register_page = $this->drupalGet('user/register');
444         // Create a new user at this page and load the user object
445        $new_user = $this->getNewUser();
446    
447        // right now the admin settings are set to allow multiple roles but not require
448        // any to be selected.  Validate selecting all available roles.
449        $new_user['user_roles[]'] = array();
450        foreach ($this->roles as $rid => $role) {
451         $new_user['user_roles[]'][$rid] = TRUE;
452        }
453        $this->drupalPost('user/register', $new_user, t('Create new account'));
454        $user = user_load(array('name' => $new_user['name'], 'mail' => $new_user['mail']));
455        foreach ($this->roles as $rid => $role) {
456          $this->assertTrue(array_key_exists($rid, $user->roles), 'The user '. $new_user['name'] .' has been assigned the correct user selected role');
457        }
458    
459        // Clear out the users selection and make sure that they can create an account
460        // without selecting any roles.
461        $new_user = $this->getNewUser();
462        $this->drupalPost('user/register', $new_user, t('Create new account'));
463        $user = user_load(array('name' => $new_user['name'], 'mail' => $new_user['mail']));
464    
465        foreach ($this->roles as $rid => $role) {
466          $this->assertFalse(array_key_exists($rid, $user->roles), 'The user '. $new_user['name'] .' was not assigned the '. $role .' role');
467        }
468    
469        // use the admin form to set the role selection as required
470        $this->drupalLogin($this->admin_user);
471        $edit['user_required'] = 1;
472        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
473        $this->drupalLogout();
474    
475        // verify a new user does not get created when submitting the registration
476        // form without selecting any roles
477        $new_user = $this->getNewUser();
478        $this->drupalPost('user/register', $new_user, t('Create new account'));
479        $user = user_load(array('name' => $new_user['name'], 'mail' => $new_user['mail']));
480        $this->assertTrue(empty($user->uid), 'The user '. $new_user['name'] .' was not created');
481    
482        // verify that the roles displayed to the user are in ascending order
483       preg_match_all('/\<option value="\d+"\>([A-Za-z0-9_]+)/', $user_register_page, $matches);
484       $this->assertEqual($this->getSortOrder($matches[1]), 'ASC', 'The roles presented to the user are in Ascending order');
485    
486       // Switch roles to descending order and verify they get displayed to the user
487       // in the correct order
488        // use the admin form to set the role selection as required
489        $this->drupalLogin($this->admin_user);
490        $edit['user_sort'] = 'SORT_DESC';
491        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
492        $this->drupalLogout();
493        $user_register_page = $this->drupalGet('user/register');
494        // verify that the roles displayed to the user are in ascending order
495       preg_match_all('/\<option value="\d+"\>([A-Za-z0-9_]+)/', $user_register_page, $matches);
496       $this->assertEqual($this->getSortOrder($matches[1]), 'DESC', 'The roles presented to the user are in Descending order');
497    
498       // Switch to check boxes and verify they appear correct order
499        $this->drupalLogin($this->admin_user);
500        $edit['user_selection'] = 2;
501        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
502        $this->drupalLogout();
503        $user_register_page = $this->drupalGet('user/register');
504        preg_match_all('/\<input type="checkbox".+\\/> ([A-Za-z0-9_]+)<\/label\>/', $user_register_page, $matches);
505        $this->assertEqual($this->getSortOrder($matches[1]), 'DESC', 'The roles presented to the user are checkboxes in Descending order');
506    
507       // Switch to single selection radio buttons and verify they appear correct order
508        $this->drupalLogin($this->admin_user);
509        $edit['user_multiple'] = 0;
510        $edit['user_selection'] = 0;
511        $this->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
512        $this->drupalLogout();
513        $user_register_page = $this->drupalGet('user/register');
514        preg_match_all('/\<input type="radio".+\\/> ([A-Za-z0-9_]+)<\/label\>/', $user_register_page, $matches);
515        $this->assertEqual($this->getSortOrder($matches[1]), 'DESC', 'The roles presented to the user are radio buttons in Descending order');
516      }
517    }
518    
519    class AutoassignroleContentProfileTestCase extends AAR {
520      public static function getInfo() {
521        return array(
522          'name' => t('Content Profile Integration'),
523          'description' => t('Verify integration between AAR and Content Profile functions as expected.'),
524          'group' => t('Autoassignrole'),
525        );
526      }
527    
528      function testIntegration() {
529      }
530    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.3