]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/SugarTestTrackerUtility.php
Added unit tests.
[Github/sugarcrm.git] / tests / SugarTestTrackerUtility.php
1 <?php
2 class SugarTestTrackerUtility
3 {
4     private static $_trackerSettings = array();
5     private static $_monitorId = '';
6     
7     private function __construct() {}
8     
9     public static function setup()
10     {
11         require('modules/Trackers/config.php');
12         foreach($tracker_config as $entry) {
13             if(isset($entry['bean'])) {
14                 $GLOBALS['tracker_' . $entry['name']] = false;
15             } //if
16         } //foreach
17         
18         $result = $GLOBALS['db']->query("SELECT category, name, value from config WHERE category = 'tracker' and name != 'prune_interval'");
19         while($row = $GLOBALS['db']->fetchByAssoc($result)){
20             self::$_trackerSettings[$row['name']] = $row['value'];
21             $GLOBALS['db']->query("DELETE FROM config WHERE category = 'tracker' AND name = '{$row['name']}'");
22         }
23     }
24     
25     public static function restore()
26     {
27         foreach(self::$_trackerSettings as $name=>$value) {
28             $GLOBALS['db']->query("INSERT INTO config (category, name, value) VALUES ('tracker', '{$name}', '{$value}')");
29         }
30     }
31     
32     public static function insertTrackerEntry($bean, $action)
33     {
34         require_once('modules/Trackers/TrackerManager.php');
35         $trackerManager = TrackerManager::getInstance();
36         $timeStamp = gmdate($GLOBALS['timedate']->get_db_date_time_format());
37         $_REQUEST['action'] = $action;
38         if($monitor = $trackerManager->getMonitor('tracker'))
39         {
40             $monitor->setValue('action', $action);
41             $monitor->setValue('user_id', $GLOBALS['current_user']->id);
42             $monitor->setValue('module_name', $bean->module_dir);
43             $monitor->setValue('date_modified', $timeStamp);
44             $monitor->setValue('visible', (($action == 'detailview') || ($action == 'editview')
45                                             || ($action == 'wirelessdetail') || ($action == 'wirelessedit')
46                                             ) ? 1 : 0);
47
48             if (!empty($bean->id))
49             {
50                 $monitor->setValue('item_id', $bean->id);
51                 $monitor->setValue('item_summary', $bean->get_summary_text());
52             }
53
54             //If visible is true, but there is no bean, do not track (invalid/unauthorized reference)
55             //Also, do not track save actions where there is no bean id
56             if($monitor->visible && empty($bean->id))
57             {
58                $trackerManager->unsetMonitor($monitor);
59                return false;
60             }
61             $trackerManager->saveMonitor($monitor, true, true);
62             if(empty(self::$_monitorId))
63             {
64                 self::$_monitorId = $monitor->monitor_id;
65             }
66         }
67     }
68     
69     public static function removeAllTrackerEntries()
70     {
71         if(!empty(self::$_monitorId))
72         {
73             $GLOBALS['db']->query("DELETE FROM tracker WHERE monitor_id = '".self::$_monitorId."'");
74         }
75     }
76 }
77 ?>