]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/RESTAPI3Test.php
Added unit tests.
[Github/sugarcrm.git] / tests / service / RESTAPI3Test.php
1 <?php
2
3 require_once('service/v3/SugarWebServiceUtilv3.php');
4 require_once('tests/service/APIv3Helper.php');
5
6
7 class RESTAPI3Test extends Sugar_PHPUnit_Framework_TestCase
8 {
9     protected $_user;
10     
11     protected $_lastRawResponse;
12     
13     private static $helperObject;
14     
15     public function setUp()
16     {
17         //Reload langauge strings 
18         $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
19         $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
20         $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], 'Accounts');
21         //Create an anonymous user for login purposes/
22         $this->_user = SugarTestUserUtilities::createAnonymousUser();
23         $this->_user->status = 'Active';
24         $this->_user->is_admin = 1;
25         $this->_user->save();
26         $GLOBALS['current_user'] = $this->_user;
27         
28         self::$helperObject = new APIv3Helper();
29     }
30     
31     public function tearDown() 
32         {
33             if(isset($GLOBALS['listViewDefs'])) unset($GLOBALS['listViewDefs']); 
34             if(isset($GLOBALS['viewdefs'])) unset($GLOBALS['viewdefs']); 
35         }
36         
37     protected function _makeRESTCall($method,$parameters)
38     {
39         // specify the REST web service to interact with 
40         $url = $GLOBALS['sugar_config']['site_url'].'/service/v3/rest.php'; 
41         // Open a curl session for making the call 
42         $curl = curl_init($url); 
43         // set URL and other appropriate options
44         curl_setopt($curl, CURLOPT_URL, $url);
45         curl_setopt($curl, CURLOPT_POST, 1);
46         curl_setopt($curl, CURLOPT_HEADER, 0);
47         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
48         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
49         curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
50         curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
51         // build the request URL
52         $json = json_encode($parameters); 
53         $postArgs = "method=$method&input_type=JSON&response_type=JSON&rest_data=$json"; 
54         curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); 
55         // Make the REST call, returning the result 
56         $response = curl_exec($curl); 
57         // Close the connection 
58         curl_close($curl);  
59         
60         $this->_lastRawResponse = $response;
61         
62         // Convert the result from JSON format to a PHP array 
63         return json_decode($response,true); 
64     }
65     
66     protected function _returnLastRawResponse()
67     {
68         return "Error in web services call. Response was: {$this->_lastRawResponse}";
69     }
70     
71     protected function _login()
72     {
73         return $this->_makeRESTCall('login',
74             array(
75                 'user_auth' => 
76                     array( 
77                         'user_name' => $this->_user->user_name, 
78                         'password' => $this->_user->user_hash, 
79                         'version' => '.01',
80                         ), 
81                 'application_name' => 'SugarTestRunner', 
82                 'name_value_list' => array(),
83                 )
84             ); 
85     }
86     
87     public function testSearchByModule()
88     {
89         $result = $this->_login();
90         $session = $result['id'];
91
92         $seedData = self::$helperObject->populateSeedDataForSearchTest($this->_user->id);
93         
94         $searchModules = array('Accounts','Contacts','Opportunities');
95         $searchString = "UNIT TEST";
96         $offSet = 0;
97         $maxResults = 10;
98
99         $results = $this->_makeRESTCall('search_by_module',
100                         array(
101                             'session' => $session,
102                             'search'  => $searchString,
103                             'modules' => $searchModules,
104                             'offset'  => $offSet,
105                             'max'     => $maxResults,
106                             'user'    => $this->_user->id)
107                         );
108                               
109         $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts') );  
110         $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts') ); 
111         $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts') ); 
112         $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities') ); 
113         $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities') ); 
114         $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
115         $GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
116         $GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
117     }
118     
119     public function testSearchByModuleWithReturnFields()
120     {
121         $result = $this->_login();
122         $session = $result['id'];
123
124         $seedData = self::$helperObject->populateSeedDataForSearchTest($this->_user->id);
125         
126         $returnFields = array('name','id','deleted');
127         $searchModules = array('Accounts','Contacts','Opportunities');
128         $searchString = "UNIT TEST";
129         $offSet = 0;
130         $maxResults = 10;
131
132         $results = $this->_makeRESTCall('search_by_module',
133                         array(
134                             'session' => $session,
135                             'search'  => $searchString,
136                             'modules' => $searchModules,
137                             'offset'  => $offSet,
138                             'max'     => $maxResults,
139                             'user'    => $this->_user->id,
140                             'selectFields' => $returnFields)
141                         );
142
143
144         $this->assertEquals($seedData[0]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts', $seedData[0]['fieldName']));
145         $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts', $seedData[1]['fieldName']));
146         $this->assertEquals($seedData[2]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts', $seedData[2]['fieldName']));
147         $this->assertEquals($seedData[3]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities', $seedData[3]['fieldName']));
148         $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities', $seedData[4]['fieldName']));
149         
150         $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
151         $GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
152         $GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
153     }
154     
155     public function testGetServerInformation()
156     {
157         require('sugar_version.php');
158         
159         $result = $this->_login();
160         $session = $result['id'];
161             
162         $result = $this->_makeRESTCall('get_server_info',array());
163         
164         $this->assertEquals($sugar_version, $result['version'],'Unable to get server information');
165         $this->assertEquals($sugar_flavor, $result['flavor'],'Unable to get server information');
166     }
167     
168     public function testGetModuleList()
169     {
170         $result = $this->_login();
171         $session = $result['id'];
172         
173         $account = new Account();
174         $account->id = uniqid();
175         $account->new_with_id = TRUE;
176         $account->name = "Test " . $account->id;
177         $account->save();
178         
179         $whereClause = "accounts.name='{$account->name}'";
180         $module = 'Accounts';
181         $orderBy = 'name';
182         $offset = 0;
183         $returnFields = array('name');
184         $result = $this->_makeRESTCall('get_entry_list', array($session, $module, $whereClause, $orderBy,$offset, $returnFields)); 
185         
186         $this->assertEquals($account->id, $result['entry_list'][0]['id'],'Unable to retrieve account list during search.');
187
188         $GLOBALS['db']->query("DELETE FROM accounts WHERE id = '{$account->id}'");
189         
190     }
191     
192     public function testLogin()
193     {
194         $result = $this->_login();
195         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
196     }
197     
198     public static function _multipleModuleLayoutProvider()
199     {
200         return array(                 
201                         array(
202                             'module' => array('Accounts','Contacts'),
203                             'type' => array('default'),
204                             'view' => array('list'),
205                             'expected_file' => array(
206                                 'Accounts' => array( 'default' => array('list' => 'modules/Accounts/metadata/listviewdefs.php')),
207                                 'Contacts' => array( 'default' => array('list' => 'modules/Contacts/metadata/listviewdefs.php')))
208                         ),
209                         array(
210                             'module' => array('Accounts','Contacts'),
211                             'type' => array('default'),
212                             'view' => array('list','detail'),
213                             'expected_file' => array(
214                                 'Accounts' => array(
215                                     'default' => array(
216                                                 'list' => 'modules/Accounts/metadata/listviewdefs.php',
217                                                 'detail' => 'modules/Accounts/metadata/detailviewdefs.php')),
218                                 'Contacts' => array(
219                                     'default' => array(
220                                                 'list' => 'modules/Contacts/metadata/listviewdefs.php',
221                                                 'detail' => 'modules/Contacts/metadata/detailviewdefs.php'))
222                         ))
223         );
224     }
225
226     /**
227      * @dataProvider _multipleModuleLayoutProvider
228      */
229     public function testGetMultipleModuleLayout($a_module, $a_type, $a_view, $a_expected_file)
230     {
231         $result = $this->_login();
232         $session = $result['id'];
233             
234         $results = $this->_makeRESTCall('get_module_layout',
235                         array(
236                             'session' => $session,
237                             'module' => $a_module,
238                             'type' => $a_type,
239                             'view' => $a_view)
240                         );
241
242         foreach ($results as $module => $moduleResults ) 
243         {                       
244             foreach ($moduleResults as $type => $viewResults)
245             {
246                 foreach ($viewResults as $view => $result)
247                 {    
248                     $expected_file = $a_expected_file[$module][$type][$view];
249                     if ( is_file('custom'  . DIRECTORY_SEPARATOR . $expected_file) ) 
250                         require('custom'  . DIRECTORY_SEPARATOR . $expected_file);
251                     else
252                         require($expected_file);
253             
254                     if($view == 'list')
255                         $expectedResults = $listViewDefs[$module];
256                     else
257                         $expectedResults = $viewdefs[$module][ucfirst($view) .'View' ];
258                     
259                     $this->assertEquals(md5(serialize($expectedResults)), md5(serialize($result)), "Unable to retrieve module layout: module {$module}, type $type, view $view");
260                 }
261                 }
262         } 
263    }
264     
265     public static function _moduleLayoutProvider()
266     {
267         return array( 
268                     array('module' => 'Accounts','type' => 'default', 'view' => 'list','expected_file' => 'modules/Accounts/metadata/listviewdefs.php' ), 
269                     array('module' => 'Accounts','type' => 'default', 'view' => 'edit','expected_file' => 'modules/Accounts/metadata/editviewdefs.php' ),  
270                     array('module' => 'Accounts','type' => 'default', 'view' => 'detail','expected_file' => 'modules/Accounts/metadata/detailviewdefs.php' ),  
271         );
272     }
273
274     /**
275      * @dataProvider _moduleLayoutProvider
276      */
277     public function testGetModuleLayout($module, $type, $view, $expected_file)
278     {
279         $result = $this->_login();
280         $session = $result['id'];
281             
282         $result = $this->_makeRESTCall('get_module_layout',
283                         array(
284                             'session' => $session,
285                             'module' => array($module),
286                             'type' => array($type),
287                             'view' => array($view))
288                         );
289                     
290         if ( is_file('custom'  . DIRECTORY_SEPARATOR . $expected_file) ) 
291                 require('custom'  . DIRECTORY_SEPARATOR . $expected_file);
292         else
293             require($expected_file);
294
295         if($view == 'list')
296             $expectedResults = $listViewDefs[$module];
297         else
298             $expectedResults = $viewdefs[$module][ucfirst($view) .'View' ];
299  
300         $a_expectedResults = array();
301         $a_expectedResults[$module][$type][$view] = $expectedResults;
302         
303         $this->assertEquals(md5(serialize($a_expectedResults)), md5(serialize($result)), "Unable to retrieve module layout: module {$module}, type $type, view $view");
304     }
305
306      /**
307      * @dataProvider _moduleLayoutProvider
308      */
309     public function testGetModuleLayoutMD5($module, $type, $view, $expected_file)
310     {
311         $result = $this->_login();
312         $session = $result['id'];
313             
314         $fullResult = $this->_makeRESTCall('get_module_layout_md5',
315                         array(
316                             'session' => $session,
317                             'module' => array($module),
318                             'type' => array($type),
319                             'view' => array($view) )
320                         );
321         $result = $fullResult['md5'];
322         if ( is_file('custom'  . DIRECTORY_SEPARATOR . $expected_file) ) 
323                 require('custom'  . DIRECTORY_SEPARATOR . $expected_file);
324         else
325             require($expected_file);
326
327         if($view == 'list')
328             $expectedResults = $listViewDefs[$module];
329         else
330             $expectedResults = $viewdefs[$module][ucfirst($view) .'View' ];
331         
332         $a_expectedResults = array();
333         $a_expectedResults[$module][$type][$view] = $expectedResults;
334         
335         $this->assertEquals(md5(serialize($expectedResults)), $result[$module][$type][$view], "Unable to retrieve module layout md5: module {$module}, type $type, view $view");
336
337     }
338     
339     public function testGetAvailableModules()
340     {
341         $this->markTestSkipped('Will be updated week of June 21, 2010');
342         
343         $result = $this->_login();
344         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
345         $session = $result['id'];
346         
347         $fullResult = $this->_makeRESTCall('get_available_modules', array('session' => $session, 'filter' => 'all' ));
348         $this->assertTrue(in_array('ACLFields', $fullResult['modules']), "Unable to get all available modules");
349         $this->assertTrue(in_array('Schedulers', $fullResult['modules']), "Unable to get all available modules");
350         $this->assertTrue(in_array('Roles', $fullResult['modules']), "Unable to get all available modules");
351         
352         $sh = new SugarWebServiceUtilv3();
353                 
354         $mobileResult = $this->_makeRESTCall('get_available_modules', array('session' => $session, 'filter' => 'mobile' ));
355         $mobileResultExpected = $sh->get_visible_mobile_modules($fullResult['modules']);
356         $mobileResultExpected = md5(serialize(array('modules' => $mobileResultExpected)));
357         $mobileResult = md5(serialize($mobileResult));
358         $this->assertEquals($mobileResultExpected, $mobileResult, "Unable to get all visible mobile modules");
359         
360         $defaultResult = $this->_makeRESTCall('get_available_modules', array('session' => $session, 'filter' => 'default' ));
361         $defaultResult = md5(serialize($defaultResult['modules']));
362         $defaultResultExpected = $sh->get_visible_modules($fullResult['modules']);
363         $defaultResultExpected = md5(serialize($defaultResultExpected));        
364         $this->assertEquals($defaultResultExpected, $defaultResult, "Unable to get all visible default modules");
365       
366     }
367     
368     public function testGetVardefsMD5()
369     {
370         $GLOBALS['reload_vardefs'] = TRUE;
371         $result = $this->_login();
372         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
373         $session = $result['id'];
374         
375         //Test a regular module
376         $fullResult = $this->_makeRESTCall('get_module_fields_md5', array('session' => $session, 'module' => 'Accounts' )); 
377         $result = $fullResult['Accounts'];
378         $a = new Account();
379         $soapHelper = new SugarWebServiceUtilv3();
380         $actualVardef = $soapHelper->get_return_module_fields($a,'Accounts','');
381         $actualMD5 = md5(serialize($actualVardef));
382         $this->assertEquals($actualMD5, $result, "Unable to retrieve vardef md5.");
383         
384         //Test a fake module
385         $result = $this->_makeRESTCall('get_module_fields_md5', array('session' => $session, 'module' => 'BadModule' )); 
386         $this->assertTrue($result['name'] == 'Module Does Not Exist');
387         unset($GLOBALS['reload_vardefs']);
388     }
389     
390     public function testAddNewAccountAndThenDeleteIt()
391     {
392         $result = $this->_login();
393         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
394         $session = $result['id']; 
395         
396         $result = $this->_makeRESTCall('set_entry',
397             array(
398                 'session' => $session, 
399                 'module' => 'Accounts', 
400                 'name_value_list' => array( 
401                     array('name' => 'name', 'value' => 'New Account'), 
402                     array('name' => 'description', 'value' => 'This is an account created from a REST web services call'), 
403                     ), 
404                 )
405             ); 
406         
407         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
408         
409         $accountId = $result['id'];
410         
411         // verify record was created
412         $result = $this->_makeRESTCall('get_entry',
413             array(
414                 'session' => $session, 
415                 'module' => 'Accounts', 
416                 'id' => $accountId,
417                 )
418             );
419         
420         $this->assertEquals($result['entry_list'][0]['id'],$accountId,$this->_returnLastRawResponse());
421         
422         // delete the record
423         $result = $this->_makeRESTCall('set_entry',
424             array(
425                 'session' => $session, 
426                 'module' => 'Accounts', 
427                 'name_value_list' => array( 
428                     array('name' => 'id', 'value' => $accountId), 
429                     array('name' => 'deleted', 'value' => '1'), 
430                     ), 
431                 )
432             ); 
433         
434         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
435         
436         // try to retrieve again to validate it is deleted
437         $result = $this->_makeRESTCall('get_entry',
438             array(
439                 'session' => $session, 
440                 'module' => 'Accounts', 
441                 'id' => $accountId,
442                 )
443             ); 
444         
445         $this->assertTrue(!empty($result['entry_list'][0]['id']) && $result['entry_list'][0]['id'] != -1,$this->_returnLastRawResponse());
446         $this->assertEquals($result['entry_list'][0]['name_value_list'][0]['name'],'warning',$this->_returnLastRawResponse());
447         $this->assertEquals($result['entry_list'][0]['name_value_list'][0]['value'],"Access to this object is denied since it has been deleted or does not exist",$this->_returnLastRawResponse());
448         $this->assertEquals($result['entry_list'][0]['name_value_list'][1]['name'],'deleted',$this->_returnLastRawResponse());
449         $this->assertEquals($result['entry_list'][0]['name_value_list'][1]['value'],1,$this->_returnLastRawResponse());
450     }
451     
452     public function testRelateAccountToTwoContacts()
453     {
454         $result = $this->_login();
455         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
456         $session = $result['id']; 
457         
458         $result = $this->_makeRESTCall('set_entry',
459             array(
460                 'session' => $session, 
461                 'module' => 'Accounts', 
462                 'name_value_list' => array( 
463                     array('name' => 'name', 'value' => 'New Account'), 
464                     array('name' => 'description', 'value' => 'This is an account created from a REST web services call'), 
465                     ), 
466                 )
467             ); 
468         
469         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
470         
471         $accountId = $result['id'];
472         
473         $result = $this->_makeRESTCall('set_entry',
474             array(
475                 'session' => $session, 
476                 'module' => 'Contacts', 
477                 'name_value_list' => array( 
478                     array('name' => 'last_name', 'value' => 'New Contact 1'), 
479                     array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'), 
480                     ), 
481                 )
482             ); 
483         
484         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
485         
486         $contactId1 = $result['id'];
487         
488         $result = $this->_makeRESTCall('set_entry',
489             array(
490                 'session' => $session, 
491                 'module' => 'Contacts', 
492                 'name_value_list' => array( 
493                     array('name' => 'last_name', 'value' => 'New Contact 2'), 
494                     array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'), 
495                     ), 
496                 )
497             ); 
498         
499         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
500         
501         $contactId2 = $result['id'];
502         
503         // now relate them together
504         $result = $this->_makeRESTCall('set_relationship',
505             array( 
506                 'session' => $session, 
507                 'module' => 'Accounts', 
508                 'module_id' => $accountId,
509                 'link_field_name' => 'contacts', 
510                 'related_ids' => array($contactId1,$contactId2), 
511                 )
512             ); 
513         
514         $this->assertEquals($result['created'],1,$this->_returnLastRawResponse());
515         
516         // check the relationship
517         $result = $this->_makeRESTCall('get_relationships',
518             array( 
519                 'session' => $session, 
520                 'module' => 'Accounts', 
521                 'module_id' => $accountId,
522                 'link_field_name' => 'contacts', 
523                 'related_module_query' => '', 
524                 'related_fields' => array('last_name','description'),
525                 'related_module_link_name_to_fields_array' => array(),
526                 'deleted' => false,
527                 )
528             );
529         
530         $returnedValues = array();
531         $returnedValues[] = $result['entry_list'][0]['name_value_list']['last_name']['value'];
532         $returnedValues[] = $result['entry_list'][1]['name_value_list']['last_name']['value'];
533         
534         
535         $this->assertContains('New Contact 1',$returnedValues,$this->_returnLastRawResponse());
536         $this->assertContains('New Contact 2',$returnedValues,$this->_returnLastRawResponse());       
537     }
538      
539     /**
540      * @group bug36658
541      */
542     public function testOrderByClauseOfGetRelationship()
543     {
544         $result = $this->_login();
545         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
546         $session = $result['id']; 
547         
548         $result = $this->_makeRESTCall('set_entry',
549             array(
550                 'session' => $session, 
551                 'module' => 'Accounts', 
552                 'name_value_list' => array( 
553                     array('name' => 'name', 'value' => 'New Account'), 
554                     array('name' => 'description', 'value' => 'This is an account created from a REST web services call'), 
555                     ), 
556                 )
557             ); 
558         
559         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
560         
561         $accountId = $result['id'];
562         
563         $result = $this->_makeRESTCall('set_entry',
564             array(
565                 'session' => $session, 
566                 'module' => 'Contacts', 
567                 'name_value_list' => array( 
568                     array('name' => 'last_name', 'value' => 'New Contact 1'), 
569                     array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'), 
570                     ), 
571                 )
572             ); 
573         
574         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
575         
576         $contactId1 = $result['id'];
577         
578         $result = $this->_makeRESTCall('set_entry',
579             array(
580                 'session' => $session, 
581                 'module' => 'Contacts', 
582                 'name_value_list' => array( 
583                     array('name' => 'last_name', 'value' => 'New Contact 2'), 
584                     array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'), 
585                     ), 
586                 )
587             ); 
588         
589         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
590         
591         $contactId2 = $result['id'];
592         
593         // now relate them together
594         $result = $this->_makeRESTCall('set_relationship',
595             array( 
596                 'session' => $session, 
597                 'module' => 'Accounts', 
598                 'module_id' => $accountId,
599                 'link_field_name' => 'contacts', 
600                 'related_ids' => array($contactId1,$contactId2), 
601                 )
602             ); 
603         
604         $this->assertEquals($result['created'],1,$this->_returnLastRawResponse());
605         
606         // check the relationship
607         $result = $this->_makeRESTCall('get_relationships',
608             array( 
609                 'session' => $session, 
610                 'module' => 'Accounts', 
611                 'module_id' => $accountId,
612                 'link_field_name' => 'contacts', 
613                 'related_module_query' => '', 
614                 'related_fields' => array('last_name','description'),
615                 'related_module_link_name_to_fields_array' => array(),
616                 'deleted' => false,
617                 'order_by' => 'last_name',
618                 )
619             );
620         
621         $this->assertEquals($result['entry_list'][0]['name_value_list']['last_name']['value'],'New Contact 1',$this->_returnLastRawResponse());
622         $this->assertEquals($result['entry_list'][1]['name_value_list']['last_name']['value'],'New Contact 2',$this->_returnLastRawResponse());       
623     }
624     
625     public static function _subpanelLayoutProvider()
626     {
627         return array(
628             array(
629                 'module' => 'Contacts',
630                 'type' => 'default',
631                 'view' => 'subpanel',
632             ),
633             array(
634                 'module' => 'Leads',
635                 'type' => 'wireless',
636                 'view' => 'subpanel',
637             )
638         );
639     }
640
641     /**
642      * @dataProvider _subpanelLayoutProvider
643      */
644     public function testGetSubpanelLayout($module, $type, $view)
645     {
646         $result = $this->_login();
647         $session = $result['id'];
648
649         $results = $this->_makeRESTCall('get_module_layout',
650             array(
651                 'session' => $session,
652                 'module' => array($module),
653                 'type' => array($type),
654                 'view' => array($view))
655         );
656
657         $this->assertTrue(isset($results[$module][$type][$view]), "Unable to get subpanel defs");
658     }
659     
660      public function testGetLastViewed()
661      {
662          $result = $this->_login();
663          $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
664          $session = $result['id'];
665
666          $testModule = 'Accounts';
667          $testModuleID = uniqid();
668
669          $this->_createTrackerEntry($testModule,$testModuleID);
670
671          $results = $this->_makeRESTCall('get_last_viewed',
672                              array(
673                              'session' => $session,
674                              'modules' => array($testModule),
675                              )
676          );
677
678          $found = FALSE;
679          foreach ($results as $entry)
680          {
681              if($entry['item_id'] == $testModuleID)
682              {
683                  $found = TRUE;
684                  break;
685              }
686          }
687
688          $this->assertTrue($found, "Unable to get last viewed modules");
689      }
690      
691      private function _createTrackerEntry($module, $id,$summaryText = "UNIT TEST SUMMARY")
692      {
693         $trackerManager = TrackerManager::getInstance();
694         $timeStamp = gmdate($GLOBALS['timedate']->get_db_date_time_format());
695         $monitor = $trackerManager->getMonitor('tracker');
696
697         $monitor->setValue('action', 'detail');
698         $monitor->setValue('user_id', $this->_user->id);
699         $monitor->setValue('module_name', $module);
700         $monitor->setValue('date_modified', $timeStamp);
701         $monitor->setValue('visible', true);
702         $monitor->setValue('item_id', $id);
703         $monitor->setValue('item_summary', $summaryText);
704         $trackerManager->saveMonitor($monitor, true, true);
705      }
706      
707      public function testGetUpcomingActivities()
708      {
709          $result = $this->_login();
710          $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
711          $session = $result['id'];
712          $expected = $this->_createUpcomingActivities(); //Seed the data.
713          $results = $this->_makeRESTCall('get_upcoming_activities',
714                              array(
715                              'session' => $session,
716                              )
717          );
718          
719          $this->assertEquals($expected[0] ,$results[0]['id'] , "Unable to get upcoming activities");
720          $this->assertEquals($expected[1] ,$results[1]['id'] , "Unable to get upcoming activities");
721          
722          $this->_removeUpcomingActivities();
723      }
724      
725      private function _removeUpcomingActivities()
726      {
727          $GLOBALS['db']->query("DELETE FROM calls where name = 'UNIT TEST'");
728          $GLOBALS['db']->query("DELETE FROM tasks where name = 'UNIT TEST'");
729      }
730      
731      private function _createUpcomingActivities()
732      {
733          $GLOBALS['current_user']->setPreference('datef','Y-m-d') ;
734          $GLOBALS['current_user']->setPreference('timef','H:i') ;
735          
736          $date1 = $GLOBALS['timedate']->to_display_date_time(gmdate("Y-m-d H:i:s", (gmmktime() + (3600 * 24 * 2) ) ),true,true, $GLOBALS['current_user']) ; //Two days from today 
737          $date2 = $GLOBALS['timedate']->to_display_date_time(gmdate("Y-m-d H:i:s", (gmmktime() + (3600 * 24 * 4) ) ),true,true, $GLOBALS['current_user']) ; //Two days from today 
738          
739          $callID = uniqid();
740          $c = new Call();
741          $c->id = $callID;
742          $c->new_with_id = TRUE;
743          $c->status = 'Not Planned';
744          $c->date_start = $date1;
745          $c->name = "UNIT TEST";
746          $c->assigned_user_id = $this->_user->id;
747          $c->save(FALSE);
748
749          $callID = uniqid();
750          $c = new Call();
751          $c->id = $callID;
752          $c->new_with_id = TRUE;
753          $c->status = 'Planned';
754          $c->date_start = $date1;
755          $c->name = "UNIT TEST";
756          $c->assigned_user_id = $this->_user->id;
757          $c->save(FALSE);
758
759          $taskID = uniqid();
760          $t = new Task();
761          $t->id = $taskID;
762          $t->new_with_id = TRUE;
763          $t->status = 'Not Started';
764          $t->date_due = $date2;
765          $t->name = "UNIT TEST";
766          $t->assigned_user_id = $this->_user->id;
767          $t->save(FALSE);
768          
769          return array($callID, $taskID);
770      }
771 }