2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
38 require_once('service/v3/SugarWebServiceUtilv3.php');
39 require_once('tests/service/APIv3Helper.php');
42 class RESTAPI3Test extends Sugar_PHPUnit_Framework_TestCase
46 protected $_lastRawResponse;
48 private static $helperObject;
50 private $_unified_search_modules_content;
52 public function setUp()
54 global $beanList, $beanFiles;
55 include('include/modules.php');
57 //Reload langauge strings
58 $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
59 $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
60 $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], 'Accounts');
61 //Create an anonymous user for login purposes/
62 $this->_user = new User();
63 $this->_user->retrieve('1');
64 $GLOBALS['current_user'] = $this->_user;
66 self::$helperObject = new APIv3Helper();
69 if(file_exists(sugar_cached('modules/unified_search_modules.php')))
71 $this->unified_search_modules_content = file_get_contents(sugar_cached('modules/unified_search_modules.php'));
72 unlink(sugar_cached('modules/unified_search_modules.php'));
75 require_once('modules/Home/UnifiedSearchAdvanced.php');
76 $unifiedSearchAdvanced = new UnifiedSearchAdvanced();
77 $_REQUEST['enabled_modules'] = 'Accounts,Contacts,Opportunities';
78 $unifiedSearchAdvanced->saveGlobalSearchSettings();
80 $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
81 $GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
82 $GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
84 //$this->useOutputBuffering = false;
87 public function tearDown()
89 if(isset($GLOBALS['listViewDefs'])) unset($GLOBALS['listViewDefs']);
90 if(isset($GLOBALS['viewdefs'])) unset($GLOBALS['viewdefs']);
91 unset($GLOBALS['app_list_strings']);
92 unset($GLOBALS['app_strings']);
93 unset($GLOBALS['mod_strings']);
95 if(!empty($this->unified_search_modules_content))
97 file_put_contents(sugar_cached('modules/unified_search_modules.php'), $this->unified_search_modules_content);
100 $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
101 $GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
102 $GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
105 protected function _makeRESTCall($method,$parameters)
107 // specify the REST web service to interact with
108 $url = $GLOBALS['sugar_config']['site_url'].'/service/v3/rest.php';
109 // Open a curl session for making the call
110 $curl = curl_init($url);
111 // set URL and other appropriate options
112 curl_setopt($curl, CURLOPT_URL, $url);
113 curl_setopt($curl, CURLOPT_POST, 1);
114 curl_setopt($curl, CURLOPT_HEADER, 0);
115 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
116 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
117 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
118 curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
119 // build the request URL
120 $json = json_encode($parameters);
121 $postArgs = "method=$method&input_type=JSON&response_type=JSON&rest_data=$json";
122 curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
123 // Make the REST call, returning the result
124 $response = curl_exec($curl);
125 // Close the connection
128 $this->_lastRawResponse = $response;
130 // Convert the result from JSON format to a PHP array
131 return json_decode($response,true);
134 protected function _returnLastRawResponse()
136 return "Error in web services call. Response was: {$this->_lastRawResponse}";
139 protected function _login()
141 $GLOBALS['db']->commit(); // Making sure we commit any changes before logging in
143 return $this->_makeRESTCall('login',
147 'user_name' => $this->_user->user_name,
148 'password' => $this->_user->user_hash,
151 'application_name' => 'SugarTestRunner',
152 'name_value_list' => array(),
157 public function testSearchByModule()
159 $seedData = self::$helperObject->populateSeedDataForSearchTest($this->_user->id);
161 $searchModules = array('Accounts','Contacts','Opportunities');
162 $searchString = "UNIT TEST";
166 $result = $this->_login(); // Logging in just before the REST call as this will also commit any pending DB changes
167 $session = $result['id'];
168 $results = $this->_makeRESTCall('search_by_module',
170 'session' => $session,
171 'search' => $searchString,
172 'modules' => $searchModules,
174 'max' => $maxResults,
175 'user' => $this->_user->id)
178 $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts') );
179 $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts') );
180 $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts') );
181 $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities') );
182 $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities') );
185 public function testSearchByModuleWithReturnFields()
187 $seedData = self::$helperObject->populateSeedDataForSearchTest($this->_user->id);
189 $returnFields = array('name','id','deleted');
190 $searchModules = array('Accounts','Contacts','Opportunities');
191 $searchString = "UNIT TEST";
195 $result = $this->_login(); // Logging in just before the REST call as this will also commit any pending DB changes
196 $session = $result['id'];
197 $results = $this->_makeRESTCall('search_by_module',
199 'session' => $session,
200 'search' => $searchString,
201 'modules' => $searchModules,
203 'max' => $maxResults,
204 'user' => $this->_user->id,
205 'selectFields' => $returnFields)
209 $this->assertEquals($seedData[0]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts', $seedData[0]['fieldName']));
210 $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts', $seedData[1]['fieldName']));
211 $this->assertEquals($seedData[2]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts', $seedData[2]['fieldName']));
212 $this->assertEquals($seedData[3]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities', $seedData[3]['fieldName']));
213 $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities', $seedData[4]['fieldName']));
216 public function testGetServerInformation()
218 require('sugar_version.php');
220 $result = $this->_login();
221 $session = $result['id'];
223 $result = $this->_makeRESTCall('get_server_info',array());
225 $this->assertEquals($sugar_version, $result['version'],'Unable to get server information');
226 $this->assertEquals($sugar_flavor, $result['flavor'],'Unable to get server information');
229 public function testGetModuleList()
231 $account = new Account();
232 $account->id = uniqid();
233 $account->new_with_id = TRUE;
234 $account->name = "Test " . $account->id;
237 $whereClause = "accounts.name='{$account->name}'";
238 $module = 'Accounts';
241 $returnFields = array('name');
243 $result = $this->_login(); // Logging in just before the REST call as this will also commit any pending DB changes
244 $session = $result['id'];
245 $result = $this->_makeRESTCall('get_entry_list', array($session, $module, $whereClause, $orderBy,$offset, $returnFields));
247 $this->assertEquals($account->id, $result['entry_list'][0]['id'],'Unable to retrieve account list during search.');
249 $GLOBALS['db']->query("DELETE FROM accounts WHERE id = '{$account->id}'");
253 public function testLogin()
255 $result = $this->_login();
256 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
259 public static function _multipleModuleLayoutProvider()
263 'module' => array('Accounts','Contacts'),
264 'type' => array('default'),
265 'view' => array('list'),
266 'expected_file' => array(
267 'Accounts' => array( 'default' => array('list' => 'modules/Accounts/metadata/listviewdefs.php')),
268 'Contacts' => array( 'default' => array('list' => 'modules/Contacts/metadata/listviewdefs.php')))
271 'module' => array('Accounts','Contacts'),
272 'type' => array('default'),
273 'view' => array('list','detail'),
274 'expected_file' => array(
277 'list' => 'modules/Accounts/metadata/listviewdefs.php',
278 'detail' => 'modules/Accounts/metadata/detailviewdefs.php')),
281 'list' => 'modules/Contacts/metadata/listviewdefs.php',
282 'detail' => 'modules/Contacts/metadata/detailviewdefs.php'))
288 * @dataProvider _multipleModuleLayoutProvider
290 public function testGetMultipleModuleLayout($a_module, $a_type, $a_view, $a_expected_file)
292 $result = $this->_login();
293 $session = $result['id'];
295 $results = $this->_makeRESTCall('get_module_layout',
297 'session' => $session,
298 'module' => $a_module,
303 foreach ($results as $module => $moduleResults )
305 foreach ($moduleResults as $type => $viewResults)
307 foreach ($viewResults as $view => $result)
309 $expected_file = $a_expected_file[$module][$type][$view];
310 if ( is_file('custom' . DIRECTORY_SEPARATOR . $expected_file) )
311 require('custom' . DIRECTORY_SEPARATOR . $expected_file);
313 require($expected_file);
316 $expectedResults = $listViewDefs[$module];
318 $expectedResults = $viewdefs[$module][ucfirst($view) .'View' ];
320 $this->assertEquals(md5(serialize($expectedResults)), md5(serialize($result)), "Unable to retrieve module layout: module {$module}, type $type, view $view");
326 public static function _moduleLayoutProvider()
329 array('module' => 'Accounts','type' => 'default', 'view' => 'list','expected_file' => 'modules/Accounts/metadata/listviewdefs.php' ),
330 array('module' => 'Accounts','type' => 'default', 'view' => 'edit','expected_file' => 'modules/Accounts/metadata/editviewdefs.php' ),
331 array('module' => 'Accounts','type' => 'default', 'view' => 'detail','expected_file' => 'modules/Accounts/metadata/detailviewdefs.php' ),
336 * @dataProvider _moduleLayoutProvider
338 public function testGetModuleLayout($module, $type, $view, $expected_file)
340 $result = $this->_login();
341 $session = $result['id'];
343 $result = $this->_makeRESTCall('get_module_layout',
345 'session' => $session,
346 'module' => array($module),
347 'type' => array($type),
348 'view' => array($view))
351 if ( is_file('custom' . DIRECTORY_SEPARATOR . $expected_file) )
352 require('custom' . DIRECTORY_SEPARATOR . $expected_file);
354 require($expected_file);
357 $expectedResults = $listViewDefs[$module];
359 $expectedResults = $viewdefs[$module][ucfirst($view) .'View' ];
361 $a_expectedResults = array();
362 $a_expectedResults[$module][$type][$view] = $expectedResults;
364 $this->assertEquals(md5(serialize($a_expectedResults)), md5(serialize($result)), "Unable to retrieve module layout: module {$module}, type $type, view $view");
368 * @dataProvider _moduleLayoutProvider
370 public function testGetModuleLayoutMD5($module, $type, $view, $expected_file)
372 $result = $this->_login();
373 $session = $result['id'];
375 $fullResult = $this->_makeRESTCall('get_module_layout_md5',
377 'session' => $session,
378 'module' => array($module),
379 'type' => array($type),
380 'view' => array($view) )
382 $result = $fullResult['md5'];
383 if ( is_file('custom' . DIRECTORY_SEPARATOR . $expected_file) )
384 require('custom' . DIRECTORY_SEPARATOR . $expected_file);
386 require($expected_file);
389 $expectedResults = $listViewDefs[$module];
391 $expectedResults = $viewdefs[$module][ucfirst($view) .'View' ];
393 $a_expectedResults = array();
394 $a_expectedResults[$module][$type][$view] = $expectedResults;
396 $this->assertEquals(md5(serialize($expectedResults)), $result[$module][$type][$view], "Unable to retrieve module layout md5: module {$module}, type $type, view $view");
400 public function testGetAvailableModules()
402 $this->markTestSkipped('Will be updated week of June 21, 2010');
404 $result = $this->_login();
405 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
406 $session = $result['id'];
408 $fullResult = $this->_makeRESTCall('get_available_modules', array('session' => $session, 'filter' => 'all' ));
409 $this->assertTrue(in_array('ACLFields', $fullResult['modules']), "Unable to get all available modules");
410 $this->assertTrue(in_array('Schedulers', $fullResult['modules']), "Unable to get all available modules");
411 $this->assertTrue(in_array('Roles', $fullResult['modules']), "Unable to get all available modules");
413 $sh = new SugarWebServiceUtilv3();
415 $mobileResult = $this->_makeRESTCall('get_available_modules', array('session' => $session, 'filter' => 'mobile' ));
416 $mobileResultExpected = $sh->get_visible_mobile_modules($fullResult['modules']);
417 $mobileResultExpected = md5(serialize(array('modules' => $mobileResultExpected)));
418 $mobileResult = md5(serialize($mobileResult));
419 $this->assertEquals($mobileResultExpected, $mobileResult, "Unable to get all visible mobile modules");
421 $defaultResult = $this->_makeRESTCall('get_available_modules', array('session' => $session, 'filter' => 'default' ));
422 $defaultResult = md5(serialize($defaultResult['modules']));
423 $defaultResultExpected = $sh->get_visible_modules($fullResult['modules']);
424 $defaultResultExpected = md5(serialize($defaultResultExpected));
425 $this->assertEquals($defaultResultExpected, $defaultResult, "Unable to get all visible default modules");
429 public function testGetVardefsMD5()
431 $GLOBALS['reload_vardefs'] = TRUE;
432 $result = $this->_login();
433 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
434 $session = $result['id'];
436 //Test a regular module
437 $fullResult = $this->_makeRESTCall('get_module_fields_md5', array('session' => $session, 'module' => 'Accounts' ));
438 $result = $fullResult['Accounts'];
440 $soapHelper = new SugarWebServiceUtilv3();
441 $actualVardef = $soapHelper->get_return_module_fields($a,'Accounts','');
442 $actualMD5 = md5(serialize($actualVardef));
443 $this->assertEquals($actualMD5, $result, "Unable to retrieve vardef md5.");
446 $result = $this->_makeRESTCall('get_module_fields_md5', array('session' => $session, 'module' => 'BadModule' ));
447 $this->assertTrue($result['name'] == 'Module Does Not Exist');
448 unset($GLOBALS['reload_vardefs']);
451 public function testAddNewAccountAndThenDeleteIt()
453 $result = $this->_login();
454 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
455 $session = $result['id'];
457 $result = $this->_makeRESTCall('set_entry',
459 'session' => $session,
460 'module' => 'Accounts',
461 'name_value_list' => array(
462 array('name' => 'name', 'value' => 'New Account'),
463 array('name' => 'description', 'value' => 'This is an account created from a REST web services call'),
468 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
470 $accountId = $result['id'];
472 // verify record was created
473 $result = $this->_makeRESTCall('get_entry',
475 'session' => $session,
476 'module' => 'Accounts',
481 $this->assertEquals($result['entry_list'][0]['id'],$accountId,$this->_returnLastRawResponse());
484 $result = $this->_makeRESTCall('set_entry',
486 'session' => $session,
487 'module' => 'Accounts',
488 'name_value_list' => array(
489 array('name' => 'id', 'value' => $accountId),
490 array('name' => 'deleted', 'value' => '1'),
495 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
497 // try to retrieve again to validate it is deleted
498 $result = $this->_makeRESTCall('get_entry',
500 'session' => $session,
501 'module' => 'Accounts',
506 $GLOBALS['db']->query("DELETE FROM accounts WHERE id= '{$accountId}'");
508 $this->assertTrue(!empty($result['entry_list'][0]['id']) && $result['entry_list'][0]['id'] != -1,$this->_returnLastRawResponse());
509 $this->assertEquals($result['entry_list'][0]['name_value_list'][0]['name'],'warning',$this->_returnLastRawResponse());
510 $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());
511 $this->assertEquals($result['entry_list'][0]['name_value_list'][1]['name'],'deleted',$this->_returnLastRawResponse());
512 $this->assertEquals($result['entry_list'][0]['name_value_list'][1]['value'],1,$this->_returnLastRawResponse());
515 public function testRelateAccountToTwoContacts()
517 $result = $this->_login();
518 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
519 $session = $result['id'];
521 $result = $this->_makeRESTCall('set_entry',
523 'session' => $session,
524 'module' => 'Accounts',
525 'name_value_list' => array(
526 array('name' => 'name', 'value' => 'New Account'),
527 array('name' => 'description', 'value' => 'This is an account created from a REST web services call'),
532 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
534 $accountId = $result['id'];
536 $result = $this->_makeRESTCall('set_entry',
538 'session' => $session,
539 'module' => 'Contacts',
540 'name_value_list' => array(
541 array('name' => 'last_name', 'value' => 'New Contact 1'),
542 array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'),
547 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
549 $contactId1 = $result['id'];
551 $result = $this->_makeRESTCall('set_entry',
553 'session' => $session,
554 'module' => 'Contacts',
555 'name_value_list' => array(
556 array('name' => 'last_name', 'value' => 'New Contact 2'),
557 array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'),
562 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
564 $contactId2 = $result['id'];
566 // now relate them together
567 $result = $this->_makeRESTCall('set_relationship',
569 'session' => $session,
570 'module' => 'Accounts',
571 'module_id' => $accountId,
572 'link_field_name' => 'contacts',
573 'related_ids' => array($contactId1,$contactId2),
577 $this->assertEquals($result['created'],1,$this->_returnLastRawResponse());
579 // check the relationship
580 $result = $this->_makeRESTCall('get_relationships',
582 'session' => $session,
583 'module' => 'Accounts',
584 'module_id' => $accountId,
585 'link_field_name' => 'contacts',
586 'related_module_query' => '',
587 'related_fields' => array('last_name','description'),
588 'related_module_link_name_to_fields_array' => array(),
593 $returnedValues = array();
594 $returnedValues[] = $result['entry_list'][0]['name_value_list']['last_name']['value'];
595 $returnedValues[] = $result['entry_list'][1]['name_value_list']['last_name']['value'];
597 $GLOBALS['db']->query("DELETE FROM accounts WHERE id= '{$accountId}'");
598 $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$contactId1}'");
599 $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$contactId2}'");
600 $GLOBALS['db']->query("DELETE FROM accounts_contacts WHERE account_id= '{$accountId}'");
602 $this->assertContains('New Contact 1',$returnedValues,$this->_returnLastRawResponse());
603 $this->assertContains('New Contact 2',$returnedValues,$this->_returnLastRawResponse());
609 public function testOrderByClauseOfGetRelationship()
611 $result = $this->_login();
612 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
613 $session = $result['id'];
615 $result = $this->_makeRESTCall('set_entry',
617 'session' => $session,
618 'module' => 'Accounts',
619 'name_value_list' => array(
620 array('name' => 'name', 'value' => 'New Account'),
621 array('name' => 'description', 'value' => 'This is an account created from a REST web services call'),
626 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
628 $accountId = $result['id'];
630 $result = $this->_makeRESTCall('set_entry',
632 'session' => $session,
633 'module' => 'Contacts',
634 'name_value_list' => array(
635 array('name' => 'last_name', 'value' => 'New Contact 1'),
636 array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'),
641 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
643 $contactId1 = $result['id'];
645 $result = $this->_makeRESTCall('set_entry',
647 'session' => $session,
648 'module' => 'Contacts',
649 'name_value_list' => array(
650 array('name' => 'last_name', 'value' => 'New Contact 3'),
651 array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'),
656 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
657 $contactId3 = $result['id'];
659 $result = $this->_makeRESTCall('set_entry',
661 'session' => $session,
662 'module' => 'Contacts',
663 'name_value_list' => array(
664 array('name' => 'last_name', 'value' => 'New Contact 2'),
665 array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'),
670 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
672 $contactId2 = $result['id'];
674 // now relate them together
675 $result = $this->_makeRESTCall('set_relationship',
677 'session' => $session,
678 'module' => 'Accounts',
679 'module_id' => $accountId,
680 'link_field_name' => 'contacts',
681 'related_ids' => array($contactId1,$contactId3,$contactId2),
685 $this->assertEquals($result['created'],1,$this->_returnLastRawResponse());
687 // check the relationship
688 $result = $this->_makeRESTCall('get_relationships',
690 'session' => $session,
691 'module' => 'Accounts',
692 'module_id' => $accountId,
693 'link_field_name' => 'contacts',
694 'related_module_query' => '',
695 'related_fields' => array('last_name','description'),
696 'related_module_link_name_to_fields_array' => array(),
698 'order_by' => 'last_name',
702 $GLOBALS['db']->query("DELETE FROM accounts WHERE id= '{$accountId}'");
703 $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$contactId1}'");
704 $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$contactId2}'");
705 $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$contactId3}'");
706 $GLOBALS['db']->query("DELETE FROM accounts_contacts WHERE account_id= '{$accountId}'");
708 $this->assertEquals($result['entry_list'][0]['name_value_list']['last_name']['value'],'New Contact 1',$this->_returnLastRawResponse());
709 $this->assertEquals($result['entry_list'][1]['name_value_list']['last_name']['value'],'New Contact 2',$this->_returnLastRawResponse());
710 $this->assertEquals($result['entry_list'][2]['name_value_list']['last_name']['value'],'New Contact 3',$this->_returnLastRawResponse());
713 public static function _subpanelLayoutProvider()
717 'module' => 'Contacts',
719 'view' => 'subpanel',
723 'type' => 'wireless',
724 'view' => 'subpanel',
730 * @dataProvider _subpanelLayoutProvider
732 public function testGetSubpanelLayout($module, $type, $view)
734 $result = $this->_login();
735 $session = $result['id'];
737 $results = $this->_makeRESTCall('get_module_layout',
739 'session' => $session,
740 'module' => array($module),
741 'type' => array($type),
742 'view' => array($view))
745 $this->assertTrue(isset($results[$module][$type][$view]), "Unable to get subpanel defs");
747 public function testGetUpcomingActivities()
749 $expected = $this->_createUpcomingActivities(); //Seed the data.
751 $result = $this->_login(); // Logging in just before the REST call as this will also commit any pending DB changes
752 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
753 $session = $result['id'];
754 $results = $this->_makeRESTCall('get_upcoming_activities',
756 'session' => $session,
760 $this->assertEquals($expected[0] ,$results[0]['id'] , "Unable to get upcoming activities");
761 $this->assertEquals($expected[1] ,$results[1]['id'] , "Unable to get upcoming activities");
763 $this->_removeUpcomingActivities();
766 private function _removeUpcomingActivities()
768 $GLOBALS['db']->query("DELETE FROM calls where name = 'UNIT TEST'");
769 $GLOBALS['db']->query("DELETE FROM tasks where name = 'UNIT TEST'");
772 private function _createUpcomingActivities()
774 $GLOBALS['current_user']->setPreference('datef','Y-m-d') ;
775 $GLOBALS['current_user']->setPreference('timef','H:i') ;
777 $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
778 $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
783 $c->new_with_id = TRUE;
784 $c->status = 'Not Planned';
785 $c->date_start = $date1;
786 $c->name = "UNIT TEST";
787 $c->assigned_user_id = $this->_user->id;
793 $c->new_with_id = TRUE;
794 $c->status = 'Planned';
795 $c->date_start = $date1;
796 $c->name = "UNIT TEST";
797 $c->assigned_user_id = $this->_user->id;
803 $t->new_with_id = TRUE;
804 $t->status = 'Not Started';
805 $t->date_due = $date2;
806 $t->name = "UNIT TEST";
807 $t->assigned_user_id = $this->_user->id;
810 return array($callID, $taskID);