]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Trackers/TrackerReportsUsageTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / modules / Trackers / TrackerReportsUsageTest.php
1 <?php
2 require_once('modules/Trackers/TrackerManager.php');
3
4 class TrackerReportsUsageTest extends Sugar_PHPUnit_Framework_TestCase
5 {
6     public function setUp()
7     {
8         SugarTestTrackerUtility::setup();
9         
10         $trackerManager = TrackerManager::getInstance();
11         $trackerManager->unPause();
12         
13         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
14         
15         //$tracker_sessions_monitor = $trackerManager->getMonitor('tracker_sessions');
16         $monitor = $trackerManager->getMonitor('tracker');
17         $monitor->setEnabled(true);
18         $monitor->setValue('module_name', 'Contacts');
19         $monitor->setValue('item_id', '10909d69-2b55-094d-ba89-47b23d3121dd');
20         $monitor->setValue('item_summary', 'Foo');
21         $monitor->setValue('date_modified', gmdate($GLOBALS['timedate']->get_db_date_time_format()), strtotime("-1 day")+5000);
22         $monitor->setValue('action', 'index');
23         $monitor->setValue('session_id', 'test_session');
24         $monitor->setValue('user_id', $GLOBALS['current_user']->id);
25         $trackerManager->save();
26         
27         $monitor->setValue('module_name', 'Contacts');
28         $monitor->setValue('item_id', '10909d69-2b55-094d-ba89-47b23d3121dd');
29         $monitor->setValue('item_summary', 'Foo');
30         $monitor->setValue('date_modified', gmdate($GLOBALS['timedate']->get_db_date_time_format(), strtotime("-1 week")+5000));
31         $monitor->setValue('action', 'index');
32         $monitor->setValue('session_id', 'test_session');        
33         $monitor->setValue('user_id', $GLOBALS['current_user']->id);
34         $trackerManager->save();
35        
36         $monitor->setValue('module_name', 'Contacts');
37         $monitor->setValue('item_id', '10909d69-2b55-094d-ba89-47b23d3121dd');
38         $monitor->setValue('item_summary', 'Foo');
39         $monitor->setValue('date_modified', gmdate($GLOBALS['timedate']->get_db_date_time_format(), strtotime("-1 month")+5000));
40         $monitor->setValue('action', 'index');
41         $monitor->setValue('session_id', 'test_session');
42         $monitor->setValue('user_id', $GLOBALS['current_user']->id);            
43         $trackerManager->save();
44
45         $beanList = array();
46         $beanFiles = array();
47         require('include/modules.php');
48         $GLOBALS['beanList'] = $beanList;
49         $GLOBALS['beanFiles'] = $beanFiles;
50     }
51     
52     public function tearDown()
53     {
54         $query = "DELETE FROM tracker WHERE session_id = 'test_session'";
55         $GLOBALS['db']->query($query);
56         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
57         SugarTestTrackerUtility::restore();
58         
59         unset($GLOBALS['beanList']);
60         unset($GLOBALS['beanFiles']);
61     }
62     
63     public function testUsageMetricsDay()
64     {
65         $query = "SELECT module_name, item_id, item_summary, date_modified from tracker where session_id = 'test_session' and user_id = '{$GLOBALS['current_user']->id}' and date_modified > ";
66         $query .= db_convert("'". gmdate($GLOBALS['timedate']->get_db_date_time_format(), strtotime("-1 day")) ."'" ,"datetime");
67         $count = $GLOBALS['db']->getRowCount($GLOBALS['db']->query($query));
68         $this->assertEquals($count,1);
69     }
70     
71     public function testUsageMetricsWeek()
72     {
73         $query = "SELECT module_name, item_id, item_summary, date_modified from tracker where session_id = 'test_session' and user_id = '{$GLOBALS['current_user']->id}' and date_modified > ";
74         $query .= db_convert("'". gmdate($GLOBALS['timedate']->get_db_date_time_format(), strtotime("-1 week")) ."'" ,"datetime");
75         $count = $GLOBALS['db']->getRowCount($GLOBALS['db']->query($query));
76         $this->assertEquals($count,2);
77     }
78     
79     public function testUsageMetricsMonth()
80     {
81         $query = "SELECT module_name, item_id, item_summary, date_modified from tracker where session_id = 'test_session' and user_id = '{$GLOBALS['current_user']->id}' and date_modified > ";
82         $query .= db_convert("'". gmdate($GLOBALS['timedate']->get_db_date_time_format(), strtotime("-1 month")) ."'" ,"datetime");
83         $count = $GLOBALS['db']->getRowCount($GLOBALS['db']->query($query));
84         $this->assertEquals($count,3);          
85     }
86 }
87
88 ?>