]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/SugarTestUserUtilities.php
Added unit tests.
[Github/sugarcrm.git] / tests / SugarTestUserUtilities.php
1 <?php
2 require_once 'modules/Users/User.php';
3
4 class SugarTestUserUtilities
5 {
6     private static $_createdUsers = array();
7
8     private function __construct() {}
9     
10     public function __destruct()
11     {
12         self::removeAllCreatedAnonymousUsers();
13     }
14
15     public static function createAnonymousUser($id = '') 
16     {
17         if (isset($_REQUEST['action'])) { 
18         unset($_REQUEST['action']);
19         }
20         
21         $time = mt_rand();
22         $userId = 'SugarUser';
23         $user = new User();
24         $user->user_name = $userId . $time;
25         $user->user_hash = md5($userId.$time);
26         $user->first_name = $userId;
27         $user->last_name = $time;
28         $user->status='Active';
29         if(!empty($id))
30         {
31             $user->new_with_id = true;
32             $user->id = $id;
33         }
34         $user->save();
35         $user->fill_in_additional_detail_fields();
36         self::$_createdUsers[] = $user;
37         return $user;
38     }
39     
40     public function removeAllCreatedAnonymousUsers() 
41     {
42         $user_ids = self::getCreatedUserIds();
43         if ( count($user_ids) > 0 ) {
44             $GLOBALS['db']->query('DELETE FROM users WHERE id IN (\'' . implode("', '", $user_ids) . '\')');
45             $GLOBALS['db']->query('DELETE FROM user_preferences WHERE assigned_user_id IN (\'' . implode("', '", $user_ids) . '\')');
46         }
47         self::$_createdUsers = array();
48     }
49     
50     public static function getCreatedUserIds() 
51     {
52         $user_ids = array();
53         foreach (self::$_createdUsers as $user)
54             $user_ids[] = $user->id;
55         
56         return $user_ids;
57     }
58 }