2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 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 = SugarTestUserUtilities::createAnonymousUser();
63 $GLOBALS['current_user'] = $this->_user;
65 self::$helperObject = new APIv3Helper();
67 if(file_exists(sugar_cached('modules/unified_search_modules.php')))
69 $this->unified_search_modules_content = file_get_contents(sugar_cached('modules/unified_search_modules.php'));
70 unlink(sugar_cached('modules/unified_search_modules.php'));
73 require_once('modules/Home/UnifiedSearchAdvanced.php');
74 $unifiedSearchAdvanced = new UnifiedSearchAdvanced();
75 $_REQUEST['enabled_modules'] = 'Accounts,Contacts,Opportunities';
76 $unifiedSearchAdvanced->saveGlobalSearchSettings();
78 $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
79 $GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
80 $GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
81 $GLOBALS['db']->query("DELETE FROM calls WHERE name like 'UNIT TEST%' ");
82 $GLOBALS['db']->query("DELETE FROM tasks WHERE name like 'UNIT TEST%' ");
83 $GLOBALS['db']->query("DELETE FROM meetings WHERE name like 'UNIT TEST%' ");
84 $GLOBALS['db']->commit();
85 //$this->useOutputBuffering = false;
88 public function tearDown()
90 if(isset($GLOBALS['listViewDefs'])) unset($GLOBALS['listViewDefs']);
91 if(isset($GLOBALS['viewdefs'])) unset($GLOBALS['viewdefs']);
92 unset($GLOBALS['app_list_strings']);
93 unset($GLOBALS['app_strings']);
94 unset($GLOBALS['mod_strings']);
96 if(!empty($this->unified_search_modules_content))
98 file_put_contents(sugar_cached('modules/unified_search_modules.php'), $this->unified_search_modules_content);
101 $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
102 $GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
103 $GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
104 $GLOBALS['db']->query("DELETE FROM calls WHERE name like 'UNIT TEST%' ");
105 $GLOBALS['db']->query("DELETE FROM tasks WHERE name like 'UNIT TEST%' ");
106 $GLOBALS['db']->query("DELETE FROM meetings WHERE name like 'UNIT TEST%' ");
107 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
108 unset($GLOBALS['reload_vardefs']);
111 protected function _makeRESTCall($method,$parameters)
113 // specify the REST web service to interact with
114 $url = $GLOBALS['sugar_config']['site_url'].'/service/v3/rest.php';
115 // Open a curl session for making the call
116 $curl = curl_init($url);
117 // set URL and other appropriate options
118 curl_setopt($curl, CURLOPT_URL, $url);
119 curl_setopt($curl, CURLOPT_POST, 1);
120 curl_setopt($curl, CURLOPT_HEADER, 0);
121 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
122 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
123 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
124 curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
125 // build the request URL
126 $json = json_encode($parameters);
127 $postArgs = "method=$method&input_type=JSON&response_type=JSON&rest_data=$json";
128 curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
129 // Make the REST call, returning the result
130 $response = curl_exec($curl);
131 // Close the connection
134 $this->_lastRawResponse = $response;
136 // Convert the result from JSON format to a PHP array
137 return json_decode($response,true);
140 protected function _returnLastRawResponse()
142 return "Error in web services call. Response was: {$this->_lastRawResponse}";
145 protected function _login()
147 $GLOBALS['db']->commit(); // Making sure we commit any changes before logging in
149 return $this->_makeRESTCall('login',
153 'user_name' => $this->_user->user_name,
154 'password' => $this->_user->user_hash,
157 'application_name' => 'SugarTestRunner',
158 'name_value_list' => array(),
163 public function testSearchByModule()
165 $seedData = self::$helperObject->populateSeedDataForSearchTest($this->_user->id);
167 $searchModules = array('Accounts','Contacts','Opportunities');
168 $searchString = "UNIT TEST";
172 $result = $this->_login(); // Logging in just before the REST call as this will also commit any pending DB changes
173 $session = $result['id'];
174 $results = $this->_makeRESTCall('search_by_module',
176 'session' => $session,
177 'search' => $searchString,
178 'modules' => $searchModules,
180 'max' => $maxResults,
181 'user' => $this->_user->id)
184 $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts') );
185 $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts') );
186 $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts') );
187 $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities') );
188 $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities') );
191 public function testSearchByModuleWithReturnFields()
193 $seedData = self::$helperObject->populateSeedDataForSearchTest($this->_user->id);
195 $returnFields = array('name','id','deleted');
196 $searchModules = array('Accounts','Contacts','Opportunities');
197 $searchString = "UNIT TEST";
201 $result = $this->_login(); // Logging in just before the REST call as this will also commit any pending DB changes
202 $session = $result['id'];
203 $results = $this->_makeRESTCall('search_by_module',
205 'session' => $session,
206 'search' => $searchString,
207 'modules' => $searchModules,
209 'max' => $maxResults,
210 'user' => $this->_user->id,
211 'selectFields' => $returnFields)
215 $this->assertEquals($seedData[0]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts', $seedData[0]['fieldName']));
216 $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts', $seedData[1]['fieldName']));
217 $this->assertEquals($seedData[2]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts', $seedData[2]['fieldName']));
218 $this->assertEquals($seedData[3]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities', $seedData[3]['fieldName']));
219 $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities', $seedData[4]['fieldName']));
222 public function testGetServerInformation()
224 require('sugar_version.php');
226 $result = $this->_login();
227 $session = $result['id'];
229 $result = $this->_makeRESTCall('get_server_info',array());
231 $this->assertEquals($sugar_version, $result['version'],'Unable to get server information');
232 $this->assertEquals($sugar_flavor, $result['flavor'],'Unable to get server information');
235 public function testGetModuleList()
237 $account = new Account();
238 $account->id = uniqid();
239 $account->new_with_id = TRUE;
240 $account->name = "Test " . $account->id;
243 $whereClause = "accounts.name='{$account->name}'";
244 $module = 'Accounts';
247 $returnFields = array('name');
249 $result = $this->_login(); // Logging in just before the REST call as this will also commit any pending DB changes
250 $session = $result['id'];
251 $result = $this->_makeRESTCall('get_entry_list', array($session, $module, $whereClause, $orderBy,$offset, $returnFields));
253 $this->assertEquals($account->id, $result['entry_list'][0]['id'],'Unable to retrieve account list during search.');
255 $GLOBALS['db']->query("DELETE FROM accounts WHERE id = '{$account->id}'");
259 public function testLogin()
261 $result = $this->_login();
262 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
265 public static function _multipleModuleLayoutProvider()
269 'module' => array('Accounts','Contacts'),
270 'type' => array('default'),
271 'view' => array('list'),
272 'expected_file' => array(
273 'Accounts' => array( 'default' => array('list' => 'modules/Accounts/metadata/listviewdefs.php')),
274 'Contacts' => array( 'default' => array('list' => 'modules/Contacts/metadata/listviewdefs.php')))
277 'module' => array('Accounts','Contacts'),
278 'type' => array('default'),
279 'view' => array('list','detail'),
280 'expected_file' => array(
283 'list' => 'modules/Accounts/metadata/listviewdefs.php',
284 'detail' => 'modules/Accounts/metadata/detailviewdefs.php')),
287 'list' => 'modules/Contacts/metadata/listviewdefs.php',
288 'detail' => 'modules/Contacts/metadata/detailviewdefs.php'))
294 * @dataProvider _multipleModuleLayoutProvider
296 public function testGetMultipleModuleLayout($a_module, $a_type, $a_view, $a_expected_file)
298 $result = $this->_login();
299 $session = $result['id'];
301 $results = $this->_makeRESTCall('get_module_layout',
303 'session' => $session,
304 'module' => $a_module,
309 foreach ($results as $module => $moduleResults )
311 foreach ($moduleResults as $type => $viewResults)
313 foreach ($viewResults as $view => $result)
315 $expected_file = $a_expected_file[$module][$type][$view];
316 if ( is_file('custom' . DIRECTORY_SEPARATOR . $expected_file) )
317 require('custom' . DIRECTORY_SEPARATOR . $expected_file);
319 require($expected_file);
322 $expectedResults = $listViewDefs[$module];
324 $expectedResults = $viewdefs[$module][ucfirst($view) .'View' ];
326 $this->assertEquals(md5(serialize($expectedResults)), md5(serialize($result)), "Unable to retrieve module layout: module {$module}, type $type, view $view");
332 public static function _moduleLayoutProvider()
335 array('module' => 'Accounts','type' => 'default', 'view' => 'list','expected_file' => 'modules/Accounts/metadata/listviewdefs.php' ),
336 array('module' => 'Accounts','type' => 'default', 'view' => 'edit','expected_file' => 'modules/Accounts/metadata/editviewdefs.php' ),
337 array('module' => 'Accounts','type' => 'default', 'view' => 'detail','expected_file' => 'modules/Accounts/metadata/detailviewdefs.php' ),
342 * @dataProvider _moduleLayoutProvider
344 public function testGetModuleLayout($module, $type, $view, $expected_file)
346 $result = $this->_login();
347 $session = $result['id'];
349 $result = $this->_makeRESTCall('get_module_layout',
351 'session' => $session,
352 'module' => array($module),
353 'type' => array($type),
354 'view' => array($view))
357 if ( is_file('custom' . DIRECTORY_SEPARATOR . $expected_file) )
358 require('custom' . DIRECTORY_SEPARATOR . $expected_file);
360 require($expected_file);
363 $expectedResults = $listViewDefs[$module];
365 $expectedResults = $viewdefs[$module][ucfirst($view) .'View' ];
367 $a_expectedResults = array();
368 $a_expectedResults[$module][$type][$view] = $expectedResults;
370 $this->assertEquals(md5(serialize($a_expectedResults)), md5(serialize($result)), "Unable to retrieve module layout: module {$module}, type $type, view $view");
374 * @dataProvider _moduleLayoutProvider
376 public function testGetModuleLayoutMD5($module, $type, $view, $expected_file)
378 $result = $this->_login();
379 $session = $result['id'];
381 $fullResult = $this->_makeRESTCall('get_module_layout_md5',
383 'session' => $session,
384 'module' => array($module),
385 'type' => array($type),
386 'view' => array($view) )
388 $result = $fullResult['md5'];
389 if ( is_file('custom' . DIRECTORY_SEPARATOR . $expected_file) )
390 require('custom' . DIRECTORY_SEPARATOR . $expected_file);
392 require($expected_file);
395 $expectedResults = $listViewDefs[$module];
397 $expectedResults = $viewdefs[$module][ucfirst($view) .'View' ];
399 $a_expectedResults = array();
400 $a_expectedResults[$module][$type][$view] = $expectedResults;
402 $this->assertEquals(md5(serialize($expectedResults)), $result[$module][$type][$view], "Unable to retrieve module layout md5: module {$module}, type $type, view $view");
406 public function testGetAvailableModules()
408 $this->markTestIncomplete('Will be updated week of June 21, 2010');
410 $result = $this->_login();
411 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
412 $session = $result['id'];
414 $fullResult = $this->_makeRESTCall('get_available_modules', array('session' => $session, 'filter' => 'all' ));
415 $this->assertTrue(in_array('ACLFields', $fullResult['modules']), "Unable to get all available modules");
416 $this->assertTrue(in_array('Schedulers', $fullResult['modules']), "Unable to get all available modules");
417 $this->assertTrue(in_array('Roles', $fullResult['modules']), "Unable to get all available modules");
419 $sh = new SugarWebServiceUtilv3();
421 $mobileResult = $this->_makeRESTCall('get_available_modules', array('session' => $session, 'filter' => 'mobile' ));
422 $mobileResultExpected = $sh->get_visible_mobile_modules($fullResult['modules']);
423 $mobileResultExpected = md5(serialize(array('modules' => $mobileResultExpected)));
424 $mobileResult = md5(serialize($mobileResult));
425 $this->assertEquals($mobileResultExpected, $mobileResult, "Unable to get all visible mobile modules");
427 $defaultResult = $this->_makeRESTCall('get_available_modules', array('session' => $session, 'filter' => 'default' ));
428 $defaultResult = md5(serialize($defaultResult['modules']));
429 $defaultResultExpected = $sh->get_visible_modules($fullResult['modules']);
430 $defaultResultExpected = md5(serialize($defaultResultExpected));
431 $this->assertEquals($defaultResultExpected, $defaultResult, "Unable to get all visible default modules");
435 public function testGetVardefsMD5()
437 $GLOBALS['reload_vardefs'] = TRUE;
438 $result = $this->_login();
439 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
440 $session = $result['id'];
442 //Test a regular module
443 $fullResult = $this->_makeRESTCall('get_module_fields_md5', array('session' => $session, 'module' => 'Currencies' ));
444 $result = $fullResult['Currencies'];
446 $soapHelper = new SugarWebServiceUtilv3();
447 $actualVardef = $soapHelper->get_return_module_fields($a,'Currencies','');
448 $actualMD5 = md5(serialize($actualVardef));
449 $this->assertEquals($actualMD5, $result, "Unable to retrieve vardef md5.");
452 $result = $this->_makeRESTCall('get_module_fields_md5', array('session' => $session, 'module' => 'BadModule' ));
453 $this->assertEquals('Module Does Not Exist', $result['name']);
456 public function testAddNewAccountAndThenDeleteIt()
458 $result = $this->_login();
459 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
460 $session = $result['id'];
462 $result = $this->_makeRESTCall('set_entry',
464 'session' => $session,
465 'module' => 'Accounts',
466 'name_value_list' => array(
467 array('name' => 'name', 'value' => 'New Account'),
468 array('name' => 'description', 'value' => 'This is an account created from a REST web services call'),
473 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
475 $accountId = $result['id'];
477 // verify record was created
478 $result = $this->_makeRESTCall('get_entry',
480 'session' => $session,
481 'module' => 'Accounts',
486 $this->assertEquals($result['entry_list'][0]['id'],$accountId,$this->_returnLastRawResponse());
489 $result = $this->_makeRESTCall('set_entry',
491 'session' => $session,
492 'module' => 'Accounts',
493 'name_value_list' => array(
494 array('name' => 'id', 'value' => $accountId),
495 array('name' => 'deleted', 'value' => '1'),
500 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
502 // try to retrieve again to validate it is deleted
503 $result = $this->_makeRESTCall('get_entry',
505 'session' => $session,
506 'module' => 'Accounts',
511 $GLOBALS['db']->query("DELETE FROM accounts WHERE id= '{$accountId}'");
513 $this->assertTrue(!empty($result['entry_list'][0]['id']) && $result['entry_list'][0]['id'] != -1,$this->_returnLastRawResponse());
514 $this->assertEquals($result['entry_list'][0]['name_value_list'][0]['name'],'warning',$this->_returnLastRawResponse());
515 $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());
516 $this->assertEquals($result['entry_list'][0]['name_value_list'][1]['name'],'deleted',$this->_returnLastRawResponse());
517 $this->assertEquals($result['entry_list'][0]['name_value_list'][1]['value'],1,$this->_returnLastRawResponse());
520 public function testRelateAccountToTwoContacts()
522 $result = $this->_login();
523 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
524 $session = $result['id'];
526 $result = $this->_makeRESTCall('set_entry',
528 'session' => $session,
529 'module' => 'Accounts',
530 'name_value_list' => array(
531 array('name' => 'name', 'value' => 'New Account'),
532 array('name' => 'description', 'value' => 'This is an account created from a REST web services call'),
537 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
539 $accountId = $result['id'];
541 $result = $this->_makeRESTCall('set_entry',
543 'session' => $session,
544 'module' => 'Contacts',
545 'name_value_list' => array(
546 array('name' => 'last_name', 'value' => 'New Contact 1'),
547 array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'),
552 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
554 $contactId1 = $result['id'];
556 $result = $this->_makeRESTCall('set_entry',
558 'session' => $session,
559 'module' => 'Contacts',
560 'name_value_list' => array(
561 array('name' => 'last_name', 'value' => 'New Contact 2'),
562 array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'),
567 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
569 $contactId2 = $result['id'];
571 // now relate them together
572 $result = $this->_makeRESTCall('set_relationship',
574 'session' => $session,
575 'module' => 'Accounts',
576 'module_id' => $accountId,
577 'link_field_name' => 'contacts',
578 'related_ids' => array($contactId1,$contactId2),
582 $this->assertEquals($result['created'],1,$this->_returnLastRawResponse());
584 // check the relationship
585 $result = $this->_makeRESTCall('get_relationships',
587 'session' => $session,
588 'module' => 'Accounts',
589 'module_id' => $accountId,
590 'link_field_name' => 'contacts',
591 'related_module_query' => '',
592 'related_fields' => array('last_name','description'),
593 'related_module_link_name_to_fields_array' => array(),
598 $returnedValues = array();
599 $returnedValues[] = $result['entry_list'][0]['name_value_list']['last_name']['value'];
600 $returnedValues[] = $result['entry_list'][1]['name_value_list']['last_name']['value'];
602 $GLOBALS['db']->query("DELETE FROM accounts WHERE id= '{$accountId}'");
603 $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$contactId1}'");
604 $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$contactId2}'");
605 $GLOBALS['db']->query("DELETE FROM accounts_contacts WHERE account_id= '{$accountId}'");
607 $this->assertContains('New Contact 1',$returnedValues,$this->_returnLastRawResponse());
608 $this->assertContains('New Contact 2',$returnedValues,$this->_returnLastRawResponse());
614 public function testOrderByClauseOfGetRelationship()
616 $result = $this->_login();
617 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
618 $session = $result['id'];
620 $result = $this->_makeRESTCall('set_entry',
622 'session' => $session,
623 'module' => 'Accounts',
624 'name_value_list' => array(
625 array('name' => 'name', 'value' => 'New Account'),
626 array('name' => 'description', 'value' => 'This is an account created from a REST web services call'),
631 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
633 $accountId = $result['id'];
635 $result = $this->_makeRESTCall('set_entry',
637 'session' => $session,
638 'module' => 'Contacts',
639 'name_value_list' => array(
640 array('name' => 'last_name', 'value' => 'New Contact 1'),
641 array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'),
646 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
648 $contactId1 = $result['id'];
650 $result = $this->_makeRESTCall('set_entry',
652 'session' => $session,
653 'module' => 'Contacts',
654 'name_value_list' => array(
655 array('name' => 'last_name', 'value' => 'New Contact 3'),
656 array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'),
661 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
662 $contactId3 = $result['id'];
664 $result = $this->_makeRESTCall('set_entry',
666 'session' => $session,
667 'module' => 'Contacts',
668 'name_value_list' => array(
669 array('name' => 'last_name', 'value' => 'New Contact 2'),
670 array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'),
675 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
677 $contactId2 = $result['id'];
679 // now relate them together
680 $result = $this->_makeRESTCall('set_relationship',
682 'session' => $session,
683 'module' => 'Accounts',
684 'module_id' => $accountId,
685 'link_field_name' => 'contacts',
686 'related_ids' => array($contactId1,$contactId3,$contactId2),
690 $this->assertEquals($result['created'],1,$this->_returnLastRawResponse());
692 // check the relationship
693 $result = $this->_makeRESTCall('get_relationships',
695 'session' => $session,
696 'module' => 'Accounts',
697 'module_id' => $accountId,
698 'link_field_name' => 'contacts',
699 'related_module_query' => '',
700 'related_fields' => array('last_name','description'),
701 'related_module_link_name_to_fields_array' => array(),
703 'order_by' => 'last_name',
707 $GLOBALS['db']->query("DELETE FROM accounts WHERE id= '{$accountId}'");
708 $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$contactId1}'");
709 $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$contactId2}'");
710 $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$contactId3}'");
711 $GLOBALS['db']->query("DELETE FROM accounts_contacts WHERE account_id= '{$accountId}'");
713 $this->assertEquals($result['entry_list'][0]['name_value_list']['last_name']['value'],'New Contact 1',$this->_returnLastRawResponse());
714 $this->assertEquals($result['entry_list'][1]['name_value_list']['last_name']['value'],'New Contact 2',$this->_returnLastRawResponse());
715 $this->assertEquals($result['entry_list'][2]['name_value_list']['last_name']['value'],'New Contact 3',$this->_returnLastRawResponse());
718 public static function _subpanelLayoutProvider()
722 'module' => 'Contacts',
724 'view' => 'subpanel',
728 'type' => 'wireless',
729 'view' => 'subpanel',
735 * @dataProvider _subpanelLayoutProvider
737 public function testGetSubpanelLayout($module, $type, $view)
739 $result = $this->_login();
740 $session = $result['id'];
742 $results = $this->_makeRESTCall('get_module_layout',
744 'session' => $session,
745 'module' => array($module),
746 'type' => array($type),
747 'view' => array($view))
750 $this->assertTrue(isset($results[$module][$type][$view]), "Unable to get subpanel defs");
752 public function testGetUpcomingActivities()
754 $expected = $this->_createUpcomingActivities(); //Seed the data.
756 $result = $this->_login(); // Logging in just before the REST call as this will also commit any pending DB changes
757 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
758 $session = $result['id'];
759 $results = $this->_makeRESTCall('get_upcoming_activities',
761 'session' => $session,
766 foreach($results as $activity)
768 $ids[$activity['id']] = $activity['id'];
771 $this->assertArrayHasKey($expected[0] , $ids , "Unable to get upcoming activities");
772 $this->assertArrayHasKey($expected[1] ,$ids , "Unable to get upcoming activities");
774 $this->_removeUpcomingActivities();
777 private function _removeUpcomingActivities()
779 $GLOBALS['db']->query("DELETE FROM calls where name = 'UNIT TEST'");
780 $GLOBALS['db']->query("DELETE FROM tasks where name = 'UNIT TEST'");
783 private function _createUpcomingActivities()
785 $GLOBALS['current_user']->setPreference('datef','Y-m-d') ;
786 $GLOBALS['current_user']->setPreference('timef','H:i') ;
788 $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
789 $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
794 $c->new_with_id = TRUE;
795 $c->status = 'Not Planned';
796 $c->date_start = $date1;
797 $c->name = "UNIT TEST";
798 $c->assigned_user_id = $this->_user->id;
804 $c->new_with_id = TRUE;
805 $c->status = 'Planned';
806 $c->date_start = $date1;
807 $c->name = "UNIT TEST";
808 $c->assigned_user_id = $this->_user->id;
814 $t->new_with_id = TRUE;
815 $t->status = 'Not Started';
816 $t->date_due = $date2;
817 $t->name = "UNIT TEST";
818 $t->assigned_user_id = $this->_user->id;
821 return array($callID, $taskID);